1 ;;; nnspool.el --- spool access for GNU Emacs
2 ;; Copyright (C) 1988,89,90,93,94,95 Free Software Foundation, Inc.
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
32 (defvar nnspool-inews-program news-inews-program
33 "Program to post news.")
35 (defvar nnspool-inews-switches '("-h")
36 "Switches for nnspool-request-post to pass to `inews' for posting news.")
38 (defvar nnspool-spool-directory news-path
39 "Local news spool directory.")
41 (defvar nnspool-nov-directory (concat nnspool-spool-directory "over.view/")
42 "Local news nov directory.")
44 (defvar nnspool-lib-dir "/usr/lib/news/"
45 "Where the local news library files are stored.")
47 (defvar nnspool-active-file (concat nnspool-lib-dir "active")
48 "Local news active file.")
50 (defvar nnspool-newsgroups-file (concat nnspool-lib-dir "newsgroups")
51 "Local news newsgroups file.")
53 (defvar nnspool-distributions-file (concat nnspool-lib-dir "distributions")
54 "Local news distributions file.")
56 (defvar nnspool-history-file (concat nnspool-lib-dir "history")
57 "Local news history file.")
59 (defvar nnspool-active-times-file (concat nnspool-lib-dir "active.times")
60 "Local news active date file.")
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.")
67 (defvar nnspool-nov-is-evil nil
68 "Non-nil means that nnspool will never return NOV lines instead of headers.")
72 (defconst nnspool-version "nnspool 2.0"
73 "Version numbers of this version of NNSPOOL.")
75 (defvar nnspool-current-directory nil
76 "Current news group directory.")
78 (defvar nnspool-current-group nil)
79 (defvar nnspool-status-string "")
83 (defvar nnspool-current-server nil)
84 (defvar nnspool-server-alist nil)
85 (defvar nnspool-server-variables
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 "")))
104 ;;; Interface functions.
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."
110 (set-buffer nntp-server-buffer)
112 (let* ((number (length sequence))
114 (do-message (and (numberp nnspool-large-newsgroup)
115 (> number nnspool-large-newsgroup)))
117 (if (not (nnspool-possibly-change-directory newsgroup))
119 (if (nnspool-retrieve-headers-with-nov sequence)
122 (setq article (car sequence))
123 (setq file (concat nnspool-current-directory
124 (int-to-string article)))
125 (and (file-exists-p file)
127 (insert (format "221 %d Article retrieved.\n" article))
129 (insert-file-contents file)
131 (search-forward "\n\n" nil t)
134 (delete-region (point) (point-max))))
135 (setq sequence (cdr sequence))
138 (zerop (% (setq count (1+ count)) 20))
139 (message "NNSPOOL: Receiving headers... %d%%"
140 (/ (* count 100) number))))
142 (and do-message (message "NNSPOOL: Receiving headers... done"))
144 ;; Fold continuation lines.
146 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
147 (replace-match " " t t))
150 (defun nnspool-open-server (server &optional defs)
151 (nnheader-init-server-buffer)
152 (if (equal server nnspool-current-server)
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)))
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)))
167 (defun nnspool-close-server (&optional server)
170 (defun nnspool-server-opened (&optional server)
171 (equal server nnspool-current-server))
173 (defun nnspool-status-message (&optional server)
174 "Return server status response as string."
175 nnspool-status-string)
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)
186 (not (file-directory-p file)))
188 (nnspool-find-file file)))))
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)
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)))
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)
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)))
212 (defun nnspool-request-group (group &optional server dont-check)
214 (let ((pathname (nnspool-article-pathname
215 (nnspool-replace-chars-in-string group ?. ?/)))
217 (if (not (file-directory-p pathname))
219 (setq nnspool-status-string
220 "Invalid group name (no such directory)")
222 (setq nnspool-current-directory pathname)
223 (setq nnspool-status-string "")
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>
235 (string-to-int name)))
239 (set-buffer nntp-server-buffer)
243 (format "211 %d %d %d %s\n" (length dir) (car dir)
244 (progn (while (cdr dir) (setq dir (cdr dir)))
247 (insert (format "211 0 0 0 %s\n" group))))))
250 (defun nnspool-close-group (group &optional server)
253 (defun nnspool-request-list (&optional server)
254 "List active newsgoups."
256 (nnspool-find-file nnspool-active-file)))
258 (defun nnspool-request-list-newsgroups (&optional server)
259 "List newsgroups (defined in NNTP2)."
261 (nnspool-find-file nnspool-newsgroups-file)))
263 (defun nnspool-request-list-distributions (&optional server)
264 "List distributions (defined in NNTP2)."
266 (nnspool-find-file nnspool-distributions-file)))
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)
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))
280 ;; Go through lines and add the latest groups to a list.
281 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
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)))
289 (goto-char (match-end 1))
290 (read (current-buffer)))
292 (setq groups (cons (buffer-substring
293 (match-beginning 1) (match-end 1))
295 (zerop (forward-line -1))))
298 (insert (car groups) " 0 0 y\n")
299 (setq groups (cdr groups))))
303 (defun nnspool-request-post (&optional server)
304 "Post a new news in current buffer."
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)
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))
325 (fset 'nnspool-request-post-buffer 'nntp-request-post-buffer)
328 ;;; Internal functions.
330 (defun nnspool-retrieve-headers-with-nov (articles)
331 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
333 (let ((nov (concat nnspool-nov-directory
334 (nnspool-replace-chars-in-string
335 nnspool-current-group ?. ?/)
338 (if (file-exists-p nov)
340 (set-buffer nntp-server-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.
348 (setq article (concat (int-to-string
349 (car articles)) "\t"))
350 (not (or (looking-at article)
351 (search-forward (concat "\n" article)
353 (setq articles (cdr articles)))
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))
369 (not (search-backward
370 (concat "\n" (int-to-string (car articles))
372 (setq articles (cdr articles)))
376 (delete-region (point) (point-max)))))
377 (or articles (progn (erase-buffer) nil)))))))
379 (defun nnspool-find-article-by-message-id (id)
380 "Return full pathname of an article identified by message-ID."
382 (set-buffer nntp-server-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))
392 (defun nnspool-find-file (file)
393 "Insert FILE in server buffer safely."
394 (set-buffer nntp-server-buffer)
397 (progn (insert-file-contents file) t)
400 (defun nnspool-possibly-change-directory (newsgroup)
402 (let ((pathname (nnspool-article-pathname
403 (nnspool-replace-chars-in-string newsgroup ?. ?/))))
404 (if (file-directory-p pathname)
406 (setq nnspool-current-directory pathname)
407 (setq nnspool-current-group newsgroup))
408 (setq nnspool-status-string
409 (format "No such newsgroup: %s" newsgroup))
413 (defun nnspool-article-pathname (group)
414 "Make pathname for GROUP."
415 (concat (file-name-as-directory nnspool-spool-directory) group "/"))
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))
422 ;; Replace all occurrences of FROM with TO.
424 (if (= (aref string idx) from)
425 (aset string idx to))
429 (defun nnspool-number-base-10 (num pos)
431 (setcdr num (+ (* (% (car num) 10) 65536) (cdr num)))
437 (aref "0123456789" (% (cdr num) 10)))
439 (setcdr num (/ (cdr num) 10))
440 (setcar num (/ (car num) 10))
441 (nnspool-number-base-10 num (1- pos))))))))
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)
450 ;;; nnspool.el ends here