1 ;;; nnspool.el --- spool access for GNU Emacs
2 ;; Copyright (C) 1988,89,90,93,94,95,96 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
32 (eval-when-compile (require 'cl))
34 (defvar nnspool-inews-program news-inews-program
35 "Program to post news.
36 This is most commonly `inews' or `injnews'.")
38 (defvar nnspool-inews-switches '("-h")
39 "Switches for nnspool-request-post to pass to `inews' for posting news.
40 If you are using Cnews, you probably should set this variable to nil.")
42 (defvar nnspool-spool-directory (file-name-as-directory news-path)
43 "Local news spool directory.")
45 (defvar nnspool-nov-directory (concat nnspool-spool-directory "over.view/")
46 "Local news nov directory.")
48 (defvar nnspool-lib-dir "/usr/lib/news/"
49 "Where the local news library files are stored.")
51 (defvar nnspool-active-file (concat nnspool-lib-dir "active")
52 "Local news active file.")
54 (defvar nnspool-newsgroups-file (concat nnspool-lib-dir "newsgroups")
55 "Local news newsgroups file.")
57 (defvar nnspool-distributions-file (concat nnspool-lib-dir "distributions")
58 "Local news distributions file.")
60 (defvar nnspool-history-file (concat nnspool-lib-dir "history")
61 "Local news history file.")
63 (defvar nnspool-active-times-file (concat nnspool-lib-dir "active.times")
64 "Local news active date file.")
66 (defvar nnspool-large-newsgroup 50
67 "The number of the articles which indicates a large newsgroup.
68 If the number of the articles is greater than the value, verbose
69 messages will be shown to indicate the current status.")
71 (defvar nnspool-nov-is-evil nil
72 "Non-nil means that nnspool will never return NOV lines instead of headers.")
74 (defconst nnspool-sift-nov-with-sed nil
75 "If non-nil, use sed to get the relevant portion from the overview file.
76 If nil, nnspool will load the entire file into a buffer and process it
79 (defvar nnspool-rejected-article-hook nil
80 "*A hook that will be run when an article has been rejected by the server.")
84 (defconst nnspool-version "nnspool 2.0"
85 "Version numbers of this version of NNSPOOL.")
87 (defvar nnspool-current-directory nil
88 "Current news group directory.")
90 (defvar nnspool-current-group nil)
91 (defvar nnspool-status-string "")
95 (defvar nnspool-current-server nil)
96 (defvar nnspool-server-alist nil)
97 (defvar nnspool-server-variables
98 `((nnspool-inews-program ,nnspool-inews-program)
99 (nnspool-inews-switches ,nnspool-inews-switches)
100 (nnspool-spool-directory ,nnspool-spool-directory)
101 (nnspool-nov-directory ,nnspool-nov-directory)
102 (nnspool-lib-dir ,nnspool-lib-dir)
103 (nnspool-active-file ,nnspool-active-file)
104 (nnspool-newsgroups-file ,nnspool-newsgroups-file)
105 (nnspool-distributions-file ,nnspool-distributions-file)
106 (nnspool-history-file ,nnspool-history-file)
107 (nnspool-active-times-file ,nnspool-active-times-file)
108 (nnspool-large-newsgroup ,nnspool-large-newsgroup)
109 (nnspool-nov-is-evil ,nnspool-nov-is-evil)
110 (nnspool-sift-nov-with-sed ,nnspool-sift-nov-with-sed)
111 (nnspool-current-directory nil)
112 (nnspool-current-group nil)
113 (nnspool-status-string "")))
116 ;;; Interface functions.
118 (defun nnspool-retrieve-headers (articles &optional group server fetch-old)
119 "Retrieve the headers of ARTICLES."
121 (set-buffer nntp-server-buffer)
123 (when (nnspool-possibly-change-directory group)
124 (let* ((number (length articles))
126 (do-message (and (numberp nnspool-large-newsgroup)
127 (> number nnspool-large-newsgroup)))
129 (if (and (numberp (car articles))
130 (nnspool-retrieve-headers-with-nov articles fetch-old))
131 ;; We successfully retrieved the NOV headers.
133 ;; No NOV headers here, so we do it the hard way.
135 (setq article (pop articles))
136 (if (stringp article)
137 ;; This is a Message-ID.
138 (setq ag (nnspool-find-id article)
139 file (and ag (nnspool-article-pathname
142 ;; This is an article in the current group.
143 (setq file (nnspool-article-pathname
144 nnspool-current-group article)))
145 ;; Insert the head of the article.
147 (file-exists-p file))
148 (insert (format "221 %d Article retrieved.\n" article))
150 (nnheader-insert-head file)
152 (search-forward "\n\n" nil t)
155 (delete-region (point) (point-max)))
158 (zerop (% (incf count) 20))
159 (message "nnspool: Receiving headers... %d%%"
160 (/ (* count 100) number))))
163 (message "nnspool: Receiving headers...done"))
165 ;; Fold continuation lines.
166 (nnheader-fold-continuation-lines)
169 (defun nnspool-open-server (server &optional defs)
170 (nnheader-change-server 'nnspool server defs)
172 ((not (file-exists-p nnspool-spool-directory))
173 (nnspool-close-server)
174 (nnheader-report 'nnspool "Spool directory doesn't exist: %s"
175 nnspool-spool-directory))
176 ((not (file-directory-p (file-truename nnspool-spool-directory)))
177 (nnspool-close-server)
178 (nnheader-report 'nnspool "Not a directory: %s" nnspool-spool-directory))
180 (nnheader-report 'nnspool "Opened server %s using directory %s"
181 server nnspool-spool-directory)
184 (defun nnspool-close-server (&optional server)
185 (setq nnspool-current-server nil)
188 (defun nnspool-server-opened (&optional server)
189 (and (equal server nnspool-current-server)
191 (buffer-name nntp-server-buffer)))
193 (defun nnspool-status-message (&optional server)
194 "Return server status response as string."
195 nnspool-status-string)
197 (defun nnspool-request-article (id &optional group server buffer)
198 "Select article by message ID (or number)."
199 (nnspool-possibly-change-directory group)
200 (let ((nntp-server-buffer (or buffer nntp-server-buffer))
203 ;; This is a Message-ID.
204 (when (setq ag (nnspool-find-id id))
205 (setq file (nnspool-article-pathname (car ag) (cdr ag))))
206 (setq file (nnspool-article-pathname nnspool-current-group id)))
209 (not (file-directory-p file))
210 (save-excursion (nnspool-find-file file))
211 ;; We return the article number and group name.
213 (cons nnspool-current-group id)
216 (defun nnspool-request-body (id &optional group server)
217 "Select article body by message ID (or number)."
218 (nnspool-possibly-change-directory group)
219 (let ((res (nnspool-request-article id)))
222 (set-buffer nntp-server-buffer)
223 (goto-char (point-min))
224 (when (search-forward "\n\n" nil t)
225 (delete-region (point-min) (point)))
228 (defun nnspool-request-head (id &optional group server)
229 "Select article head by message ID (or number)."
230 (nnspool-possibly-change-directory group)
231 (let ((res (nnspool-request-article id)))
234 (set-buffer nntp-server-buffer)
235 (goto-char (point-min))
236 (when (search-forward "\n\n" nil t)
237 (delete-region (1- (point)) (point-max)))))
240 (defun nnspool-request-group (group &optional server dont-check)
242 (let ((pathname (nnspool-article-pathname group))
244 (if (not (file-directory-p pathname))
246 'nnspool "Invalid group name (no such directory): %s" group)
247 (setq nnspool-current-directory pathname)
248 (nnheader-report 'nnspool "Selected group %s" group)
251 (nnheader-report 'nnspool "Selected group %s" group)
253 ;; Yes, completely empty spool directories *are* possible.
254 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
255 (when (setq dir (directory-files pathname nil "^[0-9]+$" t))
257 (sort (mapcar (lambda (name) (string-to-int name)) dir) '<)))
260 "211 %d %d %d %s\n" (length dir) (car dir)
261 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
263 (nnheader-report 'nnspool "Empty group %s" group)
264 (nnheader-insert "211 0 0 0 %s\n" group))))))
266 (defun nnspool-request-type (group &optional article)
269 (defun nnspool-close-group (group &optional server)
272 (defun nnspool-request-list (&optional server)
273 "List active newsgroups."
275 (nnspool-find-file nnspool-active-file)))
277 (defun nnspool-request-list-newsgroups (&optional server)
278 "List newsgroups (defined in NNTP2)."
280 (nnspool-find-file nnspool-newsgroups-file)))
282 (defun nnspool-request-list-distributions (&optional server)
283 "List distributions (defined in NNTP2)."
285 (nnspool-find-file nnspool-distributions-file)))
287 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
288 (defun nnspool-request-newgroups (date &optional server)
289 "List groups created after DATE."
290 (if (nnspool-find-file nnspool-active-times-file)
292 ;; Find the last valid line.
293 (goto-char (point-max))
294 (while (and (not (looking-at
295 "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
296 (zerop (forward-line -1))))
297 (let ((seconds (nnspool-seconds-since-epoch date))
299 ;; Go through lines and add the latest groups to a list.
300 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
302 ;; We insert a .0 to make the list reader
303 ;; interpret the number as a float. It is far
304 ;; too big to be stored in a lisp integer.
305 (goto-char (1- (match-end 0)))
308 (goto-char (match-end 1))
309 (read (current-buffer)))
311 (setq groups (cons (buffer-substring
312 (match-beginning 1) (match-end 1))
314 (zerop (forward-line -1))))
317 (insert (car groups) " 0 0 y\n")
318 (setq groups (cdr groups))))
322 (defun nnspool-request-post (&optional server)
323 "Post a new news in current buffer."
325 (let* ((process-connection-type nil) ; t bugs out on Solaris
326 (inews-buffer (generate-new-buffer " *nnspool post*"))
329 (apply 'start-process "*nnspool inews*" inews-buffer
330 nnspool-inews-program nnspool-inews-switches)
332 (nnheader-report 'nnspool "inews error: %S" err)))))
334 ;; The inews program failed.
336 (nnheader-report 'nnspool "")
337 (set-process-sentinel proc 'nnspool-inews-sentinel)
338 (process-send-region proc (point-min) (point-max))
339 ;; We slap a condition-case around this, because the process may
340 ;; have exited already...
342 (process-send-eof proc)
346 (defun nnspool-inews-sentinel (proc status)
348 (set-buffer (process-buffer proc))
349 (goto-char (point-min))
350 (if (or (zerop (buffer-size))
351 (search-forward "spooled" nil t))
352 (kill-buffer (current-buffer))
353 ;; Make status message by folding lines.
354 (while (re-search-forward "[ \t\n]+" nil t)
355 (replace-match " " t t))
356 (nnheader-report 'nnspool "%s" (buffer-string))
357 (message "nnspool: %s" nnspool-status-string)
359 (run-hooks 'nnspool-rejected-article-hook))))
362 ;;; Internal functions.
364 (defun nnspool-retrieve-headers-with-nov (articles &optional fetch-old)
365 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
367 (let ((nov (nnheader-group-pathname
368 nnspool-current-group nnspool-nov-directory ".overview")))
369 (if (not (file-exists-p nov))
372 (set-buffer nntp-server-buffer)
374 (if nnspool-sift-nov-with-sed
375 (nnspool-sift-nov-with-sed articles nov)
376 (insert-file-contents nov)
378 (not (numberp fetch-old)))
379 t ; We want all the headers.
380 ;; First we find the first wanted line.
381 (nnspool-find-nov-line
382 (if fetch-old (max 1 (- (car articles) fetch-old))
384 (delete-region (point-min) (point))
385 ;; Then we find the last wanted line.
386 (if (nnspool-find-nov-line
387 (progn (while (cdr articles) (setq articles (cdr articles)))
390 (delete-region (point) (point-max))
391 ;; If the buffer is empty, this wasn't very successful.
392 (not (zerop (buffer-size))))))))))
394 (defun nnspool-find-nov-line (article)
395 (let ((max (point-max))
396 (min (goto-char (point-min)))
397 (cur (current-buffer))
401 (goto-char (/ (+ max min) 2))
403 (if (or (= (point) prev)
407 (cond ((> (setq num (read cur)) article)
413 (when (not (eq num article))
417 (= (setq num (read cur)) article)
423 (defun nnspool-sift-nov-with-sed (articles file)
424 (let ((first (car articles))
425 (last (progn (while (cdr articles) (setq articles (cdr articles)))
427 (call-process "awk" nil t nil
428 (format "BEGIN {firstmsg=%d; lastmsg=%d;}\n $1 >= firstmsg && $1 <= lastmsg {print;}"
429 (1- first) (1+ last))
432 ;; Fixed by fdc@cliwe.ping.de (Frank D. Cringle).
433 ;; Find out what group an article identified by a Message-ID is in.
434 (defun nnspool-find-id (id)
436 (set-buffer (get-buffer-create " *nnspool work*"))
437 (buffer-disable-undo (current-buffer))
440 (call-process "grep" nil t nil id nnspool-history-file)
442 (goto-char (point-min))
444 (if (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\([^ /\t\n]+\\)/\\([0-9]+\\)[ \t\n]")
445 (cons (match-string 1) (string-to-int (match-string 2))))
446 (kill-buffer (current-buffer)))))
448 (defun nnspool-find-file (file)
449 "Insert FILE in server buffer safely."
450 (set-buffer nntp-server-buffer)
453 (progn (insert-file-contents file) t)
456 (defun nnspool-possibly-change-directory (group)
459 (let ((pathname (nnspool-article-pathname group)))
460 (if (file-directory-p pathname)
461 (setq nnspool-current-directory pathname
462 nnspool-current-group group)
463 (nnheader-report 'nnspool "No such newsgroup: %s" group)))))
465 (defun nnspool-article-pathname (group &optional article)
466 "Find the path for GROUP."
467 (nnheader-group-pathname group nnspool-spool-directory article))
469 (defun nnspool-seconds-since-epoch (date)
470 (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
471 (timezone-parse-date date)))
472 (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
474 (aref (timezone-parse-date date) 3))))
475 (unix (encode-time (nth 2 ttime) (nth 1 ttime) (nth 0 ttime)
476 (nth 2 tdate) (nth 1 tdate) (nth 0 tdate)
478 (+ (* (car unix) 65536.0)
483 ;;; nnspool.el ends here