1 ;;; nnfolder.el --- mail folder access for Gnus
3 ;; Copyright (C) 1995-2012 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
7 ;; Scott Byer <byer@mv.us.adobe.com>
8 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;; For Emacs <22.2 and XEmacs.
33 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
39 (eval-when-compile (require 'cl
))
44 ;; FIXME not explicitly used in this file.
45 (autoload 'gnus-article-unpropagatable-p
"gnus-sum")
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-active-file
57 (nnheader-concat nnfolder-directory
"active")
58 "The name of the active file.")
60 ;; I renamed this variable to something more in keeping with the general GNU
63 (defvoo nnfolder-ignore-active-file nil
64 "If non-nil, the active file is ignored.
65 This causes nnfolder to do some extra work in order to determine the
66 true active ranges of an mbox file. Note that the active file is
67 still saved, but its values are not used. This costs some extra time
68 when scanning an mbox when opening it.")
70 (defvoo nnfolder-distrust-mbox nil
71 "If non-nil, the folder will be distrusted.
72 This means that nnfolder will not trust the user with respect to
73 inserting unaccounted for mail in the middle of an mbox file. This
74 can greatly slow down scans, which now must scan the entire file for
75 unmarked messages. When nil, scans occur forward from the last marked
76 message, a huge time saver for large mailboxes.")
78 (defvoo nnfolder-newsgroups-file
79 (concat (file-name-as-directory nnfolder-directory
) "newsgroups")
80 "Mail newsgroups description file.")
82 (defvoo nnfolder-get-new-mail t
83 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
85 (defvoo nnfolder-prepare-save-mail-hook nil
86 "Hook run narrowed to an article before saving.")
88 (defvoo nnfolder-save-buffer-hook nil
89 "Hook run before saving the nnfolder mbox buffer.")
92 (defvoo nnfolder-inhibit-expiry nil
93 "If non-nil, inhibit expiry.")
97 (defconst nnfolder-version
"nnfolder 2.0"
100 (defconst nnfolder-article-marker
"X-Gnus-Article-Number: "
101 "String used to demarcate what the article number for a message is.")
103 (defvoo nnfolder-current-group nil
)
104 (defvoo nnfolder-current-buffer nil
)
105 (defvoo nnfolder-status-string
"")
106 (defvoo nnfolder-group-alist nil
)
107 (defvoo nnfolder-buffer-alist nil
)
108 (defvoo nnfolder-scantime-alist nil
)
109 (defvoo nnfolder-active-timestamp nil
)
110 (defvoo nnfolder-active-file-coding-system mm-text-coding-system
)
111 (defvoo nnfolder-active-file-coding-system-for-write
112 nnmail-active-file-coding-system
)
113 (defvoo nnfolder-file-coding-system mm-text-coding-system
)
114 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
115 "Coding system for save nnfolder file.
116 if nil, `nnfolder-file-coding-system' is used.") ; FIXME: fill-in the doc-string of this variable
118 (defvoo nnfolder-nov-is-evil nil
119 "If non-nil, Gnus will never generate and use nov databases for mail groups.
120 Using nov databases will speed up header fetching considerably.
121 This variable shouldn't be flipped much. If you have, for some reason,
122 set this to t, and want to set it to nil again, you should always run
123 the `nnfolder-generate-active-file' command. The function will go
124 through all nnfolder directories and generate nov databases for them
125 all. This may very well take some time.")
127 (defvoo nnfolder-nov-file-suffix
".nov")
129 (defvoo nnfolder-nov-buffer-alist nil
)
131 (defvar nnfolder-nov-buffer-file-name nil
)
135 ;;; Interface functions
137 (nnoo-define-basics nnfolder
)
139 (deffoo nnfolder-retrieve-headers
(articles &optional group server fetch-old
)
140 (with-current-buffer nntp-server-buffer
142 (let (article start stop num
)
143 (nnfolder-possibly-change-group group server
)
144 (when nnfolder-current-buffer
145 (set-buffer nnfolder-current-buffer
)
146 (goto-char (point-min))
147 (if (stringp (car articles
))
149 (if (nnfolder-retrieve-headers-with-nov articles fetch-old
)
151 (setq articles
(gnus-sorted-intersection
152 ;; Is ARTICLES sorted?
154 (nnfolder-existing-articles)))
155 (while (setq article
(pop articles
))
156 (set-buffer nnfolder-current-buffer
)
157 (cond ((nnfolder-goto-article article
)
159 (setq stop
(if (search-forward "\n\n" nil t
)
162 (set-buffer nntp-server-buffer
)
163 (insert (format "221 %d Article retrieved.\n" article
))
164 (insert-buffer-substring nnfolder-current-buffer
166 (goto-char (point-max))
169 ;; If we couldn't find this article, skip over ranges
170 ;; of missing articles so we don't search the whole file
175 ;; Check that we are either at BOF or after an
176 ;; article with a lower number. We do this so we
177 ;; won't be confused by out-of-order article numbers,
178 ;; as caused by active file bogosity.
181 ((search-backward (concat "\n" nnfolder-article-marker
)
183 (goto-char (match-end 0))
184 (setq num
(string-to-number
186 (point) (point-at-eol))))
189 ;; Check that we are before an article with a
191 (search-forward (concat "\n" nnfolder-article-marker
)
194 (setq num
(string-to-number
196 (point) (point-at-eol))))
198 ;; Discard any article numbers before the one we're
201 (< (car articles
) num
))
202 (setq articles
(cdr articles
))))
204 (set-buffer nntp-server-buffer
)
205 (nnheader-fold-continuation-lines)
208 (deffoo nnfolder-open-server
(server &optional defs
)
209 (nnoo-change-server 'nnfolder server defs
)
210 (nnmail-activate 'nnfolder t
)
211 (gnus-make-directory nnfolder-directory
)
212 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
213 (and nnfolder-nov-directory
214 (gnus-make-directory nnfolder-nov-directory
)))
216 ((not (file-exists-p nnfolder-directory
))
217 (nnfolder-close-server)
218 (nnheader-report 'nnfolder
"Couldn't create directory: %s"
220 ((not (file-directory-p (file-truename nnfolder-directory
)))
221 (nnfolder-close-server)
222 (nnheader-report 'nnfolder
"Not a directory: %s" nnfolder-directory
))
224 (nnmail-activate 'nnfolder
)
225 (nnheader-report 'nnfolder
"Opened server %s using directory %s"
226 server nnfolder-directory
)
229 (deffoo nnfolder-request-close
()
230 (let ((alist nnfolder-buffer-alist
))
232 (nnfolder-close-group (caar alist
) nil t
)
233 (setq alist
(cdr alist
))))
234 (nnoo-close-server 'nnfolder
)
235 (setq nnfolder-buffer-alist nil
236 nnfolder-group-alist nil
))
238 (deffoo nnfolder-request-article
(article &optional group server buffer
)
239 (nnfolder-possibly-change-group group server
)
240 (with-current-buffer nnfolder-current-buffer
241 (goto-char (point-min))
242 (when (nnfolder-goto-article article
)
246 (unless (and (nnmail-search-unix-mail-delim)
248 (goto-char (point-max)))
250 (let ((nntp-server-buffer (or buffer nntp-server-buffer
)))
251 (set-buffer nntp-server-buffer
)
253 (insert-buffer-substring nnfolder-current-buffer start stop
)
254 (goto-char (point-min))
255 (while (looking-at "From ")
257 (insert "X-From-Line: ")
259 (if (numberp article
)
260 (cons nnfolder-current-group article
)
261 (goto-char (point-min))
262 (cons nnfolder-current-group
263 (if (search-forward (concat "\n" nnfolder-article-marker
)
265 (string-to-number (buffer-substring
266 (point) (point-at-eol)))
269 (deffoo nnfolder-request-group
(group &optional server dont-check info
)
270 (nnfolder-possibly-change-group group server t
)
272 (cond ((not (assoc group nnfolder-group-alist
))
273 (nnheader-report 'nnfolder
"No such group: %s" group
))
274 ((file-directory-p (nnfolder-group-pathname group
))
275 (nnheader-report 'nnfolder
"%s is a directory"
276 (file-name-as-directory
277 (let ((nnmail-pathname-coding-system nil
))
278 (nnfolder-group-pathname group
)))))
280 (nnheader-report 'nnfolder
"Selected group %s" group
)
283 (let* ((active (assoc group nnfolder-group-alist
))
285 (range (cadr active
)))
288 (nnheader-report 'nnfolder
"No such group: %s" group
))
289 ((null nnfolder-current-group
)
290 (nnheader-report 'nnfolder
"Empty group: %s" group
))
292 (nnheader-report 'nnfolder
"Selected group %s" group
)
293 (nnheader-insert "211 %d %d %d %s\n"
294 (1+ (- (cdr range
) (car range
)))
295 (car range
) (cdr range
) group
))))))))
297 (deffoo nnfolder-request-scan
(&optional group server
)
298 (nnfolder-possibly-change-group nil server
)
299 (when nnfolder-get-new-mail
300 (nnfolder-possibly-change-group group server
)
302 'nnfolder
'nnfolder-save-all-buffers
303 nnfolder-directory group
)))
305 (defun nnfolder-save-all-buffers ()
306 (let ((bufs nnfolder-buffer-alist
))
309 (if (not (gnus-buffer-live-p (nth 1 (car bufs
))))
310 (setq nnfolder-buffer-alist
311 (delq (car bufs
) nnfolder-buffer-alist
))
312 (set-buffer (nth 1 (car bufs
)))
313 (nnfolder-save-buffer)
314 (kill-buffer (current-buffer)))
315 (setq bufs
(cdr bufs
))))))
317 ;; Don't close the buffer if we're not shutting down the server. This way,
318 ;; we can keep the buffer in the group buffer cache, and not have to grovel
319 ;; over the buffer again unless we add new mail to it or modify it in some
322 (deffoo nnfolder-close-group
(group &optional server force
)
323 ;; Make sure we _had_ the group open.
324 (when (or (assoc group nnfolder-buffer-alist
)
325 (equal group nnfolder-current-group
))
326 (let ((inf (assoc group nnfolder-buffer-alist
)))
328 (when (and nnfolder-current-group
329 nnfolder-current-buffer
)
330 (push (list nnfolder-current-group nnfolder-current-buffer
)
331 nnfolder-buffer-alist
))
332 (setq nnfolder-buffer-alist
333 (delq inf nnfolder-buffer-alist
))
334 (setq nnfolder-current-buffer
(cadr inf
)
335 nnfolder-current-group
(car inf
))))
336 (when (and nnfolder-current-buffer
337 (buffer-name nnfolder-current-buffer
))
338 (with-current-buffer nnfolder-current-buffer
339 ;; If the buffer was modified, write the file out now.
340 (nnfolder-save-buffer)
341 ;; If we're shutting the server down, we need to kill the
342 ;; buffer and remove it from the open buffer list. Or, of
343 ;; course, if we're trying to minimize our space impact.
344 (kill-buffer (current-buffer))
345 (setq nnfolder-buffer-alist
(delq (assoc group nnfolder-buffer-alist
)
346 nnfolder-buffer-alist
)))))
347 (setq nnfolder-current-group nil
348 nnfolder-current-buffer nil
)
351 (deffoo nnfolder-request-create-group
(group &optional server args
)
352 (nnfolder-possibly-change-group nil server
)
353 (nnmail-activate 'nnfolder
)
354 (cond ((zerop (length group
))
355 (nnheader-report 'nnfolder
"Invalid (empty) group name"))
356 ((file-directory-p (nnfolder-group-pathname group
))
357 (nnheader-report 'nnfolder
"%s is a directory"
358 (file-name-as-directory
359 (let ((nnmail-pathname-coding-system nil
))
360 (nnfolder-group-pathname group
)))))
361 ((assoc group nnfolder-group-alist
)
364 (push (list group
(cons 1 0)) nnfolder-group-alist
)
365 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
367 (nnfolder-read-folder group
))
370 (deffoo nnfolder-request-list
(&optional server
)
371 (nnfolder-possibly-change-group nil server
)
373 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system
))
374 (nnmail-find-file nnfolder-active-file
)
375 (setq nnfolder-group-alist
(nnmail-get-active)))
378 (deffoo nnfolder-request-newgroups
(date &optional server
)
379 (nnfolder-possibly-change-group nil server
)
380 (nnfolder-request-list server
))
382 (deffoo nnfolder-request-list-newsgroups
(&optional server
)
383 (nnfolder-possibly-change-group nil server
)
385 (let ((nnmail-file-coding-system nnfolder-file-coding-system
))
386 (nnmail-find-file nnfolder-newsgroups-file
))))
388 ;; Return a list consisting of all article numbers existing in the
391 (defun nnfolder-existing-articles ()
393 (when nnfolder-current-buffer
394 (set-buffer nnfolder-current-buffer
)
395 (goto-char (point-min))
396 (let ((marker (concat "\n" nnfolder-article-marker
))
399 (while (and (search-forward marker nil t
)
400 (re-search-forward number nil t
))
401 (let ((newnum (string-to-number (match-string 0))))
402 (if (nnmail-within-headers-p)
403 (push newnum numbers
))))
404 ;; The article numbers are increasing, so this result is sorted.
405 (nreverse numbers
)))))
407 (autoload 'gnus-request-group
"gnus-int")
408 (declare-function gnus-request-create-group
"gnus-int"
409 (group &optional gnus-command-method args
))
411 (deffoo nnfolder-request-expire-articles
(articles newsgroup
412 &optional server force
)
413 (nnfolder-possibly-change-group newsgroup server
)
415 ;; The articles we have deleted so far.
416 (deleted-articles nil
)
417 ;; The articles that really exist and will
418 ;; be expired if they are old enough.
420 (gnus-sorted-intersection articles
(nnfolder-existing-articles)))
422 (nnmail-activate 'nnfolder
)
424 (with-current-buffer nnfolder-current-buffer
425 ;; Since messages are sorted in arrival order and expired in the
426 ;; same order, we can stop as soon as we find a message that is
428 (while (and maybe-expirable is-old
)
429 (goto-char (point-min))
430 (when (and (nnfolder-goto-article (car maybe-expirable
))
431 (search-forward (concat "\n" nnfolder-article-marker
)
435 (nnmail-expired-article-p
438 (point) (progn (end-of-line) (point)))
439 force nnfolder-inhibit-expiry
))
440 (setq target nnmail-expiry-target
)
441 (unless (eq target
'delete
)
443 (nnfolder-request-article (car maybe-expirable
)
444 newsgroup server
(current-buffer))
445 (let ((nnfolder-current-directory nil
))
446 (when (functionp target
)
447 (setq target
(funcall target newsgroup
)))
448 (when (and target
(not (eq target
'delete
)))
449 (if (or (gnus-request-group target
)
450 (gnus-request-create-group target
))
451 (nnmail-expiry-target-group target newsgroup
)
452 (setq target nil
)))))
453 (nnfolder-possibly-change-group newsgroup server
))
455 (nnheader-message 5 "Deleting article %d in %s..."
456 (car maybe-expirable
) newsgroup
)
457 (nnfolder-delete-mail)
458 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
459 (nnfolder-nov-delete-article newsgroup
(car maybe-expirable
)))
460 ;; Must remember which articles were actually deleted
461 (push (car maybe-expirable
) deleted-articles
))))
462 (setq maybe-expirable
(cdr maybe-expirable
)))
463 (unless nnfolder-inhibit-expiry
464 (nnheader-message 5 "Deleting articles...done"))
465 (nnfolder-save-buffer)
466 (nnfolder-adjust-min-active newsgroup
)
467 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
468 (nnfolder-save-all-buffers)
469 (gnus-sorted-difference articles
(nreverse deleted-articles
)))))
471 (deffoo nnfolder-request-move-article
(article group server accept-form
472 &optional last move-is-internal
)
474 (let ((buf (get-buffer-create " *nnfolder move*"))
477 (nnfolder-request-article article group server
)
478 (with-current-buffer buf
480 (insert-buffer-substring nntp-server-buffer
)
481 (goto-char (point-min))
482 (while (re-search-forward
483 (concat "^" nnfolder-article-marker
)
484 (save-excursion (and (search-forward "\n\n" nil t
) (point)))
487 (setq result
(eval accept-form
))
491 (nnfolder-possibly-change-group group server
)
492 (set-buffer nnfolder-current-buffer
)
493 (goto-char (point-min))
494 (when (nnfolder-goto-article article
)
495 (nnfolder-delete-mail))
496 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
497 (nnfolder-nov-delete-article group article
))
499 (nnfolder-save-buffer)
500 (nnfolder-adjust-min-active group
)
501 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))))
504 (deffoo nnfolder-request-accept-article
(group &optional server last
)
506 (nnfolder-possibly-change-group group server
)
507 (nnmail-check-syntax)
508 (let ((buf (current-buffer))
510 (goto-char (point-min))
511 (when (looking-at "X-From-Line: ")
512 (replace-match "From ")
513 (while (progn (forward-line) (looking-at "[ \t]"))
516 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system
)
517 (nntp-server-buffer (current-buffer)))
518 (nnmail-find-file nnfolder-active-file
)
519 (setq nnfolder-group-alist
(nnmail-parse-active))))
521 (goto-char (point-min))
522 (if (search-forward "\n\n" nil t
)
524 (goto-char (point-max)))
525 (while (re-search-backward (concat "^" nnfolder-article-marker
) nil t
)
526 (delete-region (point) (progn (forward-line 1) (point))))
527 (when nnmail-cache-accepted-message-ids
528 (nnmail-cache-insert (nnmail-fetch-field "message-id")
530 (nnmail-fetch-field "subject")
531 (nnmail-fetch-field "from")))
532 (setq result
(if (stringp group
)
533 (list (cons group
(nnfolder-active-number group
)))
535 (nnmail-article-group 'nnfolder-active-number
))))
536 (if (and (null result
)
537 (yes-or-no-p "Moved to `junk' group; delete article? "))
540 (car (nnfolder-save-mail result
)))))
543 (nnfolder-possibly-change-folder (or (caar art-group
) group
))
544 (nnfolder-save-buffer)
545 (when nnmail-cache-accepted-message-ids
546 (nnmail-cache-close))))
547 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
549 (nnheader-report 'nnfolder
"Couldn't store article"))
552 (deffoo nnfolder-request-replace-article
(article group buffer
)
553 (nnfolder-possibly-change-group group
)
554 (with-current-buffer buffer
555 (goto-char (point-min))
556 (if (not (looking-at "X-From-Line: "))
557 (insert "From nobody " (current-time-string) "\n")
558 (replace-match "From ")
560 (while (looking-at "[ \t]")
563 (nnfolder-normalize-buffer)
564 (set-buffer nnfolder-current-buffer
)
565 (goto-char (point-min))
566 (if (not (nnfolder-goto-article article
))
568 (nnfolder-delete-mail)
569 (insert-buffer-substring buffer
)
570 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
571 (with-current-buffer buffer
572 (let ((headers (nnfolder-parse-head article
573 (point-min) (point-max))))
574 (with-current-buffer (nnfolder-open-nov group
)
575 (if (nnheader-find-nov-line article
)
576 (delete-region (point) (progn (forward-line 1) (point))))
577 (nnheader-insert-nov headers
)))))
578 (nnfolder-save-buffer)
581 (deffoo nnfolder-request-delete-group
(group &optional force server
)
582 (nnfolder-close-group group server t
)
583 ;; Delete all articles in GROUP.
585 () ; Don't delete the articles.
586 ;; Delete the file that holds the group.
587 (let ((data (nnfolder-group-pathname group
))
588 (nov (nnfolder-group-nov-pathname group
)))
589 (ignore-errors (delete-file data
))
590 (ignore-errors (delete-file nov
))))
591 ;; Remove the group from all structures.
592 (setq nnfolder-group-alist
593 (delq (assoc group nnfolder-group-alist
) nnfolder-group-alist
)
594 nnfolder-current-group nil
595 nnfolder-current-buffer nil
)
596 ;; Save the active file.
597 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
600 (deffoo nnfolder-request-rename-group
(group new-name
&optional server
)
601 (nnfolder-possibly-change-group group server
)
602 (with-current-buffer nnfolder-current-buffer
603 (and (file-writable-p buffer-file-name
)
605 (let ((new-file (nnfolder-group-pathname new-name
)))
606 (gnus-make-directory (file-name-directory new-file
))
607 (rename-file buffer-file-name new-file
)
608 (when (file-exists-p (nnfolder-group-nov-pathname group
))
609 (setq new-file
(nnfolder-group-nov-pathname new-name
))
610 (gnus-make-directory (file-name-directory new-file
))
611 (rename-file (nnfolder-group-nov-pathname group
) new-file
)))
613 ;; That went ok, so we change the internal structures.
614 (let ((entry (assoc group nnfolder-group-alist
)))
615 (and entry
(setcar entry new-name
))
616 (setq nnfolder-current-buffer nil
617 nnfolder-current-group nil
)
618 ;; Save the new group alist.
619 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
620 ;; We kill the buffer instead of renaming it and stuff.
621 (kill-buffer (current-buffer))
624 (deffoo nnfolder-request-regenerate
(server)
625 (nnfolder-possibly-change-group nil server
)
626 (nnfolder-generate-active-file)
630 ;;; Internal functions.
632 (defun nnfolder-adjust-min-active (group)
633 ;; Find the lowest active article in this group.
634 (let* ((active (cadr (assoc group nnfolder-group-alist
)))
635 (marker (concat "\n" nnfolder-article-marker
))
637 (activemin (cdr active
)))
638 (with-current-buffer nnfolder-current-buffer
639 (goto-char (point-min))
640 (while (and (search-forward marker nil t
)
641 (re-search-forward number nil t
))
642 (let ((newnum (string-to-number (match-string 0))))
643 (if (nnmail-within-headers-p)
644 (setq activemin
(min activemin newnum
)))))
645 (setcar active activemin
))))
647 (defun nnfolder-article-string (article)
648 (if (numberp article
)
649 (concat "\n" nnfolder-article-marker
(int-to-string article
) " ")
650 (concat "\nMessage-ID: " article
)))
652 (defun nnfolder-goto-article (article)
653 "Place point at the start of the headers of ARTICLE.
654 ARTICLE can be an article number or a Message-ID.
655 Returns t if successful, nil otherwise."
656 (let ((art-string (nnfolder-article-string article
))
658 ;; It is likely that we are at or before the delimiter line.
659 ;; We therefore go to the end of the previous line, and start
660 ;; searching from there.
665 ;; First search forward.
666 (while (and (setq found
(search-forward art-string nil t
))
667 (not (nnmail-within-headers-p))))
668 ;; If unsuccessful, search backward from where we started,
671 (while (and (setq found
(search-backward art-string nil t
))
672 (not (nnmail-within-headers-p)))))
674 (nnmail-search-unix-mail-delim-backward))))
676 (defun nnfolder-delete-mail (&optional leave-delim
)
677 "Delete the message that point is in.
678 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
679 deleted. Point is left where the deleted region was."
683 ;; In case point is at the beginning of the message already.
685 (nnmail-search-unix-mail-delim-backward)
686 (if leave-delim
(progn (forward-line 1) (point))
690 (if (nnmail-search-unix-mail-delim)
693 (run-hooks 'nnfolder-delete-mail-hook
)
694 (delete-region (point-min) (point-max))))
696 (defun nnfolder-possibly-change-group (group &optional server dont-check
)
699 (not (nnfolder-server-opened server
)))
700 (nnfolder-open-server server
))
701 (unless (gnus-buffer-live-p nnfolder-current-buffer
)
702 (setq nnfolder-current-buffer nil
703 nnfolder-current-group nil
))
705 (let ((file-name-coding-system nnmail-pathname-coding-system
))
707 (not (equal group nnfolder-current-group
))
709 (nnmail-activate 'nnfolder
)
710 (and (assoc group nnfolder-group-alist
)
711 (file-exists-p (nnfolder-group-pathname group
)))))
713 (setq nnfolder-current-group group
714 nnfolder-current-buffer nil
)
716 ;; If we have to change groups, see if we don't already have
717 ;; the folder in memory. If we do, verify the modtime and
718 ;; destroy the folder if needed so we can rescan it.
719 (setq nnfolder-current-buffer
720 (nth 1 (assoc group nnfolder-buffer-alist
)))
722 ;; If the buffer is not live, make sure it isn't in the
723 ;; alist. If it is live, verify that nobody else has
724 ;; touched the file since last time.
725 (when (and nnfolder-current-buffer
726 (not (gnus-buffer-live-p nnfolder-current-buffer
)))
727 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)
728 nnfolder-current-buffer nil
))
730 (setq nnfolder-current-group group
)
732 (when (or (not nnfolder-current-buffer
)
733 (not (verify-visited-file-modtime
734 nnfolder-current-buffer
)))
736 (setq file
(nnfolder-group-pathname group
))
737 ;; See whether we need to create the new file.
738 (unless (file-exists-p file
)
739 (gnus-make-directory (file-name-directory file
))
740 (let ((nnmail-file-coding-system
741 (or nnfolder-file-coding-system-for-write
742 nnfolder-file-coding-system-for-write
)))
743 (nnmail-write-region (point-min) (point-min)
745 (when (setq nnfolder-current-buffer
(nnfolder-read-folder group
))
746 (set-buffer nnfolder-current-buffer
)
747 (push (list group nnfolder-current-buffer
)
748 nnfolder-buffer-alist
)))))))))
750 (defun nnfolder-save-mail (group-art-list)
751 "Called narrowed to an article."
752 (let* (save-list group-art
)
753 (goto-char (point-min))
754 ;; The From line may have been quoted by movemail.
755 (when (looking-at ">From")
757 ;; This might come from somewhere else.
758 (unless (looking-at "From ")
759 (insert "From nobody " (current-time-string) "\n")
760 (goto-char (point-min)))
761 ;; Quote all "From " lines in the article.
763 (let (case-fold-search)
764 (while (re-search-forward "^From " nil t
)
767 (setq save-list group-art-list
)
768 (nnmail-insert-lines)
769 (nnmail-insert-xref group-art-list
)
770 (run-hooks 'nnmail-prepare-save-mail-hook
)
771 (run-hooks 'nnfolder-prepare-save-mail-hook
)
773 ;; Insert the mail into each of the destination groups.
774 (while (setq group-art
(pop group-art-list
))
775 ;; Kill any previous newsgroup markers.
776 (goto-char (point-min))
777 (if (search-forward "\n\n" nil t
)
779 (goto-char (point-max)))
780 (while (search-backward (concat "\n" nnfolder-article-marker
) nil t
)
781 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
783 ;; Insert the new newsgroup marker.
784 (nnfolder-insert-newsgroup-line group-art
)
787 (let ((beg (point-min))
789 (obuf (current-buffer)))
790 (nnfolder-possibly-change-folder (car group-art
))
791 (let ((buffer-read-only nil
))
792 (nnfolder-normalize-buffer)
793 (insert-buffer-substring obuf beg end
))
794 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
796 (nnfolder-add-nov (car group-art
) (cdr group-art
)
797 (nnfolder-parse-head nil beg end
))))))
799 ;; Did we save it anywhere?
802 (defun nnfolder-normalize-buffer ()
803 "Make sure there are two newlines at the end of the buffer."
804 (goto-char (point-max))
805 (skip-chars-backward "\n")
806 (delete-region (point) (point-max))
810 (defun nnfolder-insert-newsgroup-line (group-art)
812 (goto-char (point-min))
813 (unless (search-forward "\n\n" nil t
)
814 (goto-char (point-max))
817 (insert (format (concat nnfolder-article-marker
"%d %s\n")
818 (cdr group-art
) (message-make-date)))))
820 (defun nnfolder-active-number (group)
821 ;; Find the next article number in GROUP.
822 (let ((active (cadr (assoc group nnfolder-group-alist
))))
824 (setcdr active
(1+ (cdr active
)))
825 ;; This group is new, so we create a new entry for it.
826 ;; This might be a bit naughty... creating groups on the drop of
827 ;; a hat, but I don't know...
828 (push (list group
(setq active
(cons 1 1)))
829 nnfolder-group-alist
))
832 (defun nnfolder-possibly-change-folder (group)
833 (let ((inf (assoc group nnfolder-buffer-alist
)))
835 (gnus-buffer-live-p (cadr inf
)))
836 (set-buffer (cadr inf
))
838 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)))
839 (when nnfolder-group-alist
840 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))
841 (push (list group
(nnfolder-read-folder group
))
842 nnfolder-buffer-alist
))))
844 ;; This method has a problem if you've accidentally let the active
845 ;; list get out of sync with the files. This could happen, say, if
846 ;; you've accidentally gotten new mail with something other than Gnus
847 ;; (but why would _that_ ever happen? :-). In that case, we will be
848 ;; in the middle of processing the file, ready to add new X-Gnus
849 ;; article number markers, and we'll run across a message with no ID
850 ;; yet - the active list _may_not_ be ready for us yet.
852 ;; To handle this, I'm modifying this routine to maintain the maximum
853 ;; ID seen so far, and when we hit a message with no ID, we will
854 ;; _manually_ scan the rest of the message looking for any more,
855 ;; possibly higher IDs. We'll assume the maximum that we find is the
856 ;; highest active. Note that this shouldn't cost us much extra time
857 ;; at all, but will be a lot less vulnerable to glitches between the
858 ;; mbox and the active file.
860 (defun nnfolder-read-folder (group)
861 (let* ((file (nnfolder-group-pathname group
))
862 (nov (nnfolder-group-nov-pathname group
))
864 (let ((nnheader-file-coding-system
865 nnfolder-file-coding-system
))
866 (nnheader-find-file-noselect file t
)))))
867 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
868 (buffer-disable-undo)
869 (if (equal (cadr (assoc group nnfolder-scantime-alist
))
870 (nth 5 (file-attributes file
)))
871 ;; This looks up-to-date, so we don't do any scanning.
872 (if (file-exists-p file
)
874 (push (list group buffer
) nnfolder-buffer-alist
)
875 (set-buffer-modified-p t
)
876 (nnfolder-save-buffer))
877 ;; Parse the damn thing.
879 (goto-char (point-min))
880 ;; Remove any blank lines at the start.
881 (while (eq (following-char) ?
\n)
883 (nnmail-activate 'nnfolder
)
885 (let ((delim "^From ")
886 (marker (concat "\n" nnfolder-article-marker
))
888 (active (or (cadr (assoc group nnfolder-group-alist
))
890 (scantime (assoc group nnfolder-scantime-alist
))
891 (minid (or (and (boundp 'most-positive-fixnum
)
892 most-positive-fixnum
)
894 maxid start end newscantime
895 novbuf articles newnum
897 (setq maxid
(cdr active
))
899 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
900 (and (file-exists-p nov
)
901 (file-newer-than-file-p nov file
)))
902 (unless (file-exists-p nov
)
903 (gnus-make-directory (file-name-directory nov
)))
905 (setq novbuf
(nnfolder-open-nov group
))
906 (goto-char (point-min))
908 (push (read novbuf
) articles
)
910 (setq articles
(nreverse articles
))))
911 (goto-char (point-min))
913 ;; Anytime the active number is 1 or 0, it is suspect. In
914 ;; that case, search the file manually to find the active
915 ;; number. Or, of course, if we're being paranoid. (This
916 ;; would also be the place to build other lists from the
917 ;; header markers, such as expunge lists, etc., if we ever
918 ;; desired to abandon the active file entirely for mboxes.)
919 (when (or nnfolder-ignore-active-file
922 (while (and (search-forward marker nil t
)
924 (setq newnum
(string-to-number (match-string 0)))
925 (when (nnmail-within-headers-p)
926 (setq maxid
(max maxid newnum
)
927 minid
(min minid newnum
))
929 (if (memq newnum articles
)
930 (setq articles
(delq newnum articles
))
931 (let ((headers (nnfolder-parse-head newnum
)))
932 (with-current-buffer novbuf
933 (nnheader-find-nov-line newnum
)
934 (nnheader-insert-nov headers
)))))))
935 (when (and novbuf articles
)
936 (with-current-buffer novbuf
937 (dolist (article articles
)
938 (when (nnheader-find-nov-line article
)
939 (delete-region (point)
940 (progn (forward-line 1) (point)))))))
941 (setcar active
(max 1 (min minid maxid
)))
942 (setcdr active
(max maxid
(cdr active
)))
943 (goto-char (point-min)))
945 ;; As long as we trust that the user will only insert
946 ;; unmarked mail at the end, go to the end and search
947 ;; backwards for the last marker. Find the start of that
948 ;; message, and begin to search for unmarked messages from
950 (when (not (or nnfolder-distrust-mbox
952 (goto-char (point-max))
953 (unless (re-search-backward marker nil t
)
954 (goto-char (point-min)))
955 ;;(when (nnmail-search-unix-mail-delim)
956 ;; (goto-char (point-min)))
959 ;; Keep track of the active number on our own, and insert it
960 ;; back into the active list when we're done. Also, prime
961 ;; the pump to cut down on the number of searches we do.
962 (unless (nnmail-search-unix-mail-delim)
963 (goto-char (point-max)))
964 (setq end
(point-marker))
965 (while (not (= end
(point-max)))
966 (setq start
(marker-position end
))
968 ;; There may be more than one "From " line, so we skip past
970 (while (looking-at delim
)
972 (set-marker end
(if (nnmail-search-unix-mail-delim)
976 (when (not (search-forward marker end t
))
977 (narrow-to-region start end
)
978 (nnmail-insert-lines)
979 (nnfolder-insert-newsgroup-line
982 (nnfolder-active-number group
))))
984 (let ((headers (nnfolder-parse-head newnum
(point-min)
986 (with-current-buffer novbuf
987 (goto-char (point-max))
988 (nnheader-insert-nov headers
))))
992 ;; Make absolutely sure that the active list reflects
994 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
996 ;; Set the scantime for this group.
997 (setq newscantime
(visited-file-modtime))
999 (setcdr scantime
(list newscantime
))
1000 (push (list group newscantime
)
1001 nnfolder-scantime-alist
))
1004 (nnfolder-save-nov))
1005 (current-buffer))))))
1008 (defun nnfolder-generate-active-file ()
1009 "Look for mbox folders in the nnfolder directory and make them into groups.
1010 This command does not work if you use short group names."
1012 (nnmail-activate 'nnfolder
)
1013 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1014 (dolist (file (directory-files (or nnfolder-nov-directory
1018 (regexp-quote nnfolder-nov-file-suffix
)
1020 (when (not (message-mail-file-mbox-p file
))
1022 (delete-file file
)))))
1023 (dolist (file (directory-files nnfolder-directory
))
1024 (when (and (not (backup-file-name-p file
))
1025 (message-mail-file-mbox-p
1026 (nnheader-concat nnfolder-directory file
)))
1027 (let ((oldgroup (assoc file nnfolder-group-alist
)))
1029 (nnheader-message 5 "Refreshing group %s..." file
)
1030 (nnheader-message 5 "Adding group %s..." file
))
1032 (setq nnfolder-group-alist
1033 (delq oldgroup
(copy-sequence nnfolder-group-alist
))))
1034 (push (list file
(cons 1 0)) nnfolder-group-alist
)
1035 (nnfolder-possibly-change-folder file
)
1036 (nnfolder-possibly-change-group file
)
1037 (nnfolder-close-group file
))))
1038 (nnheader-message 5 ""))
1040 (defun nnfolder-group-pathname (group)
1041 "Make file name for GROUP."
1043 (mm-encode-coding-string group nnmail-pathname-coding-system
))
1044 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory
))))
1045 ;; If this file exists, we use it directly.
1046 (if (or nnmail-use-long-file-names
1047 (file-exists-p (concat dir group
)))
1049 ;; If not, we translate dots into slashes.
1050 (concat dir
(nnheader-replace-chars-in-string group ?. ?
/)))))
1052 (defun nnfolder-group-nov-pathname (group)
1053 "Make pathname for GROUP NOV."
1054 (let ((nnfolder-directory
1055 (or nnfolder-nov-directory nnfolder-directory
)))
1056 (concat (nnfolder-group-pathname group
) nnfolder-nov-file-suffix
)))
1058 (defvar copyright-update
)
1060 (defun nnfolder-save-buffer ()
1062 (let ((delete-old-versions t
))
1063 (when (buffer-modified-p)
1064 (run-hooks 'nnfolder-save-buffer-hook
)
1065 (gnus-make-directory (file-name-directory (buffer-file-name)))
1066 (let ((coding-system-for-write
1067 (or nnfolder-file-coding-system-for-write
1068 nnfolder-file-coding-system
)))
1069 (set (make-local-variable 'copyright-update
) nil
)
1071 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1072 (nnfolder-save-nov))))
1074 (defun nnfolder-save-active (group-alist active-file
)
1075 (let ((nnmail-active-file-coding-system
1076 (or nnfolder-active-file-coding-system-for-write
1077 nnfolder-active-file-coding-system
)))
1078 (nnmail-save-active group-alist active-file
)))
1080 (defun nnfolder-open-nov (group)
1081 (or (cdr (assoc group nnfolder-nov-buffer-alist
))
1082 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group
))))
1083 (with-current-buffer buffer
1084 (set (make-local-variable 'nnfolder-nov-buffer-file-name
)
1085 (nnfolder-group-nov-pathname group
))
1087 (when (file-exists-p nnfolder-nov-buffer-file-name
)
1088 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name
)))
1089 (push (cons group buffer
) nnfolder-nov-buffer-alist
)
1092 (defun nnfolder-save-nov ()
1094 (while nnfolder-nov-buffer-alist
1095 (when (buffer-name (cdar nnfolder-nov-buffer-alist
))
1096 (set-buffer (cdar nnfolder-nov-buffer-alist
))
1097 (when (buffer-modified-p)
1098 (gnus-make-directory (file-name-directory
1099 nnfolder-nov-buffer-file-name
))
1100 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1102 (set-buffer-modified-p nil
)
1103 (kill-buffer (current-buffer)))
1104 (setq nnfolder-nov-buffer-alist
(cdr nnfolder-nov-buffer-alist
)))))
1106 (defun nnfolder-nov-delete-article (group article
)
1107 (with-current-buffer (nnfolder-open-nov group
)
1108 (when (nnheader-find-nov-line article
)
1109 (delete-region (point) (progn (forward-line 1) (point))))
1112 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old
)
1113 (if (or gnus-nov-is-evil nnfolder-nov-is-evil
)
1115 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group
)))
1116 (when (file-exists-p nov
)
1117 (with-current-buffer nntp-server-buffer
1119 (nnheader-insert-file-contents nov
)
1121 (not (numberp fetch-old
)))
1122 t
; Don't remove anything.
1123 (nnheader-nov-delete-outside-range
1124 (if fetch-old
(max 1 (- (car articles
) fetch-old
))
1126 (car (last articles
)))
1129 (defun nnfolder-parse-head (&optional number b e
)
1130 "Parse the head of the current buffer."
1131 (let ((buf (current-buffer))
1135 (setq b
(if (nnmail-search-unix-mail-delim-backward)
1136 (point) (point-min)))
1138 (setq e
(if (nnmail-search-unix-mail-delim)
1139 (point) (point-max))))
1140 (setq chars
(- e b
))
1141 (unless (zerop chars
)
1143 (if (search-forward "\n\n" e t
) (setq e
(1- (point)))))
1145 (insert-buffer-substring buf b e
)
1146 (let ((headers (nnheader-parse-naked-head)))
1147 (mail-header-set-chars headers chars
)
1148 (mail-header-set-number headers number
)
1151 (defun nnfolder-add-nov (group article headers
)
1152 "Add a nov line for the GROUP base."
1153 (with-current-buffer (nnfolder-open-nov group
)
1154 (goto-char (point-max))
1155 (mail-header-set-number headers article
)
1156 (nnheader-insert-nov headers
)))
1160 ;;; nnfolder.el ends here