1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news, mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl))
32 (require 'gnus) ; for macro gnus-kill-buffer, at least
36 (require 'mail-source)
40 (autoload 'gnus-add-buffer "gnus")
41 (autoload 'gnus-kill-buffer "gnus"))
44 "Reading mail with Gnus."
47 (defgroup nnmail-retrieve nil
48 "Retrieving new mail."
51 (defgroup nnmail-prepare nil
52 "Preparing (or mangling) new mail after retrieval."
55 (defgroup nnmail-duplicate nil
56 "Handling of duplicate mail messages."
59 (defgroup nnmail-split nil
60 "Organizing the incoming mail in folders."
63 (defgroup nnmail-files nil
68 (defgroup nnmail-expire nil
72 (defgroup nnmail-procmail nil
73 "Interfacing with procmail and other mail agents."
76 (defgroup nnmail-various nil
77 "Various mail options."
80 (defcustom nnmail-split-methods '(("mail.misc" ""))
81 "*Incoming mail will be split according to this variable.
83 If you'd like, for instance, one mail group for mail from the
84 \"4ad-l\" mailing list, one group for junk mail and one for everything
85 else, you could do something like this:
87 (setq nnmail-split-methods
88 '((\"mail.4ad\" \"From:.*4ad\")
89 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
90 (\"mail.misc\" \"\")))
92 As you can see, this variable is a list of lists, where the first
93 element in each \"rule\" is the name of the group (which, by the way,
94 does not have to be called anything beginning with \"mail\",
95 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
96 nnmail will try to match on the header to find a fit.
98 The second element can also be a function. In that case, it will be
99 called narrowed to the headers with the first element of the rule as
100 the argument. It should return a non-nil value if it thinks that the
101 mail belongs in that group.
103 The last element should always have \"\" as the regexp.
105 This variable can also have a function as its value."
107 :type '(choice (repeat :tag "Alist" (group (string :tag "Name")
108 (choice regexp function)))
109 (function-item nnmail-split-fancy)
110 (function :tag "Other")))
112 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
113 (defcustom nnmail-crosspost t
114 "If non-nil, do crossposting if several split methods match the mail.
115 If nil, the first match found will be used."
119 (defcustom nnmail-split-fancy-with-parent-ignore-groups nil
120 "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
121 This can also be a list of regexps."
124 :type '(choice (const :tag "none" nil)
126 (repeat :value (".*") regexp)))
128 (defcustom nnmail-cache-ignore-groups nil
129 "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
130 This can also be a list of regexps."
133 :type '(choice (const :tag "none" nil)
135 (repeat :value (".*") regexp)))
137 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
138 (defcustom nnmail-keep-last-article nil
139 "If non-nil, nnmail will never delete/move a group's last article.
140 It can be marked expirable, so it will be deleted when it is no longer last.
142 You may need to set this variable if other programs are putting
143 new mail into folder numbers that Gnus has marked as expired."
144 :group 'nnmail-procmail
145 :group 'nnmail-various
148 (defcustom nnmail-use-long-file-names nil
149 "If non-nil the mail backends will use long file and directory names.
150 If nil, groups like \"mail.misc\" will end up in directories like
155 (defcustom nnmail-default-file-modes 384
156 "Set the mode bits of all new mail files to this integer."
160 (defcustom nnmail-expiry-wait 7
161 "*Expirable articles that are older than this will be expired.
162 This variable can either be a number (which will be interpreted as a
163 number of days) -- this doesn't have to be an integer. This variable
164 can also be `immediate' and `never'."
165 :group 'nnmail-expire
166 :type '(choice (const immediate)
170 (defcustom nnmail-expiry-wait-function nil
171 "Variable that holds function to specify how old articles should be before they are expired.
172 The function will be called with the name of the group that the expiry
173 is to be performed in, and it should return an integer that says how
174 many days an article can be stored before it is considered \"old\".
175 It can also return the values `never' and `immediate'.
179 \(setq nnmail-expiry-wait-function
181 (cond ((string-match \"private\" newsgroup) 31)
182 ((string-match \"junk\" newsgroup) 1)
183 ((string-match \"important\" newsgroup) 'never)
185 :group 'nnmail-expire
186 :type '(choice (const :tag "nnmail-expiry-wait" nil)
187 (function :format "%v" nnmail-)))
189 (defcustom nnmail-expiry-target 'delete
190 "*Variable that says where expired messages should end up.
191 The default value is `delete' (which says to delete the messages),
192 but it can also be a string or a function. If it is a string, expired
193 messages end up in that group. If it is a function, the function is
194 called in a buffer narrowed to the message in question. The function
195 receives one argument, the name of the group the message comes from.
196 The return value should be `delete' or a group name (a string)."
198 :group 'nnmail-expire
199 :type '(choice (const delete)
200 (function :format "%v" nnmail-)
203 (defcustom nnmail-fancy-expiry-targets nil
204 "Determine expiry target based on articles using fancy techniques.
206 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
207 `nnmail-expiry-target' is set to the function
208 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
209 the message will be expired to a group determined by invoking
210 `format-time-string' with TARGET used as the format string and the
211 time extracted from the articles' Date header (if missing the current
214 In the special cases that HEADER is the symbol `to-from', the regexp
215 will try to match against both the From and the To header.
219 \(setq nnmail-fancy-expiry-targets
220 '((to-from \"boss\" \"nnfolder:Work\")
221 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
222 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
224 In this case, articles containing the string \"boss\" in the To or the
225 From header will be expired to the group \"nnfolder:Work\";
226 articles containing the sting \"IMPORTANT\" in the Subject header will
227 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
228 everything else will be expired to \"nnfolder:Archive-YYYY\"."
230 :group 'nnmail-expire
231 :type '(repeat (list (choice :tag "Match against"
232 (string :tag "Header")
235 (string :tag "Target group format string"))))
237 (defcustom nnmail-cache-accepted-message-ids nil
238 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
239 If non-nil, also update the cache when copy or move articles."
243 (defcustom nnmail-spool-file '((file))
244 "*Where the mail backends will look for incoming mail.
245 This variable is a list of mail source specifiers.
246 This variable is obsolete; `mail-sources' should be used instead."
250 (defcustom nnmail-resplit-incoming nil
251 "*If non-nil, re-split incoming procmail sorted mail."
252 :group 'nnmail-procmail
255 (defcustom nnmail-scan-directory-mail-source-once nil
256 "*If non-nil, scan all incoming procmail sorted mails once.
257 It scans low-level sorted spools even when not required."
259 :group 'nnmail-procmail
262 (defcustom nnmail-delete-file-function 'delete-file
263 "Function called to delete files in some mail backends."
267 (defcustom nnmail-crosspost-link-function
268 (if (string-match "windows-nt\\|emx" (symbol-name system-type))
271 "*Function called to create a copy of a file.
272 This is `add-name-to-file' by default, which means that crossposts
273 will use hard links. If your file system doesn't allow hard
274 links, you could set this variable to `copy-file' instead."
276 :type '(radio (function-item add-name-to-file)
277 (function-item copy-file)
278 (function :tag "Other")))
280 (defcustom nnmail-read-incoming-hook
281 (if (eq system-type 'windows-nt)
282 '(nnheader-ms-strip-cr)
284 "*Hook that will be run after the incoming mail has been transferred.
285 The incoming mail is moved from the specified spool file (which normally is
286 something like \"/usr/spool/mail/$user\") to the user's home
287 directory. This hook is called after the incoming mail box has been
288 emptied, and can be used to call any mail box programs you have
289 running (\"xwatch\", etc.)
293 \(add-hook 'nnmail-read-incoming-hook
295 (call-process \"/local/bin/mailsend\" nil nil nil
296 \"read\" nnmail-spool-file)))
298 If you have xwatch running, this will alert it that mail has been
301 If you use `display-time', you could use something like this:
303 \(add-hook 'nnmail-read-incoming-hook
305 ;; Update the displayed time, since that will clear out
306 ;; the flag that says you have mail.
307 (when (eq (process-status \"display-time\") 'run)
308 (display-time-filter display-time-process \"\"))))"
309 :group 'nnmail-prepare
312 (defcustom nnmail-prepare-incoming-hook nil
313 "Hook called before treating incoming mail.
314 The hook is run in a buffer with all the new, incoming mail."
315 :group 'nnmail-prepare
318 (defcustom nnmail-prepare-incoming-header-hook nil
319 "Hook called narrowed to the headers of each message.
320 This can be used to remove excessive spaces (and stuff like
321 that) from the headers before splitting and saving the messages."
322 :group 'nnmail-prepare
325 (defcustom nnmail-prepare-incoming-message-hook nil
326 "Hook called narrowed to each message."
327 :group 'nnmail-prepare
330 (defcustom nnmail-list-identifiers nil
331 "Regexp that matches list identifiers to be removed.
332 This can also be a list of regexps."
333 :group 'nnmail-prepare
334 :type '(choice (const :tag "none" nil)
336 (repeat :value (".*") regexp)))
338 (defcustom nnmail-pre-get-new-mail-hook nil
339 "Hook called just before starting to handle new incoming mail."
340 :group 'nnmail-retrieve
343 (defcustom nnmail-post-get-new-mail-hook nil
344 "Hook called just after finishing handling new incoming mail."
345 :group 'nnmail-retrieve
348 (defcustom nnmail-split-hook nil
349 "Hook called before deciding where to split an article.
350 The functions in this hook are free to modify the buffer
351 contents in any way they choose -- the buffer contents are
352 discarded after running the split process."
356 (defcustom nnmail-spool-hook nil
357 "*A hook called when a new article is spooled."
362 (defcustom nnmail-large-newsgroup 50
363 "*The number of articles which indicates a large newsgroup or nil.
364 If the number of articles is greater than the value, verbose
365 messages will be shown to indicate the current status."
366 :group 'nnmail-various
367 :type '(choice (const :tag "infinite" nil)
368 (number :tag "count")))
370 (define-widget 'nnmail-lazy 'default
371 "Base widget for recursive datastructures.
373 This is copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
375 :convert-widget 'widget-value-convert-widget
376 :value-create (lambda (widget)
377 (let ((value (widget-get widget :value))
378 (type (widget-get widget :type)))
379 (widget-put widget :children
380 (list (widget-create-child-value
381 widget (widget-convert type) value)))))
382 :value-delete 'widget-children-value-delete
383 :value-get (lambda (widget)
384 (widget-value (car (widget-get widget :children))))
385 :value-inline (lambda (widget)
386 (widget-apply (car (widget-get widget :children))
388 :default-get (lambda (widget)
390 (widget-convert (widget-get widget :type))))
391 :match (lambda (widget value)
392 (widget-apply (widget-convert (widget-get widget :type))
394 :validate (lambda (widget)
395 (widget-apply (car (widget-get widget :children)) :validate)))
397 (define-widget 'nnmail-split-fancy 'nnmail-lazy
398 "Widget for customizing splits in the variable of the same name."
400 :type '(menu-choice :value (any ".*value.*" "misc")
402 (string :tag "Destination")
403 (list :tag "Use first match (|)" :value (|)
405 (editable-list :inline t nnmail-split-fancy))
406 (list :tag "Use all matches (&)" :value (&)
408 (editable-list :inline t nnmail-split-fancy))
409 (list :tag "Function with fixed arguments (:)"
411 (const :format "" :value :)
413 (editable-list :inline t (sexp :tag "Arg"))
415 (list :tag "Function with split arguments (!)"
419 (editable-list :inline t nnmail-split-fancy))
420 (list :tag "Field match"
425 (symbol :value mail))
432 (const :tag "Junk (delete mail)" junk)))
434 (defcustom nnmail-split-fancy "mail.misc"
435 "Incoming mail can be split according to this fancy variable.
436 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
438 The format of this variable is SPLIT, where SPLIT can be one of
441 GROUP: Mail will be stored in GROUP (a string).
443 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
444 field FIELD (a regexp) contains VALUE (a regexp), store the messages
445 as specified by SPLIT. If RESTRICT (a regexp) matches some string
446 after FIELD and before the end of the matched VALUE, return nil,
447 otherwise process SPLIT. Multiple RESTRICTs add up, further
448 restricting the possibility of processing SPLIT.
450 \(| SPLIT...): Process each SPLIT expression until one of them matches.
451 A SPLIT expression is said to match if it will cause the mail
452 message to be stored in one or more groups.
454 \(& SPLIT...): Process each SPLIT expression.
456 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
457 the buffer containing the message headers. The return value FUNCTION
458 should be a split, which is then recursively processed.
460 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
461 return value FUNCTION should be a split, which is then recursively
464 junk: Mail will be deleted. Use with care! Do not submerge in water!
466 (setq nnmail-split-fancy
467 '(| (\"Subject\" \"MAKE MONEY FAST\" junk)
468 ...other.rules.omitted...))
470 FIELD must match a complete field name. VALUE must match a complete
471 word according to the `nnmail-split-fancy-syntax-table' syntax table.
472 You can use \".*\" in the regexps to match partial field names or words.
474 FIELD and VALUE can also be Lisp symbols, in that case they are expanded
475 as specified in `nnmail-split-abbrev-alist'.
477 GROUP can contain \\& and \\N which will substitute from matching
478 \\(\\) patterns in the previous VALUE.
482 \(setq nnmail-split-methods 'nnmail-split-fancy
484 ;; Messages from the mailer daemon are not crossposted to any of
485 ;; the ordinary groups. Warnings are put in a separate group
487 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
489 ;; Non-error messages are crossposted to all relevant
490 ;; groups, but we don't crosspost between the group for the
491 ;; (ding) list and the group for other (ding) related mail.
492 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
493 (\"subject\" \"ding\" \"ding.misc\"))
494 ;; Other mailing lists...
495 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
496 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
497 ;; Both lists below have the same suffix, so prevent
498 ;; cross-posting to mkpkg.list of messages posted only to
499 ;; the bugs- list, but allow cross-posting when the
500 ;; message was really cross-posted.
501 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
502 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
505 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
506 ;; Unmatched mail goes to the catch all group.
509 :type 'nnmail-split-fancy)
511 (defcustom nnmail-split-abbrev-alist
512 '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
513 (mail . "mailer-daemon\\|postmaster\\|uucp")
514 (to . "to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
515 (from . "from\\|sender\\|resent-from")
516 (nato . "to\\|cc\\|resent-to\\|resent-cc")
517 (naany . "from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
518 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
520 :type '(repeat (cons :format "%v" symbol regexp)))
522 (defcustom nnmail-message-id-cache-length 1000
523 "*The approximate number of Message-IDs nnmail will keep in its cache.
524 If this variable is nil, no checking on duplicate messages will be
526 :group 'nnmail-duplicate
527 :type '(choice (const :tag "disable" nil)
528 (integer :format "%v")))
530 (defcustom nnmail-message-id-cache-file "~/.nnmail-cache"
531 "*The file name of the nnmail Message-ID cache."
532 :group 'nnmail-duplicate
536 (defcustom nnmail-treat-duplicates 'warn
537 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
538 Three values are valid: nil, which means that nnmail is not to keep a
539 Message-ID cache; `warn', which means that nnmail should insert extra
540 headers to warn the user about the duplication (this is the default);
541 and `delete', which means that nnmail will delete duplicated mails.
543 This variable can also be a function. It will be called from a buffer
544 narrowed to the article in question with the Message-ID as a
545 parameter. It should return nil, `warn' or `delete'."
546 :group 'nnmail-duplicate
547 :type '(choice (const :tag "off" nil)
551 (defcustom nnmail-extra-headers '(To Newsgroups)
552 "*Extra headers to parse."
555 :type '(repeat symbol))
557 (defcustom nnmail-split-header-length-limit 2048
558 "Header lines longer than this limit are excluded from the split function."
563 (defcustom nnmail-mail-splitting-charset nil
564 "Default charset to be used when splitting incoming mail."
569 (defcustom nnmail-mail-splitting-decodes nil
570 "Whether the nnmail splitting functionality should MIME decode headers."
575 (defcustom nnmail-split-fancy-match-partial-words nil
576 "Whether to match partial words when fancy splitting.
577 Normally, regexes given in `nnmail-split-fancy' are implicitly surrounded
578 by \"\\=\\<...\\>\". If this variable is true, they are not implicitly\
585 (defcustom nnmail-split-lowercase-expanded t
586 "Whether to lowercase expanded entries (i.e. \\N) when splitting mails.
587 This avoids the creation of multiple groups when users send to an address
588 using different case (i.e. mailing-list@domain vs Mailing-List@Domain)."
593 ;;; Internal variables.
595 (defvar nnmail-article-buffer " *nnmail incoming*"
596 "The buffer used for splitting incoming mails.")
598 (defvar nnmail-split-history nil
599 "List of group/article elements that say where the previous split put messages.")
601 (defvar nnmail-split-fancy-syntax-table
602 (let ((table (make-syntax-table)))
603 ;; support the %-hack
604 (modify-syntax-entry ?\% "." table)
606 "Syntax table used by `nnmail-split-fancy'.")
608 (defvar nnmail-prepare-save-mail-hook nil
609 "Hook called before saving mail.")
611 (defvar nnmail-split-tracing nil)
612 (defvar nnmail-split-trace nil)
616 (defun nnmail-request-post (&optional server)
617 (mail-send-and-exit nil))
619 (defvar nnmail-file-coding-system 'raw-text
620 "Coding system used in nnmail.")
622 (defvar nnmail-incoming-coding-system
623 mm-text-coding-system
624 "Coding system used in reading inbox")
626 (defvar nnmail-pathname-coding-system nil
627 "*Coding system for file name.")
629 (defun nnmail-find-file (file)
630 "Insert FILE in server buffer safely."
631 (set-buffer nntp-server-buffer)
632 (delete-region (point-min) (point-max))
633 (let ((format-alist nil)
634 (after-insert-file-functions nil))
636 (let ((coding-system-for-read nnmail-file-coding-system)
637 (auto-mode-alist (mm-auto-mode-alist))
638 (file-name-coding-system nnmail-pathname-coding-system))
639 (insert-file-contents file)
643 (defun nnmail-group-pathname (group dir &optional file)
644 "Make file name for GROUP."
646 (let ((dir (file-name-as-directory (expand-file-name dir))))
647 (setq group (nnheader-replace-duplicate-chars-in-string
648 (nnheader-replace-chars-in-string group ?/ ?_)
650 (setq group (nnheader-translate-file-chars group))
651 ;; If this directory exists, we use it directly.
652 (file-name-as-directory
653 (if (or nnmail-use-long-file-names
654 (file-directory-p (concat dir group)))
655 (expand-file-name group dir)
656 ;; If not, we translate dots into slashes.
658 (mm-encode-coding-string
659 (nnheader-replace-chars-in-string group ?. ?/)
660 nnmail-pathname-coding-system)
664 (defun nnmail-get-active ()
665 "Returns an assoc of group names and active ranges.
666 nn*-request-list should have been called before calling this function."
667 ;; Go through all groups from the active list.
669 (set-buffer nntp-server-buffer)
670 (nnmail-parse-active)))
672 (defun nnmail-parse-active ()
673 "Parse the active file in the current buffer and return an alist."
674 (goto-char (point-min))
675 (unless (re-search-forward "[\\\"]" nil t)
676 (goto-char (point-max))
677 (while (re-search-backward "[][';?()#]" nil t)
679 (goto-char (point-min))
680 (let ((buffer (current-buffer))
681 group-assoc group max min)