8a2c929674405dcd536cd98a900e0d145460aaf7
[gnus] / lisp / nnspool.el
1 ;;; nnspool.el --- spool access for GNU Emacs
2 ;; Copyright (C) 1988,89,90,93,94,95 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'nnheader)
29 (require 'nntp)
30 (require 'timezone)
31
32 (defvar nnspool-inews-program news-inews-program
33   "Program to post news.")
34
35 (defvar nnspool-inews-switches '("-h")
36   "Switches for nnspool-request-post to pass to `inews' for posting news.")
37
38 (defvar nnspool-spool-directory news-path
39   "Local news spool directory.")
40
41 (defvar nnspool-nov-directory (concat nnspool-spool-directory "over.view/")
42   "Local news nov directory.")
43
44 (defvar nnspool-lib-dir "/usr/lib/news/"
45   "Where the local news library files are stored.")
46
47 (defvar nnspool-active-file (concat nnspool-lib-dir "active")
48   "Local news active file.")
49
50 (defvar nnspool-newsgroups-file (concat nnspool-lib-dir "newsgroups")
51   "Local news newsgroups file.")
52
53 (defvar nnspool-distributions-file (concat nnspool-lib-dir "distributions")
54   "Local news distributions file.")
55
56 (defvar nnspool-history-file (concat nnspool-lib-dir "history")
57   "Local news history file.")
58
59 (defvar nnspool-active-times-file (concat nnspool-lib-dir "active.times")
60   "Local news active date file.")
61
62 (defvar nnspool-large-newsgroup 50
63   "The number of the articles which indicates a large newsgroup.
64 If the number of the articles is greater than the value, verbose
65 messages will be shown to indicate the current status.")
66
67 (defvar nnspool-nov-is-evil nil
68   "Non-nil means that nnspool will never return NOV lines instead of headers.")
69
70 (defvar nnspool-sift-nov-with-sed t
71   "If non-nil, use sed to get the relevant portion from the overview file.
72 If nil, nnspool will load the entire file into a buffer and process it
73 there.")
74
75 \f
76
77 (defconst nnspool-version "nnspool 2.0"
78   "Version numbers of this version of NNSPOOL.")
79
80 (defvar nnspool-current-directory nil
81   "Current news group directory.")
82
83 (defvar nnspool-current-group nil)
84 (defvar nnspool-status-string "")
85
86 \f
87
88 (defvar nnspool-current-server nil)
89 (defvar nnspool-server-alist nil)
90 (defvar nnspool-server-variables 
91   (list
92    (list 'nnspool-inews-program nnspool-inews-program)
93    (list 'nnspool-inews-switches nnspool-inews-switches)
94    (list 'nnspool-spool-directory nnspool-spool-directory)
95    (list 'nnspool-nov-directory nnspool-nov-directory)
96    (list 'nnspool-lib-dir nnspool-lib-dir)
97    (list 'nnspool-active-file nnspool-active-file)
98    (list 'nnspool-newsgroups-file nnspool-newsgroups-file)
99    (list 'nnspool-distributions-file nnspool-distributions-file)
100    (list 'nnspool-history-file nnspool-history-file)
101    (list 'nnspool-active-times-file nnspool-active-times-file)
102    (list 'nnspool-large-newsgroup nnspool-large-newsgroup)
103    (list 'nnspool-nov-is-evil nnspool-nov-is-evil)
104    (list 'nnspool-sift-nov-with-sed nnspool-sift-nov-with-sed)
105    '(nnspool-current-directory nil)
106    '(nnspool-current-group nil)
107    '(nnspool-status-string "")))
108
109 \f
110 ;;; Interface functions.
111
112 (defun nnspool-retrieve-headers (sequence &optional newsgroup server)
113   "Retrieve the headers for the articles in SEQUENCE.
114 Newsgroup must be selected before calling this function."
115   (save-excursion
116     (set-buffer nntp-server-buffer)
117     (erase-buffer)
118     (let* ((number (length sequence))
119            (count 0)
120            (do-message (and (numberp nnspool-large-newsgroup)
121                             (> number nnspool-large-newsgroup)))
122            file beg article)
123       (if (not (nnspool-possibly-change-directory newsgroup))
124           ()
125         (if (nnspool-retrieve-headers-with-nov sequence)
126             'nov
127           (while sequence
128             (setq article (car sequence))
129             (if (stringp article)
130                 (progn
131                   (format "221 %d Article retrieved.\n" 0)
132                   (nnspool-request-article article))
133               (setq file (concat nnspool-current-directory 
134                                  (int-to-string article)))
135               (and (file-exists-p file)
136                    (progn
137                      (insert (format "221 %d Article retrieved.\n" article))
138                      (setq beg (point))
139                      (insert-file-contents file)
140                      (goto-char beg)
141                      (search-forward "\n\n" nil t)
142                      (forward-char -1)
143                      (insert ".\n")
144                      (delete-region (point) (point-max)))))
145             (setq sequence (cdr sequence))
146             
147             (and do-message
148                  (zerop (% (setq count (1+ count)) 20))
149                  (message "NNSPOOL: Receiving headers... %d%%"
150                           (/ (* count 100) number))))
151           
152           (and do-message (message "NNSPOOL: Receiving headers... done"))
153           
154           ;; Fold continuation lines.
155           (goto-char 1)
156           (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
157             (replace-match " " t t))
158           'headers)))))
159
160 (defun nnspool-open-server (server &optional defs)
161   (nnheader-init-server-buffer)
162   (if (equal server nnspool-current-server)
163       t
164     (if nnspool-current-server
165         (setq nnspool-server-alist 
166               (cons (list nnspool-current-server
167                           (nnheader-save-variables nnspool-server-variables))
168                     nnspool-server-alist)))
169     (let ((state (assoc server nnspool-server-alist)))
170       (if state 
171           (progn
172             (nnheader-restore-variables (nth 1 state))
173             (setq nnspool-server-alist (delq state nnspool-server-alist)))
174         (nnheader-set-init-variables nnspool-server-variables defs)))
175     (setq nnspool-current-server server)))
176
177 (defun nnspool-close-server (&optional server)
178   t)
179
180 (defun nnspool-server-opened (&optional server)
181   (and (equal server nnspool-current-server)
182        nntp-server-buffer
183        (buffer-name nntp-server-buffer)))
184
185 (defun nnspool-status-message (&optional server)
186   "Return server status response as string."
187   nnspool-status-string)
188
189 (defun nnspool-request-article (id &optional newsgroup server buffer)
190   "Select article by message ID (or number)."
191   (nnspool-possibly-change-directory newsgroup)
192   (let ((file (if (stringp id)
193                   (nnspool-find-article-by-message-id id)
194                 (concat nnspool-current-directory (prin1-to-string id))))
195         (nntp-server-buffer (or buffer nntp-server-buffer)))
196     (if (and (stringp file)
197              (file-exists-p file)
198              (not (file-directory-p file)))
199         (save-excursion
200           (nnspool-find-file file)))))
201
202 (defun nnspool-request-body (id &optional newsgroup server)
203   "Select article body by message ID (or number)."
204   (nnspool-possibly-change-directory newsgroup)
205   (if (nnspool-request-article id)
206       (save-excursion
207         (set-buffer nntp-server-buffer)
208         (goto-char (point-min))
209         (if (search-forward "\n\n" nil t)
210             (delete-region (point-min) (point)))
211         t)))
212
213 (defun nnspool-request-head (id &optional newsgroup server)
214   "Select article head by message ID (or number)."
215   (nnspool-possibly-change-directory newsgroup)
216   (if (nnspool-request-article id)
217       (save-excursion
218         (set-buffer nntp-server-buffer)
219         (goto-char (point-min))
220         (if (search-forward "\n\n" nil t)
221             (delete-region (1- (point)) (point-max)))
222         t)))
223
224 (defun nnspool-request-group (group &optional server dont-check)
225   "Select news GROUP."
226   (let ((pathname (nnspool-article-pathname
227                    (nnspool-replace-chars-in-string group ?. ?/)))
228         dir)
229     (if (not (file-directory-p pathname))
230         (progn
231           (setq nnspool-status-string
232                 "Invalid group name (no such directory)")
233           nil)
234       (setq nnspool-current-directory pathname)
235       (setq nnspool-status-string "")
236       (if (not dont-check)
237           (progn
238             (setq dir (directory-files pathname nil "^[0-9]+$" t))
239             ;; yes, completely empty spool directories *are* possible
240             ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
241             (and dir
242                  (setq dir
243                        (sort 
244                         (mapcar
245                          (function
246                           (lambda (name)
247                             (string-to-int name)))
248                          dir)
249                         '<)))
250             (save-excursion
251               (set-buffer nntp-server-buffer)
252               (erase-buffer)
253               (if dir
254                   (insert
255                    (format "211 %d %d %d %s\n" (length dir) (car dir)
256                            (progn (while (cdr dir) (setq dir (cdr dir)))
257                                   (car dir))
258                            group))
259                 (insert (format "211 0 0 0 %s\n" group))))))
260       t)))
261
262 (defun nnspool-close-group (group &optional server)
263   t)
264
265 (defun nnspool-request-list (&optional server)
266   "List active newsgoups."
267   (save-excursion
268     (nnspool-find-file nnspool-active-file)))
269
270 (defun nnspool-request-list-newsgroups (&optional server)
271   "List newsgroups (defined in NNTP2)."
272   (save-excursion
273     (nnspool-find-file nnspool-newsgroups-file)))
274
275 (defun nnspool-request-list-distributions (&optional server)
276   "List distributions (defined in NNTP2)."
277   (save-excursion
278     (nnspool-find-file nnspool-distributions-file)))
279
280 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
281 (defun nnspool-request-newgroups (date &optional server)
282   "List groups created after DATE."
283   (if (nnspool-find-file nnspool-active-times-file)
284       (save-excursion
285         ;; Find the last valid line.
286         (goto-char (point-max))
287         (while (and (not (looking-at 
288                           "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
289                     (zerop (forward-line -1))))
290         (let ((seconds (nnspool-seconds-since-epoch date))
291               groups)
292           ;; Go through lines and add the latest groups to a list.
293           (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
294                       (progn
295                         ;; We insert a .0 to make the list reader
296                         ;; interpret the number as a float. It is far
297                         ;; too big to be stored in a lisp integer. 
298                         (goto-char (1- (match-end 0)))
299                         (insert ".0")
300                         (> (progn
301                              (goto-char (match-end 1))
302                              (read (current-buffer)))
303                            seconds))
304                       (setq groups (cons (buffer-substring
305                                           (match-beginning 1) (match-end 1))
306                                          groups))
307                       (zerop (forward-line -1))))
308           (erase-buffer)
309           (while groups
310             (insert (car groups) " 0 0 y\n")
311             (setq groups (cdr groups))))
312         t)
313     nil))
314
315 (defun nnspool-request-post (&optional server)
316   "Post a new news in current buffer."
317   (save-excursion
318     (let* ((inews-buffer (generate-new-buffer " *nnspool post*"))
319            (proc (apply 'start-process "*nnspool inews*" inews-buffer
320                         nnspool-inews-program nnspool-inews-switches)))
321       (set-process-sentinel proc 'nnspool-inews-sentinel)
322       (process-send-region proc (point-min) (point-max))
323       (process-send-eof proc)
324       t)))
325
326 (defun nnspool-inews-sentinel (proc status)
327   (save-excursion
328     (set-buffer (process-buffer proc))
329     (goto-char (point-min))
330     (if (or (zerop (buffer-size))
331             (search-forward "spooled" nil t))
332         ()
333       ;; Make status message by unfolding lines.
334       (subst-char-in-region (point-min) (point-max) ?\n ?\\ 'noundo)
335       (setq nnspool-status-string (buffer-string))
336       (message "nnspool: %s" nnspool-status-string)
337       (kill-buffer (current-buffer)))))
338
339 (fset 'nnspool-request-post-buffer 'nntp-request-post-buffer)
340
341 \f
342 ;;; Internal functions.
343
344 (defun nnspool-retrieve-headers-with-nov (articles)
345   (if (or gnus-nov-is-evil nnspool-nov-is-evil)
346       nil
347     (let ((nov (concat nnspool-nov-directory
348                        (nnspool-replace-chars-in-string
349                         nnspool-current-group ?. ?/)
350                        "/.overview"))
351           article)
352       (if (file-exists-p nov)
353           (save-excursion
354             (set-buffer nntp-server-buffer)
355             (erase-buffer)
356             (if nnspool-sift-nov-with-sed
357                 (nnspool-sift-nov-with-sed articles nov)
358               (insert-file-contents nov)
359               ;; First we find the first wanted line. We issue a number
360               ;; of search-forwards - the first article we are lookign
361               ;; for may be expired, so we have to go on searching until
362               ;; we find one of the articles we want.
363               (while (and articles
364                           (setq article (concat (int-to-string 
365                                                  (car articles)) "\t"))
366                           (not (or (looking-at article)
367                                    (search-forward (concat "\n" article) 
368                                                    nil t))))
369                 (setq articles (cdr articles)))
370               (if (not articles)
371                   ()
372                 (beginning-of-line)
373                 (delete-region (point-min) (point))
374                 ;; Then we find the last wanted line. We go to the end
375                 ;; of the buffer and search backward much the same way
376                 ;; we did to find the first article.
377                 ;; !!! Perhaps it would be better just to do a (last articles), 
378                 ;; and go forward successively over each line and
379                 ;; compare to avoid this (reverse), like this:
380                 ;; (while (and (>= last (read nntp-server-buffer)))
381                 ;;             (zerop (forward-line 1))))
382                 (setq articles (reverse articles))
383                 (goto-char (point-max))
384                 (while (and articles
385                             (not (search-backward 
386                                   (concat "\n" (int-to-string (car articles))
387                                           "\t") nil t)))
388                   (setq articles (cdr articles)))
389                 (if articles
390                     (progn
391                       (forward-line 2)
392                       (delete-region (point) (point-max)))))
393               (or articles (progn (erase-buffer) nil))))))))
394
395 (defun nnspool-sift-nov-with-sed (articles file)
396   (let ((first (car articles))
397         (last (progn (while (cdr articles) (setq articles (cdr articles)))
398                      (car articles))))
399     (call-process "sed" nil t nil "-e" 
400                   (format "1,/^%d\t/d\n/^%d\t/,$d"
401                           (1- first) (1+ last)) file)))
402
403 (defun nnspool-find-article-by-message-id (id)
404   "Return full pathname of an article identified by message-ID."
405   (save-excursion
406     (set-buffer nntp-server-buffer)
407     (erase-buffer)
408     (call-process "grep" nil t nil id nnspool-history-file)
409     (goto-char (point-min))
410     (if (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\(.*\\)$")
411         (concat nnspool-spool-directory
412                 (nnspool-replace-chars-in-string 
413                  (buffer-substring (match-beginning 1) (match-end 1)) 
414                  ?. ?/)))))
415
416 (defun nnspool-find-file (file)
417   "Insert FILE in server buffer safely."
418   (set-buffer nntp-server-buffer)
419   (erase-buffer)
420   (condition-case ()
421       (progn (insert-file-contents file) t)
422     (file-error nil)))
423
424 (defun nnspool-possibly-change-directory (newsgroup)
425   (if newsgroup
426       (let ((pathname (nnspool-article-pathname
427                        (nnspool-replace-chars-in-string newsgroup ?. ?/))))
428         (if (file-directory-p pathname)
429             (progn
430               (setq nnspool-current-directory pathname)
431               (setq nnspool-current-group newsgroup))
432           (setq nnspool-status-string 
433                 (format "No such newsgroup: %s" newsgroup))
434           nil))
435     t))
436
437 (defun nnspool-article-pathname (group)
438   "Make pathname for GROUP."
439   (concat (file-name-as-directory nnspool-spool-directory) group "/"))
440
441 (defun nnspool-replace-chars-in-string (string from to)
442   "Replace characters in STRING from FROM to TO."
443   (let ((string (substring string 0))   ;Copy string.
444         (len (length string))
445         (idx 0))
446     ;; Replace all occurrences of FROM with TO.
447     (while (< idx len)
448       (if (= (aref string idx) from)
449           (aset string idx to))
450       (setq idx (1+ idx)))
451     string))
452
453 (defun nnspool-number-base-10 (num pos)
454   (if (<= pos 0) ""
455     (setcdr num (+ (* (% (car num) 10) 65536) (cdr num)))
456     (apply
457      'concat
458      (reverse
459       (list
460        (char-to-string
461         (aref "0123456789" (% (cdr num) 10)))
462        (progn
463          (setcdr num (/ (cdr num) 10))
464          (setcar num (/ (car num) 10))
465          (nnspool-number-base-10 num (1- pos))))))))
466
467 (defun nnspool-seconds-since-epoch (date)
468   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
469                         (timezone-parse-date date)))
470          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
471                         (timezone-parse-time
472                          (aref (timezone-parse-date date) 3))))
473          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
474                         (timezone-parse-date "Jan 1 12:00:00 1970")))
475          (tday (- (timezone-absolute-from-gregorian 
476                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
477                   (timezone-absolute-from-gregorian 
478                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
479     (+ (nth 2 ttime)
480        (* (nth 1 ttime) 60)
481        (* 1.0 (nth 0 ttime) 60 60)
482        (* 1.0 tday 60 60 24))))
483
484 (provide 'nnspool)
485
486 ;;; nnspool.el ends here