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 (default-directory nnspool-current-directory)
128 (do-message (and (numberp nnspool-large-newsgroup)
129 (> number nnspool-large-newsgroup)))
131 (if (and (numberp (car articles))
132 (nnspool-retrieve-headers-with-nov articles fetch-old))
133 ;; We successfully retrieved the NOV headers.
135 ;; No NOV headers here, so we do it the hard way.
136 (while (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 (int-to-string article)))
145 ;; Insert the head of the article.
147 (file-exists-p file))
149 (princ article (current-buffer))
150 (insert " Article retrieved.\n")
152 (inline (nnheader-insert-head file))
154 (search-forward "\n\n" nil t)
157 (delete-region (point) (point-max)))
160 (zerop (% (incf count) 20))
161 (message "nnspool: Receiving headers... %d%%"
162 (/ (* count 100) number))))
165 (message "nnspool: Receiving headers...done"))
167 ;; Fold continuation lines.
168 (nnheader-fold-continuation-lines)
171 (defun nnspool-open-server (server &optional defs)
172 (nnheader-change-server 'nnspool server defs)
174 ((not (file-exists-p nnspool-spool-directory))
175 (nnspool-close-server)
176 (nnheader-report 'nnspool "Spool directory doesn't exist: %s"
177 nnspool-spool-directory))
178 ((not (file-directory-p (file-truename nnspool-spool-directory)))
179 (nnspool-close-server)
180 (nnheader-report 'nnspool "Not a directory: %s" nnspool-spool-directory))
182 (nnheader-report 'nnspool "Opened server %s using directory %s"
183 server nnspool-spool-directory)
186 (defun nnspool-close-server (&optional server)
187 (setq nnspool-current-server nil)
190 (defun nnspool-server-opened (&optional server)
191 (and (equal server nnspool-current-server)
193 (buffer-name nntp-server-buffer)))
195 (defun nnspool-status-message (&optional server)
196 "Return server status response as string."
197 nnspool-status-string)
199 (defun nnspool-request-article (id &optional group server buffer)
200 "Select article by message ID (or number)."
201 (nnspool-possibly-change-directory group)
202 (let ((nntp-server-buffer (or buffer nntp-server-buffer))
205 ;; This is a Message-ID.
206 (when (setq ag (nnspool-find-id id))
207 (setq file (nnspool-article-pathname (car ag) (cdr ag))))
208 (setq file (nnspool-article-pathname nnspool-current-group id)))
211 (not (file-directory-p file))
212 (save-excursion (nnspool-find-file file))
213 ;; We return the article number and group name.
215 (cons nnspool-current-group id)
218 (defun nnspool-request-body (id &optional group server)
219 "Select article body by message ID (or number)."
220 (nnspool-possibly-change-directory group)
221 (let ((res (nnspool-request-article id)))
224 (set-buffer nntp-server-buffer)
225 (goto-char (point-min))
226 (when (search-forward "\n\n" nil t)
227 (delete-region (point-min) (point)))
230 (defun nnspool-request-head (id &optional group server)
231 "Select article head by message ID (or number)."
232 (nnspool-possibly-change-directory group)
233 (let ((res (nnspool-request-article id)))
236 (set-buffer nntp-server-buffer)
237 (goto-char (point-min))
238 (when (search-forward "\n\n" nil t)
239 (delete-region (1- (point)) (point-max)))))
242 (defun nnspool-request-group (group &optional server dont-check)
244 (let ((pathname (nnspool-article-pathname group))
246 (if (not (file-directory-p pathname))
248 'nnspool "Invalid group name (no such directory): %s" group)
249 (setq nnspool-current-directory pathname)
250 (nnheader-report 'nnspool "Selected group %s" group)
253 (nnheader-report 'nnspool "Selected group %s" group)
255 ;; Yes, completely empty spool directories *are* possible.
256 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
257 (when (setq dir (directory-files pathname nil "^[0-9]+$" t))
259 (sort (mapcar (lambda (name) (string-to-int name)) dir) '<)))
262 "211 %d %d %d %s\n" (length dir) (car dir)
263 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
265 (nnheader-report 'nnspool "Empty group %s" group)
266 (nnheader-insert "211 0 0 0 %s\n" group))))))
268 (defun nnspool-request-type (group &optional article)
271 (defun nnspool-close-group (group &optional server)
274 (defun nnspool-request-list (&optional server)
275 "List active newsgroups."
277 (nnspool-find-file nnspool-active-file)))
279 (defun nnspool-request-list-newsgroups (&optional server)
280 "List newsgroups (defined in NNTP2)."
282 (nnspool-find-file nnspool-newsgroups-file)))
284 (defun nnspool-request-list-distributions (&optional server)
285 "List distributions (defined in NNTP2)."
287 (nnspool-find-file nnspool-distributions-file)))
289 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
290 (defun nnspool-request-newgroups (date &optional server)
291 "List groups created after DATE."
292 (if (nnspool-find-file nnspool-active-times-file)
294 ;; Find the last valid line.
295 (goto-char (point-max))
296 (while (and (not (looking-at
297 "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
298 (zerop (forward-line -1))))
299 (let ((seconds (nnspool-seconds-since-epoch date))
301 ;; Go through lines and add the latest groups to a list.
302 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
304 ;; We insert a .0 to make the list reader
305 ;; interpret the number as a float. It is far
306 ;; too big to be stored in a lisp integer.
307 (goto-char (1- (match-end 0)))
310 (goto-char (match-end 1))
311 (read (current-buffer)))
313 (setq groups (cons (buffer-substring
314 (match-beginning 1) (match-end 1))
316 (zerop (forward-line -1))))
319 (insert (car groups) " 0 0 y\n")
320 (setq groups (cdr groups))))
324 (defun nnspool-request-post (&optional server)
325 "Post a new news in current buffer."
327 (let* ((process-connection-type nil) ; t bugs out on Solaris
328 (inews-buffer (generate-new-buffer " *nnspool post*"))
331 (apply 'start-process "*nnspool inews*" inews-buffer
332 nnspool-inews-program nnspool-inews-switches)
334 (nnheader-report 'nnspool "inews error: %S" err)))))
336 ;; The inews program failed.
338 (nnheader-report 'nnspool "")
339 (set-process-sentinel proc 'nnspool-inews-sentinel)
340 (process-send-region proc (point-min) (point-max))
341 ;; We slap a condition-case around this, because the process may
342 ;; have exited already...
344 (process-send-eof proc)
348 (defun nnspool-inews-sentinel (proc status)
350 (set-buffer (process-buffer proc))
351 (goto-char (point-min))
352 (if (or (zerop (buffer-size))
353 (search-forward "spooled" nil t))
354 (kill-buffer (current-buffer))
355 ;; Make status message by folding lines.
356 (while (re-search-forward "[ \t\n]+" nil t)
357 (replace-match " " t t))
358 (nnheader-report 'nnspool "%s" (buffer-string))
359 (message "nnspool: %s" nnspool-status-string)
361 (run-hooks 'nnspool-rejected-article-hook))))
364 ;;; Internal functions.
366 (defun nnspool-retrieve-headers-with-nov (articles &optional fetch-old)
367 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
369 (let ((nov (nnheader-group-pathname
370 nnspool-current-group nnspool-nov-directory ".overview")))
371 (if (not (file-exists-p nov))
374 (set-buffer nntp-server-buffer)
376 (if nnspool-sift-nov-with-sed
377 (nnspool-sift-nov-with-sed articles nov)
378 (insert-file-contents nov)
380 (not (numberp fetch-old)))
381 t ; We want all the headers.
382 ;; First we find the first wanted line.
383 (nnspool-find-nov-line
384 (if fetch-old (max 1 (- (car articles) fetch-old))
386 (delete-region (point-min) (point))
387 ;; Then we find the last wanted line.
388 (if (nnspool-find-nov-line
389 (progn (while (cdr articles) (setq articles (cdr articles)))
392 (delete-region (point) (point-max))
393 ;; If the buffer is empty, this wasn't very successful.
394 (not (zerop (buffer-size))))))))))
396 (defun nnspool-find-nov-line (article)
397 (let ((max (point-max))
398 (min (goto-char (point-min)))
399 (cur (current-buffer))
403 (goto-char (/ (+ max min) 2))
405 (if (or (= (point) prev)
409 (cond ((> (setq num (read cur)) article)
415 (when (not (eq num article))
419 (= (setq num (read cur)) article)
425 (defun nnspool-sift-nov-with-sed (articles file)
426 (let ((first (car articles))
427 (last (progn (while (cdr articles) (setq articles (cdr articles)))
429 (call-process "awk" nil t nil
430 (format "BEGIN {firstmsg=%d; lastmsg=%d;}\n $1 >= firstmsg && $1 <= lastmsg {print;}"
431 (1- first) (1+ last))
434 ;; Fixed by fdc@cliwe.ping.de (Frank D. Cringle).
435 ;; Find out what group an article identified by a Message-ID is in.
436 (defun nnspool-find-id (id)
438 (set-buffer (get-buffer-create " *nnspool work*"))
439 (buffer-disable-undo (current-buffer))
442 (call-process "grep" nil t nil id nnspool-history-file)
444 (goto-char (point-min))
446 (if (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\([^ /\t\n]+\\)/\\([0-9]+\\)[ \t\n]")
447 (cons (match-string 1) (string-to-int (match-string 2))))
448 (kill-buffer (current-buffer)))))
450 (defun nnspool-find-file (file)
451 "Insert FILE in server buffer safely."
452 (set-buffer nntp-server-buffer)
455 (progn (insert-file-contents file) t)
458 (defun nnspool-possibly-change-directory (group)
461 (let ((pathname (nnspool-article-pathname group)))
462 (if (file-directory-p pathname)
463 (setq nnspool-current-directory pathname
464 nnspool-current-group group)
465 (nnheader-report 'nnspool "No such newsgroup: %s" group)))))
467 (defun nnspool-article-pathname (group &optional article)
468 "Find the path for GROUP."
469 (nnheader-group-pathname group nnspool-spool-directory article))
471 (defun nnspool-seconds-since-epoch (date)
472 (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
473 (timezone-parse-date date)))
474 (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
476 (aref (timezone-parse-date date) 3))))
477 (unix (encode-time (nth 2 ttime) (nth 1 ttime) (nth 0 ttime)
478 (nth 2 tdate) (nth 1 tdate) (nth 0 tdate)
480 (+ (* (car unix) 65536.0)
485 ;;; nnspool.el ends here