Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
[bpt/emacs.git] / lisp / gnus / nnfolder.el
CommitLineData
eec82323 1;;; nnfolder.el --- mail folder access for Gnus
23f87bed 2;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
16409b0b 3;; Free Software Foundation, Inc.
eec82323 4
23f87bed
MB
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>
6748645f 8;; Lars Magne Ingebrigtsen <larsi@gnus.org>
23f87bed 9;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
eec82323
LMI
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., 59 Temple Place - Suite 330,
27;; Boston, MA 02111-1307, USA.
28
29;;; Commentary:
30
31;;; Code:
32
33(require 'nnheader)
34(require 'message)
35(require 'nnmail)
36(require 'nnoo)
6748645f 37(eval-when-compile (require 'cl))
23f87bed 38(require 'gnus)
eec82323 39(require 'gnus-util)
0d972486 40(require 'gnus-range)
eec82323 41
23f87bed
MB
42(eval-and-compile
43 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
44 (autoload 'gnus-intersection "gnus-range"))
45
eec82323
LMI
46(nnoo-declare nnfolder)
47
48(defvoo nnfolder-directory (expand-file-name message-directory)
49 "The name of the nnfolder directory.")
50
23f87bed
MB
51(defvoo nnfolder-nov-directory nil
52 "The name of the nnfolder NOV directory.
53If nil, `nnfolder-directory' is used.")
54
55(defvoo nnfolder-marks-directory nil
56 "The name of the nnfolder MARKS directory.
57If nil, `nnfolder-directory' is used.")
58
eec82323 59(defvoo nnfolder-active-file
16409b0b 60 (nnheader-concat nnfolder-directory "active")
eec82323
LMI
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
dcf4ae73 67 "If non-nil, the active file is ignored.
16409b0b
GM
68This causes nnfolder to do some extra work in order to determine the
69true active ranges of an mbox file. Note that the active file is
dcf4ae73 70still saved, but its values are not used. This costs some extra time
16409b0b 71when scanning an mbox when opening it.")
eec82323
LMI
72
73(defvoo nnfolder-distrust-mbox nil
16409b0b
GM
74 "If non-nil, the folder will be distrusted.
75This means that nnfolder will not trust the user with respect to
76inserting unaccounted for mail in the middle of an mbox file. This
77can greatly slow down scans, which now must scan the entire file for
78unmarked messages. When nil, scans occur forward from the last marked
79message, a huge time saver for large mailboxes.")
eec82323
LMI
80
81(defvoo nnfolder-newsgroups-file
16409b0b 82 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
eec82323
LMI
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
23f87bed 94
eec82323
LMI
95(defvoo nnfolder-inhibit-expiry nil
96 "If non-nil, inhibit expiry.")
97
98\f
99
23f87bed 100(defconst nnfolder-version "nnfolder 2.0"
eec82323
LMI
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)
16409b0b 113(defvoo nnfolder-active-file-coding-system mm-text-coding-system)
a1506d29 114(defvoo nnfolder-active-file-coding-system-for-write
16409b0b
GM
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.
23f87bed
MB
119if 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.
123Using nov databases will speed up header fetching considerably.
124This variable shouldn't be flipped much. If you have, for some reason,
125set this to t, and want to set it to nil again, you should always run
126the `nnfolder-generate-active-file' command. The function will go
127through all nnfolder directories and generate nov databases for them
128all. 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.
138Using marks files makes it possible to backup and restore mail groups
139separately from `.newsrc.eld'. If you have, for some reason, set
140this to t, and want to set it to nil again, you should always remove
141the corresponding marks file (usually base nnfolder file name
142concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
143the 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))
eec82323
LMI
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)
23f87bed 161 (let (article start stop num)
eec82323
LMI
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
23f87bed
MB
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-int
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-int
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))))))
eec82323
LMI
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)
23f87bed
MB
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)))
eec82323
LMI
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))
6748645f 265 (when (nnfolder-goto-article article)
eec82323 266 (let (start stop)
eec82323
LMI
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))
eec82323 285 (cons nnfolder-current-group
a1506d29 286 (if (search-forward (concat "\n" nnfolder-article-marker)
16409b0b 287 nil t)
23f87bed
MB
288 (string-to-int (buffer-substring
289 (point) (gnus-point-at-eol)))
16409b0b 290 -1))))))))
eec82323
LMI
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 group
374 (unless (assoc group nnfolder-group-alist)
375 (push (list group (cons 1 0)) nnfolder-group-alist)
16409b0b 376 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
a8151ef7 377 (nnfolder-read-folder group)))
eec82323
LMI
378 t)
379
380(deffoo nnfolder-request-list (&optional server)
381 (nnfolder-possibly-change-group nil server)
382 (save-excursion
16409b0b 383 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
58724016
KH
384 (nnmail-find-file nnfolder-active-file)
385 (setq nnfolder-group-alist (nnmail-get-active)))
eec82323
LMI
386 t))
387
388(deffoo nnfolder-request-newgroups (date &optional server)
389 (nnfolder-possibly-change-group nil server)
390 (nnfolder-request-list server))
391
392(deffoo nnfolder-request-list-newsgroups (&optional server)
393 (nnfolder-possibly-change-group nil server)
394 (save-excursion
16409b0b
GM
395 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
396 (nnmail-find-file nnfolder-newsgroups-file))))
397
398;; Return a list consisting of all article numbers existing in the
399;; current folder.
400
401(defun nnfolder-existing-articles ()
402 (save-excursion
403 (when nnfolder-current-buffer
404 (set-buffer nnfolder-current-buffer)
405 (goto-char (point-min))
406 (let ((marker (concat "\n" nnfolder-article-marker))
407 (number "[0-9]+")
408 numbers)
16409b0b
GM
409 (while (and (search-forward marker nil t)
410 (re-search-forward number nil t))
411 (let ((newnum (string-to-number (match-string 0))))
412 (if (nnmail-within-headers-p)
413 (push newnum numbers))))
23f87bed
MB
414 ;; The article numbers are increasing, so this result is sorted.
415 (nreverse numbers)))))
eec82323
LMI
416
417(deffoo nnfolder-request-expire-articles
16409b0b 418 (articles newsgroup &optional server force)
eec82323
LMI
419 (nnfolder-possibly-change-group newsgroup server)
420 (let* ((is-old t)
16409b0b
GM
421 ;; The articles we have deleted so far.
422 (deleted-articles nil)
423 ;; The articles that really exist and will
424 ;; be expired if they are old enough.
425 (maybe-expirable
23f87bed 426 (gnus-sorted-intersection articles (nnfolder-existing-articles))))
eec82323
LMI
427 (nnmail-activate 'nnfolder)
428
429 (save-excursion
430 (set-buffer nnfolder-current-buffer)
16409b0b
GM
431 ;; Since messages are sorted in arrival order and expired in the
432 ;; same order, we can stop as soon as we find a message that is
433 ;; too old.
434 (while (and maybe-expirable is-old)
eec82323 435 (goto-char (point-min))
16409b0b
GM
436 (when (and (nnfolder-goto-article (car maybe-expirable))
437 (search-forward (concat "\n" nnfolder-article-marker)
438 nil t))
439 (forward-sexp)
440 (when (setq is-old
441 (nnmail-expired-article-p
442 newsgroup
443 (buffer-substring
444 (point) (progn (end-of-line) (point)))
445 force nnfolder-inhibit-expiry))
0d972486
SZ
446 (unless (eq nnmail-expiry-target 'delete)
447 (with-temp-buffer
a1506d29 448 (nnfolder-request-article (car maybe-expirable)
0d972486 449 newsgroup server (current-buffer))
23f87bed 450 (let ((nnfolder-current-directory nil))
0d972486 451 (nnmail-expiry-target-group
23f87bed
MB
452 nnmail-expiry-target newsgroup)))
453 (nnfolder-possibly-change-group newsgroup server))
0d972486 454 (nnheader-message 5 "Deleting article %d in %s..."
16409b0b
GM
455 (car maybe-expirable) newsgroup)
456 (nnfolder-delete-mail)
23f87bed
MB
457 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
458 (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
16409b0b
GM
459 ;; Must remember which articles were actually deleted
460 (push (car maybe-expirable) deleted-articles)))
461 (setq maybe-expirable (cdr maybe-expirable)))
eec82323
LMI
462 (unless nnfolder-inhibit-expiry
463 (nnheader-message 5 "Deleting articles...done"))
464 (nnfolder-save-buffer)
465 (nnfolder-adjust-min-active newsgroup)
16409b0b 466 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
23f87bed 467 (gnus-sorted-difference articles (nreverse deleted-articles)))))
eec82323 468
6748645f
LMI
469(deffoo nnfolder-request-move-article (article group server
470 accept-form &optional last)
471 (save-excursion
472 (let ((buf (get-buffer-create " *nnfolder move*"))
473 result)
474 (and
475 (nnfolder-request-article article group server)
476 (save-excursion
477 (set-buffer buf)
6748645f
LMI
478 (erase-buffer)
479 (insert-buffer-substring nntp-server-buffer)
480 (goto-char (point-min))
481 (while (re-search-forward
482 (concat "^" nnfolder-article-marker)
a1506d29 483 (save-excursion (and (search-forward "\n\n" nil t) (point)))
16409b0b 484 t)
23f87bed 485 (gnus-delete-line))
6748645f
LMI
486 (setq result (eval accept-form))
487 (kill-buffer buf)
488 result)
489 (save-excursion
490 (nnfolder-possibly-change-group group server)
491 (set-buffer nnfolder-current-buffer)
492 (goto-char (point-min))
493 (when (nnfolder-goto-article article)
494 (nnfolder-delete-mail))
23f87bed
MB
495 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
496 (nnfolder-nov-delete-article group article))
6748645f
LMI
497 (when last
498 (nnfolder-save-buffer)
499 (nnfolder-adjust-min-active group)
16409b0b 500 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
6748645f 501 result)))
eec82323
LMI
502
503(deffoo nnfolder-request-accept-article (group &optional server last)
6748645f
LMI
504 (save-excursion
505 (nnfolder-possibly-change-group group server)
506 (nnmail-check-syntax)
507 (let ((buf (current-buffer))
508 result art-group)
509 (goto-char (point-min))
510 (when (looking-at "X-From-Line: ")
f9936da6
SZ
511 (replace-match "From ")
512 (while (progn (forward-line) (looking-at "[ \t]"))
513 (delete-char -1)))
23f87bed
MB
514 (with-temp-buffer
515 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
516 (nntp-server-buffer (current-buffer)))
517 (nnmail-find-file nnfolder-active-file)
518 (setq nnfolder-group-alist (nnmail-parse-active))))
519 (save-excursion
520 (goto-char (point-min))
521 (if (search-forward "\n\n" nil t)
522 (forward-line -1)
523 (goto-char (point-max)))
524 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
525 (delete-region (point) (progn (forward-line 1) (point))))
526 (when nnmail-cache-accepted-message-ids
527 (nnmail-cache-insert (nnmail-fetch-field "message-id")
528 group
529 (nnmail-fetch-field "subject")
530 (nnmail-fetch-field "from")))
531 (setq result (if (stringp group)
532 (list (cons group (nnfolder-active-number group)))
533 (setq art-group
534 (nnmail-article-group 'nnfolder-active-number))))
535 (if (and (null result)
536 (yes-or-no-p "Moved to `junk' group; delete article? "))
537 (setq result 'junk)
538 (setq result
539 (car (nnfolder-save-mail result)))))
540 (when last
541 (save-excursion
542 (nnfolder-possibly-change-folder (or (caar art-group) group))
543 (nnfolder-save-buffer)
544 (when nnmail-cache-accepted-message-ids
545 (nnmail-cache-close))))
16409b0b 546 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
6748645f
LMI
547 (unless result
548 (nnheader-report 'nnfolder "Couldn't store article"))
549 result)))
eec82323
LMI
550
551(deffoo nnfolder-request-replace-article (article group buffer)
552 (nnfolder-possibly-change-group group)
553 (save-excursion
6748645f
LMI
554 (set-buffer buffer)
555 (goto-char (point-min))
23f87bed
MB
556 (if (not (looking-at "X-From-Line: "))
557 (insert "From nobody " (current-time-string) "\n")
558 (replace-match "From ")
559 (forward-line 1)
560 (while (looking-at "[ \t]")
561 (delete-char -1)
562 (forward-line 1)))
6748645f 563 (nnfolder-normalize-buffer)
eec82323
LMI
564 (set-buffer nnfolder-current-buffer)
565 (goto-char (point-min))
6748645f 566 (if (not (nnfolder-goto-article article))
eec82323 567 nil
6748645f 568 (nnfolder-delete-mail)
eec82323 569 (insert-buffer-substring buffer)
23f87bed
MB
570 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
571 (save-excursion
572 (set-buffer buffer)
573 (let ((headers (nnfolder-parse-head article
574 (point-min) (point-max))))
575 (with-current-buffer (nnfolder-open-nov group)
576 (if (nnheader-find-nov-line article)
577 (delete-region (point) (progn (forward-line 1) (point))))
578 (nnheader-insert-nov headers)))))
eec82323
LMI
579 (nnfolder-save-buffer)
580 t)))
581
582(deffoo nnfolder-request-delete-group (group &optional force server)
583 (nnfolder-close-group group server t)
584 ;; Delete all articles in GROUP.
585 (if (not force)
586 () ; Don't delete the articles.
587 ;; Delete the file that holds the group.
23f87bed
MB
588 (let ((data (nnfolder-group-pathname group))
589 (nov (nnfolder-group-nov-pathname group))
590 (mrk (nnfolder-group-marks-pathname group)))
591 (ignore-errors (delete-file data))
592 (ignore-errors (delete-file nov))
593 (ignore-errors (delete-file mrk))))
eec82323
LMI
594 ;; Remove the group from all structures.
595 (setq nnfolder-group-alist
596 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
597 nnfolder-current-group nil
598 nnfolder-current-buffer nil)
599 ;; Save the active file.
16409b0b 600 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
eec82323
LMI
601 t)
602
603(deffoo nnfolder-request-rename-group (group new-name &optional server)
604 (nnfolder-possibly-change-group group server)
605 (save-excursion
606 (set-buffer nnfolder-current-buffer)
607 (and (file-writable-p buffer-file-name)
608 (ignore-errors
23f87bed
MB
609 (let ((new-file (nnfolder-group-pathname new-name)))
610 (gnus-make-directory (file-name-directory new-file))
611 (rename-file buffer-file-name new-file)
612 (when (file-exists-p (nnfolder-group-nov-pathname group))
613 (setq new-file (nnfolder-group-nov-pathname new-name))
614 (gnus-make-directory (file-name-directory new-file))
615 (rename-file (nnfolder-group-nov-pathname group) new-file))
616 (when (file-exists-p (nnfolder-group-marks-pathname group))
617 (setq new-file (nnfolder-group-marks-pathname new-name))
618 (gnus-make-directory (file-name-directory new-file))
619 (rename-file (nnfolder-group-marks-pathname group) new-file)))
eec82323
LMI
620 t)
621 ;; That went ok, so we change the internal structures.
622 (let ((entry (assoc group nnfolder-group-alist)))
623 (and entry (setcar entry new-name))
624 (setq nnfolder-current-buffer nil
625 nnfolder-current-group nil)
626 ;; Save the new group alist.
16409b0b 627 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
eec82323
LMI
628 ;; We kill the buffer instead of renaming it and stuff.
629 (kill-buffer (current-buffer))
630 t))))
631
23f87bed 632(deffoo nnfolder-request-regenerate (server)
a8151ef7
LMI
633 (nnfolder-possibly-change-group nil server)
634 (nnfolder-generate-active-file)
635 t)
636
eec82323
LMI
637\f
638;;; Internal functions.
639
640(defun nnfolder-adjust-min-active (group)
641 ;; Find the lowest active article in this group.
642 (let* ((active (cadr (assoc group nnfolder-group-alist)))
643 (marker (concat "\n" nnfolder-article-marker))
644 (number "[0-9]+")
645 (activemin (cdr active)))
646 (save-excursion
647 (set-buffer nnfolder-current-buffer)
648 (goto-char (point-min))
649 (while (and (search-forward marker nil t)
650 (re-search-forward number nil t))
6748645f
LMI
651 (let ((newnum (string-to-number (match-string 0))))
652 (if (nnmail-within-headers-p)
653 (setq activemin (min activemin newnum)))))
eec82323
LMI
654 (setcar active activemin))))
655
656(defun nnfolder-article-string (article)
657 (if (numberp article)
658 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
659 (concat "\nMessage-ID: " article)))
660
6748645f
LMI
661(defun nnfolder-goto-article (article)
662 "Place point at the start of the headers of ARTICLE.
663ARTICLE can be an article number or a Message-ID.
664Returns t if successful, nil otherwise."
665 (let ((art-string (nnfolder-article-string article))
666 start found)
667 ;; It is likely that we are at or before the delimiter line.
668 ;; We therefore go to the end of the previous line, and start
669 ;; searching from there.
670 (beginning-of-line)
671 (unless (bobp)
672 (forward-char -1))
673 (setq start (point))
674 ;; First search forward.
675 (while (and (setq found (search-forward art-string nil t))
676 (not (nnmail-within-headers-p))))
677 ;; If unsuccessful, search backward from where we started,
678 (unless found
679 (goto-char start)
680 (while (and (setq found (search-backward art-string nil t))
681 (not (nnmail-within-headers-p)))))
682 (when found
683 (nnmail-search-unix-mail-delim-backward))))
684
685(defun nnfolder-delete-mail (&optional leave-delim)
686 "Delete the message that point is in.
687If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
688deleted. Point is left where the deleted region was."
16409b0b
GM
689 (save-restriction
690 (narrow-to-region
691 (save-excursion
692 ;; In case point is at the beginning of the message already.
693 (forward-line 1)
694 (nnmail-search-unix-mail-delim-backward)
695 (if leave-delim (progn (forward-line 1) (point))
696 (point)))
697 (progn
698 (forward-line 1)
699 (if (nnmail-search-unix-mail-delim)
700 (point)
701 (point-max))))
702 (run-hooks 'nnfolder-delete-mail-hook)
703 (delete-region (point-min) (point-max))))
eec82323
LMI
704
705(defun nnfolder-possibly-change-group (group &optional server dont-check)
706 ;; Change servers.
707 (when (and server
708 (not (nnfolder-server-opened server)))
709 (nnfolder-open-server server))
710 (unless (gnus-buffer-live-p nnfolder-current-buffer)
711 (setq nnfolder-current-buffer nil
712 nnfolder-current-group nil))
713 ;; Change group.
23f87bed
MB
714 (let ((file-name-coding-system nnmail-pathname-coding-system))
715 (when (and group
716 (not (equal group nnfolder-current-group))
717 (progn
718 (nnmail-activate 'nnfolder)
719 (and (assoc group nnfolder-group-alist)
720 (file-exists-p (nnfolder-group-pathname group)))))
58724016 721 (if dont-check
a8151ef7
LMI
722 (setq nnfolder-current-group group
723 nnfolder-current-buffer nil)
58724016 724 (let (inf file)
23f87bed
MB
725 ;; If we have to change groups, see if we don't already have
726 ;; the folder in memory. If we do, verify the modtime and
727 ;; destroy the folder if needed so we can rescan it.
a8151ef7
LMI
728 (setq nnfolder-current-buffer
729 (nth 1 (assoc group nnfolder-buffer-alist)))
730
23f87bed
MB
731 ;; If the buffer is not live, make sure it isn't in the
732 ;; alist. If it is live, verify that nobody else has
733 ;; touched the file since last time.
58724016
KH
734 (when (and nnfolder-current-buffer
735 (not (gnus-buffer-live-p nnfolder-current-buffer)))
736 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
737 nnfolder-current-buffer nil))
a8151ef7 738
58724016 739 (setq nnfolder-current-group group)
a8151ef7 740
58724016 741 (when (or (not nnfolder-current-buffer)
6748645f
LMI
742 (not (verify-visited-file-modtime
743 nnfolder-current-buffer)))
58724016
KH
744 (save-excursion
745 (setq file (nnfolder-group-pathname group))
746 ;; See whether we need to create the new file.
747 (unless (file-exists-p file)
748 (gnus-make-directory (file-name-directory file))
a1506d29 749 (let ((nnmail-file-coding-system
16409b0b
GM
750 (or nnfolder-file-coding-system-for-write
751 nnfolder-file-coding-system-for-write)))
be1ad767
SM
752 (nnmail-write-region (point-min) (point-min)
753 file t 'nomesg)))
58724016
KH
754 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
755 (set-buffer nnfolder-current-buffer)
756 (push (list group nnfolder-current-buffer)
757 nnfolder-buffer-alist)))))))))
eec82323
LMI
758
759(defun nnfolder-save-mail (group-art-list)
760 "Called narrowed to an article."
761 (let* (save-list group-art)
762 (goto-char (point-min))
763 ;; The From line may have been quoted by movemail.
16409b0b 764 (when (looking-at ">From")
eec82323
LMI
765 (delete-char 1))
766 ;; This might come from somewhere else.
16409b0b 767 (unless (looking-at "From ")
eec82323
LMI
768 (insert "From nobody " (current-time-string) "\n")
769 (goto-char (point-min)))
6748645f 770 ;; Quote all "From " lines in the article.
16409b0b 771 (forward-line 1)
eec82323
LMI
772 (let (case-fold-search)
773 (while (re-search-forward "^From " nil t)
774 (beginning-of-line)
775 (insert "> ")))
776 (setq save-list group-art-list)
777 (nnmail-insert-lines)
778 (nnmail-insert-xref group-art-list)
779 (run-hooks 'nnmail-prepare-save-mail-hook)
780 (run-hooks 'nnfolder-prepare-save-mail-hook)
781
782 ;; Insert the mail into each of the destination groups.
783 (while (setq group-art (pop group-art-list))
784 ;; Kill any previous newsgroup markers.
785 (goto-char (point-min))
16409b0b
GM
786 (if (search-forward "\n\n" nil t)
787 (forward-line -1)
788 (goto-char (point-max)))
eec82323
LMI
789 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
790 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
791
792 ;; Insert the new newsgroup marker.
793 (nnfolder-insert-newsgroup-line group-art)
794
795 (save-excursion
796 (let ((beg (point-min))
797 (end (point-max))
798 (obuf (current-buffer)))
799 (nnfolder-possibly-change-folder (car group-art))
800 (let ((buffer-read-only nil))
6748645f 801 (nnfolder-normalize-buffer)
23f87bed
MB
802 (insert-buffer-substring obuf beg end))
803 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
804 (set-buffer obuf)
805 (nnfolder-add-nov (car group-art) (cdr group-art)
806 (nnfolder-parse-head nil beg end))))))
eec82323
LMI
807
808 ;; Did we save it anywhere?
809 save-list))
810
6748645f
LMI
811(defun nnfolder-normalize-buffer ()
812 "Make sure there are two newlines at the end of the buffer."
813 (goto-char (point-max))
814 (skip-chars-backward "\n")
815 (delete-region (point) (point-max))
23f87bed
MB
816 (unless (bobp)
817 (insert "\n\n")))
6748645f 818
eec82323
LMI
819(defun nnfolder-insert-newsgroup-line (group-art)
820 (save-excursion
821 (goto-char (point-min))
16409b0b
GM
822 (unless (search-forward "\n\n" nil t)
823 (goto-char (point-max))
824 (insert "\n"))
825 (forward-char -1)
826 (insert (format (concat nnfolder-article-marker "%d %s\n")
827 (cdr group-art) (current-time-string)))))
eec82323
LMI
828
829(defun nnfolder-active-number (group)
830 ;; Find the next article number in GROUP.
831 (let ((active (cadr (assoc group nnfolder-group-alist))))
832 (if active
833 (setcdr active (1+ (cdr active)))
834 ;; This group is new, so we create a new entry for it.
835 ;; This might be a bit naughty... creating groups on the drop of
836 ;; a hat, but I don't know...
837 (push (list group (setq active (cons 1 1)))
838 nnfolder-group-alist))
839 (cdr active)))
840
841(defun nnfolder-possibly-change-folder (group)
842 (let ((inf (assoc group nnfolder-buffer-alist)))
843 (if (and inf
844 (gnus-buffer-live-p (cadr inf)))
845 (set-buffer (cadr inf))
846 (when inf
847 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
848 (when nnfolder-group-alist
16409b0b 849 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
eec82323
LMI
850 (push (list group (nnfolder-read-folder group))
851 nnfolder-buffer-alist))))
852
23f87bed
MB
853;; This method has a problem if you've accidentally let the active
854;; list get out of sync with the files. This could happen, say, if
855;; you've accidentally gotten new mail with something other than Gnus
856;; (but why would _that_ ever happen? :-). In that case, we will be
857;; in the middle of processing the file, ready to add new X-Gnus
858;; article number markers, and we'll run across a message with no ID
859;; yet - the active list _may_not_ be ready for us yet.
860
861;; To handle this, I'm modifying this routine to maintain the maximum
862;; ID seen so far, and when we hit a message with no ID, we will
863;; _manually_ scan the rest of the message looking for any more,
864;; possibly higher IDs. We'll assume the maximum that we find is the
865;; highest active. Note that this shouldn't cost us much extra time
866;; at all, but will be a lot less vulnerable to glitches between the
867;; mbox and the active file.
eec82323
LMI
868
869(defun nnfolder-read-folder (group)
870 (let* ((file (nnfolder-group-pathname group))
23f87bed 871 (nov (nnfolder-group-nov-pathname group))
16409b0b 872 (buffer (set-buffer
a1506d29 873 (let ((nnheader-file-coding-system
16409b0b
GM
874 nnfolder-file-coding-system))
875 (nnheader-find-file-noselect file)))))
2c902422 876 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
eec82323
LMI
877 (if (equal (cadr (assoc group nnfolder-scantime-alist))
878 (nth 5 (file-attributes file)))
879 ;; This looks up-to-date, so we don't do any scanning.
6748645f
LMI
880 (if (file-exists-p file)
881 buffer
882 (push (list group buffer) nnfolder-buffer-alist)
883 (set-buffer-modified-p t)
16409b0b 884 (nnfolder-save-buffer))
eec82323
LMI
885 ;; Parse the damn thing.
886 (save-excursion
16409b0b
GM
887 (goto-char (point-min))
888 ;; Remove any blank lines at the start.
889 (while (eq (following-char) ?\n)
890 (delete-char 1))
eec82323
LMI
891 (nnmail-activate 'nnfolder)
892 ;; Read in the file.
16409b0b 893 (let ((delim "^From ")
eec82323
LMI
894 (marker (concat "\n" nnfolder-article-marker))
895 (number "[0-9]+")
896 (active (or (cadr (assoc group nnfolder-group-alist))
897 (cons 1 0)))
898 (scantime (assoc group nnfolder-scantime-alist))
899 (minid (lsh -1 -1))
900 maxid start end newscantime
23f87bed 901 novbuf articles newnum
eec82323 902 buffer-read-only)
16409b0b 903 (buffer-disable-undo)
eec82323 904 (setq maxid (cdr active))
23f87bed
MB
905
906 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
907 (and (file-exists-p nov)
908 (file-newer-than-file-p nov file)))
909 (unless (file-exists-p nov)
910 (gnus-make-directory (file-name-directory nov)))
911 (with-current-buffer
912 (setq novbuf (nnfolder-open-nov group))
913 (goto-char (point-min))
914 (while (not (eobp))
915 (push (read novbuf) articles)
916 (forward-line 1))
917 (setq articles (nreverse articles))))
eec82323
LMI
918 (goto-char (point-min))
919
23f87bed
MB
920 ;; Anytime the active number is 1 or 0, it is suspect. In
921 ;; that case, search the file manually to find the active
922 ;; number. Or, of course, if we're being paranoid. (This
923 ;; would also be the place to build other lists from the
924 ;; header markers, such as expunge lists, etc., if we ever
925 ;; desired to abandon the active file entirely for mboxes.)
eec82323 926 (when (or nnfolder-ignore-active-file
23f87bed 927 novbuf
eec82323
LMI
928 (< maxid 2))
929 (while (and (search-forward marker nil t)
23f87bed
MB
930 (looking-at number))
931 (setq newnum (string-to-number (match-string 0)))
932 (when (nnmail-within-headers-p)
933 (setq maxid (max maxid newnum)
934 minid (min minid newnum))
935 (when novbuf
936 (if (memq newnum articles)
937 (setq articles (delq newnum articles))
938 (let ((headers (nnfolder-parse-head newnum)))
939 (with-current-buffer novbuf
940 (nnheader-find-nov-line newnum)
941 (nnheader-insert-nov headers)))))))
942 (when (and novbuf articles)
943 (with-current-buffer novbuf
944 (dolist (article articles)
945 (when (nnheader-find-nov-line article)
946 (delete-region (point)
947 (progn (forward-line 1) (point)))))))
eec82323
LMI
948 (setcar active (max 1 (min minid maxid)))
949 (setcdr active (max maxid (cdr active)))
950 (goto-char (point-min)))
951
23f87bed
MB
952 ;; As long as we trust that the user will only insert
953 ;; unmarked mail at the end, go to the end and search
954 ;; backwards for the last marker. Find the start of that
955 ;; message, and begin to search for unmarked messages from
956 ;; there.
eec82323
LMI
957 (when (not (or nnfolder-distrust-mbox
958 (< maxid 2)))
959 (goto-char (point-max))
960 (unless (re-search-backward marker nil t)
961 (goto-char (point-min)))
23f87bed
MB
962 ;;(when (nnmail-search-unix-mail-delim)
963 ;; (goto-char (point-min)))
964 )
eec82323 965
23f87bed
MB
966 ;; Keep track of the active number on our own, and insert it
967 ;; back into the active list when we're done. Also, prime
968 ;; the pump to cut down on the number of searches we do.
eec82323
LMI
969 (unless (nnmail-search-unix-mail-delim)
970 (goto-char (point-max)))
971 (setq end (point-marker))
972 (while (not (= end (point-max)))
973 (setq start (marker-position end))
974 (goto-char end)
23f87bed 975 ;; There may be more than one "From " line, so we skip past
eec82323
LMI
976 ;; them.
977 (while (looking-at delim)
978 (forward-line 1))
979 (set-marker end (if (nnmail-search-unix-mail-delim)
980 (point)
981 (point-max)))
982 (goto-char start)
983 (when (not (search-forward marker end t))
984 (narrow-to-region start end)
985 (nnmail-insert-lines)
986 (nnfolder-insert-newsgroup-line
23f87bed
MB
987 (cons nil
988 (setq newnum
989 (nnfolder-active-number group))))
990 (when novbuf
991 (let ((headers (nnfolder-parse-head newnum (point-min)
992 (point-max))))
993 (with-current-buffer novbuf
994 (goto-char (point-max))
995 (nnheader-insert-nov headers))))
eec82323
LMI
996 (widen)))
997
998 (set-marker end nil)
23f87bed
MB
999 ;; Make absolutely sure that the active list reflects
1000 ;; reality!
16409b0b 1001 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
23f87bed 1002
eec82323
LMI
1003 ;; Set the scantime for this group.
1004 (setq newscantime (visited-file-modtime))
1005 (if scantime
1006 (setcdr scantime (list newscantime))
23f87bed 1007 (push (list group newscantime)
eec82323 1008 nnfolder-scantime-alist))
23f87bed
MB
1009 ;; Save nov.
1010 (when novbuf
1011 (nnfolder-save-nov))
eec82323
LMI
1012 (current-buffer))))))
1013
1014;;;###autoload
1015(defun nnfolder-generate-active-file ()
16409b0b
GM
1016 "Look for mbox folders in the nnfolder directory and make them into groups.
1017This command does not work if you use short group names."
eec82323
LMI
1018 (interactive)
1019 (nnmail-activate 'nnfolder)
23f87bed
MB
1020 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1021 (dolist (file (directory-files (or nnfolder-nov-directory
1022 nnfolder-directory)
1023 t
1024 (concat
1025 (regexp-quote nnfolder-nov-file-suffix)
1026 "$")))
1027 (when (not (message-mail-file-mbox-p file))
1028 (ignore-errors
1029 (delete-file file)))))
eec82323 1030 (let ((files (directory-files nnfolder-directory))
23f87bed 1031 file)
eec82323
LMI
1032 (while (setq file (pop files))
1033 (when (and (not (backup-file-name-p file))
23f87bed 1034 (message-mail-file-mbox-p
eec82323 1035 (nnheader-concat nnfolder-directory file)))
23f87bed
MB
1036 (let ((oldgroup (assoc file nnfolder-group-alist)))
1037 (if oldgroup
1038 (nnheader-message 5 "Refreshing group %s..." file)
1039 (nnheader-message 5 "Adding group %s..." file))
03378294
RS
1040 (if oldgroup
1041 (setq nnfolder-group-alist
1042 (delq oldgroup (copy-sequence nnfolder-group-alist))))
23f87bed
MB
1043 (push (list file (cons 1 0)) nnfolder-group-alist)
1044 (nnfolder-possibly-change-folder file)
1045 (nnfolder-possibly-change-group file)
1046 (nnfolder-close-group file))))
6748645f 1047 (nnheader-message 5 "")))
eec82323
LMI
1048
1049(defun nnfolder-group-pathname (group)
35ef97a5 1050 "Make file name for GROUP."
16409b0b
GM
1051 (setq group
1052 (mm-encode-coding-string group nnmail-pathname-coding-system))
eec82323
LMI
1053 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
1054 ;; If this file exists, we use it directly.
1055 (if (or nnmail-use-long-file-names
1056 (file-exists-p (concat dir group)))
1057 (concat dir group)
1058 ;; If not, we translate dots into slashes.
1059 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
1060
23f87bed
MB
1061(defun nnfolder-group-nov-pathname (group)
1062 "Make pathname for GROUP NOV."
1063 (let ((nnfolder-directory
1064 (or nnfolder-nov-directory nnfolder-directory)))
1065 (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
1066
eec82323
LMI
1067(defun nnfolder-save-buffer ()
1068 "Save the buffer."
1069 (when (buffer-modified-p)
1070 (run-hooks 'nnfolder-save-buffer-hook)
a8151ef7 1071 (gnus-make-directory (file-name-directory (buffer-file-name)))
a1506d29 1072 (let ((coding-system-for-write
16409b0b
GM
1073 (or nnfolder-file-coding-system-for-write
1074 nnfolder-file-coding-system)))
23f87bed
MB
1075 (save-buffer)))
1076 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1077 (nnfolder-save-nov)))
16409b0b
GM
1078
1079(defun nnfolder-save-active (group-alist active-file)
1080 (let ((nnmail-active-file-coding-system
1081 (or nnfolder-active-file-coding-system-for-write
1082 nnfolder-active-file-coding-system)))
1083 (nnmail-save-active group-alist active-file)))
eec82323 1084
23f87bed
MB
1085(defun nnfolder-open-nov (group)
1086 (or (cdr (assoc group nnfolder-nov-buffer-alist))
1087 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1088 (save-excursion
1089 (set-buffer buffer)
1090 (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1091 (nnfolder-group-nov-pathname group))
1092 (erase-buffer)
1093 (when (file-exists-p nnfolder-nov-buffer-file-name)
1094 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1095 (push (cons group buffer) nnfolder-nov-buffer-alist)
1096 buffer)))
1097
1098(defun nnfolder-save-nov ()
1099 (save-excursion
1100 (while nnfolder-nov-buffer-alist
1101 (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1102 (set-buffer (cdar nnfolder-nov-buffer-alist))
1103 (when (buffer-modified-p)
1104 (gnus-make-directory (file-name-directory
1105 nnfolder-nov-buffer-file-name))
1106 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1107 nil 'nomesg))
1108 (set-buffer-modified-p nil)
1109 (kill-buffer (current-buffer)))
1110 (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1111
1112(defun nnfolder-nov-delete-article (group article)
1113 (save-excursion
1114 (set-buffer (nnfolder-open-nov group))
1115 (when (nnheader-find-nov-line article)
1116 (delete-region (point) (progn (forward-line 1) (point))))
1117 t))
1118
1119(defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1120 (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1121 nil
1122 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1123 (when (file-exists-p nov)
1124 (save-excursion
1125 (set-buffer nntp-server-buffer)
1126 (erase-buffer)
1127 (nnheader-insert-file-contents nov)
1128 (if (and fetch-old
1129 (not (numberp fetch-old)))
1130 t ; Don't remove anything.
1131 (nnheader-nov-delete-outside-range
1132 (if fetch-old (max 1 (- (car articles) fetch-old))
1133 (car articles))
1134 (car (last articles)))
1135 t))))))
1136
1137(defun nnfolder-parse-head (&optional number b e)
1138 "Parse the head of the current buffer."
1139 (let ((buf (current-buffer))
1140 chars)
1141 (save-excursion
1142 (unless b
1143 (setq b (if (nnmail-search-unix-mail-delim-backward)
1144 (point) (point-min)))
1145 (forward-line 1)
1146 (setq e (if (nnmail-search-unix-mail-delim)
1147 (point) (point-max))))
1148 (setq chars (- e b))
1149 (unless (zerop chars)
1150 (goto-char b)
1151 (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1152 (with-temp-buffer
1153 (insert-buffer-substring buf b e)
1154 (let ((headers (nnheader-parse-naked-head)))
1155 (mail-header-set-chars headers chars)
1156 (mail-header-set-number headers number)
1157 headers)))))
1158
1159(defun nnfolder-add-nov (group article headers)
1160 "Add a nov line for the GROUP base."
1161 (save-excursion
1162 (set-buffer (nnfolder-open-nov group))
1163 (goto-char (point-max))
1164 (mail-header-set-number headers article)
1165 (nnheader-insert-nov headers)))
1166
1167(deffoo nnfolder-request-set-mark (group actions &optional server)
1168 (when (and server
1169 (not (nnfolder-server-opened server)))
1170 (nnfolder-open-server server))
1171 (unless nnfolder-marks-is-evil
1172 (nnfolder-open-marks group server)
1173 (dolist (action actions)
1174 (let ((range (nth 0 action))
1175 (what (nth 1 action))
1176 (marks (nth 2 action)))
1177 (assert (or (eq what 'add) (eq what 'del)) t
1178 "Unknown request-set-mark action: %s" what)
1179 (dolist (mark marks)
1180 (setq nnfolder-marks (gnus-update-alist-soft
1181 mark
1182 (funcall (if (eq what 'add) 'gnus-range-add
1183 'gnus-remove-from-range)
1184 (cdr (assoc mark nnfolder-marks)) range)
1185 nnfolder-marks)))))
1186 (nnfolder-save-marks group server))
1187 nil)
1188
1189(deffoo nnfolder-request-update-info (group info &optional server)
1190 ;; Change servers.
1191 (when (and server
1192 (not (nnfolder-server-opened server)))
1193 (nnfolder-open-server server))
1194 (when (and (not nnfolder-marks-is-evil) (nnfolder-marks-changed-p group))
1195 (nnheader-message 8 "Updating marks for %s..." group)
1196 (nnfolder-open-marks group server)
1197 ;; Update info using `nnfolder-marks'.
1198 (mapcar (lambda (pred)
1199 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1200 (gnus-info-set-marks
1201 info
1202 (gnus-update-alist-soft
1203 (cdr pred)
1204 (cdr (assq (cdr pred) nnfolder-marks))
1205 (gnus-info-marks info))
1206 t)))
1207 gnus-article-mark-lists)
1208 (let ((seen (cdr (assq 'read nnfolder-marks))))
1209 (gnus-info-set-read info
1210 (if (and (integerp (car seen))
1211 (null (cdr seen)))
1212 (list (cons (car seen) (car seen)))
1213 seen)))
1214 (nnheader-message 8 "Updating marks for %s...done" group))
1215 info)
1216
1217(defun nnfolder-group-marks-pathname (group)
1218 "Make pathname for GROUP NOV."
1219 (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory)))
1220 (concat (nnfolder-group-pathname group) nnfolder-marks-file-suffix)))
1221
1222(defun nnfolder-marks-changed-p (group)
1223 (let ((file (nnfolder-group-marks-pathname group)))
1224 (if (null (gnus-gethash file nnfolder-marks-modtime))
1225 t ;; never looked at marks file, assume it has changed
1226 (not (equal (gnus-gethash file nnfolder-marks-modtime)
1227 (nth 5 (file-attributes file)))))))
1228
1229(defun nnfolder-save-marks (group server)
1230 (let ((file-name-coding-system nnmail-pathname-coding-system)
1231 (file (nnfolder-group-marks-pathname group)))
1232 (condition-case err
1233 (progn
1234 (with-temp-file file
1235 (erase-buffer)
1236 (gnus-prin1 nnfolder-marks)
1237 (insert "\n"))
1238 (gnus-sethash file
1239 (nth 5 (file-attributes file))
1240 nnfolder-marks-modtime))
1241 (error (or (gnus-yes-or-no-p
1242 (format "Could not write to %s (%s). Continue? " file err))
1243 (error "Cannot write to %s (%s)" err))))))
1244
1245(defun nnfolder-open-marks (group server)
1246 (let ((file (nnfolder-group-marks-pathname group)))
1247 (if (file-exists-p file)
1248 (condition-case err
1249 (with-temp-buffer
1250 (gnus-sethash file (nth 5 (file-attributes file))
1251 nnfolder-marks-modtime)
1252 (nnheader-insert-file-contents file)
1253 (setq nnfolder-marks (read (current-buffer)))
1254 (dolist (el gnus-article-unpropagated-mark-lists)
1255 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks))))
1256 (error (or (gnus-yes-or-no-p
1257 (format "Error reading nnfolder marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1258 (error "Cannot read nnfolder marks file %s (%s)" file err))))
1259 ;; User didn't have a .marks file. Probably first time
1260 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1261 (let ((info (gnus-get-info
1262 (gnus-group-prefixed-name
1263 group
1264 (gnus-server-to-method (format "nnfolder:%s" server))))))
1265 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1266 (setq nnfolder-marks (gnus-info-marks info))
1267 (push (cons 'read (gnus-info-read info)) nnfolder-marks)
1268 (dolist (el gnus-article-unpropagated-mark-lists)
1269 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks)))
1270 (nnfolder-save-marks group server)
1271 (nnheader-message 7 "Bootstrapping marks for %s...done" group)))))
1272
eec82323
LMI
1273(provide 'nnfolder)
1274
ab5796a9 1275;;; arch-tag: a040d0f4-4f4e-445f-8972-839575c5f7e6
eec82323 1276;;; nnfolder.el ends here