(truncate-string-to-width):
[emacs.git] / lisp / gnus / nnfolder.el
blobe7817e3af511609eb1344c77453a53f6bb0d47e2
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
4 ;; Author: Scott Byer <byer@mv.us.adobe.com>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Keywords: mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;;; Code:
30 (require 'nnheader)
31 (require 'message)
32 (require 'nnmail)
33 (require 'nnoo)
34 (require 'cl)
35 (require 'gnus-util)
37 (nnoo-declare nnfolder)
39 (defvoo nnfolder-directory (expand-file-name message-directory)
40 "The name of the nnfolder directory.")
42 (defvoo nnfolder-active-file
43 (nnheader-concat nnfolder-directory "active")
44 "The name of the active file.")
46 ;; I renamed this variable to something more in keeping with the general GNU
47 ;; style. -SLB
49 (defvoo nnfolder-ignore-active-file nil
50 "If non-nil, causes nnfolder to do some extra work in order to determine
51 the true active ranges of an mbox file. Note that the active file is still
52 saved, but it's values are not used. This costs some extra time when
53 scanning an mbox when opening it.")
55 (defvoo nnfolder-distrust-mbox nil
56 "If non-nil, causes nnfolder to not trust the user with respect to
57 inserting unaccounted for mail in the middle of an mbox file. This can greatly
58 slow down scans, which now must scan the entire file for unmarked messages.
59 When nil, scans occur forward from the last marked message, a huge
60 time saver for large mailboxes.")
62 (defvoo nnfolder-newsgroups-file
63 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
64 "Mail newsgroups description file.")
66 (defvoo nnfolder-get-new-mail t
67 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
69 (defvoo nnfolder-prepare-save-mail-hook nil
70 "Hook run narrowed to an article before saving.")
72 (defvoo nnfolder-save-buffer-hook nil
73 "Hook run before saving the nnfolder mbox buffer.")
75 (defvoo nnfolder-inhibit-expiry nil
76 "If non-nil, inhibit expiry.")
80 (defconst nnfolder-version "nnfolder 1.0"
81 "nnfolder version.")
83 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
84 "String used to demarcate what the article number for a message is.")
86 (defvoo nnfolder-current-group nil)
87 (defvoo nnfolder-current-buffer nil)
88 (defvoo nnfolder-status-string "")
89 (defvoo nnfolder-group-alist nil)
90 (defvoo nnfolder-buffer-alist nil)
91 (defvoo nnfolder-scantime-alist nil)
92 (defvoo nnfolder-active-timestamp nil)
96 ;;; Interface functions
98 (nnoo-define-basics nnfolder)
100 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
101 (save-excursion
102 (set-buffer nntp-server-buffer)
103 (erase-buffer)
104 (let (article art-string start stop)
105 (nnfolder-possibly-change-group group server)
106 (when nnfolder-current-buffer
107 (set-buffer nnfolder-current-buffer)
108 (goto-char (point-min))
109 (if (stringp (car articles))
110 'headers
111 (while articles
112 (setq article (car articles))
113 (setq art-string (nnfolder-article-string article))
114 (set-buffer nnfolder-current-buffer)
115 (when (or (search-forward art-string nil t)
116 ;; Don't search the whole file twice! Also, articles
117 ;; probably have some locality by number, so searching
118 ;; backwards will be faster. Especially if we're at the
119 ;; beginning of the buffer :-). -SLB
120 (search-backward art-string nil t))
121 (nnmail-search-unix-mail-delim-backward)
122 (setq start (point))
123 (search-forward "\n\n" nil t)
124 (setq stop (1- (point)))
125 (set-buffer nntp-server-buffer)
126 (insert (format "221 %d Article retrieved.\n" article))
127 (insert-buffer-substring nnfolder-current-buffer start stop)
128 (goto-char (point-max))
129 (insert ".\n"))
130 (setq articles (cdr articles)))
132 (set-buffer nntp-server-buffer)
133 (nnheader-fold-continuation-lines)
134 'headers)))))
136 (deffoo nnfolder-open-server (server &optional defs)
137 (nnoo-change-server 'nnfolder server defs)
138 (nnmail-activate 'nnfolder t)
139 (gnus-make-directory nnfolder-directory)
140 (cond
141 ((not (file-exists-p nnfolder-directory))
142 (nnfolder-close-server)
143 (nnheader-report 'nnfolder "Couldn't create directory: %s"
144 nnfolder-directory))
145 ((not (file-directory-p (file-truename nnfolder-directory)))
146 (nnfolder-close-server)
147 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
149 (nnmail-activate 'nnfolder)
150 (nnheader-report 'nnfolder "Opened server %s using directory %s"
151 server nnfolder-directory)
152 t)))
154 (deffoo nnfolder-request-close ()
155 (let ((alist nnfolder-buffer-alist))
156 (while alist
157 (nnfolder-close-group (caar alist) nil t)
158 (setq alist (cdr alist))))
159 (nnoo-close-server 'nnfolder)
160 (setq nnfolder-buffer-alist nil
161 nnfolder-group-alist nil))
163 (deffoo nnfolder-request-article (article &optional group server buffer)
164 (nnfolder-possibly-change-group group server)
165 (save-excursion
166 (set-buffer nnfolder-current-buffer)
167 (goto-char (point-min))
168 (when (search-forward (nnfolder-article-string article) nil t)
169 (let (start stop)
170 (nnmail-search-unix-mail-delim-backward)
171 (setq start (point))
172 (forward-line 1)
173 (unless (and (nnmail-search-unix-mail-delim)
174 (forward-line -1))
175 (goto-char (point-max)))
176 (setq stop (point))
177 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
178 (set-buffer nntp-server-buffer)
179 (erase-buffer)
180 (insert-buffer-substring nnfolder-current-buffer start stop)
181 (goto-char (point-min))
182 (while (looking-at "From ")
183 (delete-char 5)
184 (insert "X-From-Line: ")
185 (forward-line 1))
186 (if (numberp article)
187 (cons nnfolder-current-group article)
188 (goto-char (point-min))
189 (search-forward (concat "\n" nnfolder-article-marker))
190 (cons nnfolder-current-group
191 (string-to-int
192 (buffer-substring
193 (point) (progn (end-of-line) (point)))))))))))
195 (deffoo nnfolder-request-group (group &optional server dont-check)
196 (nnfolder-possibly-change-group group server t)
197 (save-excursion
198 (if (not (assoc group nnfolder-group-alist))
199 (nnheader-report 'nnfolder "No such group: %s" group)
200 (if dont-check
201 (progn
202 (nnheader-report 'nnfolder "Selected group %s" group)
204 (let* ((active (assoc group nnfolder-group-alist))
205 (group (car active))
206 (range (cadr active)))
207 (cond
208 ((null active)
209 (nnheader-report 'nnfolder "No such group: %s" group))
210 ((null nnfolder-current-group)
211 (nnheader-report 'nnfolder "Empty group: %s" group))
213 (nnheader-report 'nnfolder "Selected group %s" group)
214 (nnheader-insert "211 %d %d %d %s\n"
215 (1+ (- (cdr range) (car range)))
216 (car range) (cdr range) group))))))))
218 (deffoo nnfolder-request-scan (&optional group server)
219 (nnfolder-possibly-change-group nil server)
220 (when nnfolder-get-new-mail
221 (nnfolder-possibly-change-group group server)
222 (nnmail-get-new-mail
223 'nnfolder
224 (lambda ()
225 (let ((bufs nnfolder-buffer-alist))
226 (save-excursion
227 (while bufs
228 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
229 (setq nnfolder-buffer-alist
230 (delq (car bufs) nnfolder-buffer-alist))
231 (set-buffer (nth 1 (car bufs)))
232 (nnfolder-save-buffer)
233 (kill-buffer (current-buffer)))
234 (setq bufs (cdr bufs))))))
235 nnfolder-directory
236 group)))
238 ;; Don't close the buffer if we're not shutting down the server. This way,
239 ;; we can keep the buffer in the group buffer cache, and not have to grovel
240 ;; over the buffer again unless we add new mail to it or modify it in some
241 ;; way.
243 (deffoo nnfolder-close-group (group &optional server force)
244 ;; Make sure we _had_ the group open.
245 (when (or (assoc group nnfolder-buffer-alist)
246 (equal group nnfolder-current-group))
247 (let ((inf (assoc group nnfolder-buffer-alist)))
248 (when inf
249 (when (and nnfolder-current-group
250 nnfolder-current-buffer)
251 (push (list nnfolder-current-group nnfolder-current-buffer)
252 nnfolder-buffer-alist))
253 (setq nnfolder-buffer-alist
254 (delq inf nnfolder-buffer-alist))
255 (setq nnfolder-current-buffer (cadr inf)
256 nnfolder-current-group (car inf))))
257 (when (and nnfolder-current-buffer
258 (buffer-name nnfolder-current-buffer))
259 (save-excursion
260 (set-buffer nnfolder-current-buffer)
261 ;; If the buffer was modified, write the file out now.
262 (nnfolder-save-buffer)
263 ;; If we're shutting the server down, we need to kill the
264 ;; buffer and remove it from the open buffer list. Or, of
265 ;; course, if we're trying to minimize our space impact.
266 (kill-buffer (current-buffer))
267 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
268 nnfolder-buffer-alist)))))
269 (setq nnfolder-current-group nil
270 nnfolder-current-buffer nil)
273 (deffoo nnfolder-request-create-group (group &optional server args)
274 (nnfolder-possibly-change-group nil server)
275 (nnmail-activate 'nnfolder)
276 (when group
277 (unless (assoc group nnfolder-group-alist)
278 (push (list group (cons 1 0)) nnfolder-group-alist)
279 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
282 (deffoo nnfolder-request-list (&optional server)
283 (nnfolder-possibly-change-group nil server)
284 (save-excursion
285 ;; 1997/8/14 by MORIOKA Tomohiko
286 ;; for XEmacs/mule.
287 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
288 (pathname-coding-system 'binary)) ; for XEmacs/mule
289 (nnmail-find-file nnfolder-active-file)
290 (setq nnfolder-group-alist (nnmail-get-active)))
293 (deffoo nnfolder-request-newgroups (date &optional server)
294 (nnfolder-possibly-change-group nil server)
295 (nnfolder-request-list server))
297 (deffoo nnfolder-request-list-newsgroups (&optional server)
298 (nnfolder-possibly-change-group nil server)
299 (save-excursion
300 (nnmail-find-file nnfolder-newsgroups-file)))
302 (deffoo nnfolder-request-expire-articles
303 (articles newsgroup &optional server force)
304 (nnfolder-possibly-change-group newsgroup server)
305 (let* ((is-old t)
306 rest)
307 (nnmail-activate 'nnfolder)
309 (save-excursion
310 (set-buffer nnfolder-current-buffer)
311 (while (and articles is-old)
312 (goto-char (point-min))
313 (when (search-forward (nnfolder-article-string (car articles)) nil t)
314 (if (setq is-old
315 (nnmail-expired-article-p
316 newsgroup
317 (buffer-substring
318 (point) (progn (end-of-line) (point)))
319 force nnfolder-inhibit-expiry))
320 (progn
321 (nnheader-message 5 "Deleting article %d..."
322 (car articles) newsgroup)
323 (nnfolder-delete-mail))
324 (push (car articles) rest)))
325 (setq articles (cdr articles)))
326 (unless nnfolder-inhibit-expiry
327 (nnheader-message 5 "Deleting articles...done"))
328 (nnfolder-save-buffer)
329 (nnfolder-adjust-min-active newsgroup)
330 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
331 (nconc rest articles))))
333 (deffoo nnfolder-request-move-article
334 (article group server accept-form &optional last)
335 (let ((buf (get-buffer-create " *nnfolder move*"))
336 result)
337 (and
338 (nnfolder-request-article article group server)
339 (save-excursion
340 (set-buffer buf)
341 (buffer-disable-undo (current-buffer))
342 (erase-buffer)
343 (insert-buffer-substring nntp-server-buffer)
344 (goto-char (point-min))
345 (while (re-search-forward
346 (concat "^" nnfolder-article-marker)
347 (save-excursion (search-forward "\n\n" nil t) (point)) t)
348 (delete-region (progn (beginning-of-line) (point))
349 (progn (forward-line 1) (point))))
350 (setq result (eval accept-form))
351 (kill-buffer buf)
352 result)
353 (save-excursion
354 (nnfolder-possibly-change-group group server)
355 (set-buffer nnfolder-current-buffer)
356 (goto-char (point-min))
357 (when (search-forward (nnfolder-article-string article) nil t)
358 (nnfolder-delete-mail))
359 (when last
360 (nnfolder-save-buffer)
361 (nnfolder-adjust-min-active group)
362 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))))
363 result))
365 (deffoo nnfolder-request-accept-article (group &optional server last)
366 (nnfolder-possibly-change-group group server)
367 (nnmail-check-syntax)
368 (let ((buf (current-buffer))
369 result art-group)
370 (goto-char (point-min))
371 (when (looking-at "X-From-Line: ")
372 (replace-match "From "))
373 (and
374 (nnfolder-request-list)
375 (save-excursion
376 (set-buffer buf)
377 (goto-char (point-min))
378 (search-forward "\n\n" nil t)
379 (forward-line -1)
380 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
381 (delete-region (point) (progn (forward-line 1) (point))))
382 (when nnmail-cache-accepted-message-ids
383 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
384 (setq result (if (stringp group)
385 (list (cons group (nnfolder-active-number group)))
386 (setq art-group
387 (nnmail-article-group 'nnfolder-active-number))))
388 (if (and (null result)
389 (yes-or-no-p "Moved to `junk' group; delete article? "))
390 (setq result 'junk)
391 (setq result
392 (car (nnfolder-save-mail result)))))
393 (when last
394 (save-excursion
395 (nnfolder-possibly-change-folder (or (caar art-group) group))
396 (nnfolder-save-buffer)
397 (when nnmail-cache-accepted-message-ids
398 (nnmail-cache-close)))))
399 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
400 (unless result
401 (nnheader-report 'nnfolder "Couldn't store article"))
402 result))
404 (deffoo nnfolder-request-replace-article (article group buffer)
405 (nnfolder-possibly-change-group group)
406 (save-excursion
407 (set-buffer nnfolder-current-buffer)
408 (goto-char (point-min))
409 (if (not (search-forward (nnfolder-article-string article) nil t))
411 (nnfolder-delete-mail t t)
412 (insert-buffer-substring buffer)
413 (nnfolder-save-buffer)
414 t)))
416 (deffoo nnfolder-request-delete-group (group &optional force server)
417 (nnfolder-close-group group server t)
418 ;; Delete all articles in GROUP.
419 (if (not force)
420 () ; Don't delete the articles.
421 ;; Delete the file that holds the group.
422 (ignore-errors
423 (delete-file (nnfolder-group-pathname group))))
424 ;; Remove the group from all structures.
425 (setq nnfolder-group-alist
426 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
427 nnfolder-current-group nil
428 nnfolder-current-buffer nil)
429 ;; Save the active file.
430 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
433 (deffoo nnfolder-request-rename-group (group new-name &optional server)
434 (nnfolder-possibly-change-group group server)
435 (save-excursion
436 (set-buffer nnfolder-current-buffer)
437 (and (file-writable-p buffer-file-name)
438 (ignore-errors
439 (rename-file
440 buffer-file-name
441 (nnfolder-group-pathname new-name))
443 ;; That went ok, so we change the internal structures.
444 (let ((entry (assoc group nnfolder-group-alist)))
445 (and entry (setcar entry new-name))
446 (setq nnfolder-current-buffer nil
447 nnfolder-current-group nil)
448 ;; Save the new group alist.
449 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
450 ;; We kill the buffer instead of renaming it and stuff.
451 (kill-buffer (current-buffer))
452 t))))
455 ;;; Internal functions.
457 (defun nnfolder-adjust-min-active (group)
458 ;; Find the lowest active article in this group.
459 (let* ((active (cadr (assoc group nnfolder-group-alist)))
460 (marker (concat "\n" nnfolder-article-marker))
461 (number "[0-9]+")
462 (activemin (cdr active)))
463 (save-excursion
464 (set-buffer nnfolder-current-buffer)
465 (goto-char (point-min))
466 (while (and (search-forward marker nil t)
467 (re-search-forward number nil t))
468 (setq activemin (min activemin
469 (string-to-number (buffer-substring
470 (match-beginning 0)
471 (match-end 0))))))
472 (setcar active activemin))))
474 (defun nnfolder-article-string (article)
475 (if (numberp article)
476 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
477 (concat "\nMessage-ID: " article)))
479 (defun nnfolder-delete-mail (&optional force leave-delim)
480 "Delete the message that point is in."
481 (save-excursion
482 (delete-region
483 (save-excursion
484 (nnmail-search-unix-mail-delim-backward)
485 (if leave-delim (progn (forward-line 1) (point))
486 (point)))
487 (progn
488 (forward-line 1)
489 (if (nnmail-search-unix-mail-delim)
490 (if (and (not (bobp)) leave-delim)
491 (progn (forward-line -2) (point))
492 (point))
493 (point-max))))))
495 (defun nnfolder-possibly-change-group (group &optional server dont-check)
496 ;; Change servers.
497 (when (and server
498 (not (nnfolder-server-opened server)))
499 (nnfolder-open-server server))
500 (unless (gnus-buffer-live-p nnfolder-current-buffer)
501 (setq nnfolder-current-buffer nil
502 nnfolder-current-group nil))
503 ;; Change group.
504 (when (and group
505 (not (equal group nnfolder-current-group)))
506 ;; 1997/8/14 by MORIOKA Tomohiko
507 ;; for XEmacs/mule.
508 (let ((pathname-coding-system 'binary))
509 (nnmail-activate 'nnfolder)
510 (when (and (not (assoc group nnfolder-group-alist))
511 (not (file-exists-p
512 (nnfolder-group-pathname group))))
513 ;; The group doesn't exist, so we create a new entry for it.
514 (push (list group (cons 1 0)) nnfolder-group-alist)
515 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
517 (if dont-check
518 (setq nnfolder-current-group group)
519 (let (inf file)
520 ;; If we have to change groups, see if we don't already have the
521 ;; folder in memory. If we do, verify the modtime and destroy
522 ;; the folder if needed so we can rescan it.
523 (when (setq inf (assoc group nnfolder-buffer-alist))
524 (setq nnfolder-current-buffer (nth 1 inf)))
526 ;; If the buffer is not live, make sure it isn't in the alist. If it
527 ;; is live, verify that nobody else has touched the file since last
528 ;; time.
529 (when (and nnfolder-current-buffer
530 (not (gnus-buffer-live-p nnfolder-current-buffer)))
531 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
532 nnfolder-current-buffer nil))
534 (setq nnfolder-current-group group)
536 (when (or (not nnfolder-current-buffer)
537 (not (verify-visited-file-modtime nnfolder-current-buffer)))
538 (save-excursion
539 (setq file (nnfolder-group-pathname group))
540 ;; See whether we need to create the new file.
541 (unless (file-exists-p file)
542 (gnus-make-directory (file-name-directory file))
543 (nnmail-write-region 1 1 file t 'nomesg))
544 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
545 (set-buffer nnfolder-current-buffer)
546 (push (list group nnfolder-current-buffer)
547 nnfolder-buffer-alist)))))))))
549 (defun nnfolder-save-mail (group-art-list)
550 "Called narrowed to an article."
551 (let* (save-list group-art)
552 (goto-char (point-min))
553 ;; The From line may have been quoted by movemail.
554 (when (looking-at (concat ">" message-unix-mail-delimiter))
555 (delete-char 1))
556 ;; This might come from somewhere else.
557 (unless (looking-at message-unix-mail-delimiter)
558 (insert "From nobody " (current-time-string) "\n")
559 (goto-char (point-min)))
560 ;; Quote all "From " lines in the article.
561 (forward-line 1)
562 (let (case-fold-search)
563 (while (re-search-forward "^From " nil t)
564 (beginning-of-line)
565 (insert "> ")))
566 (setq save-list group-art-list)
567 (nnmail-insert-lines)
568 (nnmail-insert-xref group-art-list)
569 (run-hooks 'nnmail-prepare-save-mail-hook)
570 (run-hooks 'nnfolder-prepare-save-mail-hook)
572 ;; Insert the mail into each of the destination groups.
573 (while (setq group-art (pop group-art-list))
574 ;; Kill any previous newsgroup markers.
575 (goto-char (point-min))
576 (search-forward "\n\n" nil t)
577 (forward-line -1)
578 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
579 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
581 ;; Insert the new newsgroup marker.
582 (nnfolder-insert-newsgroup-line group-art)
584 (save-excursion
585 (let ((beg (point-min))
586 (end (point-max))
587 (obuf (current-buffer)))
588 (nnfolder-possibly-change-folder (car group-art))
589 (let ((buffer-read-only nil))
590 (goto-char (point-max))
591 (unless (eolp)
592 (insert "\n"))
593 (unless (bobp)
594 (insert "\n"))
595 (insert-buffer-substring obuf beg end)))))
597 ;; Did we save it anywhere?
598 save-list))
600 (defun nnfolder-insert-newsgroup-line (group-art)
601 (save-excursion
602 (goto-char (point-min))
603 (when (search-forward "\n\n" nil t)
604 (forward-char -1)
605 (insert (format (concat nnfolder-article-marker "%d %s\n")
606 (cdr group-art) (current-time-string))))))
608 (defun nnfolder-active-number (group)
609 ;; Find the next article number in GROUP.
610 (let ((active (cadr (assoc group nnfolder-group-alist))))
611 (if active
612 (setcdr active (1+ (cdr active)))
613 ;; This group is new, so we create a new entry for it.
614 ;; This might be a bit naughty... creating groups on the drop of
615 ;; a hat, but I don't know...
616 (push (list group (setq active (cons 1 1)))
617 nnfolder-group-alist))
618 (cdr active)))
620 (defun nnfolder-possibly-change-folder (group)
621 (let ((inf (assoc group nnfolder-buffer-alist)))
622 (if (and inf
623 (gnus-buffer-live-p (cadr inf)))
624 (set-buffer (cadr inf))
625 (when inf
626 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
627 (when nnfolder-group-alist
628 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
629 (push (list group (nnfolder-read-folder group))
630 nnfolder-buffer-alist))))
632 ;; This method has a problem if you've accidentally let the active list get
633 ;; out of sync with the files. This could happen, say, if you've
634 ;; accidentally gotten new mail with something other than Gnus (but why
635 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
636 ;; processing the file, ready to add new X-Gnus article number markers, and
637 ;; we'll run across a message with no ID yet - the active list _may_not_ be
638 ;; ready for us yet.
640 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
641 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
642 ;; rest of the message looking for any more, possibly higher IDs. We'll
643 ;; assume the maximum that we find is the highest active. Note that this
644 ;; shouldn't cost us much extra time at all, but will be a lot less
645 ;; vulnerable to glitches between the mbox and the active file.
647 (defun nnfolder-read-folder (group)
648 (let* ((file (nnfolder-group-pathname group))
649 (buffer (set-buffer (nnheader-find-file-noselect file))))
650 (if (equal (cadr (assoc group nnfolder-scantime-alist))
651 (nth 5 (file-attributes file)))
652 ;; This looks up-to-date, so we don't do any scanning.
653 buffer
654 ;; Parse the damn thing.
655 (save-excursion
656 (nnmail-activate 'nnfolder)
657 ;; Read in the file.
658 (let ((delim (concat "^" message-unix-mail-delimiter))
659 (marker (concat "\n" nnfolder-article-marker))
660 (number "[0-9]+")
661 (active (or (cadr (assoc group nnfolder-group-alist))
662 (cons 1 0)))
663 (scantime (assoc group nnfolder-scantime-alist))
664 (minid (lsh -1 -1))
665 maxid start end newscantime
666 buffer-read-only)
667 (buffer-disable-undo (current-buffer))
668 (setq maxid (cdr active))
669 (goto-char (point-min))
671 ;; Anytime the active number is 1 or 0, it is suspect. In that
672 ;; case, search the file manually to find the active number. Or,
673 ;; of course, if we're being paranoid. (This would also be the
674 ;; place to build other lists from the header markers, such as
675 ;; expunge lists, etc., if we ever desired to abandon the active
676 ;; file entirely for mboxes.)
677 (when (or nnfolder-ignore-active-file
678 (< maxid 2))
679 (while (and (search-forward marker nil t)
680 (re-search-forward number nil t))
681 (let ((newnum (string-to-number (match-string 0))))
682 (setq maxid (max maxid newnum))
683 (setq minid (min minid newnum))))
684 (setcar active (max 1 (min minid maxid)))
685 (setcdr active (max maxid (cdr active)))
686 (goto-char (point-min)))
688 ;; As long as we trust that the user will only insert unmarked mail
689 ;; at the end, go to the end and search backwards for the last
690 ;; marker. Find the start of that message, and begin to search for
691 ;; unmarked messages from there.
692 (when (not (or nnfolder-distrust-mbox
693 (< maxid 2)))
694 (goto-char (point-max))
695 (unless (re-search-backward marker nil t)
696 (goto-char (point-min)))
697 (when (nnmail-search-unix-mail-delim)
698 (goto-char (point-min))))
700 ;; Keep track of the active number on our own, and insert it back
701 ;; into the active list when we're done. Also, prime the pump to
702 ;; cut down on the number of searches we do.
703 (unless (nnmail-search-unix-mail-delim)
704 (goto-char (point-max)))
705 (setq end (point-marker))
706 (while (not (= end (point-max)))
707 (setq start (marker-position end))
708 (goto-char end)
709 ;; There may be more than one "From " line, so we skip past
710 ;; them.
711 (while (looking-at delim)
712 (forward-line 1))
713 (set-marker end (if (nnmail-search-unix-mail-delim)
714 (point)
715 (point-max)))
716 (goto-char start)
717 (when (not (search-forward marker end t))
718 (narrow-to-region start end)
719 (nnmail-insert-lines)
720 (nnfolder-insert-newsgroup-line
721 (cons nil (nnfolder-active-number nnfolder-current-group)))
722 (widen)))
724 (set-marker end nil)
725 ;; Make absolutely sure that the active list reflects reality!
726 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
727 ;; Set the scantime for this group.
728 (setq newscantime (visited-file-modtime))
729 (if scantime
730 (setcdr scantime (list newscantime))
731 (push (list nnfolder-current-group newscantime)
732 nnfolder-scantime-alist))
733 (current-buffer))))))
735 ;;;###autoload
736 (defun nnfolder-generate-active-file ()
737 "Look for mbox folders in the nnfolder directory and make them into groups."
738 (interactive)
739 (nnmail-activate 'nnfolder)
740 (let ((files (directory-files nnfolder-directory))
741 file)
742 (while (setq file (pop files))
743 (when (and (not (backup-file-name-p file))
744 (message-mail-file-mbox-p
745 (nnheader-concat nnfolder-directory file)))
746 (let ((oldgroup (assoc file nnfolder-group-alist)))
747 (if oldgroup
748 (nnheader-message 5 "Refreshing group %s..." file)
749 (nnheader-message 5 "Adding group %s..." file))
750 (if oldgroup
751 (setq nnfolder-group-alist
752 (delq oldgroup (copy-sequence nnfolder-group-alist))))
753 (push (list file (cons 1 0)) nnfolder-group-alist)
754 (nnfolder-possibly-change-folder file)
755 (nnfolder-possibly-change-group file)
756 (nnfolder-close-group file))))
757 (message "")))
759 (defun nnfolder-group-pathname (group)
760 "Make pathname for GROUP."
761 ;; 1997/8/14 by MORIOKA Tomohiko
762 ;; encode file name for Emacs 20.
763 (setq group (encode-coding-string group nnmail-pathname-coding-system))
764 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
765 ;; If this file exists, we use it directly.
766 (if (or nnmail-use-long-file-names
767 (file-exists-p (concat dir group)))
768 (concat dir group)
769 ;; If not, we translate dots into slashes.
770 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
772 (defun nnfolder-save-buffer ()
773 "Save the buffer."
774 (when (buffer-modified-p)
775 (run-hooks 'nnfolder-save-buffer-hook)
776 (save-buffer)))
778 (provide 'nnfolder)
780 ;;; nnfolder.el ends here