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 '("-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.")
72 (defconst nnspool-version "nnspool 2.0"
73 "Version numbers of this version of NNSPOOL.")
75 (defvar nnspool-current-directory nil
76 "Current news group directory.")
78 (defvar nnspool-current-group nil)
79 (defvar nnspool-status-string "")
83 ;;; Interface functions.
85 (defun nnspool-retrieve-headers (sequence &optional newsgroup server)
86 "Retrieve the headers for the articles in SEQUENCE.
87 Newsgroup must be selected before calling this function."
89 (set-buffer nntp-server-buffer)
91 (let* ((number (length sequence))
93 (do-message (and (numberp nnspool-large-newsgroup)
94 (> number nnspool-large-newsgroup)))
96 (if (not (nnspool-possibly-change-directory newsgroup))
98 (if (nnspool-retrieve-headers-with-nov sequence)
101 (setq article (car sequence))
102 (setq file (concat nnspool-current-directory
103 (int-to-string article)))
104 (and (file-exists-p file)
106 (insert (format "221 %d Article retrieved.\n" article))
108 (insert-file-contents file)
110 (search-forward "\n\n" nil t)
113 (delete-region (point) (point-max))))
114 (setq sequence (cdr sequence))
117 (zerop (% (setq count (1+ count)) 20))
118 (message "NNSPOOL: Receiving headers... %d%%"
119 (/ (* count 100) number))))
121 (and do-message (message "NNSPOOL: Receiving headers... done"))
123 ;; Fold continuation lines.
125 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
126 (replace-match " " t t))
129 (defun nnspool-open-server (host &optional service)
131 (setq nnspool-status-string "")
132 (cond ((and (file-directory-p nnspool-spool-directory)
133 (file-exists-p nnspool-active-file))
134 (nnheader-init-server-buffer))
136 (setq nnspool-status-string
137 (format "NNSPOOL: cannot talk to %s." host))
140 (defun nnspool-close-server (&optional server)
144 (defun nnspool-server-opened (&optional server)
145 "Return server process status, T or NIL.
146 If the stream is opened, return T, otherwise return NIL."
147 (and nntp-server-buffer
148 (buffer-name nntp-server-buffer)))
150 (defun nnspool-status-message (&optional server)
151 "Return server status response as string."
152 nnspool-status-string)
154 (defun nnspool-request-article (id &optional newsgroup server buffer)
155 "Select article by message ID (or number)."
156 (nnspool-possibly-change-directory newsgroup)
157 (let ((file (if (stringp id)
158 (nnspool-find-article-by-message-id id)
159 (concat nnspool-current-directory (prin1-to-string id))))
160 (nntp-server-buffer (or buffer nntp-server-buffer)))
161 (if (and (stringp file)
163 (not (file-directory-p file)))
165 (nnspool-find-file file)))))
167 (defun nnspool-request-body (id &optional newsgroup server)
168 "Select article body by message ID (or number)."
169 (nnspool-possibly-change-directory newsgroup)
170 (if (nnspool-request-article id)
172 (set-buffer nntp-server-buffer)
173 (goto-char (point-min))
174 (if (search-forward "\n\n" nil t)
175 (delete-region (point-min) (point)))
178 (defun nnspool-request-head (id &optional newsgroup server)
179 "Select article head by message ID (or number)."
180 (nnspool-possibly-change-directory newsgroup)
181 (if (nnspool-request-article id)
183 (set-buffer nntp-server-buffer)
184 (goto-char (point-min))
185 (if (search-forward "\n\n" nil t)
186 (delete-region (1- (point)) (point-max)))
189 (defun nnspool-request-group (group &optional server dont-check)
191 (let ((pathname (nnspool-article-pathname
192 (nnspool-replace-chars-in-string group ?. ?/)))
194 (if (not (file-directory-p pathname))
196 (setq nnspool-status-string
197 "Invalid group name (no such directory)")
199 (setq nnspool-current-directory pathname)
200 (setq nnspool-status-string "")
203 (setq dir (directory-files pathname nil "^[0-9]+$" t))
204 ;; yes, completely empty spool directories *are* possible
205 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
212 (string-to-int name)))
216 (set-buffer nntp-server-buffer)
220 (format "211 %d %d %d %s\n" (length dir) (car dir)
221 (progn (while (cdr dir) (setq dir (cdr dir)))
224 (insert (format "211 0 0 0 %s\n" group))))))
227 (defun nnspool-close-group (group &optional server)
230 (defun nnspool-request-list (&optional server)
231 "List active newsgoups."
233 (nnspool-find-file nnspool-active-file)))
235 (defun nnspool-request-list-newsgroups (&optional server)
236 "List newsgroups (defined in NNTP2)."
238 (nnspool-find-file nnspool-newsgroups-file)))
240 (defun nnspool-request-list-distributions (&optional server)
241 "List distributions (defined in NNTP2)."
243 (nnspool-find-file nnspool-distributions-file)))
245 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
246 (defun nnspool-request-newgroups (date &optional server)
247 "List groups created after DATE."
248 (if (nnspool-find-file nnspool-active-times-file)
250 ;; Find the last valid line.
251 (goto-char (point-max))
252 (while (and (not (looking-at
253 "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
254 (zerop (forward-line -1))))
255 (let ((seconds (nnspool-seconds-since-epoch))
257 ;; Go through lines and add the latest groups to a list.
258 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
260 ;; We insert a .0 to make the list reader
261 ;; interpret the number as a float. It is far
262 ;; too big to be stored in a lisp integer.
263 (goto-char (1- (match-end 0)))
266 (goto-char (match-end 1))
267 (read (current-buffer)))
269 (setq groups (cons (buffer-substring
270 (match-beginning 1) (match-end 1))
272 (zerop (forward-line -1))))
275 (insert (car groups) " 0 0 y\n")
276 (setq groups (cdr groups))))
280 (defun nnspool-request-post (&optional server)
281 "Post a new news in current buffer."
283 ;; We have to work in the server buffer because of NEmacs hack.
284 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
285 (set-buffer nntp-server-buffer)
286 (apply (function call-process-region)
287 (point-min) (point-max)
288 nnspool-inews-program 'delete t nil nnspool-inews-switches)
290 (or (zerop (buffer-size))
291 ;; If inews returns strings, it must be error message
292 ;; unless SPOOLNEWS is defined.
293 ;; This condition is very weak, but there is no good rule
294 ;; identifying errors when SPOOLNEWS is defined.
295 ;; Suggested by ohm@kaba.junet.
296 (string-match "spooled" (buffer-string)))
297 ;; Make status message by unfolding lines.
298 (subst-char-in-region (point-min) (point-max) ?\n ?\\ 'noundo)
299 (setq nnspool-status-string (buffer-string))
302 (fset 'nnspool-request-post-buffer 'nntp-request-post-buffer)
305 ;;; Internal functions.
307 (defun nnspool-retrieve-headers-with-nov (articles)
308 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
310 (let ((nov (concat nnspool-nov-directory
311 (nnspool-replace-chars-in-string
312 nnspool-current-group ?. ?/)
315 (if (file-exists-p nov)
317 (set-buffer nntp-server-buffer)
319 (insert-file-contents nov)
320 ;; First we find the first wanted line. We issue a number
321 ;; of search-forwards - the first article we are lookign
322 ;; for may be expired, so we have to go on searching until
323 ;; we find one of the articles we want.
325 (setq article (concat (int-to-string
326 (car articles)) "\t"))
327 (not (or (looking-at article)
328 (search-forward (concat "\n" article)
330 (setq articles (cdr articles)))
334 (delete-region (point-min) (point))
335 ;; Then we find the last wanted line. We go to the end
336 ;; of the buffer and search backward much the same way
337 ;; we did to find the first article.
338 ;; !!! Perhaps it would be better just to do a (last articles),
339 ;; and go forward successively over each line and
340 ;; compare to avoid this (reverse), like this:
341 ;; (while (and (>= last (read nntp-server-buffer)))
342 ;; (zerop (forward-line 1))))
343 (setq articles (reverse articles))
344 (goto-char (point-max))
346 (not (search-backward
347 (concat "\n" (int-to-string (car articles))
349 (setq articles (cdr articles)))
353 (delete-region (point) (point-max)))))
354 (or articles (progn (erase-buffer) nil)))))))
356 (defun nnspool-find-article-by-message-id (id)
357 "Return full pathname of an article identified by message-ID."
359 (set-buffer nntp-server-buffer)
361 (call-process "grep" nil t nil id nnspool-history-file)
362 (goto-char (point-min))
363 (if (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\(.*\\)$")
364 (concat nnspool-spool-directory
365 (nnspool-replace-chars-in-string
366 (buffer-substring (match-beginning 1) (match-end 1))
369 (defun nnspool-find-file (file)
370 "Insert FILE in server buffer safely."
371 (set-buffer nntp-server-buffer)
374 (progn (insert-file-contents file) t)
377 (defun nnspool-possibly-change-directory (newsgroup)
379 (let ((pathname (nnspool-article-pathname
380 (nnspool-replace-chars-in-string newsgroup ?. ?/))))
381 (if (file-directory-p pathname)
383 (setq nnspool-current-directory pathname)
384 (setq nnspool-current-group newsgroup))
385 (setq nnspool-status-string
386 (format "No such newsgroup: %s" newsgroup))
390 (defun nnspool-article-pathname (group)
391 "Make pathname for GROUP."
392 (concat (file-name-as-directory nnspool-spool-directory) group "/"))
394 (defun nnspool-replace-chars-in-string (string from to)
395 "Replace characters in STRING from FROM to TO."
396 (let ((string (substring string 0)) ;Copy string.
397 (len (length string))
399 ;; Replace all occurrences of FROM with TO.
401 (if (= (aref string idx) from)
402 (aset string idx to))
406 (defun nnspool-number-base-10 (num pos)
408 (setcdr num (+ (* (% (car num) 10) 65536) (cdr num)))
414 (aref "0123456789" (% (cdr num) 10)))
416 (setcdr num (/ (cdr num) 10))
417 (setcar num (/ (car num) 10))
418 (nnspool-number-base-10 num (1- pos))))))))
420 (defun nnspool-seconds-since-epoch ()
421 (let ((time (current-time)))
422 (+ (* 1.0 (car time) 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2)
427 ;;; nnspool.el ends here