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 (if (nnspool-request-article id)
221 (set-buffer nntp-server-buffer)
222 (goto-char (point-min))
223 (if (search-forward "\n\n" nil t)
224 (delete-region (point-min) (point)))
227 (defun nnspool-request-head (id &optional group server)
228 "Select article head by message ID (or number)."
229 (nnspool-possibly-change-directory group)
230 (let ((res (nnspool-request-article id)))
233 (set-buffer nntp-server-buffer)
234 (goto-char (point-min))
235 (when (search-forward "\n\n" nil t)
236 (delete-region (1- (point)) (point-max)))))
239 (defun nnspool-request-group (group &optional server dont-check)
241 (let ((pathname (nnspool-article-pathname group))
243 (if (not (file-directory-p pathname))
245 'nnspool "Invalid group name (no such directory): %s" group)
246 (setq nnspool-current-directory pathname)
247 (nnheader-report 'nnspool "Selected group %s" group)
250 (nnheader-report 'nnspool "Selected group %s" group)
252 ;; Yes, completely empty spool directories *are* possible
253 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
254 (when (setq dir (directory-files pathname nil "^[0-9]+$" t))
256 (sort (mapcar (lambda (name) (string-to-int name)) dir) '<)))
258 (set-buffer nntp-server-buffer)
263 (format "211 %d %d %d %s\n" (length dir) (car dir)
264 (progn (while (cdr dir) (setq dir (cdr dir)))
268 (insert (format "211 0 0 0 %s\n" group))
269 (nnheader-report 'nnspool "Empty group %s" group)
272 (defun nnspool-request-type (group &optional article)
275 (defun nnspool-close-group (group &optional server)
278 (defun nnspool-request-list (&optional server)
279 "List active newsgroups."
281 (nnspool-find-file nnspool-active-file)))
283 (defun nnspool-request-list-newsgroups (&optional server)
284 "List newsgroups (defined in NNTP2)."
286 (nnspool-find-file nnspool-newsgroups-file)))
288 (defun nnspool-request-list-distributions (&optional server)
289 "List distributions (defined in NNTP2)."
291 (nnspool-find-file nnspool-distributions-file)))
293 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
294 (defun nnspool-request-newgroups (date &optional server)
295 "List groups created after DATE."
296 (if (nnspool-find-file nnspool-active-times-file)
298 ;; Find the last valid line.
299 (goto-char (point-max))
300 (while (and (not (looking-at
301 "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
302 (zerop (forward-line -1))))
303 (let ((seconds (nnspool-seconds-since-epoch date))
305 ;; Go through lines and add the latest groups to a list.
306 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
308 ;; We insert a .0 to make the list reader
309 ;; interpret the number as a float. It is far
310 ;; too big to be stored in a lisp integer.
311 (goto-char (1- (match-end 0)))
314 (goto-char (match-end 1))
315 (read (current-buffer)))
317 (setq groups (cons (buffer-substring
318 (match-beginning 1) (match-end 1))
320 (zerop (forward-line -1))))
323 (insert (car groups) " 0 0 y\n")
324 (setq groups (cdr groups))))
328 (defun nnspool-request-post (&optional server)
329 "Post a new news in current buffer."
331 (let* ((process-connection-type nil) ; t bugs out on Solaris
332 (inews-buffer (generate-new-buffer " *nnspool post*"))
335 (apply 'start-process "*nnspool inews*" inews-buffer
336 nnspool-inews-program nnspool-inews-switches)
338 (setq nnspool-status-string (format "inews error: %S" err))
341 ;; The inews program failed.
343 (setq nnspool-status-string "")
344 (set-process-sentinel proc 'nnspool-inews-sentinel)
345 (process-send-region proc (point-min) (point-max))
346 ;; We slap a condition-case around this, because the process may
347 ;; have exited already...
349 (process-send-eof proc)
353 (defun nnspool-inews-sentinel (proc status)
355 (set-buffer (process-buffer proc))
356 (goto-char (point-min))
357 (if (or (zerop (buffer-size))
358 (search-forward "spooled" nil t))
359 (kill-buffer (current-buffer))
360 ;; Make status message by folding lines.
361 (while (re-search-forward "[ \t\n]+" nil t)
362 (replace-match " " t t))
363 (setq nnspool-status-string (buffer-string))
364 (message "nnspool: %s" nnspool-status-string)
366 (run-hooks 'nnspool-rejected-article-hook))))
369 ;;; Internal functions.
371 (defun nnspool-retrieve-headers-with-nov (articles &optional fetch-old)
372 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
374 (let ((nov (nnheader-group-pathname
375 nnspool-current-group nnspool-nov-directory ".overview")))
376 (if (not (file-exists-p nov))
379 (set-buffer nntp-server-buffer)
381 (if nnspool-sift-nov-with-sed
382 (nnspool-sift-nov-with-sed articles nov)
383 (insert-file-contents nov)
385 (not (numberp fetch-old)))
386 t ; We want all the headers.
387 ;; First we find the first wanted line.
388 (nnspool-find-nov-line
389 (if fetch-old (max 1 (- (car articles) fetch-old))
391 (delete-region (point-min) (point))
392 ;; Then we find the last wanted line.
393 (if (nnspool-find-nov-line
394 (progn (while (cdr articles) (setq articles (cdr articles)))
397 (delete-region (point) (point-max))
398 ;; If the buffer is empty, this wasn't very successful.
399 (not (zerop (buffer-size))))))))))
401 (defun nnspool-find-nov-line (article)
402 (let ((max (point-max))
403 (min (goto-char (point-min)))
404 (cur (current-buffer))
408 (goto-char (/ (+ max min) 2))
410 (if (or (= (point) prev)
414 (cond ((> (setq num (read cur)) article)
420 (when (not (eq num article))
424 (= (setq num (read cur)) article)
430 (defun nnspool-sift-nov-with-sed (articles file)
431 (let ((first (car articles))
432 (last (progn (while (cdr articles) (setq articles (cdr articles)))
434 (call-process "awk" nil t nil
435 (format "BEGIN {firstmsg=%d; lastmsg=%d;}\n $1 >= firstmsg && $1 <= lastmsg {print;}"
436 (1- first) (1+ last))
439 ;; Fixed by fdc@cliwe.ping.de (Frank D. Cringle).
440 ;; Find out what group an article identified by a Message-ID is in.
441 (defun nnspool-find-id (id)
443 (set-buffer (get-buffer-create " *nnspool work*"))
444 (buffer-disable-undo (current-buffer))
447 (call-process "grep" nil t nil id nnspool-history-file)
449 (goto-char (point-min))
451 (if (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\([^ /\t\n]+\\)/\\([0-9]+\\)[ \t\n]")
452 (cons (match-string 1) (string-to-int (match-string 2))))
453 (kill-buffer (current-buffer)))))
455 (defun nnspool-find-file (file)
456 "Insert FILE in server buffer safely."
457 (set-buffer nntp-server-buffer)
460 (progn (insert-file-contents file) t)
463 (defun nnspool-possibly-change-directory (group)
465 (let ((pathname (nnspool-article-pathname group)))
466 (if (file-directory-p pathname)
468 (setq nnspool-current-directory pathname)
469 (setq nnspool-current-group group))
470 (setq nnspool-status-string
471 (format "No such newsgroup: %s" group))
475 (defun nnspool-article-pathname (group &optional article)
476 "Find the path for GROUP."
477 (nnheader-group-pathname group nnspool-spool-directory article))
479 (defun nnspool-seconds-since-epoch (date)
480 (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
481 (timezone-parse-date date)))
482 (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
484 (aref (timezone-parse-date date) 3))))
485 (unix (encode-time (nth 2 ttime) (nth 1 ttime) (nth 0 ttime)
486 (nth 2 tdate) (nth 1 tdate) (nth 0 tdate)
488 (+ (* (car unix) 65536.0)
493 ;;; nnspool.el ends here