1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
3 ;; Copyright (C) 1995-2014 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl))
29 (require 'gnus) ; for macro gnus-kill-buffer, at least
33 (require 'mail-source)
37 (autoload 'gnus-add-buffer "gnus")
38 (autoload 'gnus-kill-buffer "gnus")
40 (autoload 'mail-send-and-exit "sendmail" nil t))
43 "Reading mail with Gnus."
46 (defgroup nnmail-retrieve nil
47 "Retrieving new mail."
50 (defgroup nnmail-prepare nil
51 "Preparing (or mangling) new mail after retrieval."
54 (defgroup nnmail-duplicate nil
55 "Handling of duplicate mail messages."
58 (defgroup nnmail-split nil
59 "Organizing the incoming mail in folders."
62 (defgroup nnmail-files nil
67 (defgroup nnmail-expire nil
71 (defgroup nnmail-procmail nil
72 "Interfacing with procmail and other mail agents."
75 (defgroup nnmail-various nil
76 "Various mail options."
79 (defcustom nnmail-split-methods '(("mail.misc" ""))
80 "*Incoming mail will be split according to this variable.
82 If you'd like, for instance, one mail group for mail from the
83 \"4ad-l\" mailing list, one group for junk mail and one for everything
84 else, you could do something like this:
86 (setq nnmail-split-methods
87 '((\"mail.4ad\" \"From:.*4ad\")
88 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
89 (\"mail.misc\" \"\")))
91 As you can see, this variable is a list of lists, where the first
92 element in each \"rule\" is the name of the group (which, by the way,
93 does not have to be called anything beginning with \"mail\",
94 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
95 nnmail will try to match on the header to find a fit.
97 The second element can also be a function. In that case, it will be
98 called narrowed to the headers with the first element of the rule as
99 the argument. It should return a non-nil value if it thinks that the
100 mail belongs in that group.
102 The last element should always have \"\" as the regexp.
104 This variable can also have a function as its value, and it can
105 also have a fancy split method as its value. See
106 `nnmail-split-fancy' for an explanation of that syntax."
108 :type '(choice (repeat :tag "Alist" (group (string :tag "Name")
109 (choice regexp function)))
110 (function-item nnmail-split-fancy)
111 (function :tag "Other")))
113 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
114 (defcustom nnmail-crosspost t
115 "If non-nil, do crossposting if several split methods match the mail.
116 If nil, the first match found will be used."
120 (defcustom nnmail-split-fancy-with-parent-ignore-groups nil
121 "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
122 This can also be a list of regexps."
125 :type '(choice (const :tag "none" nil)
127 (repeat :value (".*") regexp)))
129 (defcustom nnmail-cache-ignore-groups nil
130 "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
131 This can also be a list of regexps."
134 :type '(choice (const :tag "none" nil)
136 (repeat :value (".*") regexp)))
138 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
139 (defcustom nnmail-keep-last-article nil
140 "If non-nil, nnmail will never delete/move a group's last article.
141 It can be marked expirable, so it will be deleted when it is no longer last.
143 You may need to set this variable if other programs are putting
144 new mail into folder numbers that Gnus has marked as expired."
145 :group 'nnmail-procmail
146 :group 'nnmail-various
149 (defcustom nnmail-use-long-file-names nil
150 "If non-nil the mail backends will use long file and directory names.
151 If nil, groups like \"mail.misc\" will end up in directories like
156 (defcustom nnmail-default-file-modes 384
157 "Set the mode bits of all new mail files to this integer."
161 (defcustom nnmail-expiry-wait 7
162 "*Expirable articles that are older than this will be expired.
163 This variable can either be a number (which will be interpreted as a
164 number of days) -- this doesn't have to be an integer. This variable
165 can also be `immediate' and `never'."
166 :group 'nnmail-expire
167 :type '(choice (const immediate)
171 (defcustom nnmail-expiry-wait-function nil
172 "Variable that holds function to specify how old articles should be before they are expired.
173 The function will be called with the name of the group that the expiry
174 is to be performed in, and it should return an integer that says how
175 many days an article can be stored before it is considered \"old\".
176 It can also return the values `never' and `immediate'.
180 \(setq nnmail-expiry-wait-function
182 (cond ((string-match \"private\" newsgroup) 31)
183 ((string-match \"junk\" newsgroup) 1)
184 ((string-match \"important\" newsgroup) 'never)
186 :group 'nnmail-expire
187 :type '(choice (const :tag "nnmail-expiry-wait" nil)
188 (function :format "%v" nnmail-)))
190 (defcustom nnmail-expiry-target 'delete
191 "*Variable that says where expired messages should end up.
192 The default value is `delete' (which says to delete the messages),
193 but it can also be a string or a function. If it is a string, expired
194 messages end up in that group. If it is a function, the function is
195 called in a buffer narrowed to the message in question. The function
196 receives one argument, the name of the group the message comes from.
197 The return value should be `delete' or a group name (a string)."
199 :group 'nnmail-expire
200 :type '(choice (const delete)
204 (defcustom nnmail-fancy-expiry-targets nil
205 "Determine expiry target based on articles using fancy techniques.
207 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
208 `nnmail-expiry-target' is set to the function
209 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
210 the message will be expired to a group determined by invoking
211 `format-time-string' with TARGET used as the format string and the
212 time extracted from the articles' Date header (if missing the current
215 In the special cases that HEADER is the symbol `to-from', the regexp
216 will try to match against both the From and the To header.
220 \(setq nnmail-fancy-expiry-targets
221 '((to-from \"boss\" \"nnfolder:Work\")
222 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
223 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
225 In this case, articles containing the string \"boss\" in the To or the
226 From header will be expired to the group \"nnfolder:Work\";
227 articles containing the string \"IMPORTANT\" in the Subject header will
228 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
229 everything else will be expired to \"nnfolder:Archive-YYYY\"."
231 :group 'nnmail-expire
232 :type '(repeat (list (choice :tag "Match against"
233 (string :tag "Header")
236 (string :tag "Target group format string"))))
238 (defcustom nnmail-cache-accepted-message-ids nil
239 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
240 If non-nil, also update the cache when copy or move articles."
244 (make-obsolete-variable 'nnmail-spool-file 'mail-sources
245 "Gnus 5.9 (Emacs 22.1)")
246 ;; revision 5.29 / p0-85 / Gnus 5.9
247 ;; Variable removed in No Gnus v0.7
249 (defcustom nnmail-resplit-incoming nil
250 "*If non-nil, re-split incoming procmail sorted mail."
251 :group 'nnmail-procmail
254 (defcustom nnmail-scan-directory-mail-source-once nil
255 "*If non-nil, scan all incoming procmail sorted mails once.
256 It scans low-level sorted spools even when not required."
258 :group 'nnmail-procmail
261 (defcustom nnmail-delete-file-function 'delete-file
262 "Function called to delete files in some mail backends."
266 (defcustom nnmail-crosspost-link-function
267 (if (string-match "windows-nt" (symbol-name system-type))
270 "*Function called to create a copy of a file.
271 This is `add-name-to-file' by default, which means that crossposts
272 will use hard links. If your file system doesn't allow hard
273 links, you could set this variable to `copy-file' instead."
275 :type '(radio (function-item add-name-to-file)
276 (function-item copy-file)
277 (function :tag "Other")))
279 (defcustom nnmail-read-incoming-hook
280 (if (eq system-type 'windows-nt)
281 '(nnheader-ms-strip-cr)
283 "*Hook that will be run after the incoming mail has been transferred.
284 The incoming mail is moved from the specified spool file (which normally is
285 something like \"/usr/spool/mail/$user\") to the user's home
286 directory. This hook is called after the incoming mail box has been
287 emptied, and can be used to call any mail box programs you have
288 running (\"xwatch\", etc.)
292 \(add-hook 'nnmail-read-incoming-hook
294 (call-process \"/local/bin/mailsend\" nil nil nil
296 ;; The incoming mail box file.
297 (expand-file-name (user-login-name)
298 rmail-spool-directory))))
300 If you have xwatch running, this will alert it that mail has been
303 If you use `display-time', you could use something like this:
305 \(add-hook 'nnmail-read-incoming-hook
307 ;; Update the displayed time, since that will clear out
308 ;; the flag that says you have mail.
309 (when (eq (process-status \"display-time\") 'run)
310 (display-time-filter display-time-process \"\"))))"
311 :group 'nnmail-prepare
314 (defcustom nnmail-prepare-incoming-hook nil
315 "Hook called before treating incoming mail.
316 The hook is run in a buffer with all the new, incoming mail."
317 :group 'nnmail-prepare
320 (defcustom nnmail-prepare-incoming-header-hook nil
321 "Hook called narrowed to the headers of each message.
322 This can be used to remove excessive spaces (and stuff like
323 that) from the headers before splitting and saving the messages."
324 :group 'nnmail-prepare
327 (defcustom nnmail-prepare-incoming-message-hook nil
328 "Hook called narrowed to each message."
329 :group 'nnmail-prepare
332 (defcustom nnmail-list-identifiers nil
333 "Regexp that matches list identifiers to be removed.
334 This can also be a list of regexps."
335 :group 'nnmail-prepare
336 :type '(choice (const :tag "none" nil)
338 (repeat :value (".*") regexp)))
340 (defcustom nnmail-pre-get-new-mail-hook nil
341 "Hook called just before starting to handle new incoming mail."
342 :group 'nnmail-retrieve
345 (defcustom nnmail-post-get-new-mail-hook nil
346 "Hook called just after finishing handling new incoming mail."
347 :group 'nnmail-retrieve
350 (defcustom nnmail-split-hook nil
351 "Hook called before deciding where to split an article.
352 The functions in this hook are free to modify the buffer
353 contents in any way they choose -- the buffer contents are
354 discarded after running the split process."
358 (defcustom nnmail-spool-hook nil
359 "*A hook called when a new article is spooled."
364 (defcustom nnmail-large-newsgroup 50
365 "*The number of articles which indicates a large newsgroup or nil.
366 If the number of articles is greater than the value, verbose
367 messages will be shown to indicate the current status."
368 :group 'nnmail-various
369 :type '(choice (const :tag "infinite" nil)
370 (number :tag "count")))
372 (define-widget 'nnmail-lazy 'default
373 "Base widget for recursive data structures.
375 This is copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
377 :convert-widget 'widget-value-convert-widget
378 :value-create (lambda (widget)
379 (let ((value (widget-get widget :value))
380 (type (widget-get widget :type)))
381 (widget-put widget :children
382 (list (widget-create-child-value
383 widget (widget-convert type) value)))))
384 :value-delete 'widget-children-value-delete
385 :value-get (lambda (widget)
386 (widget-value (car (widget-get widget :children))))
387 :value-inline (lambda (widget)
388 (widget-apply (car (widget-get widget :children))
390 :default-get (lambda (widget)
392 (widget-convert (widget-get widget :type))))
393 :match (lambda (widget value)
394 (widget-apply (widget-convert (widget-get widget :type))
396 :validate (lambda (widget)
397 (widget-apply (car (widget-get widget :children)) :validate)))
399 (define-widget 'nnmail-split-fancy 'nnmail-lazy
400 "Widget for customizing splits in the variable of the same name."
402 :type '(menu-choice :value (any ".*value.*" "misc")
404 (string :tag "Destination")
405 (list :tag "Use first match (|)" :value (|)
407 (editable-list :inline t nnmail-split-fancy))
408 (list :tag "Use all matches (&)" :value (&)
410 (editable-list :inline t nnmail-split-fancy))
411 (list :tag "Function with fixed arguments (:)"
413 (const :format "" :value :)
415 (editable-list :inline t (sexp :tag "Arg"))
417 (list :tag "Function with split arguments (!)"
421 (editable-list :inline t nnmail-split-fancy))
422 (list :tag "Field match"
427 (symbol :value mail))
434 (const :tag "Junk (delete mail)" junk)))
436 (defcustom nnmail-split-fancy "mail.misc"
437 "Incoming mail can be split according to this fancy variable.
438 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
440 The format of this variable is SPLIT, where SPLIT can be one of
443 GROUP: Mail will be stored in GROUP (a string).
445 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
446 field FIELD (a regexp) contains VALUE (a regexp), store the messages
447 as specified by SPLIT. If RESTRICT (a regexp) matches some string
448 after FIELD and before the end of the matched VALUE, return nil,
449 otherwise process SPLIT. Multiple RESTRICTs add up, further
450 restricting the possibility of processing SPLIT.
452 \(| SPLIT...): Process each SPLIT expression until one of them matches.
453 A SPLIT expression is said to match if it will cause the mail
454 message to be stored in one or more groups.
456 \(& SPLIT...): Process each SPLIT expression.
458 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
459 the buffer containing the message headers. The return value FUNCTION
460 should be a split, which is then recursively processed.
462 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
463 return value FUNCTION should be a split, which is then recursively
466 junk: Mail will be deleted. Use with care! Do not submerge in water!
468 (setq nnmail-split-fancy
469 '(| (\"Subject\" \"MAKE MONEY FAST\" junk)
470 ...other.rules.omitted...))
472 FIELD must match a complete field name. VALUE must match a complete
473 word according to the `nnmail-split-fancy-syntax-table' syntax table.
474 You can use \".*\" in the regexps to match partial field names or words.
476 FIELD and VALUE can also be Lisp symbols, in that case they are expanded
477 as specified in `nnmail-split-abbrev-alist'.
479 GROUP can contain \\& and \\N which will substitute from matching
480 \\(\\) patterns in the previous VALUE.
484 \(setq nnmail-split-methods 'nnmail-split-fancy
486 ;; Messages from the mailer daemon are not crossposted to any of
487 ;; the ordinary groups. Warnings are put in a separate group
489 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
491 ;; Non-error messages are crossposted to all relevant
492 ;; groups, but we don't crosspost between the group for the
493 ;; (ding) list and the group for other (ding) related mail.
494 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
495 (\"subject\" \"ding\" \"ding.misc\"))
496 ;; Other mailing lists...
497 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
498 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
499 ;; Both lists below have the same suffix, so prevent
500 ;; cross-posting to mkpkg.list of messages posted only to
501 ;; the bugs- list, but allow cross-posting when the
502 ;; message was really cross-posted.
503 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
504 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
507 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
508 ;; Unmatched mail goes to the catch all group.
511 :type 'nnmail-split-fancy)
513 (defcustom nnmail-split-abbrev-alist
514 '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
515 (mail . "mailer-daemon\\|postmaster\\|uucp")
516 (to . "to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
517 (from . "from\\|sender\\|resent-from")
518 (nato . "to\\|cc\\|resent-to\\|resent-cc")
519 (naany . "from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
520 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
522 :type '(repeat (cons :format "%v" symbol regexp)))
524 (defcustom nnmail-message-id-cache-length 1000
525 "*The approximate number of Message-IDs nnmail will keep in its cache.
526 If this variable is nil, no checking on duplicate messages will be
528 :group 'nnmail-duplicate
529 :type '(choice (const :tag "disable" nil)
530 (integer :format "%v")))
532 (defcustom nnmail-message-id-cache-file
533 (nnheader-concat gnus-home-directory ".nnmail-cache")
534 "The file name of the nnmail Message-ID cache."
535 :group 'nnmail-duplicate
539 (defcustom nnmail-treat-duplicates 'warn
540 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
541 Three values are valid: nil, which means that nnmail is not to keep a
542 Message-ID cache; `warn', which means that nnmail should insert extra
543 headers to warn the user about the duplication (this is the default);
544 and `delete', which means that nnmail will delete duplicated mails.
546 This variable can also be a function. It will be called from a buffer
547 narrowed to the article in question with the Message-ID as a
548 parameter. It should return nil, `warn' or `delete'."
549 :group 'nnmail-duplicate
550 :type '(choice (const :tag "off" nil)
554 (defcustom nnmail-extra-headers '(To Newsgroups Cc)
555 "Extra headers to parse.
556 In addition to the standard headers, these extra headers will be
557 included in NOV headers (and the like) when backends parse headers."
560 :type '(repeat symbol))
562 (defcustom nnmail-split-header-length-limit 2048
563 "Header lines longer than this limit are excluded from the split function."
568 (defcustom nnmail-mail-splitting-charset nil
569 "Default charset to be used when splitting incoming mail."
574 (defcustom nnmail-mail-splitting-decodes nil
575 "Whether the nnmail splitting functionality should MIME decode headers."
580 (defcustom nnmail-split-fancy-match-partial-words nil
581 "Whether to match partial words when fancy splitting.
582 Normally, regexes given in `nnmail-split-fancy' are implicitly surrounded
583 by \"\\=\\<...\\>\". If this variable is true, they are not implicitly\
590 (defcustom nnmail-split-lowercase-expanded t
591 "Whether to lowercase expanded entries (i.e. \\N) when splitting mails.
592 This avoids the creation of multiple groups when users send to an address
593 using different case (i.e. mailing-list@domain vs Mailing-List@Domain)."
598 ;;; Internal variables.
600 (defvar nnmail-article-buffer " *nnmail incoming*"
601 "The buffer used for splitting incoming mails.")
603 (defvar nnmail-split-history nil
604 "List of group/article elements that say where the previous split put messages.")
606 (defvar nnmail-split-fancy-syntax-table
607 (let ((table (make-syntax-table)))
608 ;; support the %-hack
609 (modify-syntax-entry ?\% "." table)
611 "Syntax table used by `nnmail-split-fancy'.")
613 (defvar nnmail-prepare-save-mail-hook nil
614 "Hook called before saving mail.")
616 (defvar nnmail-split-tracing nil)
617 (defvar nnmail-split-trace nil)
618 (defvar nnmail-inhibit-default-split-group nil)
622 (defun nnmail-request-post (&optional server)
623 (mail-send-and-exit nil))
625 (defvar nnmail-file-coding-system 'raw-text
626 "Coding system used in nnmail.")
628 (defvar nnmail-incoming-coding-system
629 mm-text-coding-system
630 "Coding system used in reading inbox")
632 (defvar nnmail-pathname-coding-system
633 ;; This causes Emacs 22.2 and 22.3 to issue a useless warning.
634 ;;(if (and (featurep 'xemacs) (featurep 'file-coding))
635 (if (featurep 'xemacs)
636 (if (featurep 'file-coding)
637 ;; Work around a bug in many XEmacs 21.5 betas.
638 ;; Cf. http://thread.gmane.org/gmane.emacs.gnus.general/68134
639 (setq file-name-coding-system (coding-system-aliasee 'file-name))))
640 "*Coding system for file name.")
642 (defun nnmail-find-file (file)
643 "Insert FILE in server buffer safely."
644 (set-buffer nntp-server-buffer)
645 (delete-region (point-min) (point-max))
646 (let ((format-alist nil)
647 (after-insert-file-functions nil))
649 (let ((coding-system-for-read nnmail-file-coding-system)
650 (auto-mode-alist (mm-auto-mode-alist))
651 (file-name-coding-system nnmail-pathname-coding-system))
652 (insert-file-contents file)
656 (defun nnmail-group-pathname (group dir &optional file)
657 "Make file name for GROUP."
659 (let ((dir (file-name-as-directory (expand-file-name dir))))
660 (setq group (nnheader-replace-duplicate-chars-in-string
661 (nnheader-replace-chars-in-string group ?/ ?_)
663 (setq group (nnheader-translate-file-chars group))
664 ;; If this directory exists, we use it directly.
665 (file-name-as-directory
666 (if (or nnmail-use-long-file-names
667 (file-directory-p (concat dir group)))
668 (expand-file-name group dir)
669 ;; If not, we translate dots into slashes.
671 (nnheader-replace-chars-in-string group ?. ?/)
675 (defun nnmail-get-active ()
676 "Returns an assoc of group names and active ranges.
677 nn*-request-list should have been called before calling this function."
678 ;; Go through all groups from the active list.
679 (with-current-buffer nntp-server-buffer
680 (nnmail-parse-active)))
682 (defun nnmail-parse-active ()
683 "Parse the active file in the current buffer and return an alist."
684 (goto-char (point-min))
685 (unless (re-search-forward "[\\\"]" nil t)
686 (goto-char (point-max))
687 (while (re-search-backward "[][';?()#]" nil t)
689 (goto-char (point-min))
690 (let ((buffer (current-buffer))
691 group-assoc group max min)
695 (narrow-to-region (point) (point-at-eol))
696 (setq group (read buffer))
697 (unless (stringp group)
698 (setq group (symbol-name group)))
699 (if (and (numberp (setq max (read buffer)))
700 (numberp (setq min (read buffer))))
701 (push (list (mm-string-as-unibyte group) (cons min max))
708 (defvar nnmail-active-file-coding-system 'raw-text
709 "*Coding system for active file.")
711 (defun nnmail-save-active (group-assoc file-name)