* paths.el (news-inews-program): Move to gnus/nnspool.el.
[bpt/emacs.git] / lisp / gnus / nnspool.el
CommitLineData
eec82323 1;;; nnspool.el --- spool access for GNU Emacs
16409b0b 2
acaf905b 3;; Copyright (C) 1988-1990, 1993-1998, 2000-2012
9693d3c4 4;; Free Software Foundation, Inc.
eec82323
LMI
5
6;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
23f87bed 7;; Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
8;; Keywords: news
9
10;; This file is part of GNU Emacs.
11
5e809f55 12;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 13;; it under the terms of the GNU General Public License as published by
5e809f55
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
eec82323
LMI
16
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.
21
22;; You should have received a copy of the GNU General Public License
5e809f55 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
24
25;;; Commentary:
26
27;;; Code:
28
29(require 'nnheader)
30(require 'nntp)
eec82323
LMI
31(require 'nnoo)
32(eval-when-compile (require 'cl))
33
61a583ca
GM
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/")
38 "/usr/spool/news/"
39 "/var/spool/news/")
40 "The root directory below which all news files are stored.")
41(defvaralias 'news-path 'news-directory)
42
ee2f89a6
GM
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")
50 (t "inews"))
51 "Program to post news.")
52
eec82323
LMI
53(nnoo-declare nnspool)
54
55(defvoo nnspool-inews-program news-inews-program
56 "Program to post news.
57This is most commonly `inews' or `injnews'.")
58
59(defvoo nnspool-inews-switches '("-h" "-S")
60 "Switches for nnspool-request-post to pass to `inews' for posting news.
61If you are using Cnews, you probably should set this variable to nil.")
62
5153a47a
MB
63(defvoo nnspool-spool-directory
64 (file-name-as-directory (if (boundp 'news-directory)
65 (symbol-value 'news-directory)
66 news-path))
eec82323
LMI
67 "Local news spool directory.")
68
69(defvoo nnspool-nov-directory (concat nnspool-spool-directory "over.view/")
70 "Local news nov directory.")
71
23f87bed
MB
72(defvoo nnspool-lib-dir
73 (if (file-exists-p "/usr/lib/news/active")
74 "/usr/lib/news/"
75 "/var/lib/news/")
eec82323
LMI
76 "Where the local news library files are stored.")
77
78(defvoo nnspool-active-file (concat nnspool-lib-dir "active")
79 "Local news active file.")
80
81(defvoo nnspool-newsgroups-file (concat nnspool-lib-dir "newsgroups")
82 "Local news newsgroups file.")
83
84(defvoo nnspool-distributions-file (concat nnspool-lib-dir "distribs.pat")
85 "Local news distributions file.")
86
87(defvoo nnspool-history-file (concat nnspool-lib-dir "history")
88 "Local news history file.")
89
90(defvoo nnspool-active-times-file (concat nnspool-lib-dir "active.times")
91 "Local news active date file.")
92
93(defvoo nnspool-large-newsgroup 50
23f87bed
MB
94 "The number of articles which indicates a large newsgroup.
95If the number of articles is greater than the value, verbose
eec82323
LMI
96messages will be shown to indicate the current status.")
97
98(defvoo nnspool-nov-is-evil nil
99 "Non-nil means that nnspool will never return NOV lines instead of headers.")
100
101(defconst nnspool-sift-nov-with-sed nil
102 "If non-nil, use sed to get the relevant portion from the overview file.
103If nil, nnspool will load the entire file into a buffer and process it
104there.")
105
106(defvoo nnspool-rejected-article-hook nil
107 "*A hook that will be run when an article has been rejected by the server.")
108
6748645f
LMI
109(defvoo nnspool-file-coding-system nnheader-file-coding-system
110 "Coding system for nnspool.")
111
eec82323
LMI
112\f
113
114(defconst nnspool-version "nnspool 2.0"
115 "Version numbers of this version of NNSPOOL.")
116
117(defvoo nnspool-current-directory nil
118 "Current news group directory.")
119
120(defvoo nnspool-current-group nil)
121(defvoo nnspool-status-string "")
122
123\f
124;;; Interface functions.
125
126(nnoo-define-basics nnspool)
127
128(deffoo nnspool-retrieve-headers (articles &optional group server fetch-old)
129 "Retrieve the headers of ARTICLES."
20a673b2 130 (with-current-buffer nntp-server-buffer
eec82323
LMI
131 (erase-buffer)
132 (when (nnspool-possibly-change-directory group)
133 (let* ((number (length articles))
134 (count 0)
135 (default-directory nnspool-current-directory)
136 (do-message (and (numberp nnspool-large-newsgroup)
137 (> number nnspool-large-newsgroup)))
3a787ab0 138 (nnheader-file-coding-system nnspool-file-coding-system)
eec82323
LMI
139 file beg article ag)
140 (if (and (numberp (car articles))
141 (nnspool-retrieve-headers-with-nov articles fetch-old))
142 ;; We successfully retrieved the NOV headers.
143 'nov
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
150 (car ag) (cdr ag)))
151 article (cdr ag))
152 ;; This is an article in the current group.
153 (setq file (int-to-string article)))
154 ;; Insert the head of the article.
155 (when (and file
156 (file-exists-p file))
157 (insert "221 ")
158 (princ article (current-buffer))
159 (insert " Article retrieved.\n")
160 (setq beg (point))
161 (inline (nnheader-insert-head file))
162 (goto-char beg)
16409b0b
GM
163 (if (search-forward "\n\n" nil t)
164 (progn
165 (forward-char -1)
166 (insert ".\n"))
167 (goto-char (point-max))
168 (if (bolp)
169 (insert ".\n")
170 (insert "\n.\n")))
eec82323
LMI
171 (delete-region (point) (point-max)))
172
173 (and do-message
174 (zerop (% (incf count) 20))
6748645f 175 (nnheader-message 5 "nnspool: Receiving headers... %d%%"
16409b0b 176 (/ (* count 100) number))))
eec82323
LMI
177
178 (when do-message
6748645f 179 (nnheader-message 5 "nnspool: Receiving headers...done"))
eec82323
LMI
180
181 ;; Fold continuation lines.
182 (nnheader-fold-continuation-lines)
183 'headers)))))
184
185(deffoo nnspool-open-server (server &optional defs)
186 (nnoo-change-server 'nnspool server defs)
187 (cond
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
193 (directory-file-name
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))
200 (t
201 (nnheader-report 'nnspool "Opened server %s using directory %s"
202 server nnspool-spool-directory)
203 t)))
204
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))
209 file ag)
210 (if (stringp id)
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)))
215 (and file
216 (file-exists-p file)
217 (not (file-directory-p file))
218 (save-excursion (nnspool-find-file file))
219 ;; We return the article number and group name.
220 (if (numberp id)
221 (cons nnspool-current-group id)
222 ag))))
223
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)))
228 (when res
20a673b2 229 (with-current-buffer nntp-server-buffer
eec82323
LMI
230 (goto-char (point-min))
231 (when (search-forward "\n\n" nil t)
232 (delete-region (point-min) (point)))
233 res))))
234
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)))
239 (when res
20a673b2 240 (with-current-buffer nntp-server-buffer
eec82323
LMI
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)))
245 res))
246
286c4fc2 247(deffoo nnspool-request-group (group &optional server dont-check info)
eec82323
LMI
248 "Select news GROUP."
249 (let ((pathname (nnspool-article-pathname group))
250 dir)
251 (if (not (file-directory-p pathname))
252 (nnheader-report
253 'nnspool "Invalid group name (no such directory): %s" group)
254 (setq nnspool-current-directory pathname)
255 (nnheader-report 'nnspool "Selected group %s" group)
256 (if dont-check
257 (progn
258 (nnheader-report 'nnspool "Selected group %s" group)
259 t)
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))
01c52d31 263 (setq dir (sort (mapcar 'string-to-number dir) '<)))
eec82323
LMI
264 (if dir
265 (nnheader-insert
266 "211 %d %d %d %s\n" (length dir) (car dir)
01c52d31 267 (car (last dir)) group)
eec82323
LMI
268 (nnheader-report 'nnspool "Empty group %s" group)
269 (nnheader-insert "211 0 0 0 %s\n" group))))))
270
271(deffoo nnspool-request-type (group &optional article)
272 'news)
273
274(deffoo nnspool-close-group (group &optional server)
275 t)
276
277(deffoo nnspool-request-list (&optional server)
278 "List active newsgroups."
279 (save-excursion
280 (or (nnspool-find-file nnspool-active-file)
281 (nnheader-report 'nnspool (nnheader-file-error nnspool-active-file)))))
282
283(deffoo nnspool-request-list-newsgroups (&optional server)
284 "List newsgroups (defined in NNTP2)."
285 (save-excursion
286 (or (nnspool-find-file nnspool-newsgroups-file)
287 (nnheader-report 'nnspool (nnheader-file-error
288 nnspool-newsgroups-file)))))
289
290(deffoo nnspool-request-list-distributions (&optional server)
291 "List distributions (defined in NNTP2)."
292 (save-excursion
293 (or (nnspool-find-file nnspool-distributions-file)
294 (nnheader-report 'nnspool (nnheader-file-error
295 nnspool-distributions-file)))))
296
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)
301 (save-excursion
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))))
9693d3c4
GM
307 ;; We require nnheader which requires gnus-util.
308 (let ((seconds (gnus-float-time (date-to-time date)))
eec82323
LMI
309 groups)
310 ;; Go through lines and add the latest groups to a list.
311 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
312 (progn
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)))
317 (insert ".0")
318 (> (progn
319 (goto-char (match-end 1))
320 (read (current-buffer)))
321 seconds))
322 (push (buffer-substring
16409b0b
GM
323 (match-beginning 1) (match-end 1))
324 groups)
eec82323
LMI
325 (zerop (forward-line -1))))
326 (erase-buffer)
01c52d31
MB
327 (dolist (group groups)
328 (insert group " 0 0 y\n")))
eec82323
LMI
329 t)
330 nil))
331
332(deffoo nnspool-request-post (&optional server)
333 "Post a new news in current buffer."
334 (save-excursion
335 (let* ((process-connection-type nil) ; t bugs out on Solaris
336 (inews-buffer (generate-new-buffer " *nnspool post*"))
337 (proc
338 (condition-case err
339 (apply 'start-process "*nnspool inews*" inews-buffer
340 nnspool-inews-program nnspool-inews-switches)
341 (error
342 (nnheader-report 'nnspool "inews error: %S" err)))))
343 (if (not proc)
344 ;; The inews program failed.
345 ()
346 (nnheader-report 'nnspool "")
347 (set-process-sentinel proc 'nnspool-inews-sentinel)
24c4abf9
SZ
348 (mm-with-unibyte-current-buffer
349 (process-send-region proc (point-min) (point-max)))
eec82323
LMI
350 ;; We slap a condition-case around this, because the process may
351 ;; have exited already...
352 (ignore-errors
353 (process-send-eof proc))
354 t))))
355
356
357\f
358;;; Internal functions.
359
360(defun nnspool-inews-sentinel (proc status)
20a673b2 361 (with-current-buffer (process-buffer proc)
eec82323
LMI
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))
6748645f 370 (nnheader-message 5 "nnspool: %s" nnspool-status-string)
eec82323
LMI
371 (ding)
372 (run-hooks 'nnspool-rejected-article-hook))))
373
374(defun nnspool-retrieve-headers-with-nov (articles &optional fetch-old)
375 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
376 nil
377 (let ((nov (nnheader-group-pathname
378 nnspool-current-group nnspool-nov-directory ".overview"))
379 (arts articles)
23f87bed 380 (nnheader-file-coding-system nnspool-file-coding-system)
eec82323
LMI
381 last)
382 (if (not (file-exists-p nov))
383 ()
20a673b2 384 (with-current-buffer nntp-server-buffer
eec82323
LMI
385 (erase-buffer)
386 (if nnspool-sift-nov-with-sed
387 (nnspool-sift-nov-with-sed articles nov)
388 (nnheader-insert-file-contents nov)
389 (if (and fetch-old
390 (not (numberp fetch-old)))
391 t ; We want all the headers.
392 (ignore-errors
393 ;; Delete unwanted NOV lines.
394 (nnheader-nov-delete-outside-range
395 (if fetch-old (max 1 (- (car articles) fetch-old))
396 (car articles))
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
402 ;; in the group.
403 (forward-line -1)
404 (setq last (read (current-buffer)))
405 (if (= last (car articles))
406 ;; Yup, it's all there.
407 t
408 ;; Perhaps not. We try to find the missing articles.
409 (while (and arts
410 (<= last (car arts)))
411 (pop arts))
412 ;; The articles in `arts' are missing from the buffer.
01c52d31 413 (mapc 'nnspool-insert-nov-head arts)
eec82323
LMI
414 t))))))))))
415
416(defun nnspool-insert-nov-head (article)
417 "Read the head of ARTICLE, convert to NOV headers, and insert."
418 (save-excursion
419 (let ((cur (current-buffer))
094ae2ab 420 buf)
eec82323
LMI
421 (setq buf (nnheader-set-temp-buffer " *nnspool head*"))
422 (when (nnheader-insert-head
094ae2ab
G
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)))
427 (set-buffer cur)
428 (goto-char (point-max))
429 (nnheader-insert-nov headers)))
eec82323
LMI
430 (kill-buffer buf))))
431
432(defun nnspool-sift-nov-with-sed (articles file)
433 (let ((first (car articles))
01c52d31 434 (last (car (last articles))))
eec82323
LMI
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))
438 file)))
439
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)
01c52d31 443 (with-temp-buffer
eec82323
LMI
444 (ignore-errors
445 (call-process "grep" nil t nil (regexp-quote id) nnspool-history-file))
446 (goto-char (point-min))
01c52d31
MB
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))))))
eec82323
LMI
449
450(defun nnspool-find-file (file)
451 "Insert FILE in server buffer safely."
452 (set-buffer nntp-server-buffer)
453 (erase-buffer)
454 (condition-case ()
16409b0b
GM
455 (let ((coding-system-for-read nnspool-file-coding-system))
456 (mm-insert-file-contents file)
3a787ab0 457 t)
eec82323
LMI
458 (file-error nil)))
459
460(defun nnspool-possibly-change-directory (group)
461 (if (not group)
462 t
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)))))
468
469(defun nnspool-article-pathname (group &optional article)
35ef97a5 470 "Find the file name for GROUP."
eec82323
LMI
471 (nnheader-group-pathname group nnspool-spool-directory article))
472
eec82323
LMI
473(provide 'nnspool)
474
475;;; nnspool.el ends here