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