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