* nnml.el (nnml-request-article): Allow requesting by Message-ID to
[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, 2011 Free Software
5 ;;   Foundation, Inc.
6
7 ;; Authors: Didier Verna <didier@xemacs.org> (adding compaction)
8 ;;      Simon Josefsson <simon@josefsson.org> (adding MARKS)
9 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
10 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
11 ;; Keywords: news, 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
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Commentary:
29
30 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
31 ;; For an overview of what the interface functions do, please see the
32 ;; Gnus sources.
33
34 ;;; Code:
35
36 (require 'gnus)
37 (require 'nnheader)
38 (require 'nnmail)
39 (require 'nnoo)
40 (eval-when-compile (require 'cl))
41
42 ;; FIXME first is unused in this file.
43 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
44 (autoload 'gnus-backlog-remove-article "gnus-bcklg")
45
46 (nnoo-declare nnml)
47
48 (defvoo nnml-directory message-directory
49   "Spool directory for the nnml mail backend.")
50
51 (defvoo nnml-active-file
52     (expand-file-name "active" nnml-directory)
53   "Mail active file.")
54
55 (defvoo nnml-newsgroups-file
56     (expand-file-name "newsgroups" nnml-directory)
57   "Mail newsgroups description file.")
58
59 (defvoo nnml-get-new-mail t
60   "If non-nil, nnml will check the incoming mail file and split the mail.")
61
62 (defvoo nnml-nov-is-evil nil
63   "If non-nil, Gnus will never generate and use nov databases for mail spools.
64 Using nov databases will speed up header fetching considerably.
65 This variable shouldn't be flipped much.  If you have, for some reason,
66 set this to t, and want to set it to nil again, you should always run
67 the `nnml-generate-nov-databases' command.  The function will go
68 through all nnml directories and generate nov databases for them
69 all.  This may very well take some time.")
70
71 (defvoo nnml-marks-is-evil nil
72   "If non-nil, Gnus will never generate and use marks file for mail spools.
73 Using marks files makes it possible to backup and restore mail groups
74 separately from `.newsrc.eld'.  If you have, for some reason, set this
75 to t, and want to set it to nil again, you should always remove the
76 corresponding marks file (usually named `.marks' in the nnml group
77 directory, but see `nnml-marks-file-name') for the group.  Then the
78 marks file will be regenerated properly by Gnus.")
79
80 (defvoo nnml-prepare-save-mail-hook nil
81   "Hook run narrowed to an article before saving.")
82
83 (defvoo nnml-inhibit-expiry nil
84   "If non-nil, inhibit expiry.")
85
86 (defvoo nnml-use-compressed-files nil
87   "If non-nil, allow using compressed message files.
88
89 If it is a string, use it as the file extension which specifies
90 the compression program.  You can set it to \".bz2\" if your Emacs
91 supports auto-compression using the bzip2 program.  A value of t
92 is equivalent to \".gz\".")
93
94 (defvoo nnml-compressed-files-size-threshold 1000
95   "Default size threshold for compressed message files.
96 Message files with bodies larger than that many characters will
97 be automatically compressed if `nnml-use-compressed-files' is
98 non-nil.")
99
100 \f
101
102 (defconst nnml-version "nnml 1.0"
103   "nnml version.")
104
105 (defvoo nnml-nov-file-name ".overview")
106 (defvoo nnml-marks-file-name ".marks")
107
108 (defvoo nnml-current-directory nil)
109 (defvoo nnml-current-group nil)
110 (defvoo nnml-status-string "")
111 (defvoo nnml-nov-buffer-alist nil)
112 (defvoo nnml-group-alist nil)
113 (defvoo nnml-active-timestamp nil)
114 (defvoo nnml-article-file-alist nil)
115
116 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
117
118 (defvar nnml-nov-buffer-file-name nil)
119
120 (defvoo nnml-file-coding-system nnmail-file-coding-system)
121
122 (defvoo nnml-marks nil)
123
124 (defvar nnml-marks-modtime (gnus-make-hashtable))
125
126 \f
127 ;;; Interface functions.
128
129 (nnoo-define-basics nnml)
130
131 (eval-when-compile
132   (defsubst nnml-group-name-charset (group server-or-method)
133     (gnus-group-name-charset
134      (if (stringp server-or-method)
135          (gnus-server-to-method
136           (if (string-match "\\+" server-or-method)
137               (concat (substring server-or-method 0 (match-beginning 0))
138                       ":" (substring server-or-method (match-end 0)))
139             (concat "nnml:" server-or-method)))
140        (or server-or-method gnus-command-method '(nnml "")))
141      group)))
142
143 (defun nnml-decoded-group-name (group &optional server-or-method)
144   "Return a decoded group name of GROUP on SERVER-OR-METHOD."
145   (if nnmail-group-names-not-encoded-p
146       group
147     (mm-decode-coding-string
148      group
149      (nnml-group-name-charset group server-or-method))))
150
151 (defun nnml-encoded-group-name (group &optional server-or-method)
152   "Return an encoded group name of GROUP on SERVER-OR-METHOD."
153   (mm-encode-coding-string
154    group
155    (nnml-group-name-charset group server-or-method)))
156
157 (defun nnml-group-pathname (group &optional file server)
158   "Return an absolute file name of FILE for GROUP on SERVER."
159   (nnmail-group-pathname (inline (nnml-decoded-group-name group server))
160                          nnml-directory file))
161
162 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
163   (when (nnml-possibly-change-directory group server)
164     (with-current-buffer nntp-server-buffer
165       (erase-buffer)
166       (let* ((file nil)
167              (number (length sequence))
168              (count 0)
169              (file-name-coding-system nnmail-pathname-coding-system)
170              beg article)
171         (if (stringp (car sequence))
172             'headers
173           (if (nnml-retrieve-headers-with-nov sequence fetch-old)
174               'nov
175             (while sequence
176               (setq article (car sequence))
177               (setq file (nnml-article-to-file article))
178               (when (and file
179                          (file-exists-p file)
180                          (not (file-directory-p file)))
181                 (insert (format "221 %d Article retrieved.\n" article))
182                 (setq beg (point))
183                 (nnheader-insert-head file)
184                 (goto-char beg)
185                 (if (re-search-forward "\n\r?\n" nil t)
186                     (forward-char -1)
187                   (goto-char (point-max))
188                   (insert "\n\n"))
189                 (insert ".\n")
190                 (delete-region (point) (point-max)))
191               (setq sequence (cdr sequence))
192               (setq count (1+ count))
193               (and (numberp nnmail-large-newsgroup)
194                    (> number nnmail-large-newsgroup)
195                    (zerop (% count 20))
196                    (nnheader-message 6 "nnml: Receiving headers... %d%%"
197                                      (/ (* count 100) number))))
198
199             (and (numberp nnmail-large-newsgroup)
200                  (> number nnmail-large-newsgroup)
201                  (nnheader-message 6 "nnml: Receiving headers...done"))
202
203             (nnheader-fold-continuation-lines)
204             'headers))))))
205
206 (deffoo nnml-open-server (server &optional defs)
207   (nnoo-change-server 'nnml server defs)
208   (when (not (file-exists-p nnml-directory))
209     (ignore-errors (make-directory nnml-directory t)))
210   (cond
211    ((not (file-exists-p nnml-directory))
212     (nnml-close-server)
213     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
214    ((not (file-directory-p (file-truename nnml-directory)))
215     (nnml-close-server)
216     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
217    (t
218     (nnheader-report 'nnml "Opened server %s using directory %s"
219                      server nnml-directory)
220     t)))
221
222 (deffoo nnml-request-regenerate (server)
223   (nnml-possibly-change-directory nil server)
224   (nnml-generate-nov-databases server)
225   t)
226
227 (deffoo nnml-request-article (id &optional group server buffer)
228   (nnml-possibly-change-directory group server)
229   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
230          (file-name-coding-system nnmail-pathname-coding-system)
231          path gpath group-num)
232     (if (stringp id)
233         (when (and (setq group-num (nnml-find-group-number id server))
234                    (cdr
235                     (assq (cdr group-num)
236                           (nnheader-article-to-file-alist
237                            (setq gpath (nnml-group-pathname (car group-num)
238                                                             nil server))))))
239           (nnml-update-file-alist)
240           (setq path (concat gpath (if nnml-use-compressed-files
241                                        (cdr (assq (cdr group-num)
242                                                   nnml-article-file-alist))
243                                      (number-to-string (cdr group-num))))))
244       (setq path (nnml-article-to-file id)))
245     (cond
246      ((not path)
247       (nnheader-report 'nnml "No such article: %s" id))
248      ((not (file-exists-p path))
249       (nnheader-report 'nnml "No such file: %s" path))
250      ((file-directory-p path)
251       (nnheader-report 'nnml "File is a directory: %s" path))
252      ((not (save-excursion (let ((nnmail-file-coding-system
253                                   nnml-file-coding-system))
254                              (nnmail-find-file path))))
255       (nnheader-report 'nnml "Couldn't read file: %s" path))
256      (t
257       (nnheader-report 'nnml "Article %s retrieved" id)
258       ;; We return the article number.
259       (cons (if group-num (car group-num) group)
260             (string-to-number (file-name-nondirectory path)))))))
261
262 (deffoo nnml-request-group (group &optional server dont-check info)
263   (let ((file-name-coding-system nnmail-pathname-coding-system)
264         (decoded (nnml-decoded-group-name group server)))
265     (cond
266      ((not (nnml-possibly-change-directory group server))
267       (nnheader-report 'nnml "Invalid group (no such directory)"))
268      ((not (file-exists-p nnml-current-directory))
269       (nnheader-report 'nnml "Directory %s does not exist"
270                        nnml-current-directory))
271      ((not (file-directory-p nnml-current-directory))
272       (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
273      (dont-check
274       (nnheader-report 'nnml "Group %s selected" decoded)
275       t)
276      (t
277       (nnheader-re-read-dir nnml-current-directory)
278       (nnmail-activate 'nnml)
279       (let ((active (nth 1 (assoc group nnml-group-alist))))
280         (if (not active)
281             (nnheader-report 'nnml "No such group: %s" decoded)
282           (nnheader-report 'nnml "Selected group %s" decoded)
283           (nnheader-insert "211 %d %d %d %s\n"
284                            (max (1+ (- (cdr active) (car active))) 0)
285                            (car active) (cdr active) group)))))))
286
287 (deffoo nnml-request-scan (&optional group server)
288   (setq nnml-article-file-alist nil)
289   (nnml-possibly-change-directory group server)
290   (nnmail-get-new-mail 'nnml 'nnml-save-incremental-nov nnml-directory group))
291
292 (deffoo nnml-close-group (group &optional server)
293   (setq nnml-article-file-alist nil)
294   t)
295
296 (deffoo nnml-request-create-group (group &optional server args)
297   (nnml-possibly-change-directory nil server)
298   (nnmail-activate 'nnml)
299   (cond
300    ((let ((file (directory-file-name (nnml-group-pathname group nil server)))
301           (file-name-coding-system nnmail-pathname-coding-system))
302       (and (file-exists-p file)
303            (not (file-directory-p file))))
304     (nnheader-report 'nnml "%s is a file"
305                      (directory-file-name (nnml-group-pathname group
306                                                                nil server))))
307    ((assoc group nnml-group-alist)
308     t)
309    (t
310     (let (active)
311       (push (list group (setq active (cons 1 0)))
312             nnml-group-alist)
313       (nnml-possibly-create-directory group server)
314       (nnml-possibly-change-directory group server)
315       (let* ((file-name-coding-system nnmail-pathname-coding-system)
316              (articles (nnml-directory-articles nnml-current-directory)))
317         (when articles
318           (setcar active (apply 'min articles))
319           (setcdr active (apply 'max articles))))
320       (nnmail-save-active nnml-group-alist nnml-active-file)
321       t))))
322
323 (deffoo nnml-request-list (&optional server)
324   (save-excursion
325     (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
326           (file-name-coding-system nnmail-pathname-coding-system))
327       (nnmail-find-file nnml-active-file))
328     (setq nnml-group-alist (nnmail-get-active))
329     t))
330
331 (deffoo nnml-request-newgroups (date &optional server)
332   (nnml-request-list server))
333
334 (deffoo nnml-request-list-newsgroups (&optional server)
335   (save-excursion
336     (nnmail-find-file nnml-newsgroups-file)))
337
338 (deffoo nnml-request-expire-articles (articles group &optional server force)
339   (nnml-possibly-change-directory group server)
340   (let* ((file-name-coding-system nnmail-pathname-coding-system)
341          (active-articles
342           (nnml-directory-articles nnml-current-directory))
343          (is-old t)
344          (decoded (nnml-decoded-group-name group server))
345          article rest mod-time number target)
346     (nnmail-activate 'nnml)
347
348     (setq active-articles (sort active-articles '<))
349     ;; Articles not listed in active-articles are already gone,
350     ;; so don't try to expire them.
351     (setq articles (gnus-sorted-intersection articles active-articles))
352
353     (while (and articles is-old)
354       (if (and (setq article (nnml-article-to-file
355                               (setq number (pop articles))))
356                (setq mod-time (nth 5 (file-attributes article)))
357                (nnml-deletable-article-p group number)
358                (setq is-old (nnmail-expired-article-p group mod-time force
359                                                       nnml-inhibit-expiry)))
360           (progn
361             ;; Allow a special target group.
362             (setq target nnmail-expiry-target)
363             (unless (eq target 'delete)
364               (with-temp-buffer
365                 (nnml-request-article number group server (current-buffer))
366                 (let (nnml-current-directory
367                       nnml-current-group
368                       nnml-article-file-alist)
369                   (when (functionp target)
370                     (setq target (funcall target group)))
371                   (when (and target (not (eq target 'delete)))
372                     (if (or (gnus-request-group target)
373                             (gnus-request-create-group target))
374                         (nnmail-expiry-target-group target group)
375                       (setq target nil)))))
376               ;; Maybe directory is changed during nnmail-expiry-target-group.
377               (nnml-possibly-change-directory group server))
378             (if target
379                 (progn
380                   (nnheader-message 5 "Deleting article %s in %s"
381                                     number decoded)
382                   (condition-case ()
383                       (funcall nnmail-delete-file-function article)
384                     (file-error
385                      (push number rest)))
386                   (setq active-articles (delq number active-articles))
387                   (nnml-nov-delete-article group number))
388               (push number rest)))
389         (push number rest)))
390     (let ((active (nth 1 (assoc group nnml-group-alist))))
391       (when active
392         (setcar active (or (and active-articles
393                                 (apply 'min active-articles))
394                            (1+ (cdr active)))))
395       (nnmail-save-active nnml-group-alist nnml-active-file))
396     (nnml-save-nov)
397     (nconc rest articles)))
398
399 (deffoo nnml-request-move-article
400     (article group server accept-form &optional last move-is-internal)
401   (let ((buf (get-buffer-create " *nnml move*"))
402         (file-name-coding-system nnmail-pathname-coding-system)
403         result)
404     (nnml-possibly-change-directory group server)
405     (nnml-update-file-alist)
406     (and
407      (nnml-deletable-article-p group article)
408      (nnml-request-article article group server)
409      (let (nnml-current-directory
410            nnml-current-group
411            nnml-article-file-alist)
412        (with-current-buffer buf
413          (insert-buffer-substring nntp-server-buffer)
414          (setq result (eval accept-form))
415          (kill-buffer (current-buffer))
416          result))
417      (progn
418        (nnml-possibly-change-directory group server)
419        (condition-case ()
420            (funcall nnmail-delete-file-function
421                     (nnml-article-to-file  article))
422          (file-error nil))
423        (nnml-nov-delete-article group article)
424        (when last
425          (nnml-save-nov)
426          (nnmail-save-active nnml-group-alist nnml-active-file))))
427     result))
428
429 (deffoo nnml-request-accept-article (group &optional server last)
430   (nnml-possibly-change-directory group server)
431   (nnmail-check-syntax)
432   (let (result)
433     (when nnmail-cache-accepted-message-ids
434       (nnmail-cache-insert (nnmail-fetch-field "message-id")
435                            group
436                            (nnmail-fetch-field "subject")
437                            (nnmail-fetch-field "from")))
438     (if (stringp group)
439         (and
440          (nnmail-activate 'nnml)
441          (setq result (car (nnml-save-mail
442                             (list (cons group (nnml-active-number group
443                                                                   server)))
444                             server t)))
445          (progn
446            (nnmail-save-active nnml-group-alist nnml-active-file)
447            (and last (nnml-save-nov))))
448       (and
449        (nnmail-activate 'nnml)
450        (if (and (not (setq result (nnmail-article-group
451                                    `(lambda (group)
452                                       (nnml-active-number group ,server)))))
453                 (yes-or-no-p "Moved to `junk' group; delete article? "))
454            (setq result 'junk)
455          (setq result (car (nnml-save-mail result server t))))
456        (when last
457          (nnmail-save-active nnml-group-alist nnml-active-file)
458          (when nnmail-cache-accepted-message-ids
459            (nnmail-cache-close))
460          (nnml-save-nov))))
461     result))
462
463 (deffoo nnml-request-post (&optional server)
464   (nnmail-do-request-post 'nnml-request-accept-article server))
465
466 (deffoo nnml-request-replace-article (article group buffer)
467   (nnml-possibly-change-directory group)
468   (with-current-buffer buffer
469     (nnml-possibly-create-directory group)
470     (let ((chars (nnmail-insert-lines))
471           (art (concat (int-to-string article) "\t"))
472           headers)
473       (when (ignore-errors
474               (nnmail-write-region
475                (point-min) (point-max)
476                (or (nnml-article-to-file article)
477                    (expand-file-name (int-to-string article)
478                                      nnml-current-directory))
479                nil (if (nnheader-be-verbose 5) nil 'nomesg))
480               t)
481         (setq headers (nnml-parse-head chars article))
482         ;; Replace the NOV line in the NOV file.
483         (with-current-buffer (nnml-open-nov group)
484           (goto-char (point-min))
485           (if (or (looking-at art)
486                   (search-forward (concat "\n" art) nil t))
487               ;; Delete the old NOV line.
488               (gnus-delete-line)
489             ;; The line isn't here, so we have to find out where
490             ;; we should insert it.  (This situation should never
491             ;; occur, but one likes to make sure...)
492             (while (and (looking-at "[0-9]+\t")
493                         (< (string-to-number
494                             (buffer-substring
495                              (match-beginning 0) (match-end 0)))
496                            article)
497                         (zerop (forward-line 1)))))
498           (beginning-of-line)
499           (nnheader-insert-nov headers)
500           (nnml-save-nov)
501           t)))))
502
503 (deffoo nnml-request-delete-group (group &optional force server)
504   (nnml-possibly-change-directory group server)
505   (let ((file (directory-file-name nnml-current-directory))
506         (file-name-coding-system nnmail-pathname-coding-system))
507     (if (file-exists-p file)
508         (if (file-directory-p file)
509             (progn
510               (when force
511                 ;; Delete all articles in GROUP.
512                 (let ((articles
513                        (directory-files
514                         nnml-current-directory t
515                         (concat
516                          nnheader-numerical-short-files
517                          "\\|" (regexp-quote nnml-nov-file-name) "$"
518                          "\\|" (regexp-quote nnml-marks-file-name) "$")))
519                       (decoded (nnml-decoded-group-name group server)))
520                   (dolist (article articles)
521                     (when (file-writable-p article)
522                       (nnheader-message 5 "Deleting article %s in %s..."
523                                         (file-name-nondirectory article)
524                                         decoded)
525                       (funcall nnmail-delete-file-function article))))
526                 ;; Try to delete the directory itself.
527                 (ignore-errors (delete-directory nnml-current-directory))))
528           (nnheader-report 'nnml "%s is not a directory" file))
529       (nnheader-report 'nnml "No such directory: %s/" file))
530     ;; Remove the group from all structures.
531     (setq nnml-group-alist
532           (delq (assoc group nnml-group-alist) nnml-group-alist)
533           nnml-current-group nil
534           nnml-current-directory nil)
535     ;; Save the active file.
536     (nnmail-save-active nnml-group-alist nnml-active-file))
537   t)
538
539 (deffoo nnml-request-rename-group (group new-name &optional server)
540   (nnml-possibly-change-directory group server)
541   (let ((new-dir (nnml-group-pathname new-name nil server))
542         (old-dir (nnml-group-pathname group nil server))
543         (file-name-coding-system nnmail-pathname-coding-system))
544     (when (ignore-errors
545             (make-directory new-dir t)
546             t)
547       ;; We move the articles file by file instead of renaming
548       ;; the directory -- there may be subgroups in this group.
549       ;; One might be more clever, I guess.
550       (dolist (file (nnheader-article-to-file-alist old-dir))
551         (rename-file
552          (concat old-dir (cdr file))
553          (concat new-dir (cdr file))))
554       ;; Move .overview file.
555       (let ((overview (concat old-dir nnml-nov-file-name)))
556         (when (file-exists-p overview)
557           (rename-file overview (concat new-dir nnml-nov-file-name))))
558       ;; Move .marks file.
559       (let ((marks (concat old-dir nnml-marks-file-name)))
560         (when (file-exists-p marks)
561           (rename-file marks (concat new-dir nnml-marks-file-name))))
562       (when (<= (length (directory-files old-dir)) 2)
563         (ignore-errors (delete-directory old-dir)))
564       ;; That went ok, so we change the internal structures.
565       (let ((entry (assoc group nnml-group-alist)))
566         (when entry
567           (setcar entry new-name))
568         (setq nnml-current-directory nil
569               nnml-current-group nil)
570         ;; Save the new group alist.
571         (nnmail-save-active nnml-group-alist nnml-active-file)
572         t))))
573
574 (deffoo nnml-set-status (article name value &optional group server)
575   (nnml-possibly-change-directory group server)
576   (let ((file (nnml-article-to-file article)))
577     (cond
578      ((not (file-exists-p file))
579       (nnheader-report 'nnml "File %s does not exist" file))
580      (t
581       (with-temp-file file
582         (nnheader-insert-file-contents file)
583         (nnmail-replace-status name value))
584       t))))
585
586 \f
587 ;;; Internal functions.
588
589 (defun nnml-article-to-file (article)
590   (nnml-update-file-alist)
591   (let (file)
592     (if (setq file
593               (if nnml-use-compressed-files
594                   (cdr (assq article nnml-article-file-alist))
595                 (number-to-string article)))
596         (expand-file-name file nnml-current-directory)
597       (when (not nnheader-directory-files-is-safe)
598         ;; Just to make sure nothing went wrong when reading over NFS --
599         ;; check once more.
600         (when (file-exists-p
601                (setq file (expand-file-name (number-to-string article)
602                                             nnml-current-directory)))
603           (nnml-update-file-alist t)
604           file)))))
605
606 (defun nnml-deletable-article-p (group article)
607   "Say whether ARTICLE in GROUP can be deleted."
608   (let ((file-name-coding-system nnmail-pathname-coding-system)
609         path)
610     (when (setq path (nnml-article-to-file article))
611       (when (file-writable-p path)
612         (or (not nnmail-keep-last-article)
613             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
614                      article)))))))
615
616 ;; Find an article number in the current group given the Message-ID.
617 (defun nnml-find-group-number (id server)
618   (with-current-buffer (get-buffer-create " *nnml id*")
619     (let ((alist nnml-group-alist)
620           number)
621       ;; We want to look through all .overview files, but we want to
622       ;; start with the one in the current directory.  It seems most
623       ;; likely that the article we are looking for is in that group.
624       (if (setq number (nnml-find-id nnml-current-group id server))
625           (cons nnml-current-group number)
626       ;; It wasn't there, so we look through the other groups as well.
627         (while (and (not number)
628                     alist)
629           (or (string= (caar alist) nnml-current-group)
630               (setq number (nnml-find-id (caar alist) id server)))
631           (or number
632               (setq alist (cdr alist))))
633         (and number
634              (cons (caar alist) number))))))
635
636 (defun nnml-find-id (group id server)
637   (erase-buffer)
638   (let ((nov (nnml-group-pathname group nnml-nov-file-name server))
639         number found)
640     (when (file-exists-p nov)
641       (nnheader-insert-file-contents nov)
642       (while (and (not found)
643                   (search-forward id nil t)) ; We find the ID.
644         ;; And the id is in the fourth field.
645         (if (not (and (search-backward "\t" nil t 4)
646                       (not (search-backward "\t" (point-at-bol) t))))
647             (forward-line 1)
648           (beginning-of-line)
649           (setq found t)
650           ;; We return the article number.
651           (setq number
652                 (ignore-errors (read (current-buffer))))))
653       number)))
654
655 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
656   (if (or gnus-nov-is-evil nnml-nov-is-evil)
657       nil
658     (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
659       (when (file-exists-p nov)
660         (with-current-buffer nntp-server-buffer
661           (erase-buffer)
662           (nnheader-insert-file-contents nov)
663           (if (and fetch-old
664                    (not (numberp fetch-old)))
665               t                         ; Don't remove anything.
666             (nnheader-nov-delete-outside-range
667              (if fetch-old (max 1 (- (car articles) fetch-old))
668                (car articles))
669              (car (last articles)))
670             t))))))
671
672 (defun nnml-possibly-change-directory (group &optional server)
673   (when (and server
674              (not (nnml-server-opened server)))
675     (nnml-open-server server))
676   (if (not group)
677       t
678     (let ((pathname (nnml-group-pathname group nil server))
679           (file-name-coding-system nnmail-pathname-coding-system))
680       (when (not (equal pathname nnml-current-directory))
681         (setq nnml-current-directory pathname
682               nnml-current-group group
683               nnml-article-file-alist nil))
684       (file-exists-p nnml-current-directory))))
685
686 (defun nnml-possibly-create-directory (group &optional server)
687   (let ((dir (nnml-group-pathname group nil server))
688         (file-name-coding-system nnmail-pathname-coding-system))
689     (unless (file-exists-p dir)
690       (make-directory (directory-file-name dir) t)
691       (nnheader-message 5 "Creating mail directory %s" dir))))
692
693 (defun nnml-save-mail (group-art &optional server full-nov)
694   "Save a mail into the groups GROUP-ART in the nnml server SERVER.
695 GROUP-ART is a list that each element is a cons of a group name and an
696 article number.  This function is called narrowed to an article."
697   (let* ((chars (nnmail-insert-lines))
698          (extension (and nnml-use-compressed-files
699                          (> chars nnml-compressed-files-size-threshold)
700                          (if (stringp nnml-use-compressed-files)
701                              nnml-use-compressed-files
702                            ".gz")))
703          decoded dec file first headers)
704     (when nnmail-group-names-not-encoded-p
705       (dolist (ga (prog1 group-art (setq group-art nil)))
706         (setq group-art (nconc group-art
707                                (list (cons (nnml-encoded-group-name (car ga)
708                                                                     server)
709                                            (cdr ga))))
710               decoded (nconc decoded (list (car ga)))))
711       (setq dec decoded))
712     (nnmail-insert-xref group-art)
713     (run-hooks 'nnmail-prepare-save-mail-hook)
714     (run-hooks 'nnml-prepare-save-mail-hook)
715     (goto-char (point-min))
716     (while (looking-at "From ")
717       (replace-match "X-From-Line: ")
718       (forward-line 1))
719     ;; We save the article in all the groups it belongs in.
720     (dolist (ga group-art)
721       (if nnmail-group-names-not-encoded-p
722           (progn
723             (nnml-possibly-create-directory (car decoded) server)
724             (setq file (nnmail-group-pathname
725                         (pop decoded) nnml-directory
726                         (concat (number-to-string (cdr ga)) extension))))
727         (nnml-possibly-create-directory (car ga) server)
728         (setq file (nnml-group-pathname
729                     (car ga) (concat (number-to-string (cdr ga)) extension)
730                     server)))
731       (if first
732           ;; It was already saved, so we just make a hard link.
733           (let ((file-name-coding-system nnmail-pathname-coding-system))
734             (funcall nnmail-crosspost-link-function first file t))
735         ;; Save the article.
736         (nnmail-write-region (point-min) (point-max) file nil
737                              (if (nnheader-be-verbose 5) nil 'nomesg))
738         (setq first file)))
739     ;; Generate a nov line for this article.  We generate the nov
740     ;; line after saving, because nov generation destroys the
741     ;; header.
742     (setq headers (nnml-parse-head chars))
743     ;; Output the nov line to all nov databases that should have it.
744     (let ((func (if full-nov
745                     'nnml-add-nov
746                   'nnml-add-incremental-nov)))
747       (if nnmail-group-names-not-encoded-p
748           (dolist (ga group-art)
749             (funcall func (pop dec) (cdr ga) headers))
750         (dolist (ga group-art)
751           (funcall func (car ga) (cdr ga) headers)))))
752   group-art)
753
754 (defun nnml-active-number (group &optional server)
755   "Compute the next article number in GROUP on SERVER."
756   (let* ((encoded (if nnmail-group-names-not-encoded-p
757                       (nnml-encoded-group-name group server)))
758          (active (cadr (assoc (or encoded group) nnml-group-alist))))
759     ;; The group wasn't known to nnml, so we just create an active
760     ;; entry for it.
761     (unless active
762       ;; Perhaps the active file was corrupt?  See whether
763       ;; there are any articles in this group.
764       (nnml-possibly-create-directory group server)
765       (nnml-possibly-change-directory group server)
766       (unless nnml-article-file-alist
767         (setq nnml-article-file-alist
768               (sort
769                (nnml-current-group-article-to-file-alist)
770                'car-less-than-car)))
771       (setq active
772             (if nnml-article-file-alist
773                 (cons (caar nnml-article-file-alist)
774                       (caar (last nnml-article-file-alist)))
775               (cons 1 0)))
776       (push (list (or encoded group) active) nnml-group-alist))
777     (setcdr active (1+ (cdr active)))
778     (while (file-exists-p
779             (nnml-group-pathname group (int-to-string (cdr active)) server))
780       (setcdr active (1+ (cdr active))))
781     (cdr active)))
782
783 (defvar nnml-incremental-nov-buffer-alist nil)
784
785 (defun nnml-save-incremental-nov ()
786   (save-excursion
787     (while nnml-incremental-nov-buffer-alist
788       (when (buffer-name (cdar nnml-incremental-nov-buffer-alist))
789         (set-buffer (cdar nnml-incremental-nov-buffer-alist))
790         (when (buffer-modified-p)
791           (nnmail-write-region (point-min) (point-max)
792                                nnml-nov-buffer-file-name t 'nomesg))
793         (set-buffer-modified-p nil)
794         (kill-buffer (current-buffer)))
795       (setq nnml-incremental-nov-buffer-alist
796             (cdr nnml-incremental-nov-buffer-alist)))))
797
798 (defun nnml-open-incremental-nov (group)
799   (or (cdr (assoc group nnml-incremental-nov-buffer-alist))
800       (let ((buffer (nnml-get-nov-buffer group t)))
801         (push (cons group buffer) nnml-incremental-nov-buffer-alist)
802         buffer)))
803
804 (defun nnml-add-incremental-nov (group article headers)
805   "Add a nov line for the GROUP nov headers, incrementally."
806   (with-current-buffer (nnml-open-incremental-nov group)
807     (goto-char (point-max))
808     (mail-header-set-number headers article)
809     (nnheader-insert-nov headers)))
810
811 (defun nnml-add-nov (group article headers)
812   "Add a nov line for the GROUP base."
813   (with-current-buffer (nnml-open-nov group)
814     (goto-char (point-max))
815     (mail-header-set-number headers article)
816     (nnheader-insert-nov headers)))
817
818 (defsubst nnml-header-value ()
819   (buffer-substring (match-end 0) (point-at-eol)))
820
821 (defun nnml-parse-head (chars &optional number)
822   "Parse the head of the current buffer."
823   (save-excursion
824     (save-restriction
825       (unless (zerop (buffer-size))
826         (narrow-to-region
827          (goto-char (point-min))
828          (if (re-search-forward "\n\r?\n" nil t)
829              (1- (point))
830            (point-max))))
831       (let ((headers (nnheader-parse-naked-head)))
832         (mail-header-set-chars headers chars)
833         (mail-header-set-number headers number)
834         headers))))
835
836 (defun nnml-get-nov-buffer (group &optional incrementalp)
837   (let* ((decoded (nnml-decoded-group-name group))
838          (buffer (get-buffer-create (format " *nnml %soverview %s*"
839                                             (if incrementalp
840                                                 "incremental "
841                                               "")
842                                             decoded)))
843          (file-name-coding-system nnmail-pathname-coding-system))
844     (with-current-buffer buffer
845       (set (make-local-variable 'nnml-nov-buffer-file-name)
846            (nnmail-group-pathname decoded nnml-directory nnml-nov-file-name))
847       (erase-buffer)
848       (when (and (not incrementalp)
849                  (file-exists-p nnml-nov-buffer-file-name))
850         (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
851     buffer))
852
853 (defun nnml-open-nov (group)
854   (or (let ((buffer (cdr (assoc group nnml-nov-buffer-alist))))
855         (and (buffer-name buffer)
856              buffer))
857       (let ((buffer (nnml-get-nov-buffer group)))
858         (push (cons group buffer) nnml-nov-buffer-alist)
859         buffer)))
860
861 (defun nnml-save-nov ()
862   (save-excursion
863     (while nnml-nov-buffer-alist
864       (when (buffer-name (cdar nnml-nov-buffer-alist))
865         (set-buffer (cdar nnml-nov-buffer-alist))
866         (when (buffer-modified-p)
867           (nnmail-write-region (point-min) (point-max)
868                                nnml-nov-buffer-file-name nil 'nomesg))
869         (set-buffer-modified-p nil)
870         (kill-buffer (current-buffer)))
871       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
872
873 ;;;###autoload
874 (defun nnml-generate-nov-databases (&optional server)
875   "Generate NOV databases in all nnml directories."
876   (interactive (list (or (nnoo-current-server 'nnml) "")))
877   ;; Read the active file to make sure we don't re-use articles
878   ;; numbers in empty groups.
879   (nnmail-activate 'nnml)
880   (unless (nnml-server-opened server)
881     (nnml-open-server server))
882   (setq nnml-directory (expand-file-name nnml-directory))
883   ;; Recurse down the directories.
884   (nnml-generate-nov-databases-directory nnml-directory nil t)
885   ;; Save the active file.
886   (nnmail-save-active nnml-group-alist nnml-active-file))
887
888 (defvar nnml-files)
889 (defun nnml-generate-nov-databases-directory (dir &optional seen no-active)
890   "Regenerate the NOV database in DIR.
891
892 Unless no-active is non-nil, update the active file too."
893   (interactive (list (let ((file-name-coding-system
894                             nnmail-pathname-coding-system))
895                        (read-directory-name "Regenerate NOV in: "
896                                             nnml-directory nil t))))
897   (setq dir (file-name-as-directory dir))
898   (let ((file-name-coding-system nnmail-pathname-coding-system))
899     ;; Only scan this sub-tree if we haven't been here yet.
900     (unless (member (file-truename dir) seen)
901       (push (file-truename dir) seen)
902       ;; We descend recursively
903       (dolist (dir (directory-files dir t nil t))
904         (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
905                    (file-directory-p dir))
906           (nnml-generate-nov-databases-directory dir seen)))
907       ;; Do this directory.
908       (let ((nnml-files (sort (nnheader-article-to-file-alist dir)
909                          'car-less-than-car)))
910         (if (not nnml-files)
911             (let* ((group (nnheader-file-to-group
912                            (directory-file-name dir) nnml-directory))
913                    (info (cadr (assoc group nnml-group-alist))))
914               (when info
915                 (setcar info (1+ (cdr info)))))
916           (funcall nnml-generate-active-function dir)
917           ;; Generate the nov file.
918           (nnml-generate-nov-file dir nnml-files)
919           (unless no-active
920             (nnmail-save-active nnml-group-alist nnml-active-file)))))))
921
922 (defun nnml-generate-active-info (dir)
923   ;; Update the active info for this group.
924   (let ((group (directory-file-name dir))
925         entry last)
926     (setq group (nnheader-file-to-group (nnml-encoded-group-name group)
927                                         nnml-directory)
928           entry (assoc group nnml-group-alist)
929           last (or (caadr entry) 0)
930           nnml-group-alist (delq entry nnml-group-alist))
931     (push (list group
932                 (cons (or (caar nnml-files) (1+ last))
933                       (max last
934                            (or (caar (last nnml-files))
935                                0))))
936           nnml-group-alist)))
937
938 (defun nnml-generate-nov-file (dir files)
939   (let* ((dir (file-name-as-directory dir))
940          (nov (concat dir nnml-nov-file-name))
941          (nov-buffer (get-buffer-create " *nov*"))
942          chars file headers)
943     (with-current-buffer nov-buffer
944       ;; Init the nov buffer.
945       (buffer-disable-undo)
946       (erase-buffer)
947       (set-buffer nntp-server-buffer)
948       ;; Delete the old NOV file.
949       (when (file-exists-p nov)
950         (funcall nnmail-delete-file-function nov))
951       (dolist (file files)
952         (let ((path (concat dir (cdr file))))
953           (unless (file-directory-p path)
954             (erase-buffer)
955             (nnheader-insert-file-contents path)
956             (narrow-to-region
957              (goto-char (point-min))
958              (progn
959                (re-search-forward "\n\r?\n" nil t)
960                (setq chars (- (point-max) (point)))
961                (max (point-min) (1- (point)))))
962             (unless (zerop (buffer-size))
963               (goto-char (point-min))
964               (setq headers (nnml-parse-head chars (car file)))
965               (with-current-buffer nov-buffer
966                 (goto-char (point-max))
967                 (nnheader-insert-nov headers)))
968             (widen))))
969       (with-current-buffer nov-buffer
970         (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
971         (kill-buffer (current-buffer))))))
972
973 (defun nnml-nov-delete-article (group article)
974   (with-current-buffer (nnml-open-nov group)
975     (when (nnheader-find-nov-line article)
976       (delete-region (point) (progn (forward-line 1) (point)))
977       (when (bobp)
978         (let ((active (cadr (assoc group nnml-group-alist)))
979               num)
980           (when active
981             (if (eobp)
982                 (setf (car active) (1+ (cdr active)))
983               (when (and (setq num (ignore-errors (read (current-buffer))))
984                          (numberp num))
985                 (setf (car active) num)))))))
986     t))
987
988 (defun nnml-update-file-alist (&optional force)
989   (when nnml-use-compressed-files
990     (when (or (not nnml-article-file-alist)
991               force)
992       (setq nnml-article-file-alist
993             (nnml-current-group-article-to-file-alist)))))
994
995 (defun nnml-directory-articles (dir)
996   "Return a list of all article files in a directory.
997 Use the nov database for that directory if available."
998   (if (or gnus-nov-is-evil nnml-nov-is-evil
999           (not (file-exists-p
1000                 (expand-file-name nnml-nov-file-name dir))))
1001       (nnheader-directory-articles dir)
1002     ;; build list from .overview if available
1003     ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
1004     ;; defvoo'd, and we might get called when it hasn't been swapped in.
1005     (with-current-buffer (nnml-get-nov-buffer nnml-current-group)
1006       (let ((list nil)
1007             art)
1008         (goto-char (point-min))
1009         (while (not (eobp))
1010           (setq art (read (current-buffer)))
1011           (push art list)
1012           (forward-line 1))
1013         list))))
1014
1015 (defun nnml-current-group-article-to-file-alist ()
1016   "Return an alist of article/file pairs in the current group.
1017 Use the nov database for the current group if available."
1018   (if (or nnml-use-compressed-files
1019           gnus-nov-is-evil
1020           nnml-nov-is-evil
1021           (not (file-exists-p
1022                 (expand-file-name nnml-nov-file-name
1023                                   nnml-current-directory))))
1024       (nnheader-article-to-file-alist nnml-current-directory)
1025     ;; build list from .overview if available
1026     (with-current-buffer (nnml-get-nov-buffer nnml-current-group)
1027       (let ((alist nil)
1028             art)
1029         (goto-char (point-min))
1030         (while (not (eobp))
1031           (setq art (read (current-buffer)))
1032           ;; assume file name is unadorned (ie. not compressed etc)
1033           (push (cons art (int-to-string art)) alist)
1034           (forward-line 1))
1035         alist))))
1036
1037 (deffoo nnml-request-set-mark (group actions &optional server)
1038   (nnml-possibly-change-directory group server)
1039   (unless nnml-marks-is-evil
1040     (nnml-open-marks group server)
1041     (setq nnml-marks (nnheader-update-marks-actions nnml-marks actions))
1042     (nnml-save-marks group server))
1043   nil)
1044
1045 (deffoo nnml-request-marks (group info &optional server)
1046   (nnml-possibly-change-directory group server)
1047   (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group server))
1048     (nnheader-message 8 "Updating marks for %s..." group)
1049     (nnml-open-marks group server)
1050     ;; Update info using `nnml-marks'.
1051     (mapc (lambda (pred)
1052             (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1053               (gnus-info-set-marks
1054                info
1055                (gnus-update-alist-soft
1056                 (cdr pred)
1057                 (cdr (assq (cdr pred) nnml-marks))
1058                 (gnus-info-marks info))
1059                t)))
1060           gnus-article-mark-lists)
1061     (let ((seen (cdr (assq 'read nnml-marks))))
1062       (gnus-info-set-read info
1063                           (if (and (integerp (car seen))
1064                                    (null (cdr seen)))
1065                               (list (cons (car seen) (car seen)))
1066                             seen)))
1067     (nnheader-message 8 "Updating marks for %s...done" group))
1068   info)
1069
1070 (defun nnml-marks-changed-p (group server)
1071   (let ((file (nnml-group-pathname group nnml-marks-file-name server)))
1072     (if (null (gnus-gethash file nnml-marks-modtime))
1073         t ;; never looked at marks file, assume it has changed
1074       (not (equal (gnus-gethash file nnml-marks-modtime)
1075                   (nth 5 (file-attributes file)))))))
1076
1077 (defun nnml-save-marks (group server)
1078   (let ((file-name-coding-system nnmail-pathname-coding-system)
1079         (file (nnml-group-pathname group nnml-marks-file-name server)))
1080     (condition-case err
1081         (progn
1082           (nnml-possibly-create-directory group server)
1083           (with-temp-file file
1084             (erase-buffer)
1085             (gnus-prin1 nnml-marks)
1086             (insert "\n"))
1087           (gnus-sethash file
1088                         (nth 5 (file-attributes file))
1089                         nnml-marks-modtime))
1090       (error (or (gnus-yes-or-no-p
1091                   (format "Could not write to %s (%s).  Continue? " file err))
1092                  (error "Cannot write to %s (%s)" file err))))))
1093
1094 (defun nnml-open-marks (group server)
1095   (let* ((decoded (nnml-decoded-group-name group server))
1096          (file (nnmail-group-pathname decoded nnml-directory
1097                                       nnml-marks-file-name))
1098          (file-name-coding-system nnmail-pathname-coding-system))
1099     (if (file-exists-p file)
1100         (condition-case err
1101             (with-temp-buffer
1102               (gnus-sethash file (nth 5 (file-attributes file))
1103                             nnml-marks-modtime)
1104               (nnheader-insert-file-contents file)
1105               (setq nnml-marks (read (current-buffer)))
1106               (dolist (el gnus-article-unpropagated-mark-lists)
1107                 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1108           (error (or (gnus-yes-or-no-p
1109                       (format "Error reading nnml marks file %s (%s).  Continuing will use marks from .newsrc.eld.  Continue? " file err))
1110                      (error "Cannot read nnml marks file %s (%s)" file err))))
1111       ;; User didn't have a .marks file.  Probably first time
1112       ;; user of the .marks stuff.  Bootstrap it from .newsrc.eld.
1113       (let ((info (gnus-get-info
1114                    (gnus-group-prefixed-name
1115                     group
1116                     (gnus-server-to-method
1117                      (format "nnml:%s" (or server "")))))))
1118         (setq decoded (if (member server '(nil ""))
1119                           (concat "nnml:" decoded)
1120                         (format "nnml+%s:%s" server decoded)))
1121         (nnheader-message 7 "Bootstrapping marks for %s..." decoded)
1122         (setq nnml-marks (gnus-info-marks info))
1123         (push (cons 'read (gnus-info-read info)) nnml-marks)
1124         (dolist (el gnus-article-unpropagated-mark-lists)
1125           (setq nnml-marks (gnus-remassoc el nnml-marks)))
1126         (nnml-save-marks group server)
1127         (nnheader-message 7 "Bootstrapping marks for %s...done" decoded)))))
1128
1129
1130 ;;;
1131 ;;; Group and server compaction. -- dvl
1132 ;;;
1133
1134 ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1135 ;; #### know how to handle external cross-references. I actually don't know if
1136 ;; #### this is handled correctly elsewhere. For instance, what happens if you
1137 ;; #### move all articles to a new group (that's what people do for manual
1138 ;; #### compaction) ?
1139
1140 ;; #### NOTE: the function below handles the article backlog. This is
1141 ;; #### conceptually the wrong place to do it because the backend is at a
1142 ;; #### lower level. However, this is the only place where we have the needed
1143 ;; #### information to do the job. Ideally, this function should not handle
1144 ;; #### the backlog by itself, but return a list of moved groups / articles to
1145 ;; #### the caller. This will become important to avoid code duplication when
1146 ;; #### other backends get a compaction feature. Also, note that invalidating
1147 ;; #### the "original article buffer" is already done at an upper level.
1148
1149 ;; Shouldn't `nnml-request-compact-group' be interactive? --rsteib
1150
1151 (defun nnml-request-compact-group (group &optional server save)
1152   (nnml-possibly-change-directory group server)
1153   (unless nnml-article-file-alist
1154     (setq nnml-article-file-alist
1155           (sort (nnml-current-group-article-to-file-alist)
1156                 'car-less-than-car)))
1157   (if (not nnml-article-file-alist)
1158       ;; The group is empty: do nothing but return t
1159       t
1160     ;; The group is not empty:
1161     (let* ((group-full-name
1162             (gnus-group-prefixed-name
1163              group
1164              (gnus-server-to-method (format "nnml:%s" server))))
1165            (info (gnus-get-info group-full-name))
1166            (new-number 1)
1167            compacted)
1168       (let ((articles nnml-article-file-alist)
1169             article)
1170         (while (setq article (pop articles))
1171           (let ((old-number (car article)))
1172             (when (> old-number new-number)
1173               ;; There is a gap here:
1174               (let ((old-number-string (int-to-string old-number))
1175                     (new-number-string (int-to-string new-number)))
1176                 (setq compacted t)
1177                 ;; #### NOTE: `nnml-article-to-file' calls
1178                 ;; #### `nnml-update-file-alist'  (which in turn calls
1179                 ;; #### `nnml-current-group-article-to-file-alist', which
1180                 ;; #### might use the NOV database). This might turn out to be
1181                 ;; #### inefficient. In that case, we will do the work
1182                 ;; #### manually.
1183                 ;; 1/ Move the article to a new file:
1184                 (let* ((oldfile (nnml-article-to-file old-number))
1185                        (newfile
1186                         (gnus-replace-in-string
1187                          oldfile
1188                          ;; nnml-use-compressed-files might be any string, but
1189                          ;; probably it's sufficient to take into account only
1190                          ;; "\\.[a-z0-9]+".  Note that we can't only use the
1191                          ;; value of nnml-use-compressed-files because old
1192                          ;; articles might have been saved with a different
1193                          ;; value.
1194                          (concat
1195                           "\\(" old-number-string "\\)\\(\\(\\.[a-z0-9]+\\)?\\)$")
1196                          (concat new-number-string "\\2"))))
1197                   (with-current-buffer nntp-server-buffer
1198                     (nnmail-find-file oldfile)
1199                     ;; Update the Xref header in the article itself:
1200                     (when (and (re-search-forward "^Xref: [^ ]+ " nil t)
1201                                (re-search-forward
1202                                 (concat "\\<"
1203                                         (regexp-quote
1204                                          (concat group ":" old-number-string))
1205                                         "\\>")
1206                                 (point-at-eol) t))
1207                       (replace-match
1208                        (concat group ":" new-number-string)))
1209                     ;; Save to the new file:
1210                     (nnmail-write-region (point-min) (point-max) newfile))
1211                   (funcall nnmail-delete-file-function oldfile))
1212                 ;; 2/ Update all marks for this article:
1213                 ;; #### NOTE: it is possible that the new article number
1214                 ;; #### already belongs to a range, whereas the corresponding
1215                 ;; #### article doesn't exist (for example, if you delete an
1216                 ;; #### article). For that reason, it is important to update
1217                 ;; #### the ranges (meaning remove inexistent articles) before
1218                 ;; #### doing anything on them.
1219                 ;; 2 a/ read articles:
1220                 (let ((read (gnus-info-read info)))
1221                   (setq read (gnus-remove-from-range read (list new-number)))
1222                   (when (gnus-member-of-range old-number read)
1223                     (setq read (gnus-remove-from-range read (list old-number)))
1224                     (setq read (gnus-add-to-range read (list new-number))))
1225                   (gnus-info-set-read info read))
1226                 ;; 2 b/ marked articles:
1227                 (let ((oldmarks (gnus-info-marks info))
1228                       mark newmarks)
1229                   (while (setq mark (pop oldmarks))
1230                     (setcdr mark (gnus-remove-from-range (cdr mark)
1231                                                          (list new-number)))
1232                     (when (gnus-member-of-range old-number (cdr mark))
1233                       (setcdr mark (gnus-remove-from-range (cdr mark)
1234                                                            (list old-number)))
1235                       (setcdr mark (gnus-add-to-range (cdr mark)
1236                                                       (list new-number))))
1237                     (push mark newmarks))
1238                   (gnus-info-set-marks info newmarks))
1239                 ;; 3/ Update the NOV entry for this article:
1240                 (unless nnml-nov-is-evil
1241                   (with-current-buffer (nnml-open-nov group)
1242                     (when (nnheader-find-nov-line old-number)
1243                       ;; Replace the article number:
1244                       (looking-at old-number-string)
1245                       (replace-match new-number-string nil t)
1246                       ;; Update the Xref header:
1247                       (when (re-search-forward
1248                              (concat "\\(Xref:[^\t\n]* \\)\\<"
1249                                      (regexp-quote
1250                                       (concat group ":" old-number-string))
1251                                      "\\>")
1252                              (point-at-eol) t)
1253                         (replace-match
1254                          (concat "\\1" group ":" new-number-string))))))
1255                 ;; 4/ Possibly remove the article from the backlog:
1256                 (when gnus-keep-backlog
1257                   ;; #### NOTE: instead of removing the article, we could
1258                   ;; #### modify the backlog to reflect the numbering change,
1259                   ;; #### but I don't think it's worth it.
1260                   (gnus-backlog-remove-article group-full-name old-number)
1261                   (gnus-backlog-remove-article group-full-name new-number))))
1262             (setq new-number (1+ new-number)))))
1263       (if (not compacted)
1264           ;; No compaction had to be done:
1265           t
1266         ;; Some articles have actually been renamed:
1267         ;; 1/ Rebuild active information:
1268         (let ((entry (assoc group nnml-group-alist))
1269               (active (cons 1 (1- new-number))))
1270           (setq nnml-group-alist (delq entry nnml-group-alist))
1271           (push (list group active) nnml-group-alist)
1272           ;; Update the active hashtable to let the *Group* buffer display
1273           ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1274           ;; gnus-newwrc-alist are out of date, since all we did is to modify
1275           ;; the info of the group internally.
1276           (gnus-set-active group-full-name active))
1277         ;; 1 bis/
1278         ;; #### NOTE: normally, we should save the overview (NOV) file
1279         ;; #### here, just like we save the marks file. However, there is no
1280         ;; #### such function as nnml-save-nov for a single group. Only for
1281         ;; #### all groups. Gnus inconsistency is getting worse every day...
1282         ;; 2/ Rebuild marks file:
1283         (unless nnml-marks-is-evil
1284           ;; #### NOTE: this constant use of global variables everywhere is
1285           ;; #### truly disgusting. Gnus really needs a *major* cleanup.
1286           (setq nnml-marks (gnus-info-marks info))
1287           (push (cons 'read (gnus-info-read info)) nnml-marks)
1288           (dolist (el gnus-article-unpropagated-mark-lists)
1289             (setq nnml-marks (gnus-remassoc el nnml-marks)))
1290           (nnml-save-marks group server))
1291         ;; 3/ Save everything if this was not part of a bigger operation:
1292         (if (not save)
1293             ;; Nothing to save (yet):
1294             t
1295           ;; Something to save:
1296           ;; a/ Save the NOV databases:
1297           ;; #### NOTE: this should be done directory per directory in 1bis
1298           ;; #### above. See comment there.
1299           (nnml-save-nov)
1300           ;; b/ Save the active file:
1301           (nnmail-save-active nnml-group-alist nnml-active-file)
1302           t)))))
1303
1304 (defun nnml-request-compact (&optional server)
1305   "Request compaction of all SERVER nnml groups."
1306   (interactive (list (or (nnoo-current-server 'nnml) "")))
1307   (nnmail-activate 'nnml)
1308   (unless (nnml-server-opened server)
1309     (nnml-open-server server))
1310   (setq nnml-directory (expand-file-name nnml-directory))
1311   (let* ((groups (gnus-groups-from-server
1312                   (gnus-server-to-method (format "nnml:%s" server))))
1313          (first (pop groups))
1314          group)
1315     (when first
1316       (while (setq group (pop groups))
1317         (nnml-request-compact-group (gnus-group-real-name group) server))
1318       (nnml-request-compact-group (gnus-group-real-name first) server t))))
1319
1320
1321 (provide 'nnml)
1322
1323 ;;; nnml.el ends here