(syms_of_eval): Initialize debug_may_continue.
[bpt/emacs.git] / lisp / gnus / nnml.el
1 ;;; nnml.el --- mail spool access for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
28 ;; For an overview of what the interface functions do, please see the
29 ;; Gnus sources.
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (require 'nnmail)
35 (require 'nnoo)
36 (eval-when-compile (require 'cl))
37
38 (nnoo-declare nnml)
39
40 (defvoo nnml-directory message-directory
41 "Spool directory for the nnml mail backend.")
42
43 (defvoo nnml-active-file
44 (concat (file-name-as-directory nnml-directory) "active")
45 "Mail active file.")
46
47 (defvoo nnml-newsgroups-file
48 (concat (file-name-as-directory nnml-directory) "newsgroups")
49 "Mail newsgroups description file.")
50
51 (defvoo nnml-get-new-mail t
52 "If non-nil, nnml will check the incoming mail file and split the mail.")
53
54 (defvoo nnml-nov-is-evil nil
55 "If non-nil, Gnus will never generate and use nov databases for mail groups.
56 Using nov databases will speed up header fetching considerably.
57 This variable shouldn't be flipped much. If you have, for some reason,
58 set this to t, and want to set it to nil again, you should always run
59 the `nnml-generate-nov-databases' command. The function will go
60 through all nnml directories and generate nov databases for them
61 all. This may very well take some time.")
62
63 (defvoo nnml-prepare-save-mail-hook nil
64 "Hook run narrowed to an article before saving.")
65
66 (defvoo nnml-inhibit-expiry nil
67 "If non-nil, inhibit expiry.")
68
69
70 \f
71
72 (defconst nnml-version "nnml 1.0"
73 "nnml version.")
74
75 (defvoo nnml-nov-file-name ".overview")
76
77 (defvoo nnml-current-directory nil)
78 (defvoo nnml-current-group nil)
79 (defvoo nnml-status-string "")
80 (defvoo nnml-nov-buffer-alist nil)
81 (defvoo nnml-group-alist nil)
82 (defvoo nnml-active-timestamp nil)
83 (defvoo nnml-article-file-alist nil)
84
85 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
86
87 (defvar nnml-nov-buffer-file-name nil)
88
89 \f
90
91 ;;; Interface functions.
92
93 (nnoo-define-basics nnml)
94
95 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
96 (when (nnml-possibly-change-directory group server)
97 (save-excursion
98 (set-buffer nntp-server-buffer)
99 (erase-buffer)
100 (let ((file nil)
101 (number (length sequence))
102 (count 0)
103 (file-name-coding-system 'binary)
104 (pathname-coding-system 'binary)
105 beg article)
106 (if (stringp (car sequence))
107 'headers
108 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
109 'nov
110 (while sequence
111 (setq article (car sequence))
112 (setq file (nnml-article-to-file article))
113 (when (and file
114 (file-exists-p file)
115 (not (file-directory-p file)))
116 (insert (format "221 %d Article retrieved.\n" article))
117 (setq beg (point))
118 (nnheader-insert-head file)
119 (goto-char beg)
120 (if (search-forward "\n\n" nil t)
121 (forward-char -1)
122 (goto-char (point-max))
123 (insert "\n\n"))
124 (insert ".\n")
125 (delete-region (point) (point-max)))
126 (setq sequence (cdr sequence))
127 (setq count (1+ count))
128 (and (numberp nnmail-large-newsgroup)
129 (> number nnmail-large-newsgroup)
130 (zerop (% count 20))
131 (nnheader-message 6 "nnml: Receiving headers... %d%%"
132 (/ (* count 100) number))))
133
134 (and (numberp nnmail-large-newsgroup)
135 (> number nnmail-large-newsgroup)
136 (nnheader-message 6 "nnml: Receiving headers...done"))
137
138 (nnheader-fold-continuation-lines)
139 'headers))))))
140
141 (deffoo nnml-open-server (server &optional defs)
142 (nnoo-change-server 'nnml server defs)
143 (when (not (file-exists-p nnml-directory))
144 (condition-case ()
145 (make-directory nnml-directory t)
146 (error)))
147 (cond
148 ((not (file-exists-p nnml-directory))
149 (nnml-close-server)
150 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
151 ((not (file-directory-p (file-truename nnml-directory)))
152 (nnml-close-server)
153 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
154 (t
155 (nnheader-report 'nnml "Opened server %s using directory %s"
156 server nnml-directory)
157 t)))
158
159 (defun nnml-request-regenerate (server)
160 (nnml-possibly-change-directory nil server)
161 (nnml-generate-nov-databases)
162 t)
163
164 (deffoo nnml-request-article (id &optional group server buffer)
165 (nnml-possibly-change-directory group server)
166 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
167 (file-name-coding-system 'binary)
168 (pathname-coding-system 'binary)
169 path gpath group-num)
170 (if (stringp id)
171 (when (and (setq group-num (nnml-find-group-number id))
172 (cdr
173 (assq (cdr group-num)
174 (nnheader-article-to-file-alist
175 (setq gpath
176 (nnmail-group-pathname
177 (car group-num)
178 nnml-directory))))))
179 (setq path (concat gpath (int-to-string (cdr group-num)))))
180 (setq path (nnml-article-to-file id)))
181 (cond
182 ((not path)
183 (nnheader-report 'nnml "No such article: %s" id))
184 ((not (file-exists-p path))
185 (nnheader-report 'nnml "No such file: %s" path))
186 ((file-directory-p path)
187 (nnheader-report 'nnml "File is a directory: %s" path))
188 ((not (save-excursion (nnmail-find-file path)))
189 (nnheader-report 'nnml "Couldn't read file: %s" path))
190 (t
191 (nnheader-report 'nnml "Article %s retrieved" id)
192 ;; We return the article number.
193 (cons (if group-num (car group-num) group)
194 (string-to-int (file-name-nondirectory path)))))))
195
196 (deffoo nnml-request-group (group &optional server dont-check)
197 (let ((pathname-coding-system 'binary)
198 (file-name-coding-system 'binary))
199 (cond
200 ((not (nnml-possibly-change-directory group server))
201 (nnheader-report 'nnml "Invalid group (no such directory)"))
202 ((not (file-exists-p nnml-current-directory))
203 (nnheader-report 'nnml "Directory %s does not exist"
204 nnml-current-directory))
205 ((not (file-directory-p nnml-current-directory))
206 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
207 (dont-check
208 (nnheader-report 'nnml "Group %s selected" group)
209 t)
210 (t
211 (nnheader-re-read-dir nnml-current-directory)
212 (nnmail-activate 'nnml)
213 (let ((active (nth 1 (assoc group nnml-group-alist))))
214 (if (not active)
215 (nnheader-report 'nnml "No such group: %s" group)
216 (nnheader-report 'nnml "Selected group %s" group)
217 (nnheader-insert "211 %d %d %d %s\n"
218 (max (1+ (- (cdr active) (car active))) 0)
219 (car active) (cdr active) group)))))))
220
221 (deffoo nnml-request-scan (&optional group server)
222 (setq nnml-article-file-alist nil)
223 (nnml-possibly-change-directory group server)
224 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
225
226 (deffoo nnml-close-group (group &optional server)
227 (setq nnml-article-file-alist nil)
228 t)
229
230 (deffoo nnml-request-create-group (group &optional server args)
231 (nnmail-activate 'nnml)
232 (cond
233 ((assoc group nnml-group-alist)
234 t)
235 ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
236 (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
237 (nnheader-report 'nnml "%s is a file"
238 (nnmail-group-pathname group nnml-directory)))
239 (t
240 (let (active)
241 (push (list group (setq active (cons 1 0)))
242 nnml-group-alist)
243 (nnml-possibly-create-directory group)
244 (nnml-possibly-change-directory group server)
245 (let ((articles (nnheader-directory-articles nnml-current-directory)))
246 (when articles
247 (setcar active (apply 'min articles))
248 (setcdr active (apply 'max articles))))
249 (nnmail-save-active nnml-group-alist nnml-active-file)
250 t))))
251
252 (deffoo nnml-request-list (&optional server)
253 (save-excursion
254 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
255 (file-name-coding-system 'binary)
256 (pathname-coding-system 'binary))
257 (nnmail-find-file nnml-active-file)
258 )
259 (setq nnml-group-alist (nnmail-get-active))
260 t))
261
262 (deffoo nnml-request-newgroups (date &optional server)
263 (nnml-request-list server))
264
265 (deffoo nnml-request-list-newsgroups (&optional server)
266 (save-excursion
267 (nnmail-find-file nnml-newsgroups-file)))
268
269 (deffoo nnml-request-expire-articles (articles group
270 &optional server force)
271 (nnml-possibly-change-directory group server)
272 (let ((active-articles
273 (nnheader-directory-articles nnml-current-directory))
274 (is-old t)
275 article rest mod-time number)
276 (nnmail-activate 'nnml)
277
278 (setq active-articles (sort active-articles '<))
279 ;; Articles not listed in active-articles are already gone,
280 ;; so don't try to expire them.
281 (setq articles (gnus-sorted-intersection articles active-articles))
282
283 (while (and articles is-old)
284 (when (setq article (nnml-article-to-file (setq number (pop articles))))
285 (when (setq mod-time (nth 5 (file-attributes article)))
286 (if (and (nnml-deletable-article-p group number)
287 (setq is-old
288 (nnmail-expired-article-p group mod-time force
289 nnml-inhibit-expiry)))
290 (progn
291 (nnheader-message 5 "Deleting article %s in %s"
292 article group)
293 (condition-case ()
294 (funcall nnmail-delete-file-function article)
295 (file-error
296 (push number rest)))
297 (setq active-articles (delq number active-articles))
298 (nnml-nov-delete-article group number))
299 (push number rest)))))
300 (let ((active (nth 1 (assoc group nnml-group-alist))))
301 (when active
302 (setcar active (or (and active-articles
303 (apply 'min active-articles))
304 (1+ (cdr active)))))
305 (nnmail-save-active nnml-group-alist nnml-active-file))
306 (nnml-save-nov)
307 (nconc rest articles)))
308
309 (deffoo nnml-request-move-article
310 (article group server accept-form &optional last)
311 (let ((buf (get-buffer-create " *nnml move*"))
312 result)
313 (nnml-possibly-change-directory group server)
314 (nnml-update-file-alist)
315 (and
316 (nnml-deletable-article-p group article)
317 (nnml-request-article article group server)
318 (save-excursion
319 (set-buffer buf)
320 (insert-buffer-substring nntp-server-buffer)
321 (setq result (eval accept-form))
322 (kill-buffer (current-buffer))
323 result)
324 (progn
325 (nnml-possibly-change-directory group server)
326 (condition-case ()
327 (funcall nnmail-delete-file-function
328 (nnml-article-to-file article))
329 (file-error nil))
330 (nnml-nov-delete-article group article)
331 (when last
332 (nnml-save-nov)
333 (nnmail-save-active nnml-group-alist nnml-active-file))))
334 result))
335
336 (deffoo nnml-request-accept-article (group &optional server last)
337 (nnml-possibly-change-directory group server)
338 (nnmail-check-syntax)
339 (let (result)
340 (when nnmail-cache-accepted-message-ids
341 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
342 (if (stringp group)
343 (and
344 (nnmail-activate 'nnml)
345 (setq result (car (nnml-save-mail
346 (list (cons group (nnml-active-number group))))))
347 (progn
348 (nnmail-save-active nnml-group-alist nnml-active-file)
349 (and last (nnml-save-nov))))
350 (and
351 (nnmail-activate 'nnml)
352 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
353 (yes-or-no-p "Moved to `junk' group; delete article? "))
354 (setq result 'junk)
355 (setq result (car (nnml-save-mail result))))
356 (when last
357 (nnmail-save-active nnml-group-alist nnml-active-file)
358 (when nnmail-cache-accepted-message-ids
359 (nnmail-cache-close))
360 (nnml-save-nov))))
361 result))
362
363 (deffoo nnml-request-replace-article (article group buffer)
364 (nnml-possibly-change-directory group)
365 (save-excursion
366 (set-buffer buffer)
367 (nnml-possibly-create-directory group)
368 (let ((chars (nnmail-insert-lines))
369 (art (concat (int-to-string article) "\t"))
370 headers)
371 (when (condition-case ()
372 (progn
373 (nnmail-write-region
374 (point-min) (point-max)
375 (or (nnml-article-to-file article)
376 (concat nnml-current-directory
377 (int-to-string article)))
378 nil (if (nnheader-be-verbose 5) nil 'nomesg))
379 t)
380 (error nil))
381 (setq headers (nnml-parse-head chars article))
382 ;; Replace the NOV line in the NOV file.
383 (save-excursion
384 (set-buffer (nnml-open-nov group))
385 (goto-char (point-min))
386 (if (or (looking-at art)
387 (search-forward (concat "\n" art) nil t))
388 ;; Delete the old NOV line.
389 (delete-region (progn (beginning-of-line) (point))
390 (progn (forward-line 1) (point)))
391 ;; The line isn't here, so we have to find out where
392 ;; we should insert it. (This situation should never
393 ;; occur, but one likes to make sure...)
394 (while (and (looking-at "[0-9]+\t")
395 (< (string-to-int
396 (buffer-substring
397 (match-beginning 0) (match-end 0)))
398 article)
399 (zerop (forward-line 1)))))
400 (beginning-of-line)
401 (nnheader-insert-nov headers)
402 (nnml-save-nov)
403 t)))))
404
405 (deffoo nnml-request-delete-group (group &optional force server)
406 (nnml-possibly-change-directory group server)
407 (when force
408 ;; Delete all articles in GROUP.
409 (let ((articles
410 (directory-files
411 nnml-current-directory t
412 (concat nnheader-numerical-short-files
413 "\\|" (regexp-quote nnml-nov-file-name) "$")))
414 article)
415 (while articles
416 (setq article (pop articles))
417 (when (file-writable-p article)
418 (nnheader-message 5 "Deleting article %s in %s..." article group)
419 (funcall nnmail-delete-file-function article))))
420 ;; Try to delete the directory itself.
421 (condition-case ()
422 (delete-directory nnml-current-directory)
423 (error nil)))
424 ;; Remove the group from all structures.
425 (setq nnml-group-alist
426 (delq (assoc group nnml-group-alist) nnml-group-alist)
427 nnml-current-group nil
428 nnml-current-directory nil)
429 ;; Save the active file.
430 (nnmail-save-active nnml-group-alist nnml-active-file)
431 t)
432
433 (deffoo nnml-request-rename-group (group new-name &optional server)
434 (nnml-possibly-change-directory group server)
435 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
436 (old-dir (nnmail-group-pathname group nnml-directory)))
437 (when (condition-case ()
438 (progn
439 (make-directory new-dir t)
440 t)
441 (error nil))
442 ;; We move the articles file by file instead of renaming
443 ;; the directory -- there may be subgroups in this group.
444 ;; One might be more clever, I guess.
445 (let ((files (nnheader-article-to-file-alist old-dir)))
446 (while files
447 (rename-file
448 (concat old-dir (cdar files))
449 (concat new-dir (cdar files)))
450 (pop files)))
451 ;; Move .overview file.
452 (let ((overview (concat old-dir nnml-nov-file-name)))
453 (when (file-exists-p overview)
454 (rename-file overview (concat new-dir nnml-nov-file-name))))
455 (when (<= (length (directory-files old-dir)) 2)
456 (condition-case ()
457 (delete-directory old-dir)
458 (error nil)))
459 ;; That went ok, so we change the internal structures.
460 (let ((entry (assoc group nnml-group-alist)))
461 (when entry
462 (setcar entry new-name))
463 (setq nnml-current-directory nil
464 nnml-current-group nil)
465 ;; Save the new group alist.
466 (nnmail-save-active nnml-group-alist nnml-active-file)
467 t))))
468
469 (deffoo nnml-set-status (article name value &optional group server)
470 (nnml-possibly-change-directory group server)
471 (let ((file (nnml-article-to-file article)))
472 (cond
473 ((not (file-exists-p file))
474 (nnheader-report 'nnml "File %s does not exist" file))
475 (t
476 (nnheader-temp-write file
477 (nnheader-insert-file-contents file)
478 (nnmail-replace-status name value))
479 t))))
480
481 \f
482 ;;; Internal functions.
483
484 (defun nnml-article-to-file (article)
485 (nnml-update-file-alist)
486 (let (file)
487 (if (setq file (cdr (assq article nnml-article-file-alist)))
488 (concat nnml-current-directory file)
489 ;; Just to make sure nothing went wrong when reading over NFS --
490 ;; check once more.
491 (when (file-exists-p
492 (setq file (expand-file-name (number-to-string article)
493 nnml-current-directory)))
494 (nnml-update-file-alist t)
495 file))))
496
497 (defun nnml-deletable-article-p (group article)
498 "Say whether ARTICLE in GROUP can be deleted."
499 (let (path)
500 (when (setq path (nnml-article-to-file article))
501 (when (file-writable-p path)
502 (or (not nnmail-keep-last-article)
503 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
504 article)))))))
505
506 ;; Find an article number in the current group given the Message-ID.
507 (defun nnml-find-group-number (id)
508 (save-excursion
509 (set-buffer (get-buffer-create " *nnml id*"))
510 (buffer-disable-undo (current-buffer))
511 (let ((alist nnml-group-alist)
512 number)
513 ;; We want to look through all .overview files, but we want to
514 ;; start with the one in the current directory. It seems most
515 ;; likely that the article we are looking for is in that group.
516 (if (setq number (nnml-find-id nnml-current-group id))
517 (cons nnml-current-group number)
518 ;; It wasn't there, so we look through the other groups as well.
519 (while (and (not number)
520 alist)
521 (or (string= (caar alist) nnml-current-group)
522 (setq number (nnml-find-id (caar alist) id)))
523 (or number
524 (setq alist (cdr alist))))
525 (and number
526 (cons (caar alist) number))))))
527
528 (defun nnml-find-id (group id)
529 (erase-buffer)
530 (let ((nov (concat (nnmail-group-pathname group nnml-directory)
531 nnml-nov-file-name))
532 number found)
533 (when (file-exists-p nov)
534 (nnheader-insert-file-contents nov)
535 (while (and (not found)
536 (search-forward id nil t)) ; We find the ID.
537 ;; And the id is in the fourth field.
538 (if (not (and (search-backward "\t" nil t 4)
539 (not (search-backward"\t" (gnus-point-at-bol) t))))
540 (forward-line 1)
541 (beginning-of-line)
542 (setq found t)
543 ;; We return the article number.
544 (setq number
545 (condition-case ()
546 (read (current-buffer))
547 (error nil)))))
548 number)))
549
550 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
551 (if (or gnus-nov-is-evil nnml-nov-is-evil)
552 nil
553 (let ((nov (concat nnml-current-directory nnml-nov-file-name)))
554 (when (file-exists-p nov)
555 (save-excursion
556 (set-buffer nntp-server-buffer)
557 (erase-buffer)
558 (nnheader-insert-file-contents nov)
559 (if (and fetch-old
560 (not (numberp fetch-old)))
561 t ; Don't remove anything.
562 (nnheader-nov-delete-outside-range
563 (if fetch-old (max 1 (- (car articles) fetch-old))
564 (car articles))
565 (car (last articles)))
566 t))))))
567
568 (defun nnml-possibly-change-directory (group &optional server)
569 (when (and server
570 (not (nnml-server-opened server)))
571 (nnml-open-server server))
572 (if (not group)
573 t
574 (let ((pathname (nnmail-group-pathname group nnml-directory))
575 (file-name-coding-system 'binary)
576 (pathname-coding-system 'binary))
577 (when (not (equal pathname nnml-current-directory))
578 (setq nnml-current-directory pathname
579 nnml-current-group group
580 nnml-article-file-alist nil))
581 (file-exists-p nnml-current-directory))))
582
583 (defun nnml-possibly-create-directory (group)
584 (let (dir dirs)
585 (setq dir (nnmail-group-pathname group nnml-directory))
586 (while (not (file-directory-p dir))
587 (push dir dirs)
588 (setq dir (file-name-directory (directory-file-name dir))))
589 (while dirs
590 (make-directory (directory-file-name (car dirs)))
591 (nnheader-message 5 "Creating mail directory %s" (car dirs))
592 (setq dirs (cdr dirs)))))
593
594 (defun nnml-save-mail (group-art)
595 "Called narrowed to an article."
596 (let (chars headers)
597 (setq chars (nnmail-insert-lines))
598 (nnmail-insert-xref group-art)
599 (run-hooks 'nnmail-prepare-save-mail-hook)
600 (run-hooks 'nnml-prepare-save-mail-hook)
601 (goto-char (point-min))
602 (while (looking-at "From ")
603 (replace-match "X-From-Line: ")
604 (forward-line 1))
605 ;; We save the article in all the groups it belongs in.
606 (let ((ga group-art)
607 first)
608 (while ga
609 (nnml-possibly-create-directory (caar ga))
610 (let ((file (concat (nnmail-group-pathname
611 (caar ga) nnml-directory)
612 (int-to-string (cdar ga)))))
613 (if first
614 ;; It was already saved, so we just make a hard link.
615 (funcall nnmail-crosspost-link-function first file t)
616 ;; Save the article.
617 (nnmail-write-region (point-min) (point-max) file nil
618 (if (nnheader-be-verbose 5) nil 'nomesg))
619 (setq first file)))
620 (setq ga (cdr ga))))
621 ;; Generate a nov line for this article. We generate the nov
622 ;; line after saving, because nov generation destroys the
623 ;; header.
624 (setq headers (nnml-parse-head chars))
625 ;; Output the nov line to all nov databases that should have it.
626 (let ((ga group-art))
627 (while ga
628 (nnml-add-nov (caar ga) (cdar ga) headers)
629 (setq ga (cdr ga))))
630 group-art))
631
632 (defun nnml-active-number (group)
633 "Compute the next article number in GROUP."
634 (let ((active (cadr (assoc group nnml-group-alist))))
635 ;; The group wasn't known to nnml, so we just create an active
636 ;; entry for it.
637 (unless active
638 ;; Perhaps the active file was corrupt? See whether
639 ;; there are any articles in this group.
640 (nnml-possibly-create-directory group)
641 (nnml-possibly-change-directory group)
642 (unless nnml-article-file-alist
643 (setq nnml-article-file-alist
644 (sort
645 (nnheader-article-to-file-alist nnml-current-directory)
646 'car-less-than-car)))
647 (setq active
648 (if nnml-article-file-alist
649 (cons (caar nnml-article-file-alist)
650 (caar (last nnml-article-file-alist)))
651 (cons 1 0)))
652 (push (list group active) nnml-group-alist))
653 (setcdr active (1+ (cdr active)))
654 (while (file-exists-p
655 (concat (nnmail-group-pathname group nnml-directory)
656 (int-to-string (cdr active))))
657 (setcdr active (1+ (cdr active))))
658 (cdr active)))
659
660 (defun nnml-add-nov (group article headers)
661 "Add a nov line for the GROUP base."
662 (save-excursion
663 (set-buffer (nnml-open-nov group))
664 (goto-char (point-max))
665 (mail-header-set-number headers article)
666 (nnheader-insert-nov headers)))
667
668 (defsubst nnml-header-value ()
669 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
670
671 (defun nnml-parse-head (chars &optional number)
672 "Parse the head of the current buffer."
673 (save-excursion
674 (save-restriction
675 (unless (zerop (buffer-size))
676 (narrow-to-region
677 (goto-char (point-min))
678 (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
679 ;; Fold continuation lines.
680 (goto-char (point-min))
681 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
682 (replace-match " " t t))
683 ;; Remove any tabs; they are too confusing.
684 (subst-char-in-region (point-min) (point-max) ?\t ? )
685 (let ((headers (nnheader-parse-head t)))
686 (mail-header-set-chars headers chars)
687 (mail-header-set-number headers number)
688 headers))))
689
690 (defun nnml-open-nov (group)
691 (or (cdr (assoc group nnml-nov-buffer-alist))
692 (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
693 (save-excursion
694 (set-buffer buffer)
695 (set (make-local-variable 'nnml-nov-buffer-file-name)
696 (concat (nnmail-group-pathname group nnml-directory)
697 nnml-nov-file-name))
698 (erase-buffer)
699 (when (file-exists-p nnml-nov-buffer-file-name)
700 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
701 (push (cons group buffer) nnml-nov-buffer-alist)
702 buffer)))
703
704 (defun nnml-save-nov ()
705 (save-excursion
706 (while nnml-nov-buffer-alist
707 (when (buffer-name (cdar nnml-nov-buffer-alist))
708 (set-buffer (cdar nnml-nov-buffer-alist))
709 (when (buffer-modified-p)
710 (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
711 nil 'nomesg))
712 (set-buffer-modified-p nil)
713 (kill-buffer (current-buffer)))
714 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
715
716 ;;;###autoload
717 (defun nnml-generate-nov-databases ()
718 "Generate NOV databases in all nnml directories."
719 (interactive)
720 ;; Read the active file to make sure we don't re-use articles
721 ;; numbers in empty groups.
722 (nnmail-activate 'nnml)
723 (nnml-open-server (or (nnoo-current-server 'nnml) ""))
724 (setq nnml-directory (expand-file-name nnml-directory))
725 ;; Recurse down the directories.
726 (nnml-generate-nov-databases-1 nnml-directory nil t)
727 ;; Save the active file.
728 (nnmail-save-active nnml-group-alist nnml-active-file))
729
730 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
731 "Regenerate the NOV database in DIR."
732 (interactive "DRegenerate NOV in: ")
733 (setq dir (file-name-as-directory dir))
734 ;; Only scan this sub-tree if we haven't been here yet.
735 (unless (member (file-truename dir) seen)
736 (push (file-truename dir) seen)
737 ;; We descend recursively
738 (let ((dirs (directory-files dir t nil t))
739 dir)
740 (while (setq dir (pop dirs))
741 (when (and (not (member (file-name-nondirectory dir) '("." "..")))
742 (file-directory-p dir))
743 (nnml-generate-nov-databases-1 dir seen))))
744 ;; Do this directory.
745 (let ((files (sort (nnheader-article-to-file-alist dir)
746 'car-less-than-car)))
747 (if (not files)
748 (let* ((group (nnheader-file-to-group
749 (directory-file-name dir) nnml-directory))
750 (info (cadr (assoc group nnml-group-alist))))
751 (when info
752 (setcar info (1+ (cdr info)))))
753 (funcall nnml-generate-active-function dir)
754 ;; Generate the nov file.
755 (nnml-generate-nov-file dir files)
756 (unless no-active
757 (nnmail-save-active nnml-group-alist nnml-active-file))))))
758
759 (defvar files)
760 (defun nnml-generate-active-info (dir)
761 ;; Update the active info for this group.
762 (let ((group (nnheader-file-to-group
763 (directory-file-name dir) nnml-directory)))
764 (setq nnml-group-alist
765 (delq (assoc group nnml-group-alist) nnml-group-alist))
766 (push (list group
767 (cons (caar files)
768 (let ((f files))
769 (while (cdr f) (setq f (cdr f)))
770 (caar f))))
771 nnml-group-alist)))
772
773 (defun nnml-generate-nov-file (dir files)
774 (let* ((dir (file-name-as-directory dir))
775 (nov (concat dir nnml-nov-file-name))
776 (nov-buffer (get-buffer-create " *nov*"))
777 chars file headers)
778 (save-excursion
779 ;; Init the nov buffer.
780 (set-buffer nov-buffer)
781 (buffer-disable-undo (current-buffer))
782 (erase-buffer)
783 (set-buffer nntp-server-buffer)
784 ;; Delete the old NOV file.
785 (when (file-exists-p nov)
786 (funcall nnmail-delete-file-function nov))
787 (while files
788 (unless (file-directory-p (setq file (concat dir (cdar files))))
789 (erase-buffer)
790 (nnheader-insert-file-contents file)
791 (narrow-to-region
792 (goto-char (point-min))
793 (progn
794 (search-forward "\n\n" nil t)
795 (setq chars (- (point-max) (point)))
796 (max 1 (1- (point)))))
797 (unless (zerop (buffer-size))
798 (goto-char (point-min))
799 (setq headers (nnml-parse-head chars (caar files)))
800 (save-excursion
801 (set-buffer nov-buffer)
802 (goto-char (point-max))
803 (nnheader-insert-nov headers)))
804 (widen))
805 (setq files (cdr files)))
806 (save-excursion
807 (set-buffer nov-buffer)
808 (nnmail-write-region 1 (point-max) nov nil 'nomesg)
809 (kill-buffer (current-buffer))))))
810
811 (defun nnml-nov-delete-article (group article)
812 (save-excursion
813 (set-buffer (nnml-open-nov group))
814 (when (nnheader-find-nov-line article)
815 (delete-region (point) (progn (forward-line 1) (point)))
816 (when (bobp)
817 (let ((active (cadr (assoc group nnml-group-alist)))
818 num)
819 (when active
820 (if (eobp)
821 (setf (car active) (1+ (cdr active)))
822 (when (and (setq num (ignore-errors (read (current-buffer))))
823 (numberp num))
824 (setf (car active) num)))))))
825 t))
826
827 (defun nnml-update-file-alist (&optional force)
828 (when (or (not nnml-article-file-alist)
829 force)
830 (setq nnml-article-file-alist
831 (nnheader-article-to-file-alist nnml-current-directory))))
832
833 (provide 'nnml)
834
835 ;;; nnml.el ends here