* Makefile.in (install-package-ja): Compile and install lisp files first.
[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                 (nnheader-message 5 "Deleting article %d in %s..."
273                                   (car articles) newsgroup)
274                 (nnmbox-delete-mail))
275             (push (car articles) rest)))
276         (setq articles (cdr articles)))
277       (nnmbox-save-buffer)
278       ;; Find the lowest active article in this group.
279       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
280         (goto-char (point-min))
281         (while (and (not (search-forward
282                           (nnmbox-article-string (car active)) nil t))
283                     (<= (car active) (cdr active)))
284           (setcar active (1+ (car active)))
285           (goto-char (point-min))))
286       (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
287       (nconc rest articles))))
288
289 (deffoo nnmbox-request-move-article
290     (article group server accept-form &optional last)
291   (let ((buf (get-buffer-create " *nnmbox move*"))
292         result)
293     (and
294      (nnmbox-request-article article group server)
295      (save-excursion
296        (set-buffer buf)
297        (erase-buffer)
298        (insert-buffer-substring nntp-server-buffer)
299        (goto-char (point-min))
300        (while (re-search-forward
301                "^X-Gnus-Newsgroup:"
302                (save-excursion (search-forward "\n\n" nil t) (point)) t)
303          (delete-region (progn (beginning-of-line) (point))
304                         (progn (forward-line 1) (point))))
305        (setq result (eval accept-form))
306        (kill-buffer buf)
307        result)
308      (save-excursion
309        (nnmbox-possibly-change-newsgroup group server)
310        (set-buffer nnmbox-mbox-buffer)
311        (goto-char (point-min))
312        (when (search-forward (nnmbox-article-string article) nil t)
313          (nnmbox-delete-mail))
314        (and last (nnmbox-save-buffer))))
315     result))
316
317 (deffoo nnmbox-request-accept-article (group &optional server last)
318   (nnmbox-possibly-change-newsgroup group server)
319   (nnmail-check-syntax)
320   (let ((buf (current-buffer))
321         result)
322     (goto-char (point-min))
323     ;; The From line may have been quoted by movemail.
324     (when (looking-at (concat ">" message-unix-mail-delimiter))
325       (delete-char 1))
326     (if (looking-at "X-From-Line: ")
327         (replace-match "From ")
328       (insert "From nobody " (current-time-string) "\n"))
329     (and
330      (nnmail-activate 'nnmbox)
331      (progn
332        (set-buffer buf)
333        (goto-char (point-min))
334        (search-forward "\n\n" nil t)
335        (forward-line -1)
336        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
337          (delete-region (point) (progn (forward-line 1) (point))))
338        (when nnmail-cache-accepted-message-ids
339          (nnmail-cache-insert (nnmail-fetch-field "message-id")))
340        (setq result (if (stringp group)
341                         (list (cons group (nnmbox-active-number group)))
342                       (nnmail-article-group 'nnmbox-active-number)))
343        (if (and (null result)
344                 (yes-or-no-p "Moved to `junk' group; delete article? "))
345            (setq result 'junk)
346          (setq result (car (nnmbox-save-mail result)))))
347      (save-excursion
348        (set-buffer nnmbox-mbox-buffer)
349        (goto-char (point-max))
350        (insert-buffer-substring buf)
351        (when last
352          (when nnmail-cache-accepted-message-ids
353            (nnmail-cache-close))
354          (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
355          (nnmbox-save-buffer))))
356     result))
357
358 (deffoo nnmbox-request-replace-article (article group buffer)
359   (nnmbox-possibly-change-newsgroup group)
360   (save-excursion
361     (set-buffer nnmbox-mbox-buffer)
362     (goto-char (point-min))
363     (if (not (search-forward (nnmbox-article-string article) nil t))
364         nil
365       (nnmbox-delete-mail t t)
366       (insert-buffer-substring buffer)
367       (nnmbox-save-buffer)
368       t)))
369
370 (deffoo nnmbox-request-delete-group (group &optional force server)
371   (nnmbox-possibly-change-newsgroup group server)
372   ;; Delete all articles in GROUP.
373   (if (not force)
374       ()                                ; Don't delete the articles.
375     (save-excursion
376       (set-buffer nnmbox-mbox-buffer)
377       (goto-char (point-min))
378       ;; Delete all articles in this group.
379       (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
380             found)
381         (while (search-forward ident nil t)
382           (setq found t)
383           (nnmbox-delete-mail))
384         (when found
385           (nnmbox-save-buffer)))))
386   ;; Remove the group from all structures.
387   (setq nnmbox-group-alist
388         (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
389         nnmbox-current-group nil)
390   ;; Save the active file.
391   (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
392   t)
393
394 (deffoo nnmbox-request-rename-group (group new-name &optional server)
395   (nnmbox-possibly-change-newsgroup group server)
396   (save-excursion
397     (set-buffer nnmbox-mbox-buffer)
398     (goto-char (point-min))
399     (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
400           (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
401           found)
402       (while (search-forward ident nil t)
403         (replace-match new-ident t t)
404         (setq found t))
405       (when found
406         (nnmbox-save-buffer))))
407   (let ((entry (assoc group nnmbox-group-alist)))
408     (when entry
409       (setcar entry new-name))
410     (setq nnmbox-current-group nil)
411     ;; Save the new group alist.
412     (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
413     t))
414
415 \f
416 ;;; Internal functions.
417
418 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
419 ;; headers there are.  If LEAVE-DELIM, don't delete the Unix mbox
420 ;; delimiter line.
421 (defun nnmbox-delete-mail (&optional force leave-delim)
422   ;; Delete the current X-Gnus-Newsgroup line.
423   (or force
424       (delete-region
425        (progn (beginning-of-line) (point))
426        (progn (forward-line 1) (point))))
427   ;; Beginning of the article.
428   (save-excursion
429     (save-restriction
430       (narrow-to-region
431        (save-excursion
432          (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
433          (if leave-delim (progn (forward-line 1) (point))
434            (match-beginning 0)))
435        (progn
436          (forward-line 1)
437          (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
438                                      nil t)
439                   (if (and (not (bobp)) leave-delim)
440                       (progn (forward-line -2) (point))
441                     (match-beginning 0)))
442              (point-max))))
443       (goto-char (point-min))
444       ;; Only delete the article if no other groups owns it as well.
445       (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
446         (delete-region (point-min) (point-max))))))
447
448 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
449   (when (and server
450              (not (nnmbox-server-opened server)))
451     (nnmbox-open-server server))
452   (when (or (not nnmbox-mbox-buffer)
453             (not (buffer-name nnmbox-mbox-buffer)))
454     (save-excursion
455       (set-buffer (setq nnmbox-mbox-buffer
456                         (let ((nnheader-file-coding-system
457                                nnmbox-file-coding-system))
458                           (nnheader-find-file-noselect
459                            nnmbox-mbox-file nil t))))
460       (buffer-disable-undo)))
461   (when (not nnmbox-group-alist)
462     (nnmail-activate 'nnmbox))
463   (if newsgroup
464       (when (assoc newsgroup nnmbox-group-alist)
465         (setq nnmbox-current-group newsgroup))
466     t))
467
468 (defun nnmbox-article-string (article)
469   (if (numberp article)
470       (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
471               (int-to-string article) " ")
472     (concat "\nMessage-ID: " article)))
473
474 (defun nnmbox-article-group-number ()
475   (save-excursion
476     (goto-char (point-min))
477     (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
478                              nil t)
479       (cons (buffer-substring (match-beginning 1) (match-end 1))
480             (string-to-int
481              (buffer-substring (match-beginning 2) (match-end 2)))))))
482
483 (defun nnmbox-save-mail (group-art)
484   "Called narrowed to an article."
485   (let ((delim (concat "^" message-unix-mail-delimiter)))
486     (goto-char (point-min))
487     ;; This might come from somewhere else.
488     (unless (looking-at delim)
489       (insert "From nobody " (current-time-string) "\n")
490       (goto-char (point-min)))
491     ;; Quote all "From " lines in the article.
492     (forward-line 1)
493     (while (re-search-forward delim nil t)
494       (beginning-of-line)
495       (insert "> "))
496     (nnmail-insert-lines)
497     (nnmail-insert-xref group-art)
498     (nnmbox-insert-newsgroup-line group-art)
499     (run-hooks 'nnmail-prepare-save-mail-hook)
500     (run-hooks 'nnmbox-prepare-save-mail-hook)
501     group-art))
502
503 (defun nnmbox-insert-newsgroup-line (group-art)
504   (save-excursion
505     (goto-char (point-min))
506     (when (search-forward "\n\n" nil t)
507       (forward-char -1)
508       (while group-art
509         (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n"
510                         (caar group-art) (cdar group-art)
511                         (current-time-string)))
512         (setq group-art (cdr group-art))))
513     t))
514
515 (defun nnmbox-active-number (group)
516   ;; Find the next article number in GROUP.
517   (let ((active (cadr (assoc group nnmbox-group-alist))))
518     (if active
519         (setcdr active (1+ (cdr active)))
520       ;; This group is new, so we create a new entry for it.
521       ;; This might be a bit naughty... creating groups on the drop of
522       ;; a hat, but I don't know...
523       (push (list group (setq active (cons 1 1)))
524             nnmbox-group-alist))
525     (cdr active)))
526
527 (defun nnmbox-create-mbox ()
528   (when (not (file-exists-p nnmbox-mbox-file))
529     (let ((nnmail-file-coding-system
530            (or nnmbox-file-coding-system-for-write
531                nnmbox-file-coding-system)))
532       (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg))))
533
534 (defun nnmbox-read-mbox ()
535   (nnmail-activate 'nnmbox)
536   (nnmbox-create-mbox)
537   (if (and nnmbox-mbox-buffer
538            (buffer-name nnmbox-mbox-buffer)
539            (save-excursion
540              (set-buffer nnmbox-mbox-buffer)
541              (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
542       ()
543     (save-excursion
544       (let ((delim (concat "^" message-unix-mail-delimiter))
545             (alist nnmbox-group-alist)
546             start end number)
547         (set-buffer (setq nnmbox-mbox-buffer
548                           (let ((nnheader-file-coding-system
549                                  nnmbox-file-coding-system))
550                             (nnheader-find-file-noselect
551                              nnmbox-mbox-file nil t))))
552         (buffer-disable-undo)
553
554         ;; Go through the group alist and compare against
555         ;; the mbox file.
556         (while alist
557           (goto-char (point-max))
558           (when (and (re-search-backward
559                       (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
560                               (caar alist)) nil t)
561                      (> (setq number
562                               (string-to-number
563                                (buffer-substring
564                                 (match-beginning 1) (match-end 1))))
565                         (cdadar alist)))
566             (setcdr (cadar alist) number))
567           (setq alist (cdr alist)))
568
569         (goto-char (point-min))
570         (while (re-search-forward delim nil t)
571           (setq start (match-beginning 0))
572           (unless (search-forward
573                    "\nX-Gnus-Newsgroup: "
574                    (save-excursion
575                      (setq end
576                            (or
577                             (and
578                              ;; skip to end of headers first, since mail
579                              ;; which has been respooled has additional
580                              ;; "From nobody" lines.
581                              (search-forward "\n\n" nil t)
582                              (re-search-forward delim nil t)
583                              (match-beginning 0))
584                             (point-max))))
585                    t)
586             (save-excursion
587               (save-restriction
588                 (narrow-to-region start end)
589                 (nnmbox-save-mail
590                  (nnmail-article-group 'nnmbox-active-number)))))
591           (goto-char end))))))
592
593 (provide 'nnmbox)
594
595 ;;; nnmbox.el ends here