1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;; Free Software Foundation, Inc.
5 ;; Author: Scott Byer <byer@mv.us.adobe.com>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
35 (eval-when-compile (require 'cl
))
39 (nnoo-declare nnfolder
)
41 (defvoo nnfolder-directory
(expand-file-name message-directory
)
42 "The name of the nnfolder directory.")
44 (defvoo nnfolder-active-file
45 (nnheader-concat nnfolder-directory
"active")
46 "The name of the active file.")
48 ;; I renamed this variable to something more in keeping with the general GNU
51 (defvoo nnfolder-ignore-active-file nil
52 "If non-nil, the active file is ignored.
53 This causes nnfolder to do some extra work in order to determine the
54 true active ranges of an mbox file. Note that the active file is
55 still saved, but its values are not used. This costs some extra time
56 when scanning an mbox when opening it.")
58 (defvoo nnfolder-distrust-mbox nil
59 "If non-nil, the folder will be distrusted.
60 This means that nnfolder will not trust the user with respect to
61 inserting unaccounted for mail in the middle of an mbox file. This
62 can greatly slow down scans, which now must scan the entire file for
63 unmarked messages. When nil, scans occur forward from the last marked
64 message, a huge time saver for large mailboxes.")
66 (defvoo nnfolder-newsgroups-file
67 (concat (file-name-as-directory nnfolder-directory
) "newsgroups")
68 "Mail newsgroups description file.")
70 (defvoo nnfolder-get-new-mail t
71 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
73 (defvoo nnfolder-prepare-save-mail-hook nil
74 "Hook run narrowed to an article before saving.")
76 (defvoo nnfolder-save-buffer-hook nil
77 "Hook run before saving the nnfolder mbox buffer.")
79 (defvoo nnfolder-inhibit-expiry nil
80 "If non-nil, inhibit expiry.")
84 (defconst nnfolder-version
"nnfolder 1.0"
87 (defconst nnfolder-article-marker
"X-Gnus-Article-Number: "
88 "String used to demarcate what the article number for a message is.")
90 (defvoo nnfolder-current-group nil
)
91 (defvoo nnfolder-current-buffer nil
)
92 (defvoo nnfolder-status-string
"")
93 (defvoo nnfolder-group-alist nil
)
94 (defvoo nnfolder-buffer-alist nil
)
95 (defvoo nnfolder-scantime-alist nil
)
96 (defvoo nnfolder-active-timestamp nil
)
97 (defvoo nnfolder-active-file-coding-system mm-text-coding-system
)
98 (defvoo nnfolder-active-file-coding-system-for-write
99 nnmail-active-file-coding-system
)
100 (defvoo nnfolder-file-coding-system mm-text-coding-system
)
101 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
102 "Coding system for save nnfolder file.
103 If nil, `nnfolder-file-coding-system' is used.")
107 ;;; Interface functions
109 (nnoo-define-basics nnfolder
)
111 (deffoo nnfolder-retrieve-headers
(articles &optional group server fetch-old
)
113 (set-buffer nntp-server-buffer
)
115 (let (article start stop
)
116 (nnfolder-possibly-change-group group server
)
117 (when nnfolder-current-buffer
118 (set-buffer nnfolder-current-buffer
)
119 (goto-char (point-min))
120 (if (stringp (car articles
))
122 (while (setq article
(pop articles
))
123 (set-buffer nnfolder-current-buffer
)
124 (when (nnfolder-goto-article article
)
126 (setq stop
(if (search-forward "\n\n" nil t
)
129 (set-buffer nntp-server-buffer
)
130 (insert (format "221 %d Article retrieved.\n" article
))
131 (insert-buffer-substring nnfolder-current-buffer start stop
)
132 (goto-char (point-max))
135 (set-buffer nntp-server-buffer
)
136 (nnheader-fold-continuation-lines)
139 (deffoo nnfolder-open-server
(server &optional defs
)
140 (nnoo-change-server 'nnfolder server defs
)
141 (nnmail-activate 'nnfolder t
)
142 (gnus-make-directory nnfolder-directory
)
144 ((not (file-exists-p nnfolder-directory
))
145 (nnfolder-close-server)
146 (nnheader-report 'nnfolder
"Couldn't create directory: %s"
148 ((not (file-directory-p (file-truename nnfolder-directory
)))
149 (nnfolder-close-server)
150 (nnheader-report 'nnfolder
"Not a directory: %s" nnfolder-directory
))
152 (nnmail-activate 'nnfolder
)
153 (nnheader-report 'nnfolder
"Opened server %s using directory %s"
154 server nnfolder-directory
)
157 (deffoo nnfolder-request-close
()
158 (let ((alist nnfolder-buffer-alist
))
160 (nnfolder-close-group (caar alist
) nil t
)
161 (setq alist
(cdr alist
))))
162 (nnoo-close-server 'nnfolder
)
163 (setq nnfolder-buffer-alist nil
164 nnfolder-group-alist nil
))
166 (deffoo nnfolder-request-article
(article &optional group server buffer
)
167 (nnfolder-possibly-change-group group server
)
169 (set-buffer nnfolder-current-buffer
)
170 (goto-char (point-min))
171 (when (nnfolder-goto-article article
)
175 (unless (and (nnmail-search-unix-mail-delim)
177 (goto-char (point-max)))
179 (let ((nntp-server-buffer (or buffer nntp-server-buffer
)))
180 (set-buffer nntp-server-buffer
)
182 (insert-buffer-substring nnfolder-current-buffer start stop
)
183 (goto-char (point-min))
184 (while (looking-at "From ")
186 (insert "X-From-Line: ")
188 (if (numberp article
)
189 (cons nnfolder-current-group article
)
190 (goto-char (point-min))
191 (cons nnfolder-current-group
192 (if (search-forward (concat "\n" nnfolder-article-marker
)
196 (point) (progn (end-of-line) (point))))
199 (deffoo nnfolder-request-group
(group &optional server dont-check
)
200 (nnfolder-possibly-change-group group server t
)
202 (if (not (assoc group nnfolder-group-alist
))
203 (nnheader-report 'nnfolder
"No such group: %s" group
)
206 (nnheader-report 'nnfolder
"Selected group %s" group
)
208 (let* ((active (assoc group nnfolder-group-alist
))
210 (range (cadr active
)))
213 (nnheader-report 'nnfolder
"No such group: %s" group
))
214 ((null nnfolder-current-group
)
215 (nnheader-report 'nnfolder
"Empty group: %s" group
))
217 (nnheader-report 'nnfolder
"Selected group %s" group
)
218 (nnheader-insert "211 %d %d %d %s\n"
219 (1+ (- (cdr range
) (car range
)))
220 (car range
) (cdr range
) group
))))))))
222 (deffoo nnfolder-request-scan
(&optional group server
)
223 (nnfolder-possibly-change-group nil server
)
224 (when nnfolder-get-new-mail
225 (nnfolder-possibly-change-group group server
)
229 (let ((bufs nnfolder-buffer-alist
))
232 (if (not (gnus-buffer-live-p (nth 1 (car bufs
))))
233 (setq nnfolder-buffer-alist
234 (delq (car bufs
) nnfolder-buffer-alist
))
235 (set-buffer (nth 1 (car bufs
)))
236 (nnfolder-save-buffer)
237 (kill-buffer (current-buffer)))
238 (setq bufs
(cdr bufs
))))))
242 ;; Don't close the buffer if we're not shutting down the server. This way,
243 ;; we can keep the buffer in the group buffer cache, and not have to grovel
244 ;; over the buffer again unless we add new mail to it or modify it in some
247 (deffoo nnfolder-close-group
(group &optional server force
)
248 ;; Make sure we _had_ the group open.
249 (when (or (assoc group nnfolder-buffer-alist
)
250 (equal group nnfolder-current-group
))
251 (let ((inf (assoc group nnfolder-buffer-alist
)))
253 (when (and nnfolder-current-group
254 nnfolder-current-buffer
)
255 (push (list nnfolder-current-group nnfolder-current-buffer
)
256 nnfolder-buffer-alist
))
257 (setq nnfolder-buffer-alist
258 (delq inf nnfolder-buffer-alist
))
259 (setq nnfolder-current-buffer
(cadr inf
)
260 nnfolder-current-group
(car inf
))))
261 (when (and nnfolder-current-buffer
262 (buffer-name nnfolder-current-buffer
))
264 (set-buffer nnfolder-current-buffer
)
265 ;; If the buffer was modified, write the file out now.
266 (nnfolder-save-buffer)
267 ;; If we're shutting the server down, we need to kill the
268 ;; buffer and remove it from the open buffer list. Or, of
269 ;; course, if we're trying to minimize our space impact.
270 (kill-buffer (current-buffer))
271 (setq nnfolder-buffer-alist
(delq (assoc group nnfolder-buffer-alist
)
272 nnfolder-buffer-alist
)))))
273 (setq nnfolder-current-group nil
274 nnfolder-current-buffer nil
)
277 (deffoo nnfolder-request-create-group
(group &optional server args
)
278 (nnfolder-possibly-change-group nil server
)
279 (nnmail-activate 'nnfolder
)
281 (unless (assoc group nnfolder-group-alist
)
282 (push (list group
(cons 1 0)) nnfolder-group-alist
)
283 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
284 (nnfolder-read-folder group
)))
287 (deffoo nnfolder-request-list
(&optional server
)
288 (nnfolder-possibly-change-group nil server
)
290 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system
))
291 (nnmail-find-file nnfolder-active-file
)
292 (setq nnfolder-group-alist
(nnmail-get-active)))
295 (deffoo nnfolder-request-newgroups
(date &optional server
)
296 (nnfolder-possibly-change-group nil server
)
297 (nnfolder-request-list server
))
299 (deffoo nnfolder-request-list-newsgroups
(&optional server
)
300 (nnfolder-possibly-change-group nil server
)
302 (let ((nnmail-file-coding-system nnfolder-file-coding-system
))
303 (nnmail-find-file nnfolder-newsgroups-file
))))
305 ;; Return a list consisting of all article numbers existing in the
308 (defun nnfolder-existing-articles ()
310 (when nnfolder-current-buffer
311 (set-buffer nnfolder-current-buffer
)
312 (goto-char (point-min))
313 (let ((marker (concat "\n" nnfolder-article-marker
))
317 (while (and (search-forward marker nil t
)
318 (re-search-forward number nil t
))
319 (let ((newnum (string-to-number (match-string 0))))
320 (if (nnmail-within-headers-p)
321 (push newnum numbers
))))
324 (deffoo nnfolder-request-expire-articles
325 (articles newsgroup
&optional server force
)
326 (nnfolder-possibly-change-group newsgroup server
)
328 ;; The articles we have deleted so far.
329 (deleted-articles nil
)
330 ;; The articles that really exist and will
331 ;; be expired if they are old enough.
333 (gnus-intersection articles
(nnfolder-existing-articles))))
334 (nnmail-activate 'nnfolder
)
337 (set-buffer nnfolder-current-buffer
)
338 ;; Since messages are sorted in arrival order and expired in the
339 ;; same order, we can stop as soon as we find a message that is
341 (while (and maybe-expirable is-old
)
342 (goto-char (point-min))
343 (when (and (nnfolder-goto-article (car maybe-expirable
))
344 (search-forward (concat "\n" nnfolder-article-marker
)
348 (nnmail-expired-article-p
351 (point) (progn (end-of-line) (point)))
352 force nnfolder-inhibit-expiry
))
353 (unless (eq nnmail-expiry-target
'delete
)
355 (nnfolder-request-article (car maybe-expirable
)
356 newsgroup server
(current-buffer))
357 (let ((nnml-current-directory nil
))
358 (nnmail-expiry-target-group
359 nnmail-expiry-target newsgroup
))))
360 (nnheader-message 5 "Deleting article %d in %s..."
361 (car maybe-expirable
) newsgroup
)
362 (nnfolder-delete-mail)
363 ;; Must remember which articles were actually deleted
364 (push (car maybe-expirable
) deleted-articles
)))
365 (setq maybe-expirable
(cdr maybe-expirable
)))
366 (unless nnfolder-inhibit-expiry
367 (nnheader-message 5 "Deleting articles...done"))
368 (nnfolder-save-buffer)
369 (nnfolder-adjust-min-active newsgroup
)
370 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
371 (gnus-sorted-complement articles
(nreverse deleted-articles
)))))
373 (deffoo nnfolder-request-move-article
(article group server
374 accept-form
&optional last
)
376 (let ((buf (get-buffer-create " *nnfolder move*"))
379 (nnfolder-request-article article group server
)
383 (insert-buffer-substring nntp-server-buffer
)
384 (goto-char (point-min))
385 (while (re-search-forward
386 (concat "^" nnfolder-article-marker
)
387 (save-excursion (and (search-forward "\n\n" nil t
) (point)))
389 (delete-region (progn (beginning-of-line) (point))
390 (progn (forward-line 1) (point))))
391 (setq result
(eval accept-form
))
395 (nnfolder-possibly-change-group group server
)
396 (set-buffer nnfolder-current-buffer
)
397 (goto-char (point-min))
398 (when (nnfolder-goto-article article
)
399 (nnfolder-delete-mail))
401 (nnfolder-save-buffer)
402 (nnfolder-adjust-min-active group
)
403 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))))
406 (deffoo nnfolder-request-accept-article
(group &optional server last
)
408 (nnfolder-possibly-change-group group server
)
409 (nnmail-check-syntax)
410 (let ((buf (current-buffer))
412 (goto-char (point-min))
413 (when (looking-at "X-From-Line: ")
414 (replace-match "From ")
415 (while (progn (forward-line) (looking-at "[ \t]"))
418 (nnfolder-request-list)
421 (goto-char (point-min))
422 (if (search-forward "\n\n" nil t
)
424 (goto-char (point-max)))
425 (while (re-search-backward (concat "^" nnfolder-article-marker
) nil t
)
426 (delete-region (point) (progn (forward-line 1) (point))))
427 (when nnmail-cache-accepted-message-ids
428 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
429 (setq result
(if (stringp group
)
430 (list (cons group
(nnfolder-active-number group
)))
432 (nnmail-article-group 'nnfolder-active-number
))))
433 (if (and (null result
)
434 (yes-or-no-p "Moved to `junk' group; delete article? "))
437 (car (nnfolder-save-mail result
)))))
440 (nnfolder-possibly-change-folder (or (caar art-group
) group
))
441 (nnfolder-save-buffer)
442 (when nnmail-cache-accepted-message-ids
443 (nnmail-cache-close)))))
444 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
446 (nnheader-report 'nnfolder
"Couldn't store article"))
449 (deffoo nnfolder-request-replace-article
(article group buffer
)
450 (nnfolder-possibly-change-group group
)
453 (goto-char (point-min))
455 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t
)
456 (setq xfrom
(match-string 1))
458 (goto-char (point-min))
460 (insert "From " xfrom
"\n")
461 (unless (looking-at "From ")
462 (insert "From nobody " (current-time-string) "\n"))))
463 (nnfolder-normalize-buffer)
464 (set-buffer nnfolder-current-buffer
)
465 (goto-char (point-min))
466 (if (not (nnfolder-goto-article article
))
468 (nnfolder-delete-mail)
469 (insert-buffer-substring buffer
)
470 (nnfolder-save-buffer)
473 (deffoo nnfolder-request-delete-group
(group &optional force server
)
474 (nnfolder-close-group group server t
)
475 ;; Delete all articles in GROUP.
477 () ; Don't delete the articles.
478 ;; Delete the file that holds the group.
480 (delete-file (nnfolder-group-pathname group
))))
481 ;; Remove the group from all structures.
482 (setq nnfolder-group-alist
483 (delq (assoc group nnfolder-group-alist
) nnfolder-group-alist
)
484 nnfolder-current-group nil
485 nnfolder-current-buffer nil
)
486 ;; Save the active file.
487 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
490 (deffoo nnfolder-request-rename-group
(group new-name
&optional server
)
491 (nnfolder-possibly-change-group group server
)
493 (set-buffer nnfolder-current-buffer
)
494 (and (file-writable-p buffer-file-name
)
498 (let ((new-file (nnfolder-group-pathname new-name
)))
499 (gnus-make-directory (file-name-directory new-file
))
502 ;; That went ok, so we change the internal structures.
503 (let ((entry (assoc group nnfolder-group-alist
)))
504 (and entry
(setcar entry new-name
))
505 (setq nnfolder-current-buffer nil
506 nnfolder-current-group nil
)
507 ;; Save the new group alist.
508 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
509 ;; We kill the buffer instead of renaming it and stuff.
510 (kill-buffer (current-buffer))
513 (defun nnfolder-request-regenerate (server)
514 (nnfolder-possibly-change-group nil server
)
515 (nnfolder-generate-active-file)
519 ;;; Internal functions.
521 (defun nnfolder-adjust-min-active (group)
522 ;; Find the lowest active article in this group.
523 (let* ((active (cadr (assoc group nnfolder-group-alist
)))
524 (marker (concat "\n" nnfolder-article-marker
))
526 (activemin (cdr active
)))
528 (set-buffer nnfolder-current-buffer
)
529 (goto-char (point-min))
530 (while (and (search-forward marker nil t
)
531 (re-search-forward number nil t
))
532 (let ((newnum (string-to-number (match-string 0))))
533 (if (nnmail-within-headers-p)
534 (setq activemin
(min activemin newnum
)))))
535 (setcar active activemin
))))
537 (defun nnfolder-article-string (article)
538 (if (numberp article
)
539 (concat "\n" nnfolder-article-marker
(int-to-string article
) " ")
540 (concat "\nMessage-ID: " article
)))
542 (defun nnfolder-goto-article (article)
543 "Place point at the start of the headers of ARTICLE.
544 ARTICLE can be an article number or a Message-ID.
545 Returns t if successful, nil otherwise."
546 (let ((art-string (nnfolder-article-string article
))
548 ;; It is likely that we are at or before the delimiter line.
549 ;; We therefore go to the end of the previous line, and start
550 ;; searching from there.
555 ;; First search forward.
556 (while (and (setq found
(search-forward art-string nil t
))
557 (not (nnmail-within-headers-p))))
558 ;; If unsuccessful, search backward from where we started,
561 (while (and (setq found
(search-backward art-string nil t
))
562 (not (nnmail-within-headers-p)))))
564 (nnmail-search-unix-mail-delim-backward))))
566 (defun nnfolder-delete-mail (&optional leave-delim
)
567 "Delete the message that point is in.
568 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
569 deleted. Point is left where the deleted region was."
573 ;; In case point is at the beginning of the message already.
575 (nnmail-search-unix-mail-delim-backward)
576 (if leave-delim
(progn (forward-line 1) (point))
580 (if (nnmail-search-unix-mail-delim)
583 (run-hooks 'nnfolder-delete-mail-hook
)
584 (delete-region (point-min) (point-max))))
586 (defun nnfolder-possibly-change-group (group &optional server dont-check
)
589 (not (nnfolder-server-opened server
)))
590 (nnfolder-open-server server
))
591 (unless (gnus-buffer-live-p nnfolder-current-buffer
)
592 (setq nnfolder-current-buffer nil
593 nnfolder-current-group nil
))
596 (not (equal group nnfolder-current-group
)))
597 (let ((file-name-coding-system nnmail-pathname-coding-system
))
598 (nnmail-activate 'nnfolder
)
599 (when (and (not (assoc group nnfolder-group-alist
))
601 (nnfolder-group-pathname group
))))
602 ;; The group doesn't exist, so we create a new entry for it.
603 (push (list group
(cons 1 0)) nnfolder-group-alist
)
604 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))
607 (setq nnfolder-current-group group
608 nnfolder-current-buffer nil
)
610 ;; If we have to change groups, see if we don't already have the
611 ;; folder in memory. If we do, verify the modtime and destroy
612 ;; the folder if needed so we can rescan it.
613 (setq nnfolder-current-buffer
614 (nth 1 (assoc group nnfolder-buffer-alist
)))
616 ;; If the buffer is not live, make sure it isn't in the alist. If it
617 ;; is live, verify that nobody else has touched the file since last
619 (when (and nnfolder-current-buffer
620 (not (gnus-buffer-live-p nnfolder-current-buffer
)))
621 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)
622 nnfolder-current-buffer nil
))
624 (setq nnfolder-current-group group
)
626 (when (or (not nnfolder-current-buffer
)
627 (not (verify-visited-file-modtime
628 nnfolder-current-buffer
)))
630 (setq file
(nnfolder-group-pathname group
))
631 ;; See whether we need to create the new file.
632 (unless (file-exists-p file
)
633 (gnus-make-directory (file-name-directory file
))
634 (let ((nnmail-file-coding-system
635 (or nnfolder-file-coding-system-for-write
636 nnfolder-file-coding-system-for-write
)))
637 (nnmail-write-region (point-min) (point-min)
639 (when (setq nnfolder-current-buffer
(nnfolder-read-folder group
))
640 (set-buffer nnfolder-current-buffer
)
641 (push (list group nnfolder-current-buffer
)
642 nnfolder-buffer-alist
)))))))))
644 (defun nnfolder-save-mail (group-art-list)
645 "Called narrowed to an article."
646 (let* (save-list group-art
)
647 (goto-char (point-min))
648 ;; The From line may have been quoted by movemail.
649 (when (looking-at ">From")
651 ;; This might come from somewhere else.
652 (unless (looking-at "From ")
653 (insert "From nobody " (current-time-string) "\n")
654 (goto-char (point-min)))
655 ;; Quote all "From " lines in the article.
657 (let (case-fold-search)
658 (while (re-search-forward "^From " nil t
)
661 (setq save-list group-art-list
)
662 (nnmail-insert-lines)
663 (nnmail-insert-xref group-art-list
)
664 (run-hooks 'nnmail-prepare-save-mail-hook
)
665 (run-hooks 'nnfolder-prepare-save-mail-hook
)
667 ;; Insert the mail into each of the destination groups.
668 (while (setq group-art
(pop group-art-list
))
669 ;; Kill any previous newsgroup markers.
670 (goto-char (point-min))
671 (if (search-forward "\n\n" nil t
)
673 (goto-char (point-max)))
674 (while (search-backward (concat "\n" nnfolder-article-marker
) nil t
)
675 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
677 ;; Insert the new newsgroup marker.
678 (nnfolder-insert-newsgroup-line group-art
)
681 (let ((beg (point-min))
683 (obuf (current-buffer)))
684 (nnfolder-possibly-change-folder (car group-art
))
685 (let ((buffer-read-only nil
))
686 (nnfolder-normalize-buffer)
687 (insert-buffer-substring obuf beg end
)))))
689 ;; Did we save it anywhere?
692 (defun nnfolder-normalize-buffer ()
693 "Make sure there are two newlines at the end of the buffer."
694 (goto-char (point-max))
695 (skip-chars-backward "\n")
696 (delete-region (point) (point-max))
699 (defun nnfolder-insert-newsgroup-line (group-art)
701 (goto-char (point-min))
702 (unless (search-forward "\n\n" nil t
)
703 (goto-char (point-max))
706 (insert (format (concat nnfolder-article-marker
"%d %s\n")
707 (cdr group-art
) (current-time-string)))))
709 (defun nnfolder-active-number (group)
710 ;; Find the next article number in GROUP.
711 (let ((active (cadr (assoc group nnfolder-group-alist
))))
713 (setcdr active
(1+ (cdr active
)))
714 ;; This group is new, so we create a new entry for it.
715 ;; This might be a bit naughty... creating groups on the drop of
716 ;; a hat, but I don't know...
717 (push (list group
(setq active
(cons 1 1)))
718 nnfolder-group-alist
))
721 (defun nnfolder-possibly-change-folder (group)
722 (let ((inf (assoc group nnfolder-buffer-alist
)))
724 (gnus-buffer-live-p (cadr inf
)))
725 (set-buffer (cadr inf
))
727 (setq nnfolder-buffer-alist
(delq inf nnfolder-buffer-alist
)))
728 (when nnfolder-group-alist
729 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
))
730 (push (list group
(nnfolder-read-folder group
))
731 nnfolder-buffer-alist
))))
733 ;; This method has a problem if you've accidentally let the active list get
734 ;; out of sync with the files. This could happen, say, if you've
735 ;; accidentally gotten new mail with something other than Gnus (but why
736 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
737 ;; processing the file, ready to add new X-Gnus article number markers, and
738 ;; we'll run across a message with no ID yet - the active list _may_not_ be
741 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
742 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
743 ;; rest of the message looking for any more, possibly higher IDs. We'll
744 ;; assume the maximum that we find is the highest active. Note that this
745 ;; shouldn't cost us much extra time at all, but will be a lot less
746 ;; vulnerable to glitches between the mbox and the active file.
748 (defun nnfolder-read-folder (group)
749 (let* ((file (nnfolder-group-pathname group
))
751 (let ((nnheader-file-coding-system
752 nnfolder-file-coding-system
))
753 (nnheader-find-file-noselect file
)))))
754 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
755 (if (equal (cadr (assoc group nnfolder-scantime-alist
))
756 (nth 5 (file-attributes file
)))
757 ;; This looks up-to-date, so we don't do any scanning.
758 (if (file-exists-p file
)
760 (push (list group buffer
) nnfolder-buffer-alist
)
761 (set-buffer-modified-p t
)
762 (nnfolder-save-buffer))
763 ;; Parse the damn thing.
765 (goto-char (point-min))
766 ;; Remove any blank lines at the start.
767 (while (eq (following-char) ?
\n)
769 (nnmail-activate 'nnfolder
)
771 (let ((delim "^From ")
772 (marker (concat "\n" nnfolder-article-marker
))
774 (active (or (cadr (assoc group nnfolder-group-alist
))
776 (scantime (assoc group nnfolder-scantime-alist
))
778 maxid start end newscantime
780 (buffer-disable-undo)
781 (setq maxid
(cdr active
))
782 (goto-char (point-min))
784 ;; Anytime the active number is 1 or 0, it is suspect. In that
785 ;; case, search the file manually to find the active number. Or,
786 ;; of course, if we're being paranoid. (This would also be the
787 ;; place to build other lists from the header markers, such as
788 ;; expunge lists, etc., if we ever desired to abandon the active
789 ;; file entirely for mboxes.)
790 (when (or nnfolder-ignore-active-file
792 (while (and (search-forward marker nil t
)
793 (re-search-forward number nil t
))
794 (let ((newnum (string-to-number (match-string 0))))
795 (if (nnmail-within-headers-p)
796 (setq maxid
(max maxid newnum
)
797 minid
(min minid newnum
)))))
798 (setcar active
(max 1 (min minid maxid
)))
799 (setcdr active
(max maxid
(cdr active
)))
800 (goto-char (point-min)))
802 ;; As long as we trust that the user will only insert unmarked mail
803 ;; at the end, go to the end and search backwards for the last
804 ;; marker. Find the start of that message, and begin to search for
805 ;; unmarked messages from there.
806 (when (not (or nnfolder-distrust-mbox
808 (goto-char (point-max))
809 (unless (re-search-backward marker nil t
)
810 (goto-char (point-min)))
811 (when (nnmail-search-unix-mail-delim)
812 (goto-char (point-min))))
814 ;; Keep track of the active number on our own, and insert it back
815 ;; into the active list when we're done. Also, prime the pump to
816 ;; cut down on the number of searches we do.
817 (unless (nnmail-search-unix-mail-delim)
818 (goto-char (point-max)))
819 (setq end
(point-marker))
820 (while (not (= end
(point-max)))
821 (setq start
(marker-position end
))
823 ;; There may be more than one "From " line, so we skip past
825 (while (looking-at delim
)
827 (set-marker end
(if (nnmail-search-unix-mail-delim)
831 (when (not (search-forward marker end t
))
832 (narrow-to-region start end
)
833 (nnmail-insert-lines)
834 (nnfolder-insert-newsgroup-line
835 (cons nil
(nnfolder-active-number nnfolder-current-group
)))
839 ;; Make absolutely sure that the active list reflects reality!
840 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file
)
841 ;; Set the scantime for this group.
842 (setq newscantime
(visited-file-modtime))
844 (setcdr scantime
(list newscantime
))
845 (push (list nnfolder-current-group newscantime
)
846 nnfolder-scantime-alist
))
847 (current-buffer))))))
850 (defun nnfolder-generate-active-file ()
851 "Look for mbox folders in the nnfolder directory and make them into groups.
852 This command does not work if you use short group names."
854 (nnmail-activate 'nnfolder
)
855 (let ((files (directory-files nnfolder-directory
))
857 (while (setq file
(pop files
))
858 (when (and (not (backup-file-name-p file
))
859 (message-mail-file-mbox-p
860 (nnheader-concat nnfolder-directory file
)))
861 (let ((oldgroup (assoc file nnfolder-group-alist
)))
863 (nnheader-message 5 "Refreshing group %s..." file
)
864 (nnheader-message 5 "Adding group %s..." file
))
866 (setq nnfolder-group-alist
867 (delq oldgroup
(copy-sequence nnfolder-group-alist
))))
868 (push (list file
(cons 1 0)) nnfolder-group-alist
)
869 (nnfolder-possibly-change-folder file
)
870 (nnfolder-possibly-change-group file
)
871 (nnfolder-close-group file
))))
872 (nnheader-message 5 "")))
874 (defun nnfolder-group-pathname (group)
875 "Make file name for GROUP."
877 (mm-encode-coding-string group nnmail-pathname-coding-system
))
878 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory
))))
879 ;; If this file exists, we use it directly.
880 (if (or nnmail-use-long-file-names
881 (file-exists-p (concat dir group
)))
883 ;; If not, we translate dots into slashes.
884 (concat dir
(nnheader-replace-chars-in-string group ?. ?
/)))))
886 (defun nnfolder-save-buffer ()
888 (when (buffer-modified-p)
889 (run-hooks 'nnfolder-save-buffer-hook
)
890 (gnus-make-directory (file-name-directory (buffer-file-name)))
891 (let ((coding-system-for-write
892 (or nnfolder-file-coding-system-for-write
893 nnfolder-file-coding-system
)))
896 (defun nnfolder-save-active (group-alist active-file
)
897 (let ((nnmail-active-file-coding-system
898 (or nnfolder-active-file-coding-system-for-write
899 nnfolder-active-file-coding-system
)))
900 (nnmail-save-active group-alist active-file
)))
904 ;;; nnfolder.el ends here