1 ;;; nnspool.el --- spool access for GNU Emacs
2 ;; Copyright (C) 1988,89,90,93,94,95 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32 (defvar nnspool-inews-program news-inews-program
33 "Program to post news.
34 This is most commonly `inews' or `injnews'.")
36 (defvar nnspool-inews-switches '("-h")
37 "Switches for nnspool-request-post to pass to `inews' for posting news.
38 If you are using Cnews, you probably should set this variable to nil.")
40 (defvar nnspool-spool-directory (file-name-as-directory news-path)
41 "Local news spool directory.")
43 (defvar nnspool-nov-directory (concat nnspool-spool-directory "over.view/")
44 "Local news nov directory.")
46 (defvar nnspool-lib-dir "/usr/lib/news/"
47 "Where the local news library files are stored.")
49 (defvar nnspool-active-file (concat nnspool-lib-dir "active")
50 "Local news active file.")
52 (defvar nnspool-newsgroups-file (concat nnspool-lib-dir "newsgroups")
53 "Local news newsgroups file.")
55 (defvar nnspool-distributions-file (concat nnspool-lib-dir "distributions")
56 "Local news distributions file.")
58 (defvar nnspool-history-file (concat nnspool-lib-dir "history")
59 "Local news history file.")
61 (defvar nnspool-active-times-file (concat nnspool-lib-dir "active.times")
62 "Local news active date file.")
64 (defvar nnspool-large-newsgroup 50
65 "The number of the articles which indicates a large newsgroup.
66 If the number of the articles is greater than the value, verbose
67 messages will be shown to indicate the current status.")
69 (defvar nnspool-nov-is-evil nil
70 "Non-nil means that nnspool will never return NOV lines instead of headers.")
72 (defconst nnspool-sift-nov-with-sed nil
73 "If non-nil, use sed to get the relevant portion from the overview file.
74 If nil, nnspool will load the entire file into a buffer and process it
77 (defvar nnspool-rejected-article-hook nil
78 "*A hook that will be run when an article has been rejected by the server.")
82 (defconst nnspool-version "nnspool 2.0"
83 "Version numbers of this version of NNSPOOL.")
85 (defvar nnspool-current-directory nil
86 "Current news group directory.")
88 (defvar nnspool-current-group nil)
89 (defvar nnspool-status-string "")
93 (defvar nnspool-current-server nil)
94 (defvar nnspool-server-alist nil)
95 (defvar nnspool-server-variables
97 (list 'nnspool-inews-program nnspool-inews-program)
98 (list 'nnspool-inews-switches nnspool-inews-switches)
99 (list 'nnspool-spool-directory nnspool-spool-directory)
100 (list 'nnspool-nov-directory nnspool-nov-directory)
101 (list 'nnspool-lib-dir nnspool-lib-dir)
102 (list 'nnspool-active-file nnspool-active-file)
103 (list 'nnspool-newsgroups-file nnspool-newsgroups-file)
104 (list 'nnspool-distributions-file nnspool-distributions-file)
105 (list 'nnspool-history-file nnspool-history-file)
106 (list 'nnspool-active-times-file nnspool-active-times-file)
107 (list 'nnspool-large-newsgroup nnspool-large-newsgroup)
108 (list 'nnspool-nov-is-evil nnspool-nov-is-evil)
109 (list 'nnspool-sift-nov-with-sed nnspool-sift-nov-with-sed)
110 '(nnspool-current-directory nil)
111 '(nnspool-current-group nil)
112 '(nnspool-status-string "")))
115 ;;; Interface functions.
117 (defun nnspool-retrieve-headers (articles &optional group server fetch-old)
118 "Retrieve the headers of ARTICLES."
120 (set-buffer nntp-server-buffer)
122 (when (nnspool-possibly-change-directory group)
123 (let* ((number (length articles))
125 (do-message (and (numberp nnspool-large-newsgroup)
126 (> number nnspool-large-newsgroup)))
128 (if (and (numberp (car articles))
129 (nnspool-retrieve-headers-with-nov articles fetch-old))
130 ;; We successfully retrieved the NOV headers.
132 ;; No NOV headers here, so we do it the hard way.
134 (setq article (pop articles))
135 (if (stringp article)
136 ;; This is a Message-ID.
137 (setq ag (nnspool-find-id article)
138 file (and ag (nnspool-article-pathname
141 ;; This is an article in the current group.
142 (setq file (nnspool-article-pathname
143 nnspool-current-group article)))
144 ;; Insert the head of the article.
146 (file-exists-p file))
147 (insert (format "221 %d Article retrieved.\n" article))
149 (nnheader-insert-head file)
151 (search-forward "\n\n" nil t)
154 (delete-region (point) (point-max)))
157 (zerop (% (incf count) 20))
158 (message "nnspool: Receiving headers... %d%%"
159 (/ (* count 100) number))))
162 (message "nnspool: Receiving headers...done"))
164 ;; Fold continuation lines.
165 (goto-char (point-min))
166 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
167 (replace-match " " t t))
170 (defun nnspool-open-server (server &optional defs)
171 (nnheader-init-server-buffer)
172 (if (equal server nnspool-current-server)
174 (if nnspool-current-server
175 (setq nnspool-server-alist
176 (cons (list nnspool-current-server
177 (nnheader-save-variables nnspool-server-variables))
178 nnspool-server-alist)))
179 (let ((state (assoc server nnspool-server-alist)))
182 (nnheader-restore-variables (nth 1 state))
183 (setq nnspool-server-alist (delq state nnspool-server-alist)))
184 (nnheader-set-init-variables nnspool-server-variables defs)))
185 (setq nnspool-current-server server)))
187 (defun nnspool-close-server (&optional server)
188 (setq nnspool-current-server nil)
191 (defun nnspool-server-opened (&optional server)
192 (and (equal server nnspool-current-server)
194 (buffer-name nntp-server-buffer)))
196 (defun nnspool-status-message (&optional server)
197 "Return server status response as string."
198 nnspool-status-string)
200 (defun nnspool-request-article (id &optional group server buffer)
201 "Select article by message ID (or number)."
202 (nnspool-possibly-change-directory group)
203 (let ((nntp-server-buffer (or buffer nntp-server-buffer))
206 ;; This is a Message-ID.
207 (when (setq ag (nnspool-find-id id))
208 (setq file (nnspool-article-pathname (car ag) (cdr ag))))
209 (setq file (nnspool-article-pathname nnspool-current-group id)))
212 (not (file-directory-p file))
213 (save-excursion (nnspool-find-file file))
214 ;; We return the article number and group name.
216 (cons nnspool-current-group id)
219 (defun nnspool-request-body (id &optional group server)
220 "Select article body by message ID (or number)."
221 (nnspool-possibly-change-directory group)
222 (if (nnspool-request-article id)
224 (set-buffer nntp-server-buffer)
225 (goto-char (point-min))
226 (if (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 (if (nnspool-request-article id)
235 (set-buffer nntp-server-buffer)
236 (goto-char (point-min))
237 (if (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 (setq nnspool-status-string
248 "Invalid group name (no such directory)")
250 (setq nnspool-current-directory pathname)
251 (setq nnspool-status-string "")
254 (setq dir (directory-files pathname nil "^[0-9]+$" t))
255 ;; yes, completely empty spool directories *are* possible
256 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
263 (string-to-int name)))
267 (set-buffer nntp-server-buffer)
271 (format "211 %d %d %d %s\n" (length dir) (car dir)
272 (progn (while (cdr dir) (setq dir (cdr dir)))
275 (insert (format "211 0 0 0 %s\n" group))))))
278 (defun nnspool-close-group (group &optional server)
281 (defun nnspool-request-list (&optional server)
282 "List active newsgroups."
284 (nnspool-find-file nnspool-active-file)))
286 (defun nnspool-request-list-newsgroups (&optional server)
287 "List newsgroups (defined in NNTP2)."
289 (nnspool-find-file nnspool-newsgroups-file)))
291 (defun nnspool-request-list-distributions (&optional server)
292 "List distributions (defined in NNTP2)."
294 (nnspool-find-file nnspool-distributions-file)))
296 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
297 (defun nnspool-request-newgroups (date &optional server)
298 "List groups created after DATE."
299 (if (nnspool-find-file nnspool-active-times-file)
301 ;; Find the last valid line.
302 (goto-char (point-max))
303 (while (and (not (looking-at
304 "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
305 (zerop (forward-line -1))))
306 (let ((seconds (nnspool-seconds-since-epoch date))
308 ;; Go through lines and add the latest groups to a list.
309 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
311 ;; We insert a .0 to make the list reader
312 ;; interpret the number as a float. It is far
313 ;; too big to be stored in a lisp integer.
314 (goto-char (1- (match-end 0)))
317 (goto-char (match-end 1))
318 (read (current-buffer)))
320 (setq groups (cons (buffer-substring
321 (match-beginning 1) (match-end 1))
323 (zerop (forward-line -1))))
326 (insert (car groups) " 0 0 y\n")
327 (setq groups (cdr groups))))
331 (defun nnspool-request-post (&optional server)
332 "Post a new news in current buffer."
334 (let* ((process-connection-type nil) ; t bugs out on Solaris
335 (inews-buffer (generate-new-buffer " *nnspool post*"))
338 (apply 'start-process "*nnspool inews*" inews-buffer
339 nnspool-inews-program nnspool-inews-switches)
341 (setq nnspool-status-string (format "inews error: %S" err))
344 ;; The inews program failed.
346 (setq nnspool-status-string "")
347 (set-process-sentinel proc 'nnspool-inews-sentinel)
348 (process-send-region proc (point-min) (point-max))
349 ;; We slap a condition-case around this, because the process may
350 ;; have exited already...
352 (process-send-eof proc)
356 (defun nnspool-inews-sentinel (proc status)
358 (set-buffer (process-buffer proc))
359 (goto-char (point-min))
360 (if (or (zerop (buffer-size))
361 (search-forward "spooled" nil t))
362 (kill-buffer (current-buffer))
363 ;; Make status message by folding lines.
364 (while (re-search-forward "[ \t\n]+" nil t)
365 (replace-match " " t t))
366 (setq nnspool-status-string (buffer-string))
367 (message "nnspool: %s" nnspool-status-string)
369 (run-hooks 'nnspool-rejected-article-hook))))
372 ;;; Internal functions.
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 (concat (file-name-as-directory nnspool-nov-directory)
378 (nnspool-replace-chars-in-string
379 nnspool-current-group ?. ?/)
381 (if (not (file-exists-p nov))
384 (set-buffer nntp-server-buffer)
386 (if nnspool-sift-nov-with-sed
387 (nnspool-sift-nov-with-sed articles nov)
388 (insert-file-contents nov)
390 (not (numberp fetch-old)))
391 t ; We want all the headers.
392 ;; First we find the first wanted line.
393 (nnspool-find-nov-line
394 (if fetch-old (max 1 (- (car articles) fetch-old))
396 (delete-region (point-min) (point))
397 ;; Then we find the last wanted line.
398 (if (nnspool-find-nov-line
399 (progn (while (cdr articles) (setq articles (cdr articles)))
402 (delete-region (point) (point-max))
403 ;; If the buffer is empty, this wasn't very successful.
404 (not (zerop (buffer-size))))))))))
406 (defun nnspool-find-nov-line (article)
407 (let ((max (point-max))
408 (min (goto-char (point-min)))
409 (cur (current-buffer))
413 (goto-char (/ (+ max min) 2))
415 (if (or (= (point) prev)
419 (cond ((> (setq num (read cur)) article)
425 (when (not (eq num article))
429 (= (setq num (read cur)) article)
435 (defun nnspool-sift-nov-with-sed (articles file)
436 (let ((first (car articles))
437 (last (progn (while (cdr articles) (setq articles (cdr articles)))
439 (call-process "awk" nil t nil
440 (format "BEGIN {firstmsg=%d; lastmsg=%d;}\n $1 >= firstmsg && $1 <= lastmsg {print;}"
441 (1- first) (1+ last))
444 ;; Fixed by fdc@cliwe.ping.de (Frank D. Cringle).
445 ;; Find out what group an article identified by a Message-ID is in.
446 (defun nnspool-find-id (id)
448 (set-buffer (get-buffer-create " *nnspool work*"))
449 (buffer-disable-undo (current-buffer))
451 (call-process "grep" nil t nil id nnspool-history-file)
452 (goto-char (point-min))
454 (if (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\([^ /\t\n]+\\)/\\([0-9]+\\)[ \t\n]")
455 (cons (match-string 1) (string-to-int (match-string 2))))
456 (kill-buffer (current-buffer)))))
458 (defun nnspool-find-file (file)
459 "Insert FILE in server buffer safely."
460 (set-buffer nntp-server-buffer)
463 (progn (insert-file-contents file) t)
466 (defun nnspool-possibly-change-directory (group)
468 (let ((pathname (nnspool-article-pathname group)))
469 (if (file-directory-p pathname)
471 (setq nnspool-current-directory pathname)
472 (setq nnspool-current-group group))
473 (setq nnspool-status-string
474 (format "No such newsgroup: %s" group))
478 (defun nnspool-article-pathname (group &optional article)
479 "Find the path for GROUP."
481 (file-name-as-directory nnspool-spool-directory)
482 (nnspool-replace-chars-in-string group ?. ?/)
484 (if article (int-to-string article) "")))
486 (defun nnspool-replace-chars-in-string (string from to)
487 "Replace characters in STRING from FROM to TO."
488 (let ((string (substring string 0)) ;Copy string.
489 (len (length string))
491 ;; Replace all occurrences of FROM with TO.
493 (if (= (aref string idx) from)
494 (aset string idx to))
498 (defun nnspool-number-base-10 (num pos)
500 (setcdr num (+ (* (% (car num) 10) 65536) (cdr num)))
506 (aref "0123456789" (% (cdr num) 10)))
508 (setcdr num (/ (cdr num) 10))
509 (setcar num (/ (car num) 10))
510 (nnspool-number-base-10 num (1- pos))))))))
512 (defun nnspool-seconds-since-epoch (date)
513 (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
514 (timezone-parse-date date)))
515 (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
517 (aref (timezone-parse-date date) 3))))
518 (unix (encode-time (nth 2 ttime) (nth 1 ttime) (nth 0 ttime)
519 (nth 2 tdate) (nth 1 tdate) (nth 0 tdate)
521 (+ (* (car unix) 65536.0)
526 ;;; nnspool.el ends here