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.")
35 (defvar nnspool-inews-switches '("-S" "-h")
36 "Switches for nnspool-request-post to pass to `inews' for posting news.")
38 (defvar nnspool-spool-directory news-path
39 "Local news spool directory.")
41 (defvar nnspool-nov-directory (concat nnspool-spool-directory "over.view/")
42 "Local news nov directory.")
44 (defvar nnspool-lib-dir "/usr/lib/news/"
45 "Where the local news library files are stored.")
47 (defvar nnspool-active-file (concat nnspool-lib-dir "active")
48 "Local news active file.")
50 (defvar nnspool-newsgroups-file (concat nnspool-lib-dir "newsgroups")
51 "Local news newsgroups file.")
53 (defvar nnspool-distributions-file (concat nnspool-lib-dir "distributions")
54 "Local news distributions file.")
56 (defvar nnspool-history-file (concat nnspool-lib-dir "history")
57 "Local news history file.")
59 (defvar nnspool-active-times-file (concat nnspool-lib-dir "active.times")
60 "Local news active date file.")
62 (defvar nnspool-large-newsgroup 50
63 "The number of the articles which indicates a large newsgroup.
64 If the number of the articles is greater than the value, verbose
65 messages will be shown to indicate the current status.")
67 (defvar nnspool-nov-is-evil nil
68 "Non-nil means that nnspool will never return NOV lines instead of headers.")
70 (defvar nnspool-sift-nov-with-sed t
71 "If non-nil, use sed to get the relevant portion from the overview file.
72 If nil, nnspool will load the entire file into a buffer and process it
77 (defconst nnspool-version "nnspool 2.0"
78 "Version numbers of this version of NNSPOOL.")
80 (defvar nnspool-current-directory nil
81 "Current news group directory.")
83 (defvar nnspool-current-group nil)
84 (defvar nnspool-status-string "")
88 (defvar nnspool-current-server nil)
89 (defvar nnspool-server-alist nil)
90 (defvar nnspool-server-variables
92 (list 'nnspool-inews-program nnspool-inews-program)
93 (list 'nnspool-inews-switches nnspool-inews-switches)
94 (list 'nnspool-spool-directory nnspool-spool-directory)
95 (list 'nnspool-nov-directory nnspool-nov-directory)
96 (list 'nnspool-lib-dir nnspool-lib-dir)
97 (list 'nnspool-active-file nnspool-active-file)
98 (list 'nnspool-newsgroups-file nnspool-newsgroups-file)
99 (list 'nnspool-distributions-file nnspool-distributions-file)
100 (list 'nnspool-history-file nnspool-history-file)
101 (list 'nnspool-active-times-file nnspool-active-times-file)
102 (list 'nnspool-large-newsgroup nnspool-large-newsgroup)
103 (list 'nnspool-nov-is-evil nnspool-nov-is-evil)
104 (list 'nnspool-sift-nov-with-sed nnspool-sift-nov-with-sed)
105 '(nnspool-current-directory nil)
106 '(nnspool-current-group nil)
107 '(nnspool-status-string "")))
110 ;;; Interface functions.
112 (defun nnspool-retrieve-headers (sequence &optional newsgroup server)
113 "Retrieve the headers for the articles in SEQUENCE.
114 Newsgroup must be selected before calling this function."
116 (set-buffer nntp-server-buffer)
118 (let* ((number (length sequence))
120 (do-message (and (numberp nnspool-large-newsgroup)
121 (> number nnspool-large-newsgroup)))
123 (if (not (nnspool-possibly-change-directory newsgroup))
125 (if (nnspool-retrieve-headers-with-nov sequence)
128 (setq article (car sequence))
129 (if (stringp article)
131 (format "221 %d Article retrieved.\n" 0)
132 (nnspool-request-article article))
133 (setq file (concat nnspool-current-directory
134 (int-to-string article)))
135 (and (file-exists-p file)
137 (insert (format "221 %d Article retrieved.\n" article))
139 (insert-file-contents file)
141 (search-forward "\n\n" nil t)
144 (delete-region (point) (point-max)))))
145 (setq sequence (cdr sequence))
148 (zerop (% (setq count (1+ count)) 20))
149 (message "NNSPOOL: Receiving headers... %d%%"
150 (/ (* count 100) number))))
152 (and do-message (message "NNSPOOL: Receiving headers... done"))
154 ;; Fold continuation lines.
155 (goto-char (point-min))
156 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
157 (replace-match " " t t))
160 (defun nnspool-open-server (server &optional defs)
161 (nnheader-init-server-buffer)
162 (if (equal server nnspool-current-server)
164 (if nnspool-current-server
165 (setq nnspool-server-alist
166 (cons (list nnspool-current-server
167 (nnheader-save-variables nnspool-server-variables))
168 nnspool-server-alist)))
169 (let ((state (assoc server nnspool-server-alist)))
172 (nnheader-restore-variables (nth 1 state))
173 (setq nnspool-server-alist (delq state nnspool-server-alist)))
174 (nnheader-set-init-variables nnspool-server-variables defs)))
175 (setq nnspool-current-server server)))
177 (defun nnspool-close-server (&optional server)
180 (defun nnspool-server-opened (&optional server)
181 (and (equal server nnspool-current-server)
183 (buffer-name nntp-server-buffer)))
185 (defun nnspool-status-message (&optional server)
186 "Return server status response as string."
187 nnspool-status-string)
189 (defun nnspool-request-article (id &optional newsgroup server buffer)
190 "Select article by message ID (or number)."
191 (nnspool-possibly-change-directory newsgroup)
192 (let ((file (if (stringp id)
193 (nnspool-find-article-by-message-id id)
194 (concat nnspool-current-directory (prin1-to-string id))))
195 (nntp-server-buffer (or buffer nntp-server-buffer)))
196 (if (and (stringp file)
198 (not (file-directory-p file)))
200 (nnspool-find-file file)))))
202 (defun nnspool-request-body (id &optional newsgroup server)
203 "Select article body by message ID (or number)."
204 (nnspool-possibly-change-directory newsgroup)
205 (if (nnspool-request-article id)
207 (set-buffer nntp-server-buffer)
208 (goto-char (point-min))
209 (if (search-forward "\n\n" nil t)
210 (delete-region (point-min) (point)))
213 (defun nnspool-request-head (id &optional newsgroup server)
214 "Select article head by message ID (or number)."
215 (nnspool-possibly-change-directory newsgroup)
216 (if (nnspool-request-article id)
218 (set-buffer nntp-server-buffer)
219 (goto-char (point-min))
220 (if (search-forward "\n\n" nil t)
221 (delete-region (1- (point)) (point-max)))
224 (defun nnspool-request-group (group &optional server dont-check)
226 (let ((pathname (nnspool-article-pathname
227 (nnspool-replace-chars-in-string group ?. ?/)))
229 (if (not (file-directory-p pathname))
231 (setq nnspool-status-string
232 "Invalid group name (no such directory)")
234 (setq nnspool-current-directory pathname)
235 (setq nnspool-status-string "")
238 (setq dir (directory-files pathname nil "^[0-9]+$" t))
239 ;; yes, completely empty spool directories *are* possible
240 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
247 (string-to-int name)))
251 (set-buffer nntp-server-buffer)
255 (format "211 %d %d %d %s\n" (length dir) (car dir)
256 (progn (while (cdr dir) (setq dir (cdr dir)))
259 (insert (format "211 0 0 0 %s\n" group))))))
262 (defun nnspool-close-group (group &optional server)
265 (defun nnspool-request-list (&optional server)
266 "List active newsgoups."
268 (nnspool-find-file nnspool-active-file)))
270 (defun nnspool-request-list-newsgroups (&optional server)
271 "List newsgroups (defined in NNTP2)."
273 (nnspool-find-file nnspool-newsgroups-file)))
275 (defun nnspool-request-list-distributions (&optional server)
276 "List distributions (defined in NNTP2)."
278 (nnspool-find-file nnspool-distributions-file)))
280 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
281 (defun nnspool-request-newgroups (date &optional server)
282 "List groups created after DATE."
283 (if (nnspool-find-file nnspool-active-times-file)
285 ;; Find the last valid line.
286 (goto-char (point-max))
287 (while (and (not (looking-at
288 "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
289 (zerop (forward-line -1))))
290 (let ((seconds (nnspool-seconds-since-epoch date))
292 ;; Go through lines and add the latest groups to a list.
293 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
295 ;; We insert a .0 to make the list reader
296 ;; interpret the number as a float. It is far
297 ;; too big to be stored in a lisp integer.
298 (goto-char (1- (match-end 0)))
301 (goto-char (match-end 1))
302 (read (current-buffer)))
304 (setq groups (cons (buffer-substring
305 (match-beginning 1) (match-end 1))
307 (zerop (forward-line -1))))
310 (insert (car groups) " 0 0 y\n")
311 (setq groups (cdr groups))))
315 (defun nnspool-request-post (&optional server)
316 "Post a new news in current buffer."
318 (let* ((inews-buffer (generate-new-buffer " *nnspool post*"))
319 (proc (apply 'start-process "*nnspool inews*" inews-buffer
320 nnspool-inews-program nnspool-inews-switches)))
321 (set-process-sentinel proc 'nnspool-inews-sentinel)
322 (process-send-region proc (point-min) (point-max))
323 (process-send-eof proc)
326 (defun nnspool-inews-sentinel (proc status)
328 (set-buffer (process-buffer proc))
329 (goto-char (point-min))
330 (if (or (zerop (buffer-size))
331 (search-forward "spooled" nil t))
333 ;; Make status message by unfolding lines.
334 (subst-char-in-region (point-min) (point-max) ?\n ?\\ 'noundo)
335 (setq nnspool-status-string (buffer-string))
336 (message "nnspool: %s" nnspool-status-string)
337 (kill-buffer (current-buffer)))))
339 (fset 'nnspool-request-post-buffer 'nntp-request-post-buffer)
342 ;;; Internal functions.
344 (defun nnspool-retrieve-headers-with-nov (articles)
345 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
347 (let ((nov (concat nnspool-nov-directory
348 (nnspool-replace-chars-in-string
349 nnspool-current-group ?. ?/)
352 (if (file-exists-p nov)
354 (set-buffer nntp-server-buffer)
356 (if nnspool-sift-nov-with-sed
357 (nnspool-sift-nov-with-sed articles nov)
358 (insert-file-contents nov)
359 ;; First we find the first wanted line. We issue a number
360 ;; of search-forwards - the first article we are lookign
361 ;; for may be expired, so we have to go on searching until
362 ;; we find one of the articles we want.
364 (setq article (concat (int-to-string
365 (car articles)) "\t"))
366 (not (or (looking-at article)
367 (search-forward (concat "\n" article)
369 (setq articles (cdr articles)))
373 (delete-region (point-min) (point))
374 ;; Then we find the last wanted line. We go to the end
375 ;; of the buffer and search backward much the same way
376 ;; we did to find the first article.
377 ;; !!! Perhaps it would be better just to do a (last articles),
378 ;; and go forward successively over each line and
379 ;; compare to avoid this (reverse), like this:
380 ;; (while (and (>= last (read nntp-server-buffer)))
381 ;; (zerop (forward-line 1))))
382 (setq articles (reverse articles))
383 (goto-char (point-max))
385 (not (search-backward
386 (concat "\n" (int-to-string (car articles))
388 (setq articles (cdr articles)))
392 (delete-region (point) (point-max)))))
393 (or articles (progn (erase-buffer) nil))))))))
395 (defun nnspool-sift-nov-with-sed (articles file)
396 (let ((first (car articles))
397 (last (progn (while (cdr articles) (setq articles (cdr articles)))
399 (call-process "sed" nil t nil "-e"
400 (format "1,/^%d\t/d\n/^%d\t/,$d"
401 (1- first) (1+ last)) file)))
403 (defun nnspool-find-article-by-message-id (id)
404 "Return full pathname of an article identified by message-ID."
406 (set-buffer nntp-server-buffer)
408 (call-process "grep" nil t nil id nnspool-history-file)
409 (goto-char (point-min))
410 (if (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\(.*\\)$")
411 (concat nnspool-spool-directory
412 (nnspool-replace-chars-in-string
413 (buffer-substring (match-beginning 1) (match-end 1))
416 (defun nnspool-find-file (file)
417 "Insert FILE in server buffer safely."
418 (set-buffer nntp-server-buffer)
421 (progn (insert-file-contents file) t)
424 (defun nnspool-possibly-change-directory (newsgroup)
426 (let ((pathname (nnspool-article-pathname
427 (nnspool-replace-chars-in-string newsgroup ?. ?/))))
428 (if (file-directory-p pathname)
430 (setq nnspool-current-directory pathname)
431 (setq nnspool-current-group newsgroup))
432 (setq nnspool-status-string
433 (format "No such newsgroup: %s" newsgroup))
437 (defun nnspool-article-pathname (group)
438 "Make pathname for GROUP."
439 (concat (file-name-as-directory nnspool-spool-directory) group "/"))
441 (defun nnspool-replace-chars-in-string (string from to)
442 "Replace characters in STRING from FROM to TO."
443 (let ((string (substring string 0)) ;Copy string.
444 (len (length string))
446 ;; Replace all occurrences of FROM with TO.
448 (if (= (aref string idx) from)
449 (aset string idx to))
453 (defun nnspool-number-base-10 (num pos)
455 (setcdr num (+ (* (% (car num) 10) 65536) (cdr num)))
461 (aref "0123456789" (% (cdr num) 10)))
463 (setcdr num (/ (cdr num) 10))
464 (setcar num (/ (car num) 10))
465 (nnspool-number-base-10 num (1- pos))))))))
467 (defun nnspool-seconds-since-epoch (date)
468 (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
469 (timezone-parse-date date)))
470 (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
472 (aref (timezone-parse-date date) 3))))
473 (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
474 (timezone-parse-date "Jan 1 12:00:00 1970")))
475 (tday (- (timezone-absolute-from-gregorian
476 (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
477 (timezone-absolute-from-gregorian
478 (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
481 (* 1.0 (nth 0 ttime) 60 60)
482 (* 1.0 tday 60 60 24))))
486 ;;; nnspool.el ends here