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