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