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