1 ;;; nnspool.el --- spool access for GNU Emacs
3 ;; Copyright (C) 1988, 1989, 1990, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 2000 Free Software Foundation, Inc.
6 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
34 (eval-when-compile (require 'cl))
36 (nnoo-declare nnspool)
38 (defvoo nnspool-inews-program news-inews-program
39 "Program to post news.
40 This is most commonly `inews' or `injnews'.")
42 (defvoo nnspool-inews-switches '("-h" "-S")
43 "Switches for nnspool-request-post to pass to `inews' for posting news.
44 If you are using Cnews, you probably should set this variable to nil.")
46 (defvoo nnspool-spool-directory (file-name-as-directory news-path)
47 "Local news spool directory.")
49 (defvoo nnspool-nov-directory (concat nnspool-spool-directory "over.view/")
50 "Local news nov directory.")
52 (defvoo nnspool-lib-dir
53 (if (file-exists-p "/usr/lib/news/active")
56 "Where the local news library files are stored.")
58 (defvoo nnspool-active-file (concat nnspool-lib-dir "active")
59 "Local news active file.")
61 (defvoo nnspool-newsgroups-file (concat nnspool-lib-dir "newsgroups")
62 "Local news newsgroups file.")
64 (defvoo nnspool-distributions-file (concat nnspool-lib-dir "distribs.pat")
65 "Local news distributions file.")
67 (defvoo nnspool-history-file (concat nnspool-lib-dir "history")
68 "Local news history file.")
70 (defvoo nnspool-active-times-file (concat nnspool-lib-dir "active.times")
71 "Local news active date file.")
73 (defvoo nnspool-large-newsgroup 50
74 "The number of the articles which indicates a large newsgroup.
75 If the number of the articles is greater than the value, verbose
76 messages will be shown to indicate the current status.")
78 (defvoo nnspool-nov-is-evil nil
79 "Non-nil means that nnspool will never return NOV lines instead of headers.")
81 (defconst nnspool-sift-nov-with-sed nil
82 "If non-nil, use sed to get the relevant portion from the overview file.
83 If nil, nnspool will load the entire file into a buffer and process it
86 (defvoo nnspool-rejected-article-hook nil
87 "*A hook that will be run when an article has been rejected by the server.")
89 (defvoo nnspool-file-coding-system nnheader-file-coding-system
90 "Coding system for nnspool.")
94 (defconst nnspool-version "nnspool 2.0"
95 "Version numbers of this version of NNSPOOL.")
97 (defvoo nnspool-current-directory nil
98 "Current news group directory.")
100 (defvoo nnspool-current-group nil)
101 (defvoo nnspool-status-string "")
104 ;;; Interface functions.
106 (nnoo-define-basics nnspool)
108 (deffoo nnspool-retrieve-headers (articles &optional group server fetch-old)
109 "Retrieve the headers of ARTICLES."
111 (set-buffer nntp-server-buffer)
113 (when (nnspool-possibly-change-directory group)
114 (let* ((number (length articles))
116 (default-directory nnspool-current-directory)
117 (do-message (and (numberp nnspool-large-newsgroup)
118 (> number nnspool-large-newsgroup)))
119 (nnheader-file-coding-system nnspool-file-coding-system)
121 (if (and (numberp (car articles))
122 (nnspool-retrieve-headers-with-nov articles fetch-old))
123 ;; We successfully retrieved the NOV headers.
125 ;; No NOV headers here, so we do it the hard way.
126 (while (setq article (pop articles))
127 (if (stringp article)
128 ;; This is a Message-ID.
129 (setq ag (nnspool-find-id article)
130 file (and ag (nnspool-article-pathname
133 ;; This is an article in the current group.
134 (setq file (int-to-string article)))
135 ;; Insert the head of the article.
137 (file-exists-p file))
139 (princ article (current-buffer))
140 (insert " Article retrieved.\n")
142 (inline (nnheader-insert-head file))
144 (if (search-forward "\n\n" nil t)
148 (goto-char (point-max))
152 (delete-region (point) (point-max)))
155 (zerop (% (incf count) 20))
156 (nnheader-message 5 "nnspool: Receiving headers... %d%%"
157 (/ (* count 100) number))))
160 (nnheader-message 5 "nnspool: Receiving headers...done"))
162 ;; Fold continuation lines.
163 (nnheader-fold-continuation-lines)
166 (deffoo nnspool-open-server (server &optional defs)
167 (nnoo-change-server 'nnspool server defs)
169 ((not (file-exists-p nnspool-spool-directory))
170 (nnspool-close-server)
171 (nnheader-report 'nnspool "Spool directory doesn't exist: %s"
172 nnspool-spool-directory))
173 ((not (file-directory-p
175 (file-truename nnspool-spool-directory))))
176 (nnspool-close-server)
177 (nnheader-report 'nnspool "Not a directory: %s" nnspool-spool-directory))
178 ((not (file-exists-p nnspool-active-file))
179 (nnheader-report 'nnspool "The active file doesn't exist: %s"
180 nnspool-active-file))
182 (nnheader-report 'nnspool "Opened server %s using directory %s"
183 server nnspool-spool-directory)
186 (deffoo nnspool-request-article (id &optional group server buffer)
187 "Select article by message ID (or number)."
188 (nnspool-possibly-change-directory group)
189 (let ((nntp-server-buffer (or buffer nntp-server-buffer))
192 ;; This is a Message-ID.
193 (when (setq ag (nnspool-find-id id))
194 (setq file (nnspool-article-pathname (car ag) (cdr ag))))
195 (setq file (nnspool-article-pathname nnspool-current-group id)))
198 (not (file-directory-p file))
199 (save-excursion (nnspool-find-file file))
200 ;; We return the article number and group name.
202 (cons nnspool-current-group id)
205 (deffoo nnspool-request-body (id &optional group server)
206 "Select article body by message ID (or number)."
207 (nnspool-possibly-change-directory group)
208 (let ((res (nnspool-request-article id)))
211 (set-buffer nntp-server-buffer)
212 (goto-char (point-min))
213 (when (search-forward "\n\n" nil t)
214 (delete-region (point-min) (point)))
217 (deffoo nnspool-request-head (id &optional group server)
218 "Select article head 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 (1- (point)) (point-max)))
227 (nnheader-fold-continuation-lines)))
230 (deffoo nnspool-request-group (group &optional server dont-check)
232 (let ((pathname (nnspool-article-pathname group))
234 (if (not (file-directory-p pathname))
236 'nnspool "Invalid group name (no such directory): %s" group)
237 (setq nnspool-current-directory pathname)
238 (nnheader-report 'nnspool "Selected group %s" group)
241 (nnheader-report 'nnspool "Selected group %s" group)
243 ;; Yes, completely empty spool directories *are* possible.
244 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
245 (when (setq dir (directory-files pathname nil "^[0-9]+$" t))
247 (sort (mapcar (lambda (name) (string-to-int name)) dir) '<)))
250 "211 %d %d %d %s\n" (length dir) (car dir)
251 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
253 (nnheader-report 'nnspool "Empty group %s" group)
254 (nnheader-insert "211 0 0 0 %s\n" group))))))
256 (deffoo nnspool-request-type (group &optional article)
259 (deffoo nnspool-close-group (group &optional server)
262 (deffoo nnspool-request-list (&optional server)
263 "List active newsgroups."
265 (or (nnspool-find-file nnspool-active-file)
266 (nnheader-report 'nnspool (nnheader-file-error nnspool-active-file)))))
268 (deffoo nnspool-request-list-newsgroups (&optional server)
269 "List newsgroups (defined in NNTP2)."
271 (or (nnspool-find-file nnspool-newsgroups-file)
272 (nnheader-report 'nnspool (nnheader-file-error
273 nnspool-newsgroups-file)))))
275 (deffoo nnspool-request-list-distributions (&optional server)
276 "List distributions (defined in NNTP2)."
278 (or (nnspool-find-file nnspool-distributions-file)
279 (nnheader-report 'nnspool (nnheader-file-error
280 nnspool-distributions-file)))))
282 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
283 (deffoo nnspool-request-newgroups (date &optional server)
284 "List groups created after DATE."
285 (if (nnspool-find-file nnspool-active-times-file)
287 ;; Find the last valid line.
288 (goto-char (point-max))
289 (while (and (not (looking-at
290 "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
291 (zerop (forward-line -1))))
292 (let ((seconds (time-to-seconds (date-to-time date)))
294 ;; Go through lines and add the latest groups to a list.
295 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
297 ;; We insert a .0 to make the list reader
298 ;; interpret the number as a float. It is far
299 ;; too big to be stored in a lisp integer.
300 (goto-char (1- (match-end 0)))
303 (goto-char (match-end 1))
304 (read (current-buffer)))
306 (push (buffer-substring
307 (match-beginning 1) (match-end 1))
309 (zerop (forward-line -1))))
312 (insert (car groups) " 0 0 y\n")
313 (setq groups (cdr groups))))
317 (deffoo nnspool-request-post (&optional server)
318 "Post a new news in current buffer."
320 (let* ((process-connection-type nil) ; t bugs out on Solaris
321 (inews-buffer (generate-new-buffer " *nnspool post*"))
324 (apply 'start-process "*nnspool inews*" inews-buffer
325 nnspool-inews-program nnspool-inews-switches)
327 (nnheader-report 'nnspool "inews error: %S" err)))))
329 ;; The inews program failed.
331 (nnheader-report 'nnspool "")
332 (set-process-sentinel proc 'nnspool-inews-sentinel)
333 (process-send-region proc (point-min) (point-max))
334 ;; We slap a condition-case around this, because the process may
335 ;; have exited already...
337 (process-send-eof proc))
342 ;;; Internal functions.
344 (defun nnspool-inews-sentinel (proc status)
346 (set-buffer (process-buffer proc))
347 (goto-char (point-min))
348 (if (or (zerop (buffer-size))
349 (search-forward "spooled" nil t))
350 (kill-buffer (current-buffer))
351 ;; Make status message by folding lines.
352 (while (re-search-forward "[ \t\n]+" nil t)
353 (replace-match " " t t))
354 (nnheader-report 'nnspool "%s" (buffer-string))
355 (nnheader-message 5 "nnspool: %s" nnspool-status-string)
357 (run-hooks 'nnspool-rejected-article-hook))))
359 (defun nnspool-retrieve-headers-with-nov (articles &optional fetch-old)
360 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
362 (let ((nov (nnheader-group-pathname
363 nnspool-current-group nnspool-nov-directory ".overview"))
365 (nnheader-file-coding-system nnspool-file-coding-system)
367 (if (not (file-exists-p nov))
370 (set-buffer nntp-server-buffer)
372 (if nnspool-sift-nov-with-sed
373 (nnspool-sift-nov-with-sed articles nov)
374 (nnheader-insert-file-contents nov)
376 (not (numberp fetch-old)))
377 t ; We want all the headers.
379 ;; Delete unwanted NOV lines.
380 (nnheader-nov-delete-outside-range
381 (if fetch-old (max 1 (- (car articles) fetch-old))
383 (car (last articles)))
384 ;; If the buffer is empty, this wasn't very successful.
385 (unless (zerop (buffer-size))
386 ;; We check what the last article number was.
387 ;; The NOV file may be out of sync with the articles
390 (setq last (read (current-buffer)))
391 (if (= last (car articles))
392 ;; Yup, it's all there.
394 ;; Perhaps not. We try to find the missing articles.
396 (<= last (car arts)))
398 ;; The articles in `arts' are missing from the buffer.
400 (nnspool-insert-nov-head (pop arts)))
403 (defun nnspool-insert-nov-head (article)
404 "Read the head of ARTICLE, convert to NOV headers, and insert."
406 (let ((cur (current-buffer))
408 (setq buf (nnheader-set-temp-buffer " *nnspool head*"))
409 (when (nnheader-insert-head
410 (nnspool-article-pathname nnspool-current-group article))
411 (nnheader-insert-article-line article)
412 (let ((headers (nnheader-parse-head)))
414 (goto-char (point-max))
415 (nnheader-insert-nov headers)))
418 (defun nnspool-sift-nov-with-sed (articles file)
419 (let ((first (car articles))
420 (last (progn (while (cdr articles) (setq articles (cdr articles)))
422 (call-process "awk" nil t nil
423 (format "BEGIN {firstmsg=%d; lastmsg=%d;}\n $1 >= firstmsg && $1 <= lastmsg {print;}"
424 (1- first) (1+ last))
427 ;; Fixed by fdc@cliwe.ping.de (Frank D. Cringle).
428 ;; Find out what group an article identified by a Message-ID is in.
429 (defun nnspool-find-id (id)
431 (set-buffer (get-buffer-create " *nnspool work*"))
434 (call-process "grep" nil t nil (regexp-quote id) nnspool-history-file))
435 (goto-char (point-min))
437 (when (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\([^ /\t\n]+\\)/\\([0-9]+\\)[ \t\n]")
438 (cons (match-string 1) (string-to-int (match-string 2))))
439 (kill-buffer (current-buffer)))))
441 (defun nnspool-find-file (file)
442 "Insert FILE in server buffer safely."
443 (set-buffer nntp-server-buffer)
446 (let ((coding-system-for-read nnspool-file-coding-system))
447 (mm-insert-file-contents file)
451 (defun nnspool-possibly-change-directory (group)
454 (let ((pathname (nnspool-article-pathname group)))
455 (if (file-directory-p pathname)
456 (setq nnspool-current-directory pathname
457 nnspool-current-group group)
458 (nnheader-report 'nnspool "No such newsgroup: %s" group)))))
460 (defun nnspool-article-pathname (group &optional article)
461 "Find the path for GROUP."
462 (nnheader-group-pathname group nnspool-spool-directory article))
466 ;;; nnspool.el ends here