Applied a patch from Keiichi-san.
[elisp/gnus.git-] / lisp / nnspool.el
1 ;;; nnspool.el --- spool access for GNU Emacs
2
3 ;; Copyright (C) 1988, 1989, 1990, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;;               2000, 2002
5 ;;               Free Software Foundation, Inc.
6
7 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; Keywords: news
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 (eval-when-compile (require 'cl))
33 (eval-when-compile (require 'gnus-clfns))
34
35 (require 'nnheader)
36 (require 'nntp)
37 (require 'nnoo)
38
39 (nnoo-declare nnspool)
40
41 (defvoo nnspool-inews-program news-inews-program
42   "Program to post news.
43 This is most commonly `inews' or `injnews'.")
44
45 (defvoo nnspool-inews-switches '("-h" "-S")
46   "Switches for nnspool-request-post to pass to `inews' for posting news.
47 If you are using Cnews, you probably should set this variable to nil.")
48
49 (defvoo nnspool-spool-directory (file-name-as-directory news-path)
50   "Local news spool directory.")
51
52 (defvoo nnspool-nov-directory (concat nnspool-spool-directory "over.view/")
53   "Local news nov directory.")
54
55 (defvoo nnspool-lib-dir
56     (if (file-exists-p "/usr/lib/news/active")
57         "/usr/lib/news/"
58       "/var/lib/news/")
59   "Where the local news library files are stored.")
60
61 (defvoo nnspool-active-file (concat nnspool-lib-dir "active")
62   "Local news active file.")
63
64 (defvoo nnspool-newsgroups-file (concat nnspool-lib-dir "newsgroups")
65   "Local news newsgroups file.")
66
67 (defvoo nnspool-distributions-file (concat nnspool-lib-dir "distribs.pat")
68   "Local news distributions file.")
69
70 (defvoo nnspool-history-file (concat nnspool-lib-dir "history")
71   "Local news history file.")
72
73 (defvoo nnspool-active-times-file (concat nnspool-lib-dir "active.times")
74   "Local news active date file.")
75
76 (defvoo nnspool-large-newsgroup 50
77   "The number of the articles which indicates a large newsgroup.
78 If the number of the articles is greater than the value, verbose
79 messages will be shown to indicate the current status.")
80
81 (defvoo nnspool-nov-is-evil nil
82   "Non-nil means that nnspool will never return NOV lines instead of headers.")
83
84 (defconst nnspool-sift-nov-with-sed nil
85   "If non-nil, use sed to get the relevant portion from the overview file.
86 If nil, nnspool will load the entire file into a buffer and process it
87 there.")
88
89 (defvoo nnspool-rejected-article-hook nil
90   "*A hook that will be run when an article has been rejected by the server.")
91
92 (defvoo nnspool-file-coding-system nnheader-file-coding-system
93   "Coding system for nnspool.")
94
95 \f
96
97 (defconst nnspool-version "nnspool 2.0"
98   "Version numbers of this version of NNSPOOL.")
99
100 (defvoo nnspool-current-directory nil
101   "Current news group directory.")
102
103 (defvoo nnspool-current-group nil)
104 (defvoo nnspool-status-string "")
105
106 \f
107 ;;; Interface functions.
108
109 (nnoo-define-basics nnspool)
110
111 (deffoo nnspool-retrieve-headers (articles &optional group server fetch-old)
112   "Retrieve the headers of ARTICLES."
113   (save-excursion
114     (set-buffer nntp-server-buffer)
115     (erase-buffer)
116     (when (nnspool-possibly-change-directory group)
117       (let* ((number (length articles))
118              (count 0)
119              (default-directory nnspool-current-directory)
120              (do-message (and (numberp nnspool-large-newsgroup)
121                               (> number nnspool-large-newsgroup)))
122              (nnheader-file-coding-system nnspool-file-coding-system)
123              file beg article ag)
124         (if (and (numberp (car articles))
125                  (nnspool-retrieve-headers-with-nov articles fetch-old))
126             ;; We successfully retrieved the NOV headers.
127             'nov
128           ;; No NOV headers here, so we do it the hard way.
129           (while (setq article (pop articles))
130             (if (stringp article)
131                 ;; This is a Message-ID.
132                 (setq ag (nnspool-find-id article)
133                       file (and ag (nnspool-article-pathname
134                                     (car ag) (cdr ag)))
135                       article (cdr ag))
136               ;; This is an article in the current group.
137               (setq file (int-to-string article)))
138             ;; Insert the head of the article.
139             (when (and file
140                        (file-exists-p file))
141               (insert "221 ")
142               (princ article (current-buffer))
143               (insert " Article retrieved.\n")
144               (setq beg (point))
145               (inline (nnheader-insert-head file))
146               (goto-char beg)
147               (if (search-forward "\n\n" nil t)
148                   (progn (forward-char -1)
149                          (insert ".\n"))
150                 (goto-char (point-max))
151                 (if (bolp)
152                     (insert ".\n")
153                   (insert "\n.\n")))
154               (delete-region (point) (point-max)))
155
156             (and do-message
157                  (zerop (% (incf count) 20))
158                  (nnheader-message 5 "nnspool: Receiving headers... %d%%"
159                                    (/ (* count 100) number))))
160
161           (when do-message
162             (nnheader-message 5 "nnspool: Receiving headers...done"))
163
164           ;; Fold continuation lines.
165           (nnheader-fold-continuation-lines)
166           'headers)))))
167
168 (deffoo nnspool-open-server (server &optional defs)
169   (nnoo-change-server 'nnspool server defs)
170   (cond
171    ((not (file-exists-p nnspool-spool-directory))
172     (nnspool-close-server)
173     (nnheader-report 'nnspool "Spool directory doesn't exist: %s"
174                      nnspool-spool-directory))
175    ((not (file-directory-p
176           (directory-file-name
177            (file-truename nnspool-spool-directory))))
178     (nnspool-close-server)
179     (nnheader-report 'nnspool "Not a directory: %s" nnspool-spool-directory))
180    ((not (file-exists-p nnspool-active-file))
181     (nnheader-report 'nnspool "The active file doesn't exist: %s"
182                      nnspool-active-file))
183    (t
184     (nnheader-report 'nnspool "Opened server %s using directory %s"
185                      server nnspool-spool-directory)
186     t)))
187
188 (deffoo nnspool-request-article (id &optional group server buffer)
189   "Select article by message ID (or number)."
190   (nnspool-possibly-change-directory group)
191   (let ((nntp-server-buffer (or buffer nntp-server-buffer))
192         file ag)
193     (if (stringp id)
194         ;; This is a Message-ID.
195         (when (setq ag (nnspool-find-id id))
196           (setq file (nnspool-article-pathname (car ag) (cdr ag))))
197       (setq file (nnspool-article-pathname nnspool-current-group id)))
198     (and file
199          (file-exists-p file)
200          (not (file-directory-p file))
201          (save-excursion (nnspool-find-file file))
202          ;; We return the article number and group name.
203          (if (numberp id)
204              (cons nnspool-current-group id)
205            ag))))
206
207 (deffoo nnspool-request-body (id &optional group server)
208   "Select article body by message ID (or number)."
209   (nnspool-possibly-change-directory group)
210   (let ((res (nnspool-request-article id)))
211     (when res
212       (save-excursion
213         (set-buffer nntp-server-buffer)
214         (goto-char (point-min))
215         (when (search-forward "\n\n" nil t)
216           (delete-region (point-min) (point)))
217         res))))
218
219 (deffoo nnspool-request-head (id &optional group server)
220   "Select article head by message ID (or number)."
221   (nnspool-possibly-change-directory group)
222   (let ((res (nnspool-request-article id)))
223     (when res
224       (save-excursion
225         (set-buffer nntp-server-buffer)
226         (goto-char (point-min))
227         (when (search-forward "\n\n" nil t)
228           (delete-region (1- (point)) (point-max)))
229         (nnheader-fold-continuation-lines)))
230     res))
231
232 (deffoo nnspool-request-group (group &optional server dont-check)
233   "Select news GROUP."
234   (let ((pathname (nnspool-article-pathname group))
235         dir)
236     (if (not (file-directory-p pathname))
237         (nnheader-report
238          'nnspool "Invalid group name (no such directory): %s" group)
239       (setq nnspool-current-directory pathname)
240       (nnheader-report 'nnspool "Selected group %s" group)
241       (if dont-check
242           (progn
243             (nnheader-report 'nnspool "Selected group %s" group)
244             t)
245         ;; Yes, completely empty spool directories *are* possible.
246         ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
247         (when (setq dir (directory-files pathname nil "^[0-9]+$" t))
248           (setq dir
249                 (sort (mapcar (lambda (name) (string-to-int name)) dir) '<)))
250         (if dir
251             (nnheader-insert
252              "211 %d %d %d %s\n" (length dir) (car dir)
253              (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
254              group)
255           (nnheader-report 'nnspool "Empty group %s" group)
256           (nnheader-insert "211 0 0 0 %s\n" group))))))
257
258 (deffoo nnspool-request-type (group &optional article)
259   'news)
260
261 (deffoo nnspool-close-group (group &optional server)
262   t)
263
264 (deffoo nnspool-request-list (&optional server)
265   "List active newsgroups."
266   (save-excursion
267     (or (nnspool-find-file nnspool-active-file)
268         (nnheader-report 'nnspool (nnheader-file-error nnspool-active-file)))))
269
270 (deffoo nnspool-request-list-newsgroups (&optional server)
271   "List newsgroups (defined in NNTP2)."
272   (save-excursion
273     (or (nnspool-find-file nnspool-newsgroups-file)
274         (nnheader-report 'nnspool (nnheader-file-error
275                                    nnspool-newsgroups-file)))))
276
277 (deffoo nnspool-request-list-distributions (&optional server)
278   "List distributions (defined in NNTP2)."
279   (save-excursion
280     (or (nnspool-find-file nnspool-distributions-file)
281         (nnheader-report 'nnspool (nnheader-file-error
282                                    nnspool-distributions-file)))))
283
284 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
285 (deffoo nnspool-request-newgroups (date &optional server)
286   "List groups created after DATE."
287   (if (nnspool-find-file nnspool-active-times-file)
288       (save-excursion
289         ;; Find the last valid line.
290         (goto-char (point-max))
291         (while (and (not (looking-at
292                           "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
293                     (zerop (forward-line -1))))
294         (let ((seconds (time-to-seconds (date-to-time date)))
295               groups)
296           ;; Go through lines and add the latest groups to a list.
297           (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
298                       (progn
299                         ;; We insert a .0 to make the list reader
300                         ;; interpret the number as a float.  It is far
301                         ;; too big to be stored in a lisp integer.
302                         (goto-char (1- (match-end 0)))
303                         (insert ".0")
304                         (> (progn
305                              (goto-char (match-end 1))
306                              (read (current-buffer)))
307                            seconds))
308                       (push (buffer-substring
309                              (match-beginning 1) (match-end 1))
310                             groups)
311                       (zerop (forward-line -1))))
312           (erase-buffer)
313           (while groups
314             (insert (car groups) " 0 0 y\n")
315             (setq groups (cdr groups))))
316         t)
317     nil))
318
319 (deffoo nnspool-request-post (&optional server)
320   "Post a new news in current buffer."
321   (save-excursion
322     (let* ((process-connection-type nil) ; t bugs out on Solaris
323            (inews-buffer (generate-new-buffer " *nnspool post*"))
324            (proc
325             (condition-case err
326                 (apply 'start-process "*nnspool inews*" inews-buffer
327                        nnspool-inews-program nnspool-inews-switches)
328               (error
329                (nnheader-report 'nnspool "inews error: %S" err)))))
330       (if (not proc)
331           ;; The inews program failed.
332           ()
333         (nnheader-report 'nnspool "")
334         (set-process-sentinel proc 'nnspool-inews-sentinel)
335         (mm-with-unibyte-current-buffer
336           (process-send-region proc (point-min) (point-max)))
337         ;; We slap a condition-case around this, because the process may
338         ;; have exited already...
339         (ignore-errors
340           (process-send-eof proc))
341         t))))
342
343
344 \f
345 ;;; Internal functions.
346
347 (defun nnspool-inews-sentinel (proc status)
348   (save-excursion
349     (set-buffer (process-buffer proc))
350     (goto-char (point-min))
351     (if (or (zerop (buffer-size))
352             (search-forward "spooled" nil t))
353         (kill-buffer (current-buffer))
354       ;; Make status message by folding lines.
355       (while (re-search-forward "[ \t\n]+" nil t)
356         (replace-match " " t t))
357       (nnheader-report 'nnspool "%s" (buffer-string))
358       (nnheader-message 5 "nnspool: %s" nnspool-status-string)
359       (ding)
360       (run-hooks 'nnspool-rejected-article-hook))))
361
362 (defun nnspool-retrieve-headers-with-nov (articles &optional fetch-old)
363   (if (or gnus-nov-is-evil nnspool-nov-is-evil)
364       nil
365     (let ((nov (nnheader-group-pathname
366                 nnspool-current-group nnspool-nov-directory ".overview"))
367           (arts articles)
368           (nnheader-file-coding-system nnspool-file-coding-system)
369           last)
370       (if (not (file-exists-p nov))
371           ()
372         (save-excursion
373           (set-buffer nntp-server-buffer)
374           (erase-buffer)
375           (if nnspool-sift-nov-with-sed
376               (nnspool-sift-nov-with-sed articles nov)
377             (nnheader-insert-file-contents nov)
378             (if (and fetch-old
379                      (not (numberp fetch-old)))
380                 t                       ; We want all the headers.
381               (ignore-errors
382                 ;; Delete unwanted NOV lines.
383                 (nnheader-nov-delete-outside-range
384                  (if fetch-old (max 1 (- (car articles) fetch-old))
385                    (car articles))
386                  (car (last articles)))
387                 ;; If the buffer is empty, this wasn't very successful.
388                 (unless (zerop (buffer-size))
389                   ;; We check what the last article number was.
390                   ;; The NOV file may be out of sync with the articles
391                   ;; in the group.
392                   (forward-line -1)
393                   (setq last (read (current-buffer)))
394                   (if (= last (car articles))
395                       ;; Yup, it's all there.
396                       t
397                     ;; Perhaps not.  We try to find the missing articles.
398                     (while (and arts
399                                 (<= last (car arts)))
400                       (pop arts))
401                     ;; The articles in `arts' are missing from the buffer.
402                     (while arts
403                       (nnspool-insert-nov-head (pop arts)))
404                     t))))))))))
405
406 (defun nnspool-insert-nov-head (article)
407   "Read the head of ARTICLE, convert to NOV headers, and insert."
408   (save-excursion
409     (let ((cur (current-buffer))
410           buf)
411       (setq buf (nnheader-set-temp-buffer " *nnspool head*"))
412       (when (nnheader-insert-head
413              (nnspool-article-pathname nnspool-current-group article))
414         (nnheader-insert-article-line article)
415         (let ((headers (nnheader-parse-head)))
416           (set-buffer cur)
417           (goto-char (point-max))
418           (nnheader-insert-nov headers)))
419       (kill-buffer buf))))
420
421 (defun nnspool-sift-nov-with-sed (articles file)
422   (let ((first (car articles))
423         (last (progn (while (cdr articles) (setq articles (cdr articles)))
424                      (car articles))))
425     (call-process "awk" nil t nil
426                   (format "BEGIN {firstmsg=%d; lastmsg=%d;}\n $1 >= firstmsg && $1 <= lastmsg {print;}"
427                           (1- first) (1+ last))
428                   file)))
429
430 ;; Fixed by fdc@cliwe.ping.de (Frank D. Cringle).
431 ;; Find out what group an article identified by a Message-ID is in.
432 (defun nnspool-find-id (id)
433   (save-excursion
434     (set-buffer (get-buffer-create " *nnspool work*"))
435     (erase-buffer)
436     (ignore-errors
437       (call-process "grep" nil t nil (regexp-quote id) nnspool-history-file))
438     (goto-char (point-min))
439     (prog1
440         (when (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\([^ /\t\n]+\\)/\\([0-9]+\\)[ \t\n]")
441           (cons (match-string 1) (string-to-int (match-string 2))))
442       (kill-buffer (current-buffer)))))
443
444 (defun nnspool-find-file (file)
445   "Insert FILE in server buffer safely."
446   (set-buffer nntp-server-buffer)
447   (erase-buffer)
448   (condition-case ()
449       (let ((nnheader-file-coding-system nnspool-file-coding-system))
450         (nnheader-insert-file-contents file)
451         t)
452     (file-error nil)))
453
454 (defun nnspool-possibly-change-directory (group)
455   (if (not group)
456       t
457     (let ((pathname (nnspool-article-pathname group)))
458       (if (file-directory-p pathname)
459           (setq nnspool-current-directory pathname
460                 nnspool-current-group group)
461         (nnheader-report 'nnspool "No such newsgroup: %s" group)))))
462
463 (defun nnspool-article-pathname (group &optional article)
464   "Find the path for GROUP."
465   (nnheader-group-pathname group nnspool-spool-directory article))
466
467 (provide 'nnspool)
468
469 ;;; nnspool.el ends here