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