Patch by Ed L. Cashin to make gnus-move-split-methods move to
[gnus] / lisp / nnmbox.el
1 ;;; nnmbox.el --- mail mbox access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
17 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
19
20 ;;; Commentary:
21
22 ;; For an overview of what the interface functions do, please see the
23 ;; Gnus sources.
24
25 ;;; Code:
26
27 (require 'nnheader)
28 (require 'message)
29 (require 'nnmail)
30 (require 'nnoo)
31 (eval-when-compile (require 'cl))
32
33 (nnoo-declare nnmbox)
34
35 (defvoo nnmbox-mbox-file (expand-file-name "~/mbox")
36   "The name of the mail box file in the user's home directory.")
37
38 (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
39   "The name of the active file for the mail box.")
40
41 (defvoo nnmbox-get-new-mail t
42   "If non-nil, nnmbox will check the incoming mail file and split the mail.")
43
44 (defvoo nnmbox-prepare-save-mail-hook nil
45   "Hook run narrowed to an article before saving.")
46
47 \f
48
49 (defconst nnmbox-version "nnmbox 1.0"
50   "nnmbox version.")
51
52 (defvoo nnmbox-current-group nil
53   "Current nnmbox news group directory.")
54
55 (defconst nnmbox-mbox-buffer nil)
56
57 (defvoo nnmbox-status-string "")
58
59 (defvoo nnmbox-group-alist nil)
60 (defvoo nnmbox-active-timestamp nil)
61
62 (defvoo nnmbox-file-coding-system mm-text-coding-system)
63 (defvoo nnmbox-file-coding-system-for-write nil)
64 (defvoo nnmbox-active-file-coding-system mm-text-coding-system)
65 (defvoo nnmbox-active-file-coding-system-for-write nil)
66
67 \f
68
69 ;;; Interface functions
70
71 (nnoo-define-basics nnmbox)
72
73 (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
74   (save-excursion
75     (set-buffer nntp-server-buffer)
76     (erase-buffer)
77     (let ((number (length sequence))
78           (count 0)
79           article art-string start stop)
80       (nnmbox-possibly-change-newsgroup newsgroup server)
81       (while sequence
82         (setq article (car sequence))
83         (setq art-string (nnmbox-article-string article))
84         (set-buffer nnmbox-mbox-buffer)
85         (when (or (search-forward art-string nil t)
86                   (progn (goto-char (point-min))
87                          (search-forward art-string nil t)))
88           (setq start
89                 (save-excursion
90                   (re-search-backward
91                    (concat "^" message-unix-mail-delimiter) nil t)
92                   (point)))
93           (search-forward "\n\n" nil t)
94           (setq stop (1- (point)))
95           (set-buffer nntp-server-buffer)
96           (insert (format "221 %d Article retrieved.\n" article))
97           (insert-buffer-substring nnmbox-mbox-buffer start stop)
98           (goto-char (point-max))
99           (insert ".\n"))
100         (setq sequence (cdr sequence))
101         (setq count (1+ count))
102         (and (numberp nnmail-large-newsgroup)
103              (> number nnmail-large-newsgroup)
104              (zerop (% count 20))
105              (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
106                                (/ (* count 100) number))))
107
108       (and (numberp nnmail-large-newsgroup)
109            (> number nnmail-large-newsgroup)
110            (nnheader-message 5 "nnmbox: Receiving headers...done"))
111
112       (set-buffer nntp-server-buffer)
113       (nnheader-fold-continuation-lines)
114       'headers)))
115
116 (deffoo nnmbox-open-server (server &optional defs)
117   (nnoo-change-server 'nnmbox server defs)
118   (nnmbox-create-mbox)
119   (cond
120    ((not (file-exists-p nnmbox-mbox-file))
121     (nnmbox-close-server)
122     (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
123    ((file-directory-p nnmbox-mbox-file)
124     (nnmbox-close-server)
125     (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
126    (t
127     (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
128                      nnmbox-mbox-file)
129     t)))
130
131 (deffoo nnmbox-close-server (&optional server)
132   (when (and nnmbox-mbox-buffer
133              (buffer-name nnmbox-mbox-buffer))
134     (kill-buffer nnmbox-mbox-buffer))
135   (nnoo-close-server 'nnmbox server)
136   t)
137
138 (deffoo nnmbox-server-opened (&optional server)
139   (and (nnoo-current-server-p 'nnmbox server)
140        nnmbox-mbox-buffer
141        (buffer-name nnmbox-mbox-buffer)
142        nntp-server-buffer
143        (buffer-name nntp-server-buffer)))
144
145 (deffoo nnmbox-request-article (article &optional newsgroup server buffer)
146   (nnmbox-possibly-change-newsgroup newsgroup server)
147   (save-excursion
148     (set-buffer nnmbox-mbox-buffer)
149     (goto-char (point-min))
150     (when (search-forward (nnmbox-article-string article) nil t)
151       (let (start stop)
152         (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
153         (setq start (point))
154         (forward-line 1)
155         (or (and (re-search-forward
156                   (concat "^" message-unix-mail-delimiter) nil t)
157                  (forward-line -1))
158             (goto-char (point-max)))
159         (setq stop (point))
160         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
161           (set-buffer nntp-server-buffer)
162           (erase-buffer)
163           (insert-buffer-substring nnmbox-mbox-buffer start stop)
164           (goto-char (point-min))
165           (while (looking-at "From ")
166             (delete-char 5)
167             (insert "X-From-Line: ")
168             (forward-line 1))
169           (if (numberp article)
170               (cons nnmbox-current-group article)
171             (nnmbox-article-group-number)))))))
172
173 (deffoo nnmbox-request-group (group &optional server dont-check)
174   (nnmbox-possibly-change-newsgroup nil server)
175   (let ((active (cadr (assoc group nnmbox-group-alist))))
176     (cond
177      ((or (null active)
178           (null (nnmbox-possibly-change-newsgroup group server)))
179       (nnheader-report 'nnmbox "No such group: %s" group))
180      (dont-check
181       (nnheader-report 'nnmbox "Selected group %s" group)
182       (nnheader-insert ""))
183      (t
184       (nnheader-report 'nnmbox "Selected group %s" group)
185       (nnheader-insert "211 %d %d %d %s\n"
186                        (1+ (- (cdr active) (car active)))
187                        (car active) (cdr active) group)))))
188
189 (defun nnmbox-save-buffer ()
190   (let ((coding-system-for-write 
191          (or nnmbox-file-coding-system-for-write
192              nnmbox-file-coding-system)))
193     (save-buffer)))
194
195 (defun nnmbox-save-active (group-alist active-file)
196   (let ((nnmail-active-file-coding-system
197          (or nnmbox-active-file-coding-system-for-write
198              nnmbox-active-file-coding-system)))
199     (nnmail-save-active group-alist active-file)))
200
201 (deffoo nnmbox-request-scan (&optional group server)
202   (nnmbox-possibly-change-newsgroup group server)
203   (nnmbox-read-mbox)
204   (nnmail-get-new-mail
205    'nnmbox
206    (lambda ()
207      (save-excursion
208        (set-buffer nnmbox-mbox-buffer)
209        (nnmbox-save-buffer)))
210    (file-name-directory nnmbox-mbox-file)
211    group
212    (lambda ()
213      (save-excursion
214        (let ((in-buf (current-buffer)))
215          (set-buffer nnmbox-mbox-buffer)
216          (goto-char (point-max))
217          (insert-buffer-substring in-buf)))
218      (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
219
220 (deffoo nnmbox-close-group (group &optional server)
221   t)
222
223 (deffoo nnmbox-request-create-group (group &optional server args)
224   (nnmail-activate 'nnmbox)
225   (unless (assoc group nnmbox-group-alist)
226     (push (list group (cons 1 0))
227           nnmbox-group-alist)
228     (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
229   t)
230
231 (deffoo nnmbox-request-list (&optional server)
232   (save-excursion
233     (let ((nnmail-file-coding-system
234            nnmbox-active-file-coding-system))
235       (nnmail-find-file nnmbox-active-file))
236     (setq nnmbox-group-alist (nnmail-get-active))
237     t))
238
239 (deffoo nnmbox-request-newgroups (date &optional server)
240   (nnmbox-request-list server))
241
242 (deffoo nnmbox-request-list-newsgroups (&optional server)
243   (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
244
245 (deffoo nnmbox-request-expire-articles
246     (articles newsgroup &optional server force)
247   (nnmbox-possibly-change-newsgroup newsgroup server)
248   (let* ((is-old t)
249          rest)
250     (nnmail-activate 'nnmbox)
251
252     (save-excursion
253       (set-buffer nnmbox-mbox-buffer)
254       (while (and articles is-old)
255         (goto-char (point-min))
256         (when (search-forward (nnmbox-article-string (car articles)) nil t)
257           (if (setq is-old
258                     (nnmail-expired-article-p
259                      newsgroup
260                      (buffer-substring
261                       (point) (progn (end-of-line) (point))) force))
262               (progn
263                 (nnheader-message 5 "Deleting article %d in %s..."
264                                   (car articles) newsgroup)
265                 (nnmbox-delete-mail))
266             (push (car articles) rest)))
267         (setq articles (cdr articles)))
268       (nnmbox-save-buffer)
269       ;; Find the lowest active article in this group.
270       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
271         (goto-char (point-min))
272         (while (and (not (search-forward
273                           (nnmbox-article-string (car active)) nil t))
274                     (<= (car active) (cdr active)))
275           (setcar active (1+ (car active)))
276           (goto-char (point-min))))
277       (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
278       (nconc rest articles))))
279
280 (deffoo nnmbox-request-move-article
281     (article group server accept-form &optional last)
282   (let ((buf (get-buffer-create " *nnmbox move*"))
283         result)
284     (and
285      (nnmbox-request-article article group server)
286      (save-excursion
287        (set-buffer buf)
288        (erase-buffer)
289        (insert-buffer-substring nntp-server-buffer)
290        (goto-char (point-min))
291        (while (re-search-forward
292                "^X-Gnus-Newsgroup:"
293                (save-excursion (search-forward "\n\n" nil t) (point)) t)
294          (delete-region (progn (beginning-of-line) (point))
295                         (progn (forward-line 1) (point))))
296        (setq result (eval accept-form))
297        (kill-buffer buf)
298        result)
299      (save-excursion
300        (nnmbox-possibly-change-newsgroup group server)
301        (set-buffer nnmbox-mbox-buffer)
302        (goto-char (point-min))
303        (when (search-forward (nnmbox-article-string article) nil t)
304          (nnmbox-delete-mail))
305        (and last (nnmbox-save-buffer))))
306     result))
307
308 (deffoo nnmbox-request-accept-article (group &optional server last)
309   (nnmbox-possibly-change-newsgroup group server)
310   (nnmail-check-syntax)
311   (let ((buf (current-buffer))
312         result)
313     (goto-char (point-min))
314     ;; The From line may have been quoted by movemail.
315     (when (looking-at (concat ">" message-unix-mail-delimiter))
316       (delete-char 1))
317     (if (looking-at "X-From-Line: ")
318         (replace-match "From ")
319       (insert "From nobody " (current-time-string) "\n"))
320     (and
321      (nnmail-activate 'nnmbox)
322      (progn
323        (set-buffer buf)
324        (goto-char (point-min))
325        (search-forward "\n\n" nil t)
326        (forward-line -1)
327        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
328          (delete-region (point) (progn (forward-line 1) (point))))
329        (when nnmail-cache-accepted-message-ids
330          (nnmail-cache-insert (nnmail-fetch-field "message-id")))
331        (setq result (if (stringp group)
332                         (list (cons group (nnmbox-active-number group)))
333                       (nnmail-article-group 'nnmbox-active-number)))
334        (if (and (null result)
335                 (yes-or-no-p "Moved to `junk' group; delete article? "))
336            (setq result 'junk)
337          (setq result (car (nnmbox-save-mail result)))))
338      (save-excursion
339        (set-buffer nnmbox-mbox-buffer)
340        (goto-char (point-max))
341        (insert-buffer-substring buf)
342        (when last
343          (when nnmail-cache-accepted-message-ids
344            (nnmail-cache-close))
345          (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
346          (nnmbox-save-buffer))))
347     result))
348
349 (deffoo nnmbox-request-replace-article (article group buffer)
350   (nnmbox-possibly-change-newsgroup group)
351   (save-excursion
352     (set-buffer nnmbox-mbox-buffer)
353     (goto-char (point-min))
354     (if (not (search-forward (nnmbox-article-string article) nil t))
355         nil
356       (nnmbox-delete-mail t t)
357       (insert-buffer-substring buffer)
358       (nnmbox-save-buffer)
359       t)))
360
361 (deffoo nnmbox-request-delete-group (group &optional force server)
362   (nnmbox-possibly-change-newsgroup group server)
363   ;; Delete all articles in GROUP.
364   (if (not force)
365       ()                                ; Don't delete the articles.
366     (save-excursion
367       (set-buffer nnmbox-mbox-buffer)
368       (goto-char (point-min))
369       ;; Delete all articles in this group.
370       (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
371             found)
372         (while (search-forward ident nil t)
373           (setq found t)
374           (nnmbox-delete-mail))
375         (when found
376           (nnmbox-save-buffer)))))
377   ;; Remove the group from all structures.
378   (setq nnmbox-group-alist
379         (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
380         nnmbox-current-group nil)
381   ;; Save the active file.
382   (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
383   t)
384
385 (deffoo nnmbox-request-rename-group (group new-name &optional server)
386   (nnmbox-possibly-change-newsgroup group server)
387   (save-excursion
388     (set-buffer nnmbox-mbox-buffer)
389     (goto-char (point-min))
390     (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
391           (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
392           found)
393       (while (search-forward ident nil t)
394         (replace-match new-ident t t)
395         (setq found t))
396       (when found
397         (nnmbox-save-buffer))))
398   (let ((entry (assoc group nnmbox-group-alist)))
399     (when entry
400       (setcar entry new-name))
401     (setq nnmbox-current-group nil)
402     ;; Save the new group alist.
403     (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
404     t))
405
406 \f
407 ;;; Internal functions.
408
409 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
410 ;; headers there are.  If LEAVE-DELIM, don't delete the Unix mbox
411 ;; delimiter line.
412 (defun nnmbox-delete-mail (&optional force leave-delim)
413   ;; Delete the current X-Gnus-Newsgroup line.
414   (or force
415       (delete-region
416        (progn (beginning-of-line) (point))
417        (progn (forward-line 1) (point))))
418   ;; Beginning of the article.
419   (save-excursion
420     (save-restriction
421       (narrow-to-region
422        (save-excursion
423          (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
424          (if leave-delim (progn (forward-line 1) (point))
425            (match-beginning 0)))
426        (progn
427          (forward-line 1)
428          (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
429                                      nil t)
430                   (if (and (not (bobp)) leave-delim)
431                       (progn (forward-line -2) (point))
432                     (match-beginning 0)))
433              (point-max))))
434       (goto-char (point-min))
435       ;; Only delete the article if no other groups owns it as well.
436       (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
437         (delete-region (point-min) (point-max))))))
438
439 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
440   (when (and server
441              (not (nnmbox-server-opened server)))
442     (nnmbox-open-server server))
443   (when (or (not nnmbox-mbox-buffer)
444             (not (buffer-name nnmbox-mbox-buffer)))
445     (save-excursion
446       (set-buffer (setq nnmbox-mbox-buffer
447                         (let ((nnheader-file-coding-system
448                                nnmbox-file-coding-system))
449                           (nnheader-find-file-noselect
450                            nnmbox-mbox-file nil t))))
451       (mm-enable-multibyte)
452       (buffer-disable-undo)))
453   (when (not nnmbox-group-alist)
454     (nnmail-activate 'nnmbox))
455   (if newsgroup
456       (when (assoc newsgroup nnmbox-group-alist)
457         (setq nnmbox-current-group newsgroup))
458     t))
459
460 (defun nnmbox-article-string (article)
461   (if (numberp article)
462       (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
463               (int-to-string article) " ")
464     (concat "\nMessage-ID: " article)))
465
466 (defun nnmbox-article-group-number ()
467   (save-excursion
468     (goto-char (point-min))
469     (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
470                              nil t)
471       (cons (buffer-substring (match-beginning 1) (match-end 1))
472             (string-to-int
473              (buffer-substring (match-beginning 2) (match-end 2)))))))
474
475 (defun nnmbox-save-mail (group-art)
476   "Called narrowed to an article."
477   (let ((delim (concat "^" message-unix-mail-delimiter)))
478     (goto-char (point-min))
479     ;; This might come from somewhere else.
480     (unless (looking-at delim)
481       (insert "From nobody " (current-time-string) "\n")
482       (goto-char (point-min)))
483     ;; Quote all "From " lines in the article.
484     (forward-line 1)
485     (while (re-search-forward delim nil t)
486       (beginning-of-line)
487       (insert "> "))
488     (nnmail-insert-lines)
489     (nnmail-insert-xref group-art)
490     (nnmbox-insert-newsgroup-line group-art)
491     (run-hooks 'nnmail-prepare-save-mail-hook)
492     (run-hooks 'nnmbox-prepare-save-mail-hook)
493     group-art))
494
495 (defun nnmbox-insert-newsgroup-line (group-art)
496   (save-excursion
497     (goto-char (point-min))
498     (when (search-forward "\n\n" nil t)
499       (forward-char -1)
500       (while group-art
501         (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n"
502                         (caar group-art) (cdar group-art)
503                         (current-time-string)))
504         (setq group-art (cdr group-art))))
505     t))
506
507 (defun nnmbox-active-number (group)
508   ;; Find the next article number in GROUP.
509   (let ((active (cadr (assoc group nnmbox-group-alist))))
510     (if active
511         (setcdr active (1+ (cdr active)))
512       ;; This group is new, so we create a new entry for it.
513       ;; This might be a bit naughty... creating groups on the drop of
514       ;; a hat, but I don't know...
515       (push (list group (setq active (cons 1 1)))
516             nnmbox-group-alist))
517     (cdr active)))
518
519 (defun nnmbox-create-mbox ()
520   (when (not (file-exists-p nnmbox-mbox-file))
521     (let ((nnmail-file-coding-system
522            (or nnmbox-file-coding-system-for-write
523                nnmbox-file-coding-system)))
524       (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg))))
525
526 (defun nnmbox-read-mbox ()
527   (nnmail-activate 'nnmbox)
528   (nnmbox-create-mbox)
529   (if (and nnmbox-mbox-buffer
530            (buffer-name nnmbox-mbox-buffer)
531            (save-excursion
532              (set-buffer nnmbox-mbox-buffer)
533              (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
534       ()
535     (save-excursion
536       (let ((delim (concat "^" message-unix-mail-delimiter))
537             (alist nnmbox-group-alist)
538             start end number)
539         (set-buffer (setq nnmbox-mbox-buffer
540                           (let ((nnheader-file-coding-system
541                                  nnmbox-file-coding-system))
542                             (nnheader-find-file-noselect
543                              nnmbox-mbox-file nil t))))
544         (mm-enable-multibyte)
545         (buffer-disable-undo)
546
547         ;; Go through the group alist and compare against
548         ;; the mbox file.
549         (while alist
550           (goto-char (point-max))
551           (when (and (re-search-backward
552                       (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
553                               (caar alist)) nil t)
554                      (> (setq number
555                               (string-to-number
556                                (buffer-substring
557                                 (match-beginning 1) (match-end 1))))
558                         (cdadar alist)))
559             (setcdr (cadar alist) number))
560           (setq alist (cdr alist)))
561
562         (goto-char (point-min))
563         (while (re-search-forward delim nil t)
564           (setq start (match-beginning 0))
565           (unless (search-forward
566                    "\nX-Gnus-Newsgroup: "
567                    (save-excursion
568                      (setq end
569                            (or
570                             (and
571                              ;; skip to end of headers first, since mail
572                              ;; which has been respooled has additional
573                              ;; "From nobody" lines.
574                              (search-forward "\n\n" nil t)
575                              (re-search-forward delim nil t)
576                              (match-beginning 0))
577                             (point-max))))
578                    t)
579             (save-excursion
580               (save-restriction
581                 (narrow-to-region start end)
582                 (nnmbox-save-mail
583                  (nnmail-article-group 'nnmbox-active-number)))))
584           (goto-char end))))))
585
586 (provide 'nnmbox)
587
588 ;;; nnmbox.el ends here