Prefer HTTPS to FTP and HTTP in documentation
[emacs.git] / lisp / gnus / nnfolder.el
blob9b1317347a72a3abc46cf421f45d53350d692f67
1 ;;; nnfolder.el --- mail folder access for Gnus
3 ;; Copyright (C) 1995-2017 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
7 ;; Scott Byer <byer@mv.us.adobe.com>
8 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; Keywords: mail
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;;; Code:
31 (require 'nnheader)
32 (require 'message)
33 (require 'nnmail)
34 (require 'nnoo)
35 (eval-when-compile (require 'cl))
36 (require 'gnus)
37 (require 'gnus-util)
38 (require 'gnus-range)
40 ;; FIXME not explicitly used in this file.
41 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
43 (nnoo-declare nnfolder)
45 (defvoo nnfolder-directory (expand-file-name message-directory)
46 "The name of the nnfolder directory.")
48 (defvoo nnfolder-nov-directory nil
49 "The name of the nnfolder NOV directory.
50 If nil, `nnfolder-directory' is used.")
52 (defvoo nnfolder-active-file
53 (nnheader-concat nnfolder-directory "active")
54 "The name of the active file.")
56 ;; I renamed this variable to something more in keeping with the general GNU
57 ;; style. -SLB
59 (defvoo nnfolder-ignore-active-file nil
60 "If non-nil, the active file is ignored.
61 This causes nnfolder to do some extra work in order to determine the
62 true active ranges of an mbox file. Note that the active file is
63 still saved, but its values are not used. This costs some extra time
64 when scanning an mbox when opening it.")
66 (defvoo nnfolder-distrust-mbox nil
67 "If non-nil, the folder will be distrusted.
68 This means that nnfolder will not trust the user with respect to
69 inserting unaccounted for mail in the middle of an mbox file. This
70 can greatly slow down scans, which now must scan the entire file for
71 unmarked messages. When nil, scans occur forward from the last marked
72 message, a huge time saver for large mailboxes.")
74 (defvoo nnfolder-newsgroups-file
75 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
76 "Mail newsgroups description file.")
78 (defvoo nnfolder-get-new-mail t
79 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
81 (defvoo nnfolder-prepare-save-mail-hook nil
82 "Hook run narrowed to an article before saving.")
84 (defvoo nnfolder-save-buffer-hook nil
85 "Hook run before saving the nnfolder mbox buffer.")
88 (defvoo nnfolder-inhibit-expiry nil
89 "If non-nil, inhibit expiry.")
93 (defconst nnfolder-version "nnfolder 2.0"
94 "nnfolder version.")
96 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
97 "String used to demarcate what the article number for a message is.")
99 (defvoo nnfolder-current-group nil)
100 (defvoo nnfolder-current-buffer nil)
101 (defvoo nnfolder-status-string "")
102 (defvoo nnfolder-group-alist nil)
103 (defvoo nnfolder-buffer-alist nil)
104 (defvoo nnfolder-scantime-alist nil)
105 (defvoo nnfolder-active-timestamp nil)
106 (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
107 (defvoo nnfolder-active-file-coding-system-for-write
108 nnmail-active-file-coding-system)
109 (defvoo nnfolder-file-coding-system mm-text-coding-system)
110 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
111 "Coding system for save nnfolder file.
112 if nil, `nnfolder-file-coding-system' is used.") ; FIXME: fill-in the doc-string of this variable
114 (defvoo nnfolder-nov-is-evil nil
115 "If non-nil, Gnus will never generate and use nov databases for mail groups.
116 Using nov databases will speed up header fetching considerably.
117 This variable shouldn't be flipped much. If you have, for some reason,
118 set this to t, and want to set it to nil again, you should always run
119 the `nnfolder-generate-active-file' command. The function will go
120 through all nnfolder directories and generate nov databases for them
121 all. This may very well take some time.")
123 (defvoo nnfolder-nov-file-suffix ".nov")
125 (defvoo nnfolder-nov-buffer-alist nil)
127 (defvar nnfolder-nov-buffer-file-name nil)
131 ;;; Interface functions
133 (nnoo-define-basics nnfolder)
135 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
136 (with-current-buffer nntp-server-buffer
137 (erase-buffer)
138 (let (article start stop num)
139 (nnfolder-possibly-change-group group server)
140 (when nnfolder-current-buffer
141 (set-buffer nnfolder-current-buffer)
142 (goto-char (point-min))
143 (if (stringp (car articles))
144 'headers
145 (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
146 'nov
147 (setq articles (gnus-sorted-intersection
148 ;; Is ARTICLES sorted?
149 (sort articles '<)
150 (nnfolder-existing-articles)))
151 (while (setq article (pop articles))
152 (set-buffer nnfolder-current-buffer)
153 (cond ((nnfolder-goto-article article)
154 (setq start (point))
155 (setq stop (if (search-forward "\n\n" nil t)
156 (1- (point))
157 (point-max)))
158 (set-buffer nntp-server-buffer)
159 (insert (format "221 %d Article retrieved.\n" article))
160 (insert-buffer-substring nnfolder-current-buffer
161 start stop)
162 (goto-char (point-max))
163 (insert ".\n"))
165 ;; If we couldn't find this article, skip over ranges
166 ;; of missing articles so we don't search the whole file
167 ;; for each of them.
168 ((numberp article)
169 (setq start (point))
170 (and
171 ;; Check that we are either at BOF or after an
172 ;; article with a lower number. We do this so we
173 ;; won't be confused by out-of-order article numbers,
174 ;; as caused by active file bogosity.
175 (cond
176 ((bobp))
177 ((search-backward (concat "\n" nnfolder-article-marker)
178 nil t)
179 (goto-char (match-end 0))
180 (setq num (string-to-number
181 (buffer-substring
182 (point) (point-at-eol))))
183 (goto-char start)
184 (< num article)))
185 ;; Check that we are before an article with a
186 ;; higher number.
187 (search-forward (concat "\n" nnfolder-article-marker)
188 nil t)
189 (progn
190 (setq num (string-to-number
191 (buffer-substring
192 (point) (point-at-eol))))
193 (> num article))
194 ;; Discard any article numbers before the one we're
195 ;; now looking at.
196 (while (and articles
197 (< (car articles) num))
198 (setq articles (cdr articles))))
199 (goto-char start))))
200 (set-buffer nntp-server-buffer)
201 (nnheader-fold-continuation-lines)
202 'headers))))))
204 (deffoo nnfolder-open-server (server &optional defs)
205 (nnoo-change-server 'nnfolder server defs)
206 (nnmail-activate 'nnfolder t)
207 (gnus-make-directory nnfolder-directory)
208 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
209 (and nnfolder-nov-directory
210 (gnus-make-directory nnfolder-nov-directory)))
211 (cond
212 ((not (file-exists-p nnfolder-directory))
213 (nnfolder-close-server)
214 (nnheader-report 'nnfolder "Couldn't create directory: %s"
215 nnfolder-directory))
216 ((not (file-directory-p (file-truename nnfolder-directory)))
217 (nnfolder-close-server)
218 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
220 (nnmail-activate 'nnfolder)
221 (nnheader-report 'nnfolder "Opened server %s using directory %s"
222 server nnfolder-directory)
223 t)))
225 (deffoo nnfolder-request-close ()
226 (let ((alist nnfolder-buffer-alist))
227 (while alist
228 (nnfolder-close-group (caar alist) nil t)
229 (setq alist (cdr alist))))
230 (nnoo-close-server 'nnfolder)
231 (setq nnfolder-buffer-alist nil
232 nnfolder-group-alist nil))
234 (deffoo nnfolder-request-article (article &optional group server buffer)
235 (nnfolder-possibly-change-group group server)
236 (with-current-buffer nnfolder-current-buffer
237 (goto-char (point-min))
238 (when (nnfolder-goto-article article)
239 (let (start stop)
240 (setq start (point))
241 (forward-line 1)
242 (unless (and (nnmail-search-unix-mail-delim)
243 (forward-line -1))
244 (goto-char (point-max)))
245 (setq stop (point))
246 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
247 (set-buffer nntp-server-buffer)
248 (erase-buffer)
249 (insert-buffer-substring nnfolder-current-buffer start stop)
250 (goto-char (point-min))
251 (while (looking-at "From ")
252 (delete-char 5)
253 (insert "X-From-Line: ")
254 (forward-line 1))
255 (if (numberp article)
256 (cons nnfolder-current-group article)
257 (goto-char (point-min))
258 (cons nnfolder-current-group
259 (if (search-forward (concat "\n" nnfolder-article-marker)
260 nil t)
261 (string-to-number (buffer-substring
262 (point) (point-at-eol)))
263 -1))))))))
265 (deffoo nnfolder-request-group (group &optional server dont-check info)
266 (nnfolder-possibly-change-group group server t)
267 (save-excursion
268 (cond ((not (assoc group nnfolder-group-alist))
269 (nnheader-report 'nnfolder "No such group: %s" group))
270 ((file-directory-p (nnfolder-group-pathname group))
271 (nnheader-report 'nnfolder "%s is a directory"
272 (file-name-as-directory
273 (let ((nnmail-pathname-coding-system nil))
274 (nnfolder-group-pathname group)))))
275 (dont-check
276 (nnheader-report 'nnfolder "Selected group %s" group)
279 (let* ((active (assoc group nnfolder-group-alist))
280 (group (car active))
281 (range (cadr active)))
282 (cond
283 ((null active)
284 (nnheader-report 'nnfolder "No such group: %s" group))
285 ((null nnfolder-current-group)
286 (nnheader-report 'nnfolder "Empty group: %s" group))
288 (nnheader-report 'nnfolder "Selected group %s" group)
289 (nnheader-insert "211 %d %d %d %s\n"
290 (1+ (- (cdr range) (car range)))
291 (car range) (cdr range) group))))))))
293 (deffoo nnfolder-request-scan (&optional group server)
294 (nnfolder-possibly-change-group nil server)
295 (when nnfolder-get-new-mail
296 (nnfolder-possibly-change-group group server)
297 (nnmail-get-new-mail
298 'nnfolder 'nnfolder-save-all-buffers
299 nnfolder-directory group)))
301 (defun nnfolder-save-all-buffers ()
302 (let ((bufs nnfolder-buffer-alist))
303 (save-excursion
304 (while bufs
305 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
306 (setq nnfolder-buffer-alist
307 (delq (car bufs) nnfolder-buffer-alist))
308 (set-buffer (nth 1 (car bufs)))
309 (nnfolder-save-buffer)
310 (kill-buffer (current-buffer)))
311 (setq bufs (cdr bufs))))))
313 ;; Don't close the buffer if we're not shutting down the server. This way,
314 ;; we can keep the buffer in the group buffer cache, and not have to grovel
315 ;; over the buffer again unless we add new mail to it or modify it in some
316 ;; way.
318 (deffoo nnfolder-close-group (group &optional server force)
319 ;; Make sure we _had_ the group open.
320 (when (or (assoc group nnfolder-buffer-alist)
321 (equal group nnfolder-current-group))
322 (let ((inf (assoc group nnfolder-buffer-alist)))
323 (when inf
324 (when (and nnfolder-current-group
325 nnfolder-current-buffer)
326 (push (list nnfolder-current-group nnfolder-current-buffer)
327 nnfolder-buffer-alist))
328 (setq nnfolder-buffer-alist
329 (delq inf nnfolder-buffer-alist))
330 (setq nnfolder-current-buffer (cadr inf)
331 nnfolder-current-group (car inf))))
332 (when (and nnfolder-current-buffer
333 (buffer-name nnfolder-current-buffer))
334 (with-current-buffer nnfolder-current-buffer
335 ;; If the buffer was modified, write the file out now.
336 (nnfolder-save-buffer)
337 ;; If we're shutting the server down, we need to kill the
338 ;; buffer and remove it from the open buffer list. Or, of
339 ;; course, if we're trying to minimize our space impact.
340 (kill-buffer (current-buffer))
341 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
342 nnfolder-buffer-alist)))))
343 (setq nnfolder-current-group nil
344 nnfolder-current-buffer nil)
347 (deffoo nnfolder-request-create-group (group &optional server args)
348 (nnfolder-possibly-change-group nil server)
349 (nnmail-activate 'nnfolder)
350 (cond ((zerop (length group))
351 (nnheader-report 'nnfolder "Invalid (empty) group name"))
352 ((file-directory-p (nnfolder-group-pathname group))
353 (nnheader-report 'nnfolder "%s is a directory"
354 (file-name-as-directory
355 (let ((nnmail-pathname-coding-system nil))
356 (nnfolder-group-pathname group)))))
357 ((assoc group nnfolder-group-alist)
360 (push (list group (cons 1 0)) nnfolder-group-alist)
361 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
362 (save-current-buffer
363 (nnfolder-read-folder group))
364 t)))
366 (deffoo nnfolder-request-list (&optional server)
367 (nnfolder-possibly-change-group nil server)
368 (save-excursion
369 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
370 (nnmail-find-file nnfolder-active-file)
371 (setq nnfolder-group-alist (nnmail-get-active)))
374 (deffoo nnfolder-request-newgroups (date &optional server)
375 (nnfolder-possibly-change-group nil server)
376 (nnfolder-request-list server))
378 (deffoo nnfolder-request-list-newsgroups (&optional server)
379 (nnfolder-possibly-change-group nil server)
380 (save-excursion
381 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
382 (nnmail-find-file nnfolder-newsgroups-file))))
384 ;; Return a list consisting of all article numbers existing in the
385 ;; current folder.
387 (defun nnfolder-existing-articles ()
388 (save-excursion
389 (when nnfolder-current-buffer
390 (set-buffer nnfolder-current-buffer)
391 (goto-char (point-min))
392 (let ((marker (concat "\n" nnfolder-article-marker))
393 (number "[0-9]+")
394 numbers)
395 (while (and (search-forward marker nil t)
396 (re-search-forward number nil t))
397 (let ((newnum (string-to-number (match-string 0))))
398 (if (nnmail-within-headers-p)
399 (push newnum numbers))))
400 ;; The article numbers are increasing, so this result is sorted.
401 (nreverse numbers)))))
403 (autoload 'gnus-request-group "gnus-int")
404 (declare-function gnus-request-create-group "gnus-int"
405 (group &optional gnus-command-method args))
407 (deffoo nnfolder-request-expire-articles (articles newsgroup
408 &optional server force)
409 (nnfolder-possibly-change-group newsgroup server)
410 (let ((is-old t)
411 ;; The articles we have deleted so far.
412 (deleted-articles nil)
413 ;; The articles that really exist and will
414 ;; be expired if they are old enough.
415 (maybe-expirable
416 (gnus-sorted-intersection articles (nnfolder-existing-articles)))
417 target)
418 (nnmail-activate 'nnfolder)
420 (with-current-buffer nnfolder-current-buffer
421 ;; Since messages are sorted in arrival order and expired in the
422 ;; same order, we can stop as soon as we find a message that is
423 ;; too old.
424 (while (and maybe-expirable is-old)
425 (goto-char (point-min))
426 (when (and (nnfolder-goto-article (car maybe-expirable))
427 (search-forward (concat "\n" nnfolder-article-marker)
428 nil t))
429 (forward-sexp)
430 (when (setq is-old
431 (nnmail-expired-article-p
432 newsgroup
433 (buffer-substring
434 (point) (progn (end-of-line) (point)))
435 force nnfolder-inhibit-expiry))
436 (setq target nnmail-expiry-target)
437 (unless (eq target 'delete)
438 (with-temp-buffer
439 (nnfolder-request-article (car maybe-expirable)
440 newsgroup server (current-buffer))
441 (let ((nnfolder-current-directory nil))
442 (when (functionp target)
443 (setq target (funcall target newsgroup)))
444 (when (and target (not (eq target 'delete)))
445 (if (or (gnus-request-group target)
446 (gnus-request-create-group target))
447 (nnmail-expiry-target-group target newsgroup)
448 (setq target nil)))))
449 (nnfolder-possibly-change-group newsgroup server))
450 (when target
451 (nnheader-message 5 "Deleting article %d in %s..."
452 (car maybe-expirable) newsgroup)
453 (nnfolder-delete-mail)
454 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
455 (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
456 ;; Must remember which articles were actually deleted
457 (push (car maybe-expirable) deleted-articles))))
458 (setq maybe-expirable (cdr maybe-expirable)))
459 (unless nnfolder-inhibit-expiry
460 (nnheader-message 5 "Deleting articles...done"))
461 (nnfolder-save-buffer)
462 (nnfolder-adjust-min-active newsgroup)
463 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
464 (nnfolder-save-all-buffers)
465 (gnus-sorted-difference articles (nreverse deleted-articles)))))
467 (deffoo nnfolder-request-move-article (article group server accept-form
468 &optional last move-is-internal)
469 (save-excursion
470 (let ((buf (get-buffer-create " *nnfolder move*"))
471 result)
472 (and
473 (nnfolder-request-article article group server)
474 (with-current-buffer buf
475 (erase-buffer)
476 (insert-buffer-substring nntp-server-buffer)
477 (goto-char (point-min))
478 (while (re-search-forward
479 (concat "^" nnfolder-article-marker)
480 (save-excursion (and (search-forward "\n\n" nil t) (point)))
482 (gnus-delete-line))
483 (setq result (eval accept-form))
484 (kill-buffer buf)
485 result)
486 (save-excursion
487 (nnfolder-possibly-change-group group server)
488 (set-buffer nnfolder-current-buffer)
489 (goto-char (point-min))
490 (when (nnfolder-goto-article article)
491 (nnfolder-delete-mail))
492 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
493 (nnfolder-nov-delete-article group article))
494 (when last
495 (nnfolder-save-buffer)
496 (nnfolder-adjust-min-active group)
497 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
498 result)))
500 (deffoo nnfolder-request-accept-article (group &optional server last)
501 (save-excursion
502 (nnfolder-possibly-change-group group server)
503 (nnmail-check-syntax)
504 (let ((buf (current-buffer))
505 result art-group)
506 (goto-char (point-min))
507 (when (looking-at "X-From-Line: ")
508 (replace-match "From ")
509 (while (progn (forward-line) (looking-at "[ \t]"))
510 (delete-char -1)))
511 (with-temp-buffer
512 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
513 (nntp-server-buffer (current-buffer)))
514 (nnmail-find-file nnfolder-active-file)
515 (setq nnfolder-group-alist (nnmail-parse-active))))
516 (save-excursion
517 (goto-char (point-min))
518 (if (search-forward "\n\n" nil t)
519 (forward-line -1)
520 (goto-char (point-max)))
521 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
522 (delete-region (point) (progn (forward-line 1) (point))))
523 (when nnmail-cache-accepted-message-ids
524 (nnmail-cache-insert (nnmail-fetch-field "message-id")
525 group
526 (nnmail-fetch-field "subject")
527 (nnmail-fetch-field "from")))
528 (setq result (if (stringp group)
529 (list (cons group (nnfolder-active-number group)))
530 (setq art-group
531 (nnmail-article-group 'nnfolder-active-number))))
532 (if (and (null result)
533 (yes-or-no-p "Moved to `junk' group; delete article? "))
534 (setq result 'junk)
535 (setq result
536 (car (nnfolder-save-mail result)))))
537 (when last
538 (save-excursion
539 (nnfolder-possibly-change-folder (or (caar art-group) group))
540 (nnfolder-save-buffer)
541 (when nnmail-cache-accepted-message-ids
542 (nnmail-cache-close))))
543 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
544 (unless result
545 (nnheader-report 'nnfolder "Couldn't store article"))
546 result)))
548 (deffoo nnfolder-request-replace-article (article group buffer)
549 (nnfolder-possibly-change-group group)
550 (with-current-buffer buffer
551 (goto-char (point-min))
552 (if (not (looking-at "X-From-Line: "))
553 (insert "From nobody " (current-time-string) "\n")
554 (replace-match "From ")
555 (forward-line 1)
556 (while (looking-at "[ \t]")
557 (delete-char -1)
558 (forward-line 1)))
559 (nnfolder-normalize-buffer)
560 (set-buffer nnfolder-current-buffer)
561 (goto-char (point-min))
562 (if (not (nnfolder-goto-article article))
564 (nnfolder-delete-mail)
565 (insert-buffer-substring buffer)
566 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
567 (with-current-buffer buffer
568 (let ((headers (nnfolder-parse-head article
569 (point-min) (point-max))))
570 (with-current-buffer (nnfolder-open-nov group)
571 (if (nnheader-find-nov-line article)
572 (delete-region (point) (progn (forward-line 1) (point))))
573 (nnheader-insert-nov headers)))))
574 (nnfolder-save-buffer)
575 t)))
577 (deffoo nnfolder-request-delete-group (group &optional force server)
578 (nnfolder-close-group group server t)
579 ;; Delete all articles in GROUP.
580 (if (not force)
581 () ; Don't delete the articles.
582 ;; Delete the file that holds the group.
583 (let ((data (nnfolder-group-pathname group))
584 (nov (nnfolder-group-nov-pathname group)))
585 (ignore-errors (delete-file data))
586 (ignore-errors (delete-file nov))))
587 ;; Remove the group from all structures.
588 (setq nnfolder-group-alist
589 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
590 nnfolder-current-group nil
591 nnfolder-current-buffer nil)
592 ;; Save the active file.
593 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
596 (deffoo nnfolder-request-rename-group (group new-name &optional server)
597 (nnfolder-possibly-change-group group server)
598 (with-current-buffer nnfolder-current-buffer
599 (and (file-writable-p buffer-file-name)
600 (ignore-errors
601 (let ((new-file (nnfolder-group-pathname new-name)))
602 (gnus-make-directory (file-name-directory new-file))
603 (rename-file buffer-file-name new-file)
604 (when (file-exists-p (nnfolder-group-nov-pathname group))
605 (setq new-file (nnfolder-group-nov-pathname new-name))
606 (gnus-make-directory (file-name-directory new-file))
607 (rename-file (nnfolder-group-nov-pathname group) new-file)))
609 ;; That went ok, so we change the internal structures.
610 (let ((entry (assoc group nnfolder-group-alist)))
611 (and entry (setcar entry new-name))
612 (setq nnfolder-current-buffer nil
613 nnfolder-current-group nil)
614 ;; Save the new group alist.
615 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
616 ;; We kill the buffer instead of renaming it and stuff.
617 (kill-buffer (current-buffer))
618 t))))
620 (deffoo nnfolder-request-regenerate (server)
621 (nnfolder-possibly-change-group nil server)
622 (nnfolder-generate-active-file)
626 ;;; Internal functions.
628 (defun nnfolder-adjust-min-active (group)
629 ;; Find the lowest active article in this group.
630 (let* ((active (cadr (assoc group nnfolder-group-alist)))
631 (marker (concat "\n" nnfolder-article-marker))
632 (number "[0-9]+")
633 (activemin (cdr active)))
634 (with-current-buffer nnfolder-current-buffer
635 (goto-char (point-min))
636 (while (and (search-forward marker nil t)
637 (re-search-forward number nil t))
638 (let ((newnum (string-to-number (match-string 0))))
639 (if (nnmail-within-headers-p)
640 (setq activemin (min activemin newnum)))))
641 (setcar active activemin))))
643 (defun nnfolder-article-string (article)
644 (if (numberp article)
645 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
646 (concat "\nMessage-ID: " article)))
648 (defun nnfolder-goto-article (article)
649 "Place point at the start of the headers of ARTICLE.
650 ARTICLE can be an article number or a Message-ID.
651 Returns t if successful, nil otherwise."
652 (let ((art-string (nnfolder-article-string article))
653 start found)
654 ;; It is likely that we are at or before the delimiter line.
655 ;; We therefore go to the end of the previous line, and start
656 ;; searching from there.
657 (beginning-of-line)
658 (unless (bobp)
659 (forward-char -1))
660 (setq start (point))
661 ;; First search forward.
662 (while (and (setq found (search-forward art-string nil t))
663 (not (nnmail-within-headers-p))))
664 ;; If unsuccessful, search backward from where we started,
665 (unless found
666 (goto-char start)
667 (while (and (setq found (search-backward art-string nil t))
668 (not (nnmail-within-headers-p)))))
669 (when found
670 (nnmail-search-unix-mail-delim-backward))))
672 (defun nnfolder-delete-mail (&optional leave-delim)
673 "Delete the message that point is in.
674 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
675 deleted. Point is left where the deleted region was."
676 (save-restriction
677 (narrow-to-region
678 (save-excursion
679 ;; In case point is at the beginning of the message already.
680 (forward-line 1)
681 (nnmail-search-unix-mail-delim-backward)
682 (if leave-delim (progn (forward-line 1) (point))
683 (point)))
684 (progn
685 (forward-line 1)
686 (if (nnmail-search-unix-mail-delim)
687 (point)
688 (point-max))))
689 (run-hooks 'nnfolder-delete-mail-hook)
690 (delete-region (point-min) (point-max))))
692 (defun nnfolder-possibly-change-group (group &optional server dont-check)
693 ;; Change servers.
694 (when (and server
695 (not (nnfolder-server-opened server)))
696 (nnfolder-open-server server))
697 (unless (gnus-buffer-live-p nnfolder-current-buffer)
698 (setq nnfolder-current-buffer nil
699 nnfolder-current-group nil))
700 ;; Change group.
701 (let ((file-name-coding-system nnmail-pathname-coding-system))
702 (when (and group
703 (not (equal group nnfolder-current-group))
704 (progn
705 (nnmail-activate 'nnfolder)
706 (and (assoc group nnfolder-group-alist)
707 (file-exists-p (nnfolder-group-pathname group)))))
708 (if dont-check
709 (setq nnfolder-current-group group
710 nnfolder-current-buffer nil)
711 (let (inf file)
712 ;; If we have to change groups, see if we don't already have
713 ;; the folder in memory. If we do, verify the modtime and
714 ;; destroy the folder if needed so we can rescan it.
715 (setq nnfolder-current-buffer
716 (nth 1 (assoc group nnfolder-buffer-alist)))
718 ;; If the buffer is not live, make sure it isn't in the
719 ;; alist. If it is live, verify that nobody else has
720 ;; touched the file since last time.
721 (when (and nnfolder-current-buffer
722 (not (gnus-buffer-live-p nnfolder-current-buffer)))
723 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
724 nnfolder-current-buffer nil))
726 (setq nnfolder-current-group group)
728 (when (or (not nnfolder-current-buffer)
729 (not (verify-visited-file-modtime
730 nnfolder-current-buffer)))
731 (save-excursion
732 (setq file (nnfolder-group-pathname group))
733 ;; See whether we need to create the new file.
734 (unless (file-exists-p file)
735 (gnus-make-directory (file-name-directory file))
736 (let ((nnmail-file-coding-system
737 (or nnfolder-file-coding-system-for-write
738 nnfolder-file-coding-system-for-write)))
739 (nnmail-write-region (point-min) (point-min)
740 file t 'nomesg)))
741 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
742 (set-buffer nnfolder-current-buffer)
743 (push (list group nnfolder-current-buffer)
744 nnfolder-buffer-alist)))))))))
746 (defun nnfolder-save-mail (group-art-list)
747 "Called narrowed to an article."
748 (let* (save-list group-art)
749 (goto-char (point-min))
750 ;; The From line may have been quoted by movemail.
751 (when (looking-at ">From")
752 (delete-char 1))
753 ;; This might come from somewhere else.
754 (unless (looking-at "From ")
755 (insert "From nobody " (current-time-string) "\n")
756 (goto-char (point-min)))
757 ;; Quote all "From " lines in the article.
758 (forward-line 1)
759 (let (case-fold-search)
760 (while (re-search-forward "^From " nil t)
761 (beginning-of-line)
762 (insert "> ")))
763 (setq save-list group-art-list)
764 (nnmail-insert-lines)
765 (nnmail-insert-xref group-art-list)
766 (run-hooks 'nnmail-prepare-save-mail-hook)
767 (run-hooks 'nnfolder-prepare-save-mail-hook)
769 ;; Insert the mail into each of the destination groups.
770 (while (setq group-art (pop group-art-list))
771 ;; Kill any previous newsgroup markers.
772 (goto-char (point-min))
773 (if (search-forward "\n\n" nil t)
774 (forward-line -1)
775 (goto-char (point-max)))
776 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
777 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
779 ;; Insert the new newsgroup marker.
780 (nnfolder-insert-newsgroup-line group-art)
782 (save-excursion
783 (let ((beg (point-min))
784 (end (point-max))
785 (obuf (current-buffer)))
786 (nnfolder-possibly-change-folder (car group-art))
787 (let ((buffer-read-only nil))
788 (nnfolder-normalize-buffer)
789 (insert-buffer-substring obuf beg end))
790 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
791 (set-buffer obuf)
792 (nnfolder-add-nov (car group-art) (cdr group-art)
793 (nnfolder-parse-head nil beg end))))))
795 ;; Did we save it anywhere?
796 save-list))
798 (defun nnfolder-normalize-buffer ()
799 "Make sure there are two newlines at the end of the buffer."
800 (goto-char (point-max))
801 (skip-chars-backward "\n")
802 (delete-region (point) (point-max))
803 (unless (bobp)
804 (insert "\n\n")))
806 (defun nnfolder-insert-newsgroup-line (group-art)
807 (save-excursion
808 (goto-char (point-min))
809 (unless (search-forward "\n\n" nil t)
810 (goto-char (point-max))
811 (insert "\n"))
812 (forward-char -1)
813 (insert (format (concat nnfolder-article-marker "%d %s\n")
814 (cdr group-art) (message-make-date)))))
816 (defun nnfolder-active-number (group)
817 ;; Find the next article number in GROUP.
818 (let ((active (cadr (assoc group nnfolder-group-alist))))
819 (if active
820 (setcdr active (1+ (cdr active)))
821 ;; This group is new, so we create a new entry for it.
822 ;; This might be a bit naughty... creating groups on the drop of
823 ;; a hat, but I don't know...
824 (push (list group (setq active (cons 1 1)))
825 nnfolder-group-alist))
826 (cdr active)))
828 (defun nnfolder-possibly-change-folder (group)
829 (let ((inf (assoc group nnfolder-buffer-alist)))
830 (if (and inf
831 (gnus-buffer-live-p (cadr inf)))
832 (set-buffer (cadr inf))
833 (when inf
834 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
835 (when nnfolder-group-alist
836 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
837 (push (list group (nnfolder-read-folder group))
838 nnfolder-buffer-alist))))
840 ;; This method has a problem if you've accidentally let the active
841 ;; list get out of sync with the files. This could happen, say, if
842 ;; you've accidentally gotten new mail with something other than Gnus
843 ;; (but why would _that_ ever happen? :-). In that case, we will be
844 ;; in the middle of processing the file, ready to add new X-Gnus
845 ;; article number markers, and we'll run across a message with no ID
846 ;; yet - the active list _may_not_ be ready for us yet.
848 ;; To handle this, I'm modifying this routine to maintain the maximum
849 ;; ID seen so far, and when we hit a message with no ID, we will
850 ;; _manually_ scan the rest of the message looking for any more,
851 ;; possibly higher IDs. We'll assume the maximum that we find is the
852 ;; highest active. Note that this shouldn't cost us much extra time
853 ;; at all, but will be a lot less vulnerable to glitches between the
854 ;; mbox and the active file.
856 (defun nnfolder-read-folder (group)
857 (let* ((file (nnfolder-group-pathname group))
858 (nov (nnfolder-group-nov-pathname group))
859 (buffer (set-buffer
860 (let ((nnheader-file-coding-system
861 nnfolder-file-coding-system))
862 (nnheader-find-file-noselect file t)))))
863 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
864 (buffer-disable-undo)
865 (if (equal (cadr (assoc group nnfolder-scantime-alist))
866 (nth 5 (file-attributes file)))
867 ;; This looks up-to-date, so we don't do any scanning.
868 (if (file-exists-p file)
869 buffer
870 (push (list group buffer) nnfolder-buffer-alist)
871 (set-buffer-modified-p t)
872 (nnfolder-save-buffer))
873 ;; Parse the damn thing.
874 (save-excursion
875 (goto-char (point-min))
876 ;; Remove any blank lines at the start.
877 (while (eq (following-char) ?\n)
878 (delete-char 1))
879 (nnmail-activate 'nnfolder)
880 ;; Read in the file.
881 (let ((delim "^From ")
882 (marker (concat "\n" nnfolder-article-marker))
883 (number "[0-9]+")
884 (active (or (cadr (assoc group nnfolder-group-alist))
885 (cons 1 0)))
886 (scantime (assoc group nnfolder-scantime-alist))
887 (minid most-positive-fixnum)
888 maxid start end newscantime
889 novbuf articles newnum
890 buffer-read-only)
891 (setq maxid (cdr active))
893 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
894 (and (file-exists-p nov)
895 (file-newer-than-file-p nov file)))
896 (unless (file-exists-p nov)
897 (gnus-make-directory (file-name-directory nov)))
898 (with-current-buffer
899 (setq novbuf (nnfolder-open-nov group))
900 (goto-char (point-min))
901 (while (not (eobp))
902 (push (read novbuf) articles)
903 (forward-line 1))
904 (setq articles (nreverse articles))))
905 (goto-char (point-min))
907 ;; Anytime the active number is 1 or 0, it is suspect. In
908 ;; that case, search the file manually to find the active
909 ;; number. Or, of course, if we're being paranoid. (This
910 ;; would also be the place to build other lists from the
911 ;; header markers, such as expunge lists, etc., if we ever
912 ;; desired to abandon the active file entirely for mboxes.)
913 (when (or nnfolder-ignore-active-file
914 novbuf
915 (< maxid 2))
916 (while (and (search-forward marker nil t)
917 (looking-at number))
918 (setq newnum (string-to-number (match-string 0)))
919 (when (nnmail-within-headers-p)
920 (setq maxid (max maxid newnum)
921 minid (min minid newnum))
922 (when novbuf
923 (if (memq newnum articles)
924 (setq articles (delq newnum articles))
925 (let ((headers (nnfolder-parse-head newnum)))
926 (with-current-buffer novbuf
927 (nnheader-find-nov-line newnum)
928 (nnheader-insert-nov headers)))))))
929 (when (and novbuf articles)
930 (with-current-buffer novbuf
931 (dolist (article articles)
932 (when (nnheader-find-nov-line article)
933 (delete-region (point)
934 (progn (forward-line 1) (point)))))))
935 (setcar active (max 1 (min minid maxid)))
936 (setcdr active (max maxid (cdr active)))
937 (goto-char (point-min)))
939 ;; As long as we trust that the user will only insert
940 ;; unmarked mail at the end, go to the end and search
941 ;; backwards for the last marker. Find the start of that
942 ;; message, and begin to search for unmarked messages from
943 ;; there.
944 (when (not (or nnfolder-distrust-mbox
945 (< maxid 2)))
946 (goto-char (point-max))
947 (unless (re-search-backward marker nil t)
948 (goto-char (point-min)))
949 ;;(when (nnmail-search-unix-mail-delim)
950 ;; (goto-char (point-min)))
953 ;; Keep track of the active number on our own, and insert it
954 ;; back into the active list when we're done. Also, prime
955 ;; the pump to cut down on the number of searches we do.
956 (unless (nnmail-search-unix-mail-delim)
957 (goto-char (point-max)))
958 (setq end (point-marker))
959 (while (not (= end (point-max)))
960 (setq start (marker-position end))
961 (goto-char end)
962 ;; There may be more than one "From " line, so we skip past
963 ;; them.
964 (while (looking-at delim)
965 (forward-line 1))
966 (set-marker end (if (nnmail-search-unix-mail-delim)
967 (point)
968 (point-max)))
969 (goto-char start)
970 (when (not (search-forward marker end t))
971 (narrow-to-region start end)
972 (nnmail-insert-lines)
973 (nnfolder-insert-newsgroup-line
974 (cons nil
975 (setq newnum
976 (nnfolder-active-number group))))
977 (when novbuf
978 (let ((headers (nnfolder-parse-head newnum (point-min)
979 (point-max))))
980 (with-current-buffer novbuf
981 (goto-char (point-max))
982 (nnheader-insert-nov headers))))
983 (widen)))
985 (set-marker end nil)
986 ;; Make absolutely sure that the active list reflects
987 ;; reality!
988 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
990 ;; Set the scantime for this group.
991 (setq newscantime (visited-file-modtime))
992 (if scantime
993 (setcdr scantime (list newscantime))
994 (push (list group newscantime)
995 nnfolder-scantime-alist))
996 ;; Save nov.
997 (when novbuf
998 (nnfolder-save-nov))
999 (current-buffer))))))
1001 (defun nnfolder-recursive-directory-files (dir prefix)
1002 (let ((files nil))
1003 (dolist (file (directory-files dir))
1004 (cond
1005 ((or (file-symlink-p (expand-file-name file dir))
1006 (member file '("." "..")))
1007 ;; Ignore
1009 ((file-directory-p (expand-file-name file dir))
1010 (setq files (nconc (nnfolder-recursive-directory-files
1011 (expand-file-name file dir)
1012 (if prefix
1013 (concat prefix "." (directory-file-name file))
1014 (file-name-nondirectory file)))
1015 files)))
1016 ((file-regular-p (expand-file-name file dir))
1017 (push (if prefix
1018 (concat prefix "." file)
1019 file)
1020 files))))
1021 files))
1023 ;;;###autoload
1024 (defun nnfolder-generate-active-file ()
1025 "Look for mbox folders in the nnfolder directory and make them into groups.
1026 This command does not work if you use short group names."
1027 (interactive)
1028 (nnmail-activate 'nnfolder)
1029 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1030 (dolist (file (directory-files (or nnfolder-nov-directory
1031 nnfolder-directory)
1033 (concat
1034 (regexp-quote nnfolder-nov-file-suffix)
1035 "$")))
1036 (when (not (message-mail-file-mbox-p file))
1037 (ignore-errors
1038 (delete-file file)))))
1039 (dolist (file (if nnmail-use-long-file-names
1040 (directory-files nnfolder-directory)
1041 (nnfolder-recursive-directory-files
1042 nnfolder-directory nil)))
1043 (when (and (not (backup-file-name-p file))
1044 (message-mail-file-mbox-p
1045 (nnfolder-group-pathname file)))
1046 (let ((oldgroup (assoc file nnfolder-group-alist)))
1047 (if oldgroup
1048 (nnheader-message 5 "Refreshing group %s..." file)
1049 (nnheader-message 5 "Adding group %s..." file))
1050 (if oldgroup
1051 (setq nnfolder-group-alist
1052 (delq oldgroup (copy-sequence nnfolder-group-alist))))
1053 (push (list file (cons 1 0)) nnfolder-group-alist)
1054 (nnfolder-possibly-change-folder file)
1055 (nnfolder-possibly-change-group file)
1056 (nnfolder-close-group file))))
1057 (nnheader-message 5 ""))
1059 (defun nnfolder-group-pathname (group)
1060 "Make file name for GROUP."
1061 (setq group
1062 (encode-coding-string group nnmail-pathname-coding-system))
1063 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
1064 ;; If this file exists, we use it directly.
1065 (if (or nnmail-use-long-file-names
1066 (file-exists-p (concat dir group)))
1067 (concat dir group)
1068 ;; If not, we translate dots into slashes.
1069 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
1071 (defun nnfolder-group-nov-pathname (group)
1072 "Make pathname for GROUP NOV."
1073 (let ((nnfolder-directory
1074 (or nnfolder-nov-directory nnfolder-directory)))
1075 (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
1077 (defvar copyright-update)
1079 (defun nnfolder-save-buffer ()
1080 "Save the buffer."
1081 (let ((delete-old-versions t))
1082 (when (buffer-modified-p)
1083 (run-hooks 'nnfolder-save-buffer-hook)
1084 (gnus-make-directory (file-name-directory (buffer-file-name)))
1085 (let ((coding-system-for-write
1086 (or nnfolder-file-coding-system-for-write
1087 nnfolder-file-coding-system)))
1088 (set (make-local-variable 'copyright-update) nil)
1089 (save-buffer)))
1090 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1091 (nnfolder-save-nov))))
1093 (defun nnfolder-save-active (group-alist active-file)
1094 (let ((nnmail-active-file-coding-system
1095 (or nnfolder-active-file-coding-system-for-write
1096 nnfolder-active-file-coding-system)))
1097 (nnmail-save-active group-alist active-file)))
1099 (defun nnfolder-open-nov (group)
1100 (or (cdr (assoc group nnfolder-nov-buffer-alist))
1101 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1102 (with-current-buffer buffer
1103 (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1104 (nnfolder-group-nov-pathname group))
1105 (erase-buffer)
1106 (when (file-exists-p nnfolder-nov-buffer-file-name)
1107 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1108 (push (cons group buffer) nnfolder-nov-buffer-alist)
1109 buffer)))
1111 (defun nnfolder-save-nov ()
1112 (save-excursion
1113 (while nnfolder-nov-buffer-alist
1114 (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1115 (set-buffer (cdar nnfolder-nov-buffer-alist))
1116 (when (buffer-modified-p)
1117 (gnus-make-directory (file-name-directory
1118 nnfolder-nov-buffer-file-name))
1119 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1120 nil 'nomesg))
1121 (set-buffer-modified-p nil)
1122 (kill-buffer (current-buffer)))
1123 (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1125 (defun nnfolder-nov-delete-article (group article)
1126 (with-current-buffer (nnfolder-open-nov group)
1127 (when (nnheader-find-nov-line article)
1128 (delete-region (point) (progn (forward-line 1) (point))))
1131 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1132 (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1134 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1135 (when (file-exists-p nov)
1136 (with-current-buffer nntp-server-buffer
1137 (erase-buffer)
1138 (nnheader-insert-file-contents nov)
1139 (if (and fetch-old
1140 (not (numberp fetch-old)))
1141 t ; Don't remove anything.
1142 (nnheader-nov-delete-outside-range
1143 (if fetch-old (max 1 (- (car articles) fetch-old))
1144 (car articles))
1145 (car (last articles)))
1146 t))))))
1148 (defun nnfolder-parse-head (&optional number b e)
1149 "Parse the head of the current buffer."
1150 (let ((buf (current-buffer))
1151 chars)
1152 (save-excursion
1153 (unless b
1154 (setq b (if (nnmail-search-unix-mail-delim-backward)
1155 (point) (point-min)))
1156 (forward-line 1)
1157 (setq e (if (nnmail-search-unix-mail-delim)
1158 (point) (point-max))))
1159 (setq chars (- e b))
1160 (unless (zerop chars)
1161 (goto-char b)
1162 (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1163 (with-temp-buffer
1164 (insert-buffer-substring buf b e)
1165 (let ((headers (nnheader-parse-naked-head)))
1166 (mail-header-set-chars headers chars)
1167 (mail-header-set-number headers number)
1168 headers)))))
1170 (defun nnfolder-add-nov (group article headers)
1171 "Add a nov line for the GROUP base."
1172 (with-current-buffer (nnfolder-open-nov group)
1173 (goto-char (point-max))
1174 (mail-header-set-number headers article)
1175 (nnheader-insert-nov headers)))
1177 (provide 'nnfolder)
1179 ;;; nnfolder.el ends here