gravatar.el (gravatar-create-image): New function.
[bpt/emacs.git] / lisp / gnus / nnml.el
CommitLineData
eec82323 1;;; nnml.el --- mail spool access for Gnus
e84b4b86
TTN
2
3;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
114f9c96 4;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
eec82323 5
01c52d31
MB
6;; Authors: Didier Verna <didier@xemacs.org> (adding compaction)
7;; Simon Josefsson <simon@josefsson.org> (adding MARKS)
8;; Lars Magne Ingebrigtsen <larsi@gnus.org>
23f87bed 9;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
eec82323
LMI
10;; Keywords: news, mail
11
12;; This file is part of GNU Emacs.
13
5e809f55 14;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 15;; it under the terms of the GNU General Public License as published by
5e809f55
GM
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
eec82323
LMI
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
5e809f55 25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
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
23f87bed 35(require 'gnus)
eec82323
LMI
36(require 'nnheader)
37(require 'nnmail)
38(require 'nnoo)
349f4e97 39(eval-when-compile (require 'cl))
23f87bed 40
8abf1b22
GM
41;; FIXME first is unused in this file.
42(autoload 'gnus-article-unpropagatable-p "gnus-sum")
43(autoload 'gnus-backlog-remove-article "gnus-bcklg")
eec82323
LMI
44
45(nnoo-declare nnml)
46
47(defvoo nnml-directory message-directory
a8151ef7 48 "Spool directory for the nnml mail backend.")
eec82323
LMI
49
50(defvoo nnml-active-file
16409b0b 51 (expand-file-name "active" nnml-directory)
eec82323
LMI
52 "Mail active file.")
53
54(defvoo nnml-newsgroups-file
16409b0b 55 (expand-file-name "newsgroups" nnml-directory)
eec82323
LMI
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
23f87bed 62 "If non-nil, Gnus will never generate and use nov databases for mail spools.
eec82323
LMI
63Using nov databases will speed up header fetching considerably.
64This variable shouldn't be flipped much. If you have, for some reason,
65set this to t, and want to set it to nil again, you should always run
66the `nnml-generate-nov-databases' command. The function will go
67through all nnml directories and generate nov databases for them
68all. This may very well take some time.")
69
23f87bed
MB
70(defvoo nnml-marks-is-evil nil
71 "If non-nil, Gnus will never generate and use marks file for mail spools.
72Using marks files makes it possible to backup and restore mail groups
73separately from `.newsrc.eld'. If you have, for some reason, set this
74to t, and want to set it to nil again, you should always remove the
75corresponding marks file (usually named `.marks' in the nnml group
76directory, but see `nnml-marks-file-name') for the group. Then the
77marks file will be regenerated properly by Gnus.")
78
eec82323
LMI
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
23f87bed 85(defvoo nnml-use-compressed-files nil
01c52d31
MB
86 "If non-nil, allow using compressed message files.
87
88If it is a string, use it as the file extension which specifies
89the compression program. You can set it to \".bz2\" if your Emacs
90supports auto-compression using the bzip2 program. A value of t
91is equivalent to \".gz\".")
92
93(defvoo nnml-compressed-files-size-threshold 1000
94 "Default size threshold for compressed message files.
95Message files with bodies larger than that many characters will
96be automatically compressed if `nnml-use-compressed-files' is
97non-nil.")
eec82323
LMI
98
99\f
100
101(defconst nnml-version "nnml 1.0"
102 "nnml version.")
103
104(defvoo nnml-nov-file-name ".overview")
23f87bed 105(defvoo nnml-marks-file-name ".marks")
eec82323
LMI
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
6748645f
LMI
117(defvar nnml-nov-buffer-file-name nil)
118
16409b0b
GM
119(defvoo nnml-file-coding-system nnmail-file-coding-system)
120
23f87bed
MB
121(defvoo nnml-marks nil)
122
123(defvar nnml-marks-modtime (gnus-make-hashtable))
eec82323 124
23f87bed 125\f
eec82323
LMI
126;;; Interface functions.
127
128(nnoo-define-basics nnml)
129
01c52d31
MB
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
eec82323
LMI
161(deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
162 (when (nnml-possibly-change-directory group server)
20a673b2 163 (with-current-buffer nntp-server-buffer
eec82323 164 (erase-buffer)
23f87bed
MB
165 (let* ((file nil)
166 (number (length sequence))
167 (count 0)
168 (file-name-coding-system nnmail-pathname-coding-system)
169 beg article)
eec82323
LMI
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)
23f87bed 184 (if (re-search-forward "\n\r?\n" nil t)
eec82323
LMI
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))
16409b0b 208 (ignore-errors (make-directory nnml-directory t)))
eec82323
LMI
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
23f87bed 221(deffoo nnml-request-regenerate (server)
eec82323 222 (nnml-possibly-change-directory nil server)
23f87bed 223 (nnml-generate-nov-databases server)
eec82323
LMI
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))
16409b0b 229 (file-name-coding-system nnmail-pathname-coding-system)
eec82323
LMI
230 path gpath group-num)
231 (if (stringp id)
01c52d31 232 (when (and (setq group-num (nnml-find-group-number id server))
eec82323
LMI
233 (cdr
234 (assq (cdr group-num)
235 (nnheader-article-to-file-alist
01c52d31
MB
236 (setq gpath (nnml-group-pathname (car group-num)
237 nil server))))))
eec82323
LMI
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))
16409b0b
GM
247 ((not (save-excursion (let ((nnmail-file-coding-system
248 nnml-file-coding-system))
249 (nnmail-find-file path))))
eec82323
LMI
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)
e9bd5782 255 (string-to-number (file-name-nondirectory path)))))))
eec82323 256
286c4fc2 257(deffoo nnml-request-group (group &optional server dont-check info)
1428d46b
MB
258 (let ((file-name-coding-system nnmail-pathname-coding-system)
259 (decoded (nnml-decoded-group-name group server)))
d49d7823
KH
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
1428d46b 269 (nnheader-report 'nnml "Group %s selected" decoded)
d49d7823
KH
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)
1428d46b
MB
276 (nnheader-report 'nnml "No such group: %s" decoded)
277 (nnheader-report 'nnml "Selected group %s" decoded)
d49d7823
KH
278 (nnheader-insert "211 %d %d %d %s\n"
279 (max (1+ (- (cdr active) (car active))) 0)
280 (car active) (cdr active) group)))))))
eec82323
LMI
281
282(deffoo nnml-request-scan (&optional group server)
283 (setq nnml-article-file-alist nil)
284 (nnml-possibly-change-directory group server)
85816ac1 285 (nnmail-get-new-mail 'nnml 'nnml-save-incremental-nov nnml-directory group))
eec82323
LMI
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)
16409b0b 292 (nnml-possibly-change-directory nil server)
eec82323 293 (nnmail-activate 'nnml)
6748645f 294 (cond
01c52d31
MB
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))))
6748645f
LMI
302 ((assoc group nnml-group-alist)
303 t)
6748645f 304 (t
eec82323
LMI
305 (let (active)
306 (push (list group (setq active (cons 1 0)))
307 nnml-group-alist)
01c52d31 308 (nnml-possibly-create-directory group server)
eec82323 309 (nnml-possibly-change-directory group server)
01c52d31
MB
310 (let* ((file-name-coding-system nnmail-pathname-coding-system)
311 (articles (nnml-directory-articles nnml-current-directory)))
eec82323
LMI
312 (when articles
313 (setcar active (apply 'min articles))
314 (setcdr active (apply 'max articles))))
6748645f
LMI
315 (nnmail-save-active nnml-group-alist nnml-active-file)
316 t))))
eec82323
LMI
317
318(deffoo nnml-request-list (&optional server)
319 (save-excursion
d49d7823 320 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
16409b0b
GM
321 (file-name-coding-system nnmail-pathname-coding-system))
322 (nnmail-find-file nnml-active-file))
eec82323
LMI
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
16409b0b 333(deffoo nnml-request-expire-articles (articles group &optional server force)
eec82323 334 (nnml-possibly-change-directory group server)
01c52d31
MB
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)
eec82323
LMI
341 (nnmail-activate 'nnml)
342
6748645f
LMI
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
eec82323 348 (while (and articles is-old)
23f87bed
MB
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.
01c52d31
MB
357 (setq target nnmail-expiry-target)
358 (unless (eq target 'delete)
23f87bed
MB
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)
01c52d31
MB
364 (when (functionp target)
365 (setq target (funcall target group)))
9091f2d3
MB
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)))))
23f87bed
MB
371 ;; Maybe directory is changed during nnmail-expiry-target-group.
372 (nnml-possibly-change-directory group server))
01c52d31
MB
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)))
23f87bed 384 (push number rest)))
eec82323
LMI
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
01c52d31 395 (article group server accept-form &optional last move-is-internal)
eec82323 396 (let ((buf (get-buffer-create " *nnml move*"))
01c52d31 397 (file-name-coding-system nnmail-pathname-coding-system)
eec82323
LMI
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)
a1506d29
JB
404 (let (nnml-current-directory
405 nnml-current-group
16409b0b 406 nnml-article-file-alist)
20a673b2 407 (with-current-buffer buf
16409b0b
GM
408 (insert-buffer-substring nntp-server-buffer)
409 (setq result (eval accept-form))
410 (kill-buffer (current-buffer))
411 result))
eec82323
LMI
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
01c52d31 429 (nnmail-cache-insert (nnmail-fetch-field "message-id")
23f87bed
MB
430 group
431 (nnmail-fetch-field "subject")
432 (nnmail-fetch-field "from")))
eec82323
LMI
433 (if (stringp group)
434 (and
435 (nnmail-activate 'nnml)
436 (setq result (car (nnml-save-mail
01c52d31
MB
437 (list (cons group (nnml-active-number group
438 server)))
85816ac1 439 server t)))
eec82323
LMI
440 (progn
441 (nnmail-save-active nnml-group-alist nnml-active-file)
442 (and last (nnml-save-nov))))
443 (and
444 (nnmail-activate 'nnml)
01c52d31
MB
445 (if (and (not (setq result (nnmail-article-group
446 `(lambda (group)
447 (nnml-active-number group ,server)))))
eec82323
LMI
448 (yes-or-no-p "Moved to `junk' group; delete article? "))
449 (setq result 'junk)
85816ac1 450 (setq result (car (nnml-save-mail result server t))))
eec82323
LMI
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
23f87bed
MB
458(deffoo nnml-request-post (&optional server)
459 (nnmail-do-request-post 'nnml-request-accept-article server))
460
eec82323
LMI
461(deffoo nnml-request-replace-article (article group buffer)
462 (nnml-possibly-change-directory group)
20a673b2 463 (with-current-buffer buffer
eec82323
LMI
464 (nnml-possibly-create-directory group)
465 (let ((chars (nnmail-insert-lines))
466 (art (concat (int-to-string article) "\t"))
467 headers)
16409b0b
GM
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)
eec82323
LMI
476 (setq headers (nnml-parse-head chars article))
477 ;; Replace the NOV line in the NOV file.
20a673b2 478 (with-current-buffer (nnml-open-nov group)
eec82323
LMI
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.
23f87bed 483 (gnus-delete-line)
eec82323
LMI
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")
e9bd5782 488 (< (string-to-number
eec82323
LMI
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)
01c52d31
MB
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))
eec82323
LMI
532 t)
533
534(deffoo nnml-request-rename-group (group new-name &optional server)
535 (nnml-possibly-change-directory group server)
01c52d31 536 (let ((new-dir (nnml-group-pathname new-name nil server))
4d8a28ec
MB
537 (old-dir (nnml-group-pathname group nil server))
538 (file-name-coding-system nnmail-pathname-coding-system))
16409b0b
GM
539 (when (ignore-errors
540 (make-directory new-dir t)
541 t)
eec82323
LMI
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.
01c52d31
MB
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))))
eec82323
LMI
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))))
23f87bed
MB
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))))
eec82323 557 (when (<= (length (directory-files old-dir)) 2)
16409b0b 558 (ignore-errors (delete-directory old-dir)))
eec82323
LMI
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
16409b0b 576 (with-temp-file file
eec82323
LMI
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)
23f87bed
MB
587 (if (setq file
588 (if nnml-use-compressed-files
589 (cdr (assq article nnml-article-file-alist))
590 (number-to-string article)))
16409b0b 591 (expand-file-name file nnml-current-directory)
23f87bed
MB
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)))))
eec82323
LMI
600
601(defun nnml-deletable-article-p (group article)
602 "Say whether ARTICLE in GROUP can be deleted."
01c52d31
MB
603 (let ((file-name-coding-system nnmail-pathname-coding-system)
604 path)
eec82323
LMI
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.
01c52d31 612(defun nnml-find-group-number (id server)
20a673b2 613 (with-current-buffer (get-buffer-create " *nnml id*")
eec82323
LMI
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.
01c52d31 619 (if (setq number (nnml-find-id nnml-current-group id server))
eec82323 620 (cons nnml-current-group number)
23f87bed 621 ;; It wasn't there, so we look through the other groups as well.
eec82323
LMI
622 (while (and (not number)
623 alist)
624 (or (string= (caar alist) nnml-current-group)
01c52d31 625 (setq number (nnml-find-id (caar alist) id server)))
eec82323
LMI
626 (or number
627 (setq alist (cdr alist))))
628 (and number
629 (cons (caar alist) number))))))
630
01c52d31 631(defun nnml-find-id (group id server)
eec82323 632 (erase-buffer)
01c52d31 633 (let ((nov (nnml-group-pathname group nnml-nov-file-name server))
eec82323
LMI
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)
01c52d31 641 (not (search-backward "\t" (point-at-bol) t))))
eec82323
LMI
642 (forward-line 1)
643 (beginning-of-line)
644 (setq found t)
645 ;; We return the article number.
646 (setq number
16409b0b 647 (ignore-errors (read (current-buffer))))))
eec82323
LMI
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
16409b0b 653 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
eec82323 654 (when (file-exists-p nov)
20a673b2 655 (with-current-buffer nntp-server-buffer
eec82323
LMI
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
01c52d31 673 (let ((pathname (nnml-group-pathname group nil server))
16409b0b 674 (file-name-coding-system nnmail-pathname-coding-system))
eec82323
LMI
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
01c52d31
MB
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))
16409b0b
GM
684 (unless (file-exists-p dir)
685 (make-directory (directory-file-name dir) t)
686 (nnheader-message 5 "Creating mail directory %s" dir))))
eec82323 687
85816ac1 688(defun nnml-save-mail (group-art &optional server full-nov)
01c52d31
MB
689 "Save a mail into the groups GROUP-ART in the nnml server SERVER.
690GROUP-ART is a list that each element is a cons of a group name and an
691article 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))
eec82323
LMI
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: ")
713 (forward-line 1))
714 ;; We save the article in all the groups it belongs in.
01c52d31
MB
715 (dolist (ga group-art)
716 (if nnmail-group-names-not-encoded-p
717 (progn
718 (nnml-possibly-create-directory (car decoded) server)
719 (setq file (nnmail-group-pathname
720 (pop decoded) nnml-directory
721 (concat (number-to-string (cdr ga)) extension))))
722 (nnml-possibly-create-directory (car ga) server)
723 (setq file (nnml-group-pathname
724 (car ga) (concat (number-to-string (cdr ga)) extension)
725 server)))
726 (if first
727 ;; It was already saved, so we just make a hard link.
728 (let ((file-name-coding-system nnmail-pathname-coding-system))
729 (funcall nnmail-crosspost-link-function first file t))
730 ;; Save the article.
731 (nnmail-write-region (point-min) (point-max) file nil
732 (if (nnheader-be-verbose 5) nil 'nomesg))
733 (setq first file)))
eec82323
LMI
734 ;; Generate a nov line for this article. We generate the nov
735 ;; line after saving, because nov generation destroys the
736 ;; header.
737 (setq headers (nnml-parse-head chars))
738 ;; Output the nov line to all nov databases that should have it.
85816ac1
LMI
739 (let ((func (if full-nov
740 'nnml-add-nov
741 'nnml-add-incremental-nov)))
742 (if nnmail-group-names-not-encoded-p
743 (dolist (ga group-art)
744 (funcall func (pop dec) (cdr ga) headers))
01c52d31 745 (dolist (ga group-art)
85816ac1 746 (funcall func (car ga) (cdr ga) headers)))))
01c52d31
MB
747 group-art)
748
749(defun nnml-active-number (group &optional server)
750 "Compute the next article number in GROUP on SERVER."
e9d1aaff
KY
751 (let* ((encoded (if nnmail-group-names-not-encoded-p
752 (nnml-encoded-group-name group server)))
753 (active (cadr (assoc (or encoded group) nnml-group-alist))))
eec82323
LMI
754 ;; The group wasn't known to nnml, so we just create an active
755 ;; entry for it.
756 (unless active
757 ;; Perhaps the active file was corrupt? See whether
758 ;; there are any articles in this group.
01c52d31
MB
759 (nnml-possibly-create-directory group server)
760 (nnml-possibly-change-directory group server)
eec82323
LMI
761 (unless nnml-article-file-alist
762 (setq nnml-article-file-alist
763 (sort
23f87bed 764 (nnml-current-group-article-to-file-alist)
6748645f 765 'car-less-than-car)))
eec82323
LMI
766 (setq active
767 (if nnml-article-file-alist
768 (cons (caar nnml-article-file-alist)
769 (caar (last nnml-article-file-alist)))
770 (cons 1 0)))
e9d1aaff 771 (push (list (or encoded group) active) nnml-group-alist))
eec82323
LMI
772 (setcdr active (1+ (cdr active)))
773 (while (file-exists-p
01c52d31 774 (nnml-group-pathname group (int-to-string (cdr active)) server))
eec82323
LMI
775 (setcdr active (1+ (cdr active))))
776 (cdr active)))
777
85816ac1
LMI
778(defvar nnml-incremental-nov-buffer-alist nil)
779
780(defun nnml-save-incremental-nov ()
85816ac1
LMI
781 (save-excursion
782 (while nnml-incremental-nov-buffer-alist
783 (when (buffer-name (cdar nnml-incremental-nov-buffer-alist))
784 (set-buffer (cdar nnml-incremental-nov-buffer-alist))
785 (when (buffer-modified-p)
786 (nnmail-write-region (point-min) (point-max)
787 nnml-nov-buffer-file-name t 'nomesg))
788 (set-buffer-modified-p nil)
789 (kill-buffer (current-buffer)))
790 (setq nnml-incremental-nov-buffer-alist
5edc8186 791 (cdr nnml-incremental-nov-buffer-alist)))))
85816ac1
LMI
792
793(defun nnml-open-incremental-nov (group)
794 (or (cdr (assoc group nnml-incremental-nov-buffer-alist))
795 (let ((buffer (nnml-get-nov-buffer group t)))
796 (push (cons group buffer) nnml-incremental-nov-buffer-alist)
797 buffer)))
798
799(defun nnml-add-incremental-nov (group article headers)
800 "Add a nov line for the GROUP nov headers, incrementally."
20a673b2 801 (with-current-buffer (nnml-open-incremental-nov group)
85816ac1
LMI
802 (goto-char (point-max))
803 (mail-header-set-number headers article)
804 (nnheader-insert-nov headers)))
805
eec82323
LMI
806(defun nnml-add-nov (group article headers)
807 "Add a nov line for the GROUP base."
20a673b2 808 (with-current-buffer (nnml-open-nov group)
eec82323
LMI
809 (goto-char (point-max))
810 (mail-header-set-number headers article)
811 (nnheader-insert-nov headers)))
812
813(defsubst nnml-header-value ()
01c52d31 814 (buffer-substring (match-end 0) (point-at-eol)))
eec82323
LMI
815
816(defun nnml-parse-head (chars &optional number)
817 "Parse the head of the current buffer."
818 (save-excursion
819 (save-restriction
6748645f
LMI
820 (unless (zerop (buffer-size))
821 (narrow-to-region
822 (goto-char (point-min))
23f87bed
MB
823 (if (re-search-forward "\n\r?\n" nil t)
824 (1- (point))
825 (point-max))))
826 (let ((headers (nnheader-parse-naked-head)))
eec82323
LMI
827 (mail-header-set-chars headers chars)
828 (mail-header-set-number headers number)
829 headers))))
830
85816ac1 831(defun nnml-get-nov-buffer (group &optional incrementalp)
01c52d31 832 (let* ((decoded (nnml-decoded-group-name group))
85816ac1
LMI
833 (buffer (get-buffer-create (format " *nnml %soverview %s*"
834 (if incrementalp
835 "incremental "
836 "")
837 decoded)))
01c52d31 838 (file-name-coding-system nnmail-pathname-coding-system))
20a673b2 839 (with-current-buffer buffer
23f87bed 840 (set (make-local-variable 'nnml-nov-buffer-file-name)
01c52d31 841 (nnmail-group-pathname decoded nnml-directory nnml-nov-file-name))
23f87bed 842 (erase-buffer)
85816ac1
LMI
843 (when (and (not incrementalp)
844 (file-exists-p nnml-nov-buffer-file-name))
23f87bed
MB
845 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
846 buffer))
847
eec82323 848(defun nnml-open-nov (group)
1821a7b4
LMI
849 (or (let ((buffer (cdr (assoc group nnml-nov-buffer-alist))))
850 (and (buffer-name buffer)
851 buffer))
23f87bed 852 (let ((buffer (nnml-get-nov-buffer group)))
eec82323
LMI
853 (push (cons group buffer) nnml-nov-buffer-alist)
854 buffer)))
855
856(defun nnml-save-nov ()
857 (save-excursion
858 (while nnml-nov-buffer-alist
859 (when (buffer-name (cdar nnml-nov-buffer-alist))
860 (set-buffer (cdar nnml-nov-buffer-alist))
861 (when (buffer-modified-p)
9930767b
SM
862 (nnmail-write-region (point-min) (point-max)
863 nnml-nov-buffer-file-name nil 'nomesg))
eec82323
LMI
864 (set-buffer-modified-p nil)
865 (kill-buffer (current-buffer)))
5edc8186 866 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
eec82323
LMI
867
868;;;###autoload
23f87bed 869(defun nnml-generate-nov-databases (&optional server)
eec82323 870 "Generate NOV databases in all nnml directories."
23f87bed 871 (interactive (list (or (nnoo-current-server 'nnml) "")))
eec82323
LMI
872 ;; Read the active file to make sure we don't re-use articles
873 ;; numbers in empty groups.
874 (nnmail-activate 'nnml)
23f87bed
MB
875 (unless (nnml-server-opened server)
876 (nnml-open-server server))
eec82323
LMI
877 (setq nnml-directory (expand-file-name nnml-directory))
878 ;; Recurse down the directories.
01c52d31 879 (nnml-generate-nov-databases-directory nnml-directory nil t)
eec82323
LMI
880 ;; Save the active file.
881 (nnmail-save-active nnml-group-alist nnml-active-file))
882
149caf54 883(defvar nnml-files)
01c52d31
MB
884(defun nnml-generate-nov-databases-directory (dir &optional seen no-active)
885 "Regenerate the NOV database in DIR.
886
887Unless no-active is non-nil, update the active file too."
888 (interactive (list (let ((file-name-coding-system
889 nnmail-pathname-coding-system))
890 (read-directory-name "Regenerate NOV in: "
891 nnml-directory nil t))))
eec82323 892 (setq dir (file-name-as-directory dir))
01c52d31
MB
893 (let ((file-name-coding-system nnmail-pathname-coding-system))
894 ;; Only scan this sub-tree if we haven't been here yet.
895 (unless (member (file-truename dir) seen)
896 (push (file-truename dir) seen)
897 ;; We descend recursively
898 (dolist (dir (directory-files dir t nil t))
16409b0b 899 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
eec82323 900 (file-directory-p dir))
01c52d31
MB
901 (nnml-generate-nov-databases-directory dir seen)))
902 ;; Do this directory.
149caf54 903 (let ((nnml-files (sort (nnheader-article-to-file-alist dir)
01c52d31 904 'car-less-than-car)))
149caf54 905 (if (not nnml-files)
01c52d31
MB
906 (let* ((group (nnheader-file-to-group
907 (directory-file-name dir) nnml-directory))
908 (info (cadr (assoc group nnml-group-alist))))
909 (when info
910 (setcar info (1+ (cdr info)))))
911 (funcall nnml-generate-active-function dir)
912 ;; Generate the nov file.
149caf54 913 (nnml-generate-nov-file dir nnml-files)
01c52d31
MB
914 (unless no-active
915 (nnmail-save-active nnml-group-alist nnml-active-file)))))))
eec82323 916
eec82323
LMI
917(defun nnml-generate-active-info (dir)
918 ;; Update the active info for this group.
01c52d31
MB
919 (let ((group (directory-file-name dir))
920 entry last)
921 (setq group (nnheader-file-to-group (nnml-encoded-group-name group)
922 nnml-directory)
923 entry (assoc group nnml-group-alist)
924 last (or (caadr entry) 0)
925 nnml-group-alist (delq entry nnml-group-alist))
eec82323 926 (push (list group
149caf54 927 (cons (or (caar nnml-files) (1+ last))
23f87bed 928 (max last
149caf54 929 (or (caar (last nnml-files))
23f87bed 930 0))))
eec82323
LMI
931 nnml-group-alist)))
932
933(defun nnml-generate-nov-file (dir files)
934 (let* ((dir (file-name-as-directory dir))
935 (nov (concat dir nnml-nov-file-name))
936 (nov-buffer (get-buffer-create " *nov*"))
937 chars file headers)
20a673b2 938 (with-current-buffer nov-buffer
eec82323 939 ;; Init the nov buffer.
16409b0b 940 (buffer-disable-undo)
eec82323
LMI
941 (erase-buffer)
942 (set-buffer nntp-server-buffer)
943 ;; Delete the old NOV file.
944 (when (file-exists-p nov)
945 (funcall nnmail-delete-file-function nov))
149caf54 946 (dolist (file files)
0617bb00
LMI
947 (let ((path (concat dir (cdr file))))
948 (unless (file-directory-p path)
949 (erase-buffer)
950 (nnheader-insert-file-contents path)
951 (narrow-to-region
952 (goto-char (point-min))
953 (progn
954 (re-search-forward "\n\r?\n" nil t)
955 (setq chars (- (point-max) (point)))
956 (max (point-min) (1- (point)))))
957 (unless (zerop (buffer-size))
958 (goto-char (point-min))
959 (setq headers (nnml-parse-head chars (car file)))
960 (with-current-buffer nov-buffer
961 (goto-char (point-max))
962 (nnheader-insert-nov headers)))
963 (widen))))
20a673b2 964 (with-current-buffer nov-buffer
9930767b 965 (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
eec82323
LMI
966 (kill-buffer (current-buffer))))))
967
968(defun nnml-nov-delete-article (group article)
20a673b2 969 (with-current-buffer (nnml-open-nov group)
eec82323
LMI
970 (when (nnheader-find-nov-line article)
971 (delete-region (point) (progn (forward-line 1) (point)))
972 (when (bobp)
973 (let ((active (cadr (assoc group nnml-group-alist)))
974 num)
975 (when active
976 (if (eobp)
977 (setf (car active) (1+ (cdr active)))
978 (when (and (setq num (ignore-errors (read (current-buffer))))
979 (numberp num))
980 (setf (car active) num)))))))
981 t))
982
a8151ef7 983(defun nnml-update-file-alist (&optional force)
23f87bed
MB
984 (when nnml-use-compressed-files
985 (when (or (not nnml-article-file-alist)
986 force)
987 (setq nnml-article-file-alist
988 (nnml-current-group-article-to-file-alist)))))
989
990(defun nnml-directory-articles (dir)
991 "Return a list of all article files in a directory.
992Use the nov database for that directory if available."
993 (if (or gnus-nov-is-evil nnml-nov-is-evil
994 (not (file-exists-p
995 (expand-file-name nnml-nov-file-name dir))))
996 (nnheader-directory-articles dir)
997 ;; build list from .overview if available
998 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
999 ;; defvoo'd, and we might get called when it hasn't been swapped in.
149caf54 1000 (with-current-buffer (nnml-get-nov-buffer nnml-current-group)
23f87bed 1001 (let ((list nil)
149caf54 1002 art)
23f87bed
MB
1003 (goto-char (point-min))
1004 (while (not (eobp))
1005 (setq art (read (current-buffer)))
1006 (push art list)
1007 (forward-line 1))
1008 list))))
1009
1010(defun nnml-current-group-article-to-file-alist ()
1011 "Return an alist of article/file pairs in the current group.
1012Use the nov database for the current group if available."
1013 (if (or nnml-use-compressed-files
1014 gnus-nov-is-evil
1015 nnml-nov-is-evil
1016 (not (file-exists-p
1017 (expand-file-name nnml-nov-file-name
1018 nnml-current-directory))))
1019 (nnheader-article-to-file-alist nnml-current-directory)
1020 ;; build list from .overview if available
149caf54 1021 (with-current-buffer (nnml-get-nov-buffer nnml-current-group)
23f87bed 1022 (let ((alist nil)
23f87bed 1023 art)
23f87bed
MB
1024 (goto-char (point-min))
1025 (while (not (eobp))
1026 (setq art (read (current-buffer)))
1027 ;; assume file name is unadorned (ie. not compressed etc)
1028 (push (cons art (int-to-string art)) alist)
1029 (forward-line 1))
1030 alist))))
1031
1032(deffoo nnml-request-set-mark (group actions &optional server)
1033 (nnml-possibly-change-directory group server)
1034 (unless nnml-marks-is-evil
1035 (nnml-open-marks group server)
1036 (dolist (action actions)
1037 (let ((range (nth 0 action))
1038 (what (nth 1 action))
1039 (marks (nth 2 action)))
c1d7d285 1040 (assert (or (eq what 'add) (eq what 'del)) nil
23f87bed
MB
1041 "Unknown request-set-mark action: %s" what)
1042 (dolist (mark marks)
1043 (setq nnml-marks (gnus-update-alist-soft
1044 mark
1045 (funcall (if (eq what 'add) 'gnus-range-add
1046 'gnus-remove-from-range)
1047 (cdr (assoc mark nnml-marks)) range)
1048 nnml-marks)))))
1049 (nnml-save-marks group server))
1050 nil)
1051
b1ae92ba 1052(deffoo nnml-request-marks (group info &optional server)
23f87bed 1053 (nnml-possibly-change-directory group server)
01c52d31 1054 (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group server))
23f87bed
MB
1055 (nnheader-message 8 "Updating marks for %s..." group)
1056 (nnml-open-marks group server)
1057 ;; Update info using `nnml-marks'.
01c52d31
MB
1058 (mapc (lambda (pred)
1059 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1060 (gnus-info-set-marks
1061 info
1062 (gnus-update-alist-soft
1063 (cdr pred)
1064 (cdr (assq (cdr pred) nnml-marks))
1065 (gnus-info-marks info))
1066 t)))
1067 gnus-article-mark-lists)
23f87bed
MB
1068 (let ((seen (cdr (assq 'read nnml-marks))))
1069 (gnus-info-set-read info
1070 (if (and (integerp (car seen))
1071 (null (cdr seen)))
1072 (list (cons (car seen) (car seen)))
1073 seen)))
1074 (nnheader-message 8 "Updating marks for %s...done" group))
1075 info)
1076
01c52d31
MB
1077(defun nnml-marks-changed-p (group server)
1078 (let ((file (nnml-group-pathname group nnml-marks-file-name server)))
23f87bed
MB
1079 (if (null (gnus-gethash file nnml-marks-modtime))
1080 t ;; never looked at marks file, assume it has changed
1081 (not (equal (gnus-gethash file nnml-marks-modtime)
1082 (nth 5 (file-attributes file)))))))
1083
1084(defun nnml-save-marks (group server)
1085 (let ((file-name-coding-system nnmail-pathname-coding-system)
01c52d31 1086 (file (nnml-group-pathname group nnml-marks-file-name server)))
23f87bed
MB
1087 (condition-case err
1088 (progn
01c52d31 1089 (nnml-possibly-create-directory group server)
23f87bed
MB
1090 (with-temp-file file
1091 (erase-buffer)
1092 (gnus-prin1 nnml-marks)
1093 (insert "\n"))
1094 (gnus-sethash file
1095 (nth 5 (file-attributes file))
1096 nnml-marks-modtime))
1097 (error (or (gnus-yes-or-no-p
1098 (format "Could not write to %s (%s). Continue? " file err))
5145dbc5 1099 (error "Cannot write to %s (%s)" file err))))))
23f87bed
MB
1100
1101(defun nnml-open-marks (group server)
01c52d31
MB
1102 (let* ((decoded (nnml-decoded-group-name group server))
1103 (file (nnmail-group-pathname decoded nnml-directory
1104 nnml-marks-file-name))
1105 (file-name-coding-system nnmail-pathname-coding-system))
23f87bed
MB
1106 (if (file-exists-p file)
1107 (condition-case err
1108 (with-temp-buffer
1109 (gnus-sethash file (nth 5 (file-attributes file))
1110 nnml-marks-modtime)
1111 (nnheader-insert-file-contents file)
1112 (setq nnml-marks (read (current-buffer)))
1113 (dolist (el gnus-article-unpropagated-mark-lists)
1114 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1115 (error (or (gnus-yes-or-no-p
1116 (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1117 (error "Cannot read nnml marks file %s (%s)" file err))))
1118 ;; User didn't have a .marks file. Probably first time
1119 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1120 (let ((info (gnus-get-info
1121 (gnus-group-prefixed-name
1122 group
01c52d31
MB
1123 (gnus-server-to-method
1124 (format "nnml:%s" (or server "")))))))
1125 (setq decoded (if (member server '(nil ""))
1126 (concat "nnml:" decoded)
1127 (format "nnml+%s:%s" server decoded)))
1128 (nnheader-message 7 "Bootstrapping marks for %s..." decoded)
23f87bed
MB
1129 (setq nnml-marks (gnus-info-marks info))
1130 (push (cons 'read (gnus-info-read info)) nnml-marks)
1131 (dolist (el gnus-article-unpropagated-mark-lists)
1132 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1133 (nnml-save-marks group server)
01c52d31
MB
1134 (nnheader-message 7 "Bootstrapping marks for %s...done" decoded)))))
1135
1136
1137;;;
1138;;; Group and server compaction. -- dvl
1139;;;
1140
1141;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1142;; #### know how to handle external cross-references. I actually don't know if
1143;; #### this is handled correctly elsewhere. For instance, what happens if you
1144;; #### move all articles to a new group (that's what people do for manual
1145;; #### compaction) ?
1146
1147;; #### NOTE: the function below handles the article backlog. This is
1148;; #### conceptually the wrong place to do it because the backend is at a
1149;; #### lower level. However, this is the only place where we have the needed
1150;; #### information to do the job. Ideally, this function should not handle
1151;; #### the backlog by itself, but return a list of moved groups / articles to
1152;; #### the caller. This will become important to avoid code duplication when
1153;; #### other backends get a compaction feature. Also, note that invalidating
1154;; #### the "original article buffer" is already done at an upper level.
1155
1156;; Shouldn't `nnml-request-compact-group' be interactive? --rsteib
1157
1158(defun nnml-request-compact-group (group &optional server save)
1159 (nnml-possibly-change-directory group server)
1160 (unless nnml-article-file-alist
1161 (setq nnml-article-file-alist
1162 (sort (nnml-current-group-article-to-file-alist)
1163 'car-less-than-car)))
1164 (if (not nnml-article-file-alist)
1165 ;; The group is empty: do nothing but return t
1166 t
1167 ;; The group is not empty:
1168 (let* ((group-full-name
1169 (gnus-group-prefixed-name
1170 group
1171 (gnus-server-to-method (format "nnml:%s" server))))
1172 (info (gnus-get-info group-full-name))
1173 (new-number 1)
1174 compacted)
1175 (let ((articles nnml-article-file-alist)
1176 article)
1177 (while (setq article (pop articles))
1178 (let ((old-number (car article)))
1179 (when (> old-number new-number)
1180 ;; There is a gap here:
1181 (let ((old-number-string (int-to-string old-number))
1182 (new-number-string (int-to-string new-number)))
1183 (setq compacted t)
1184 ;; #### NOTE: `nnml-article-to-file' calls
1185 ;; #### `nnml-update-file-alist' (which in turn calls
1186 ;; #### `nnml-current-group-article-to-file-alist', which
1187 ;; #### might use the NOV database). This might turn out to be
1188 ;; #### inefficient. In that case, we will do the work
1189 ;; #### manually.
1190 ;; 1/ Move the article to a new file:
1191 (let* ((oldfile (nnml-article-to-file old-number))
1192 (newfile
1193 (gnus-replace-in-string
1194 oldfile
1195 ;; nnml-use-compressed-files might be any string, but
1196 ;; probably it's sufficient to take into account only
1197 ;; "\\.[a-z0-9]+". Note that we can't only use the
1198 ;; value of nnml-use-compressed-files because old
1199 ;; articles might have been saved with a different
1200 ;; value.
1201 (concat
1202 "\\(" old-number-string "\\)\\(\\(\\.[a-z0-9]+\\)?\\)$")
1203 (concat new-number-string "\\2"))))
1204 (with-current-buffer nntp-server-buffer
1205 (nnmail-find-file oldfile)
1206 ;; Update the Xref header in the article itself:
1207 (when (and (re-search-forward "^Xref: [^ ]+ " nil t)
1208 (re-search-forward
1209 (concat "\\<"
1210 (regexp-quote
1211 (concat group ":" old-number-string))
1212 "\\>")
1213 (point-at-eol) t))
1214 (replace-match
1215 (concat group ":" new-number-string)))
1216 ;; Save to the new file:
1217 (nnmail-write-region (point-min) (point-max) newfile))
1218 (funcall nnmail-delete-file-function oldfile))
1219 ;; 2/ Update all marks for this article:
1220 ;; #### NOTE: it is possible that the new article number
1221 ;; #### already belongs to a range, whereas the corresponding
1222 ;; #### article doesn't exist (for example, if you delete an
1223 ;; #### article). For that reason, it is important to update
5a89f0a7 1224 ;; #### the ranges (meaning remove inexistent articles) before
01c52d31
MB
1225 ;; #### doing anything on them.
1226 ;; 2 a/ read articles:
1227 (let ((read (gnus-info-read info)))
1228 (setq read (gnus-remove-from-range read (list new-number)))
1229 (when (gnus-member-of-range old-number read)
1230 (setq read (gnus-remove-from-range read (list old-number)))
1231 (setq read (gnus-add-to-range read (list new-number))))
1232 (gnus-info-set-read info read))
1233 ;; 2 b/ marked articles:
1234 (let ((oldmarks (gnus-info-marks info))
1235 mark newmarks)
1236 (while (setq mark (pop oldmarks))
1237 (setcdr mark (gnus-remove-from-range (cdr mark)
1238 (list new-number)))
1239 (when (gnus-member-of-range old-number (cdr mark))
1240 (setcdr mark (gnus-remove-from-range (cdr mark)
1241 (list old-number)))
1242 (setcdr mark (gnus-add-to-range (cdr mark)
1243 (list new-number))))
1244 (push mark newmarks))
1245 (gnus-info-set-marks info newmarks))
1246 ;; 3/ Update the NOV entry for this article:
1247 (unless nnml-nov-is-evil
20a673b2 1248 (with-current-buffer (nnml-open-nov group)
01c52d31
MB
1249 (when (nnheader-find-nov-line old-number)
1250 ;; Replace the article number:
1251 (looking-at old-number-string)
1252 (replace-match new-number-string nil t)
1253 ;; Update the Xref header:
1254 (when (re-search-forward
1255 (concat "\\(Xref:[^\t\n]* \\)\\<"
1256 (regexp-quote
1257 (concat group ":" old-number-string))
1258 "\\>")
1259 (point-at-eol) t)
1260 (replace-match
1261 (concat "\\1" group ":" new-number-string))))))
1262 ;; 4/ Possibly remove the article from the backlog:
1263 (when gnus-keep-backlog
1264 ;; #### NOTE: instead of removing the article, we could
1265 ;; #### modify the backlog to reflect the numbering change,
1266 ;; #### but I don't think it's worth it.
1267 (gnus-backlog-remove-article group-full-name old-number)
1268 (gnus-backlog-remove-article group-full-name new-number))))
1269 (setq new-number (1+ new-number)))))
1270 (if (not compacted)
1271 ;; No compaction had to be done:
1272 t
1273 ;; Some articles have actually been renamed:
1274 ;; 1/ Rebuild active information:
1275 (let ((entry (assoc group nnml-group-alist))
1276 (active (cons 1 (1- new-number))))
1277 (setq nnml-group-alist (delq entry nnml-group-alist))
1278 (push (list group active) nnml-group-alist)
1279 ;; Update the active hashtable to let the *Group* buffer display
1280 ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1281 ;; gnus-newwrc-alist are out of date, since all we did is to modify
1282 ;; the info of the group internally.
1283 (gnus-set-active group-full-name active))
1284 ;; 1 bis/
1285 ;; #### NOTE: normally, we should save the overview (NOV) file
1286 ;; #### here, just like we save the marks file. However, there is no
1287 ;; #### such function as nnml-save-nov for a single group. Only for
1288 ;; #### all groups. Gnus inconsistency is getting worse every day...
1289 ;; 2/ Rebuild marks file:
1290 (unless nnml-marks-is-evil
1291 ;; #### NOTE: this constant use of global variables everywhere is
1292 ;; #### truly disgusting. Gnus really needs a *major* cleanup.
1293 (setq nnml-marks (gnus-info-marks info))
1294 (push (cons 'read (gnus-info-read info)) nnml-marks)
1295 (dolist (el gnus-article-unpropagated-mark-lists)
1296 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1297 (nnml-save-marks group server))
1298 ;; 3/ Save everything if this was not part of a bigger operation:
1299 (if (not save)
1300 ;; Nothing to save (yet):
1301 t
1302 ;; Something to save:
1303 ;; a/ Save the NOV databases:
1304 ;; #### NOTE: this should be done directory per directory in 1bis
1305 ;; #### above. See comment there.
1306 (nnml-save-nov)
1307 ;; b/ Save the active file:
1308 (nnmail-save-active nnml-group-alist nnml-active-file)
1309 t)))))
1310
1311(defun nnml-request-compact (&optional server)
1312 "Request compaction of all SERVER nnml groups."
1313 (interactive (list (or (nnoo-current-server 'nnml) "")))
1314 (nnmail-activate 'nnml)
1315 (unless (nnml-server-opened server)
1316 (nnml-open-server server))
1317 (setq nnml-directory (expand-file-name nnml-directory))
1318 (let* ((groups (gnus-groups-from-server
1319 (gnus-server-to-method (format "nnml:%s" server))))
1320 (first (pop groups))
1321 group)
1322 (when first
1323 (while (setq group (pop groups))
1324 (nnml-request-compact-group (gnus-group-real-name group) server))
1325 (nnml-request-compact-group (gnus-group-real-name first) server t))))
1326
eec82323
LMI
1327
1328(provide 'nnml)
1329
1330;;; nnml.el ends here