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-rejected-article-hook nil)
107 (nnspool-history-file ,nnspool-history-file)
108 (nnspool-active-times-file ,nnspool-active-times-file)
109 (nnspool-large-newsgroup ,nnspool-large-newsgroup)
110 (nnspool-nov-is-evil ,nnspool-nov-is-evil)
111 (nnspool-sift-nov-with-sed ,nnspool-sift-nov-with-sed)
112 (nnspool-current-directory nil)
113 (nnspool-current-group nil)
114 (nnspool-status-string "")))
117 ;;; Interface functions.
119 (defun nnspool-retrieve-headers (articles &optional group server fetch-old)
120 "Retrieve the headers of ARTICLES."
122 (set-buffer nntp-server-buffer)
124 (when (nnspool-possibly-change-directory group)
125 (let* ((number (length articles))
127 (do-message (and (numberp nnspool-large-newsgroup)
128 (> number nnspool-large-newsgroup)))
130 (if (and (numberp (car articles))
131 (nnspool-retrieve-headers-with-nov articles fetch-old))
132 ;; We successfully retrieved the NOV headers.
134 ;; No NOV headers here, so we do it the hard way.
136 (setq article (pop articles))
137 (if (stringp article)
138 ;; This is a Message-ID.
139 (setq ag (nnspool-find-id article)
140 file (and ag (nnspool-article-pathname
143 ;; This is an article in the current group.
144 (setq file (nnspool-article-pathname
145 nnspool-current-group article)))
146 ;; Insert the head of the article.
148 (file-exists-p file))
149 (insert (format "221 %d Article retrieved.\n" article))
151 (nnheader-insert-head file)
153 (search-forward "\n\n" nil t)
156 (delete-region (point) (point-max)))
159 (zerop (% (incf count) 20))
160 (message "nnspool: Receiving headers... %d%%"
161 (/ (* count 100) number))))
164 (message "nnspool: Receiving headers...done"))
166 ;; Fold continuation lines.
167 (nnheader-fold-continuation-lines)
170 (defun nnspool-open-server (server &optional defs)
171 (nnheader-change-server 'nnspool server defs)
173 ((not (file-exists-p nnspool-spool-directory))
174 (nnspool-close-server)
175 (nnheader-report 'nnspool "Spool directory doesn't exist: %s"
176 nnspool-spool-directory))
177 ((not (file-directory-p (file-truename nnspool-spool-directory)))
178 (nnspool-close-server)
179 (nnheader-report 'nnspool "Not a directory: %s" nnspool-spool-directory))
181 (nnheader-report 'nnspool "Opened server %s using directory %s"
182 server nnspool-spool-directory)
185 (defun nnspool-close-server (&optional server)
186 (setq nnspool-current-server nil)
189 (defun nnspool-server-opened (&optional server)
190 (and (equal server nnspool-current-server)
192 (buffer-name nntp-server-buffer)))
194 (defun nnspool-status-message (&optional server)
195 "Return server status response as string."
196 nnspool-status-string)
198 (defun nnspool-request-article (id &optional group server buffer)
199 "Select article by message ID (or number)."
200 (nnspool-possibly-change-directory group)
201 (let ((nntp-server-buffer (or buffer nntp-server-buffer))
204 ;; This is a Message-ID.
205 (when (setq ag (nnspool-find-id id))
206 (setq file (nnspool-article-pathname (car ag) (cdr ag))))
207 (setq file (nnspool-article-pathname nnspool-current-group id)))
210 (not (file-directory-p file))
211 (save-excursion (nnspool-find-file file))
212 ;; We return the article number and group name.
214 (cons nnspool-current-group id)
217 (defun nnspool-request-body (id &optional group server)
218 "Select article body by message ID (or number)."
219 (nnspool-possibly-change-directory group)
220 (let ((res (nnspool-request-article id)))
223 (set-buffer nntp-server-buffer)
224 (goto-char (point-min))
225 (when (search-forward "\n\n" nil t)
226 (delete-region (point-min) (point)))
229 (defun nnspool-request-head (id &optional group server)
230 "Select article head by message ID (or number)."
231 (nnspool-possibly-change-directory group)
232 (let ((res (nnspool-request-article id)))
235 (set-buffer nntp-server-buffer)
236 (goto-char (point-min))
237 (when (search-forward "\n\n" nil t)
238 (delete-region (1- (point)) (point-max)))))
241 (defun nnspool-request-group (group &optional server dont-check)
243 (let ((pathname (nnspool-article-pathname group))
245 (if (not (file-directory-p pathname))
247 'nnspool "Invalid group name (no such directory): %s" group)
248 (setq nnspool-current-directory pathname)
249 (nnheader-report 'nnspool "Selected group %s" group)
252 (nnheader-report 'nnspool "Selected group %s" group)
254 ;; Yes, completely empty spool directories *are* possible.
255 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
256 (when (setq dir (directory-files pathname nil "^[0-9]+$" t))
258 (sort (mapcar (lambda (name) (string-to-int name)) dir) '<)))
261 "211 %d %d %d %s\n" (length dir) (car dir)
262 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
264 (nnheader-report 'nnspool "Empty group %s" group)
265 (nnheader-insert "211 0 0 0 %s\n" group))))))
267 (defun nnspool-request-type (group &optional article)
270 (defun nnspool-close-group (group &optional server)
273 (defun nnspool-request-list (&optional server)
274 "List active newsgroups."
276 (nnspool-find-file nnspool-active-file)))
278 (defun nnspool-request-list-newsgroups (&optional server)
279 "List newsgroups (defined in NNTP2)."
281 (nnspool-find-file nnspool-newsgroups-file)))
283 (defun nnspool-request-list-distributions (&optional server)
284 "List distributions (defined in NNTP2)."
286 (nnspool-find-file nnspool-distributions-file)))
288 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
289 (defun nnspool-request-newgroups (date &optional server)
290 "List groups created after DATE."
291 (if (nnspool-find-file nnspool-active-times-file)
293 ;; Find the last valid line.
294 (goto-char (point-max))
295 (while (and (not (looking-at
296 "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
297 (zerop (forward-line -1))))
298 (let ((seconds (nnspool-seconds-since-epoch date))
300 ;; Go through lines and add the latest groups to a list.
301 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
303 ;; We insert a .0 to make the list reader
304 ;; interpret the number as a float. It is far
305 ;; too big to be stored in a lisp integer.
306 (goto-char (1- (match-end 0)))
309 (goto-char (match-end 1))
310 (read (current-buffer)))
312 (setq groups (cons (buffer-substring
313 (match-beginning 1) (match-end 1))
315 (zerop (forward-line -1))))
318 (insert (car groups) " 0 0 y\n")
319 (setq groups (cdr groups))))
323 (defun nnspool-request-post (&optional server)
324 "Post a new news in current buffer."
326 (let* ((process-connection-type nil) ; t bugs out on Solaris
327 (inews-buffer (generate-new-buffer " *nnspool post*"))
330 (apply 'start-process "*nnspool inews*" inews-buffer
331 nnspool-inews-program nnspool-inews-switches)
333 (nnheader-report 'nnspool "inews error: %S" err)))))
335 ;; The inews program failed.
337 (nnheader-report 'nnspool "")
338 (set-process-sentinel proc 'nnspool-inews-sentinel)
339 (process-send-region proc (point-min) (point-max))
340 ;; We slap a condition-case around this, because the process may
341 ;; have exited already...
343 (process-send-eof proc)
347 (defun nnspool-inews-sentinel (proc status)
349 (set-buffer (process-buffer proc))
350 (goto-char (point-min))
351 (if (or (zerop (buffer-size))
352 (search-forward "spooled" nil t))
353 (kill-buffer (current-buffer))
354 ;; Make status message by folding lines.
355 (while (re-search-forward "[ \t\n]+" nil t)
356 (replace-match " " t t))
357 (nnheader-report 'nnspool "%s" (buffer-string))
358 (message "nnspool: %s" nnspool-status-string)
360 (run-hooks 'nnspool-rejected-article-hook))))
363 ;;; Internal functions.
365 (defun nnspool-retrieve-headers-with-nov (articles &optional fetch-old)
366 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
368 (let ((nov (nnheader-group-pathname
369 nnspool-current-group nnspool-nov-directory ".overview")))
370 (if (not (file-exists-p nov))
373 (set-buffer nntp-server-buffer)
375 (if nnspool-sift-nov-with-sed
376 (nnspool-sift-nov-with-sed articles nov)
377 (insert-file-contents nov)
379 (not (numberp fetch-old)))
380 t ; We want all the headers.
381 ;; First we find the first wanted line.
382 (nnspool-find-nov-line
383 (if fetch-old (max 1 (- (car articles) fetch-old))
385 (delete-region (point-min) (point))
386 ;; Then we find the last wanted line.
387 (if (nnspool-find-nov-line
388 (progn (while (cdr articles) (setq articles (cdr articles)))
391 (delete-region (point) (point-max))
392 ;; If the buffer is empty, this wasn't very successful.
393 (not (zerop (buffer-size))))))))))
395 (defun nnspool-find-nov-line (article)
396 (let ((max (point-max))
397 (min (goto-char (point-min)))
398 (cur (current-buffer))
402 (goto-char (/ (+ max min) 2))
404 (if (or (= (point) prev)
408 (cond ((> (setq num (read cur)) article)
414 (when (not (eq num article))
418 (= (setq num (read cur)) article)
424 (defun nnspool-sift-nov-with-sed (articles file)
425 (let ((first (car articles))
426 (last (progn (while (cdr articles) (setq articles (cdr articles)))
428 (call-process "awk" nil t nil
429 (format "BEGIN {firstmsg=%d; lastmsg=%d;}\n $1 >= firstmsg && $1 <= lastmsg {print;}"
430 (1- first) (1+ last))
433 ;; Fixed by fdc@cliwe.ping.de (Frank D. Cringle).
434 ;; Find out what group an article identified by a Message-ID is in.
435 (defun nnspool-find-id (id)
437 (set-buffer (get-buffer-create " *nnspool work*"))
438 (buffer-disable-undo (current-buffer))
441 (call-process "grep" nil t nil id nnspool-history-file)
443 (goto-char (point-min))
445 (if (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\([^ /\t\n]+\\)/\\([0-9]+\\)[ \t\n]")
446 (cons (match-string 1) (string-to-int (match-string 2))))
447 (kill-buffer (current-buffer)))))
449 (defun nnspool-find-file (file)
450 "Insert FILE in server buffer safely."
451 (set-buffer nntp-server-buffer)
454 (progn (insert-file-contents file) t)
457 (defun nnspool-possibly-change-directory (group)
460 (let ((pathname (nnspool-article-pathname group)))
461 (if (file-directory-p pathname)
462 (setq nnspool-current-directory pathname
463 nnspool-current-group group)
464 (nnheader-report 'nnspool "No such newsgroup: %s" group)))))
466 (defun nnspool-article-pathname (group &optional article)
467 "Find the path for GROUP."
468 (nnheader-group-pathname group nnspool-spool-directory article))
470 (defun nnspool-seconds-since-epoch (date)
471 (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
472 (timezone-parse-date date)))
473 (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
475 (aref (timezone-parse-date date) 3))))
476 (unix (encode-time (nth 2 ttime) (nth 1 ttime) (nth 0 ttime)
477 (nth 2 tdate) (nth 1 tdate) (nth 0 tdate)
479 (+ (* (car unix) 65536.0)
484 ;;; nnspool.el ends here