gnus-group-insert-group-line: Compute icon to return
[gnus] / lisp / nnml.el
1 ;;; nnml.el --- mail spool access for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Authors: Didier Verna <didier@xemacs.org> (adding compaction)
7 ;;      Simon Josefsson <simon@josefsson.org> (adding MARKS)
8 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; Keywords: news, mail
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
30 ;; For an overview of what the interface functions do, please see the
31 ;; Gnus sources.
32
33 ;;; Code:
34
35 (require 'gnus)
36 (require 'nnheader)
37 (require 'nnmail)
38 (require 'nnoo)
39 (eval-when-compile (require 'cl))
40
41 ;; FIXME first is unused in this file.
42 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
43 (autoload 'gnus-backlog-remove-article "gnus-bcklg")
44
45 (nnoo-declare nnml)
46
47 (defvoo nnml-directory message-directory
48   "Spool directory for the nnml mail backend.")
49
50 (defvoo nnml-active-file
51     (expand-file-name "active" nnml-directory)
52   "Mail active file.")
53
54 (defvoo nnml-newsgroups-file
55     (expand-file-name "newsgroups" nnml-directory)
56   "Mail newsgroups description file.")
57
58 (defvoo nnml-get-new-mail t
59   "If non-nil, nnml will check the incoming mail file and split the mail.")
60
61 (defvoo nnml-nov-is-evil nil
62   "If non-nil, Gnus will never generate and use nov databases for mail spools.
63 Using nov databases will speed up header fetching considerably.
64 This variable shouldn't be flipped much.  If you have, for some reason,
65 set this to t, and want to set it to nil again, you should always run
66 the `nnml-generate-nov-databases' command.  The function will go
67 through all nnml directories and generate nov databases for them
68 all.  This may very well take some time.")
69
70 (defvoo nnml-marks-is-evil nil
71   "If non-nil, Gnus will never generate and use marks file for mail spools.
72 Using marks files makes it possible to backup and restore mail groups
73 separately from `.newsrc.eld'.  If you have, for some reason, set this
74 to t, and want to set it to nil again, you should always remove the
75 corresponding marks file (usually named `.marks' in the nnml group
76 directory, but see `nnml-marks-file-name') for the group.  Then the
77 marks file will be regenerated properly by Gnus.")
78
79 (defvoo nnml-prepare-save-mail-hook nil
80   "Hook run narrowed to an article before saving.")
81
82 (defvoo nnml-inhibit-expiry nil
83   "If non-nil, inhibit expiry.")
84
85 (defvoo nnml-use-compressed-files nil
86   "If non-nil, allow using compressed message files.
87
88 If it is a string, use it as the file extension which specifies
89 the compression program.  You can set it to \".bz2\" if your Emacs
90 supports auto-compression using the bzip2 program.  A value of t
91 is equivalent to \".gz\".")
92
93 (defvoo nnml-compressed-files-size-threshold 1000
94   "Default size threshold for compressed message files.
95 Message files with bodies larger than that many characters will
96 be automatically compressed if `nnml-use-compressed-files' is
97 non-nil.")
98
99 \f
100
101 (defconst nnml-version "nnml 1.0"
102   "nnml version.")
103
104 (defvoo nnml-nov-file-name ".overview")
105 (defvoo nnml-marks-file-name ".marks")
106
107 (defvoo nnml-current-directory nil)
108 (defvoo nnml-current-group nil)
109 (defvoo nnml-status-string "")
110 (defvoo nnml-nov-buffer-alist nil)
111 (defvoo nnml-group-alist nil)
112 (defvoo nnml-active-timestamp nil)
113 (defvoo nnml-article-file-alist nil)
114
115 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
116
117 (defvar nnml-nov-buffer-file-name nil)
118
119 (defvoo nnml-file-coding-system nnmail-file-coding-system)
120
121 (defvoo nnml-marks nil)
122
123 (defvar nnml-marks-modtime (gnus-make-hashtable))
124
125 \f
126 ;;; Interface functions.
127
128 (nnoo-define-basics nnml)
129
130 (eval-when-compile
131   (defsubst nnml-group-name-charset (group server-or-method)
132     (gnus-group-name-charset
133      (if (stringp server-or-method)
134          (gnus-server-to-method
135           (if (string-match "\\+" server-or-method)
136               (concat (substring server-or-method 0 (match-beginning 0))
137                       ":" (substring server-or-method (match-end 0)))
138             (concat "nnml:" server-or-method)))
139        (or server-or-method gnus-command-method '(nnml "")))
140      group)))
141
142 (defun nnml-decoded-group-name (group &optional server-or-method)
143   "Return a decoded group name of GROUP on SERVER-OR-METHOD."
144   (if nnmail-group-names-not-encoded-p
145       group
146     (mm-decode-coding-string
147      group
148      (nnml-group-name-charset group server-or-method))))
149
150 (defun nnml-encoded-group-name (group &optional server-or-method)
151   "Return an encoded group name of GROUP on SERVER-OR-METHOD."
152   (mm-encode-coding-string
153    group
154    (nnml-group-name-charset group server-or-method)))
155
156 (defun nnml-group-pathname (group &optional file server)
157   "Return an absolute file name of FILE for GROUP on SERVER."
158   (nnmail-group-pathname (inline (nnml-decoded-group-name group server))
159                          nnml-directory file))
160
161 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
162   (when (nnml-possibly-change-directory group server)
163     (with-current-buffer nntp-server-buffer
164       (erase-buffer)
165       (let* ((file nil)
166              (number (length sequence))
167              (count 0)
168              (file-name-coding-system nnmail-pathname-coding-system)
169              beg article)
170         (if (stringp (car sequence))
171             'headers
172           (if (nnml-retrieve-headers-with-nov sequence fetch-old)
173               'nov
174             (while sequence
175               (setq article (car sequence))
176               (setq file (nnml-article-to-file article))
177               (when (and file
178                          (file-exists-p file)
179                          (not (file-directory-p file)))
180                 (insert (format "221 %d Article retrieved.\n" article))
181                 (setq beg (point))
182                 (nnheader-insert-head file)
183                 (goto-char beg)
184                 (if (re-search-forward "\n\r?\n" nil t)
185                     (forward-char -1)
186                   (goto-char (point-max))
187                   (insert "\n\n"))
188                 (insert ".\n")
189                 (delete-region (point) (point-max)))
190               (setq sequence (cdr sequence))
191               (setq count (1+ count))
192               (and (numberp nnmail-large-newsgroup)
193                    (> number nnmail-large-newsgroup)
194                    (zerop (% count 20))
195                    (nnheader-message 6 "nnml: Receiving headers... %d%%"
196                                      (/ (* count 100) number))))
197
198             (and (numberp nnmail-large-newsgroup)
199                  (> number nnmail-large-newsgroup)
200                  (nnheader-message 6 "nnml: Receiving headers...done"))
201
202             (nnheader-fold-continuation-lines)
203             'headers))))))
204
205 (deffoo nnml-open-server (server &optional defs)
206   (nnoo-change-server 'nnml server defs)
207   (when (not (file-exists-p nnml-directory))
208     (ignore-errors (make-directory nnml-directory t)))
209   (cond
210    ((not (file-exists-p nnml-directory))
211     (nnml-close-server)
212     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
213    ((not (file-directory-p (file-truename nnml-directory)))
214     (nnml-close-server)
215     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
216    (t
217     (nnheader-report 'nnml "Opened server %s using directory %s"
218                      server nnml-directory)
219     t)))
220
221 (deffoo nnml-request-regenerate (server)
222   (nnml-possibly-change-directory nil server)
223   (nnml-generate-nov-databases server)
224   t)
225
226 (deffoo nnml-request-article (id &optional group server buffer)
227   (nnml-possibly-change-directory group server)
228   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
229          (file-name-coding-system nnmail-pathname-coding-system)
230          path gpath group-num)
231     (if (stringp id)
232         (when (and (setq group-num (nnml-find-group-number id server))
233                    (cdr
234                     (assq (cdr group-num)
235                           (nnheader-article-to-file-alist
236                            (setq gpath (nnml-group-pathname (car group-num)
237                                                             nil server))))))
238           (setq path (concat gpath (int-to-string (cdr group-num)))))
239       (setq path (nnml-article-to-file id)))
240     (cond
241      ((not path)
242       (nnheader-report 'nnml "No such article: %s" id))
243      ((not (file-exists-p path))
244       (nnheader-report 'nnml "No such file: %s" path))
245      ((file-directory-p path)
246       (nnheader-report 'nnml "File is a directory: %s" path))
247      ((not (save-excursion (let ((nnmail-file-coding-system
248                                   nnml-file-coding-system))
249                              (nnmail-find-file path))))
250       (nnheader-report 'nnml "Couldn't read file: %s" path))
251      (t
252       (nnheader-report 'nnml "Article %s retrieved" id)
253       ;; We return the article number.
254       (cons (if group-num (car group-num) group)
255             (string-to-number (file-name-nondirectory path)))))))
256
257 (deffoo nnml-request-group (group &optional server dont-check info)
258   (let ((file-name-coding-system nnmail-pathname-coding-system)
259         (decoded (nnml-decoded-group-name group server)))
260     (cond
261      ((not (nnml-possibly-change-directory group server))
262       (nnheader-report 'nnml "Invalid group (no such directory)"))
263      ((not (file-exists-p nnml-current-directory))
264       (nnheader-report 'nnml "Directory %s does not exist"
265                        nnml-current-directory))
266      ((not (file-directory-p nnml-current-directory))
267       (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
268      (dont-check
269       (nnheader-report 'nnml "Group %s selected" decoded)
270       t)
271      (t
272       (nnheader-re-read-dir nnml-current-directory)
273       (nnmail-activate 'nnml)
274       (let ((active (nth 1 (assoc group nnml-group-alist))))
275         (if (not active)
276             (nnheader-report 'nnml "No such group: %s" decoded)
277           (nnheader-report 'nnml "Selected group %s" decoded)
278           (nnheader-insert "211 %d %d %d %s\n"
279                            (max (1+ (- (cdr active) (car active))) 0)
280                            (car active) (cdr active) group)))))))
281
282 (deffoo nnml-request-scan (&optional group server)
283   (setq nnml-article-file-alist nil)
284   (nnml-possibly-change-directory group server)
285   (nnmail-get-new-mail 'nnml 'nnml-save-incremental-nov nnml-directory group))
286
287 (deffoo nnml-close-group (group &optional server)
288   (setq nnml-article-file-alist nil)
289   t)
290
291 (deffoo nnml-request-create-group (group &optional server args)
292   (nnml-possibly-change-directory nil server)
293   (nnmail-activate 'nnml)
294   (cond
295    ((let ((file (directory-file-name (nnml-group-pathname group nil server)))
296           (file-name-coding-system nnmail-pathname-coding-system))
297       (and (file-exists-p file)
298            (not (file-directory-p file))))
299     (nnheader-report 'nnml "%s is a file"
300                      (directory-file-name (nnml-group-pathname group
301                                                                nil server))))
302    ((assoc group nnml-group-alist)
303     t)
304    (t
305     (let (active)
306       (push (list group (setq active (cons 1 0)))
307             nnml-group-alist)
308       (nnml-possibly-create-directory group server)
309       (nnml-possibly-change-directory group server)
310       (let* ((file-name-coding-system nnmail-pathname-coding-system)
311              (articles (nnml-directory-articles nnml-current-directory)))
312         (when articles
313           (setcar active (apply 'min articles))
314           (setcdr active (apply 'max articles))))
315       (nnmail-save-active nnml-group-alist nnml-active-file)
316       t))))
317
318 (deffoo nnml-request-list (&optional server)
319   (save-excursion
320     (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
321           (file-name-coding-system nnmail-pathname-coding-system))
322       (nnmail-find-file nnml-active-file))
323     (setq nnml-group-alist (nnmail-get-active))
324     t))
325
326 (deffoo nnml-request-newgroups (date &optional server)
327   (nnml-request-list server))
328
329 (deffoo nnml-request-list-newsgroups (&optional server)
330   (save-excursion
331     (nnmail-find-file nnml-newsgroups-file)))
332
333 (deffoo nnml-request-expire-articles (articles group &optional server force)
334   (nnml-possibly-change-directory group server)
335   (let* ((file-name-coding-system nnmail-pathname-coding-system)
336          (active-articles
337           (nnml-directory-articles nnml-current-directory))
338          (is-old t)
339          (decoded (nnml-decoded-group-name group server))
340          article rest mod-time number target)
341     (nnmail-activate 'nnml)
342
343     (setq active-articles (sort active-articles '<))
344     ;; Articles not listed in active-articles are already gone,
345     ;; so don't try to expire them.
346     (setq articles (gnus-sorted-intersection articles active-articles))
347
348     (while (and articles is-old)
349       (if (and (setq article (nnml-article-to-file
350                               (setq number (pop articles))))
351                (setq mod-time (nth 5 (file-attributes article)))
352                (nnml-deletable-article-p group number)
353                (setq is-old (nnmail-expired-article-p group mod-time force
354                                                       nnml-inhibit-expiry)))
355           (progn
356             ;; Allow a special target group.
357             (setq target nnmail-expiry-target)
358             (unless (eq target 'delete)
359               (with-temp-buffer
360                 (nnml-request-article number group server (current-buffer))
361                 (let (nnml-current-directory
362                       nnml-current-group
363                       nnml-article-file-alist)
364                   (when (functionp target)
365                     (setq target (funcall target group)))
366                   (when (and target (not (eq target 'delete)))
367                     (if (or (gnus-request-group target)
368                             (gnus-request-create-group target))
369                         (nnmail-expiry-target-group target group)
370                       (setq target nil)))))
371               ;; Maybe directory is changed during nnmail-expiry-target-group.
372               (nnml-possibly-change-directory group server))
373             (if target
374                 (progn
375                   (nnheader-message 5 "Deleting article %s in %s"
376                                     number decoded)
377                   (condition-case ()
378                       (funcall nnmail-delete-file-function article)
379                     (file-error
380                      (push number rest)))
381                   (setq active-articles (delq number active-articles))
382                   (nnml-nov-delete-article group number))
383               (push number rest)))
384         (push number rest)))
385     (let ((active (nth 1 (assoc group nnml-group-alist))))
386       (when active
387         (setcar active (or (and active-articles
388                                 (apply 'min active-articles))
389                            (1+ (cdr active)))))
390       (nnmail-save-active nnml-group-alist nnml-active-file))
391     (nnml-save-nov)
392     (nconc rest articles)))
393
394 (deffoo nnml-request-move-article
395     (article group server accept-form &optional last move-is-internal)
396   (let ((buf (get-buffer-create " *nnml move*"))
397         (file-name-coding-system nnmail-pathname-coding-system)
398         result)
399     (nnml-possibly-change-directory group server)
400     (nnml-update-file-alist)
401     (and
402      (nnml-deletable-article-p group article)
403      (nnml-request-article article group server)
404      (let (nnml-current-directory
405            nnml-current-group
406            nnml-article-file-alist)
407        (with-current-buffer buf
408          (insert-buffer-substring nntp-server-buffer)
409          (setq result (eval accept-form))
410          (kill-buffer (current-buffer))
411          result))
412      (progn
413        (nnml-possibly-change-directory group server)
414        (condition-case ()
415            (funcall nnmail-delete-file-function
416                     (nnml-article-to-file  article))
417          (file-error nil))
418        (nnml-nov-delete-article group article)
419        (when last
420          (nnml-save-nov)
421          (nnmail-save-active nnml-group-alist nnml-active-file))))
422     result))
423
424 (deffoo nnml-request-accept-article (group &optional server last)
425   (nnml-possibly-change-directory group server)
426   (nnmail-check-syntax)
427   (let (result)
428     (when nnmail-cache-accepted-message-ids
429       (nnmail-cache-insert (nnmail-fetch-field "message-id")
430                            group
431                            (nnmail-fetch-field "subject")
432                            (nnmail-fetch-field "from")))
433     (if (stringp group)
434         (and
435          (nnmail-activate 'nnml)
436          (setq result (car (nnml-save-mail
437                             (list (cons group (nnml-active-number group
438                                                                   server)))
439                             server t)))
440          (progn
441            (nnmail-save-active nnml-group-alist nnml-active-file)
442            (and last (nnml-save-nov))))
443       (and
444        (nnmail-activate 'nnml)
445        (if (and (not (setq result (nnmail-article-group
446                                    `(lambda (group)
447                                       (nnml-active-number group ,server)))))
448                 (yes-or-no-p "Moved to `junk' group; delete article? "))
449            (setq result 'junk)
450          (setq result (car (nnml-save-mail result server t))))
451        (when last
452          (nnmail-save-active nnml-group-alist nnml-active-file)
453          (when nnmail-cache-accepted-message-ids
454            (nnmail-cache-close))
455          (nnml-save-nov))))
456     result))
457
458 (deffoo nnml-request-post (&optional server)
459   (nnmail-do-request-post 'nnml-request-accept-article server))
460
461 (deffoo nnml-request-replace-article (article group buffer)
462   (nnml-possibly-change-directory group)
463   (with-current-buffer buffer
464     (nnml-possibly-create-directory group)
465     (let ((chars (nnmail-insert-lines))
466           (art (concat (int-to-string article) "\t"))
467           headers)
468       (when (ignore-errors
469               (nnmail-write-region
470                (point-min) (point-max)
471                (or (nnml-article-to-file article)
472                    (expand-file-name (int-to-string article)
473                                      nnml-current-directory))
474                nil (if (nnheader-be-verbose 5) nil 'nomesg))
475               t)
476         (setq headers (nnml-parse-head chars article))
477         ;; Replace the NOV line in the NOV file.
478         (with-current-buffer (nnml-open-nov group)
479           (goto-char (point-min))
480           (if (or (looking-at art)
481                   (search-forward (concat "\n" art) nil t))
482               ;; Delete the old NOV line.
483               (gnus-delete-line)
484             ;; The line isn't here, so we have to find out where
485             ;; we should insert it.  (This situation should never
486             ;; occur, but one likes to make sure...)
487             (while (and (looking-at "[0-9]+\t")
488                         (< (string-to-number
489                             (buffer-substring
490                              (match-beginning 0) (match-end 0)))
491                            article)
492                         (zerop (forward-line 1)))))
493           (beginning-of-line)
494           (nnheader-insert-nov headers)
495           (nnml-save-nov)
496           t)))))
497
498 (deffoo nnml-request-delete-group (group &optional force server)
499   (nnml-possibly-change-directory group server)
500   (let ((file (directory-file-name nnml-current-directory))
501         (file-name-coding-system nnmail-pathname-coding-system))
502     (if (file-exists-p file)
503         (if (file-directory-p file)
504             (progn
505               (when force
506                 ;; Delete all articles in GROUP.
507                 (let ((articles
508                        (directory-files
509                         nnml-current-directory t
510                         (concat
511                          nnheader-numerical-short-files
512                          "\\|" (regexp-quote nnml-nov-file-name) "$"
513                          "\\|" (regexp-quote nnml-marks-file-name) "$")))
514                       (decoded (nnml-decoded-group-name group server)))
515                   (dolist (article articles)
516                     (when (file-writable-p article)
517                       (nnheader-message 5 "Deleting article %s in %s..."
518                                         (file-name-nondirectory article)
519                                         decoded)
520                       (funcall nnmail-delete-file-function article))))
521                 ;; Try to delete the directory itself.
522                 (ignore-errors (delete-directory nnml-current-directory))))
523           (nnheader-report 'nnml "%s is not a directory" file))
524       (nnheader-report 'nnml "No such directory: %s/" file))
525     ;; Remove the group from all structures.
526     (setq nnml-group-alist
527           (delq (assoc group nnml-group-alist) nnml-group-alist)
528           nnml-current-group nil
529           nnml-current-directory nil)
530     ;; Save the active file.
531     (nnmail-save-active nnml-group-alist nnml-active-file))
532   t)
533
534 (deffoo nnml-request-rename-group (group new-name &optional server)
535   (nnml-possibly-change-directory group server)
536   (let ((new-dir (nnml-group-pathname new-name nil server))
537         (old-dir (nnml-group-pathname group nil server))
538         (file-name-coding-system nnmail-pathname-coding-system))
539     (when (ignore-errors
540             (make-directory new-dir t)
541             t)
542       ;; We move the articles file by file instead of renaming
543       ;; the directory -- there may be subgroups in this group.
544       ;; One might be more clever, I guess.
545       (dolist (file (nnheader-article-to-file-alist old-dir))
546         (rename-file
547          (concat old-dir (cdr file))
548          (concat new-dir (cdr file))))
549       ;; Move .overview file.
550       (let ((overview (concat old-dir nnml-nov-file-name)))
551         (when (file-exists-p overview)
552           (rename-file overview (concat new-dir nnml-nov-file-name))))
553       ;; Move .marks file.
554       (let ((marks (concat old-dir nnml-marks-file-name)))
555         (when (file-exists-p marks)
556           (rename-file marks (concat new-dir nnml-marks-file-name))))
557       (when (<= (length (directory-files old-dir)) 2)
558         (ignore-errors (delete-directory old-dir)))
559       ;; That went ok, so we change the internal structures.
560       (let ((entry (assoc group nnml-group-alist)))
561         (when entry
562           (setcar entry new-name))
563         (setq nnml-current-directory nil
564               nnml-current-group nil)
565         ;; Save the new group alist.
566         (nnmail-save-active nnml-group-alist nnml-active-file)
567         t))))
568
569 (deffoo nnml-set-status (article name value &optional group server)
570   (nnml-possibly-change-directory group server)
571   (let ((file (nnml-article-to-file article)))
572     (cond
573      ((not (file-exists-p file))
574       (nnheader-report 'nnml "File %s does not exist" file))
575      (t
576       (with-temp-file file
577         (nnheader-insert-file-contents file)
578         (nnmail-replace-status name value))
579       t))))
580
581 \f
582 ;;; Internal functions.
583
584 (defun nnml-article-to-file (article)
585   (nnml-update-file-alist)
586   (let (file)
587     (if (setq file
588               (if nnml-use-compressed-files
589                   (cdr (assq article nnml-article-file-alist))
590                 (number-to-string article)))
591         (expand-file-name file nnml-current-directory)
592       (when (not nnheader-directory-files-is-safe)
593         ;; Just to make sure nothing went wrong when reading over NFS --
594         ;; check once more.
595         (when (file-exists-p
596                (setq file (expand-file-name (number-to-string article)
597                                             nnml-current-directory)))
598           (nnml-update-file-alist t)
599           file)))))
600
601 (defun nnml-deletable-article-p (group article)
602   "Say whether ARTICLE in GROUP can be deleted."
603   (let ((file-name-coding-system nnmail-pathname-coding-system)
604         path)
605     (when (setq path (nnml-article-to-file article))
606       (when (file-writable-p path)
607         (or (not nnmail-keep-last-article)
608             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
609                      article)))))))
610
611 ;; Find an article number in the current group given the Message-ID.
612 (defun nnml-find-group-number (id server)
613   (with-current-buffer (get-buffer-create " *nnml id*")
614     (let ((alist nnml-group-alist)
615           number)
616       ;; We want to look through all .overview files, but we want to
617       ;; start with the one in the current directory.  It seems most
618       ;; likely that the article we are looking for is in that group.
619       (if (setq number (nnml-find-id nnml-current-group id server))
620           (cons nnml-current-group number)
621       ;; It wasn't there, so we look through the other groups as well.
622         (while (and (not number)
623                     alist)
624           (or (string= (caar alist) nnml-current-group)
625               (setq number (nnml-find-id (caar alist) id server)))
626           (or number
627               (setq alist (cdr alist))))
628         (and number
629              (cons (caar alist) number))))))
630
631 (defun nnml-find-id (group id server)
632   (erase-buffer)
633   (let ((nov (nnml-group-pathname group nnml-nov-file-name server))
634         number found)
635     (when (file-exists-p nov)
636       (nnheader-insert-file-contents nov)
637       (while (and (not found)
638                   (search-forward id nil t)) ; We find the ID.
639         ;; And the id is in the fourth field.
640         (if (not (and (search-backward "\t" nil t 4)
641                       (not (search-backward "\t" (point-at-bol) t))))
642             (forward-line 1)
643           (beginning-of-line)
644           (setq found t)
645           ;; We return the article number.
646           (setq number
647                 (ignore-errors (read (current-buffer))))))
648       number)))
649
650 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
651   (if (or gnus-nov-is-evil nnml-nov-is-evil)
652       nil
653     (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
654       (when (file-exists-p nov)
655         (with-current-buffer nntp-server-buffer
656           (erase-buffer)
657           (nnheader-insert-file-contents nov)
658           (if (and fetch-old
659                    (not (numberp fetch-old)))
660               t                         ; Don't remove anything.
661             (nnheader-nov-delete-outside-range
662              (if fetch-old (max 1 (- (car articles) fetch-old))
663                (car articles))
664              (car (last articles)))
665             t))))))
666
667 (defun nnml-possibly-change-directory (group &optional server)
668   (when (and server
669              (not (nnml-server-opened server)))
670     (nnml-open-server server))
671   (if (not group)
672       t
673     (let ((pathname (nnml-group-pathname group nil server))
674           (file-name-coding-system nnmail-pathname-coding-system))
675       (when (not (equal pathname nnml-current-directory))
676         (setq nnml-current-directory pathname
677               nnml-current-group group
678               nnml-article-file-alist nil))
679       (file-exists-p nnml-current-directory))))
680
681 (defun nnml-possibly-create-directory (group &optional server)
682   (let ((dir (nnml-group-pathname group nil server))
683         (file-name-coding-system nnmail-pathname-coding-system))
684     (unless (file-exists-p dir)
685       (make-directory (directory-file-name dir) t)
686       (nnheader-message 5 "Creating mail directory %s" dir))))
687
688 (defun nnml-save-mail (group-art &optional server full-nov)
689   "Save a mail into the groups GROUP-ART in the nnml server SERVER.
690 GROUP-ART is a list that each element is a cons of a group name and an
691 article number.  This function is called narrowed to an article."
692   (let* ((chars (nnmail-insert-lines))
693          (extension (and nnml-use-compressed-files
694                          (> chars nnml-compressed-files-size-threshold)
695                          (if (stringp nnml-use-compressed-files)
696                              nnml-use-compressed-files
697                            ".gz")))
698          decoded dec file first headers)
699     (when nnmail-group-names-not-encoded-p
700       (dolist (ga (prog1 group-art (setq group-art nil)))
701         (setq group-art (nconc group-art
702                                (list (cons (nnml-encoded-group-name (car ga)
703                                                                     server)
704                                            (cdr ga))))
705               decoded (nconc decoded (list (car ga)))))
706       (setq dec decoded))
707     (nnmail-insert-xref group-art)
708     (run-hooks 'nnmail-prepare-save-mail-hook)
709     (run-hooks 'nnml-prepare-save-mail-hook)
710     (goto-char (point-min))
711     (while (looking-at "From ")
712       (replace-match "X-From-Line: ")