comment
[bpt/emacs.git] / lisp / gnus / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
4
5 ;; Author: Scott Byer <byer@mv.us.adobe.com>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: mail
9
10 ;; This file is part of GNU Emacs.
11
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)
15 ;; any later version.
16
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.
21
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.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (require 'nnheader)
32 (require 'message)
33 (require 'nnmail)
34 (require 'nnoo)
35 (eval-when-compile (require 'cl))
36 (require 'gnus-util)
37
38 (nnoo-declare nnfolder)
39
40 (defvoo nnfolder-directory (expand-file-name message-directory)
41 "The name of the nnfolder directory.")
42
43 (defvoo nnfolder-active-file
44 (nnheader-concat nnfolder-directory "active")
45 "The name of the active file.")
46
47 ;; I renamed this variable to something more in keeping with the general GNU
48 ;; style. -SLB
49
50 (defvoo nnfolder-ignore-active-file nil
51 "If non-nil, the active file is ignored.
52 This causes nnfolder to do some extra work in order to determine the
53 true active ranges of an mbox file. Note that the active file is
54 still saved, but its values are not used. This costs some extra time
55 when scanning an mbox when opening it.")
56
57 (defvoo nnfolder-distrust-mbox nil
58 "If non-nil, the folder will be distrusted.
59 This means that nnfolder will not trust the user with respect to
60 inserting unaccounted for mail in the middle of an mbox file. This
61 can greatly slow down scans, which now must scan the entire file for
62 unmarked messages. When nil, scans occur forward from the last marked
63 message, a huge time saver for large mailboxes.")
64
65 (defvoo nnfolder-newsgroups-file
66 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
67 "Mail newsgroups description file.")
68
69 (defvoo nnfolder-get-new-mail t
70 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
71
72 (defvoo nnfolder-prepare-save-mail-hook nil
73 "Hook run narrowed to an article before saving.")
74
75 (defvoo nnfolder-save-buffer-hook nil
76 "Hook run before saving the nnfolder mbox buffer.")
77
78 (defvoo nnfolder-inhibit-expiry nil
79 "If non-nil, inhibit expiry.")
80
81 \f
82
83 (defconst nnfolder-version "nnfolder 1.0"
84 "nnfolder version.")
85
86 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
87 "String used to demarcate what the article number for a message is.")
88
89 (defvoo nnfolder-current-group nil)
90 (defvoo nnfolder-current-buffer nil)
91 (defvoo nnfolder-status-string "")
92 (defvoo nnfolder-group-alist nil)
93 (defvoo nnfolder-buffer-alist nil)
94 (defvoo nnfolder-scantime-alist nil)
95 (defvoo nnfolder-active-timestamp nil)
96 (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
97 (defvoo nnfolder-active-file-coding-system-for-write
98 nnmail-active-file-coding-system)
99 (defvoo nnfolder-file-coding-system mm-text-coding-system)
100 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
101 "Coding system for save nnfolder file.
102 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
103
104 \f
105
106 ;;; Interface functions
107
108 (nnoo-define-basics nnfolder)
109
110 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
111 (save-excursion
112 (set-buffer nntp-server-buffer)
113 (erase-buffer)
114 (let (article start stop)
115 (nnfolder-possibly-change-group group server)
116 (when nnfolder-current-buffer
117 (set-buffer nnfolder-current-buffer)
118 (goto-char (point-min))
119 (if (stringp (car articles))
120 'headers
121 (while (setq article (pop articles))
122 (set-buffer nnfolder-current-buffer)
123 (when (nnfolder-goto-article article)
124 (setq start (point))
125 (setq stop (if (search-forward "\n\n" nil t)
126 (1- (point))
127 (point-max)))
128 (set-buffer nntp-server-buffer)
129 (insert (format "221 %d Article retrieved.\n" article))
130 (insert-buffer-substring nnfolder-current-buffer start stop)
131 (goto-char (point-max))
132 (insert ".\n")))
133
134 (set-buffer nntp-server-buffer)
135 (nnheader-fold-continuation-lines)
136 'headers)))))
137
138 (deffoo nnfolder-open-server (server &optional defs)
139 (nnoo-change-server 'nnfolder server defs)
140 (nnmail-activate 'nnfolder t)
141 (gnus-make-directory nnfolder-directory)
142 (cond
143 ((not (file-exists-p nnfolder-directory))
144 (nnfolder-close-server)
145 (nnheader-report 'nnfolder "Couldn't create directory: %s"
146 nnfolder-directory))
147 ((not (file-directory-p (file-truename nnfolder-directory)))
148 (nnfolder-close-server)
149 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
150 (t
151 (nnmail-activate 'nnfolder)
152 (nnheader-report 'nnfolder "Opened server %s using directory %s"
153 server nnfolder-directory)
154 t)))
155
156 (deffoo nnfolder-request-close ()
157 (let ((alist nnfolder-buffer-alist))
158 (while alist
159 (nnfolder-close-group (caar alist) nil t)
160 (setq alist (cdr alist))))
161 (nnoo-close-server 'nnfolder)
162 (setq nnfolder-buffer-alist nil
163 nnfolder-group-alist nil))
164
165 (deffoo nnfolder-request-article (article &optional group server buffer)
166 (nnfolder-possibly-change-group group server)
167 (save-excursion
168 (set-buffer nnfolder-current-buffer)
169 (goto-char (point-min))
170 (when (nnfolder-goto-article article)
171 (let (start stop)
172 (setq start (point))
173 (forward-line 1)
174 (unless (and (nnmail-search-unix-mail-delim)
175 (forward-line -1))
176 (goto-char (point-max)))
177 (setq stop (point))
178 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
179 (set-buffer nntp-server-buffer)
180 (erase-buffer)
181 (insert-buffer-substring nnfolder-current-buffer start stop)
182 (goto-char (point-min))
183 (while (looking-at "From ")
184 (delete-char 5)
185 (insert "X-From-Line: ")
186 (forward-line 1))
187 (if (numberp article)
188 (cons nnfolder-current-group article)
189 (goto-char (point-min))
190 (cons nnfolder-current-group
191 (if (search-forward (concat "\n" nnfolder-article-marker)
192 nil t)
193 (string-to-int
194 (buffer-substring
195 (point) (progn (end-of-line) (point))))
196 -1))))))))
197
198 (deffoo nnfolder-request-group (group &optional server dont-check)
199 (nnfolder-possibly-change-group group server t)
200 (save-excursion
201 (if (not (assoc group nnfolder-group-alist))
202 (nnheader-report 'nnfolder "No such group: %s" group)
203 (if dont-check
204 (progn
205 (nnheader-report 'nnfolder "Selected group %s" group)
206 t)
207 (let* ((active (assoc group nnfolder-group-alist))
208 (group (car active))
209 (range (cadr active)))
210 (cond
211 ((null active)
212 (nnheader-report 'nnfolder "No such group: %s" group))
213 ((null nnfolder-current-group)
214 (nnheader-report 'nnfolder "Empty group: %s" group))
215 (t
216 (nnheader-report 'nnfolder "Selected group %s" group)
217 (nnheader-insert "211 %d %d %d %s\n"
218 (1+ (- (cdr range) (car range)))
219 (car range) (cdr range) group))))))))
220
221 (deffoo nnfolder-request-scan (&optional group server)
222 (nnfolder-possibly-change-group nil server)
223 (when nnfolder-get-new-mail
224 (nnfolder-possibly-change-group group server)
225 (nnmail-get-new-mail
226 'nnfolder
227 (lambda ()
228 (let ((bufs nnfolder-buffer-alist))
229 (save-excursion
230 (while bufs
231 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
232 (setq nnfolder-buffer-alist
233 (delq (car bufs) nnfolder-buffer-alist))
234 (set-buffer (nth 1 (car bufs)))
235 (nnfolder-save-buffer)
236 (kill-buffer (current-buffer)))
237 (setq bufs (cdr bufs))))))
238 nnfolder-directory
239 group)))
240
241 ;; Don't close the buffer if we're not shutting down the server. This way,
242 ;; we can keep the buffer in the group buffer cache, and not have to grovel
243 ;; over the buffer again unless we add new mail to it or modify it in some
244 ;; way.
245
246 (deffoo nnfolder-close-group (group &optional server force)
247 ;; Make sure we _had_ the group open.
248 (when (or (assoc group nnfolder-buffer-alist)
249 (equal group nnfolder-current-group))
250 (let ((inf (assoc group nnfolder-buffer-alist)))
251 (when inf
252 (when (and nnfolder-current-group
253 nnfolder-current-buffer)
254 (push (list nnfolder-current-group nnfolder-current-buffer)
255 nnfolder-buffer-alist))
256 (setq nnfolder-buffer-alist
257 (delq inf nnfolder-buffer-alist))
258 (setq nnfolder-current-buffer (cadr inf)
259 nnfolder-current-group (car inf))))
260 (when (and nnfolder-current-buffer
261 (buffer-name nnfolder-current-buffer))
262 (save-excursion
263 (set-buffer nnfolder-current-buffer)
264 ;; If the buffer was modified, write the file out now.
265 (nnfolder-save-buffer)
266 ;; If we're shutting the server down, we need to kill the
267 ;; buffer and remove it from the open buffer list. Or, of
268 ;; course, if we're trying to minimize our space impact.
269 (kill-buffer (current-buffer))
270 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
271 nnfolder-buffer-alist)))))
272 (setq nnfolder-current-group nil
273 nnfolder-current-buffer nil)
274 t)
275
276 (deffoo nnfolder-request-create-group (group &optional server args)
277 (nnfolder-possibly-change-group nil server)
278 (nnmail-activate 'nnfolder)
279 (when group
280 (unless (assoc group nnfolder-group-alist)
281 (push (list group (cons 1 0)) nnfolder-group-alist)
282 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
283 (nnfolder-read-folder group)))
284 t)
285
286 (deffoo nnfolder-request-list (&optional server)
287 (nnfolder-possibly-change-group nil server)
288 (save-excursion
289 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
290 (nnmail-find-file nnfolder-active-file)
291 (setq nnfolder-group-alist (nnmail-get-active)))
292 t))
293
294 (deffoo nnfolder-request-newgroups (date &optional server)
295 (nnfolder-possibly-change-group nil server)
296 (nnfolder-request-list server))
297
298 (deffoo nnfolder-request-list-newsgroups (&optional server)
299 (nnfolder-possibly-change-group nil server)
300 (save-excursion
301 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
302 (nnmail-find-file nnfolder-newsgroups-file))))
303
304 ;; Return a list consisting of all article numbers existing in the
305 ;; current folder.
306
307 (defun nnfolder-existing-articles ()
308 (save-excursion
309 (when nnfolder-current-buffer
310 (set-buffer nnfolder-current-buffer)
311 (goto-char (point-min))
312 (let ((marker (concat "\n" nnfolder-article-marker))
313 (number "[0-9]+")
314 numbers)
315
316 (while (and (search-forward marker nil t)
317 (re-search-forward number nil t))
318 (let ((newnum (string-to-number (match-string 0))))
319 (if (nnmail-within-headers-p)
320 (push newnum numbers))))
321 numbers))))
322
323 (deffoo nnfolder-request-expire-articles
324 (articles newsgroup &optional server force)
325 (nnfolder-possibly-change-group newsgroup server)
326 (let* ((is-old t)
327 ;; The articles we have deleted so far.
328 (deleted-articles nil)
329 ;; The articles that really exist and will
330 ;; be expired if they are old enough.
331 (maybe-expirable
332 (gnus-intersection articles (nnfolder-existing-articles))))
333 (nnmail-activate 'nnfolder)
334
335 (save-excursion
336 (set-buffer nnfolder-current-buffer)
337 ;; Since messages are sorted in arrival order and expired in the
338 ;; same order, we can stop as soon as we find a message that is
339 ;; too old.
340 (while (and maybe-expirable is-old)
341 (goto-char (point-min))
342 (when (and (nnfolder-goto-article (car maybe-expirable))
343 (search-forward (concat "\n" nnfolder-article-marker)
344 nil t))
345 (forward-sexp)
346 (when (setq is-old
347 (nnmail-expired-article-p
348 newsgroup
349 (buffer-substring
350 (point) (progn (end-of-line) (point)))
351 force nnfolder-inhibit-expiry))
352 (nnheader-message 5 "Deleting article %d..."
353 (car maybe-expirable) newsgroup)
354 (nnfolder-delete-mail)
355 ;; Must remember which articles were actually deleted
356 (push (car maybe-expirable) deleted-articles)))
357 (setq maybe-expirable (cdr maybe-expirable)))
358 (unless nnfolder-inhibit-expiry
359 (nnheader-message 5 "Deleting articles...done"))
360 (nnfolder-save-buffer)
361 (nnfolder-adjust-min-active newsgroup)
362 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
363 (gnus-sorted-complement articles (nreverse deleted-articles)))))
364
365 (deffoo nnfolder-request-move-article (article group server
366 accept-form &optional last)
367 (save-excursion
368 (let ((buf (get-buffer-create " *nnfolder move*"))
369 result)
370 (and
371 (nnfolder-request-article article group server)
372 (save-excursion
373 (set-buffer buf)
374 (erase-buffer)
375 (insert-buffer-substring nntp-server-buffer)
376 (goto-char (point-min))
377 (while (re-search-forward
378 (concat "^" nnfolder-article-marker)
379 (save-excursion (and (search-forward "\n\n" nil t) (point)))
380 t)
381 (delete-region (progn (beginning-of-line) (point))
382 (progn (forward-line 1) (point))))
383 (setq result (eval accept-form))
384 (kill-buffer buf)
385 result)
386 (save-excursion
387 (nnfolder-possibly-change-group group server)
388 (set-buffer nnfolder-current-buffer)
389 (goto-char (point-min))
390 (when (nnfolder-goto-article article)
391 (nnfolder-delete-mail))
392 (when last
393 (nnfolder-save-buffer)
394 (nnfolder-adjust-min-active group)
395 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
396 result)))
397
398 (deffoo nnfolder-request-accept-article (group &optional server last)
399 (save-excursion
400 (nnfolder-possibly-change-group group server)
401 (nnmail-check-syntax)
402 (let ((buf (current-buffer))
403 result art-group)
404 (goto-char (point-min))
405 (when (looking-at "X-From-Line: ")
406 (replace-match "From "))
407 (and
408 (nnfolder-request-list)
409 (save-excursion
410 (set-buffer buf)
411 (goto-char (point-min))
412 (if (search-forward "\n\n" nil t)
413 (forward-line -1)
414 (goto-char (point-max)))
415 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
416 (delete-region (point) (progn (forward-line 1) (point))))
417 (when nnmail-cache-accepted-message-ids
418 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
419 (setq result (if (stringp group)
420 (list (cons group (nnfolder-active-number group)))
421 (setq art-group
422 (nnmail-article-group 'nnfolder-active-number))))
423 (if (and (null result)
424 (yes-or-no-p "Moved to `junk' group; delete article? "))
425 (setq result 'junk)
426 (setq result
427 (car (nnfolder-save-mail result)))))
428 (when last
429 (save-excursion
430 (nnfolder-possibly-change-folder (or (caar art-group) group))
431 (nnfolder-save-buffer)
432 (when nnmail-cache-accepted-message-ids
433 (nnmail-cache-close)))))
434 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
435 (unless result
436 (nnheader-report 'nnfolder "Couldn't store article"))
437 result)))
438
439 (deffoo nnfolder-request-replace-article (article group buffer)
440 (nnfolder-possibly-change-group group)
441 (save-excursion
442 (set-buffer buffer)
443 (goto-char (point-min))
444 (let (xfrom)
445 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
446 (setq xfrom (match-string 1))
447 (gnus-delete-line))
448 (goto-char (point-min))
449 (if xfrom
450 (insert "From " xfrom "\n")
451 (unless (looking-at "From ")
452 (insert "From nobody " (current-time-string) "\n"))))
453 (nnfolder-normalize-buffer)
454 (set-buffer nnfolder-current-buffer)
455 (goto-char (point-min))
456 (if (not (nnfolder-goto-article article))
457 nil
458 (nnfolder-delete-mail)
459 (insert-buffer-substring buffer)
460 (nnfolder-save-buffer)
461 t)))
462
463 (deffoo nnfolder-request-delete-group (group &optional force server)
464 (nnfolder-close-group group server t)
465 ;; Delete all articles in GROUP.
466 (if (not force)
467 () ; Don't delete the articles.
468 ;; Delete the file that holds the group.
469 (ignore-errors
470 (delete-file (nnfolder-group-pathname group))))
471 ;; Remove the group from all structures.
472 (setq nnfolder-group-alist
473 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
474 nnfolder-current-group nil
475 nnfolder-current-buffer nil)
476 ;; Save the active file.
477 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
478 t)
479
480 (deffoo nnfolder-request-rename-group (group new-name &optional server)
481 (nnfolder-possibly-change-group group server)
482 (save-excursion
483 (set-buffer nnfolder-current-buffer)
484 (and (file-writable-p buffer-file-name)
485 (ignore-errors
486 (rename-file
487 buffer-file-name
488 (let ((new-file (nnfolder-group-pathname new-name)))
489 (gnus-make-directory (file-name-directory new-file))
490 new-file))
491 t)
492 ;; That went ok, so we change the internal structures.
493 (let ((entry (assoc group nnfolder-group-alist)))
494 (and entry (setcar entry new-name))
495 (setq nnfolder-current-buffer nil
496 nnfolder-current-group nil)
497 ;; Save the new group alist.
498 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
499 ;; We kill the buffer instead of renaming it and stuff.
500 (kill-buffer (current-buffer))
501 t))))
502
503 (defun nnfolder-request-regenerate (server)
504 (nnfolder-possibly-change-group nil server)
505 (nnfolder-generate-active-file)
506 t)
507
508 \f
509 ;;; Internal functions.
510
511 (defun nnfolder-adjust-min-active (group)
512 ;; Find the lowest active article in this group.
513 (let* ((active (cadr (assoc group nnfolder-group-alist)))
514 (marker (concat "\n" nnfolder-article-marker))
515 (number "[0-9]+")
516 (activemin (cdr active)))
517 (save-excursion
518 (set-buffer nnfolder-current-buffer)
519 (goto-char (point-min))
520 (while (and (search-forward marker nil t)
521 (re-search-forward number nil t))
522 (let ((newnum (string-to-number (match-string 0))))
523 (if (nnmail-within-headers-p)
524 (setq activemin (min activemin newnum)))))
525 (setcar active activemin))))
526
527 (defun nnfolder-article-string (article)
528 (if (numberp article)
529 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
530 (concat "\nMessage-ID: " article)))
531
532 (defun nnfolder-goto-article (article)
533 "Place point at the start of the headers of ARTICLE.
534 ARTICLE can be an article number or a Message-ID.
535 Returns t if successful, nil otherwise."
536 (let ((art-string (nnfolder-article-string article))
537 start found)
538 ;; It is likely that we are at or before the delimiter line.
539 ;; We therefore go to the end of the previous line, and start
540 ;; searching from there.
541 (beginning-of-line)
542 (unless (bobp)
543 (forward-char -1))
544 (setq start (point))
545 ;; First search forward.
546 (while (and (setq found (search-forward art-string nil t))
547 (not (nnmail-within-headers-p))))
548 ;; If unsuccessful, search backward from where we started,
549 (unless found
550 (goto-char start)
551 (while (and (setq found (search-backward art-string nil t))
552 (not (nnmail-within-headers-p)))))
553 (when found
554 (nnmail-search-unix-mail-delim-backward))))
555
556 (defun nnfolder-delete-mail (&optional leave-delim)
557 "Delete the message that point is in.
558 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
559 deleted. Point is left where the deleted region was."
560 (save-restriction
561 (narrow-to-region
562 (save-excursion
563 ;; In case point is at the beginning of the message already.
564 (forward-line 1)
565 (nnmail-search-unix-mail-delim-backward)
566 (if leave-delim (progn (forward-line 1) (point))
567 (point)))
568 (progn
569 (forward-line 1)
570 (if (nnmail-search-unix-mail-delim)
571 (point)
572 (point-max))))
573 (run-hooks 'nnfolder-delete-mail-hook)
574 (delete-region (point-min) (point-max))))
575
576 (defun nnfolder-possibly-change-group (group &optional server dont-check)
577 ;; Change servers.
578 (when (and server
579 (not (nnfolder-server-opened server)))
580 (nnfolder-open-server server))
581 (unless (gnus-buffer-live-p nnfolder-current-buffer)
582 (setq nnfolder-current-buffer nil
583 nnfolder-current-group nil))
584 ;; Change group.
585 (when (and group
586 (not (equal group nnfolder-current-group)))
587 (let ((file-name-coding-system nnmail-pathname-coding-system))
588 (nnmail-activate 'nnfolder)
589 (when (and (not (assoc group nnfolder-group-alist))
590 (not (file-exists-p
591 (nnfolder-group-pathname group))))
592 ;; The group doesn't exist, so we create a new entry for it.
593 (push (list group (cons 1 0)) nnfolder-group-alist)
594 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
595
596 (if dont-check
597 (setq nnfolder-current-group group
598 nnfolder-current-buffer nil)
599 (let (inf file)
600 ;; If we have to change groups, see if we don't already have the
601 ;; folder in memory. If we do, verify the modtime and destroy
602 ;; the folder if needed so we can rescan it.
603 (setq nnfolder-current-buffer
604 (nth 1 (assoc group nnfolder-buffer-alist)))
605
606 ;; If the buffer is not live, make sure it isn't in the alist. If it
607 ;; is live, verify that nobody else has touched the file since last
608 ;; time.
609 (when (and nnfolder-current-buffer
610 (not (gnus-buffer-live-p nnfolder-current-buffer)))
611 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
612 nnfolder-current-buffer nil))
613
614 (setq nnfolder-current-group group)
615
616 (when (or (not nnfolder-current-buffer)
617 (not (verify-visited-file-modtime
618 nnfolder-current-buffer)))
619 (save-excursion
620 (setq file (nnfolder-group-pathname group))
621 ;; See whether we need to create the new file.
622 (unless (file-exists-p file)
623 (gnus-make-directory (file-name-directory file))
624 (let ((nnmail-file-coding-system
625 (or nnfolder-file-coding-system-for-write
626 nnfolder-file-coding-system-for-write)))
627 (nnmail-write-region 1 1 file t 'nomesg)))
628 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
629 (set-buffer nnfolder-current-buffer)
630 (push (list group nnfolder-current-buffer)
631 nnfolder-buffer-alist)))))))))
632
633 (defun nnfolder-save-mail (group-art-list)
634 "Called narrowed to an article."
635 (let* (save-list group-art)
636 (goto-char (point-min))
637 ;; The From line may have been quoted by movemail.
638 (when (looking-at ">From")
639 (delete-char 1))
640 ;; This might come from somewhere else.
641 (unless (looking-at "From ")
642 (insert "From nobody " (current-time-string) "\n")
643 (goto-char (point-min)))
644 ;; Quote all "From " lines in the article.
645 (forward-line 1)
646 (let (case-fold-search)
647 (while (re-search-forward "^From " nil t)
648 (beginning-of-line)
649 (insert "> ")))
650 (setq save-list group-art-list)
651 (nnmail-insert-lines)
652 (nnmail-insert-xref group-art-list)
653 (run-hooks 'nnmail-prepare-save-mail-hook)
654 (run-hooks 'nnfolder-prepare-save-mail-hook)
655
656 ;; Insert the mail into each of the destination groups.
657 (while (setq group-art (pop group-art-list))
658 ;; Kill any previous newsgroup markers.
659 (goto-char (point-min))
660 (if (search-forward "\n\n" nil t)
661 (forward-line -1)
662 (goto-char (point-max)))
663 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
664 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
665
666 ;; Insert the new newsgroup marker.
667 (nnfolder-insert-newsgroup-line group-art)
668
669 (save-excursion
670 (let ((beg (point-min))
671 (end (point-max))
672 (obuf (current-buffer)))
673 (nnfolder-possibly-change-folder (car group-art))
674 (let ((buffer-read-only nil))
675 (nnfolder-normalize-buffer)
676 (insert-buffer-substring obuf beg end)))))
677
678 ;; Did we save it anywhere?
679 save-list))
680
681 (defun nnfolder-normalize-buffer ()
682 "Make sure there are two newlines at the end of the buffer."
683 (goto-char (point-max))
684 (skip-chars-backward "\n")
685 (delete-region (point) (point-max))
686 (insert "\n\n"))
687
688 (defun nnfolder-insert-newsgroup-line (group-art)
689 (save-excursion
690 (goto-char (point-min))
691 (unless (search-forward "\n\n" nil t)
692 (goto-char (point-max))
693 (insert "\n"))
694 (forward-char -1)
695 (insert (format (concat nnfolder-article-marker "%d %s\n")
696 (cdr group-art) (current-time-string)))))
697
698 (defun nnfolder-active-number (group)
699 ;; Find the next article number in GROUP.
700 (let ((active (cadr (assoc group nnfolder-group-alist))))
701 (if active
702 (setcdr active (1+ (cdr active)))
703 ;; This group is new, so we create a new entry for it.
704 ;; This might be a bit naughty... creating groups on the drop of
705 ;; a hat, but I don't know...
706 (push (list group (setq active (cons 1 1)))
707 nnfolder-group-alist))
708 (cdr active)))
709
710 (defun nnfolder-possibly-change-folder (group)
711 (let ((inf (assoc group nnfolder-buffer-alist)))
712 (if (and inf
713 (gnus-buffer-live-p (cadr inf)))
714 (set-buffer (cadr inf))
715 (when inf
716 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
717 (when nnfolder-group-alist
718 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
719 (push (list group (nnfolder-read-folder group))
720 nnfolder-buffer-alist))))
721
722 ;; This method has a problem if you've accidentally let the active list get
723 ;; out of sync with the files. This could happen, say, if you've
724 ;; accidentally gotten new mail with something other than Gnus (but why
725 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
726 ;; processing the file, ready to add new X-Gnus article number markers, and
727 ;; we'll run across a message with no ID yet - the active list _may_not_ be
728 ;; ready for us yet.
729
730 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
731 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
732 ;; rest of the message looking for any more, possibly higher IDs. We'll
733 ;; assume the maximum that we find is the highest active. Note that this
734 ;; shouldn't cost us much extra time at all, but will be a lot less
735 ;; vulnerable to glitches between the mbox and the active file.
736
737 (defun nnfolder-read-folder (group)
738 (let* ((file (nnfolder-group-pathname group))
739 (buffer (set-buffer
740 (let ((nnheader-file-coding-system
741 nnfolder-file-coding-system))
742 (nnheader-find-file-noselect file)))))
743 (if (equal (cadr (assoc group nnfolder-scantime-alist))
744 (nth 5 (file-attributes file)))
745 ;; This looks up-to-date, so we don't do any scanning.
746 (if (file-exists-p file)
747 buffer
748 (push (list group buffer) nnfolder-buffer-alist)
749 (set-buffer-modified-p t)
750 (nnfolder-save-buffer))
751 ;; Parse the damn thing.
752 (save-excursion
753 (goto-char (point-min))
754 ;; Remove any blank lines at the start.
755 (while (eq (following-char) ?\n)
756 (delete-char 1))
757 (nnmail-activate 'nnfolder)
758 ;; Read in the file.
759 (let ((delim "^From ")
760 (marker (concat "\n" nnfolder-article-marker))
761 (number "[0-9]+")
762 (active (or (cadr (assoc group nnfolder-group-alist))
763 (cons 1 0)))
764 (scantime (assoc group nnfolder-scantime-alist))
765 (minid (lsh -1 -1))
766 maxid start end newscantime
767 buffer-read-only)
768 (buffer-disable-undo)
769 (setq maxid (cdr active))
770 (goto-char (point-min))
771
772 ;; Anytime the active number is 1 or 0, it is suspect. In that
773 ;; case, search the file manually to find the active number. Or,
774 ;; of course, if we're being paranoid. (This would also be the
775 ;; place to build other lists from the header markers, such as
776 ;; expunge lists, etc., if we ever desired to abandon the active
777 ;; file entirely for mboxes.)
778 (when (or nnfolder-ignore-active-file
779 (< maxid 2))
780 (while (and (search-forward marker nil t)
781 (re-search-forward number nil t))
782 (let ((newnum (string-to-number (match-string 0))))
783 (if (nnmail-within-headers-p)
784 (setq maxid (max maxid newnum)
785 minid (min minid newnum)))))
786 (setcar active (max 1 (min minid maxid)))
787 (setcdr active (max maxid (cdr active)))
788 (goto-char (point-min)))
789
790 ;; As long as we trust that the user will only insert unmarked mail
791 ;; at the end, go to the end and search backwards for the last
792 ;; marker. Find the start of that message, and begin to search for
793 ;; unmarked messages from there.
794 (when (not (or nnfolder-distrust-mbox
795 (< maxid 2)))
796 (goto-char (point-max))
797 (unless (re-search-backward marker nil t)
798 (goto-char (point-min)))
799 (when (nnmail-search-unix-mail-delim)
800 (goto-char (point-min))))
801
802 ;; Keep track of the active number on our own, and insert it back
803 ;; into the active list when we're done. Also, prime the pump to
804 ;; cut down on the number of searches we do.
805 (unless (nnmail-search-unix-mail-delim)
806 (goto-char (point-max)))
807 (setq end (point-marker))
808 (while (not (= end (point-max)))
809 (setq start (marker-position end))
810 (goto-char end)
811 ;; There may be more than one "From " line, so we skip past
812 ;; them.
813 (while (looking-at delim)
814 (forward-line 1))
815 (set-marker end (if (nnmail-search-unix-mail-delim)
816 (point)
817 (point-max)))
818 (goto-char start)
819 (when (not (search-forward marker end t))
820 (narrow-to-region start end)
821 (nnmail-insert-lines)
822 (nnfolder-insert-newsgroup-line
823 (cons nil (nnfolder-active-number nnfolder-current-group)))
824 (widen)))
825
826 (set-marker end nil)
827 ;; Make absolutely sure that the active list reflects reality!
828 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
829 ;; Set the scantime for this group.
830 (setq newscantime (visited-file-modtime))
831 (if scantime
832 (setcdr scantime (list newscantime))
833 (push (list nnfolder-current-group newscantime)
834 nnfolder-scantime-alist))
835 (current-buffer))))))
836
837 ;;;###autoload
838 (defun nnfolder-generate-active-file ()
839 "Look for mbox folders in the nnfolder directory and make them into groups.
840 This command does not work if you use short group names."
841 (interactive)
842 (nnmail-activate 'nnfolder)
843 (let ((files (directory-files nnfolder-directory))
844 file)
845 (while (setq file (pop files))
846 (when (and (not (backup-file-name-p file))
847 (message-mail-file-mbox-p
848 (nnheader-concat nnfolder-directory file)))
849 (let ((oldgroup (assoc file nnfolder-group-alist)))
850 (if oldgroup
851 (nnheader-message 5 "Refreshing group %s..." file)
852 (nnheader-message 5 "Adding group %s..." file))
853 (if oldgroup
854 (setq nnfolder-group-alist
855 (delq oldgroup (copy-sequence nnfolder-group-alist))))
856 (push (list file (cons 1 0)) nnfolder-group-alist)
857 (nnfolder-possibly-change-folder file)
858 (nnfolder-possibly-change-group file)
859 (nnfolder-close-group file))))
860 (nnheader-message 5 "")))
861
862 (defun nnfolder-group-pathname (group)
863 "Make pathname for GROUP."
864 (setq group
865 (mm-encode-coding-string group nnmail-pathname-coding-system))
866 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
867 ;; If this file exists, we use it directly.
868 (if (or nnmail-use-long-file-names
869 (file-exists-p (concat dir group)))
870 (concat dir group)
871 ;; If not, we translate dots into slashes.
872 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
873
874 (defun nnfolder-save-buffer ()
875 "Save the buffer."
876 (when (buffer-modified-p)
877 (run-hooks 'nnfolder-save-buffer-hook)
878 (gnus-make-directory (file-name-directory (buffer-file-name)))
879 (let ((coding-system-for-write
880 (or nnfolder-file-coding-system-for-write
881 nnfolder-file-coding-system)))
882 (save-buffer))))
883
884 (defun nnfolder-save-active (group-alist active-file)
885 (let ((nnmail-active-file-coding-system
886 (or nnfolder-active-file-coding-system-for-write
887 nnfolder-active-file-coding-system)))
888 (nnmail-save-active group-alist active-file)))
889
890 (provide 'nnfolder)
891
892 ;;; nnfolder.el ends here