Merged from miles@gnu.org--gnu-2005 (patch 469)
[bpt/emacs.git] / lisp / gnus / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;; Free Software Foundation, Inc.
4
5 ;; Author: Simon Josefsson <simon@josefsson.org> (adding MARKS)
6 ;; ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
7 ;; Scott Byer <byer@mv.us.adobe.com>
8 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; Keywords: mail
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
28
29 ;;; Commentary:
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (require 'message)
35 (require 'nnmail)
36 (require 'nnoo)
37 (eval-when-compile (require 'cl))
38 (require 'gnus)
39 (require 'gnus-util)
40 (require 'gnus-range)
41
42 (eval-and-compile
43 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
44 (autoload 'gnus-intersection "gnus-range"))
45
46 (nnoo-declare nnfolder)
47
48 (defvoo nnfolder-directory (expand-file-name message-directory)
49 "The name of the nnfolder directory.")
50
51 (defvoo nnfolder-nov-directory nil
52 "The name of the nnfolder NOV directory.
53 If nil, `nnfolder-directory' is used.")
54
55 (defvoo nnfolder-marks-directory nil
56 "The name of the nnfolder MARKS directory.
57 If nil, `nnfolder-directory' is used.")
58
59 (defvoo nnfolder-active-file
60 (nnheader-concat nnfolder-directory "active")
61 "The name of the active file.")
62
63 ;; I renamed this variable to something more in keeping with the general GNU
64 ;; style. -SLB
65
66 (defvoo nnfolder-ignore-active-file nil
67 "If non-nil, the active file is ignored.
68 This causes nnfolder to do some extra work in order to determine the
69 true active ranges of an mbox file. Note that the active file is
70 still saved, but its values are not used. This costs some extra time
71 when scanning an mbox when opening it.")
72
73 (defvoo nnfolder-distrust-mbox nil
74 "If non-nil, the folder will be distrusted.
75 This means that nnfolder will not trust the user with respect to
76 inserting unaccounted for mail in the middle of an mbox file. This
77 can greatly slow down scans, which now must scan the entire file for
78 unmarked messages. When nil, scans occur forward from the last marked
79 message, a huge time saver for large mailboxes.")
80
81 (defvoo nnfolder-newsgroups-file
82 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
83 "Mail newsgroups description file.")
84
85 (defvoo nnfolder-get-new-mail t
86 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
87
88 (defvoo nnfolder-prepare-save-mail-hook nil
89 "Hook run narrowed to an article before saving.")
90
91 (defvoo nnfolder-save-buffer-hook nil
92 "Hook run before saving the nnfolder mbox buffer.")
93
94
95 (defvoo nnfolder-inhibit-expiry nil
96 "If non-nil, inhibit expiry.")
97
98 \f
99
100 (defconst nnfolder-version "nnfolder 2.0"
101 "nnfolder version.")
102
103 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
104 "String used to demarcate what the article number for a message is.")
105
106 (defvoo nnfolder-current-group nil)
107 (defvoo nnfolder-current-buffer nil)
108 (defvoo nnfolder-status-string "")
109 (defvoo nnfolder-group-alist nil)
110 (defvoo nnfolder-buffer-alist nil)
111 (defvoo nnfolder-scantime-alist nil)
112 (defvoo nnfolder-active-timestamp nil)
113 (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
114 (defvoo nnfolder-active-file-coding-system-for-write
115 nnmail-active-file-coding-system)
116 (defvoo nnfolder-file-coding-system mm-text-coding-system)
117 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
118 "Coding system for save nnfolder file.
119 if nil, `nnfolder-file-coding-system' is used.") ; FIXME: fill-in the doc-string of this variable
120
121 (defvoo nnfolder-nov-is-evil nil
122 "If non-nil, Gnus will never generate and use nov databases for mail groups.
123 Using nov databases will speed up header fetching considerably.
124 This variable shouldn't be flipped much. If you have, for some reason,
125 set this to t, and want to set it to nil again, you should always run
126 the `nnfolder-generate-active-file' command. The function will go
127 through all nnfolder directories and generate nov databases for them
128 all. This may very well take some time.")
129
130 (defvoo nnfolder-nov-file-suffix ".nov")
131
132 (defvoo nnfolder-nov-buffer-alist nil)
133
134 (defvar nnfolder-nov-buffer-file-name nil)
135
136 (defvoo nnfolder-marks-is-evil nil
137 "If non-nil, Gnus will never generate and use marks file for mail groups.
138 Using marks files makes it possible to backup and restore mail groups
139 separately from `.newsrc.eld'. If you have, for some reason, set
140 this to t, and want to set it to nil again, you should always remove
141 the corresponding marks file (usually base nnfolder file name
142 concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
143 the group. Then the marks file will be regenerated properly by Gnus.")
144
145 (defvoo nnfolder-marks nil)
146
147 (defvoo nnfolder-marks-file-suffix ".mrk")
148
149 (defvar nnfolder-marks-modtime (gnus-make-hashtable))
150
151 \f
152
153 ;;; Interface functions
154
155 (nnoo-define-basics nnfolder)
156
157 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
158 (save-excursion
159 (set-buffer nntp-server-buffer)
160 (erase-buffer)
161 (let (article start stop num)
162 (nnfolder-possibly-change-group group server)
163 (when nnfolder-current-buffer
164 (set-buffer nnfolder-current-buffer)
165 (goto-char (point-min))
166 (if (stringp (car articles))
167 'headers
168 (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
169 'nov
170 (setq articles (gnus-sorted-intersection
171 ;; Is ARTICLES sorted?
172 (sort articles '<)
173 (nnfolder-existing-articles)))
174 (while (setq article (pop articles))
175 (set-buffer nnfolder-current-buffer)
176 (cond ((nnfolder-goto-article article)
177 (setq start (point))
178 (setq stop (if (search-forward "\n\n" nil t)
179 (1- (point))
180 (point-max)))
181 (set-buffer nntp-server-buffer)
182 (insert (format "221 %d Article retrieved.\n" article))
183 (insert-buffer-substring nnfolder-current-buffer
184 start stop)
185 (goto-char (point-max))
186 (insert ".\n"))
187
188 ;; If we couldn't find this article, skip over ranges
189 ;; of missing articles so we don't search the whole file
190 ;; for each of them.
191 ((numberp article)
192 (setq start (point))
193 (and
194 ;; Check that we are either at BOF or after an
195 ;; article with a lower number. We do this so we
196 ;; won't be confused by out-of-order article numbers,
197 ;; as caused by active file bogosity.
198 (cond
199 ((bobp))
200 ((search-backward (concat "\n" nnfolder-article-marker)
201 nil t)
202 (goto-char (match-end 0))
203 (setq num (string-to-number
204 (buffer-substring
205 (point) (gnus-point-at-eol))))
206 (goto-char start)
207 (< num article)))
208 ;; Check that we are before an article with a
209 ;; higher number.
210 (search-forward (concat "\n" nnfolder-article-marker)
211 nil t)
212 (progn
213 (setq num (string-to-number
214 (buffer-substring
215 (point) (gnus-point-at-eol))))
216 (> num article))
217 ;; Discard any article numbers before the one we're
218 ;; now looking at.
219 (while (and articles
220 (< (car articles) num))
221 (setq articles (cdr articles))))
222 (goto-char start))))
223 (set-buffer nntp-server-buffer)
224 (nnheader-fold-continuation-lines)
225 'headers))))))
226
227 (deffoo nnfolder-open-server (server &optional defs)
228 (nnoo-change-server 'nnfolder server defs)
229 (nnmail-activate 'nnfolder t)
230 (gnus-make-directory nnfolder-directory)
231 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
232 (and nnfolder-nov-directory
233 (gnus-make-directory nnfolder-nov-directory)))
234 (unless nnfolder-marks-is-evil
235 (and nnfolder-marks-directory
236 (gnus-make-directory nnfolder-marks-directory)))
237 (cond
238 ((not (file-exists-p nnfolder-directory))
239 (nnfolder-close-server)
240 (nnheader-report 'nnfolder "Couldn't create directory: %s"
241 nnfolder-directory))
242 ((not (file-directory-p (file-truename nnfolder-directory)))
243 (nnfolder-close-server)
244 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
245 (t
246 (nnmail-activate 'nnfolder)
247 (nnheader-report 'nnfolder "Opened server %s using directory %s"
248 server nnfolder-directory)
249 t)))
250
251 (deffoo nnfolder-request-close ()
252 (let ((alist nnfolder-buffer-alist))
253 (while alist
254 (nnfolder-close-group (caar alist) nil t)
255 (setq alist (cdr alist))))
256 (nnoo-close-server 'nnfolder)
257 (setq nnfolder-buffer-alist nil
258 nnfolder-group-alist nil))
259
260 (deffoo nnfolder-request-article (article &optional group server buffer)
261 (nnfolder-possibly-change-group group server)
262 (save-excursion
263 (set-buffer nnfolder-current-buffer)
264 (goto-char (point-min))
265 (when (nnfolder-goto-article article)
266 (let (start stop)
267 (setq start (point))
268 (forward-line 1)
269 (unless (and (nnmail-search-unix-mail-delim)
270 (forward-line -1))
271 (goto-char (point-max)))
272 (setq stop (point))
273 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
274 (set-buffer nntp-server-buffer)
275 (erase-buffer)
276 (insert-buffer-substring nnfolder-current-buffer start stop)
277 (goto-char (point-min))
278 (while (looking-at "From ")
279 (delete-char 5)
280 (insert "X-From-Line: ")
281 (forward-line 1))
282 (if (numberp article)
283 (cons nnfolder-current-group article)
284 (goto-char (point-min))
285 (cons nnfolder-current-group
286 (if (search-forward (concat "\n" nnfolder-article-marker)
287 nil t)
288 (string-to-number (buffer-substring
289 (point) (gnus-point-at-eol)))
290 -1))))))))
291
292 (deffoo nnfolder-request-group (group &optional server dont-check)
293 (nnfolder-possibly-change-group group server t)
294 (save-excursion
295 (if (not (assoc group nnfolder-group-alist))
296 (nnheader-report 'nnfolder "No such group: %s" group)
297 (if dont-check
298 (progn
299 (nnheader-report 'nnfolder "Selected group %s" group)
300 t)
301 (let* ((active (assoc group nnfolder-group-alist))
302 (group (car active))
303 (range (cadr active)))
304 (cond
305 ((null active)
306 (nnheader-report 'nnfolder "No such group: %s" group))
307 ((null nnfolder-current-group)
308 (nnheader-report 'nnfolder "Empty group: %s" group))
309 (t
310 (nnheader-report 'nnfolder "Selected group %s" group)
311 (nnheader-insert "211 %d %d %d %s\n"
312 (1+ (- (cdr range) (car range)))
313 (car range) (cdr range) group))))))))
314
315 (deffoo nnfolder-request-scan (&optional group server)
316 (nnfolder-possibly-change-group nil server)
317 (when nnfolder-get-new-mail
318 (nnfolder-possibly-change-group group server)
319 (nnmail-get-new-mail
320 'nnfolder
321 (lambda ()
322 (let ((bufs nnfolder-buffer-alist))
323 (save-excursion
324 (while bufs
325 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
326 (setq nnfolder-buffer-alist
327 (delq (car bufs) nnfolder-buffer-alist))
328 (set-buffer (nth 1 (car bufs)))
329 (nnfolder-save-buffer)
330 (kill-buffer (current-buffer)))
331 (setq bufs (cdr bufs))))))
332 nnfolder-directory
333 group)))
334
335 ;; Don't close the buffer if we're not shutting down the server. This way,
336 ;; we can keep the buffer in the group buffer cache, and not have to grovel
337 ;; over the buffer again unless we add new mail to it or modify it in some
338 ;; way.
339
340 (deffoo nnfolder-close-group (group &optional server force)
341 ;; Make sure we _had_ the group open.
342 (when (or (assoc group nnfolder-buffer-alist)
343 (equal group nnfolder-current-group))
344 (let ((inf (assoc group nnfolder-buffer-alist)))
345 (when inf
346 (when (and nnfolder-current-group
347 nnfolder-current-buffer)
348 (push (list nnfolder-current-group nnfolder-current-buffer)
349 nnfolder-buffer-alist))
350 (setq nnfolder-buffer-alist
351 (delq inf nnfolder-buffer-alist))
352 (setq nnfolder-current-buffer (cadr inf)
353 nnfolder-current-group (car inf))))
354 (when (and nnfolder-current-buffer
355 (buffer-name nnfolder-current-buffer))
356 (save-excursion
357 (set-buffer nnfolder-current-buffer)
358 ;; If the buffer was modified, write the file out now.
359 (nnfolder-save-buffer)
360 ;; If we're shutting the server down, we need to kill the
361 ;; buffer and remove it from the open buffer list. Or, of
362 ;; course, if we're trying to minimize our space impact.
363 (kill-buffer (current-buffer))
364 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
365 nnfolder-buffer-alist)))))
366 (setq nnfolder-current-group nil
367 nnfolder-current-buffer nil)
368 t)
369
370 (deffoo nnfolder-request-create-group (group &optional server args)
371 (nnfolder-possibly-change-group nil server)
372 (nnmail-activate 'nnfolder)
373 (when (and group
374 (not (assoc group nnfolder-group-alist)))
375 (push (list group (cons 1 0)) nnfolder-group-alist)
376 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
377 (save-current-buffer
378 (nnfolder-read-folder group)))
379 t)
380
381 (deffoo nnfolder-request-list (&optional server)
382 (nnfolder-possibly-change-group nil server)
383 (save-excursion
384 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
385 (nnmail-find-file nnfolder-active-file)
386 (setq nnfolder-group-alist (nnmail-get-active)))
387 t))
388
389 (deffoo nnfolder-request-newgroups (date &optional server)
390 (nnfolder-possibly-change-group nil server)
391 (nnfolder-request-list server))
392
393 (deffoo nnfolder-request-list-newsgroups (&optional server)
394 (nnfolder-possibly-change-group nil server)
395 (save-excursion
396 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
397 (nnmail-find-file nnfolder-newsgroups-file))))
398
399 ;; Return a list consisting of all article numbers existing in the
400 ;; current folder.
401
402 (defun nnfolder-existing-articles ()
403 (save-excursion
404 (when nnfolder-current-buffer
405 (set-buffer nnfolder-current-buffer)
406 (goto-char (point-min))
407 (let ((marker (concat "\n" nnfolder-article-marker))
408 (number "[0-9]+")
409 numbers)
410 (while (and (search-forward marker nil t)
411 (re-search-forward number nil t))
412 (let ((newnum (string-to-number (match-string 0))))
413 (if (nnmail-within-headers-p)
414 (push newnum numbers))))
415 ;; The article numbers are increasing, so this result is sorted.
416 (nreverse numbers)))))
417
418 (deffoo nnfolder-request-expire-articles
419 (articles newsgroup &optional server force)
420 (nnfolder-possibly-change-group newsgroup server)
421 (let* ((is-old t)
422 ;; The articles we have deleted so far.
423 (deleted-articles nil)
424 ;; The articles that really exist and will
425 ;; be expired if they are old enough.
426 (maybe-expirable
427 (gnus-sorted-intersection articles (nnfolder-existing-articles))))
428 (nnmail-activate 'nnfolder)
429
430 (save-excursion
431 (set-buffer nnfolder-current-buffer)
432 ;; Since messages are sorted in arrival order and expired in the
433 ;; same order, we can stop as soon as we find a message that is
434 ;; too old.
435 (while (and maybe-expirable is-old)
436 (goto-char (point-min))
437 (when (and (nnfolder-goto-article (car maybe-expirable))
438 (search-forward (concat "\n" nnfolder-article-marker)
439 nil t))
440 (forward-sexp)
441 (when (setq is-old
442 (nnmail-expired-article-p
443 newsgroup
444 (buffer-substring
445 (point) (progn (end-of-line) (point)))
446 force nnfolder-inhibit-expiry))
447 (unless (eq nnmail-expiry-target 'delete)
448 (with-temp-buffer
449 (nnfolder-request-article (car maybe-expirable)
450 newsgroup server (current-buffer))
451 (let ((nnfolder-current-directory nil))
452 (nnmail-expiry-target-group
453 nnmail-expiry-target newsgroup)))
454 (nnfolder-possibly-change-group newsgroup server))
455 (nnheader-message 5 "Deleting article %d in %s..."
456 (car maybe-expirable) newsgroup)
457 (nnfolder-delete-mail)
458 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
459 (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
460 ;; Must remember which articles were actually deleted
461 (push (car maybe-expirable) deleted-articles)))
462 (setq maybe-expirable (cdr maybe-expirable)))
463 (unless nnfolder-inhibit-expiry
464 (nnheader-message 5 "Deleting articles...done"))
465 (nnfolder-save-buffer)
466 (nnfolder-adjust-min-active newsgroup)
467 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
468 (gnus-sorted-difference articles (nreverse deleted-articles)))))
469
470 (deffoo nnfolder-request-move-article (article group server
471 accept-form &optional last)
472 (save-excursion
473 (let ((buf (get-buffer-create " *nnfolder move*"))
474 result)
475 (and
476 (nnfolder-request-article article group server)
477 (save-excursion
478 (set-buffer buf)
479 (erase-buffer)
480 (insert-buffer-substring nntp-server-buffer)
481 (goto-char (point-min))
482 (while (re-search-forward
483 (concat "^" nnfolder-article-marker)
484 (save-excursion (and (search-forward "\n\n" nil t) (point)))
485 t)
486 (gnus-delete-line))
487 (setq result (eval accept-form))
488 (kill-buffer buf)
489 result)
490 (save-excursion
491 (nnfolder-possibly-change-group group server)
492 (set-buffer nnfolder-current-buffer)
493 (goto-char (point-min))
494 (when (nnfolder-goto-article article)
495 (nnfolder-delete-mail))
496 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
497 (nnfolder-nov-delete-article group article))
498 (when last
499 (nnfolder-save-buffer)
500 (nnfolder-adjust-min-active group)
501 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
502 result)))
503
504 (deffoo nnfolder-request-accept-article (group &optional server last)
505 (save-excursion
506 (nnfolder-possibly-change-group group server)
507 (nnmail-check-syntax)
508 (let ((buf (current-buffer))
509 result art-group)
510 (goto-char (point-min))
511 (when (looking-at "X-From-Line: ")
512 (replace-match "From ")
513 (while (progn (forward-line) (looking-at "[ \t]"))
514 (delete-char -1)))
515 (with-temp-buffer
516 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
517 (nntp-server-buffer (current-buffer)))
518 (nnmail-find-file nnfolder-active-file)
519 (setq nnfolder-group-alist (nnmail-parse-active))))
520 (save-excursion
521 (goto-char (point-min))
522 (if (search-forward "\n\n" nil t)
523 (forward-line -1)
524 (goto-char (point-max)))
525 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
526 (delete-region (point) (progn (forward-line 1) (point))))
527 (when nnmail-cache-accepted-message-ids
528 (nnmail-cache-insert (nnmail-fetch-field "message-id")
529 group
530 (nnmail-fetch-field "subject")
531 (nnmail-fetch-field "from")))
532 (setq result (if (stringp group)
533 (list (cons group (nnfolder-active-number group)))
534 (setq art-group
535 (nnmail-article-group 'nnfolder-active-number))))
536 (if (and (null result)
537 (yes-or-no-p "Moved to `junk' group; delete article? "))
538 (setq result 'junk)
539 (setq result
540 (car (nnfolder-save-mail result)))))
541 (when last
542 (save-excursion
543 (nnfolder-possibly-change-folder (or (caar art-group) group))
544 (nnfolder-save-buffer)
545 (when nnmail-cache-accepted-message-ids
546 (nnmail-cache-close))))
547 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
548 (unless result
549 (nnheader-report 'nnfolder "Couldn't store article"))
550 result)))
551
552 (deffoo nnfolder-request-replace-article (article group buffer)
553 (nnfolder-possibly-change-group group)
554 (save-excursion
555 (set-buffer buffer)
556 (goto-char (point-min))
557 (if (not (looking-at "X-From-Line: "))
558 (insert "From nobody " (current-time-string) "\n")
559 (replace-match "From ")
560 (forward-line 1)
561 (while (looking-at "[ \t]")
562 (delete-char -1)
563 (forward-line 1)))
564 (nnfolder-normalize-buffer)
565 (set-buffer nnfolder-current-buffer)
566 (goto-char (point-min))
567 (if (not (nnfolder-goto-article article))
568 nil
569 (nnfolder-delete-mail)
570 (insert-buffer-substring buffer)
571 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
572 (save-excursion
573 (set-buffer buffer)
574 (let ((headers (nnfolder-parse-head article
575 (point-min) (point-max))))
576 (with-current-buffer (nnfolder-open-nov group)
577 (if (nnheader-find-nov-line article)
578 (delete-region (point) (progn (forward-line 1) (point))))
579 (nnheader-insert-nov headers)))))
580 (nnfolder-save-buffer)
581 t)))
582
583 (deffoo nnfolder-request-delete-group (group &optional force server)
584 (nnfolder-close-group group server t)
585 ;; Delete all articles in GROUP.
586 (if (not force)
587 () ; Don't delete the articles.
588 ;; Delete the file that holds the group.
589 (let ((data (nnfolder-group-pathname group))
590 (nov (nnfolder-group-nov-pathname group))
591 (mrk (nnfolder-group-marks-pathname group)))
592 (ignore-errors (delete-file data))
593 (ignore-errors (delete-file nov))
594 (ignore-errors (delete-file mrk))))
595 ;; Remove the group from all structures.
596 (setq nnfolder-group-alist
597 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
598 nnfolder-current-group nil
599 nnfolder-current-buffer nil)
600 ;; Save the active file.
601 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
602 t)
603
604 (deffoo nnfolder-request-rename-group (group new-name &optional server)
605 (nnfolder-possibly-change-group group server)
606 (save-excursion
607 (set-buffer nnfolder-current-buffer)
608 (and (file-writable-p buffer-file-name)
609 (ignore-errors
610 (let ((new-file (nnfolder-group-pathname new-name)))
611 (gnus-make-directory (file-name-directory new-file))
612 (rename-file buffer-file-name new-file)
613 (when (file-exists-p (nnfolder-group-nov-pathname group))
614 (setq new-file (nnfolder-group-nov-pathname new-name))
615 (gnus-make-directory (file-name-directory new-file))
616 (rename-file (nnfolder-group-nov-pathname group) new-file))
617 (when (file-exists-p (nnfolder-group-marks-pathname group))
618 (setq new-file (nnfolder-group-marks-pathname new-name))
619 (gnus-make-directory (file-name-directory new-file))
620 (rename-file (nnfolder-group-marks-pathname group) new-file)))
621 t)
622 ;; That went ok, so we change the internal structures.
623 (let ((entry (assoc group nnfolder-group-alist)))
624 (and entry (setcar entry new-name))
625 (setq nnfolder-current-buffer nil
626 nnfolder-current-group nil)
627 ;; Save the new group alist.
628 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
629 ;; We kill the buffer instead of renaming it and stuff.
630 (kill-buffer (current-buffer))
631 t))))
632
633 (deffoo nnfolder-request-regenerate (server)
634 (nnfolder-possibly-change-group nil server)
635 (nnfolder-generate-active-file)
636 t)
637
638 \f
639 ;;; Internal functions.
640
641 (defun nnfolder-adjust-min-active (group)
642 ;; Find the lowest active article in this group.
643 (let* ((active (cadr (assoc group nnfolder-group-alist)))
644 (marker (concat "\n" nnfolder-article-marker))
645 (number "[0-9]+")
646 (activemin (cdr active)))
647 (save-excursion
648 (set-buffer nnfolder-current-buffer)
649 (goto-char (point-min))
650 (while (and (search-forward marker nil t)
651 (re-search-forward number nil t))
652 (let ((newnum (string-to-number (match-string 0))))
653 (if (nnmail-within-headers-p)
654 (setq activemin (min activemin newnum)))))
655 (setcar active activemin))))
656
657 (defun nnfolder-article-string (article)
658 (if (numberp article)
659 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
660 (concat "\nMessage-ID: " article)))
661
662 (defun nnfolder-goto-article (article)
663 "Place point at the start of the headers of ARTICLE.
664 ARTICLE can be an article number or a Message-ID.
665 Returns t if successful, nil otherwise."
666 (let ((art-string (nnfolder-article-string article))
667 start found)
668 ;; It is likely that we are at or before the delimiter line.
669 ;; We therefore go to the end of the previous line, and start
670 ;; searching from there.
671 (beginning-of-line)
672 (unless (bobp)
673 (forward-char -1))
674 (setq start (point))
675 ;; First search forward.
676 (while (and (setq found (search-forward art-string nil t))
677 (not (nnmail-within-headers-p))))
678 ;; If unsuccessful, search backward from where we started,
679 (unless found
680 (goto-char start)
681 (while (and (setq found (search-backward art-string nil t))
682 (not (nnmail-within-headers-p)))))
683 (when found
684 (nnmail-search-unix-mail-delim-backward))))
685
686 (defun nnfolder-delete-mail (&optional leave-delim)
687 "Delete the message that point is in.
688 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
689 deleted. Point is left where the deleted region was."
690 (save-restriction
691 (narrow-to-region
692 (save-excursion
693 ;; In case point is at the beginning of the message already.
694 (forward-line 1)
695 (nnmail-search-unix-mail-delim-backward)
696 (if leave-delim (progn (forward-line 1) (point))
697 (point)))
698 (progn
699 (forward-line 1)
700 (if (nnmail-search-unix-mail-delim)
701 (point)
702 (point-max))))
703 (run-hooks 'nnfolder-delete-mail-hook)
704 (delete-region (point-min) (point-max))))
705
706 (defun nnfolder-possibly-change-group (group &optional server dont-check)
707 ;; Change servers.
708 (when (and server
709 (not (nnfolder-server-opened server)))
710 (nnfolder-open-server server))
711 (unless (gnus-buffer-live-p nnfolder-current-buffer)
712 (setq nnfolder-current-buffer nil
713 nnfolder-current-group nil))
714 ;; Change group.
715 (let ((file-name-coding-system nnmail-pathname-coding-system))
716 (when (and group
717 (not (equal group nnfolder-current-group))
718 (progn
719 (nnmail-activate 'nnfolder)
720 (and (assoc group nnfolder-group-alist)
721 (file-exists-p (nnfolder-group-pathname group)))))
722 (if dont-check
723 (setq nnfolder-current-group group
724 nnfolder-current-buffer nil)
725 (let (inf file)
726 ;; If we have to change groups, see if we don't already have
727 ;; the folder in memory. If we do, verify the modtime and
728 ;; destroy the folder if needed so we can rescan it.
729 (setq nnfolder-current-buffer
730 (nth 1 (assoc group nnfolder-buffer-alist)))
731
732 ;; If the buffer is not live, make sure it isn't in the
733 ;; alist. If it is live, verify that nobody else has
734 ;; touched the file since last time.
735 (when (and nnfolder-current-buffer
736 (not (gnus-buffer-live-p nnfolder-current-buffer)))
737 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
738 nnfolder-current-buffer nil))
739
740 (setq nnfolder-current-group group)
741
742 (when (or (not nnfolder-current-buffer)
743 (not (verify-visited-file-modtime
744 nnfolder-current-buffer)))
745 (save-excursion
746 (setq file (nnfolder-group-pathname group))
747 ;; See whether we need to create the new file.
748 (unless (file-exists-p file)
749 (gnus-make-directory (file-name-directory file))
750 (let ((nnmail-file-coding-system
751 (or nnfolder-file-coding-system-for-write
752 nnfolder-file-coding-system-for-write)))
753 (nnmail-write-region (point-min) (point-min)
754 file t 'nomesg)))
755 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
756 (set-buffer nnfolder-current-buffer)
757 (push (list group nnfolder-current-buffer)
758 nnfolder-buffer-alist)))))))))
759
760 (defun nnfolder-save-mail (group-art-list)
761 "Called narrowed to an article."
762 (let* (save-list group-art)
763 (goto-char (point-min))
764 ;; The From line may have been quoted by movemail.
765 (when (looking-at ">From")
766 (delete-char 1))
767 ;; This might come from somewhere else.
768 (unless (looking-at "From ")
769 (insert "From nobody " (current-time-string) "\n")
770 (goto-char (point-min)))
771 ;; Quote all "From " lines in the article.
772 (forward-line 1)
773 (let (case-fold-search)
774 (while (re-search-forward "^From " nil t)
775 (beginning-of-line)
776 (insert "> ")))
777 (setq save-list group-art-list)
778 (nnmail-insert-lines)
779 (nnmail-insert-xref group-art-list)
780 (run-hooks 'nnmail-prepare-save-mail-hook)
781 (run-hooks 'nnfolder-prepare-save-mail-hook)
782
783 ;; Insert the mail into each of the destination groups.
784 (while (setq group-art (pop group-art-list))
785 ;; Kill any previous newsgroup markers.
786 (goto-char (point-min))
787 (if (search-forward "\n\n" nil t)
788 (forward-line -1)
789 (goto-char (point-max)))
790 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
791 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
792
793 ;; Insert the new newsgroup marker.
794 (nnfolder-insert-newsgroup-line group-art)
795
796 (save-excursion
797 (let ((beg (point-min))
798 (end (point-max))
799 (obuf (current-buffer)))
800 (nnfolder-possibly-change-folder (car group-art))
801 (let ((buffer-read-only nil))
802 (nnfolder-normalize-buffer)
803 (insert-buffer-substring obuf beg end))
804 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
805 (set-buffer obuf)
806 (nnfolder-add-nov (car group-art) (cdr group-art)
807 (nnfolder-parse-head nil beg end))))))
808
809 ;; Did we save it anywhere?
810 save-list))
811
812 (defun nnfolder-normalize-buffer ()
813 "Make sure there are two newlines at the end of the buffer."
814 (goto-char (point-max))
815 (skip-chars-backward "\n")
816 (delete-region (point) (point-max))
817 (unless (bobp)
818 (insert "\n\n")))
819
820 (defun nnfolder-insert-newsgroup-line (group-art)
821 (save-excursion
822 (goto-char (point-min))
823 (unless (search-forward "\n\n" nil t)
824 (goto-char (point-max))
825 (insert "\n"))
826 (forward-char -1)
827 (insert (format (concat nnfolder-article-marker "%d %s\n")
828 (cdr group-art) (current-time-string)))))
829
830 (defun nnfolder-active-number (group)
831 ;; Find the next article number in GROUP.
832 (let ((active (cadr (assoc group nnfolder-group-alist))))
833 (if active
834 (setcdr active (1+ (cdr active)))
835 ;; This group is new, so we create a new entry for it.
836 ;; This might be a bit naughty... creating groups on the drop of
837 ;; a hat, but I don't know...
838 (push (list group (setq active (cons 1 1)))
839 nnfolder-group-alist))
840 (cdr active)))
841
842 (defun nnfolder-possibly-change-folder (group)
843 (let ((inf (assoc group nnfolder-buffer-alist)))
844 (if (and inf
845 (gnus-buffer-live-p (cadr inf)))
846 (set-buffer (cadr inf))
847 (when inf
848 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
849 (when nnfolder-group-alist
850 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
851 (push (list group (nnfolder-read-folder group))
852 nnfolder-buffer-alist))))
853
854 ;; This method has a problem if you've accidentally let the active
855 ;; list get out of sync with the files. This could happen, say, if
856 ;; you've accidentally gotten new mail with something other than Gnus
857 ;; (but why would _that_ ever happen? :-). In that case, we will be
858 ;; in the middle of processing the file, ready to add new X-Gnus
859 ;; article number markers, and we'll run across a message with no ID
860 ;; yet - the active list _may_not_ be ready for us yet.
861
862 ;; To handle this, I'm modifying this routine to maintain the maximum
863 ;; ID seen so far, and when we hit a message with no ID, we will
864 ;; _manually_ scan the rest of the message looking for any more,
865 ;; possibly higher IDs. We'll assume the maximum that we find is the
866 ;; highest active. Note that this shouldn't cost us much extra time
867 ;; at all, but will be a lot less vulnerable to glitches between the
868 ;; mbox and the active file.
869
870 (defun nnfolder-read-folder (group)
871 (let* ((file (nnfolder-group-pathname group))
872 (nov (nnfolder-group-nov-pathname group))
873 (buffer (set-buffer
874 (let ((nnheader-file-coding-system
875 nnfolder-file-coding-system))
876 (nnheader-find-file-noselect file t)))))
877 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
878 (buffer-disable-undo)
879 (if (equal (cadr (assoc group nnfolder-scantime-alist))
880 (nth 5 (file-attributes file)))
881 ;; This looks up-to-date, so we don't do any scanning.
882 (if (file-exists-p file)
883 buffer
884 (push (list group buffer) nnfolder-buffer-alist)
885 (set-buffer-modified-p t)
886 (nnfolder-save-buffer))
887 ;; Parse the damn thing.
888 (save-excursion
889 (goto-char (point-min))
890 ;; Remove any blank lines at the start.
891 (while (eq (following-char) ?\n)
892 (delete-char 1))
893 (nnmail-activate 'nnfolder)
894 ;; Read in the file.
895 (let ((delim "^From ")
896 (marker (concat "\n" nnfolder-article-marker))
897 (number "[0-9]+")
898 (active (or (cadr (assoc group nnfolder-group-alist))
899 (cons 1 0)))
900 (scantime (assoc group nnfolder-scantime-alist))
901 (minid (lsh -1 -1))
902 maxid start end newscantime
903 novbuf articles newnum
904 buffer-read-only)
905 (setq maxid (cdr active))
906
907 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
908 (and (file-exists-p nov)
909 (file-newer-than-file-p nov file)))
910 (unless (file-exists-p nov)
911 (gnus-make-directory (file-name-directory nov)))
912 (with-current-buffer
913 (setq novbuf (nnfolder-open-nov group))
914 (goto-char (point-min))
915 (while (not (eobp))
916 (push (read novbuf) articles)
917 (forward-line 1))
918 (setq articles (nreverse articles))))
919 (goto-char (point-min))
920
921 ;; Anytime the active number is 1 or 0, it is suspect. In
922 ;; that case, search the file manually to find the active
923 ;; number. Or, of course, if we're being paranoid. (This
924 ;; would also be the place to build other lists from the
925 ;; header markers, such as expunge lists, etc., if we ever
926 ;; desired to abandon the active file entirely for mboxes.)
927 (when (or nnfolder-ignore-active-file
928 novbuf
929 (< maxid 2))
930 (while (and (search-forward marker nil t)
931 (looking-at number))
932 (setq newnum (string-to-number (match-string 0)))
933 (when (nnmail-within-headers-p)
934 (setq maxid (max maxid newnum)
935 minid (min minid newnum))
936 (when novbuf
937 (if (memq newnum articles)
938 (setq articles (delq newnum articles))
939 (let ((headers (nnfolder-parse-head newnum)))
940 (with-current-buffer novbuf
941 (nnheader-find-nov-line newnum)
942 (nnheader-insert-nov headers)))))))
943 (when (and novbuf articles)
944 (with-current-buffer novbuf
945 (dolist (article articles)
946 (when (nnheader-find-nov-line article)
947 (delete-region (point)
948 (progn (forward-line 1) (point)))))))
949 (setcar active (max 1 (min minid maxid)))
950 (setcdr active (max maxid (cdr active)))
951 (goto-char (point-min)))
952
953 ;; As long as we trust that the user will only insert
954 ;; unmarked mail at the end, go to the end and search
955 ;; backwards for the last marker. Find the start of that
956 ;; message, and begin to search for unmarked messages from
957 ;; there.
958 (when (not (or nnfolder-distrust-mbox
959 (< maxid 2)))
960 (goto-char (point-max))
961 (unless (re-search-backward marker nil t)
962 (goto-char (point-min)))
963 ;;(when (nnmail-search-unix-mail-delim)
964 ;; (goto-char (point-min)))
965 )
966
967 ;; Keep track of the active number on our own, and insert it
968 ;; back into the active list when we're done. Also, prime
969 ;; the pump to cut down on the number of searches we do.
970 (unless (nnmail-search-unix-mail-delim)
971 (goto-char (point-max)))
972 (setq end (point-marker))
973 (while (not (= end (point-max)))
974 (setq start (marker-position end))
975 (goto-char end)
976 ;; There may be more than one "From " line, so we skip past
977 ;; them.
978 (while (looking-at delim)
979 (forward-line 1))
980 (set-marker end (if (nnmail-search-unix-mail-delim)
981 (point)
982 (point-max)))
983 (goto-char start)
984 (when (not (search-forward marker end t))
985 (narrow-to-region start end)
986 (nnmail-insert-lines)
987 (nnfolder-insert-newsgroup-line
988 (cons nil
989 (setq newnum
990 (nnfolder-active-number group))))
991 (when novbuf
992 (let ((headers (nnfolder-parse-head newnum (point-min)
993 (point-max))))
994 (with-current-buffer novbuf
995 (goto-char (point-max))
996 (nnheader-insert-nov headers))))
997 (widen)))
998
999 (set-marker end nil)
1000 ;; Make absolutely sure that the active list reflects
1001 ;; reality!
1002 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
1003
1004 ;; Set the scantime for this group.
1005 (setq newscantime (visited-file-modtime))
1006 (if scantime
1007 (setcdr scantime (list newscantime))
1008 (push (list group newscantime)
1009 nnfolder-scantime-alist))
1010 ;; Save nov.
1011 (when novbuf
1012 (nnfolder-save-nov))
1013 (current-buffer))))))
1014
1015 ;;;###autoload
1016 (defun nnfolder-generate-active-file ()
1017 "Look for mbox folders in the nnfolder directory and make them into groups.
1018 This command does not work if you use short group names."
1019 (interactive)
1020 (nnmail-activate 'nnfolder)
1021 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1022 (dolist (file (directory-files (or nnfolder-nov-directory
1023 nnfolder-directory)
1024 t
1025 (concat
1026 (regexp-quote nnfolder-nov-file-suffix)
1027 "$")))
1028 (when (not (message-mail-file-mbox-p file))
1029 (ignore-errors
1030 (delete-file file)))))
1031 (let ((files (directory-files nnfolder-directory))
1032 file)
1033 (while (setq file (pop files))
1034 (when (and (not (backup-file-name-p file))
1035 (message-mail-file-mbox-p
1036 (nnheader-concat nnfolder-directory file)))
1037 (let ((oldgroup (assoc file nnfolder-group-alist)))
1038 (if oldgroup
1039 (nnheader-message 5 "Refreshing group %s..." file)
1040 (nnheader-message 5 "Adding group %s..." file))
1041 (if oldgroup
1042 (setq nnfolder-group-alist
1043 (delq oldgroup (copy-sequence nnfolder-group-alist))))
1044 (push (list file (cons 1 0)) nnfolder-group-alist)
1045 (nnfolder-possibly-change-folder file)
1046 (nnfolder-possibly-change-group file)
1047 (nnfolder-close-group file))))
1048 (nnheader-message 5 "")))
1049
1050 (defun nnfolder-group-pathname (group)
1051 "Make file name for GROUP."
1052 (setq group
1053 (mm-encode-coding-string group nnmail-pathname-coding-system))
1054 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
1055 ;; If this file exists, we use it directly.
1056 (if (or nnmail-use-long-file-names
1057 (file-exists-p (concat dir group)))
1058 (concat dir group)
1059 ;; If not, we translate dots into slashes.
1060 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
1061
1062 (defun nnfolder-group-nov-pathname (group)
1063 "Make pathname for GROUP NOV."
1064 (let ((nnfolder-directory
1065 (or nnfolder-nov-directory nnfolder-directory)))
1066 (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
1067
1068 (defun nnfolder-save-buffer ()
1069 "Save the buffer."
1070 (when (buffer-modified-p)
1071 (run-hooks 'nnfolder-save-buffer-hook)
1072 (gnus-make-directory (file-name-directory (buffer-file-name)))
1073 (let ((coding-system-for-write
1074 (or nnfolder-file-coding-system-for-write
1075 nnfolder-file-coding-system)))
1076 (save-buffer)))
1077 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1078 (nnfolder-save-nov)))
1079
1080 (defun nnfolder-save-active (group-alist active-file)
1081 (let ((nnmail-active-file-coding-system
1082 (or nnfolder-active-file-coding-system-for-write
1083 nnfolder-active-file-coding-system)))
1084 (nnmail-save-active group-alist active-file)))
1085
1086 (defun nnfolder-open-nov (group)
1087 (or (cdr (assoc group nnfolder-nov-buffer-alist))
1088 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1089 (save-excursion
1090 (set-buffer buffer)
1091 (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1092 (nnfolder-group-nov-pathname group))
1093 (erase-buffer)
1094 (when (file-exists-p nnfolder-nov-buffer-file-name)
1095 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1096 (push (cons group buffer) nnfolder-nov-buffer-alist)
1097 buffer)))
1098
1099 (defun nnfolder-save-nov ()
1100 (save-excursion
1101 (while nnfolder-nov-buffer-alist
1102 (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1103 (set-buffer (cdar nnfolder-nov-buffer-alist))
1104 (when (buffer-modified-p)
1105 (gnus-make-directory (file-name-directory
1106 nnfolder-nov-buffer-file-name))
1107 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1108 nil 'nomesg))
1109 (set-buffer-modified-p nil)
1110 (kill-buffer (current-buffer)))
1111 (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1112
1113 (defun nnfolder-nov-delete-article (group article)
1114 (save-excursion
1115 (set-buffer (nnfolder-open-nov group))
1116 (when (nnheader-find-nov-line article)
1117 (delete-region (point) (progn (forward-line 1) (point))))
1118 t))
1119
1120 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1121 (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1122 nil
1123 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1124 (when (file-exists-p nov)
1125 (save-excursion
1126 (set-buffer nntp-server-buffer)
1127 (erase-buffer)
1128 (nnheader-insert-file-contents nov)
1129 (if (and fetch-old
1130 (not (numberp fetch-old)))
1131 t ; Don't remove anything.
1132 (nnheader-nov-delete-outside-range
1133 (if fetch-old (max 1 (- (car articles) fetch-old))
1134 (car articles))
1135 (car (last articles)))
1136 t))))))
1137
1138 (defun nnfolder-parse-head (&optional number b e)
1139 "Parse the head of the current buffer."
1140 (let ((buf (current-buffer))
1141 chars)
1142 (save-excursion
1143 (unless b
1144 (setq b (if (nnmail-search-unix-mail-delim-backward)
1145 (point) (point-min)))
1146 (forward-line 1)
1147 (setq e (if (nnmail-search-unix-mail-delim)
1148 (point) (point-max))))
1149 (setq chars (- e b))
1150 (unless (zerop chars)
1151 (goto-char b)
1152 (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1153 (with-temp-buffer
1154 (insert-buffer-substring buf b e)
1155 (let ((headers (nnheader-parse-naked-head)))
1156 (mail-header-set-chars headers chars)
1157 (mail-header-set-number headers number)
1158 headers)))))
1159
1160 (defun nnfolder-add-nov (group article headers)
1161 "Add a nov line for the GROUP base."
1162 (save-excursion
1163 (set-buffer (nnfolder-open-nov group))
1164 (goto-char (point-max))
1165 (mail-header-set-number headers article)
1166 (nnheader-insert-nov headers)))
1167
1168 (deffoo nnfolder-request-set-mark (group actions &optional server)
1169 (when (and server
1170 (not (nnfolder-server-opened server)))
1171 (nnfolder-open-server server))
1172 (unless nnfolder-marks-is-evil
1173 (nnfolder-open-marks group server)
1174 (dolist (action actions)
1175 (let ((range (nth 0 action))
1176 (what (nth 1 action))
1177 (marks (nth 2 action)))
1178 (assert (or (eq what 'add) (eq what 'del)) nil
1179 "Unknown request-set-mark action: %s" what)
1180 (dolist (mark marks)
1181 (setq nnfolder-marks (gnus-update-alist-soft
1182 mark
1183 (funcall (if (eq what 'add) 'gnus-range-add
1184 'gnus-remove-from-range)
1185 (cdr (assoc mark nnfolder-marks)) range)
1186 nnfolder-marks)))))
1187 (nnfolder-save-marks group server))
1188 nil)
1189
1190 (deffoo nnfolder-request-update-info (group info &optional server)
1191 ;; Change servers.
1192 (when (and server
1193 (not (nnfolder-server-opened server)))
1194 (nnfolder-open-server server))
1195 (when (and (not nnfolder-marks-is-evil) (nnfolder-marks-changed-p group))
1196 (nnheader-message 8 "Updating marks for %s..." group)
1197 (nnfolder-open-marks group server)
1198 ;; Update info using `nnfolder-marks'.
1199 (mapcar (lambda (pred)
1200 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1201 (gnus-info-set-marks
1202 info
1203 (gnus-update-alist-soft
1204 (cdr pred)
1205 (cdr (assq (cdr pred) nnfolder-marks))
1206 (gnus-info-marks info))
1207 t)))
1208 gnus-article-mark-lists)
1209 (let ((seen (cdr (assq 'read nnfolder-marks))))
1210 (gnus-info-set-read info
1211 (if (and (integerp (car seen))
1212 (null (cdr seen)))
1213 (list (cons (car seen) (car seen)))
1214 seen)))
1215 (nnheader-message 8 "Updating marks for %s...done" group))
1216 info)
1217
1218 (defun nnfolder-group-marks-pathname (group)
1219 "Make pathname for GROUP NOV."
1220 (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory)))
1221 (concat (nnfolder-group-pathname group) nnfolder-marks-file-suffix)))
1222
1223 (defun nnfolder-marks-changed-p (group)
1224 (let ((file (nnfolder-group-marks-pathname group)))
1225 (if (null (gnus-gethash file nnfolder-marks-modtime))
1226 t ;; never looked at marks file, assume it has changed
1227 (not (equal (gnus-gethash file nnfolder-marks-modtime)
1228 (nth 5 (file-attributes file)))))))
1229
1230 (defun nnfolder-save-marks (group server)
1231 (let ((file-name-coding-system nnmail-pathname-coding-system)
1232 (file (nnfolder-group-marks-pathname group)))
1233 (condition-case err
1234 (progn
1235 (with-temp-file file
1236 (erase-buffer)
1237 (gnus-prin1 nnfolder-marks)
1238 (insert "\n"))
1239 (gnus-sethash file
1240 (nth 5 (file-attributes file))
1241 nnfolder-marks-modtime))
1242 (error (or (gnus-yes-or-no-p
1243 (format "Could not write to %s (%s). Continue? " file err))
1244 (error "Cannot write to %s (%s)" file err))))))
1245
1246 (defun nnfolder-open-marks (group server)
1247 (let ((file (nnfolder-group-marks-pathname group)))
1248 (if (file-exists-p file)
1249 (condition-case err
1250 (with-temp-buffer
1251 (gnus-sethash file (nth 5 (file-attributes file))
1252 nnfolder-marks-modtime)
1253 (nnheader-insert-file-contents file)
1254 (setq nnfolder-marks (read (current-buffer)))
1255 (dolist (el gnus-article-unpropagated-mark-lists)
1256 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks))))
1257 (error (or (gnus-yes-or-no-p
1258 (format "Error reading nnfolder marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1259 (error "Cannot read nnfolder marks file %s (%s)" file err))))
1260 ;; User didn't have a .marks file. Probably first time
1261 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1262 (let ((info (gnus-get-info
1263 (gnus-group-prefixed-name
1264 group
1265 (gnus-server-to-method (format "nnfolder:%s" server))))))
1266 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1267 (setq nnfolder-marks (gnus-info-marks info))
1268 (push (cons 'read (gnus-info-read info)) nnfolder-marks)
1269 (dolist (el gnus-article-unpropagated-mark-lists)
1270 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks)))
1271 (nnfolder-save-marks group server)
1272 (nnheader-message 7 "Bootstrapping marks for %s...done" group)))))
1273
1274 (provide 'nnfolder)
1275
1276 ;;; arch-tag: a040d0f4-4f4e-445f-8972-839575c5f7e6
1277 ;;; nnfolder.el ends here