Suppress localized settings in Tramp test.
[emacs.git] / lisp / gnus / nnspool.el
blob9e9537af1f9d698c038a85a3c38fb4027f67e120
1 ;;; nnspool.el --- spool access for GNU Emacs
3 ;; Copyright (C) 1988-1990, 1993-1998, 2000-2015 Free Software
4 ;; Foundation, Inc.
6 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; Keywords: news
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/>.
25 ;;; Commentary:
27 ;;; Code:
29 (require 'nnheader)
30 (require 'nntp)
31 (require 'nnoo)
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 (eval-and-compile
38 (defvar news-directory (if (file-exists-p "/usr/spool/news/")
39 "/usr/spool/news/"
40 "/var/spool/news/")
41 "The root directory below which all news files are stored.")
42 (defvaralias 'news-path 'news-directory))
44 ;; Ditto re obsolescence.
45 (defvar news-inews-program
46 (cond ((file-exists-p "/usr/bin/inews") "/usr/bin/inews")
47 ((file-exists-p "/usr/local/inews") "/usr/local/inews")
48 ((file-exists-p "/usr/local/bin/inews") "/usr/local/bin/inews")
49 ((file-exists-p "/usr/contrib/lib/news/inews") "/usr/contrib/lib/news/inews")
50 ((file-exists-p "/usr/lib/news/inews") "/usr/lib/news/inews")
51 (t "inews"))
52 "Program to post news.")
54 (nnoo-declare nnspool)
56 (defvoo nnspool-inews-program news-inews-program
57 "Program to post news.
58 This is most commonly `inews' or `injnews'.")
60 (defvoo nnspool-inews-switches '("-h" "-S")
61 "Switches for nnspool-request-post to pass to `inews' for posting news.
62 If you are using Cnews, you probably should set this variable to nil.")
64 (defvoo nnspool-spool-directory
65 (file-name-as-directory (if (boundp 'news-directory)
66 (symbol-value 'news-directory)
67 news-path))
68 "Local news spool directory.")
70 (defvoo nnspool-nov-directory (concat nnspool-spool-directory "over.view/")
71 "Local news nov directory.")
73 (defvoo nnspool-lib-dir
74 (if (file-exists-p "/usr/lib/news/active")
75 "/usr/lib/news/"
76 "/var/lib/news/")
77 "Where the local news library files are stored.")
79 (defvoo nnspool-active-file (concat nnspool-lib-dir "active")
80 "Local news active file.")
82 (defvoo nnspool-newsgroups-file (concat nnspool-lib-dir "newsgroups")
83 "Local news newsgroups file.")
85 (defvoo nnspool-distributions-file (concat nnspool-lib-dir "distribs.pat")
86 "Local news distributions file.")
88 (defvoo nnspool-history-file (concat nnspool-lib-dir "history")
89 "Local news history file.")
91 (defvoo nnspool-active-times-file (concat nnspool-lib-dir "active.times")
92 "Local news active date file.")
94 (defvoo nnspool-large-newsgroup 50
95 "The number of articles which indicates a large newsgroup.
96 If the number of articles is greater than the value, verbose
97 messages will be shown to indicate the current status.")
99 (defvoo nnspool-nov-is-evil nil
100 "Non-nil means that nnspool will never return NOV lines instead of headers.")
102 (defconst nnspool-sift-nov-with-sed nil
103 "If non-nil, use sed to get the relevant portion from the overview file.
104 If nil, nnspool will load the entire file into a buffer and process it
105 there.")
107 (defvoo nnspool-rejected-article-hook nil
108 "*A hook that will be run when an article has been rejected by the server.")
110 (defvoo nnspool-file-coding-system nnheader-file-coding-system
111 "Coding system for nnspool.")
115 (defconst nnspool-version "nnspool 2.0"
116 "Version numbers of this version of NNSPOOL.")
118 (defvoo nnspool-current-directory nil
119 "Current news group directory.")
121 (defvoo nnspool-current-group nil)
122 (defvoo nnspool-status-string "")
125 ;;; Interface functions.
127 (nnoo-define-basics nnspool)
129 (deffoo nnspool-retrieve-headers (articles &optional group server fetch-old)
130 "Retrieve the headers of ARTICLES."
131 (with-current-buffer nntp-server-buffer
132 (erase-buffer)
133 (when (nnspool-possibly-change-directory group)
134 (let* ((number (length articles))
135 (count 0)
136 (default-directory nnspool-current-directory)
137 (do-message (and (numberp nnspool-large-newsgroup)
138 (> number nnspool-large-newsgroup)))
139 (nnheader-file-coding-system nnspool-file-coding-system)
140 file beg article ag)
141 (if (and (numberp (car articles))
142 (nnspool-retrieve-headers-with-nov articles fetch-old))
143 ;; We successfully retrieved the NOV headers.
144 'nov
145 ;; No NOV headers here, so we do it the hard way.
146 (while (setq article (pop articles))
147 (if (stringp article)
148 ;; This is a Message-ID.
149 (setq ag (nnspool-find-id article)
150 file (and ag (nnspool-article-pathname
151 (car ag) (cdr ag)))
152 article (cdr ag))
153 ;; This is an article in the current group.
154 (setq file (int-to-string article)))
155 ;; Insert the head of the article.
156 (when (and file
157 (file-exists-p file))
158 (insert "221 ")
159 (princ article (current-buffer))
160 (insert " Article retrieved.\n")
161 (setq beg (point))
162 (inline (nnheader-insert-head file))
163 (goto-char beg)
164 (if (search-forward "\n\n" nil t)
165 (progn
166 (forward-char -1)
167 (insert ".\n"))
168 (goto-char (point-max))
169 (if (bolp)
170 (insert ".\n")
171 (insert "\n.\n")))
172 (delete-region (point) (point-max)))
174 (and do-message
175 (zerop (% (incf count) 20))
176 (nnheader-message 5 "nnspool: Receiving headers... %d%%"
177 (/ (* count 100) number))))
179 (when do-message
180 (nnheader-message 5 "nnspool: Receiving headers...done"))
182 ;; Fold continuation lines.
183 (nnheader-fold-continuation-lines)
184 'headers)))))
186 (deffoo nnspool-open-server (server &optional defs)
187 (nnoo-change-server 'nnspool server defs)
188 (cond
189 ((not (file-exists-p nnspool-spool-directory))
190 (nnspool-close-server)
191 (nnheader-report 'nnspool "Spool directory doesn't exist: %s"
192 nnspool-spool-directory))
193 ((not (file-directory-p
194 (directory-file-name
195 (file-truename nnspool-spool-directory))))
196 (nnspool-close-server)
197 (nnheader-report 'nnspool "Not a directory: %s" nnspool-spool-directory))
198 ((not (file-exists-p nnspool-active-file))
199 (nnheader-report 'nnspool "The active file doesn't exist: %s"
200 nnspool-active-file))
202 (nnheader-report 'nnspool "Opened server %s using directory %s"
203 server nnspool-spool-directory)
204 t)))
206 (deffoo nnspool-request-article (id &optional group server buffer)
207 "Select article by message ID (or number)."
208 (nnspool-possibly-change-directory group)
209 (let ((nntp-server-buffer (or buffer nntp-server-buffer))
210 file ag)
211 (if (stringp id)
212 ;; This is a Message-ID.
213 (when (setq ag (nnspool-find-id id))
214 (setq file (nnspool-article-pathname (car ag) (cdr ag))))
215 (setq file (nnspool-article-pathname nnspool-current-group id)))
216 (and file
217 (file-exists-p file)
218 (not (file-directory-p file))
219 (save-excursion (nnspool-find-file file))
220 ;; We return the article number and group name.
221 (if (numberp id)
222 (cons nnspool-current-group id)
223 ag))))
225 (deffoo nnspool-request-body (id &optional group server)
226 "Select article body by message ID (or number)."
227 (nnspool-possibly-change-directory group)
228 (let ((res (nnspool-request-article id)))
229 (when res
230 (with-current-buffer nntp-server-buffer
231 (goto-char (point-min))
232 (when (search-forward "\n\n" nil t)
233 (delete-region (point-min) (point)))
234 res))))
236 (deffoo nnspool-request-head (id &optional group server)
237 "Select article head by message ID (or number)."
238 (nnspool-possibly-change-directory group)
239 (let ((res (nnspool-request-article id)))
240 (when res
241 (with-current-buffer nntp-server-buffer
242 (goto-char (point-min))
243 (when (search-forward "\n\n" nil t)
244 (delete-region (1- (point)) (point-max)))
245 (nnheader-fold-continuation-lines)))
246 res))
248 (deffoo nnspool-request-group (group &optional server dont-check info)
249 "Select news GROUP."
250 (let ((pathname (nnspool-article-pathname group))
251 dir)
252 (if (not (file-directory-p pathname))
253 (nnheader-report
254 'nnspool "Invalid group name (no such directory): %s" group)
255 (setq nnspool-current-directory pathname)
256 (nnheader-report 'nnspool "Selected group %s" group)
257 (if dont-check
258 (progn
259 (nnheader-report 'nnspool "Selected group %s" group)
261 ;; Yes, completely empty spool directories *are* possible.
262 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
263 (when (setq dir (directory-files pathname nil "^[0-9]+$" t))
264 (setq dir (sort (mapcar 'string-to-number dir) '<)))
265 (if dir
266 (nnheader-insert
267 "211 %d %d %d %s\n" (length dir) (car dir)
268 (car (last dir)) group)
269 (nnheader-report 'nnspool "Empty group %s" group)
270 (nnheader-insert "211 0 0 0 %s\n" group))))))
272 (deffoo nnspool-request-type (group &optional article)
273 'news)
275 (deffoo nnspool-close-group (group &optional server)
278 (deffoo nnspool-request-list (&optional server)
279 "List active newsgroups."
280 (save-excursion
281 (or (nnspool-find-file nnspool-active-file)
282 (nnheader-report 'nnspool (nnheader-file-error nnspool-active-file)))))
284 (deffoo nnspool-request-list-newsgroups (&optional server)
285 "List newsgroups (defined in NNTP2)."
286 (save-excursion
287 (or (nnspool-find-file nnspool-newsgroups-file)
288 (nnheader-report 'nnspool (nnheader-file-error
289 nnspool-newsgroups-file)))))
291 (deffoo nnspool-request-list-distributions (&optional server)
292 "List distributions (defined in NNTP2)."
293 (save-excursion
294 (or (nnspool-find-file nnspool-distributions-file)
295 (nnheader-report 'nnspool (nnheader-file-error
296 nnspool-distributions-file)))))
298 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
299 (deffoo nnspool-request-newgroups (date &optional server)
300 "List groups created after DATE."
301 (if (nnspool-find-file nnspool-active-times-file)
302 (save-excursion
303 ;; Find the last valid line.
304 (goto-char (point-max))
305 (while (and (not (looking-at
306 "\\([^ ]+\\) +\\([0-9]+\\)[0-9][0-9][0-9] "))
307 (zerop (forward-line -1))))
308 ;; We require nnheader which requires gnus-util.
309 (let ((seconds (gnus-float-time (date-to-time date)))
310 groups)
311 ;; Go through lines and add the latest groups to a list.
312 (while (and (looking-at "\\([^ ]+\\) +[0-9]+ ")
313 (progn
314 ;; We insert a .0 to make the list reader
315 ;; interpret the number as a float. It is far
316 ;; too big to be stored in a lisp integer.
317 (goto-char (1- (match-end 0)))
318 (insert ".0")
319 (> (progn
320 (goto-char (match-end 1))
321 (read (current-buffer)))
322 seconds))
323 (push (buffer-substring
324 (match-beginning 1) (match-end 1))
325 groups)
326 (zerop (forward-line -1))))
327 (erase-buffer)
328 (dolist (group groups)
329 (insert group " 0 0 y\n")))
331 nil))
333 (deffoo nnspool-request-post (&optional server)
334 "Post a new news in current buffer."
335 (save-excursion
336 (let* ((process-connection-type nil) ; t bugs out on Solaris
337 (inews-buffer (generate-new-buffer " *nnspool post*"))
338 (proc
339 (condition-case err
340 (apply 'start-process "*nnspool inews*" inews-buffer
341 nnspool-inews-program nnspool-inews-switches)
342 (error
343 (nnheader-report 'nnspool "inews error: %S" err)))))
344 (if (not proc)
345 ;; The inews program failed.
347 (nnheader-report 'nnspool "")
348 (set-process-sentinel proc 'nnspool-inews-sentinel)
349 (mm-with-unibyte-current-buffer
350 (process-send-region proc (point-min) (point-max)))
351 ;; We slap a condition-case around this, because the process may
352 ;; have exited already...
353 (ignore-errors
354 (process-send-eof proc))
355 t))))
359 ;;; Internal functions.
361 (defun nnspool-inews-sentinel (proc status)
362 (with-current-buffer (process-buffer proc)
363 (goto-char (point-min))
364 (if (or (zerop (buffer-size))
365 (search-forward "spooled" nil t))
366 (kill-buffer (current-buffer))
367 ;; Make status message by folding lines.
368 (while (re-search-forward "[ \t\n]+" nil t)
369 (replace-match " " t t))
370 (nnheader-report 'nnspool "%s" (buffer-string))
371 (nnheader-message 5 "nnspool: %s" nnspool-status-string)
372 (ding)
373 (run-hooks 'nnspool-rejected-article-hook))))
375 (defun nnspool-retrieve-headers-with-nov (articles &optional fetch-old)
376 (if (or gnus-nov-is-evil nnspool-nov-is-evil)
378 (let ((nov (nnheader-group-pathname
379 nnspool-current-group nnspool-nov-directory ".overview"))
380 (arts articles)
381 (nnheader-file-coding-system nnspool-file-coding-system)
382 last)
383 (if (not (file-exists-p nov))
385 (with-current-buffer nntp-server-buffer
386 (erase-buffer)
387 (if nnspool-sift-nov-with-sed
388 (nnspool-sift-nov-with-sed articles nov)
389 (nnheader-insert-file-contents nov)
390 (if (and fetch-old
391 (not (numberp fetch-old)))
392 t ; We want all the headers.
393 (ignore-errors
394 ;; Delete unwanted NOV lines.
395 (nnheader-nov-delete-outside-range
396 (if fetch-old (max 1 (- (car articles) fetch-old))
397 (car articles))
398 (car (last articles)))
399 ;; If the buffer is empty, this wasn't very successful.
400 (unless (zerop (buffer-size))
401 ;; We check what the last article number was.
402 ;; The NOV file may be out of sync with the articles
403 ;; in the group.
404 (forward-line -1)
405 (setq last (read (current-buffer)))
406 (if (= last (car articles))
407 ;; Yup, it's all there.
409 ;; Perhaps not. We try to find the missing articles.
410 (while (and arts
411 (<= last (car arts)))
412 (pop arts))
413 ;; The articles in `arts' are missing from the buffer.
414 (mapc 'nnspool-insert-nov-head arts)
415 t))))))))))
417 (defun nnspool-insert-nov-head (article)
418 "Read the head of ARTICLE, convert to NOV headers, and insert."
419 (save-excursion
420 (let ((cur (current-buffer))
421 buf)
422 (setq buf (nnheader-set-temp-buffer " *nnspool head*"))
423 (when (nnheader-insert-head
424 (nnspool-article-pathname nnspool-current-group article))
425 (nnheader-insert-article-line article)
426 (goto-char (point-min))
427 (let ((headers (nnheader-parse-head)))
428 (set-buffer cur)
429 (goto-char (point-max))
430 (nnheader-insert-nov headers)))
431 (kill-buffer buf))))
433 (defun nnspool-sift-nov-with-sed (articles file)
434 (let ((first (car articles))
435 (last (car (last articles))))
436 (call-process "awk" nil t nil
437 (format "BEGIN {firstmsg=%d; lastmsg=%d;}\n $1 >= firstmsg && $1 <= lastmsg {print;}"
438 (1- first) (1+ last))
439 file)))
441 ;; Fixed by fdc@cliwe.ping.de (Frank D. Cringle).
442 ;; Find out what group an article identified by a Message-ID is in.
443 (defun nnspool-find-id (id)
444 (with-temp-buffer
445 (ignore-errors
446 (call-process "grep" nil t nil (regexp-quote id) nnspool-history-file))
447 (goto-char (point-min))
448 (when (looking-at "<[^>]+>[ \t]+[-0-9~]+[ \t]+\\([^ /\t\n]+\\)/\\([0-9]+\\)[ \t\n]")
449 (cons (match-string 1) (string-to-number (match-string 2))))))
451 (defun nnspool-find-file (file)
452 "Insert FILE in server buffer safely."
453 (set-buffer nntp-server-buffer)
454 (erase-buffer)
455 (condition-case ()
456 (let ((coding-system-for-read nnspool-file-coding-system))
457 (mm-insert-file-contents file)
459 (file-error nil)))
461 (defun nnspool-possibly-change-directory (group)
462 (if (not group)
464 (let ((pathname (nnspool-article-pathname group)))
465 (if (file-directory-p pathname)
466 (setq nnspool-current-directory pathname
467 nnspool-current-group group)
468 (nnheader-report 'nnspool "No such newsgroup: %s" group)))))
470 (defun nnspool-article-pathname (group &optional article)
471 "Find the file name for GROUP."
472 (nnheader-group-pathname group nnspool-spool-directory article))
474 (provide 'nnspool)
476 ;;; nnspool.el ends here