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