1 ;;; nnspool.el --- spool access for GNU Emacs
3 ;; Copyright (C) 1988-1990, 1993-1998, 2000-2012
4 ;; 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 3 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
32 (eval-when-compile (require 'cl))
34 ;; Probably this entire thing should be obsolete.
35 ;; It's only used to init nnspool-spool-directory, so why not just
36 ;; set that variable's default directly?
37 (defvar news-directory (if (file-exists-p "/usr/spool/news/")
40 "The root directory below which all news files are stored.")
41 (defvaralias 'news-path 'news-directory)
43 ;; Ditto re obsolescence.
44 (defvar news-inews-program
45 (cond ((file-exists-p "/usr/bin/inews") "/usr/bin/inews")
46 ((file-exists-p "/usr/local/inews") "/usr/local/inews")
47 ((file-exists-p "/usr/local/bin/inews") "/usr/local/bin/inews")
48 ((file-exists-p "/usr/contrib/lib/news/inews") "/usr/contrib/lib/news/inews")
49 ((file-exists-p "/usr/lib/news/inews") "/usr/lib/news/inews")
51 "Program to post news.")
53 (nnoo-declare nnspool)
55 (defvoo nnspool-inews-program news-inews-program
56 "Program to post news.
57 This is most commonly `inews' or `injnews'.")
59 (defvoo nnspool-inews-switches '("-h" "-S")
60 "Switches for nnspool-request-post to pass to `inews' for posting news.
61 If you are using Cnews, you probably should set this variable to nil.")
63 (defvoo nnspool-spool-directory
64 (file-name-as-directory (if (boundp 'news-directory)
65 (symbol-value 'news-directory)
67 "Local news spool directory.")
69 (defvoo nnspool-nov-directory (concat nnspool-spool-directory "over.view/")
70 "Local news nov directory.")
72 (defvoo nnspool-lib-dir
73 (if (file-exists-p "/usr/lib/news/active")
76 "Where the local news library files are stored.")
78 (defvoo nnspool-active-file (concat nnspool-lib-dir "active")
79 "Local news active file.")
81 (defvoo nnspool-newsgroups-file (concat nnspool-lib-dir "newsgroups")
82 "Local news newsgroups file.")
84 (defvoo nnspool-distributions-file (concat nnspool-lib-dir "distribs.pat")
85 "Local news distributions file.")
87 (defvoo nnspool-history-file (concat nnspool-lib-dir "history")
88 "Local news history file.")
90 (defvoo nnspool-active-times-file (concat nnspool-lib-dir "active.times")
91 "Local news active date file.")
93 (defvoo nnspool-large-newsgroup 50
94 "The number of articles which indicates a large newsgroup.
95 If the number of articles is greater than the value, verbose
96 messages will be shown to indicate the current status.")
98 (defvoo nnspool-nov-is-evil nil
99 "Non-nil means that nnspool will never return NOV lines instead of headers.")
101 (defconst nnspool-sift-nov-with-sed nil
102 "If non-nil, use sed to get the relevant portion from the overview file.
103 If nil, nnspool will load the entire file into a buffer and process it
106 (defvoo nnspool-rejected-article-hook nil
107 "*A hook that will be run when an article has been rejected by the server.")
109 (defvoo nnspool-file-coding-system nnheader-file-coding-system
110 "Coding system for nnspool.")
114 (defconst nnspool-version "nnspool 2.0"
115 "Version numbers of this version of NNSPOOL.")
117 (defvoo nnspool-current-directory nil
118 "Current news group directory.")
120 (defvoo nnspool-current-group nil)
121 (defvoo nnspool-status-string "")
124 ;;; Interface functions.
126 (nnoo-define-basics nnspool)
128 (deffoo nnspool-retrieve-headers (articles &optional group server fetch-old)
129 "Retrieve the headers of ARTICLES."
130 (with-current-buffer nntp-server-buffer
132 (when (nnspool-possibly-change-directory group)
133 (let* ((number (length articles))
135 (default-directory nnspool-current-directory)
136 (do-message (and (numberp nnspool-large-newsgroup)
137 (> number nnspool-large-newsgroup)))
138 (nnheader-file-coding-system nnspool-file-coding-system)
140 (if (and (numberp (car articles))
141 (nnspool-retrieve-headers-with-nov articles fetch-old))
142 ;; We successfully retrieved the NOV headers.
144 ;; No NOV headers here, so we do it the hard way.
145 (while (setq article (pop articles))
146 (if (stringp article)
147 ;; This is a Message-ID.
148 (setq ag (nnspool-find-id article)
149 file (and ag (nnspool-article-pathname
152 ;; This is an article in the current group.
153 (setq file (int-to-string article)))
154 ;; Insert the head of the article.
156 (file-exists-p file))
158 (princ article (current-buffer))
159 (insert " Article retrieved.\n")
161 (inline (nnheader-insert-head file))
163 (if (search-forward "\n\n" nil t)
167 (goto-char (point-max))
171 (delete-region (point) (point-max)))
174 (zerop (% (incf count) 20))
175 (nnheader-message 5 "nnspool: Receiving headers... %d%%"
176 (/ (* count 100) number))))
179 (nnheader-message 5 "nnspool: Receiving headers...done"))
181 ;; Fold continuation lines.
182 (nnheader-fold-continuation-lines)
185 (deffoo nnspool-open-server (server &optional defs)
186 (nnoo-change-server 'nnspool server defs)
188 ((not (file-exists-p nnspool-spool-directory))
189 (nnspool-close-server)
190 (nnheader-report 'nnspool "Spool directory doesn't exist: %s"
191 nnspool-spool-directory))
192 ((not (file-directory-p
194 (file-truename nnspool-spool-directory))))
195 (nnspool-close-server)
196 (nnheader-report 'nnspool "Not a directory: %s" nnspool-spool-directory))
197 ((not (file-exists-p nnspool-active-file))
198 (nnheader-report 'nnspool "The active file doesn't exist: %s"
199 nnspool-active-file))
201 (nnheader-report 'nnspool "Opened server %s using directory %s"
202 server nnspool-spool-directory)
205 (deffoo nnspool-request-article (id &optional group server buffer)
206 "Select article by message ID (or number)."
207 (nnspool-possibly-change-directory group)
208 (let ((nntp-server-buffer (or buffer nntp-server-buffer))
211 ;; This is a Message-ID.
212 (when (setq ag (nnspool-find-id id))
213 (setq file (nnspool-article-pathname (car ag) (cdr ag))))
214 (setq file (nnspool-article-pathname nnspool-current-group id)))
217 (not (file-directory-p file))
218 (save-excursion (nnspool-find-file file))
219 ;; We return the article number and group name.
221 (cons nnspool-current-group id)
224 (deffoo nnspool-request-body (id &optional group server)
225 "Select article body by message ID (or number)."
226 (nnspool-possibly-change-directory group)
227 (let ((res (nnspool-request-article id)))
229 (with-current-buffer nntp-server-buffer
230 (goto-char (point-min))
231 (when (search-forward "\n\n" nil t)
232 (delete-region (point-min) (point)))
235 (deffoo nnspool-request-head (id &optional group server)
236 "Select article head by message ID (or number)."
237 (nnspool-possibly-change-directory group)
238 (let ((res (nnspool-request-article id)))
240 (with-current-buffer nntp-server-buffer
241 (goto-char (point-min))
242 (when (search-forward "\n\n" nil t)
243 (delete-region (1- (point)) (point-max)))
244 (nnheader-fold-continuation-lines)))
247 (deffoo nnspool-request-group (group &optional server dont-check info)
249 (let ((pathname (nnspool-article-pathname group))
251 (if (not (file-directory-p pathname))
253 'nnspool "Invalid group name (no such directory): %s" group)
254 (setq nnspool-current-directory pathname)
255 (nnheader-report 'nnspool "Selected group %s" group)
258 (nnheader-report 'nnspool "Selected group %s" group)
260 ;; Yes, completely empty spool directories *are* possible.
261 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
262 (when (setq dir (directory-files pathname nil "^[0-9]+$" t))
263 (setq dir (sort (mapcar 'string-to-number dir) '<)))
266 "211 %d %d %d %s\n" (length dir) (car dir)
267 (car (last dir)) group)
268 (nnheader-report 'nnspool "Empty group %s" group)
269 (nnheader-insert "211 0 0 0 %s\n" group))))))
271 (deffoo nnspool-request-type (group &optional article)
274 (deffoo nnspool-close-group (group &optional server)
277 (deffoo nnspool-request-list (&optional server)
278 "List active newsgroups."
280 (or (nnspool-find-file nnspool-active-file)
281 (nnheader-report 'nnspool (nnheader-file-error nnspool-active-file)))))
283 (deffoo nnspool-request-list-newsgroups (&optional server)
284 "List newsgroups (defined in NNTP2)."
286 (or (nnspool-find-file nnspool-newsgroups-file)
287 (nnheader-report 'nnspool (nnheader-file-error
288 nnspool-newsgroups-file)))))
290 (deffoo nnspool-request-list-distributions (&optional server)
291 "List distributions (defined in NNTP2)."
293 (or (nnspool-find-file nnspool-distributions-file)
294 (nnheader-report 'nnspool (nnheader-file-error
295 nnspool-distributions-file)))))
297 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
298 (deffoo nnspool-request-newgroups (date &optional server)
299 "List groups created after DATE."
300 (if (nnspool-find-file nnspool-active-times-file)
302 ;; Find the last valid line.
303 (goto-char (point-max))
304 (while (and (not (looking-at
305 "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
306 (zerop (forward-line -1))))
307 ;; We require nnheader which requires gnus-util.
308 (let ((seconds (gnus-float-time (date-to-time date)))
310 ;; Go through lines and add the latest groups to a list.
311 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
313 ;; We insert a .0 to make the list reader
314 ;; interpret the number as a float. It is far
315 ;; too big to be stored in a lisp integer.
316 (goto-char (1- (match-end 0)))
319 (goto-char (match-end 1))
320 (read (current-buffer)))
322 (push (buffer-substring
323 (match-beginning 1) (match-end 1))
325 (zerop (forward-line -1))))
327 (dolist (group groups)
328 (insert group " 0 0 y\n")))
332 (deffoo nnspool-request-post (&optional server)
333 "Post a new news in current buffer."
335 (let* ((process-connection-type nil) ; t bugs out on Solaris
336 (inews-buffer (generate-new-buffer " *nnspool post*"))
339 (apply 'start-process "*nnspool inews*" inews-buffer
340 nnspool-inews-program nnspool-inews-switches)
342 (nnheader-report 'nnspool "inews error: %S" err)))))
344 ;; The inews program failed.
346 (nnheader-report 'nnspool "")
347 (set-process-sentinel proc 'nnspool-inews-sentinel)
348 (mm-with-unibyte-current-buffer
349 (process-send-region proc (point-min) (point-max)))
350 ;; We slap a condition-case around this, because the process may
351 ;; have exited already...
353 (process-send-eof proc))
358 ;;; Internal functions.
360 (defun nnspool-inews-sentinel (proc status)
361 (with-current-buffer (process-buffer proc)
362 (goto-char (point-min))
363 (if (or (zerop (buffer-size))
364 (search-forward "spooled" nil t))
365 (kill-buffer (current-buffer))
366 ;; Make status message by folding lines.
367 (while (re-search-forward "[ \t\n]+" nil t)
368 (replace-match " " t t))
369 (nnheader-report 'nnspool "%s" (buffer-string))
370 (nnheader-message 5 "nnspool: %s" nnspool-status-string)
372 (run-hooks 'nnspool-rejected-article-hook))))
374 (defun nnspool-retrieve-headers-with-nov (articles &optional fetch-old)
375 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
377 (let ((nov (nnheader-group-pathname
378 nnspool-current-group nnspool-nov-directory ".overview"))
380 (nnheader-file-coding-system nnspool-file-coding-system)
382 (if (not (file-exists-p nov))
384 (with-current-buffer nntp-server-buffer
386 (if nnspool-sift-nov-with-sed
387 (nnspool-sift-nov-with-sed articles nov)
388 (nnheader-insert-file-contents nov)
390 (not (numberp fetch-old)))
391 t ; We want all the headers.
393 ;; Delete unwanted NOV lines.
394 (nnheader-nov-delete-outside-range
395 (if fetch-old (max 1 (- (car articles) fetch-old))
397 (car (last articles)))
398 ;; If the buffer is empty, this wasn't very successful.
399 (unless (zerop (buffer-size))
400 ;; We check what the last article number was.
401 ;; The NOV file may be out of sync with the articles
404 (setq last (read (current-buffer)))
405 (if (= last (car articles))
406 ;; Yup, it's all there.
408 ;; Perhaps not. We try to find the missing articles.
410 (<= last (car arts)))
412 ;; The articles in `arts' are missing from the buffer.
413 (mapc 'nnspool-insert-nov-head arts)
416 (defun nnspool-insert-nov-head (article)
417 "Read the head of ARTICLE, convert to NOV headers, and insert."
419 (let ((cur (current-buffer))
421 (setq buf (nnheader-set-temp-buffer " *nnspool head*"))
422 (when (nnheader-insert-head
423 (nnspool-article-pathname nnspool-current-group article))
424 (nnheader-insert-article-line article)
425 (goto-char (point-min))
426 (let ((headers (nnheader-parse-head)))
428 (goto-char (point-max))
429 (nnheader-insert-nov headers)))
432 (defun nnspool-sift-nov-with-sed (articles file)
433 (let ((first (car articles))
434 (last (car (last articles))))
435 (call-process "awk" nil t nil
436 (format "BEGIN {firstmsg=%d; lastmsg=%d;}\n $1 >= firstmsg && $1 <= lastmsg {print;}"
437 (1- first) (1+ last))
440 ;; Fixed by fdc@cliwe.ping.de (Frank D. Cringle).
441 ;; Find out what group an article identified by a Message-ID is in.
442 (defun nnspool-find-id (id)
445 (call-process "grep" nil t nil (regexp-quote id) nnspool-history-file))
446 (goto-char (point-min))
447 (when (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\([^ /\t\n]+\\)/\\([0-9]+\\)[ \t\n]")
448 (cons (match-string 1) (string-to-number (match-string 2))))))
450 (defun nnspool-find-file (file)
451 "Insert FILE in server buffer safely."
452 (set-buffer nntp-server-buffer)
455 (let ((coding-system-for-read nnspool-file-coding-system))
456 (mm-insert-file-contents file)
460 (defun nnspool-possibly-change-directory (group)
463 (let ((pathname (nnspool-article-pathname group)))
464 (if (file-directory-p pathname)
465 (setq nnspool-current-directory pathname
466 nnspool-current-group group)
467 (nnheader-report 'nnspool "No such newsgroup: %s" group)))))
469 (defun nnspool-article-pathname (group &optional article)
470 "Find the file name for GROUP."
471 (nnheader-group-pathname group nnspool-spool-directory article))
475 ;;; nnspool.el ends here