1 ;;; nnfolder.el --- mail folder access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <simon@josefsson.org> (adding MARKS)
7 ;; ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
8 ;; Scott Byer <byer@mv.us.adobe.com>
9 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
10 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
34 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
40 (eval-when-compile (require 'cl
))
46 (autoload 'gnus-article-unpropagatable-p
"gnus-sum")
47 (autoload 'gnus-intersection
"gnus-range"))
49 (nnoo-declare nnfolder
)
51 (defvoo nnfolder-directory
(expand-file-name message-directory
)
52 "The name of the nnfolder directory.")
54 (defvoo nnfolder-nov-directory nil
55 "The name of the nnfolder NOV directory.
56 If nil, `nnfolder-directory' is used.")
58 (defvoo nnfolder-marks-directory nil
59 "The name of the nnfolder MARKS directory.
60 If nil, `nnfolder-directory' is used.")
62 (defvoo nnfolder-active-file
63 (nnheader-concat nnfolder-directory
"active")
64 "The name of the active file.")
66 ;; I renamed this variable to something more in keeping with the general GNU
69 (defvoo nnfolder-ignore-active-file nil
70 "If non-nil, the active file is ignored.
71 This causes nnfolder to do some extra work in order to determine the
72 true active ranges of an mbox file. Note that the active file is
73 still saved, but its values are not used. This costs some extra time
74 when scanning an mbox when opening it.")
76 (defvoo nnfolder-distrust-mbox nil
77 "If non-nil, the folder will be distrusted.
78 This means that nnfolder will not trust the user with respect to
79 inserting unaccounted for mail in the middle of an mbox file. This
80 can greatly slow down scans, which now must scan the entire file for
81 unmarked messages. When nil, scans occur forward from the last marked
82 message, a huge time saver for large mailboxes.")
84 (defvoo nnfolder-newsgroups-file
85 (concat (file-name-as-directory nnfolder-directory
) "newsgroups")
86 "Mail newsgroups description file.")
88 (defvoo nnfolder-get-new-mail t
89 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
91 (defvoo nnfolder-prepare-save-mail-hook nil
92 "Hook run narrowed to an article before saving.")
94 (defvoo nnfolder-save-buffer-hook nil
95 "Hook run before saving the nnfolder mbox buffer.")
98 (defvoo nnfolder-inhibit-expiry nil
99 "If non-nil, inhibit expiry.")
103 (defconst nnfolder-version
"nnfolder 2.0"
106 (defconst nnfolder-article-marker
"X-Gnus-Article-Number: "
107 "String used to demarcate what the article number for a message is.")
109 (defvoo nnfolder-current-group nil
)
110 (defvoo nnfolder-current-buffer nil
)
111 (defvoo nnfolder-status-string
"")
112 (defvoo nnfolder-group-alist nil
)
113 (defvoo nnfolder-buffer-alist nil
)
114 (defvoo nnfolder-scantime-alist nil
)
115 (defvoo nnfolder-active-timestamp nil
)
116 (defvoo nnfolder-active-file-coding-system mm-text-coding-system
)
117 (defvoo nnfolder-active-file-coding-system-for-write
118 nnmail-active-file-coding-system
)
119 (defvoo nnfolder-file-coding-system mm-text-coding-system
)
120 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
121 "Coding system for save nnfolder file.
122 if nil, `nnfolder-file-coding-system' is used.") ; FIXME: fill-in the doc-string of this variable
124 (defvoo nnfolder-nov-is-evil nil
125 "If non-nil, Gnus will never generate and use nov databases for mail groups.
126 Using nov databases will speed up header fetching considerably.
127 This variable shouldn't be flipped much. If you have, for some reason,
128 set this to t, and want to set it to nil again, you should always run
129 the `nnfolder-generate-active-file' command. The function will go
130 through all nnfolder directories and generate nov databases for them
131 all. This may very well take some time.")
133 (defvoo nnfolder-nov-file-suffix
".nov")
135 (defvoo nnfolder-nov-buffer-alist nil
)
137 (defvar nnfolder-nov-buffer-file-name nil
)
139 (defvoo nnfolder-marks-is-evil nil
140 "If non-nil, Gnus will never generate and use marks file for mail groups.
141 Using marks files makes it possible to backup and restore mail groups
142 separately from `.newsrc.eld'. If you have, for some reason, set
143 this to t, and want to set it to nil again, you should always remove
144 the corresponding marks file (usually base nnfolder file name
145 concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
146 the group. Then the marks file will be regenerated properly by Gnus.")
148 (defvoo nnfolder-marks nil
)
150 (defvoo nnfolder-marks-file-suffix
".mrk")
152 (defvar nnfolder-marks-modtime
(gnus-make-hashtable))
156 ;;; Interface functions
158 (nnoo-define-basics nnfolder
)
160 (deffoo nnfolder-retrieve-headers
(articles &optional group server fetch-old
)
162 (set-buffer nntp-server-buffer
)
164 (let (article start stop num
)
165 (nnfolder-possibly-change-group group server
)
166 (when nnfolder-current-buffer
167 (set-buffer nnfolder-current-buffer
)
168 (goto-char (point-min))
169 (if (stringp (car articles
))
171 (if (nnfolder-retrieve-headers-with-nov articles fetch-old
)
173 (setq articles
(gnus-sorted-intersection
174 ;; Is ARTICLES sorted?
176 (nnfolder-existing-articles)))
177 (while (setq article
(pop articles
))
178 (set-buffer nnfolder-current-buffer
)
179 (cond ((nnfolder-goto-article article
)
181 (setq stop
(if (search-forward "\n\n" nil t
)
184 (set-buffer nntp-server-buffer
)
185 (insert (format "221 %d Article retrieved.\n" article
))
186 (insert-buffer-substring nnfolder-current-buffer
188 (goto-char (point-max))
191 ;; If we couldn't find this article, skip over ranges
192 ;; of missing articles so we don't search the whole file
197 ;; Check that we are either at BOF or after an
198 ;; article with a lower number. We do this so we
199 ;; won't be confused by out-of-order article numbers,
200 ;; as caused by active file bogosity.
203 ((search-backward (concat "\n" nnfolder-article-marker
)
205 (goto-char (match-end 0))
206 (setq num
(string-to-number
208 (point) (point-at-eol))))
211 ;; Check that we are before an article with a
213 (search-forward (concat "\n" nnfolder-article-marker
)
216 (setq num
(string-to-number
218 (point) (point-at-eol))))
220 ;; Discard any article numbers before the one we're
223 (< (car articles
) num
))
224 (setq articles
(cdr articles
))))
226 (set-buffer nntp-server-buffer
)
227 (nnheader-fold-continuation-lines)
230 (deffoo nnfolder-open-server
(server &optional defs
)
231 (nnoo-change-server 'nnfolder server defs
)
232 (nnmail-activate 'nnfolder t
)
233 (gnus-make-directory nnfolder-directory
)
234 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
235 (and nnfolder-nov-directory
236 (gnus-make-directory nnfolder-nov-directory
)))
237 (unless nnfolder-marks-is-evil
238 (and nnfolder-marks-directory
239 (gnus-make-directory nnfolder-marks-directory
)))
241 ((not (file-exists-p nnfolder-directory
))
242 (nnfolder-close-server)
243 (nnheader-report 'nnfolder
"Couldn't create directory: %s"
245 ((not (file-directory-p (file-truename nnfolder-directory
)))
246 (nnfolder-close-server)
247 (nnheader-report 'nnfolder
"Not a directory: %s" nnfolder-directory
))
249 (nnmail-activate 'nnfolder
)
250 (nnheader-report 'nnfolder
"Opened server %s using directory %s"
251 server nnfolder-directory
)
254 (deffoo nnfolder-request-close
()
255 (let ((alist nnfolder-buffer-alist
))
257 (nnfolder-close-group (caar alist
) nil t
)
258 (setq alist
(cdr alist
))))
259 (nnoo-close-server 'nnfolder
)
260 (setq nnfolder-buffer-alist nil
261 nnfolder-group-alist nil
))
263 (deffoo nnfolder-request-article
(article &optional group server buffer
)
264 (nnfolder-possibly-change-group group server
)
266 (set-buffer nnfolder-current-buffer
)
267 (goto-char (point-min))
268 (when (nnfolder-goto-article article
)
272 (unless (and (nnmail-search-unix-mail-delim)
274 (goto-char (point-max)))
276 (let ((nntp-server-buffer (or buffer nntp-server-buffer
)))
277 (set-buffer nntp-server-buffer
)
279 (insert-buffer-substring nnfolder-current-buffer start stop
)
280 (goto-char (point-min))
281 (while (looking-at "From ")
283 (insert "X-From-Line: ")
285 (if (numberp article
)
286 (cons nnfolder-current-group article
)
287 (goto-char (point-min))
288 (cons nnfolder-current-group
289 (if (search-forward (concat "\n" nnfolder-article-marker
)
291 (string-to-number (buffer-substring
292 (point) (point-at-eol)))
295 (deffoo nnfolder-request-group
(group &optional server dont-check
)
296 (nnfolder-possibly-change-group group server t
)
298 (cond ((not (assoc group nnfolder-group-alist
))
299 (nnheader-report 'nnfolder
"No such group: %s" group
))
300 ((file-directory-p (nnfolder-group-pathname group
))
301 (nnheader-report 'nnfolder
"%s is a directory"
302 (file-name-as-directory
303 (let ((nnmail-pathname-coding-system nil
))
304 (nnfolder-group-pathname group
)))))
306 (nnheader-report 'nnfolder
"Selected group %s" group
)
309 (let* ((active (assoc group nnfolder-group-alist
))
311 (range (cadr active
)))
314 (nnheader-report 'nnfolder
"No such group: %s" group
))
315 ((null nnfolder-current-group
)
316 (nnheader-report 'nnfolder
"Empty group: %s" group
))
318 (nnheader-report 'nnfolder
"Selected group %s" group
)
319 (nnheader-insert "211 %d %d %d %s\n"
320 (1+ (- (cdr range
) (car range
)))
321 (car range
) (cdr range
) group
))))))))
323 (deffoo nnfolder-request-scan
(&optional group server
)
324 (nnfolder-possibly-change-group nil server
)
325 (when nnfolder-get-new-mail
326 (nnfolder-possibly-change-group group server
)
330 (let ((bufs nnfolder-buffer-alist
))
333 (if (not (gnus-buffer-live-p (nth 1 (car bufs
))))
334 (setq nnfolder-buffer-alist
335 (delq (car bufs
) nnfolder-buffer-alist
))
336 (set-buffer (nth 1 (car bufs
)))
337 (nnfolder-save-buffer)
338 (kill-buffer (current-buffer)))
339 (setq bufs
(cdr bufs
))))))
343 ;; Don't close the buffer if we're not shutting down the server. This way,
344 ;; we can keep the buffer in the group buffer cache, and not have to grovel
345 ;; over the buffer again unless we add new mail to it or modify it in some
348 (deffoo nnfolder-close-group
(group &optional server force
)
349 ;; Make sure we _had_ the group open.
350 (when (or (assoc group nnfolder-buffer-alist
)
351 (equal group nnfolder-current-group
))
352 (let ((inf (assoc group nnfolder-buffer-alist
)))
354 (when (and nnfolder-current-group
355 nnfolder-current-buffer
)
356 (push (list nnfolder-current-group nnfolder-current-buffer
)
357 nnfolder-buffer-alist
))
358 (setq nnfolder-buffer-alist
359 (delq inf nnfolder-buffer-alist
))
360 (setq nnfolder-current-buffer
(cadr inf
)
361 nnfolder-current-group
(car inf
))))
362 (when (and nnfolder-current-buffer
363 (buffer-name nnfolder-current-buffer
))
365 (set-buffer nnfolder-current-buffer
)
366 ;; If the buffer was modified, write the file out now.
367 (nnfolder-save-buffer)
368 ;; If we're shutting the server down, we need to kill the
369 ;; buffer and remove it from the open buffer list. Or, of
370 ;; course, if we're trying to minimize our space impact.
371 (kill-buffer (current-buffer))
372 (setq nnfolder-buffer-alist
(delq (assoc group nnfolder-buffer-alist
)
373 nnfolder-buffer-alist
)))))
374 (setq nnfolder-current-group nil
375 nnfolder-current-buffer nil
)
378 (deffoo nnfolder-request-create-group
(group &optional server args
)
379 (nnfolder-possibly-change-group nil server
)
380 (nnmail-activate 'nnfolder
)
381 (cond ((zerop (length group
))
382 (nnheader-report 'nnfolder
"Invalid (empty) group name"))
383 ((file-directory-p (nnfolder-group-pathname group
))
384 (nnheader-report 'nnfolder
"%s is a directory"
385 (file-name-as-directory
386 (let ((nnmail-pathname-coding-system nil
))
387 (nnfolder-group-pathname group
)))))
388 ((assoc group nnfolder-group-alist
)
391 (push (list group
(cons 1 0)) nnfolder-group-alist
)
392 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
394 (nnfolder-read-folder group
))
397 (deffoo nnfolder-request-list
(&optional server
)
398 (nnfolder-possibly-change-group nil server
)
400 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system
))
401 (nnmail-find-file nnfolder-active-file
)
402 (setq nnfolder-group-alist
(nnmail-get-active)))
405 (deffoo nnfolder-request-newgroups
(date &optional server
)
406 (nnfolder-possibly-change-group nil server
)
407 (nnfolder-request-list server
))
409 (deffoo nnfolder-request-list-newsgroups
(&optional server
)
410 (nnfolder-possibly-change-group nil server
)
412 (let ((nnmail-file-coding-system nnfolder-file-coding-system
))
413 (nnmail-find-file nnfolder-newsgroups-file
))))
415 ;; Return a list consisting of all article numbers existing in the
418 (defun nnfolder-existing-articles ()
420 (when nnfolder-current-buffer
421 (set-buffer nnfolder-current-buffer
)
422 (goto-char (point-min))
423 (let ((marker (concat "\n" nnfolder-article-marker
))
426 (while (and (search-forward marker nil t
)
427 (re-search-forward number nil t
))
428 (let ((newnum (string-to-number (match-string 0))))
429 (if (nnmail-within-headers-p)
430 (push newnum numbers
))))
431 ;; The article numbers are increasing, so this result is sorted.
432 (nreverse numbers
)))))
434 (autoload 'gnus-request-group
"gnus-int")
435 (declare-function gnus-request-create-group
"gnus-int"
436 (group &optional gnus-command-method args
))
438 (deffoo nnfolder-request-expire-articles
(articles newsgroup
439 &optional server force
)
440 (nnfolder-possibly-change-group newsgroup server
)
442 ;; The articles we have deleted so far.
443 (deleted-articles nil
)
444 ;; The articles that really exist and will
445 ;; be expired if they are old enough.
447 (gnus-sorted-intersection articles
(nnfolder-existing-articles)))
449 (nnmail-activate 'nnfolder
)
452 (set-buffer nnfolder-current-buffer
)
453 ;; Since messages are sorted in arrival order and expired in the
454 ;; same order, we can stop as soon as we find a message that is
456 (while (and maybe-expirable is-old
)
457 (goto-char (point-min))
458 (when (and (nnfolder-goto-article (car maybe-expirable
))
459 (search-forward (concat "\n" nnfolder-article-marker
)
463 (nnmail-expired-article-p
466 (point) (progn (end-of-line) (point)))
467 force nnfolder-inhibit-expiry
))
468 (setq target nnmail-expiry-target
)
469 (unless (eq target
'delete
)
471 (nnfolder-request-article (car maybe-expirable
)
472 newsgroup server
(current-buffer))
473 (let ((nnfolder-current-directory nil
))
474 (when (functionp target
)
475 (setq target
(funcall target newsgroup
)))
477 (or (gnus-request-group target
)
478 (gnus-request-create-group target
)))
479 (nnmail-expiry-target-group target newsgroup
)
481 (nnfolder-possibly-change-group newsgroup server
))
483 (nnheader-message 5 "Deleting article %d in %s..."
484 (car maybe-expirable
) newsgroup
)
485 (nnfolder-delete-mail)
486 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
487 (nnfolder-nov-delete-article newsgroup
(car maybe-expirable
)))
488 ;; Must remember which articles were actually deleted
489 (push (car maybe-expirable
) deleted-articles
))))
490 (setq maybe-expirable
(cdr maybe-expirable
)))
491 (unless nnfolder-inhibit-expiry
492 (nnheader-message 5 "Deleting articles...done"))
493 (nnfolder-save-buffer)
494 (nnfolder-adjust-min-active newsgroup
)
495 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
496 (gnus-sorted-difference articles
(nreverse deleted-articles
)))))
498 (deffoo nnfolder-request-move-article
(article group server accept-form
499 &optional last move-is-internal
)
501 (let ((buf (get-buffer-create " *nnfolder move*"))
504 (nnfolder-request-article article group server
)
508 (insert-buffer-substring nntp-server-buffer
)
509 (goto-char (point-min))
510 (while (re-search-forward
511 (concat "^" nnfolder-article-marker
)
512 (save-excursion (and (search-forward "\n\n" nil t
) (point)))
515 (setq result
(eval accept-form
))
519 (nnfolder-possibly-change-group group server
)
520 (set-buffer nnfolder-current-buffer
)
521 (goto-char (point-min))
522 (when (nnfolder-goto-article article
)
523 (nnfolder-delete-mail))
524 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
525 (nnfolder-nov-delete-article group article
))
527 (nnfolder-save-buffer)
528 (nnfolder-adjust-min-active group
)
529 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))))
532 (deffoo nnfolder-request-accept-article
(group &optional server last
)
534 (nnfolder-possibly-change-group group server
)
535 (nnmail-check-syntax)
536 (let ((buf (current-buffer))
538 (goto-char (point-min))
539 (when (looking-at "X-From-Line: ")
540 (replace-match "From ")
541 (while (progn (forward-line) (looking-at "[ \t]"))
544 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system
)
545 (nntp-server-buffer (current-buffer)))
546 (nnmail-find-file nnfolder-active-file
)
547 (setq nnfolder-group-alist
(nnmail-parse-active))))
549 (goto-char (point-min))
550 (if (search-forward "\n\n" nil t
)
552 (goto-char (point-max)))
553 (while (re-search-backward (concat "^" nnfolder-article-marker
) nil t
)
554 (delete-region (point) (progn (forward-line 1) (point))))
555 (when nnmail-cache-accepted-message-ids
556 (nnmail-cache-insert (nnmail-fetch-field "message-id")
558 (nnmail-fetch-field "subject")
559 (nnmail-fetch-field "from")))
560 (setq result
(if (stringp group
)
561 (list (cons group
(nnfolder-active-number group
)))
563 (nnmail-article-group 'nnfolder-active-number
))))
564 (if (and (null result
)
565 (yes-or-no-p "Moved to `junk' group; delete article? "))
568 (car (nnfolder-save-mail result
)))))
571 (nnfolder-possibly-change-folder (or (caar art-group
) group
))
572 (nnfolder-save-buffer)
573 (when nnmail-cache-accepted-message-ids
574 (nnmail-cache-close))))
575 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
577 (nnheader-report 'nnfolder
"Couldn't store article"))
580 (deffoo nnfolder-request-replace-article
(article group buffer
)
581 (nnfolder-possibly-change-group group
)
584 (goto-char (point-min))
585 (if (not (looking-at "X-From-Line: "))
586 (insert "From nobody " (current-time-string) "\n")
587 (replace-match "From ")
589 (while (looking-at "[ \t]")
592 (nnfolder-normalize-buffer)
593 (set-buffer nnfolder-current-buffer
)
594 (goto-char (point-min))
595 (if (not (nnfolder-goto-article article
))
597 (nnfolder-delete-mail)
598 (insert-buffer-substring buffer
)
599 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
602 (let ((headers (nnfolder-parse-head article
603 (point-min) (point-max))))
604 (with-current-buffer (nnfolder-open-nov group
)
605 (if (nnheader-find-nov-line article
)
606 (delete-region (point) (progn (forward-line 1) (point))))
607 (nnheader-insert-nov headers
)))))
608 (nnfolder-save-buffer)
611 (deffoo nnfolder-request-delete-group
(group &optional force server
)
612 (nnfolder-close-group group server t
)
613 ;; Delete all articles in GROUP.
615 () ; Don't delete the articles.
616 ;; Delete the file that holds the group.
617 (let ((data (nnfolder-group-pathname group
))
618 (nov (nnfolder-group-nov-pathname group
))
619 (mrk (nnfolder-group-marks-pathname group
)))
620 (ignore-errors (delete-file data
))
621 (ignore-errors (delete-file nov
))
622 (ignore-errors (delete-file mrk
))))
623 ;; Remove the group from all structures.
624 (setq nnfolder-group-alist
625 (delq (assoc group nnfolder-group-alist
) nnfolder-group-alist
)
626 nnfolder-current-group nil
627 nnfolder-current-buffer nil
)
628 ;; Save the active file.
629 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
632 (deffoo nnfolder-request-rename-group
(group new-name
&optional server
)
633 (nnfolder-possibly-change-group group server
)
635 (set-buffer nnfolder-current-buffer
)
636 (and (file-writable-p buffer-file-name
)
638 (let ((new-file (nnfolder-group-pathname new-name
)))
639 (gnus-make-directory (file-name-directory new-file
))
640 (rename-file buffer-file-name new-file
)
641 (when (file-exists-p (nnfolder-group-nov-pathname group
))
642 (setq new-file
(nnfolder-group-nov-pathname new-name
))
643 (gnus-make-directory (file-name-directory new-file
))
644 (rename-file (nnfolder-group-nov-pathname group
) new-file
))
645 (when (file-exists-p (nnfolder-group-marks-pathname group
))
646 (setq new-file
(nnfolder-group-marks-pathname new-name
))
647 (gnus-make-directory (file-name-directory new-file
))
648 (rename-file (nnfolder-group-marks-pathname group
) new-file
)))
650 ;; That went ok, so we change the internal structures.
651 (let ((entry (assoc group nnfolder-group-alist
)))
652 (and entry
(setcar entry new-name
))
653 (setq nnfolder-current-buffer nil
654 nnfolder-current-group nil
)
655 ;; Save the new group alist.
656 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
657 ;; We kill the buffer instead of renaming it and stuff.
658 (kill-buffer (current-buffer))
661 (deffoo nnfolder-request-regenerate
(server)
662 (nnfolder-possibly-change-group nil server
)
663 (nnfolder-generate-active-file)
667 ;;; Internal functions.
669 (defun nnfolder-adjust-min-active (group)
670 ;; Find the lowest active article in this group.
671 (let* ((active (cadr (assoc group nnfolder-group-alist
)))
672 (marker (concat "\n" nnfolder-article-marker
))
674 (activemin (cdr active
)))
676 (set-buffer nnfolder-current-buffer
)
677 (goto-char (point-min))
678 (while (and (search-forward marker nil t
)
679 (re-search-forward number nil t
))
680 (let ((newnum (string-to-number (match-string 0))))
681 (if (nnmail-within-headers-p)
682 (setq activemin
(min activemin newnum
)))))
683 (setcar active activemin
))))
685 (defun nnfolder-article-string (article)
686 (if (numberp article
)
687 (concat "\n" nnfolder-article-marker
(int-to-string article
) " ")
688 (concat "\nMessage-ID: " article
)))
690 (defun nnfolder-goto-article (article)
691 "Place point at the start of the headers of ARTICLE.
692 ARTICLE can be an article number or a Message-ID.
693 Returns t if successful, nil otherwise."
694 (let ((art-string (nnfolder-article-string article
))
696 ;; It is likely that we are at or before the delimiter line.
697 ;; We therefore go to the end of the previous line, and start
698 ;; searching from there.
703 ;; First search forward.
704 (while (and (setq found
(search-forward art-string nil t
))
705 (not (nnmail-within-headers-p))))
706 ;; If unsuccessful, search backward from where we started,
709 (while (and (setq found
(search-backward art-string nil t
))
710 (not (nnmail-within-headers-p)))))
712 (nnmail-search-unix-mail-delim-backward))))
714 (defun nnfolder-delete-mail (&optional leave-delim
)
715 "Delete the message that point is in.
716 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
717 deleted. Point is left where the deleted region was."
721 ;; In case point is at the beginning of the message already.
723 (nnmail-search-unix-mail-delim-backward)
724 (if leave-delim
(progn (forward-line 1) (point))
728 (if (nnmail-search-unix-mail-delim)
731 (run-hooks 'nnfolder-delete-mail-hook
)
732 (delete-region (point-min) (point-max))))
734 (defun nnfolder-possibly-change-group (group &optional server dont-check
)
737 (not (nnfolder-server-opened server
)))
738 (nnfolder-open-server server
))
739 (unless (gnus-buffer-live-p nnfolder-current-buffer
)
740 (setq nnfolder-current-buffer nil
741 nnfolder-current-group nil
))
743 (let ((file-name-coding-system nnmail-pathname-coding-system
))
745 (not (equal group nnfolder-current-group
))
747 (nnmail-activate 'nnfolder
)
748 (and (assoc group nnfolder-group-alist
)
749 (file-exists-p (nnfolder-group-pathname group
)))))
751 (setq nnfolder-current-group group
752 nnfolder-current-buffer nil
)
754 ;; If we have to change groups, see if we don't already have
755 ;; the folder in memory. If we do, verify the modtime and
756 ;; destroy the folder if needed so we can rescan it.
757 (setq nnfolder-current-buffer
758 (nth 1 (assoc group nnfolder-buffer-alist
)))
760 ;; If the buffer is not live, make sure it isn't in the
761 ;; alist. If it is live, verify that nobody else has
762 ;; touched the file since last time.
763 (when (and nnfolder-current-buffer
764 (not (gnus-buffer-live-p nnfolder-current-buffer
)))
765 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)
766 nnfolder-current-buffer nil
))
768 (setq nnfolder-current-group group
)
770 (when (or (not nnfolder-current-buffer
)
771 (not (verify-visited-file-modtime
772 nnfolder-current-buffer
)))
774 (setq file
(nnfolder-group-pathname group
))
775 ;; See whether we need to create the new file.
776 (unless (file-exists-p file
)
777 (gnus-make-directory (file-name-directory file
))
778 (let ((nnmail-file-coding-system
779 (or nnfolder-file-coding-system-for-write
780 nnfolder-file-coding-system-for-write
)))
781 (nnmail-write-region (point-min) (point-min)
783 (when (setq nnfolder-current-buffer
(nnfolder-read-folder group
))
784 (set-buffer nnfolder-current-buffer
)
785 (push (list group nnfolder-current-buffer
)
786 nnfolder-buffer-alist
)))))))))
788 (defun nnfolder-save-mail (group-art-list)
789 "Called narrowed to an article."
790 (let* (save-list group-art
)
791 (goto-char (point-min))
792 ;; The From line may have been quoted by movemail.
793 (when (looking-at ">From")
795 ;; This might come from somewhere else.
796 (unless (looking-at "From ")
797 (insert "From nobody " (current-time-string) "\n")
798 (goto-char (point-min)))
799 ;; Quote all "From " lines in the article.
801 (let (case-fold-search)
802 (while (re-search-forward "^From " nil t
)
805 (setq save-list group-art-list
)
806 (nnmail-insert-lines)
807 (nnmail-insert-xref group-art-list
)
808 (run-hooks 'nnmail-prepare-save-mail-hook
)
809 (run-hooks 'nnfolder-prepare-save-mail-hook
)
811 ;; Insert the mail into each of the destination groups.
812 (while (setq group-art
(pop group-art-list
))
813 ;; Kill any previous newsgroup markers.
814 (goto-char (point-min))
815 (if (search-forward "\n\n" nil t
)
817 (goto-char (point-max)))
818 (while (search-backward (concat "\n" nnfolder-article-marker
) nil t
)
819 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
821 ;; Insert the new newsgroup marker.
822 (nnfolder-insert-newsgroup-line group-art
)
825 (let ((beg (point-min))
827 (obuf (current-buffer)))
828 (nnfolder-possibly-change-folder (car group-art
))
829 (let ((buffer-read-only nil
))
830 (nnfolder-normalize-buffer)
831 (insert-buffer-substring obuf beg end
))
832 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
834 (nnfolder-add-nov (car group-art
) (cdr group-art
)
835 (nnfolder-parse-head nil beg end
))))))
837 ;; Did we save it anywhere?
840 (defun nnfolder-normalize-buffer ()
841 "Make sure there are two newlines at the end of the buffer."
842 (goto-char (point-max))
843 (skip-chars-backward "\n")
844 (delete-region (point) (point-max))
848 (defun nnfolder-insert-newsgroup-line (group-art)
850 (goto-char (point-min))
851 (unless (search-forward "\n\n" nil t
)
852 (goto-char (point-max))
855 (insert (format (concat nnfolder-article-marker
"%d %s\n")
856 (cdr group-art
) (message-make-date)))))
858 (defun nnfolder-active-number (group)
859 ;; Find the next article number in GROUP.
860 (let ((active (cadr (assoc group nnfolder-group-alist
))))
862 (setcdr active
(1+ (cdr active
)))
863 ;; This group is new, so we create a new entry for it.
864 ;; This might be a bit naughty... creating groups on the drop of
865 ;; a hat, but I don't know...
866 (push (list group
(setq active
(cons 1 1)))
867 nnfolder-group-alist
))
870 (defun nnfolder-possibly-change-folder (group)
871 (let ((inf (assoc group nnfolder-buffer-alist
)))
873 (gnus-buffer-live-p (cadr inf
)))
874 (set-buffer (cadr inf
))
876 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)))
877 (when nnfolder-group-alist
878 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))
879 (push (list group
(nnfolder-read-folder group
))
880 nnfolder-buffer-alist
))))
882 ;; This method has a problem if you've accidentally let the active
883 ;; list get out of sync with the files. This could happen, say, if
884 ;; you've accidentally gotten new mail with something other than Gnus
885 ;; (but why would _that_ ever happen? :-). In that case, we will be
886 ;; in the middle of processing the file, ready to add new X-Gnus
887 ;; article number markers, and we'll run across a message with no ID
888 ;; yet - the active list _may_not_ be ready for us yet.
890 ;; To handle this, I'm modifying this routine to maintain the maximum
891 ;; ID seen so far, and when we hit a message with no ID, we will
892 ;; _manually_ scan the rest of the message looking for any more,
893 ;; possibly higher IDs. We'll assume the maximum that we find is the
894 ;; highest active. Note that this shouldn't cost us much extra time
895 ;; at all, but will be a lot less vulnerable to glitches between the
896 ;; mbox and the active file.
898 (defun nnfolder-read-folder (group)
899 (let* ((file (nnfolder-group-pathname group
))
900 (nov (nnfolder-group-nov-pathname group
))
902 (let ((nnheader-file-coding-system
903 nnfolder-file-coding-system
))
904 (nnheader-find-file-noselect file t
)))))
905 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
906 (buffer-disable-undo)
907 (if (equal (cadr (assoc group nnfolder-scantime-alist
))
908 (nth 5 (file-attributes file
)))
909 ;; This looks up-to-date, so we don't do any scanning.
910 (if (file-exists-p file
)
912 (push (list group buffer
) nnfolder-buffer-alist
)
913 (set-buffer-modified-p t
)
914 (nnfolder-save-buffer))
915 ;; Parse the damn thing.
917 (goto-char (point-min))
918 ;; Remove any blank lines at the start.
919 (while (eq (following-char) ?
\n)
921 (nnmail-activate 'nnfolder
)
923 (let ((delim "^From ")
924 (marker (concat "\n" nnfolder-article-marker
))
926 (active (or (cadr (assoc group nnfolder-group-alist
))
928 (scantime (assoc group nnfolder-scantime-alist
))
930 maxid start end newscantime
931 novbuf articles newnum
933 (setq maxid
(cdr active
))
935 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
936 (and (file-exists-p nov
)
937 (file-newer-than-file-p nov file
)))
938 (unless (file-exists-p nov
)
939 (gnus-make-directory (file-name-directory nov
)))
941 (setq novbuf
(nnfolder-open-nov group
))
942 (goto-char (point-min))
944 (push (read novbuf
) articles
)
946 (setq articles
(nreverse articles
))))
947 (goto-char (point-min))
949 ;; Anytime the active number is 1 or 0, it is suspect. In
950 ;; that case, search the file manually to find the active
951 ;; number. Or, of course, if we're being paranoid. (This
952 ;; would also be the place to build other lists from the
953 ;; header markers, such as expunge lists, etc., if we ever
954 ;; desired to abandon the active file entirely for mboxes.)
955 (when (or nnfolder-ignore-active-file
958 (while (and (search-forward marker nil t
)
960 (setq newnum
(string-to-number (match-string 0)))
961 (when (nnmail-within-headers-p)
962 (setq maxid
(max maxid newnum
)
963 minid
(min minid newnum
))
965 (if (memq newnum articles
)
966 (setq articles
(delq newnum articles
))
967 (let ((headers (nnfolder-parse-head newnum
)))
968 (with-current-buffer novbuf
969 (nnheader-find-nov-line newnum
)
970 (nnheader-insert-nov headers
)))))))
971 (when (and novbuf articles
)
972 (with-current-buffer novbuf
973 (dolist (article articles
)
974 (when (nnheader-find-nov-line article
)
975 (delete-region (point)
976 (progn (forward-line 1) (point)))))))
977 (setcar active
(max 1 (min minid maxid
)))
978 (setcdr active
(max maxid
(cdr active
)))
979 (goto-char (point-min)))
981 ;; As long as we trust that the user will only insert
982 ;; unmarked mail at the end, go to the end and search
983 ;; backwards for the last marker. Find the start of that
984 ;; message, and begin to search for unmarked messages from
986 (when (not (or nnfolder-distrust-mbox
988 (goto-char (point-max))
989 (unless (re-search-backward marker nil t
)
990 (goto-char (point-min)))
991 ;;(when (nnmail-search-unix-mail-delim)
992 ;; (goto-char (point-min)))
995 ;; Keep track of the active number on our own, and insert it
996 ;; back into the active list when we're done. Also, prime
997 ;; the pump to cut down on the number of searches we do.
998 (unless (nnmail-search-unix-mail-delim)
999 (goto-char (point-max)))
1000 (setq end
(point-marker))
1001 (while (not (= end
(point-max)))
1002 (setq start
(marker-position end
))
1004 ;; There may be more than one "From " line, so we skip past
1006 (while (looking-at delim
)
1008 (set-marker end
(if (nnmail-search-unix-mail-delim)
1012 (when (not (search-forward marker end t
))
1013 (narrow-to-region start end
)
1014 (nnmail-insert-lines)
1015 (nnfolder-insert-newsgroup-line
1018 (nnfolder-active-number group
))))
1020 (let ((headers (nnfolder-parse-head newnum
(point-min)
1022 (with-current-buffer novbuf
1023 (goto-char (point-max))
1024 (nnheader-insert-nov headers
))))
1027 (set-marker end nil
)
1028 ;; Make absolutely sure that the active list reflects
1030 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
1032 ;; Set the scantime for this group.
1033 (setq newscantime
(visited-file-modtime))
1035 (setcdr scantime
(list newscantime
))
1036 (push (list group newscantime
)
1037 nnfolder-scantime-alist
))
1040 (nnfolder-save-nov))
1041 (current-buffer))))))
1044 (defun nnfolder-generate-active-file ()
1045 "Look for mbox folders in the nnfolder directory and make them into groups.
1046 This command does not work if you use short group names."
1048 (nnmail-activate 'nnfolder
)
1049 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1050 (dolist (file (directory-files (or nnfolder-nov-directory
1054 (regexp-quote nnfolder-nov-file-suffix
)
1056 (when (not (message-mail-file-mbox-p file
))
1058 (delete-file file
)))))
1059 (dolist (file (directory-files nnfolder-directory
))
1060 (when (and (not (backup-file-name-p file
))
1061 (message-mail-file-mbox-p
1062 (nnheader-concat nnfolder-directory file
)))
1063 (let ((oldgroup (assoc file nnfolder-group-alist
)))
1065 (nnheader-message 5 "Refreshing group %s..." file
)
1066 (nnheader-message 5 "Adding group %s..." file
))
1068 (setq nnfolder-group-alist
1069 (delq oldgroup
(copy-sequence nnfolder-group-alist
))))
1070 (push (list file
(cons 1 0)) nnfolder-group-alist
)
1071 (nnfolder-possibly-change-folder file
)
1072 (nnfolder-possibly-change-group file
)
1073 (nnfolder-close-group file
))))
1074 (nnheader-message 5 ""))
1076 (defun nnfolder-group-pathname (group)
1077 "Make file name for GROUP."
1079 (mm-encode-coding-string group nnmail-pathname-coding-system
))
1080 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory
))))
1081 ;; If this file exists, we use it directly.
1082 (if (or nnmail-use-long-file-names
1083 (file-exists-p (concat dir group
)))
1085 ;; If not, we translate dots into slashes.
1086 (concat dir
(nnheader-replace-chars-in-string group ?. ?
/)))))
1088 (defun nnfolder-group-nov-pathname (group)
1089 "Make pathname for GROUP NOV."
1090 (let ((nnfolder-directory
1091 (or nnfolder-nov-directory nnfolder-directory
)))
1092 (concat (nnfolder-group-pathname group
) nnfolder-nov-file-suffix
)))
1094 (defun nnfolder-save-buffer ()
1096 (when (buffer-modified-p)
1097 (run-hooks 'nnfolder-save-buffer-hook
)
1098 (gnus-make-directory (file-name-directory (buffer-file-name)))
1099 (let ((coding-system-for-write
1100 (or nnfolder-file-coding-system-for-write
1101 nnfolder-file-coding-system
))
1102 (copyright-update nil
))
1104 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1105 (nnfolder-save-nov)))
1107 (defun nnfolder-save-active (group-alist active-file
)
1108 (let ((nnmail-active-file-coding-system
1109 (or nnfolder-active-file-coding-system-for-write
1110 nnfolder-active-file-coding-system
)))
1111 (nnmail-save-active group-alist active-file
)))
1113 (defun nnfolder-open-nov (group)
1114 (or (cdr (assoc group nnfolder-nov-buffer-alist
))
1115 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group
))))
1118 (set (make-local-variable 'nnfolder-nov-buffer-file-name
)
1119 (nnfolder-group-nov-pathname group
))
1121 (when (file-exists-p nnfolder-nov-buffer-file-name
)
1122 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name
)))
1123 (push (cons group buffer
) nnfolder-nov-buffer-alist
)
1126 (defun nnfolder-save-nov ()
1128 (while nnfolder-nov-buffer-alist
1129 (when (buffer-name (cdar nnfolder-nov-buffer-alist
))
1130 (set-buffer (cdar nnfolder-nov-buffer-alist
))
1131 (when (buffer-modified-p)
1132 (gnus-make-directory (file-name-directory
1133 nnfolder-nov-buffer-file-name
))
1134 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1136 (set-buffer-modified-p nil
)
1137 (kill-buffer (current-buffer)))
1138 (setq nnfolder-nov-buffer-alist
(cdr nnfolder-nov-buffer-alist
)))))
1140 (defun nnfolder-nov-delete-article (group article
)
1142 (set-buffer (nnfolder-open-nov group
))
1143 (when (nnheader-find-nov-line article
)
1144 (delete-region (point) (progn (forward-line 1) (point))))
1147 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old
)
1148 (if (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1150 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group
)))
1151 (when (file-exists-p nov
)
1153 (set-buffer nntp-server-buffer
)
1155 (nnheader-insert-file-contents nov
)
1157 (not (numberp fetch-old
)))
1158 t
; Don't remove anything.
1159 (nnheader-nov-delete-outside-range
1160 (if fetch-old
(max 1 (- (car articles
) fetch-old
))
1162 (car (last articles
)))
1165 (defun nnfolder-parse-head (&optional number b e
)
1166 "Parse the head of the current buffer."
1167 (let ((buf (current-buffer))
1171 (setq b
(if (nnmail-search-unix-mail-delim-backward)
1172 (point) (point-min)))
1174 (setq e
(if (nnmail-search-unix-mail-delim)
1175 (point) (point-max))))
1176 (setq chars
(- e b
))
1177 (unless (zerop chars
)
1179 (if (search-forward "\n\n" e t
) (setq e
(1- (point)))))
1181 (insert-buffer-substring buf b e
)
1182 (let ((headers (nnheader-parse-naked-head)))
1183 (mail-header-set-chars headers chars
)
1184 (mail-header-set-number headers number
)
1187 (defun nnfolder-add-nov (group article headers
)
1188 "Add a nov line for the GROUP base."
1190 (set-buffer (nnfolder-open-nov group
))
1191 (goto-char (point-max))
1192 (mail-header-set-number headers article
)
1193 (nnheader-insert-nov headers
)))
1195 (deffoo nnfolder-request-set-mark
(group actions
&optional server
)
1197 (not (nnfolder-server-opened server
)))
1198 (nnfolder-open-server server
))
1199 (unless nnfolder-marks-is-evil
1200 (nnfolder-open-marks group server
)
1201 (dolist (action actions
)
1202 (let ((range (nth 0 action
))
1203 (what (nth 1 action
))
1204 (marks (nth 2 action
)))
1205 (assert (or (eq what
'add
) (eq what
'del
)) nil
1206 "Unknown request-set-mark action: %s" what
)
1207 (dolist (mark marks
)
1208 (setq nnfolder-marks
(gnus-update-alist-soft
1210 (funcall (if (eq what
'add
) 'gnus-range-add
1211 'gnus-remove-from-range
)
1212 (cdr (assoc mark nnfolder-marks
)) range
)
1214 (nnfolder-save-marks group server
))
1217 (deffoo nnfolder-request-update-info
(group info
&optional server
)
1220 (not (nnfolder-server-opened server
)))
1221 (nnfolder-open-server server
))
1222 (when (and (not nnfolder-marks-is-evil
) (nnfolder-marks-changed-p group
))
1223 (nnheader-message 8 "Updating marks for %s..." group
)
1224 (nnfolder-open-marks group server
)
1225 ;; Update info using `nnfolder-marks'.
1226 (mapc (lambda (pred)
1227 (unless (memq (cdr pred
) gnus-article-unpropagated-mark-lists
)
1228 (gnus-info-set-marks
1230 (gnus-update-alist-soft
1232 (cdr (assq (cdr pred
) nnfolder-marks
))
1233 (gnus-info-marks info
))
1235 gnus-article-mark-lists
)
1236 (let ((seen (cdr (assq 'read nnfolder-marks
))))
1237 (gnus-info-set-read info
1238 (if (and (integerp (car seen
))
1240 (list (cons (car seen
) (car seen
)))
1242 (nnheader-message 8 "Updating marks for %s...done" group
))
1245 (defun nnfolder-group-marks-pathname (group)
1246 "Make pathname for GROUP NOV."
1247 (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory
)))
1248 (concat (nnfolder-group-pathname group
) nnfolder-marks-file-suffix
)))
1250 (defun nnfolder-marks-changed-p (group)
1251 (let ((file (nnfolder-group-marks-pathname group
)))
1252 (if (null (gnus-gethash file nnfolder-marks-modtime
))
1253 t
;; never looked at marks file, assume it has changed
1254 (not (equal (gnus-gethash file nnfolder-marks-modtime
)
1255 (nth 5 (file-attributes file
)))))))
1257 (defun nnfolder-save-marks (group server
)
1258 (let ((file-name-coding-system nnmail-pathname-coding-system
)
1259 (file (nnfolder-group-marks-pathname group
)))
1262 (with-temp-file file
1264 (gnus-prin1 nnfolder-marks
)
1267 (nth 5 (file-attributes file
))
1268 nnfolder-marks-modtime
))
1269 (error (or (gnus-yes-or-no-p
1270 (format "Could not write to %s (%s). Continue? " file err
))
1271 (error "Cannot write to %s (%s)" file err
))))))
1273 (defun nnfolder-open-marks (group server
)
1274 (let ((file (nnfolder-group-marks-pathname group
)))
1275 (if (file-exists-p file
)
1278 (gnus-sethash file
(nth 5 (file-attributes file
))
1279 nnfolder-marks-modtime
)
1280 (nnheader-insert-file-contents file
)
1281 (setq nnfolder-marks
(read (current-buffer)))
1282 (dolist (el gnus-article-unpropagated-mark-lists
)
1283 (setq nnfolder-marks
(gnus-remassoc el nnfolder-marks
))))
1284 (error (or (gnus-yes-or-no-p
1285 (format "Error reading nnfolder marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err
))
1286 (error "Cannot read nnfolder marks file %s (%s)" file err
))))
1287 ;; User didn't have a .marks file. Probably first time
1288 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1289 (let ((info (gnus-get-info
1290 (gnus-group-prefixed-name
1292 (gnus-server-to-method (format "nnfolder:%s" server
))))))
1293 (nnheader-message 7 "Bootstrapping marks for %s..." group
)
1294 (setq nnfolder-marks
(gnus-info-marks info
))
1295 (push (cons 'read
(gnus-info-read info
)) nnfolder-marks
)
1296 (dolist (el gnus-article-unpropagated-mark-lists
)
1297 (setq nnfolder-marks
(gnus-remassoc el nnfolder-marks
)))
1298 (nnfolder-save-marks group server
)
1299 (nnheader-message 7 "Bootstrapping marks for %s...done" group
)))))
1303 ;; arch-tag: a040d0f4-4f4e-445f-8972-839575c5f7e6
1304 ;;; nnfolder.el ends here