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