Delete #if 0 old definitions.
[bpt/emacs.git] / lisp / gnus / nnfolder.el
CommitLineData
eec82323
LMI
1;;; nnfolder.el --- mail folder access for Gnus
2;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
3
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
8
9;; This file is part of GNU Emacs.
10
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.
15
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.
20
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.
25
26;;; Commentary:
27
28;;; Code:
29
30(require 'nnheader)
31(require 'message)
32(require 'nnmail)
33(require 'nnoo)
34(require 'cl)
35(require 'gnus-util)
36
37(nnoo-declare nnfolder)
38
39(defvoo nnfolder-directory (expand-file-name message-directory)
40 "The name of the nnfolder directory.")
41
42(defvoo nnfolder-active-file
43 (nnheader-concat nnfolder-directory "active")
44 "The name of the active file.")
45
46;; I renamed this variable to something more in keeping with the general GNU
47;; style. -SLB
48
49(defvoo nnfolder-ignore-active-file nil
50 "If non-nil, causes nnfolder to do some extra work in order to determine
51the true active ranges of an mbox file. Note that the active file is still
52saved, but it's values are not used. This costs some extra time when
53scanning an mbox when opening it.")
54
55(defvoo nnfolder-distrust-mbox nil
56 "If non-nil, causes nnfolder to not trust the user with respect to
57inserting unaccounted for mail in the middle of an mbox file. This can greatly
58slow down scans, which now must scan the entire file for unmarked messages.
59When nil, scans occur forward from the last marked message, a huge
60time saver for large mailboxes.")
61
62(defvoo nnfolder-newsgroups-file
63 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
64 "Mail newsgroups description file.")
65
66(defvoo nnfolder-get-new-mail t
67 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
68
69(defvoo nnfolder-prepare-save-mail-hook nil
70 "Hook run narrowed to an article before saving.")
71
72(defvoo nnfolder-save-buffer-hook nil
73 "Hook run before saving the nnfolder mbox buffer.")
74
75(defvoo nnfolder-inhibit-expiry nil
76 "If non-nil, inhibit expiry.")
77
78\f
79
80(defconst nnfolder-version "nnfolder 1.0"
81 "nnfolder version.")
82
83(defconst nnfolder-article-marker "X-Gnus-Article-Number: "
84 "String used to demarcate what the article number for a message is.")
85
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)
93
94\f
95
96;;; Interface functions
97
98(nnoo-define-basics nnfolder)
99
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)))
131
132 (set-buffer nntp-server-buffer)
133 (nnheader-fold-continuation-lines)
134 'headers)))))
135
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))
148 (t
149 (nnmail-activate 'nnfolder)
150 (nnheader-report 'nnfolder "Opened server %s using directory %s"
151 server nnfolder-directory)
152 t)))
153
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))
162
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)))))))))))
194
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)
203 t)
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))
212 (t
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))))))))
217
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)))
237
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.
242
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)
271 t)
272
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)
a8151ef7
LMI
279 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
280 (nnfolder-read-folder group)))
eec82323
LMI
281 t)
282
283(deffoo nnfolder-request-list (&optional server)
284 (nnfolder-possibly-change-group nil server)
285 (save-excursion
58724016
KH
286 ;; 1997/8/14 by MORIOKA Tomohiko
287 ;; for XEmacs/mule.
288 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
ddcfd8de 289 (file-name-coding-system 'binary) ; for Emacs 20
58724016
KH
290 (pathname-coding-system 'binary)) ; for XEmacs/mule
291 (nnmail-find-file nnfolder-active-file)
292 (setq nnfolder-group-alist (nnmail-get-active)))
eec82323
LMI
293 t))
294
295(deffoo nnfolder-request-newgroups (date &optional server)
296 (nnfolder-possibly-change-group nil server)
297 (nnfolder-request-list server))
298
299(deffoo nnfolder-request-list-newsgroups (&optional server)
300 (nnfolder-possibly-change-group nil server)
301 (save-excursion
302 (nnmail-find-file nnfolder-newsgroups-file)))
303
304(deffoo nnfolder-request-expire-articles
305 (articles newsgroup &optional server force)
306 (nnfolder-possibly-change-group newsgroup server)
307 (let* ((is-old t)
308 rest)
309 (nnmail-activate 'nnfolder)
310
311 (save-excursion
312 (set-buffer nnfolder-current-buffer)
313 (while (and articles is-old)
314 (goto-char (point-min))
315 (when (search-forward (nnfolder-article-string (car articles)) nil t)
316 (if (setq is-old
317 (nnmail-expired-article-p
318 newsgroup
319 (buffer-substring
320 (point) (progn (end-of-line) (point)))
321 force nnfolder-inhibit-expiry))
322 (progn
323 (nnheader-message 5 "Deleting article %d..."
324 (car articles) newsgroup)
325 (nnfolder-delete-mail))
326 (push (car articles) rest)))
327 (setq articles (cdr articles)))
328 (unless nnfolder-inhibit-expiry
329 (nnheader-message 5 "Deleting articles...done"))
330 (nnfolder-save-buffer)
331 (nnfolder-adjust-min-active newsgroup)
332 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
333 (nconc rest articles))))
334
335(deffoo nnfolder-request-move-article
336 (article group server accept-form &optional last)
337 (let ((buf (get-buffer-create " *nnfolder move*"))
338 result)
339 (and
340 (nnfolder-request-article article group server)
341 (save-excursion
342 (set-buffer buf)
343 (buffer-disable-undo (current-buffer))
344 (erase-buffer)
345 (insert-buffer-substring nntp-server-buffer)
346 (goto-char (point-min))
347 (while (re-search-forward
348 (concat "^" nnfolder-article-marker)
349 (save-excursion (search-forward "\n\n" nil t) (point)) t)
350 (delete-region (progn (beginning-of-line) (point))
351 (progn (forward-line 1) (point))))
352 (setq result (eval accept-form))
353 (kill-buffer buf)
354 result)
355 (save-excursion
356 (nnfolder-possibly-change-group group server)
357 (set-buffer nnfolder-current-buffer)
358 (goto-char (point-min))
359 (when (search-forward (nnfolder-article-string article) nil t)
360 (nnfolder-delete-mail))
361 (when last
362 (nnfolder-save-buffer)
363 (nnfolder-adjust-min-active group)
364 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))))
365 result))
366
367(deffoo nnfolder-request-accept-article (group &optional server last)
368 (nnfolder-possibly-change-group group server)
369 (nnmail-check-syntax)
370 (let ((buf (current-buffer))
371 result art-group)
372 (goto-char (point-min))
373 (when (looking-at "X-From-Line: ")
374 (replace-match "From "))
375 (and
376 (nnfolder-request-list)
377 (save-excursion
378 (set-buffer buf)
379 (goto-char (point-min))
380 (search-forward "\n\n" nil t)
381 (forward-line -1)
382 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
383 (delete-region (point) (progn (forward-line 1) (point))))
384 (when nnmail-cache-accepted-message-ids
385 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
386 (setq result (if (stringp group)
387 (list (cons group (nnfolder-active-number group)))
388 (setq art-group
389 (nnmail-article-group 'nnfolder-active-number))))
390 (if (and (null result)
391 (yes-or-no-p "Moved to `junk' group; delete article? "))
392 (setq result 'junk)
393 (setq result
394 (car (nnfolder-save-mail result)))))
395 (when last
396 (save-excursion
397 (nnfolder-possibly-change-folder (or (caar art-group) group))
398 (nnfolder-save-buffer)
399 (when nnmail-cache-accepted-message-ids
400 (nnmail-cache-close)))))
401 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
402 (unless result
403 (nnheader-report 'nnfolder "Couldn't store article"))
404 result))
405
406(deffoo nnfolder-request-replace-article (article group buffer)
407 (nnfolder-possibly-change-group group)
408 (save-excursion
409 (set-buffer nnfolder-current-buffer)
410 (goto-char (point-min))
411 (if (not (search-forward (nnfolder-article-string article) nil t))
412 nil
413 (nnfolder-delete-mail t t)
414 (insert-buffer-substring buffer)
415 (nnfolder-save-buffer)
416 t)))
417
418(deffoo nnfolder-request-delete-group (group &optional force server)
419 (nnfolder-close-group group server t)
420 ;; Delete all articles in GROUP.
421 (if (not force)
422 () ; Don't delete the articles.
423 ;; Delete the file that holds the group.
424 (ignore-errors
425 (delete-file (nnfolder-group-pathname group))))
426 ;; Remove the group from all structures.
427 (setq nnfolder-group-alist
428 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
429 nnfolder-current-group nil
430 nnfolder-current-buffer nil)
431 ;; Save the active file.
432 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
433 t)
434
435(deffoo nnfolder-request-rename-group (group new-name &optional server)
436 (nnfolder-possibly-change-group group server)
437 (save-excursion
438 (set-buffer nnfolder-current-buffer)
439 (and (file-writable-p buffer-file-name)
440 (ignore-errors
441 (rename-file
442 buffer-file-name
443 (nnfolder-group-pathname new-name))
444 t)
445 ;; That went ok, so we change the internal structures.
446 (let ((entry (assoc group nnfolder-group-alist)))
447 (and entry (setcar entry new-name))
448 (setq nnfolder-current-buffer nil
449 nnfolder-current-group nil)
450 ;; Save the new group alist.
451 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
452 ;; We kill the buffer instead of renaming it and stuff.
453 (kill-buffer (current-buffer))
454 t))))
455
a8151ef7
LMI
456(defun nnfolder-request-regenerate (server)
457 (nnfolder-possibly-change-group nil server)
458 (nnfolder-generate-active-file)
459 t)
460
eec82323
LMI
461\f
462;;; Internal functions.
463
464(defun nnfolder-adjust-min-active (group)
465 ;; Find the lowest active article in this group.
466 (let* ((active (cadr (assoc group nnfolder-group-alist)))
467 (marker (concat "\n" nnfolder-article-marker))
468 (number "[0-9]+")
469 (activemin (cdr active)))
470 (save-excursion
471 (set-buffer nnfolder-current-buffer)
472 (goto-char (point-min))
473 (while (and (search-forward marker nil t)
474 (re-search-forward number nil t))
475 (setq activemin (min activemin
476 (string-to-number (buffer-substring
477 (match-beginning 0)
478 (match-end 0))))))
479 (setcar active activemin))))
480
481(defun nnfolder-article-string (article)
482 (if (numberp article)
483 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
484 (concat "\nMessage-ID: " article)))
485
486(defun nnfolder-delete-mail (&optional force leave-delim)
487 "Delete the message that point is in."
488 (save-excursion
489 (delete-region
490 (save-excursion
491 (nnmail-search-unix-mail-delim-backward)
492 (if leave-delim (progn (forward-line 1) (point))
493 (point)))
494 (progn
495 (forward-line 1)
496 (if (nnmail-search-unix-mail-delim)
497 (if (and (not (bobp)) leave-delim)
498 (progn (forward-line -2) (point))
499 (point))
500 (point-max))))))
501
502(defun nnfolder-possibly-change-group (group &optional server dont-check)
503 ;; Change servers.
504 (when (and server
505 (not (nnfolder-server-opened server)))
506 (nnfolder-open-server server))
507 (unless (gnus-buffer-live-p nnfolder-current-buffer)
508 (setq nnfolder-current-buffer nil
509 nnfolder-current-group nil))
510 ;; Change group.
511 (when (and group
512 (not (equal group nnfolder-current-group)))
ddcfd8de
KH
513 (let ((file-name-coding-system 'binary)
514 (pathname-coding-system 'binary))
58724016
KH
515 (nnmail-activate 'nnfolder)
516 (when (and (not (assoc group nnfolder-group-alist))
517 (not (file-exists-p
518 (nnfolder-group-pathname group))))
519 ;; The group doesn't exist, so we create a new entry for it.
520 (push (list group (cons 1 0)) nnfolder-group-alist)
521 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
a8151ef7 522
58724016 523 (if dont-check
a8151ef7
LMI
524 (setq nnfolder-current-group group
525 nnfolder-current-buffer nil)
58724016
KH
526 (let (inf file)
527 ;; If we have to change groups, see if we don't already have the
528 ;; folder in memory. If we do, verify the modtime and destroy
529 ;; the folder if needed so we can rescan it.
a8151ef7
LMI
530 (setq nnfolder-current-buffer
531 (nth 1 (assoc group nnfolder-buffer-alist)))
532
58724016
KH
533 ;; If the buffer is not live, make sure it isn't in the alist. If it
534 ;; is live, verify that nobody else has touched the file since last
535 ;; time.
536 (when (and nnfolder-current-buffer
537 (not (gnus-buffer-live-p nnfolder-current-buffer)))
538 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
539 nnfolder-current-buffer nil))
a8151ef7 540
58724016 541 (setq nnfolder-current-group group)
a8151ef7 542
58724016
KH
543 (when (or (not nnfolder-current-buffer)
544 (not (verify-visited-file-modtime nnfolder-current-buffer)))
545 (save-excursion
546 (setq file (nnfolder-group-pathname group))
547 ;; See whether we need to create the new file.
548 (unless (file-exists-p file)
549 (gnus-make-directory (file-name-directory file))
550 (nnmail-write-region 1 1 file t 'nomesg))
551 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
552 (set-buffer nnfolder-current-buffer)
553 (push (list group nnfolder-current-buffer)
554 nnfolder-buffer-alist)))))))))
eec82323
LMI
555
556(defun nnfolder-save-mail (group-art-list)
557 "Called narrowed to an article."
558 (let* (save-list group-art)
559 (goto-char (point-min))
560 ;; The From line may have been quoted by movemail.
561 (when (looking-at (concat ">" message-unix-mail-delimiter))
562 (delete-char 1))
563 ;; This might come from somewhere else.
564 (unless (looking-at message-unix-mail-delimiter)
565 (insert "From nobody " (current-time-string) "\n")
566 (goto-char (point-min)))
567 ;; Quote all "From " lines in the article.
568 (forward-line 1)
569 (let (case-fold-search)
570 (while (re-search-forward "^From " nil t)
571 (beginning-of-line)
572 (insert "> ")))
573 (setq save-list group-art-list)
574 (nnmail-insert-lines)
575 (nnmail-insert-xref group-art-list)
576 (run-hooks 'nnmail-prepare-save-mail-hook)
577 (run-hooks 'nnfolder-prepare-save-mail-hook)
578
579 ;; Insert the mail into each of the destination groups.
580 (while (setq group-art (pop group-art-list))
581 ;; Kill any previous newsgroup markers.
582 (goto-char (point-min))
583 (search-forward "\n\n" nil t)
584 (forward-line -1)
585 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
586 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
587
588 ;; Insert the new newsgroup marker.
589 (nnfolder-insert-newsgroup-line group-art)
590
591 (save-excursion
592 (let ((beg (point-min))
593 (end (point-max))
594 (obuf (current-buffer)))
595 (nnfolder-possibly-change-folder (car group-art))
596 (let ((buffer-read-only nil))
597 (goto-char (point-max))
598 (unless (eolp)
599 (insert "\n"))
600 (unless (bobp)
601 (insert "\n"))
602 (insert-buffer-substring obuf beg end)))))
603
604 ;; Did we save it anywhere?
605 save-list))
606
607(defun nnfolder-insert-newsgroup-line (group-art)
608 (save-excursion
609 (goto-char (point-min))
610 (when (search-forward "\n\n" nil t)
611 (forward-char -1)
612 (insert (format (concat nnfolder-article-marker "%d %s\n")
613 (cdr group-art) (current-time-string))))))
614
615(defun nnfolder-active-number (group)
616 ;; Find the next article number in GROUP.
617 (let ((active (cadr (assoc group nnfolder-group-alist))))
618 (if active
619 (setcdr active (1+ (cdr active)))
620 ;; This group is new, so we create a new entry for it.
621 ;; This might be a bit naughty... creating groups on the drop of
622 ;; a hat, but I don't know...
623 (push (list group (setq active (cons 1 1)))
624 nnfolder-group-alist))
625 (cdr active)))
626
627(defun nnfolder-possibly-change-folder (group)
628 (let ((inf (assoc group nnfolder-buffer-alist)))
629 (if (and inf
630 (gnus-buffer-live-p (cadr inf)))
631 (set-buffer (cadr inf))
632 (when inf
633 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
634 (when nnfolder-group-alist
635 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
636 (push (list group (nnfolder-read-folder group))
637 nnfolder-buffer-alist))))
638
639;; This method has a problem if you've accidentally let the active list get
640;; out of sync with the files. This could happen, say, if you've
641;; accidentally gotten new mail with something other than Gnus (but why
642;; would _that_ ever happen? :-). In that case, we will be in the middle of
643;; processing the file, ready to add new X-Gnus article number markers, and
644;; we'll run across a message with no ID yet - the active list _may_not_ be
645;; ready for us yet.
646
647;; To handle this, I'm modifying this routine to maintain the maximum ID seen
648;; so far, and when we hit a message with no ID, we will _manually_ scan the
649;; rest of the message looking for any more, possibly higher IDs. We'll
650;; assume the maximum that we find is the highest active. Note that this
651;; shouldn't cost us much extra time at all, but will be a lot less
652;; vulnerable to glitches between the mbox and the active file.
653
654(defun nnfolder-read-folder (group)
655 (let* ((file (nnfolder-group-pathname group))
656 (buffer (set-buffer (nnheader-find-file-noselect file))))
657 (if (equal (cadr (assoc group nnfolder-scantime-alist))
658 (nth 5 (file-attributes file)))
659 ;; This looks up-to-date, so we don't do any scanning.
660 buffer
661 ;; Parse the damn thing.
662 (save-excursion
663 (nnmail-activate 'nnfolder)
664 ;; Read in the file.
665 (let ((delim (concat "^" message-unix-mail-delimiter))
666 (marker (concat "\n" nnfolder-article-marker))
667 (number "[0-9]+")
668 (active (or (cadr (assoc group nnfolder-group-alist))
669 (cons 1 0)))
670 (scantime (assoc group nnfolder-scantime-alist))
671 (minid (lsh -1 -1))
672 maxid start end newscantime
673 buffer-read-only)
674 (buffer-disable-undo (current-buffer))
675 (setq maxid (cdr active))
676 (goto-char (point-min))
677
678 ;; Anytime the active number is 1 or 0, it is suspect. In that
679 ;; case, search the file manually to find the active number. Or,
680 ;; of course, if we're being paranoid. (This would also be the
681 ;; place to build other lists from the header markers, such as
682 ;; expunge lists, etc., if we ever desired to abandon the active
683 ;; file entirely for mboxes.)
684 (when (or nnfolder-ignore-active-file
685 (< maxid 2))
686 (while (and (search-forward marker nil t)
687 (re-search-forward number nil t))
688 (let ((newnum (string-to-number (match-string 0))))
689 (setq maxid (max maxid newnum))
690 (setq minid (min minid newnum))))
691 (setcar active (max 1 (min minid maxid)))
692 (setcdr active (max maxid (cdr active)))
693 (goto-char (point-min)))
694
695 ;; As long as we trust that the user will only insert unmarked mail
696 ;; at the end, go to the end and search backwards for the last
697 ;; marker. Find the start of that message, and begin to search for
698 ;; unmarked messages from there.
699 (when (not (or nnfolder-distrust-mbox
700 (< maxid 2)))
701 (goto-char (point-max))
702 (unless (re-search-backward marker nil t)
703 (goto-char (point-min)))
704 (when (nnmail-search-unix-mail-delim)
705 (goto-char (point-min))))
706
707 ;; Keep track of the active number on our own, and insert it back
708 ;; into the active list when we're done. Also, prime the pump to
709 ;; cut down on the number of searches we do.
710 (unless (nnmail-search-unix-mail-delim)
711 (goto-char (point-max)))
712 (setq end (point-marker))
713 (while (not (= end (point-max)))
714 (setq start (marker-position end))
715 (goto-char end)
716 ;; There may be more than one "From " line, so we skip past
717 ;; them.
718 (while (looking-at delim)
719 (forward-line 1))
720 (set-marker end (if (nnmail-search-unix-mail-delim)
721 (point)
722 (point-max)))
723 (goto-char start)
724 (when (not (search-forward marker end t))
725 (narrow-to-region start end)
726 (nnmail-insert-lines)
727 (nnfolder-insert-newsgroup-line
728 (cons nil (nnfolder-active-number nnfolder-current-group)))
729 (widen)))
730
731 (set-marker end nil)
732 ;; Make absolutely sure that the active list reflects reality!
733 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
734 ;; Set the scantime for this group.
735 (setq newscantime (visited-file-modtime))
736 (if scantime
737 (setcdr scantime (list newscantime))
738 (push (list nnfolder-current-group newscantime)
739 nnfolder-scantime-alist))
740 (current-buffer))))))
741
742;;;###autoload
743(defun nnfolder-generate-active-file ()
744 "Look for mbox folders in the nnfolder directory and make them into groups."
745 (interactive)
746 (nnmail-activate 'nnfolder)
747 (let ((files (directory-files nnfolder-directory))
748 file)
749 (while (setq file (pop files))
750 (when (and (not (backup-file-name-p file))
751 (message-mail-file-mbox-p
752 (nnheader-concat nnfolder-directory file)))
753 (let ((oldgroup (assoc file nnfolder-group-alist)))
754 (if oldgroup
755 (nnheader-message 5 "Refreshing group %s..." file)
756 (nnheader-message 5 "Adding group %s..." file))
03378294
RS
757 (if oldgroup
758 (setq nnfolder-group-alist
759 (delq oldgroup (copy-sequence nnfolder-group-alist))))
eec82323
LMI
760 (push (list file (cons 1 0)) nnfolder-group-alist)
761 (nnfolder-possibly-change-folder file)
762 (nnfolder-possibly-change-group file)
763 (nnfolder-close-group file))))
764 (message "")))
765
766(defun nnfolder-group-pathname (group)
767 "Make pathname for GROUP."
a8151ef7 768 (setq group (gnus-encode-coding-string group nnmail-pathname-coding-system))
eec82323
LMI
769 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
770 ;; If this file exists, we use it directly.
771 (if (or nnmail-use-long-file-names
772 (file-exists-p (concat dir group)))
773 (concat dir group)
774 ;; If not, we translate dots into slashes.
775 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
776
777(defun nnfolder-save-buffer ()
778 "Save the buffer."
779 (when (buffer-modified-p)
780 (run-hooks 'nnfolder-save-buffer-hook)
a8151ef7 781 (gnus-make-directory (file-name-directory (buffer-file-name)))
eec82323
LMI
782 (save-buffer)))
783
784(provide 'nnfolder)
785
786;;; nnfolder.el ends here