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