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 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, or (at your option)
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; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
38 (eval-when-compile (require 'cl
))
44 (autoload 'gnus-article-unpropagatable-p
"gnus-sum")
45 (autoload 'gnus-intersection
"gnus-range"))
47 (nnoo-declare nnfolder
)
49 (defvoo nnfolder-directory
(expand-file-name message-directory
)
50 "The name of the nnfolder directory.")
52 (defvoo nnfolder-nov-directory nil
53 "The name of the nnfolder NOV directory.
54 If nil, `nnfolder-directory' is used.")
56 (defvoo nnfolder-marks-directory nil
57 "The name of the nnfolder MARKS directory.
58 If nil, `nnfolder-directory' is used.")
60 (defvoo nnfolder-active-file
61 (nnheader-concat nnfolder-directory
"active")
62 "The name of the active file.")
64 ;; I renamed this variable to something more in keeping with the general GNU
67 (defvoo nnfolder-ignore-active-file nil
68 "If non-nil, the active file is ignored.
69 This causes nnfolder to do some extra work in order to determine the
70 true active ranges of an mbox file. Note that the active file is
71 still saved, but its values are not used. This costs some extra time
72 when scanning an mbox when opening it.")
74 (defvoo nnfolder-distrust-mbox nil
75 "If non-nil, the folder will be distrusted.
76 This means that nnfolder will not trust the user with respect to
77 inserting unaccounted for mail in the middle of an mbox file. This
78 can greatly slow down scans, which now must scan the entire file for
79 unmarked messages. When nil, scans occur forward from the last marked
80 message, a huge time saver for large mailboxes.")
82 (defvoo nnfolder-newsgroups-file
83 (concat (file-name-as-directory nnfolder-directory
) "newsgroups")
84 "Mail newsgroups description file.")
86 (defvoo nnfolder-get-new-mail t
87 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
89 (defvoo nnfolder-prepare-save-mail-hook nil
90 "Hook run narrowed to an article before saving.")
92 (defvoo nnfolder-save-buffer-hook nil
93 "Hook run before saving the nnfolder mbox buffer.")
96 (defvoo nnfolder-inhibit-expiry nil
97 "If non-nil, inhibit expiry.")
101 (defconst nnfolder-version
"nnfolder 2.0"
104 (defconst nnfolder-article-marker
"X-Gnus-Article-Number: "
105 "String used to demarcate what the article number for a message is.")
107 (defvoo nnfolder-current-group nil
)
108 (defvoo nnfolder-current-buffer nil
)
109 (defvoo nnfolder-status-string
"")
110 (defvoo nnfolder-group-alist nil
)
111 (defvoo nnfolder-buffer-alist nil
)
112 (defvoo nnfolder-scantime-alist nil
)
113 (defvoo nnfolder-active-timestamp nil
)
114 (defvoo nnfolder-active-file-coding-system mm-text-coding-system
)
115 (defvoo nnfolder-active-file-coding-system-for-write
116 nnmail-active-file-coding-system
)
117 (defvoo nnfolder-file-coding-system mm-text-coding-system
)
118 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
119 "Coding system for save nnfolder file.
120 if nil, `nnfolder-file-coding-system' is used.") ; FIXME: fill-in the doc-string of this variable
122 (defvoo nnfolder-nov-is-evil nil
123 "If non-nil, Gnus will never generate and use nov databases for mail groups.
124 Using nov databases will speed up header fetching considerably.
125 This variable shouldn't be flipped much. If you have, for some reason,
126 set this to t, and want to set it to nil again, you should always run
127 the `nnfolder-generate-active-file' command. The function will go
128 through all nnfolder directories and generate nov databases for them
129 all. This may very well take some time.")
131 (defvoo nnfolder-nov-file-suffix
".nov")
133 (defvoo nnfolder-nov-buffer-alist nil
)
135 (defvar nnfolder-nov-buffer-file-name nil
)
137 (defvoo nnfolder-marks-is-evil nil
138 "If non-nil, Gnus will never generate and use marks file for mail groups.
139 Using marks files makes it possible to backup and restore mail groups
140 separately from `.newsrc.eld'. If you have, for some reason, set
141 this to t, and want to set it to nil again, you should always remove
142 the corresponding marks file (usually base nnfolder file name
143 concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
144 the group. Then the marks file will be regenerated properly by Gnus.")
146 (defvoo nnfolder-marks nil
)
148 (defvoo nnfolder-marks-file-suffix
".mrk")
150 (defvar nnfolder-marks-modtime
(gnus-make-hashtable))
154 ;;; Interface functions
156 (nnoo-define-basics nnfolder
)
158 (deffoo nnfolder-retrieve-headers
(articles &optional group server fetch-old
)
160 (set-buffer nntp-server-buffer
)
162 (let (article start stop num
)
163 (nnfolder-possibly-change-group group server
)
164 (when nnfolder-current-buffer
165 (set-buffer nnfolder-current-buffer
)
166 (goto-char (point-min))
167 (if (stringp (car articles
))
169 (if (nnfolder-retrieve-headers-with-nov articles fetch-old
)
171 (setq articles
(gnus-sorted-intersection
172 ;; Is ARTICLES sorted?
174 (nnfolder-existing-articles)))
175 (while (setq article
(pop articles
))
176 (set-buffer nnfolder-current-buffer
)
177 (cond ((nnfolder-goto-article article
)
179 (setq stop
(if (search-forward "\n\n" nil t
)
182 (set-buffer nntp-server-buffer
)
183 (insert (format "221 %d Article retrieved.\n" article
))
184 (insert-buffer-substring nnfolder-current-buffer
186 (goto-char (point-max))
189 ;; If we couldn't find this article, skip over ranges
190 ;; of missing articles so we don't search the whole file
195 ;; Check that we are either at BOF or after an
196 ;; article with a lower number. We do this so we
197 ;; won't be confused by out-of-order article numbers,
198 ;; as caused by active file bogosity.
201 ((search-backward (concat "\n" nnfolder-article-marker
)
203 (goto-char (match-end 0))
204 (setq num
(string-to-number
206 (point) (point-at-eol))))
209 ;; Check that we are before an article with a
211 (search-forward (concat "\n" nnfolder-article-marker
)
214 (setq num
(string-to-number
216 (point) (point-at-eol))))
218 ;; Discard any article numbers before the one we're
221 (< (car articles
) num
))
222 (setq articles
(cdr articles
))))
224 (set-buffer nntp-server-buffer
)
225 (nnheader-fold-continuation-lines)
228 (deffoo nnfolder-open-server
(server &optional defs
)
229 (nnoo-change-server 'nnfolder server defs
)
230 (nnmail-activate 'nnfolder t
)
231 (gnus-make-directory nnfolder-directory
)
232 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
233 (and nnfolder-nov-directory
234 (gnus-make-directory nnfolder-nov-directory
)))
235 (unless nnfolder-marks-is-evil
236 (and nnfolder-marks-directory
237 (gnus-make-directory nnfolder-marks-directory
)))
239 ((not (file-exists-p nnfolder-directory
))
240 (nnfolder-close-server)
241 (nnheader-report 'nnfolder
"Couldn't create directory: %s"
243 ((not (file-directory-p (file-truename nnfolder-directory
)))
244 (nnfolder-close-server)
245 (nnheader-report 'nnfolder
"Not a directory: %s" nnfolder-directory
))
247 (nnmail-activate 'nnfolder
)
248 (nnheader-report 'nnfolder
"Opened server %s using directory %s"
249 server nnfolder-directory
)
252 (deffoo nnfolder-request-close
()
253 (let ((alist nnfolder-buffer-alist
))
255 (nnfolder-close-group (caar alist
) nil t
)
256 (setq alist
(cdr alist
))))
257 (nnoo-close-server 'nnfolder
)
258 (setq nnfolder-buffer-alist nil
259 nnfolder-group-alist nil
))
261 (deffoo nnfolder-request-article
(article &optional group server buffer
)
262 (nnfolder-possibly-change-group group server
)
264 (set-buffer nnfolder-current-buffer
)
265 (goto-char (point-min))
266 (when (nnfolder-goto-article article
)
270 (unless (and (nnmail-search-unix-mail-delim)
272 (goto-char (point-max)))
274 (let ((nntp-server-buffer (or buffer nntp-server-buffer
)))
275 (set-buffer nntp-server-buffer
)
277 (insert-buffer-substring nnfolder-current-buffer start stop
)
278 (goto-char (point-min))
279 (while (looking-at "From ")
281 (insert "X-From-Line: ")
283 (if (numberp article
)
284 (cons nnfolder-current-group article
)
285 (goto-char (point-min))
286 (cons nnfolder-current-group
287 (if (search-forward (concat "\n" nnfolder-article-marker
)
289 (string-to-number (buffer-substring
290 (point) (point-at-eol)))
293 (deffoo nnfolder-request-group
(group &optional server dont-check
)
294 (nnfolder-possibly-change-group group server t
)
296 (cond ((not (assoc group nnfolder-group-alist
))
297 (nnheader-report 'nnfolder
"No such group: %s" group
))
298 ((file-directory-p (nnfolder-group-pathname group
))
299 (nnheader-report 'nnfolder
"%s is a directory"
300 (file-name-as-directory
301 (let ((nnmail-pathname-coding-system nil
))
302 (nnfolder-group-pathname group
)))))
304 (nnheader-report 'nnfolder
"Selected group %s" group
)
307 (let* ((active (assoc group nnfolder-group-alist
))
309 (range (cadr active
)))
312 (nnheader-report 'nnfolder
"No such group: %s" group
))
313 ((null nnfolder-current-group
)
314 (nnheader-report 'nnfolder
"Empty group: %s" group
))
316 (nnheader-report 'nnfolder
"Selected group %s" group
)
317 (nnheader-insert "211 %d %d %d %s\n"
318 (1+ (- (cdr range
) (car range
)))
319 (car range
) (cdr range
) group
))))))))
321 (deffoo nnfolder-request-scan
(&optional group server
)
322 (nnfolder-possibly-change-group nil server
)
323 (when nnfolder-get-new-mail
324 (nnfolder-possibly-change-group group server
)
328 (let ((bufs nnfolder-buffer-alist
))
331 (if (not (gnus-buffer-live-p (nth 1 (car bufs
))))
332 (setq nnfolder-buffer-alist
333 (delq (car bufs
) nnfolder-buffer-alist
))
334 (set-buffer (nth 1 (car bufs
)))
335 (nnfolder-save-buffer)
336 (kill-buffer (current-buffer)))
337 (setq bufs
(cdr bufs
))))))
341 ;; Don't close the buffer if we're not shutting down the server. This way,
342 ;; we can keep the buffer in the group buffer cache, and not have to grovel
343 ;; over the buffer again unless we add new mail to it or modify it in some
346 (deffoo nnfolder-close-group
(group &optional server force
)
347 ;; Make sure we _had_ the group open.
348 (when (or (assoc group nnfolder-buffer-alist
)
349 (equal group nnfolder-current-group
))
350 (let ((inf (assoc group nnfolder-buffer-alist
)))
352 (when (and nnfolder-current-group
353 nnfolder-current-buffer
)
354 (push (list nnfolder-current-group nnfolder-current-buffer
)
355 nnfolder-buffer-alist
))
356 (setq nnfolder-buffer-alist
357 (delq inf nnfolder-buffer-alist
))
358 (setq nnfolder-current-buffer
(cadr inf
)
359 nnfolder-current-group
(car inf
))))
360 (when (and nnfolder-current-buffer
361 (buffer-name nnfolder-current-buffer
))
363 (set-buffer nnfolder-current-buffer
)
364 ;; If the buffer was modified, write the file out now.
365 (nnfolder-save-buffer)
366 ;; If we're shutting the server down, we need to kill the
367 ;; buffer and remove it from the open buffer list. Or, of
368 ;; course, if we're trying to minimize our space impact.
369 (kill-buffer (current-buffer))
370 (setq nnfolder-buffer-alist
(delq (assoc group nnfolder-buffer-alist
)
371 nnfolder-buffer-alist
)))))
372 (setq nnfolder-current-group nil
373 nnfolder-current-buffer nil
)
376 (deffoo nnfolder-request-create-group
(group &optional server args
)
377 (nnfolder-possibly-change-group nil server
)
378 (nnmail-activate 'nnfolder
)
379 (cond ((zerop (length group
))
380 (nnheader-report 'nnfolder
"Invalid (empty) group name"))
381 ((file-directory-p (nnfolder-group-pathname group
))
382 (nnheader-report 'nnfolder
"%s is a directory"
383 (file-name-as-directory
384 (let ((nnmail-pathname-coding-system nil
))
385 (nnfolder-group-pathname group
)))))
386 ((assoc group nnfolder-group-alist
)
389 (push (list group
(cons 1 0)) nnfolder-group-alist
)
390 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
392 (nnfolder-read-folder group
))
395 (deffoo nnfolder-request-list
(&optional server
)
396 (nnfolder-possibly-change-group nil server
)
398 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system
))
399 (nnmail-find-file nnfolder-active-file
)
400 (setq nnfolder-group-alist
(nnmail-get-active)))
403 (deffoo nnfolder-request-newgroups
(date &optional server
)
404 (nnfolder-possibly-change-group nil server
)
405 (nnfolder-request-list server
))
407 (deffoo nnfolder-request-list-newsgroups
(&optional server
)
408 (nnfolder-possibly-change-group nil server
)
410 (let ((nnmail-file-coding-system nnfolder-file-coding-system
))
411 (nnmail-find-file nnfolder-newsgroups-file
))))
413 ;; Return a list consisting of all article numbers existing in the
416 (defun nnfolder-existing-articles ()
418 (when nnfolder-current-buffer
419 (set-buffer nnfolder-current-buffer
)
420 (goto-char (point-min))
421 (let ((marker (concat "\n" nnfolder-article-marker
))
424 (while (and (search-forward marker nil t
)
425 (re-search-forward number nil t
))
426 (let ((newnum (string-to-number (match-string 0))))
427 (if (nnmail-within-headers-p)
428 (push newnum numbers
))))
429 ;; The article numbers are increasing, so this result is sorted.
430 (nreverse numbers
)))))
432 (deffoo nnfolder-request-expire-articles
(articles newsgroup
433 &optional server force
)
434 (nnfolder-possibly-change-group newsgroup server
)
436 ;; The articles we have deleted so far.
437 (deleted-articles nil
)
438 ;; The articles that really exist and will
439 ;; be expired if they are old enough.
441 (gnus-sorted-intersection articles
(nnfolder-existing-articles)))
443 (nnmail-activate 'nnfolder
)
446 (set-buffer nnfolder-current-buffer
)
447 ;; Since messages are sorted in arrival order and expired in the
448 ;; same order, we can stop as soon as we find a message that is
450 (while (and maybe-expirable is-old
)
451 (goto-char (point-min))
452 (when (and (nnfolder-goto-article (car maybe-expirable
))
453 (search-forward (concat "\n" nnfolder-article-marker
)
457 (nnmail-expired-article-p
460 (point) (progn (end-of-line) (point)))
461 force nnfolder-inhibit-expiry
))
462 (setq target nnmail-expiry-target
)
463 (unless (eq target
'delete
)
465 (nnfolder-request-article (car maybe-expirable
)
466 newsgroup server
(current-buffer))
467 (let ((nnfolder-current-directory nil
))
468 (when (functionp target
)
469 (setq target
(funcall target newsgroup
)))
471 (or (gnus-request-group target
)
472 (gnus-request-create-group target
)))
473 (nnmail-expiry-target-group target newsgroup
)
475 (nnfolder-possibly-change-group newsgroup server
))
477 (nnheader-message 5 "Deleting article %d in %s..."
478 (car maybe-expirable
) newsgroup
)
479 (nnfolder-delete-mail)
480 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
481 (nnfolder-nov-delete-article newsgroup
(car maybe-expirable
)))
482 ;; Must remember which articles were actually deleted
483 (push (car maybe-expirable
) deleted-articles
))))
484 (setq maybe-expirable
(cdr maybe-expirable
)))
485 (unless nnfolder-inhibit-expiry
486 (nnheader-message 5 "Deleting articles...done"))
487 (nnfolder-save-buffer)
488 (nnfolder-adjust-min-active newsgroup
)
489 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
490 (gnus-sorted-difference articles
(nreverse deleted-articles
)))))
492 (deffoo nnfolder-request-move-article
(article group server accept-form
493 &optional last move-is-internal
)
495 (let ((buf (get-buffer-create " *nnfolder move*"))
498 (nnfolder-request-article article group server
)
502 (insert-buffer-substring nntp-server-buffer
)
503 (goto-char (point-min))
504 (while (re-search-forward
505 (concat "^" nnfolder-article-marker
)
506 (save-excursion (and (search-forward "\n\n" nil t
) (point)))
509 (setq result
(eval accept-form
))
513 (nnfolder-possibly-change-group group server
)
514 (set-buffer nnfolder-current-buffer
)
515 (goto-char (point-min))
516 (when (nnfolder-goto-article article
)
517 (nnfolder-delete-mail))
518 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
519 (nnfolder-nov-delete-article group article
))
521 (nnfolder-save-buffer)
522 (nnfolder-adjust-min-active group
)
523 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))))
526 (deffoo nnfolder-request-accept-article
(group &optional server last
)
528 (nnfolder-possibly-change-group group server
)
529 (nnmail-check-syntax)
530 (let ((buf (current-buffer))
532 (goto-char (point-min))
533 (when (looking-at "X-From-Line: ")
534 (replace-match "From ")
535 (while (progn (forward-line) (looking-at "[ \t]"))
538 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system
)
539 (nntp-server-buffer (current-buffer)))
540 (nnmail-find-file nnfolder-active-file
)
541 (setq nnfolder-group-alist
(nnmail-parse-active))))
543 (goto-char (point-min))
544 (if (search-forward "\n\n" nil t
)
546 (goto-char (point-max)))
547 (while (re-search-backward (concat "^" nnfolder-article-marker
) nil t
)
548 (delete-region (point) (progn (forward-line 1) (point))))
549 (when nnmail-cache-accepted-message-ids
550 (nnmail-cache-insert (nnmail-fetch-field "message-id")
552 (nnmail-fetch-field "subject")
553 (nnmail-fetch-field "from")))
554 (setq result
(if (stringp group
)
555 (list (cons group
(nnfolder-active-number group
)))
557 (nnmail-article-group 'nnfolder-active-number
))))
558 (if (and (null result
)
559 (yes-or-no-p "Moved to `junk' group; delete article? "))
562 (car (nnfolder-save-mail result
)))))
565 (nnfolder-possibly-change-folder (or (caar art-group
) group
))
566 (nnfolder-save-buffer)
567 (when nnmail-cache-accepted-message-ids
568 (nnmail-cache-close))))
569 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
571 (nnheader-report 'nnfolder
"Couldn't store article"))
574 (deffoo nnfolder-request-replace-article
(article group buffer
)
575 (nnfolder-possibly-change-group group
)
578 (goto-char (point-min))
579 (if (not (looking-at "X-From-Line: "))
580 (insert "From nobody " (current-time-string) "\n")
581 (replace-match "From ")
583 (while (looking-at "[ \t]")
586 (nnfolder-normalize-buffer)
587 (set-buffer nnfolder-current-buffer
)
588 (goto-char (point-min))
589 (if (not (nnfolder-goto-article article
))
591 (nnfolder-delete-mail)
592 (insert-buffer-substring buffer
)
593 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
596 (let ((headers (nnfolder-parse-head article
597 (point-min) (point-max))))
598 (with-current-buffer (nnfolder-open-nov group
)
599 (if (nnheader-find-nov-line article
)
600 (delete-region (point) (progn (forward-line 1) (point))))
601 (nnheader-insert-nov headers
)))))
602 (nnfolder-save-buffer)
605 (deffoo nnfolder-request-delete-group
(group &optional force server
)
606 (nnfolder-close-group group server t
)
607 ;; Delete all articles in GROUP.
609 () ; Don't delete the articles.
610 ;; Delete the file that holds the group.
611 (let ((data (nnfolder-group-pathname group
))
612 (nov (nnfolder-group-nov-pathname group
))
613 (mrk (nnfolder-group-marks-pathname group
)))
614 (ignore-errors (delete-file data
))
615 (ignore-errors (delete-file nov
))
616 (ignore-errors (delete-file mrk
))))
617 ;; Remove the group from all structures.
618 (setq nnfolder-group-alist
619 (delq (assoc group nnfolder-group-alist
) nnfolder-group-alist
)
620 nnfolder-current-group nil
621 nnfolder-current-buffer nil
)
622 ;; Save the active file.
623 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
626 (deffoo nnfolder-request-rename-group
(group new-name
&optional server
)
627 (nnfolder-possibly-change-group group server
)
629 (set-buffer nnfolder-current-buffer
)
630 (and (file-writable-p buffer-file-name
)
632 (let ((new-file (nnfolder-group-pathname new-name
)))
633 (gnus-make-directory (file-name-directory new-file
))
634 (rename-file buffer-file-name new-file
)
635 (when (file-exists-p (nnfolder-group-nov-pathname group
))
636 (setq new-file
(nnfolder-group-nov-pathname new-name
))
637 (gnus-make-directory (file-name-directory new-file
))
638 (rename-file (nnfolder-group-nov-pathname group
) new-file
))
639 (when (file-exists-p (nnfolder-group-marks-pathname group
))
640 (setq new-file
(nnfolder-group-marks-pathname new-name
))
641 (gnus-make-directory (file-name-directory new-file
))
642 (rename-file (nnfolder-group-marks-pathname group
) new-file
)))
644 ;; That went ok, so we change the internal structures.
645 (let ((entry (assoc group nnfolder-group-alist
)))
646 (and entry
(setcar entry new-name
))
647 (setq nnfolder-current-buffer nil
648 nnfolder-current-group nil
)
649 ;; Save the new group alist.
650 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
651 ;; We kill the buffer instead of renaming it and stuff.
652 (kill-buffer (current-buffer))
655 (deffoo nnfolder-request-regenerate
(server)
656 (nnfolder-possibly-change-group nil server
)
657 (nnfolder-generate-active-file)
661 ;;; Internal functions.
663 (defun nnfolder-adjust-min-active (group)
664 ;; Find the lowest active article in this group.
665 (let* ((active (cadr (assoc group nnfolder-group-alist
)))
666 (marker (concat "\n" nnfolder-article-marker
))
668 (activemin (cdr active
)))
670 (set-buffer nnfolder-current-buffer
)
671 (goto-char (point-min))
672 (while (and (search-forward marker nil t
)
673 (re-search-forward number nil t
))
674 (let ((newnum (string-to-number (match-string 0))))
675 (if (nnmail-within-headers-p)
676 (setq activemin
(min activemin newnum
)))))
677 (setcar active activemin
))))
679 (defun nnfolder-article-string (article)
680 (if (numberp article
)
681 (concat "\n" nnfolder-article-marker
(int-to-string article
) " ")
682 (concat "\nMessage-ID: " article
)))
684 (defun nnfolder-goto-article (article)
685 "Place point at the start of the headers of ARTICLE.
686 ARTICLE can be an article number or a Message-ID.
687 Returns t if successful, nil otherwise."
688 (let ((art-string (nnfolder-article-string article
))
690 ;; It is likely that we are at or before the delimiter line.
691 ;; We therefore go to the end of the previous line, and start
692 ;; searching from there.
697 ;; First search forward.
698 (while (and (setq found
(search-forward art-string nil t
))
699 (not (nnmail-within-headers-p))))
700 ;; If unsuccessful, search backward from where we started,
703 (while (and (setq found
(search-backward art-string nil t
))
704 (not (nnmail-within-headers-p)))))
706 (nnmail-search-unix-mail-delim-backward))))
708 (defun nnfolder-delete-mail (&optional leave-delim
)
709 "Delete the message that point is in.
710 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
711 deleted. Point is left where the deleted region was."
715 ;; In case point is at the beginning of the message already.
717 (nnmail-search-unix-mail-delim-backward)
718 (if leave-delim
(progn (forward-line 1) (point))
722 (if (nnmail-search-unix-mail-delim)
725 (run-hooks 'nnfolder-delete-mail-hook
)
726 (delete-region (point-min) (point-max))))
728 (defun nnfolder-possibly-change-group (group &optional server dont-check
)
731 (not (nnfolder-server-opened server
)))
732 (nnfolder-open-server server
))
733 (unless (gnus-buffer-live-p nnfolder-current-buffer
)
734 (setq nnfolder-current-buffer nil
735 nnfolder-current-group nil
))
737 (let ((file-name-coding-system nnmail-pathname-coding-system
))
739 (not (equal group nnfolder-current-group
))
741 (nnmail-activate 'nnfolder
)
742 (and (assoc group nnfolder-group-alist
)
743 (file-exists-p (nnfolder-group-pathname group
)))))
745 (setq nnfolder-current-group group
746 nnfolder-current-buffer nil
)
748 ;; If we have to change groups, see if we don't already have
749 ;; the folder in memory. If we do, verify the modtime and
750 ;; destroy the folder if needed so we can rescan it.
751 (setq nnfolder-current-buffer
752 (nth 1 (assoc group nnfolder-buffer-alist
)))
754 ;; If the buffer is not live, make sure it isn't in the
755 ;; alist. If it is live, verify that nobody else has
756 ;; touched the file since last time.
757 (when (and nnfolder-current-buffer
758 (not (gnus-buffer-live-p nnfolder-current-buffer
)))
759 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)
760 nnfolder-current-buffer nil
))
762 (setq nnfolder-current-group group
)
764 (when (or (not nnfolder-current-buffer
)
765 (not (verify-visited-file-modtime
766 nnfolder-current-buffer
)))
768 (setq file
(nnfolder-group-pathname group
))
769 ;; See whether we need to create the new file.
770 (unless (file-exists-p file
)
771 (gnus-make-directory (file-name-directory file
))
772 (let ((nnmail-file-coding-system
773 (or nnfolder-file-coding-system-for-write
774 nnfolder-file-coding-system-for-write
)))
775 (nnmail-write-region (point-min) (point-min)
777 (when (setq nnfolder-current-buffer
(nnfolder-read-folder group
))
778 (set-buffer nnfolder-current-buffer
)
779 (push (list group nnfolder-current-buffer
)
780 nnfolder-buffer-alist
)))))))))
782 (defun nnfolder-save-mail (group-art-list)
783 "Called narrowed to an article."
784 (let* (save-list group-art
)
785 (goto-char (point-min))
786 ;; The From line may have been quoted by movemail.
787 (when (looking-at ">From")
789 ;; This might come from somewhere else.
790 (unless (looking-at "From ")
791 (insert "From nobody " (current-time-string) "\n")
792 (goto-char (point-min)))
793 ;; Quote all "From " lines in the article.
795 (let (case-fold-search)
796 (while (re-search-forward "^From " nil t
)
799 (setq save-list group-art-list
)
800 (nnmail-insert-lines)
801 (nnmail-insert-xref group-art-list
)
802 (run-hooks 'nnmail-prepare-save-mail-hook
)
803 (run-hooks 'nnfolder-prepare-save-mail-hook
)
805 ;; Insert the mail into each of the destination groups.
806 (while (setq group-art
(pop group-art-list
))
807 ;; Kill any previous newsgroup markers.
808 (goto-char (point-min))
809 (if (search-forward "\n\n" nil t
)
811 (goto-char (point-max)))
812 (while (search-backward (concat "\n" nnfolder-article-marker
) nil t
)
813 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
815 ;; Insert the new newsgroup marker.
816 (nnfolder-insert-newsgroup-line group-art
)
819 (let ((beg (point-min))
821 (obuf (current-buffer)))
822 (nnfolder-possibly-change-folder (car group-art
))
823 (let ((buffer-read-only nil
))
824 (nnfolder-normalize-buffer)
825 (insert-buffer-substring obuf beg end
))
826 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
828 (nnfolder-add-nov (car group-art
) (cdr group-art
)
829 (nnfolder-parse-head nil beg end
))))))
831 ;; Did we save it anywhere?
834 (defun nnfolder-normalize-buffer ()
835 "Make sure there are two newlines at the end of the buffer."
836 (goto-char (point-max))
837 (skip-chars-backward "\n")
838 (delete-region (point) (point-max))
842 (defun nnfolder-insert-newsgroup-line (group-art)
844 (goto-char (point-min))
845 (unless (search-forward "\n\n" nil t
)
846 (goto-char (point-max))
849 (insert (format (concat nnfolder-article-marker
"%d %s\n")
850 (cdr group-art
) (message-make-date)))))
852 (defun nnfolder-active-number (group)
853 ;; Find the next article number in GROUP.
854 (let ((active (cadr (assoc group nnfolder-group-alist
))))
856 (setcdr active
(1+ (cdr active
)))
857 ;; This group is new, so we create a new entry for it.
858 ;; This might be a bit naughty... creating groups on the drop of
859 ;; a hat, but I don't know...
860 (push (list group
(setq active
(cons 1 1)))
861 nnfolder-group-alist
))
864 (defun nnfolder-possibly-change-folder (group)
865 (let ((inf (assoc group nnfolder-buffer-alist
)))
867 (gnus-buffer-live-p (cadr inf
)))
868 (set-buffer (cadr inf
))
870 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)))
871 (when nnfolder-group-alist
872 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))
873 (push (list group
(nnfolder-read-folder group
))
874 nnfolder-buffer-alist
))))
876 ;; This method has a problem if you've accidentally let the active
877 ;; list get out of sync with the files. This could happen, say, if
878 ;; you've accidentally gotten new mail with something other than Gnus
879 ;; (but why would _that_ ever happen? :-). In that case, we will be
880 ;; in the middle of processing the file, ready to add new X-Gnus
881 ;; article number markers, and we'll run across a message with no ID
882 ;; yet - the active list _may_not_ be ready for us yet.
884 ;; To handle this, I'm modifying this routine to maintain the maximum
885 ;; ID seen so far, and when we hit a message with no ID, we will
886 ;; _manually_ scan the rest of the message looking for any more,
887 ;; possibly higher IDs. We'll assume the maximum that we find is the
888 ;; highest active. Note that this shouldn't cost us much extra time
889 ;; at all, but will be a lot less vulnerable to glitches between the
890 ;; mbox and the active file.
892 (defun nnfolder-read-folder (group)
893 (let* ((file (nnfolder-group-pathname group
))
894 (nov (nnfolder-group-nov-pathname group
))
896 (let ((nnheader-file-coding-system
897 nnfolder-file-coding-system
))
898 (nnheader-find-file-noselect file t
)))))
899 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
900 (buffer-disable-undo)
901 (if (equal (cadr (assoc group nnfolder-scantime-alist
))
902 (nth 5 (file-attributes file
)))
903 ;; This looks up-to-date, so we don't do any scanning.
904 (if (file-exists-p file
)
906 (push (list group buffer
) nnfolder-buffer-alist
)
907 (set-buffer-modified-p t
)
908 (nnfolder-save-buffer))
909 ;; Parse the damn thing.
911 (goto-char (point-min))
912 ;; Remove any blank lines at the start.
913 (while (eq (following-char) ?
\n)
915 (nnmail-activate 'nnfolder
)
917 (let ((delim "^From ")
918 (marker (concat "\n" nnfolder-article-marker
))
920 (active (or (cadr (assoc group nnfolder-group-alist
))
922 (scantime (assoc group nnfolder-scantime-alist
))
924 maxid start end newscantime
925 novbuf articles newnum
927 (setq maxid
(cdr active
))
929 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
930 (and (file-exists-p nov
)
931 (file-newer-than-file-p nov file
)))
932 (unless (file-exists-p nov
)
933 (gnus-make-directory (file-name-directory nov
)))
935 (setq novbuf
(nnfolder-open-nov group
))
936 (goto-char (point-min))
938 (push (read novbuf
) articles
)
940 (setq articles
(nreverse articles
))))
941 (goto-char (point-min))
943 ;; Anytime the active number is 1 or 0, it is suspect. In
944 ;; that case, search the file manually to find the active
945 ;; number. Or, of course, if we're being paranoid. (This
946 ;; would also be the place to build other lists from the
947 ;; header markers, such as expunge lists, etc., if we ever
948 ;; desired to abandon the active file entirely for mboxes.)
949 (when (or nnfolder-ignore-active-file
952 (while (and (search-forward marker nil t
)
954 (setq newnum
(string-to-number (match-string 0)))
955 (when (nnmail-within-headers-p)
956 (setq maxid
(max maxid newnum
)
957 minid
(min minid newnum
))
959 (if (memq newnum articles
)
960 (setq articles
(delq newnum articles
))
961 (let ((headers (nnfolder-parse-head newnum
)))
962 (with-current-buffer novbuf
963 (nnheader-find-nov-line newnum
)
964 (nnheader-insert-nov headers
)))))))
965 (when (and novbuf articles
)
966 (with-current-buffer novbuf
967 (dolist (article articles
)
968 (when (nnheader-find-nov-line article
)
969 (delete-region (point)
970 (progn (forward-line 1) (point)))))))
971 (setcar active
(max 1 (min minid maxid
)))
972 (setcdr active
(max maxid
(cdr active
)))
973 (goto-char (point-min)))
975 ;; As long as we trust that the user will only insert
976 ;; unmarked mail at the end, go to the end and search
977 ;; backwards for the last marker. Find the start of that
978 ;; message, and begin to search for unmarked messages from
980 (when (not (or nnfolder-distrust-mbox
982 (goto-char (point-max))
983 (unless (re-search-backward marker nil t
)
984 (goto-char (point-min)))
985 ;;(when (nnmail-search-unix-mail-delim)
986 ;; (goto-char (point-min)))
989 ;; Keep track of the active number on our own, and insert it
990 ;; back into the active list when we're done. Also, prime
991 ;; the pump to cut down on the number of searches we do.
992 (unless (nnmail-search-unix-mail-delim)
993 (goto-char (point-max)))
994 (setq end
(point-marker))
995 (while (not (= end
(point-max)))
996 (setq start
(marker-position end
))
998 ;; There may be more than one "From " line, so we skip past
1000 (while (looking-at delim
)
1002 (set-marker end
(if (nnmail-search-unix-mail-delim)
1006 (when (not (search-forward marker end t
))
1007 (narrow-to-region start end
)
1008 (nnmail-insert-lines)
1009 (nnfolder-insert-newsgroup-line
1012 (nnfolder-active-number group
))))
1014 (let ((headers (nnfolder-parse-head newnum
(point-min)
1016 (with-current-buffer novbuf
1017 (goto-char (point-max))
1018 (nnheader-insert-nov headers
))))
1021 (set-marker end nil
)
1022 ;; Make absolutely sure that the active list reflects
1024 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
1026 ;; Set the scantime for this group.
1027 (setq newscantime
(visited-file-modtime))
1029 (setcdr scantime
(list newscantime
))
1030 (push (list group newscantime
)
1031 nnfolder-scantime-alist
))
1034 (nnfolder-save-nov))
1035 (current-buffer))))))
1038 (defun nnfolder-generate-active-file ()
1039 "Look for mbox folders in the nnfolder directory and make them into groups.
1040 This command does not work if you use short group names."
1042 (nnmail-activate 'nnfolder
)
1043 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1044 (dolist (file (directory-files (or nnfolder-nov-directory
1048 (regexp-quote nnfolder-nov-file-suffix
)
1050 (when (not (message-mail-file-mbox-p file
))
1052 (delete-file file
)))))
1053 (dolist (file (directory-files nnfolder-directory
))
1054 (when (and (not (backup-file-name-p file
))
1055 (message-mail-file-mbox-p
1056 (nnheader-concat nnfolder-directory file
)))
1057 (let ((oldgroup (assoc file nnfolder-group-alist
)))
1059 (nnheader-message 5 "Refreshing group %s..." file
)
1060 (nnheader-message 5 "Adding group %s..." file
))
1062 (setq nnfolder-group-alist
1063 (delq oldgroup
(copy-sequence nnfolder-group-alist
))))
1064 (push (list file
(cons 1 0)) nnfolder-group-alist
)
1065 (nnfolder-possibly-change-folder file
)
1066 (nnfolder-possibly-change-group file
)
1067 (nnfolder-close-group file
))))
1068 (nnheader-message 5 ""))
1070 (defun nnfolder-group-pathname (group)
1071 "Make file name for GROUP."
1073 (mm-encode-coding-string group nnmail-pathname-coding-system
))
1074 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory
))))
1075 ;; If this file exists, we use it directly.
1076 (if (or nnmail-use-long-file-names
1077 (file-exists-p (concat dir group
)))
1079 ;; If not, we translate dots into slashes.
1080 (concat dir
(nnheader-replace-chars-in-string group ?. ?
/)))))
1082 (defun nnfolder-group-nov-pathname (group)
1083 "Make pathname for GROUP NOV."
1084 (let ((nnfolder-directory
1085 (or nnfolder-nov-directory nnfolder-directory
)))
1086 (concat (nnfolder-group-pathname group
) nnfolder-nov-file-suffix
)))
1088 (defun nnfolder-save-buffer ()
1090 (when (buffer-modified-p)
1091 (run-hooks 'nnfolder-save-buffer-hook
)
1092 (gnus-make-directory (file-name-directory (buffer-file-name)))
1093 (let ((coding-system-for-write
1094 (or nnfolder-file-coding-system-for-write
1095 nnfolder-file-coding-system
))
1096 (copyright-update nil
))
1098 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1099 (nnfolder-save-nov)))
1101 (defun nnfolder-save-active (group-alist active-file
)
1102 (let ((nnmail-active-file-coding-system
1103 (or nnfolder-active-file-coding-system-for-write
1104 nnfolder-active-file-coding-system
)))
1105 (nnmail-save-active group-alist active-file
)))
1107 (defun nnfolder-open-nov (group)
1108 (or (cdr (assoc group nnfolder-nov-buffer-alist
))
1109 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group
))))
1112 (set (make-local-variable 'nnfolder-nov-buffer-file-name
)
1113 (nnfolder-group-nov-pathname group
))
1115 (when (file-exists-p nnfolder-nov-buffer-file-name
)
1116 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name
)))
1117 (push (cons group buffer
) nnfolder-nov-buffer-alist
)
1120 (defun nnfolder-save-nov ()
1122 (while nnfolder-nov-buffer-alist
1123 (when (buffer-name (cdar nnfolder-nov-buffer-alist
))
1124 (set-buffer (cdar nnfolder-nov-buffer-alist
))
1125 (when (buffer-modified-p)
1126 (gnus-make-directory (file-name-directory
1127 nnfolder-nov-buffer-file-name
))
1128 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1130 (set-buffer-modified-p nil
)
1131 (kill-buffer (current-buffer)))
1132 (setq nnfolder-nov-buffer-alist
(cdr nnfolder-nov-buffer-alist
)))))
1134 (defun nnfolder-nov-delete-article (group article
)
1136 (set-buffer (nnfolder-open-nov group
))
1137 (when (nnheader-find-nov-line article
)
1138 (delete-region (point) (progn (forward-line 1) (point))))
1141 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old
)
1142 (if (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1144 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group
)))
1145 (when (file-exists-p nov
)
1147 (set-buffer nntp-server-buffer
)
1149 (nnheader-insert-file-contents nov
)
1151 (not (numberp fetch-old
)))
1152 t
; Don't remove anything.
1153 (nnheader-nov-delete-outside-range
1154 (if fetch-old
(max 1 (- (car articles
) fetch-old
))
1156 (car (last articles
)))
1159 (defun nnfolder-parse-head (&optional number b e
)
1160 "Parse the head of the current buffer."
1161 (let ((buf (current-buffer))
1165 (setq b
(if (nnmail-search-unix-mail-delim-backward)
1166 (point) (point-min)))
1168 (setq e
(if (nnmail-search-unix-mail-delim)
1169 (point) (point-max))))
1170 (setq chars
(- e b
))
1171 (unless (zerop chars
)
1173 (if (search-forward "\n\n" e t
) (setq e
(1- (point)))))
1175 (insert-buffer-substring buf b e
)
1176 (let ((headers (nnheader-parse-naked-head)))
1177 (mail-header-set-chars headers chars
)
1178 (mail-header-set-number headers number
)
1181 (defun nnfolder-add-nov (group article headers
)
1182 "Add a nov line for the GROUP base."
1184 (set-buffer (nnfolder-open-nov group
))
1185 (goto-char (point-max))
1186 (mail-header-set-number headers article
)
1187 (nnheader-insert-nov headers
)))
1189 (deffoo nnfolder-request-set-mark
(group actions
&optional server
)
1191 (not (nnfolder-server-opened server
)))
1192 (nnfolder-open-server server
))
1193 (unless nnfolder-marks-is-evil
1194 (nnfolder-open-marks group server
)
1195 (dolist (action actions
)
1196 (let ((range (nth 0 action
))
1197 (what (nth 1 action
))
1198 (marks (nth 2 action
)))
1199 (assert (or (eq what
'add
) (eq what
'del
)) nil
1200 "Unknown request-set-mark action: %s" what
)
1201 (dolist (mark marks
)
1202 (setq nnfolder-marks
(gnus-update-alist-soft
1204 (funcall (if (eq what
'add
) 'gnus-range-add
1205 'gnus-remove-from-range
)
1206 (cdr (assoc mark nnfolder-marks
)) range
)
1208 (nnfolder-save-marks group server
))
1211 (deffoo nnfolder-request-update-info
(group info
&optional server
)
1214 (not (nnfolder-server-opened server
)))
1215 (nnfolder-open-server server
))
1216 (when (and (not nnfolder-marks-is-evil
) (nnfolder-marks-changed-p group
))
1217 (nnheader-message 8 "Updating marks for %s..." group
)
1218 (nnfolder-open-marks group server
)
1219 ;; Update info using `nnfolder-marks'.
1220 (mapc (lambda (pred)
1221 (unless (memq (cdr pred
) gnus-article-unpropagated-mark-lists
)
1222 (gnus-info-set-marks
1224 (gnus-update-alist-soft
1226 (cdr (assq (cdr pred
) nnfolder-marks
))
1227 (gnus-info-marks info
))
1229 gnus-article-mark-lists
)
1230 (let ((seen (cdr (assq 'read nnfolder-marks
))))
1231 (gnus-info-set-read info
1232 (if (and (integerp (car seen
))
1234 (list (cons (car seen
) (car seen
)))
1236 (nnheader-message 8 "Updating marks for %s...done" group
))
1239 (defun nnfolder-group-marks-pathname (group)
1240 "Make pathname for GROUP NOV."
1241 (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory
)))
1242 (concat (nnfolder-group-pathname group
) nnfolder-marks-file-suffix
)))
1244 (defun nnfolder-marks-changed-p (group)
1245 (let ((file (nnfolder-group-marks-pathname group
)))
1246 (if (null (gnus-gethash file nnfolder-marks-modtime
))
1247 t
;; never looked at marks file, assume it has changed
1248 (not (equal (gnus-gethash file nnfolder-marks-modtime
)
1249 (nth 5 (file-attributes file
)))))))
1251 (defun nnfolder-save-marks (group server
)
1252 (let ((file-name-coding-system nnmail-pathname-coding-system
)
1253 (file (nnfolder-group-marks-pathname group
)))
1256 (with-temp-file file
1258 (gnus-prin1 nnfolder-marks
)
1261 (nth 5 (file-attributes file
))
1262 nnfolder-marks-modtime
))
1263 (error (or (gnus-yes-or-no-p
1264 (format "Could not write to %s (%s). Continue? " file err
))
1265 (error "Cannot write to %s (%s)" file err
))))))
1267 (defun nnfolder-open-marks (group server
)
1268 (let ((file (nnfolder-group-marks-pathname group
)))
1269 (if (file-exists-p file
)
1272 (gnus-sethash file
(nth 5 (file-attributes file
))
1273 nnfolder-marks-modtime
)
1274 (nnheader-insert-file-contents file
)
1275 (setq nnfolder-marks
(read (current-buffer)))
1276 (dolist (el gnus-article-unpropagated-mark-lists
)
1277 (setq nnfolder-marks
(gnus-remassoc el nnfolder-marks
))))
1278 (error (or (gnus-yes-or-no-p
1279 (format "Error reading nnfolder marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err
))
1280 (error "Cannot read nnfolder marks file %s (%s)" file err
))))
1281 ;; User didn't have a .marks file. Probably first time
1282 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1283 (let ((info (gnus-get-info
1284 (gnus-group-prefixed-name
1286 (gnus-server-to-method (format "nnfolder:%s" server
))))))
1287 (nnheader-message 7 "Bootstrapping marks for %s..." group
)
1288 (setq nnfolder-marks
(gnus-info-marks info
))
1289 (push (cons 'read
(gnus-info-read info
)) nnfolder-marks
)
1290 (dolist (el gnus-article-unpropagated-mark-lists
)
1291 (setq nnfolder-marks
(gnus-remassoc el nnfolder-marks
)))
1292 (nnfolder-save-marks group server
)
1293 (nnheader-message 7 "Bootstrapping marks for %s...done" group
)))))
1297 ;;; arch-tag: a040d0f4-4f4e-445f-8972-839575c5f7e6
1298 ;;; nnfolder.el ends here