1 ;;; nnfolder.el --- mail folder access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 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 2, 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) (gnus-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) (gnus-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) (gnus-point-at-eol)))
293 (deffoo nnfolder-request-group
(group &optional server dont-check
)
294 (nnfolder-possibly-change-group group server t
)
296 (if (not (assoc group nnfolder-group-alist
))
297 (nnheader-report 'nnfolder
"No such group: %s" group
)
300 (nnheader-report 'nnfolder
"Selected group %s" group
)
302 (let* ((active (assoc group nnfolder-group-alist
))
304 (range (cadr active
)))
307 (nnheader-report 'nnfolder
"No such group: %s" group
))
308 ((null nnfolder-current-group
)
309 (nnheader-report 'nnfolder
"Empty group: %s" group
))
311 (nnheader-report 'nnfolder
"Selected group %s" group
)
312 (nnheader-insert "211 %d %d %d %s\n"
313 (1+ (- (cdr range
) (car range
)))
314 (car range
) (cdr range
) group
))))))))
316 (deffoo nnfolder-request-scan
(&optional group server
)
317 (nnfolder-possibly-change-group nil server
)
318 (when nnfolder-get-new-mail
319 (nnfolder-possibly-change-group group server
)
323 (let ((bufs nnfolder-buffer-alist
))
326 (if (not (gnus-buffer-live-p (nth 1 (car bufs
))))
327 (setq nnfolder-buffer-alist
328 (delq (car bufs
) nnfolder-buffer-alist
))
329 (set-buffer (nth 1 (car bufs
)))
330 (nnfolder-save-buffer)
331 (kill-buffer (current-buffer)))
332 (setq bufs
(cdr bufs
))))))
336 ;; Don't close the buffer if we're not shutting down the server. This way,
337 ;; we can keep the buffer in the group buffer cache, and not have to grovel
338 ;; over the buffer again unless we add new mail to it or modify it in some
341 (deffoo nnfolder-close-group
(group &optional server force
)
342 ;; Make sure we _had_ the group open.
343 (when (or (assoc group nnfolder-buffer-alist
)
344 (equal group nnfolder-current-group
))
345 (let ((inf (assoc group nnfolder-buffer-alist
)))
347 (when (and nnfolder-current-group
348 nnfolder-current-buffer
)
349 (push (list nnfolder-current-group nnfolder-current-buffer
)
350 nnfolder-buffer-alist
))
351 (setq nnfolder-buffer-alist
352 (delq inf nnfolder-buffer-alist
))
353 (setq nnfolder-current-buffer
(cadr inf
)
354 nnfolder-current-group
(car inf
))))
355 (when (and nnfolder-current-buffer
356 (buffer-name nnfolder-current-buffer
))
358 (set-buffer nnfolder-current-buffer
)
359 ;; If the buffer was modified, write the file out now.
360 (nnfolder-save-buffer)
361 ;; If we're shutting the server down, we need to kill the
362 ;; buffer and remove it from the open buffer list. Or, of
363 ;; course, if we're trying to minimize our space impact.
364 (kill-buffer (current-buffer))
365 (setq nnfolder-buffer-alist
(delq (assoc group nnfolder-buffer-alist
)
366 nnfolder-buffer-alist
)))))
367 (setq nnfolder-current-group nil
368 nnfolder-current-buffer nil
)
371 (deffoo nnfolder-request-create-group
(group &optional server args
)
372 (nnfolder-possibly-change-group nil server
)
373 (nnmail-activate 'nnfolder
)
375 (not (assoc group nnfolder-group-alist
)))
376 (push (list group
(cons 1 0)) nnfolder-group-alist
)
377 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
379 (nnfolder-read-folder group
)))
382 (deffoo nnfolder-request-list
(&optional server
)
383 (nnfolder-possibly-change-group nil server
)
385 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system
))
386 (nnmail-find-file nnfolder-active-file
)
387 (setq nnfolder-group-alist
(nnmail-get-active)))
390 (deffoo nnfolder-request-newgroups
(date &optional server
)
391 (nnfolder-possibly-change-group nil server
)
392 (nnfolder-request-list server
))
394 (deffoo nnfolder-request-list-newsgroups
(&optional server
)
395 (nnfolder-possibly-change-group nil server
)
397 (let ((nnmail-file-coding-system nnfolder-file-coding-system
))
398 (nnmail-find-file nnfolder-newsgroups-file
))))
400 ;; Return a list consisting of all article numbers existing in the
403 (defun nnfolder-existing-articles ()
405 (when nnfolder-current-buffer
406 (set-buffer nnfolder-current-buffer
)
407 (goto-char (point-min))
408 (let ((marker (concat "\n" nnfolder-article-marker
))
411 (while (and (search-forward marker nil t
)
412 (re-search-forward number nil t
))
413 (let ((newnum (string-to-number (match-string 0))))
414 (if (nnmail-within-headers-p)
415 (push newnum numbers
))))
416 ;; The article numbers are increasing, so this result is sorted.
417 (nreverse numbers
)))))
419 (deffoo nnfolder-request-expire-articles
420 (articles newsgroup
&optional server force
)
421 (nnfolder-possibly-change-group newsgroup server
)
423 ;; The articles we have deleted so far.
424 (deleted-articles nil
)
425 ;; The articles that really exist and will
426 ;; be expired if they are old enough.
428 (gnus-sorted-intersection articles
(nnfolder-existing-articles))))
429 (nnmail-activate 'nnfolder
)
432 (set-buffer nnfolder-current-buffer
)
433 ;; Since messages are sorted in arrival order and expired in the
434 ;; same order, we can stop as soon as we find a message that is
436 (while (and maybe-expirable is-old
)
437 (goto-char (point-min))
438 (when (and (nnfolder-goto-article (car maybe-expirable
))
439 (search-forward (concat "\n" nnfolder-article-marker
)
443 (nnmail-expired-article-p
446 (point) (progn (end-of-line) (point)))
447 force nnfolder-inhibit-expiry
))
448 (unless (eq nnmail-expiry-target
'delete
)
450 (nnfolder-request-article (car maybe-expirable
)
451 newsgroup server
(current-buffer))
452 (let ((nnfolder-current-directory nil
))
453 (nnmail-expiry-target-group
454 nnmail-expiry-target newsgroup
)))
455 (nnfolder-possibly-change-group newsgroup server
))
456 (nnheader-message 5 "Deleting article %d in %s..."
457 (car maybe-expirable
) newsgroup
)
458 (nnfolder-delete-mail)
459 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
460 (nnfolder-nov-delete-article newsgroup
(car maybe-expirable
)))
461 ;; Must remember which articles were actually deleted
462 (push (car maybe-expirable
) deleted-articles
)))
463 (setq maybe-expirable
(cdr maybe-expirable
)))
464 (unless nnfolder-inhibit-expiry
465 (nnheader-message 5 "Deleting articles...done"))
466 (nnfolder-save-buffer)
467 (nnfolder-adjust-min-active newsgroup
)
468 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
469 (gnus-sorted-difference articles
(nreverse deleted-articles
)))))
471 (deffoo nnfolder-request-move-article
(article group server
472 accept-form
&optional last
)
474 (let ((buf (get-buffer-create " *nnfolder move*"))
477 (nnfolder-request-article article group server
)
481 (insert-buffer-substring nntp-server-buffer
)
482 (goto-char (point-min))
483 (while (re-search-forward
484 (concat "^" nnfolder-article-marker
)
485 (save-excursion (and (search-forward "\n\n" nil t
) (point)))
488 (setq result
(eval accept-form
))
492 (nnfolder-possibly-change-group group server
)
493 (set-buffer nnfolder-current-buffer
)
494 (goto-char (point-min))
495 (when (nnfolder-goto-article article
)
496 (nnfolder-delete-mail))
497 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
498 (nnfolder-nov-delete-article group article
))
500 (nnfolder-save-buffer)
501 (nnfolder-adjust-min-active group
)
502 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))))
505 (deffoo nnfolder-request-accept-article
(group &optional server last
)
507 (nnfolder-possibly-change-group group server
)
508 (nnmail-check-syntax)
509 (let ((buf (current-buffer))
511 (goto-char (point-min))
512 (when (looking-at "X-From-Line: ")
513 (replace-match "From ")
514 (while (progn (forward-line) (looking-at "[ \t]"))
517 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system
)
518 (nntp-server-buffer (current-buffer)))
519 (nnmail-find-file nnfolder-active-file
)
520 (setq nnfolder-group-alist
(nnmail-parse-active))))
522 (goto-char (point-min))
523 (if (search-forward "\n\n" nil t
)
525 (goto-char (point-max)))
526 (while (re-search-backward (concat "^" nnfolder-article-marker
) nil t
)
527 (delete-region (point) (progn (forward-line 1) (point))))
528 (when nnmail-cache-accepted-message-ids
529 (nnmail-cache-insert (nnmail-fetch-field "message-id")
531 (nnmail-fetch-field "subject")
532 (nnmail-fetch-field "from")))
533 (setq result
(if (stringp group
)
534 (list (cons group
(nnfolder-active-number group
)))
536 (nnmail-article-group 'nnfolder-active-number
))))
537 (if (and (null result
)
538 (yes-or-no-p "Moved to `junk' group; delete article? "))
541 (car (nnfolder-save-mail result
)))))
544 (nnfolder-possibly-change-folder (or (caar art-group
) group
))
545 (nnfolder-save-buffer)
546 (when nnmail-cache-accepted-message-ids
547 (nnmail-cache-close))))
548 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
550 (nnheader-report 'nnfolder
"Couldn't store article"))
553 (deffoo nnfolder-request-replace-article
(article group buffer
)
554 (nnfolder-possibly-change-group group
)
557 (goto-char (point-min))
558 (if (not (looking-at "X-From-Line: "))
559 (insert "From nobody " (current-time-string) "\n")
560 (replace-match "From ")
562 (while (looking-at "[ \t]")
565 (nnfolder-normalize-buffer)
566 (set-buffer nnfolder-current-buffer
)
567 (goto-char (point-min))
568 (if (not (nnfolder-goto-article article
))
570 (nnfolder-delete-mail)
571 (insert-buffer-substring buffer
)
572 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
575 (let ((headers (nnfolder-parse-head article
576 (point-min) (point-max))))
577 (with-current-buffer (nnfolder-open-nov group
)
578 (if (nnheader-find-nov-line article
)
579 (delete-region (point) (progn (forward-line 1) (point))))
580 (nnheader-insert-nov headers
)))))
581 (nnfolder-save-buffer)
584 (deffoo nnfolder-request-delete-group
(group &optional force server
)
585 (nnfolder-close-group group server t
)
586 ;; Delete all articles in GROUP.
588 () ; Don't delete the articles.
589 ;; Delete the file that holds the group.
590 (let ((data (nnfolder-group-pathname group
))
591 (nov (nnfolder-group-nov-pathname group
))
592 (mrk (nnfolder-group-marks-pathname group
)))
593 (ignore-errors (delete-file data
))
594 (ignore-errors (delete-file nov
))
595 (ignore-errors (delete-file mrk
))))
596 ;; Remove the group from all structures.
597 (setq nnfolder-group-alist
598 (delq (assoc group nnfolder-group-alist
) nnfolder-group-alist
)
599 nnfolder-current-group nil
600 nnfolder-current-buffer nil
)
601 ;; Save the active file.
602 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
605 (deffoo nnfolder-request-rename-group
(group new-name
&optional server
)
606 (nnfolder-possibly-change-group group server
)
608 (set-buffer nnfolder-current-buffer
)
609 (and (file-writable-p buffer-file-name
)
611 (let ((new-file (nnfolder-group-pathname new-name
)))
612 (gnus-make-directory (file-name-directory new-file
))
613 (rename-file buffer-file-name new-file
)
614 (when (file-exists-p (nnfolder-group-nov-pathname group
))
615 (setq new-file
(nnfolder-group-nov-pathname new-name
))
616 (gnus-make-directory (file-name-directory new-file
))
617 (rename-file (nnfolder-group-nov-pathname group
) new-file
))
618 (when (file-exists-p (nnfolder-group-marks-pathname group
))
619 (setq new-file
(nnfolder-group-marks-pathname new-name
))
620 (gnus-make-directory (file-name-directory new-file
))
621 (rename-file (nnfolder-group-marks-pathname group
) new-file
)))
623 ;; That went ok, so we change the internal structures.
624 (let ((entry (assoc group nnfolder-group-alist
)))
625 (and entry
(setcar entry new-name
))
626 (setq nnfolder-current-buffer nil
627 nnfolder-current-group nil
)
628 ;; Save the new group alist.
629 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
630 ;; We kill the buffer instead of renaming it and stuff.
631 (kill-buffer (current-buffer))
634 (deffoo nnfolder-request-regenerate
(server)
635 (nnfolder-possibly-change-group nil server
)
636 (nnfolder-generate-active-file)
640 ;;; Internal functions.
642 (defun nnfolder-adjust-min-active (group)
643 ;; Find the lowest active article in this group.
644 (let* ((active (cadr (assoc group nnfolder-group-alist
)))
645 (marker (concat "\n" nnfolder-article-marker
))
647 (activemin (cdr active
)))
649 (set-buffer nnfolder-current-buffer
)
650 (goto-char (point-min))
651 (while (and (search-forward marker nil t
)
652 (re-search-forward number nil t
))
653 (let ((newnum (string-to-number (match-string 0))))
654 (if (nnmail-within-headers-p)
655 (setq activemin
(min activemin newnum
)))))
656 (setcar active activemin
))))
658 (defun nnfolder-article-string (article)
659 (if (numberp article
)
660 (concat "\n" nnfolder-article-marker
(int-to-string article
) " ")
661 (concat "\nMessage-ID: " article
)))
663 (defun nnfolder-goto-article (article)
664 "Place point at the start of the headers of ARTICLE.
665 ARTICLE can be an article number or a Message-ID.
666 Returns t if successful, nil otherwise."
667 (let ((art-string (nnfolder-article-string article
))
669 ;; It is likely that we are at or before the delimiter line.
670 ;; We therefore go to the end of the previous line, and start
671 ;; searching from there.
676 ;; First search forward.
677 (while (and (setq found
(search-forward art-string nil t
))
678 (not (nnmail-within-headers-p))))
679 ;; If unsuccessful, search backward from where we started,
682 (while (and (setq found
(search-backward art-string nil t
))
683 (not (nnmail-within-headers-p)))))
685 (nnmail-search-unix-mail-delim-backward))))
687 (defun nnfolder-delete-mail (&optional leave-delim
)
688 "Delete the message that point is in.
689 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
690 deleted. Point is left where the deleted region was."
694 ;; In case point is at the beginning of the message already.
696 (nnmail-search-unix-mail-delim-backward)
697 (if leave-delim
(progn (forward-line 1) (point))
701 (if (nnmail-search-unix-mail-delim)
704 (run-hooks 'nnfolder-delete-mail-hook
)
705 (delete-region (point-min) (point-max))))
707 (defun nnfolder-possibly-change-group (group &optional server dont-check
)
710 (not (nnfolder-server-opened server
)))
711 (nnfolder-open-server server
))
712 (unless (gnus-buffer-live-p nnfolder-current-buffer
)
713 (setq nnfolder-current-buffer nil
714 nnfolder-current-group nil
))
716 (let ((file-name-coding-system nnmail-pathname-coding-system
))
718 (not (equal group nnfolder-current-group
))
720 (nnmail-activate 'nnfolder
)
721 (and (assoc group nnfolder-group-alist
)
722 (file-exists-p (nnfolder-group-pathname group
)))))
724 (setq nnfolder-current-group group
725 nnfolder-current-buffer nil
)
727 ;; If we have to change groups, see if we don't already have
728 ;; the folder in memory. If we do, verify the modtime and
729 ;; destroy the folder if needed so we can rescan it.
730 (setq nnfolder-current-buffer
731 (nth 1 (assoc group nnfolder-buffer-alist
)))
733 ;; If the buffer is not live, make sure it isn't in the
734 ;; alist. If it is live, verify that nobody else has
735 ;; touched the file since last time.
736 (when (and nnfolder-current-buffer
737 (not (gnus-buffer-live-p nnfolder-current-buffer
)))
738 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)
739 nnfolder-current-buffer nil
))
741 (setq nnfolder-current-group group
)
743 (when (or (not nnfolder-current-buffer
)
744 (not (verify-visited-file-modtime
745 nnfolder-current-buffer
)))
747 (setq file
(nnfolder-group-pathname group
))
748 ;; See whether we need to create the new file.
749 (unless (file-exists-p file
)
750 (gnus-make-directory (file-name-directory file
))
751 (let ((nnmail-file-coding-system
752 (or nnfolder-file-coding-system-for-write
753 nnfolder-file-coding-system-for-write
)))
754 (nnmail-write-region (point-min) (point-min)
756 (when (setq nnfolder-current-buffer
(nnfolder-read-folder group
))
757 (set-buffer nnfolder-current-buffer
)
758 (push (list group nnfolder-current-buffer
)
759 nnfolder-buffer-alist
)))))))))
761 (defun nnfolder-save-mail (group-art-list)
762 "Called narrowed to an article."
763 (let* (save-list group-art
)
764 (goto-char (point-min))
765 ;; The From line may have been quoted by movemail.
766 (when (looking-at ">From")
768 ;; This might come from somewhere else.
769 (unless (looking-at "From ")
770 (insert "From nobody " (current-time-string) "\n")
771 (goto-char (point-min)))
772 ;; Quote all "From " lines in the article.
774 (let (case-fold-search)
775 (while (re-search-forward "^From " nil t
)
778 (setq save-list group-art-list
)
779 (nnmail-insert-lines)
780 (nnmail-insert-xref group-art-list
)
781 (run-hooks 'nnmail-prepare-save-mail-hook
)
782 (run-hooks 'nnfolder-prepare-save-mail-hook
)
784 ;; Insert the mail into each of the destination groups.
785 (while (setq group-art
(pop group-art-list
))
786 ;; Kill any previous newsgroup markers.
787 (goto-char (point-min))
788 (if (search-forward "\n\n" nil t
)
790 (goto-char (point-max)))
791 (while (search-backward (concat "\n" nnfolder-article-marker
) nil t
)
792 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
794 ;; Insert the new newsgroup marker.
795 (nnfolder-insert-newsgroup-line group-art
)
798 (let ((beg (point-min))
800 (obuf (current-buffer)))
801 (nnfolder-possibly-change-folder (car group-art
))
802 (let ((buffer-read-only nil
))
803 (nnfolder-normalize-buffer)
804 (insert-buffer-substring obuf beg end
))
805 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
807 (nnfolder-add-nov (car group-art
) (cdr group-art
)
808 (nnfolder-parse-head nil beg end
))))))
810 ;; Did we save it anywhere?
813 (defun nnfolder-normalize-buffer ()
814 "Make sure there are two newlines at the end of the buffer."
815 (goto-char (point-max))
816 (skip-chars-backward "\n")
817 (delete-region (point) (point-max))
821 (defun nnfolder-insert-newsgroup-line (group-art)
823 (goto-char (point-min))
824 (unless (search-forward "\n\n" nil t
)
825 (goto-char (point-max))
828 (insert (format (concat nnfolder-article-marker
"%d %s\n")
829 (cdr group-art
) (current-time-string)))))
831 (defun nnfolder-active-number (group)
832 ;; Find the next article number in GROUP.
833 (let ((active (cadr (assoc group nnfolder-group-alist
))))
835 (setcdr active
(1+ (cdr active
)))
836 ;; This group is new, so we create a new entry for it.
837 ;; This might be a bit naughty... creating groups on the drop of
838 ;; a hat, but I don't know...
839 (push (list group
(setq active
(cons 1 1)))
840 nnfolder-group-alist
))
843 (defun nnfolder-possibly-change-folder (group)
844 (let ((inf (assoc group nnfolder-buffer-alist
)))
846 (gnus-buffer-live-p (cadr inf
)))
847 (set-buffer (cadr inf
))
849 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)))
850 (when nnfolder-group-alist
851 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))
852 (push (list group
(nnfolder-read-folder group
))
853 nnfolder-buffer-alist
))))
855 ;; This method has a problem if you've accidentally let the active
856 ;; list get out of sync with the files. This could happen, say, if
857 ;; you've accidentally gotten new mail with something other than Gnus
858 ;; (but why would _that_ ever happen? :-). In that case, we will be
859 ;; in the middle of processing the file, ready to add new X-Gnus
860 ;; article number markers, and we'll run across a message with no ID
861 ;; yet - the active list _may_not_ be ready for us yet.
863 ;; To handle this, I'm modifying this routine to maintain the maximum
864 ;; ID seen so far, and when we hit a message with no ID, we will
865 ;; _manually_ scan the rest of the message looking for any more,
866 ;; possibly higher IDs. We'll assume the maximum that we find is the
867 ;; highest active. Note that this shouldn't cost us much extra time
868 ;; at all, but will be a lot less vulnerable to glitches between the
869 ;; mbox and the active file.
871 (defun nnfolder-read-folder (group)
872 (let* ((file (nnfolder-group-pathname group
))
873 (nov (nnfolder-group-nov-pathname group
))
875 (let ((nnheader-file-coding-system
876 nnfolder-file-coding-system
))
877 (nnheader-find-file-noselect file t
)))))
878 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
879 (buffer-disable-undo)
880 (if (equal (cadr (assoc group nnfolder-scantime-alist
))
881 (nth 5 (file-attributes file
)))
882 ;; This looks up-to-date, so we don't do any scanning.
883 (if (file-exists-p file
)
885 (push (list group buffer
) nnfolder-buffer-alist
)
886 (set-buffer-modified-p t
)
887 (nnfolder-save-buffer))
888 ;; Parse the damn thing.
890 (goto-char (point-min))
891 ;; Remove any blank lines at the start.
892 (while (eq (following-char) ?
\n)
894 (nnmail-activate 'nnfolder
)
896 (let ((delim "^From ")
897 (marker (concat "\n" nnfolder-article-marker
))
899 (active (or (cadr (assoc group nnfolder-group-alist
))
901 (scantime (assoc group nnfolder-scantime-alist
))
903 maxid start end newscantime
904 novbuf articles newnum
906 (setq maxid
(cdr active
))
908 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
909 (and (file-exists-p nov
)
910 (file-newer-than-file-p nov file
)))
911 (unless (file-exists-p nov
)
912 (gnus-make-directory (file-name-directory nov
)))
914 (setq novbuf
(nnfolder-open-nov group
))
915 (goto-char (point-min))
917 (push (read novbuf
) articles
)
919 (setq articles
(nreverse articles
))))
920 (goto-char (point-min))
922 ;; Anytime the active number is 1 or 0, it is suspect. In
923 ;; that case, search the file manually to find the active
924 ;; number. Or, of course, if we're being paranoid. (This
925 ;; would also be the place to build other lists from the
926 ;; header markers, such as expunge lists, etc., if we ever
927 ;; desired to abandon the active file entirely for mboxes.)
928 (when (or nnfolder-ignore-active-file
931 (while (and (search-forward marker nil t
)
933 (setq newnum
(string-to-number (match-string 0)))
934 (when (nnmail-within-headers-p)
935 (setq maxid
(max maxid newnum
)
936 minid
(min minid newnum
))
938 (if (memq newnum articles
)
939 (setq articles
(delq newnum articles
))
940 (let ((headers (nnfolder-parse-head newnum
)))
941 (with-current-buffer novbuf
942 (nnheader-find-nov-line newnum
)
943 (nnheader-insert-nov headers
)))))))
944 (when (and novbuf articles
)
945 (with-current-buffer novbuf
946 (dolist (article articles
)
947 (when (nnheader-find-nov-line article
)
948 (delete-region (point)
949 (progn (forward-line 1) (point)))))))
950 (setcar active
(max 1 (min minid maxid
)))
951 (setcdr active
(max maxid
(cdr active
)))
952 (goto-char (point-min)))
954 ;; As long as we trust that the user will only insert
955 ;; unmarked mail at the end, go to the end and search
956 ;; backwards for the last marker. Find the start of that
957 ;; message, and begin to search for unmarked messages from
959 (when (not (or nnfolder-distrust-mbox
961 (goto-char (point-max))
962 (unless (re-search-backward marker nil t
)
963 (goto-char (point-min)))
964 ;;(when (nnmail-search-unix-mail-delim)
965 ;; (goto-char (point-min)))
968 ;; Keep track of the active number on our own, and insert it
969 ;; back into the active list when we're done. Also, prime
970 ;; the pump to cut down on the number of searches we do.
971 (unless (nnmail-search-unix-mail-delim)
972 (goto-char (point-max)))
973 (setq end
(point-marker))
974 (while (not (= end
(point-max)))
975 (setq start
(marker-position end
))
977 ;; There may be more than one "From " line, so we skip past
979 (while (looking-at delim
)
981 (set-marker end
(if (nnmail-search-unix-mail-delim)
985 (when (not (search-forward marker end t
))
986 (narrow-to-region start end
)
987 (nnmail-insert-lines)
988 (nnfolder-insert-newsgroup-line
991 (nnfolder-active-number group
))))
993 (let ((headers (nnfolder-parse-head newnum
(point-min)
995 (with-current-buffer novbuf
996 (goto-char (point-max))
997 (nnheader-insert-nov headers
))))
1000 (set-marker end nil
)
1001 ;; Make absolutely sure that the active list reflects
1003 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
1005 ;; Set the scantime for this group.
1006 (setq newscantime
(visited-file-modtime))
1008 (setcdr scantime
(list newscantime
))
1009 (push (list group newscantime
)
1010 nnfolder-scantime-alist
))
1013 (nnfolder-save-nov))
1014 (current-buffer))))))
1017 (defun nnfolder-generate-active-file ()
1018 "Look for mbox folders in the nnfolder directory and make them into groups.
1019 This command does not work if you use short group names."
1021 (nnmail-activate 'nnfolder
)
1022 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1023 (dolist (file (directory-files (or nnfolder-nov-directory
1027 (regexp-quote nnfolder-nov-file-suffix
)
1029 (when (not (message-mail-file-mbox-p file
))
1031 (delete-file file
)))))
1032 (let ((files (directory-files nnfolder-directory
))
1034 (while (setq file
(pop files
))
1035 (when (and (not (backup-file-name-p file
))
1036 (message-mail-file-mbox-p
1037 (nnheader-concat nnfolder-directory file
)))
1038 (let ((oldgroup (assoc file nnfolder-group-alist
)))
1040 (nnheader-message 5 "Refreshing group %s..." file
)
1041 (nnheader-message 5 "Adding group %s..." file
))
1043 (setq nnfolder-group-alist
1044 (delq oldgroup
(copy-sequence nnfolder-group-alist
))))
1045 (push (list file
(cons 1 0)) nnfolder-group-alist
)
1046 (nnfolder-possibly-change-folder file
)
1047 (nnfolder-possibly-change-group file
)
1048 (nnfolder-close-group file
))))
1049 (nnheader-message 5 "")))
1051 (defun nnfolder-group-pathname (group)
1052 "Make file name for GROUP."
1054 (mm-encode-coding-string group nnmail-pathname-coding-system
))
1055 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory
))))
1056 ;; If this file exists, we use it directly.
1057 (if (or nnmail-use-long-file-names
1058 (file-exists-p (concat dir group
)))
1060 ;; If not, we translate dots into slashes.
1061 (concat dir
(nnheader-replace-chars-in-string group ?. ?
/)))))
1063 (defun nnfolder-group-nov-pathname (group)
1064 "Make pathname for GROUP NOV."
1065 (let ((nnfolder-directory
1066 (or nnfolder-nov-directory nnfolder-directory
)))
1067 (concat (nnfolder-group-pathname group
) nnfolder-nov-file-suffix
)))
1069 (defun nnfolder-save-buffer ()
1071 (when (buffer-modified-p)
1072 (run-hooks 'nnfolder-save-buffer-hook
)
1073 (gnus-make-directory (file-name-directory (buffer-file-name)))
1074 (let ((coding-system-for-write
1075 (or nnfolder-file-coding-system-for-write
1076 nnfolder-file-coding-system
)))
1078 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1079 (nnfolder-save-nov)))
1081 (defun nnfolder-save-active (group-alist active-file
)
1082 (let ((nnmail-active-file-coding-system
1083 (or nnfolder-active-file-coding-system-for-write
1084 nnfolder-active-file-coding-system
)))
1085 (nnmail-save-active group-alist active-file
)))
1087 (defun nnfolder-open-nov (group)
1088 (or (cdr (assoc group nnfolder-nov-buffer-alist
))
1089 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group
))))
1092 (set (make-local-variable 'nnfolder-nov-buffer-file-name
)
1093 (nnfolder-group-nov-pathname group
))
1095 (when (file-exists-p nnfolder-nov-buffer-file-name
)
1096 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name
)))
1097 (push (cons group buffer
) nnfolder-nov-buffer-alist
)
1100 (defun nnfolder-save-nov ()
1102 (while nnfolder-nov-buffer-alist
1103 (when (buffer-name (cdar nnfolder-nov-buffer-alist
))
1104 (set-buffer (cdar nnfolder-nov-buffer-alist
))
1105 (when (buffer-modified-p)
1106 (gnus-make-directory (file-name-directory
1107 nnfolder-nov-buffer-file-name
))
1108 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1110 (set-buffer-modified-p nil
)
1111 (kill-buffer (current-buffer)))
1112 (setq nnfolder-nov-buffer-alist
(cdr nnfolder-nov-buffer-alist
)))))
1114 (defun nnfolder-nov-delete-article (group article
)
1116 (set-buffer (nnfolder-open-nov group
))
1117 (when (nnheader-find-nov-line article
)
1118 (delete-region (point) (progn (forward-line 1) (point))))
1121 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old
)
1122 (if (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1124 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group
)))
1125 (when (file-exists-p nov
)
1127 (set-buffer nntp-server-buffer
)
1129 (nnheader-insert-file-contents nov
)
1131 (not (numberp fetch-old
)))
1132 t
; Don't remove anything.
1133 (nnheader-nov-delete-outside-range
1134 (if fetch-old
(max 1 (- (car articles
) fetch-old
))
1136 (car (last articles
)))
1139 (defun nnfolder-parse-head (&optional number b e
)
1140 "Parse the head of the current buffer."
1141 (let ((buf (current-buffer))
1145 (setq b
(if (nnmail-search-unix-mail-delim-backward)
1146 (point) (point-min)))
1148 (setq e
(if (nnmail-search-unix-mail-delim)
1149 (point) (point-max))))
1150 (setq chars
(- e b
))
1151 (unless (zerop chars
)
1153 (if (search-forward "\n\n" e t
) (setq e
(1- (point)))))
1155 (insert-buffer-substring buf b e
)
1156 (let ((headers (nnheader-parse-naked-head)))
1157 (mail-header-set-chars headers chars
)
1158 (mail-header-set-number headers number
)
1161 (defun nnfolder-add-nov (group article headers
)
1162 "Add a nov line for the GROUP base."
1164 (set-buffer (nnfolder-open-nov group
))
1165 (goto-char (point-max))
1166 (mail-header-set-number headers article
)
1167 (nnheader-insert-nov headers
)))
1169 (deffoo nnfolder-request-set-mark
(group actions
&optional server
)
1171 (not (nnfolder-server-opened server
)))
1172 (nnfolder-open-server server
))
1173 (unless nnfolder-marks-is-evil
1174 (nnfolder-open-marks group server
)
1175 (dolist (action actions
)
1176 (let ((range (nth 0 action
))
1177 (what (nth 1 action
))
1178 (marks (nth 2 action
)))
1179 (assert (or (eq what
'add
) (eq what
'del
)) nil
1180 "Unknown request-set-mark action: %s" what
)
1181 (dolist (mark marks
)
1182 (setq nnfolder-marks
(gnus-update-alist-soft
1184 (funcall (if (eq what
'add
) 'gnus-range-add
1185 'gnus-remove-from-range
)
1186 (cdr (assoc mark nnfolder-marks
)) range
)
1188 (nnfolder-save-marks group server
))
1191 (deffoo nnfolder-request-update-info
(group info
&optional server
)
1194 (not (nnfolder-server-opened server
)))
1195 (nnfolder-open-server server
))
1196 (when (and (not nnfolder-marks-is-evil
) (nnfolder-marks-changed-p group
))
1197 (nnheader-message 8 "Updating marks for %s..." group
)
1198 (nnfolder-open-marks group server
)
1199 ;; Update info using `nnfolder-marks'.
1200 (mapcar (lambda (pred)
1201 (unless (memq (cdr pred
) gnus-article-unpropagated-mark-lists
)
1202 (gnus-info-set-marks
1204 (gnus-update-alist-soft
1206 (cdr (assq (cdr pred
) nnfolder-marks
))
1207 (gnus-info-marks info
))
1209 gnus-article-mark-lists
)
1210 (let ((seen (cdr (assq 'read nnfolder-marks
))))
1211 (gnus-info-set-read info
1212 (if (and (integerp (car seen
))
1214 (list (cons (car seen
) (car seen
)))
1216 (nnheader-message 8 "Updating marks for %s...done" group
))
1219 (defun nnfolder-group-marks-pathname (group)
1220 "Make pathname for GROUP NOV."
1221 (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory
)))
1222 (concat (nnfolder-group-pathname group
) nnfolder-marks-file-suffix
)))
1224 (defun nnfolder-marks-changed-p (group)
1225 (let ((file (nnfolder-group-marks-pathname group
)))
1226 (if (null (gnus-gethash file nnfolder-marks-modtime
))
1227 t
;; never looked at marks file, assume it has changed
1228 (not (equal (gnus-gethash file nnfolder-marks-modtime
)
1229 (nth 5 (file-attributes file
)))))))
1231 (defun nnfolder-save-marks (group server
)
1232 (let ((file-name-coding-system nnmail-pathname-coding-system
)
1233 (file (nnfolder-group-marks-pathname group
)))
1236 (with-temp-file file
1238 (gnus-prin1 nnfolder-marks
)
1241 (nth 5 (file-attributes file
))
1242 nnfolder-marks-modtime
))
1243 (error (or (gnus-yes-or-no-p
1244 (format "Could not write to %s (%s). Continue? " file err
))
1245 (error "Cannot write to %s (%s)" file err
))))))
1247 (defun nnfolder-open-marks (group server
)
1248 (let ((file (nnfolder-group-marks-pathname group
)))
1249 (if (file-exists-p file
)
1252 (gnus-sethash file
(nth 5 (file-attributes file
))
1253 nnfolder-marks-modtime
)
1254 (nnheader-insert-file-contents file
)
1255 (setq nnfolder-marks
(read (current-buffer)))
1256 (dolist (el gnus-article-unpropagated-mark-lists
)
1257 (setq nnfolder-marks
(gnus-remassoc el nnfolder-marks
))))
1258 (error (or (gnus-yes-or-no-p
1259 (format "Error reading nnfolder marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err
))
1260 (error "Cannot read nnfolder marks file %s (%s)" file err
))))
1261 ;; User didn't have a .marks file. Probably first time
1262 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1263 (let ((info (gnus-get-info
1264 (gnus-group-prefixed-name
1266 (gnus-server-to-method (format "nnfolder:%s" server
))))))
1267 (nnheader-message 7 "Bootstrapping marks for %s..." group
)
1268 (setq nnfolder-marks
(gnus-info-marks info
))
1269 (push (cons 'read
(gnus-info-read info
)) nnfolder-marks
)
1270 (dolist (el gnus-article-unpropagated-mark-lists
)
1271 (setq nnfolder-marks
(gnus-remassoc el nnfolder-marks
)))
1272 (nnfolder-save-marks group server
)
1273 (nnheader-message 7 "Bootstrapping marks for %s...done" group
)))))
1277 ;;; arch-tag: a040d0f4-4f4e-445f-8972-839575c5f7e6
1278 ;;; nnfolder.el ends here