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 '("-S" "-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.")
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
77 (defconst nnspool-version "nnspool 2.0"
78 "Version numbers of this version of NNSPOOL.")
80 (defvar nnspool-current-directory nil
81 "Current news group directory.")
83 (defvar nnspool-current-group nil)
84 (defvar nnspool-status-string "")
88 (defvar nnspool-current-server nil)
89 (defvar nnspool-server-alist nil)
90 (defvar nnspool-server-variables
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 "")))
110 ;;; Interface functions.
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."
116 (set-buffer nntp-server-buffer)
118 (let* ((number (length sequence))
120 (do-message (and (numberp nnspool-large-newsgroup)
121 (> number nnspool-large-newsgroup)))
123 (if (not (nnspool-possibly-change-directory newsgroup))
125 (if (and (numberp (car sequence))
126 (nnspool-retrieve-headers-with-nov sequence))
129 (setq article (car sequence))
130 (if (stringp article)
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)
138 (insert (format "221 %d Article retrieved.\n" article))
140 (nnheader-insert-head file)
142 (search-forward "\n\n" nil t)
145 (delete-region (point) (point-max)))))
146 (setq sequence (cdr sequence))
149 (zerop (% (setq count (1+ count)) 20))
150 (message "NNSPOOL: Receiving headers... %d%%"
151 (/ (* count 100) number))))
153 (and do-message (message "NNSPOOL: Receiving headers... done"))
155 ;; Fold continuation lines.
156 (goto-char (point-min))
157 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
158 (replace-match " " t t))
161 (defun nnspool-open-server (server &optional defs)
162 (nnheader-init-server-buffer)
163 (if (equal server nnspool-current-server)
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)))
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)))
178 (defun nnspool-close-server (&optional server)
181 (defun nnspool-server-opened (&optional server)
182 (and (equal server nnspool-current-server)
184 (buffer-name nntp-server-buffer)))
186 (defun nnspool-status-message (&optional server)
187 "Return server status response as string."
188 nnspool-status-string)
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)
199 (not (file-directory-p file)))
201 (nnspool-find-file file)))))
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)
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)))
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)
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)))
225 (defun nnspool-request-group (group &optional server dont-check)
227 (let ((pathname (nnspool-article-pathname
228 (nnspool-replace-chars-in-string group ?. ?/)))
230 (if (not (file-directory-p pathname))
232 (setq nnspool-status-string
233 "Invalid group name (no such directory)")
235 (setq nnspool-current-directory pathname)
236 (setq nnspool-status-string "")
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>
248 (string-to-int name)))
252 (set-buffer nntp-server-buffer)
256 (format "211 %d %d %d %s\n" (length dir) (car dir)
257 (progn (while (cdr dir) (setq dir (cdr dir)))
260 (insert (format "211 0 0 0 %s\n" group))))))
263 (defun nnspool-close-group (group &optional server)
266 (defun nnspool-request-list (&optional server)
267 "List active newsgoups."
269 (nnspool-find-file nnspool-active-file)))
271 (defun nnspool-request-list-newsgroups (&optional server)
272 "List newsgroups (defined in NNTP2)."
274 (nnspool-find-file nnspool-newsgroups-file)))
276 (defun nnspool-request-list-distributions (&optional server)
277 "List distributions (defined in NNTP2)."
279 (nnspool-find-file nnspool-distributions-file)))
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)
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))
293 ;; Go through lines and add the latest groups to a list.
294 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
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)))
302 (goto-char (match-end 1))
303 (read (current-buffer)))
305 (setq groups (cons (buffer-substring
306 (match-beginning 1) (match-end 1))
308 (zerop (forward-line -1))))
311 (insert (car groups) " 0 0 y\n")
312 (setq groups (cdr groups))))
316 (defun nnspool-request-post (&optional server)
317 "Post a new news in current buffer."
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...
327 (process-send-eof proc)
331 (defun nnspool-inews-sentinel (proc status)
333 (set-buffer (process-buffer proc))
334 (goto-char (point-min))
335 (if (or (zerop (buffer-size))
336 (search-forward "spooled" nil t))
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)))))
344 (fset 'nnspool-request-post-buffer 'nntp-request-post-buffer)
347 ;;; Internal functions.
349 (defun nnspool-retrieve-headers-with-nov (articles)
350 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
352 (let ((nov (concat nnspool-nov-directory
353 (nnspool-replace-chars-in-string
354 nnspool-current-group ?. ?/)
357 (if (file-exists-p nov)
359 (set-buffer nntp-server-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.
369 (setq article (concat (int-to-string
370 (car articles)) "\t"))
371 (not (or (looking-at article)
372 (search-forward (concat "\n" article)
374 (setq articles (cdr articles)))
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))
390 (not (search-backward
391 (concat "\n" (int-to-string (car articles))
393 (setq articles (cdr articles)))
397 (delete-region (point) (point-max)))))
398 (or articles (progn (erase-buffer) nil))))))))
400 (defun nnspool-sift-nov-with-sed (articles file)
401 (let ((first (car articles))
402 (last (progn (while (cdr articles) (setq articles (cdr 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))
409 (defun nnspool-find-article-by-message-id (id)
410 "Return full pathname of an article identified by message-ID."
412 (set-buffer nntp-server-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))
422 (defun nnspool-find-file (file)
423 "Insert FILE in server buffer safely."
424 (set-buffer nntp-server-buffer)
427 (progn (insert-file-contents file) t)
430 (defun nnspool-possibly-change-directory (newsgroup)
432 (let ((pathname (nnspool-article-pathname
433 (nnspool-replace-chars-in-string newsgroup ?. ?/))))
434 (if (file-directory-p pathname)
436 (setq nnspool-current-directory pathname)
437 (setq nnspool-current-group newsgroup))
438 (setq nnspool-status-string
439 (format "No such newsgroup: %s" newsgroup))
443 (defun nnspool-article-pathname (group)
444 "Make pathname for GROUP."
445 (concat (file-name-as-directory nnspool-spool-directory) group "/"))
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))
452 ;; Replace all occurrences of FROM with TO.
454 (if (= (aref string idx) from)
455 (aset string idx to))
459 (defun nnspool-number-base-10 (num pos)
461 (setcdr num (+ (* (% (car num) 10) 65536) (cdr num)))
467 (aref "0123456789" (% (cdr num) 10)))
469 (setcdr num (/ (cdr num) 10))
470 (setcar num (/ (car num) 10))
471 (nnspool-number-base-10 num (1- pos))))))))
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)))
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)))))
487 (* 1.0 (nth 0 ttime) 60 60)
488 (* 1.0 tday 60 60 24))))
492 ;;; nnspool.el ends here