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