1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news, mail
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32 (defvar nnmail-split-methods
34 "*Incoming mail will be split according to this variable.
36 If you'd like, for instance, one mail group for mail from the
37 \"4ad-l\" mailing list, one group for junk mail and one for everything
38 else, you could do something like this:
40 (setq nnmail-split-methods
41 '((\"mail.4ad\" \"From:.*4ad\")
42 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
43 (\"mail.misc\" \"\")))
45 As you can see, this variable is a list of lists, where the first
46 element in each \"rule\" is the name of the group (which, by the way,
47 does not have to be called anything beginning with \"mail\",
48 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
49 nnmail will try to match on the header to find a fit.
51 The second element can also be a function. In that case, it will be
52 called narrowed to the headers with the first element of the rule as
53 the argument. It should return a non-nil value if it thinks that the
54 mail belongs in that group.
56 The last element should always have \"\" as the regexp.")
58 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
59 (defvar nnmail-crosspost t
60 "*If non-nil, do crossposting if several split methods match the mail.
61 If nil, the first match found will be used.")
63 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
64 (defvar nnmail-keep-last-article nil
65 "*If non-nil, nnmail will never delete the last expired article in a
66 directory. You may need to set this variable if other programs are putting
67 new mail into folder numbers that Gnus has marked as expired.")
69 (defvar nnmail-expiry-wait 7
70 "*Articles that are older than `nnmail-expiry-wait' days will be expired.")
72 (defvar nnmail-expiry-wait-function nil
73 "*Variable that holds funtion to specify how old articles should be before they are expired.
74 The function will be called with the name of the group that the
75 expiry is to be performed in, and it should return an integer that
76 says how many days an article can be stored before it is considered
81 (setq nnmail-expiry-wait-function
83 (cond ((string-match \"private\" newsgroup) 31)
84 ((string-match \"junk\" newsgroup) 1)
87 (defvar nnmail-spool-file
89 (concat "/usr/spool/mail/" (user-login-name)))
90 "Where the mail backends will look for incoming mail.
91 This variable is \"/usr/spool/mail/$user\" by default.
92 If this variable is nil, no mail backends will read incoming mail.
93 If this variable is `procmail', the mail backends will look in
94 `nnmail-procmail-directory' for spool files.")
96 (defvar nnmail-procmail-directory "~/incoming/"
97 "*When using procmail (and the like), incoming mail is put in this directory.
98 The Gnus mail backends will read the mail from this directory.")
100 (defvar nnmail-procmail-suffix ".spool"
101 "*Suffix of files created by procmail (and the like).")
103 (defvar nnmail-read-incoming-hook nil
104 "*Hook that will be run after the incoming mail has been transferred.
105 The incoming mail is moved from `nnmail-spool-file' (which normally is
106 something like \"/usr/spool/mail/$user\") to the user's home
107 directory. This hook is called after the incoming mail box has been
108 emptied, and can be used to call any mail box programs you have
109 running (\"xwatch\", etc.)
113 (add-hook 'nnmail-read-incoming-hook
115 (start-process \"mailsend\" nil
116 \"/local/bin/mailsend\" \"read\" \"mbox\")))")
118 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
119 (defvar nnmail-prepare-incoming-hook nil
120 "*Hook called before treating incoming mail.
121 The hook is run in a buffer with all the new, incoming mail.")
123 ;; Suggested by Mejia Pablo J <pjm9806@usl.edu>.
124 (defvar nnmail-tmp-directory nil
125 "*If non-nil, use this directory for temporary storage when reading incoming mail.")
127 (defvar nnmail-large-newsgroup 50
128 "*The number of the articles which indicates a large newsgroup.
129 If the number of the articles is greater than the value, verbose
130 messages will be shown to indicate the current status.")
132 (defvar nnmail-split-fancy "mail.misc"
133 "*Incoming mail can be split according to this fancy variable.
134 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
136 The format is this variable is SPLIT, where SPLIT can be one of
139 GROUP: Mail will be stored in GROUP (a string).
141 \(FIELD VALUE SPLIT): If the message field FIELD (a regexp) contains
142 VALUE (a regexp), store the messages as specified by SPLIT.
144 \(| SPLIT...): Process each SPLIT expression until one of them matches.
145 A SPLIT expression is said to match if it will cause the mail
146 message to be stored in one or more groups.
148 \(& SPLIT...): Process each SPLIT expression.
150 FIELD must match a complete field name. VALUE must match a complete
151 word according to the fundamental mode syntax table. You can use .*
152 in the regexps to match partial field names or words.
154 FIELD and VALUE can also be lisp symbols, in that case they are expanded
155 as specified in `nnmail-split-abbrev-alist'.
159 \(setq nnmail-split-methods 'nnmail-split-fancy
161 ;; Messages from the mailer deamon are not crossposted to any of
162 ;; the ordinary groups. Warnings are put in a separate group
164 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
166 ;; Non-error messages are crossposted to all relevant
167 ;; groups, but we don't crosspost between the group for the
168 ;; (ding) list and the group for other (ding) related mail.
169 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
170 (\"subject\" \"ding\" \"ding.misc\"))
171 ;; Other mailing lists...
172 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
173 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
175 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
176 ;; Unmatched mail goes to the catch all group.
179 (defvar nnmail-split-abbrev-alist
180 '((any . "from\\|to\\|cc\\|sender\\|apparently-to")
181 (mail . "mailer-daemon\\|postmaster"))
182 "*Alist of abbrevations allowed in `nnmail-split-fancy'.")
186 (defconst nnmail-version "nnml 0.0"
191 (defun nnmail-request-post-buffer (post group subject header article-buffer
192 info follow-to respect-poster)
193 (let ((method-address (cdr (assq 'to-address (nth 5 info))))
194 from date to reply-to message-of
195 references message-id sender cc sendto elt)
197 (if (and (stringp method-address)
198 (string= method-address ""))
201 (set-buffer (get-buffer-create "*mail*"))
203 (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
204 (if (and (buffer-modified-p)
206 (not (y-or-n-p "Unsent mail being composed; erase it? ")))
210 (mail-setup method-address subject nil nil nil nil)
212 (set-buffer article-buffer)
213 (goto-char (point-min))
214 (narrow-to-region (point-min)
215 (progn (search-forward "\n\n") (point)))
216 (let ((buffer-read-only nil))
217 (set-text-properties (point-min) (point-max) nil))
218 (setq from (header-from header))
219 (setq date (header-date header))
222 (string-match " *at \\| *@ \\| *(\\| *<" from)))
224 (concat (if stop-pos (substring from 0 stop-pos) from)
225 "'s message of " date))))
226 (setq sender (mail-fetch-field "sender"))
227 (setq cc (mail-fetch-field "cc"))
228 (setq to (mail-fetch-field "to"))
229 (setq subject (header-subject header))
230 (or (string-match "^[Rr][Ee]:" subject)
231 (setq subject (concat "Re: " subject)))
232 (setq reply-to (mail-fetch-field "reply-to"))
233 (setq references (header-references header))
234 (setq message-id (header-id header))
236 (setq news-reply-yank-from from)
237 (setq news-reply-yank-message-id message-id)
239 ;; Gather the "to" addresses out of the follow-to list and remove
241 (if (and follow-to (listp follow-to))
242 (while (setq elt (assoc "To" follow-to))
243 (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
244 (setq follow-to (delq elt follow-to))))
245 (mail-setup (if (and follow-to (listp follow-to)) sendto
247 (concat (or sender reply-to from "")
248 (if to (concat ", " to) "")
249 (if cc (concat ", " cc) ""))))
250 subject message-of nil article-buffer nil)
251 ;; Note that "To" elements should already be in the message.
252 (if (and follow-to (listp follow-to))
254 (goto-char (point-min))
255 (re-search-forward "^To:" nil t)
260 (car (car follow-to)) ": " (cdr (car follow-to)) "\n")
261 (setq follow-to (cdr follow-to)))))
262 ;; Fold long references line to follow RFC1036.
263 (mail-position-on-field "References")
264 (let ((begin (- (point) (length "References: ")))
267 (if references (insert references))
268 (if (and references message-id) (insert " "))
269 (if message-id (insert message-id))
270 ;; The region must end with a newline to fill the region
271 ;; without inserting extra newline.
272 (fill-region-as-paragraph begin (1+ (point))))))
275 (defun nnmail-find-file (file)
276 "Insert FILE in server buffer safely."
277 (set-buffer nntp-server-buffer)
280 (progn (insert-file-contents file) t)
283 (defun nnmail-article-pathname (group mail-dir)
284 "Make pathname for GROUP."
285 (concat (file-name-as-directory (expand-file-name mail-dir))
286 (nnmail-replace-chars-in-string group ?. ?/) "/"))
288 (defun nnmail-replace-chars-in-string (string from to)
289 "Replace characters in STRING from FROM to TO."
290 (let ((string (substring string 0)) ;Copy string.
291 (len (length string))
293 ;; Replace all occurrences of FROM with TO.
295 (if (= (aref string idx) from)
296 (aset string idx to))
300 (defun nnmail-days-between (date1 date2)
301 ;; Return the number of days between date1 and date2.
302 (let ((d1 (mapcar (lambda (s) (and s (string-to-int s)) )
303 (timezone-parse-date date1)))
304 (d2 (mapcar (lambda (s) (and s (string-to-int s)) )
305 (timezone-parse-date date2))))
306 (- (timezone-absolute-from-gregorian
307 (nth 1 d1) (nth 2 d1) (car d1))
308 (timezone-absolute-from-gregorian
309 (nth 1 d2) (nth 2 d2) (car d2)))))
311 ;; Function taken from rmail.el.
312 (defun nnmail-move-inbox (inbox tofile)
313 (let ((inbox (file-truename
314 (expand-file-name (substitute-in-file-name inbox))))
315 (tofile (nnmail-make-complex-temp-name (expand-file-name tofile)))
316 movemail popmail errors)
317 ;; Check whether the inbox is to be moved to the special tmp dir.
318 (if nnmail-tmp-directory
319 (setq tofile (concat (file-name-as-directory nnmail-tmp-directory)
320 (file-name-nondirectory tofile))))
321 ;; If getting from mail spool directory,
322 ;; use movemail to move rather than just renaming,
323 ;; so as to interlock with the mailer.
324 (setq movemail (string= (file-name-directory inbox)
325 (file-truename rmail-spool-directory))
326 popmail (string-match "^po:" (file-name-nondirectory inbox)))
327 (if popmail (setq inbox (file-name-nondirectory inbox)))
329 ;; On some systems, /usr/spool/mail/foo is a directory
330 ;; and the actual inbox is /usr/spool/mail/foo/foo.
331 (if (file-directory-p inbox)
332 (setq inbox (expand-file-name (user-login-name) inbox))))
334 (message "Getting mail from post office ...")
335 (if (or (and (file-exists-p tofile)
336 (/= 0 (nth 7 (file-attributes tofile))))
337 (and (file-exists-p inbox)
338 (/= 0 (nth 7 (file-attributes inbox)))))
339 (message "Getting mail from %s..." inbox)))
340 ;; Set TOFILE if have not already done so, and
341 ;; rename or copy the file INBOX to TOFILE if and as appropriate.
342 (cond ((or (file-exists-p tofile) (and (not popmail)
343 (not (file-exists-p inbox))))
345 ((and (not movemail) (not popmail))
346 ;; Try copying. If that fails (perhaps no space),
349 (copy-file inbox tofile nil)
351 ;; Third arg is t so we can replace existing file TOFILE.
352 (rename-file inbox tofile t)))
353 ;; Make the real inbox file empty.
354 ;; Leaving it deleted could cause lossage
355 ;; because mailers often won't create the file.
357 (write-region (point) (point) inbox)
362 (setq errors (generate-new-buffer " *nnmail loss*"))
363 (buffer-disable-undo errors)
365 (expand-file-name "movemail" exec-directory)
366 nil errors nil inbox tofile)
367 (if (not (buffer-modified-p errors))
368 ;; No output => movemail won
371 (subst-char-in-region (point-min) (point-max)
373 (goto-char (point-max))
374 (skip-chars-backward " \t")
375 (delete-region (point) (point-max))
376 (goto-char (point-min))
377 (if (looking-at "movemail: ")
378 (delete-region (point-min) (match-end 0)))
380 (message (concat "movemail: "
381 (buffer-substring (point-min)
387 (kill-buffer errors))
391 (defun nnmail-get-active ()
392 "Returns an assoc of group names and active ranges.
393 nn*-request-list should have been called before calling this function."
395 ;; Go through all groups from the active list.
397 (set-buffer nntp-server-buffer)
398 (goto-char (point-min))
399 (while (re-search-forward
400 "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
402 (cons (list (buffer-substring (match-beginning 1)
405 (buffer-substring (match-beginning 3)
408 (buffer-substring (match-beginning 2)
411 ;; In addition, add all groups mentioned in `nnmail-split-methods'.
412 (let ((methods (and (not (symbolp nnmail-split-methods))
413 nnmail-split-methods)))
415 (if (not (assoc (car (car methods)) group-assoc))
417 (cons (list (car (car methods)) (cons 1 0))
419 (setq methods (cdr methods))))
422 (defun nnmail-save-active (group-assoc file-name)
425 (set-buffer (get-buffer-create " *nnmail active*"))
426 (buffer-disable-undo (current-buffer))
429 (setq group (car group-assoc))
430 (insert (format "%s %d %d y\n" (car group) (cdr (car (cdr group)) )
431 (car (car (cdr group)))))
432 (setq group-assoc (cdr group-assoc)))
433 (write-region 1 (point-max) (expand-file-name file-name) nil 'nomesg)
434 (kill-buffer (current-buffer)))))
436 (defun nnmail-split-incoming (incoming func &optional dont-kill)
437 "Go through the entire INCOMING file and pick out each individual mail.
438 FUNC will be called with the buffer narrowed to each mail."
439 (let ((delim (concat "^" rmail-unix-mail-delimiter))
440 start end content-length do-search)
442 (set-buffer (get-buffer-create " *nnmail incoming*"))
443 (buffer-disable-undo (current-buffer))
445 (insert-file-contents incoming)
446 (goto-char (point-min))
447 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
448 ;; Go to the beginning of the first mail...
449 (if (and (re-search-forward delim nil t)
450 (goto-char (match-beginning 0)))
451 ;; and then carry on until the bitter end.
454 ;; Skip all the headers in case there are more "From "s...
455 (if (not (search-forward "\n\n" nil t))
457 ;; Look for a Content-Length header.
458 (if (not (save-excursion
459 (and (re-search-backward
460 "^Content-Length: \\([0-9]+\\)" nil t)
461 (setq content-length (string-to-int
465 ;; We destroy the header, since none of
466 ;; the backends ever use it, and we do not
467 ;; want to confuse other mailers by having
468 ;; a (possibly) faulty header.
469 (progn (insert "X-") t))))
472 (forward-char content-length)
475 (forward-char content-length)
476 (setq do-search nil))
478 ;; Go to the beginning of the next article - or to the end
481 (if (re-search-forward delim nil t)
482 (goto-char (match-beginning 0))
483 (goto-char (point-max))))
486 (narrow-to-region start (point))
487 (goto-char (point-min))
489 (setq end (point-max))))
493 (kill-buffer (current-buffer))))))
495 ;; Mail crossposts syggested by Brian Edmonds <edmonds@cs.ubc.ca>.
496 (defun nnmail-article-group (func)
497 "Look at the headers and return an alist of groups that match.
498 FUNC will be called with the group name to determine the article number."
499 (let ((methods nnmail-split-methods)
500 (obuf (current-buffer))
506 (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
507 (set-buffer (get-buffer-create " *nnmail work*"))
508 (buffer-disable-undo (current-buffer))
510 ;; Copy the headers into the work buffer.
511 (insert-buffer-substring obuf beg end)
512 ;; Fold continuation lines.
513 (goto-char (point-min))
514 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
515 (replace-match " " t t))
516 (if (and (symbolp nnmail-split-methods)
517 (fboundp nnmail-split-methods))
520 (lambda (group) (cons group (funcall func group)))
521 (funcall nnmail-split-methods)))
522 ;; Go throught the split methods to find a match.
523 (while (and methods (or nnmail-crosspost (not group-art)))
524 (goto-char (point-max))
525 (if (or (cdr methods)
526 (not (equal "" (nth 1 (car methods)))))
527 (if (and (condition-case ()
528 (if (stringp (nth 1 (car methods)))
530 (car (cdr (car methods))) nil t)
531 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
532 (funcall (nth 1 (car methods))
533 (car (car methods))))
535 ;; Don't enter the article into the same group twice.
536 (not (assoc (car (car methods)) group-art)))
538 (cons (cons (car (car methods))
539 (funcall func (car (car methods))))
543 (list (cons (car (car methods))
544 (funcall func (car (car methods))))))))
545 (setq methods (cdr methods))))
546 (kill-buffer (current-buffer))
549 (defun nnmail-insert-lines ()
550 "Insert how many lines and chars there are in the body of the mail."
553 (goto-char (point-min))
554 (if (search-forward "\n\n" nil t)
556 (setq chars (- (point-max) (point)))
557 (setq lines (- (count-lines (point) (point-max)) 1))
560 (if (re-search-backward "^Lines: " nil t)
561 (delete-region (point) (progn (forward-line 1) (point)))))
562 (insert (format "Lines: %d\n" lines))
565 (defun nnmail-insert-xref (group-alist)
566 "Insert an Xref line based on the (group . article) alist."
568 (goto-char (point-min))
569 (if (search-forward "\n\n" nil t)
572 (if (re-search-backward "^Xref: " nil t)
573 (delete-region (match-beginning 0)
574 (progn (forward-line 1) (point))))
575 (insert (format "Xref: %s" (system-name)))
577 (insert (format " %s:%d" (car (car group-alist))
578 (cdr (car group-alist))))
579 (setq group-alist (cdr group-alist)))
582 ;; Written by byer@mv.us.adobe.com (Scott Byer).
583 (defun nnmail-make-complex-temp-name (prefix)
584 (let ((newname (make-temp-name prefix))
586 (while (file-exists-p newname)
587 (setq newprefix (concat newprefix "x"))
588 (setq newname (make-temp-name newprefix)))
591 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
593 (defun nnmail-split-fancy ()
594 "Fancy splitting method.
595 See the documentation for the variable `nnmail-split-fancy' for documentation."
596 (nnmail-split-it nnmail-split-fancy))
598 (defvar nnmail-split-cache nil)
599 ;; Alist of split expresions their equivalent regexps.
601 (defun nnmail-split-it (split)
602 ;; Return a list of groups matching SPLIT.
603 (cond ((stringp split)
607 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
610 (while (and (not done) (cdr split))
611 (setq split (cdr split)
612 done (nnmail-split-it (car split))))
613 done)) ((assq split nnmail-split-cache)
614 ;; A compiled match expression.
615 (goto-char (point-max))
616 (if (re-search-backward (cdr (assq split nnmail-split-cache)) nil t)
617 (nnmail-split-it (nth 2 split))))
619 ;; An uncompiled match.
620 (let* ((field (nth 0 split))
621 (value (nth 1 split))
622 (regexp (concat "^\\("
625 nnmail-split-abbrev-alist))
630 nnmail-split-abbrev-alist))
633 (setq nnmail-split-cache
634 (cons (cons split regexp) nnmail-split-cache))
635 (goto-char (point-max))
636 (if (re-search-backward regexp nil t)
637 (nnmail-split-it (nth 2 split)))))))
639 ;; Get a list of spool files to read.
640 (defun nnmail-get-spool-files (&optional group)
641 (if (null nnmail-spool-file)
642 ;; No spool file whatsoever.
645 ;; If procmail is used to get incoming mail, the files
646 ;; are stored in this directory.
647 (and (file-exists-p nnmail-procmail-directory)
649 nnmail-procmail-directory
650 t (concat (if group group "")
651 nnmail-procmail-suffix "$") t))))
652 (cond ((listp nnmail-spool-file)
653 (append nnmail-spool-file procmails))
654 ((stringp nnmail-spool-file)
655 (cons nnmail-spool-file procmails))
661 ;;; nnml.el ends here