*** empty log message ***
[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   (if (nnspool-request-article id)
220       (save-excursion
221         (set-buffer nntp-server-buffer)
222         (goto-char (point-min))
223         (if (search-forward "\n\n" nil t)
224             (delete-region (point-min) (point)))
225         t)))
226
227 (defun nnspool-request-head (id &optional group server)
228   "Select article head by message ID (or number)."
229   (nnspool-possibly-change-directory group)
230   (let ((res (nnspool-request-article id)))
231     (when res
232       (save-excursion
233         (set-buffer nntp-server-buffer)
234         (goto-char (point-min))
235         (when (search-forward "\n\n" nil t)
236           (delete-region (1- (point)) (point-max)))))
237     res))
238
239 (defun nnspool-request-group (group &optional server dont-check)
240   "Select news GROUP."
241   (let ((pathname (nnspool-article-pathname group))
242         dir)
243     (if (not (file-directory-p pathname))
244         (nnheader-report 
245          'nnspool "Invalid group name (no such directory): %s" group)
246       (setq nnspool-current-directory pathname)
247       (nnheader-report 'nnspool "Selected group %s" group)
248       (if dont-check
249           (progn
250             (nnheader-report 'nnspool "Selected group %s" group)
251             t)
252         ;; Yes, completely empty spool directories *are* possible
253         ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
254         (when (setq dir (directory-files pathname nil "^[0-9]+$" t))
255           (setq dir 
256                 (sort (mapcar (lambda (name) (string-to-int name)) dir) '<)))
257         (save-excursion
258           (set-buffer nntp-server-buffer)
259           (erase-buffer)
260           (if dir
261               (progn
262                 (insert
263                  (format "211 %d %d %d %s\n" (length dir) (car dir)
264                          (progn (while (cdr dir) (setq dir (cdr dir)))
265                                 (car dir))
266                          group))
267                 t)
268             (insert (format "211 0 0 0 %s\n" group))
269             (nnheader-report 'nnspool "Empty group %s" group)
270             t))))))
271
272 (defun nnspool-request-type (group &optional article)
273   'news)
274
275 (defun nnspool-close-group (group &optional server)
276   t)
277
278 (defun nnspool-request-list (&optional server)
279   "List active newsgroups."
280   (save-excursion
281     (nnspool-find-file nnspool-active-file)))
282
283 (defun nnspool-request-list-newsgroups (&optional server)
284   "List newsgroups (defined in NNTP2)."
285   (save-excursion
286     (nnspool-find-file nnspool-newsgroups-file)))
287
288 (defun nnspool-request-list-distributions (&optional server)
289   "List distributions (defined in NNTP2)."
290   (save-excursion
291     (nnspool-find-file nnspool-distributions-file)))
292
293 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
294 (defun nnspool-request-newgroups (date &optional server)
295   "List groups created after DATE."
296   (if (nnspool-find-file nnspool-active-times-file)
297       (save-excursion
298         ;; Find the last valid line.
299         (goto-char (point-max))
300         (while (and (not (looking-at 
301                           "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
302                     (zerop (forward-line -1))))
303         (let ((seconds (nnspool-seconds-since-epoch date))
304               groups)
305           ;; Go through lines and add the latest groups to a list.
306           (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
307                       (progn
308                         ;; We insert a .0 to make the list reader
309                         ;; interpret the number as a float. It is far
310                         ;; too big to be stored in a lisp integer. 
311                         (goto-char (1- (match-end 0)))
312                         (insert ".0")
313                         (> (progn
314                              (goto-char (match-end 1))
315                              (read (current-buffer)))
316                            seconds))
317                       (setq groups (cons (buffer-substring
318                                           (match-beginning 1) (match-end 1))
319                                          groups))
320                       (zerop (forward-line -1))))
321           (erase-buffer)
322           (while groups
323             (insert (car groups) " 0 0 y\n")
324             (setq groups (cdr groups))))
325         t)
326     nil))
327
328 (defun nnspool-request-post (&optional server)
329   "Post a new news in current buffer."
330   (save-excursion
331     (let* ((process-connection-type nil) ; t bugs out on Solaris
332            (inews-buffer (generate-new-buffer " *nnspool post*"))
333            (proc 
334             (condition-case err
335                 (apply 'start-process "*nnspool inews*" inews-buffer
336                        nnspool-inews-program nnspool-inews-switches)
337               (error
338                (setq nnspool-status-string (format "inews error: %S" err))
339                nil))))
340       (if (not proc)
341           ;; The inews program failed.
342           ()
343         (setq nnspool-status-string "")
344         (set-process-sentinel proc 'nnspool-inews-sentinel)
345         (process-send-region proc (point-min) (point-max))
346         ;; We slap a condition-case around this, because the process may
347         ;; have exited already...
348         (condition-case nil
349             (process-send-eof proc)
350           (error nil))
351         t))))
352
353 (defun nnspool-inews-sentinel (proc status)
354   (save-excursion
355     (set-buffer (process-buffer proc))
356     (goto-char (point-min))
357     (if (or (zerop (buffer-size))
358             (search-forward "spooled" nil t))
359         (kill-buffer (current-buffer))
360       ;; Make status message by folding lines.
361       (while (re-search-forward "[ \t\n]+" nil t)
362         (replace-match " " t t))
363       (setq nnspool-status-string (buffer-string))
364       (message "nnspool: %s" nnspool-status-string)
365       (ding)
366       (run-hooks 'nnspool-rejected-article-hook))))
367
368 \f
369 ;;; Internal functions.
370
371 (defun nnspool-retrieve-headers-with-nov (articles &optional fetch-old)
372   (if (or gnus-nov-is-evil nnspool-nov-is-evil)
373       nil
374     (let ((nov (nnheader-group-pathname 
375                 nnspool-current-group nnspool-nov-directory ".overview")))
376       (if (not (file-exists-p nov))
377           ()
378         (save-excursion
379           (set-buffer nntp-server-buffer)
380           (erase-buffer)
381           (if nnspool-sift-nov-with-sed
382               (nnspool-sift-nov-with-sed articles nov)
383             (insert-file-contents nov)
384             (if (and fetch-old
385                      (not (numberp fetch-old)))
386                 t                       ; We want all the headers.
387               ;; First we find the first wanted line.
388               (nnspool-find-nov-line
389                (if fetch-old (max 1 (- (car articles) fetch-old))
390                  (car articles)))
391               (delete-region (point-min) (point))
392               ;; Then we find the last wanted line. 
393               (if (nnspool-find-nov-line 
394                    (progn (while (cdr articles) (setq articles (cdr articles)))
395                           (car articles)))
396                   (forward-line 1))
397               (delete-region (point) (point-max))
398               ;; If the buffer is empty, this wasn't very successful.
399               (not (zerop (buffer-size))))))))))
400
401 (defun nnspool-find-nov-line (article)
402   (let ((max (point-max))
403         (min (goto-char (point-min)))
404         (cur (current-buffer))
405         (prev (point-min))
406         num found)
407     (while (not found)
408       (goto-char (/ (+ max min) 2))
409       (beginning-of-line)
410       (if (or (= (point) prev)
411               (eobp))
412           (setq found t)
413         (setq prev (point))
414         (cond ((> (setq num (read cur)) article)
415                (setq max (point)))
416               ((< num article)
417                (setq min (point)))
418               (t
419                (setq found t)))))
420     (when (not (eq num article))
421       (setq found (point))
422       (forward-line 1)
423       (or (eobp)
424           (= (setq num (read cur)) article)
425           (goto-char found)))
426     (beginning-of-line)
427     (eq num article)))
428     
429
430 (defun nnspool-sift-nov-with-sed (articles file)
431   (let ((first (car articles))
432         (last (progn (while (cdr articles) (setq articles (cdr articles)))
433                      (car articles))))
434     (call-process "awk" nil t nil 
435                   (format "BEGIN {firstmsg=%d; lastmsg=%d;}\n $1 >= firstmsg && $1 <= lastmsg {print;}"
436                           (1- first) (1+ last))
437                   file)))
438
439 ;; Fixed by fdc@cliwe.ping.de (Frank D. Cringle). 
440 ;; Find out what group an article identified by a Message-ID is in.
441 (defun nnspool-find-id (id)
442   (save-excursion
443     (set-buffer (get-buffer-create " *nnspool work*"))
444     (buffer-disable-undo (current-buffer))
445     (erase-buffer)
446     (condition-case ()
447         (call-process "grep" nil t nil id nnspool-history-file)
448       (error nil))
449     (goto-char (point-min))
450     (prog1
451         (if (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\([^ /\t\n]+\\)/\\([0-9]+\\)[ \t\n]")
452             (cons (match-string 1) (string-to-int (match-string 2))))
453       (kill-buffer (current-buffer)))))
454
455 (defun nnspool-find-file (file)
456   "Insert FILE in server buffer safely."
457   (set-buffer nntp-server-buffer)
458   (erase-buffer)
459   (condition-case ()
460       (progn (insert-file-contents file) t)
461     (file-error nil)))
462
463 (defun nnspool-possibly-change-directory (group)
464   (if group
465       (let ((pathname (nnspool-article-pathname group)))
466         (if (file-directory-p pathname)
467             (progn
468               (setq nnspool-current-directory pathname)
469               (setq nnspool-current-group group))
470           (setq nnspool-status-string 
471                 (format "No such newsgroup: %s" group))
472           nil))
473     t))
474
475 (defun nnspool-article-pathname (group &optional article)
476   "Find the path for GROUP."
477   (nnheader-group-pathname group nnspool-spool-directory article))
478
479 (defun nnspool-seconds-since-epoch (date)
480   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
481                         (timezone-parse-date date)))
482          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
483                         (timezone-parse-time
484                          (aref (timezone-parse-date date) 3))))
485          (unix (encode-time (nth 2 ttime) (nth 1 ttime) (nth 0 ttime)
486                             (nth 2 tdate) (nth 1 tdate) (nth 0 tdate) 
487                             (nth 4 tdate))))
488     (+ (* (car unix) 65536.0)
489        (car (cdr unix)))))
490
491 (provide 'nnspool)
492
493 ;;; nnspool.el ends here