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.
33 (defvar nnspool-inews-program news-inews-program
34 "Program to post news.
35 This is most commonly `inews' or `injnews'.")
37 (defvar nnspool-inews-switches '("-h")
38 "Switches for nnspool-request-post to pass to `inews' for posting news.
39 If you are using Cnews, you probably should set this variable to nil.")
41 (defvar nnspool-spool-directory (file-name-as-directory news-path)
42 "Local news spool directory.")
44 (defvar nnspool-nov-directory (concat nnspool-spool-directory "over.view/")
45 "Local news nov directory.")
47 (defvar nnspool-lib-dir "/usr/lib/news/"
48 "Where the local news library files are stored.")
50 (defvar nnspool-active-file (concat nnspool-lib-dir "active")
51 "Local news active file.")
53 (defvar nnspool-newsgroups-file (concat nnspool-lib-dir "newsgroups")
54 "Local news newsgroups file.")
56 (defvar nnspool-distributions-file (concat nnspool-lib-dir "distributions")
57 "Local news distributions file.")
59 (defvar nnspool-history-file (concat nnspool-lib-dir "history")
60 "Local news history file.")
62 (defvar nnspool-active-times-file (concat nnspool-lib-dir "active.times")
63 "Local news active date file.")
65 (defvar nnspool-large-newsgroup 50
66 "The number of the articles which indicates a large newsgroup.
67 If the number of the articles is greater than the value, verbose
68 messages will be shown to indicate the current status.")
70 (defvar nnspool-nov-is-evil nil
71 "Non-nil means that nnspool will never return NOV lines instead of headers.")
73 (defconst nnspool-sift-nov-with-sed nil
74 "If non-nil, use sed to get the relevant portion from the overview file.
75 If nil, nnspool will load the entire file into a buffer and process it
78 (defvar nnspool-rejected-article-hook nil
79 "*A hook that will be run when an article has been rejected by the server.")
83 (defconst nnspool-version "nnspool 2.0"
84 "Version numbers of this version of NNSPOOL.")
86 (defvar nnspool-current-directory nil
87 "Current news group directory.")
89 (defvar nnspool-current-group nil)
90 (defvar nnspool-status-string "")
94 (defvar nnspool-current-server nil)
95 (defvar nnspool-server-alist nil)
96 (defvar nnspool-server-variables
98 (list 'nnspool-inews-program nnspool-inews-program)
99 (list 'nnspool-inews-switches nnspool-inews-switches)
100 (list 'nnspool-spool-directory nnspool-spool-directory)
101 (list 'nnspool-nov-directory nnspool-nov-directory)
102 (list 'nnspool-lib-dir nnspool-lib-dir)
103 (list 'nnspool-active-file nnspool-active-file)
104 (list 'nnspool-newsgroups-file nnspool-newsgroups-file)
105 (list 'nnspool-distributions-file nnspool-distributions-file)
106 (list 'nnspool-history-file nnspool-history-file)
107 (list 'nnspool-active-times-file nnspool-active-times-file)
108 (list 'nnspool-large-newsgroup nnspool-large-newsgroup)
109 (list 'nnspool-nov-is-evil nnspool-nov-is-evil)
110 (list '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-init-server-buffer)
171 (if (equal server nnspool-current-server)
173 (if nnspool-current-server
174 (setq nnspool-server-alist
175 (cons (list nnspool-current-server
176 (nnheader-save-variables nnspool-server-variables))
177 nnspool-server-alist)))
178 (let ((state (assoc server nnspool-server-alist)))
181 (nnheader-restore-variables (nth 1 state))
182 (setq nnspool-server-alist (delq state nnspool-server-alist)))
183 (nnheader-set-init-variables nnspool-server-variables defs)))
184 (setq nnspool-current-server server)))
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 (if (nnspool-request-article id)
223 (set-buffer nntp-server-buffer)
224 (goto-char (point-min))
225 (if (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) '<)))
260 (set-buffer nntp-server-buffer)
265 (format "211 %d %d %d %s\n" (length dir) (car dir)
266 (progn (while (cdr dir) (setq dir (cdr dir)))
270 (insert (format "211 0 0 0 %s\n" group))
271 (nnheader-report 'nnspool "Empty group %s" group)
274 (defun nnspool-request-type (group &optional article)
277 (defun nnspool-close-group (group &optional server)
280 (defun nnspool-request-list (&optional server)
281 "List active newsgroups."
283 (nnspool-find-file nnspool-active-file)))
285 (defun nnspool-request-list-newsgroups (&optional server)
286 "List newsgroups (defined in NNTP2)."
288 (nnspool-find-file nnspool-newsgroups-file)))
290 (defun nnspool-request-list-distributions (&optional server)
291 "List distributions (defined in NNTP2)."
293 (nnspool-find-file nnspool-distributions-file)))
295 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
296 (defun nnspool-request-newgroups (date &optional server)
297 "List groups created after DATE."
298 (if (nnspool-find-file nnspool-active-times-file)
300 ;; Find the last valid line.
301 (goto-char (point-max))
302 (while (and (not (looking-at
303 "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
304 (zerop (forward-line -1))))
305 (let ((seconds (nnspool-seconds-since-epoch date))
307 ;; Go through lines and add the latest groups to a list.
308 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
310 ;; We insert a .0 to make the list reader
311 ;; interpret the number as a float. It is far
312 ;; too big to be stored in a lisp integer.
313 (goto-char (1- (match-end 0)))
316 (goto-char (match-end 1))
317 (read (current-buffer)))
319 (setq groups (cons (buffer-substring
320 (match-beginning 1) (match-end 1))
322 (zerop (forward-line -1))))
325 (insert (car groups) " 0 0 y\n")
326 (setq groups (cdr groups))))
330 (defun nnspool-request-post (&optional server)
331 "Post a new news in current buffer."
333 (let* ((process-connection-type nil) ; t bugs out on Solaris
334 (inews-buffer (generate-new-buffer " *nnspool post*"))
337 (apply 'start-process "*nnspool inews*" inews-buffer
338 nnspool-inews-program nnspool-inews-switches)
340 (setq nnspool-status-string (format "inews error: %S" err))
343 ;; The inews program failed.
345 (setq nnspool-status-string "")
346 (set-process-sentinel proc 'nnspool-inews-sentinel)
347 (process-send-region proc (point-min) (point-max))
348 ;; We slap a condition-case around this, because the process may
349 ;; have exited already...
351 (process-send-eof proc)
355 (defun nnspool-inews-sentinel (proc status)
357 (set-buffer (process-buffer proc))
358 (goto-char (point-min))
359 (if (or (zerop (buffer-size))
360 (search-forward "spooled" nil t))
361 (kill-buffer (current-buffer))
362 ;; Make status message by folding lines.
363 (while (re-search-forward "[ \t\n]+" nil t)
364 (replace-match " " t t))
365 (setq nnspool-status-string (buffer-string))
366 (message "nnspool: %s" nnspool-status-string)
368 (run-hooks 'nnspool-rejected-article-hook))))
371 ;;; Internal functions.
373 (defun nnspool-retrieve-headers-with-nov (articles &optional fetch-old)
374 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
376 (let ((nov (nnheader-group-pathname
377 nnspool-current-group nnspool-nov-directory ".overview")))
378 (if (not (file-exists-p nov))
381 (set-buffer nntp-server-buffer)
383 (if nnspool-sift-nov-with-sed
384 (nnspool-sift-nov-with-sed articles nov)
385 (insert-file-contents nov)
387 (not (numberp fetch-old)))
388 t ; We want all the headers.
389 ;; First we find the first wanted line.
390 (nnspool-find-nov-line
391 (if fetch-old (max 1 (- (car articles) fetch-old))
393 (delete-region (point-min) (point))
394 ;; Then we find the last wanted line.
395 (if (nnspool-find-nov-line
396 (progn (while (cdr articles) (setq articles (cdr articles)))
399 (delete-region (point) (point-max))
400 ;; If the buffer is empty, this wasn't very successful.
401 (not (zerop (buffer-size))))))))))
403 (defun nnspool-find-nov-line (article)
404 (let ((max (point-max))
405 (min (goto-char (point-min)))
406 (cur (current-buffer))
410 (goto-char (/ (+ max min) 2))
412 (if (or (= (point) prev)
416 (cond ((> (setq num (read cur)) article)
422 (when (not (eq num article))
426 (= (setq num (read cur)) article)
432 (defun nnspool-sift-nov-with-sed (articles file)
433 (let ((first (car articles))
434 (last (progn (while (cdr articles) (setq articles (cdr articles)))
436 (call-process "awk" nil t nil
437 (format "BEGIN {firstmsg=%d; lastmsg=%d;}\n $1 >= firstmsg && $1 <= lastmsg {print;}"
438 (1- first) (1+ last))
441 ;; Fixed by fdc@cliwe.ping.de (Frank D. Cringle).
442 ;; Find out what group an article identified by a Message-ID is in.
443 (defun nnspool-find-id (id)
445 (set-buffer (get-buffer-create " *nnspool work*"))
446 (buffer-disable-undo (current-buffer))
449 (call-process "grep" nil t nil id nnspool-history-file)
451 (goto-char (point-min))
453 (if (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\([^ /\t\n]+\\)/\\([0-9]+\\)[ \t\n]")
454 (cons (match-string 1) (string-to-int (match-string 2))))
455 (kill-buffer (current-buffer)))))
457 (defun nnspool-find-file (file)
458 "Insert FILE in server buffer safely."
459 (set-buffer nntp-server-buffer)
462 (progn (insert-file-contents file) t)
465 (defun nnspool-possibly-change-directory (group)
467 (let ((pathname (nnspool-article-pathname group)))
468 (if (file-directory-p pathname)
470 (setq nnspool-current-directory pathname)
471 (setq nnspool-current-group group))
472 (setq nnspool-status-string
473 (format "No such newsgroup: %s" group))
477 (defun nnspool-article-pathname (group &optional article)
478 "Find the path for GROUP."
479 (nnheader-group-pathname group nnspool-spool-directory article))
481 (defun nnspool-seconds-since-epoch (date)
482 (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
483 (timezone-parse-date date)))
484 (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
486 (aref (timezone-parse-date date) 3))))
487 (unix (encode-time (nth 2 ttime) (nth 1 ttime) (nth 0 ttime)
488 (nth 2 tdate) (nth 1 tdate) (nth 0 tdate)
490 (+ (* (car unix) 65536.0)
495 ;;; nnspool.el ends here