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