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