Merge from emacs--devo--0
[gnus] / lisp / nnmail.el
1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news, mail
8
9 ;; This file is part of GNU Emacs.
10
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 3 of the License, or
14 ;; (at your option) any later version.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31
32 (eval-when-compile (require 'cl))
33
34 (require 'gnus)                         ; for macro gnus-kill-buffer, at least
35 (require 'nnheader)
36 (require 'message)
37 (require 'gnus-util)
38 (require 'mail-source)
39 (require 'mm-util)
40 (require 'gnus-int)
41
42 (autoload 'gnus-add-buffer "gnus")
43 (autoload 'gnus-kill-buffer "gnus")
44
45 (defgroup nnmail nil
46   "Reading mail with Gnus."
47   :group 'gnus)
48
49 (defgroup nnmail-retrieve nil
50   "Retrieving new mail."
51   :group 'nnmail)
52
53 (defgroup nnmail-prepare nil
54   "Preparing (or mangling) new mail after retrieval."
55   :group 'nnmail)
56
57 (defgroup nnmail-duplicate nil
58   "Handling of duplicate mail messages."
59   :group 'nnmail)
60
61 (defgroup nnmail-split nil
62   "Organizing the incoming mail in folders."
63   :group 'nnmail)
64
65 (defgroup nnmail-files nil
66   "Mail files."
67   :group 'gnus-files
68   :group 'nnmail)
69
70 (defgroup nnmail-expire nil
71   "Expiring old mail."
72   :group 'nnmail)
73
74 (defgroup nnmail-procmail nil
75   "Interfacing with procmail and other mail agents."
76   :group 'nnmail)
77
78 (defgroup nnmail-various nil
79   "Various mail options."
80   :group 'nnmail)
81
82 (defcustom nnmail-split-methods '(("mail.misc" ""))
83   "*Incoming mail will be split according to this variable.
84
85 If you'd like, for instance, one mail group for mail from the
86 \"4ad-l\" mailing list, one group for junk mail and one for everything
87 else, you could do something like this:
88
89  (setq nnmail-split-methods
90        '((\"mail.4ad\" \"From:.*4ad\")
91          (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
92          (\"mail.misc\" \"\")))
93
94 As you can see, this variable is a list of lists, where the first
95 element in each \"rule\" is the name of the group (which, by the way,
96 does not have to be called anything beginning with \"mail\",
97 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
98 nnmail will try to match on the header to find a fit.
99
100 The second element can also be a function.  In that case, it will be
101 called narrowed to the headers with the first element of the rule as
102 the argument.  It should return a non-nil value if it thinks that the
103 mail belongs in that group.
104
105 The last element should always have \"\" as the regexp.
106
107 This variable can also have a function as its value."
108   :group 'nnmail-split
109   :type '(choice (repeat :tag "Alist" (group (string :tag "Name")
110                                              (choice regexp function)))
111                  (function-item nnmail-split-fancy)
112                  (function :tag "Other")))
113
114 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
115 (defcustom nnmail-crosspost t
116   "If non-nil, do crossposting if several split methods match the mail.
117 If nil, the first match found will be used."
118   :group 'nnmail-split
119   :type 'boolean)
120
121 (defcustom nnmail-split-fancy-with-parent-ignore-groups nil
122   "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
123 This can also be a list of regexps."
124   :version "22.1"
125   :group 'nnmail-split
126   :type '(choice (const :tag "none" nil)
127                  (regexp :value ".*")
128                  (repeat :value (".*") regexp)))
129
130 (defcustom nnmail-cache-ignore-groups nil
131   "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
132 This can also be a list of regexps."
133   :version "22.1"
134   :group 'nnmail-split
135   :type '(choice (const :tag "none" nil)
136                  (regexp :value ".*")
137                  (repeat :value (".*") regexp)))
138
139 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
140 (defcustom nnmail-keep-last-article nil
141   "If non-nil, nnmail will never delete/move a group's last article.
142 It can be marked expirable, so it will be deleted when it is no longer last.
143
144 You may need to set this variable if other programs are putting
145 new mail into folder numbers that Gnus has marked as expired."
146   :group 'nnmail-procmail
147   :group 'nnmail-various
148   :type 'boolean)
149
150 (defcustom nnmail-use-long-file-names nil
151   "If non-nil the mail backends will use long file and directory names.
152 If nil, groups like \"mail.misc\" will end up in directories like
153 \"mail/misc/\"."
154   :group 'nnmail-files
155   :type 'boolean)
156
157 (defcustom nnmail-default-file-modes 384
158   "Set the mode bits of all new mail files to this integer."
159   :group 'nnmail-files
160   :type 'integer)
161
162 (defcustom nnmail-expiry-wait 7
163   "*Expirable articles that are older than this will be expired.
164 This variable can either be a number (which will be interpreted as a
165 number of days) -- this doesn't have to be an integer.  This variable
166 can also be `immediate' and `never'."
167   :group 'nnmail-expire
168   :type '(choice (const immediate)
169                  (number :tag "days")
170                  (const never)))
171
172 (defcustom nnmail-expiry-wait-function nil
173   "Variable that holds function to specify how old articles should be before they are expired.
174 The function will be called with the name of the group that the expiry
175 is to be performed in, and it should return an integer that says how
176 many days an article can be stored before it is considered \"old\".
177 It can also return the values `never' and `immediate'.
178
179 Eg.:
180
181 \(setq nnmail-expiry-wait-function
182       (lambda (newsgroup)
183         (cond ((string-match \"private\" newsgroup) 31)
184               ((string-match \"junk\" newsgroup) 1)
185               ((string-match \"important\" newsgroup) 'never)
186               (t 7))))"
187   :group 'nnmail-expire
188   :type '(choice (const :tag "nnmail-expiry-wait" nil)
189                  (function :format "%v" nnmail-)))
190
191 (defcustom nnmail-expiry-target 'delete
192   "*Variable that says where expired messages should end up.
193 The default value is `delete' (which says to delete the messages),
194 but it can also be a string or a function.  If it is a string, expired
195 messages end up in that group.  If it is a function, the function is
196 called in a buffer narrowed to the message in question.  The function
197 receives one argument, the name of the group the message comes from.
198 The return value should be `delete' or a group name (a string)."
199   :version "21.1"
200   :group 'nnmail-expire
201   :type '(choice (const delete)
202                  function
203                  string))
204
205 (defcustom nnmail-fancy-expiry-targets nil
206   "Determine expiry target based on articles using fancy techniques.
207
208 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries.  If
209 `nnmail-expiry-target' is set to the function
210 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
211 the message will be expired to a group determined by invoking
212 `format-time-string' with TARGET used as the format string and the
213 time extracted from the articles' Date header (if missing the current
214 time is used).
215
216 In the special cases that HEADER is the symbol `to-from', the regexp
217 will try to match against both the From and the To header.
218
219 Example:
220
221 \(setq nnmail-fancy-expiry-targets
222       '((to-from \"boss\" \"nnfolder:Work\")
223         (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
224         (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
225
226 In this case, articles containing the string \"boss\" in the To or the
227 From header will be expired to the group \"nnfolder:Work\";
228 articles containing the sting \"IMPORTANT\" in the Subject header will
229 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
230 everything else will be expired to \"nnfolder:Archive-YYYY\"."
231   :version "22.1"
232   :group 'nnmail-expire
233   :type '(repeat (list (choice :tag "Match against"
234                                (string :tag "Header")
235                                (const to-from))
236                        regexp
237                        (string :tag "Target group format string"))))
238
239 (defcustom nnmail-cache-accepted-message-ids nil
240   "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
241 If non-nil, also update the cache when copy or move articles."
242   :group 'nnmail
243   :type 'boolean)
244
245 (make-obsolete-variable 'nnmail-spool-file
246                         "This option is obsolete in Gnus 5.9.  \
247 Use `mail-sources' instead.")
248 ;; revision 5.29 / p0-85 / Gnus 5.9
249 ;; Variable removed in No Gnus v0.7
250
251 (defcustom nnmail-resplit-incoming nil
252   "*If non-nil, re-split incoming procmail sorted mail."
253   :group 'nnmail-procmail
254   :type 'boolean)
255
256 (defcustom nnmail-scan-directory-mail-source-once nil
257   "*If non-nil, scan all incoming procmail sorted mails once.
258 It scans low-level sorted spools even when not required."
259   :version "21.1"
260   :group 'nnmail-procmail
261   :type 'boolean)
262
263 (defcustom nnmail-delete-file-function 'delete-file
264   "Function called to delete files in some mail backends."
265   :group 'nnmail-files
266   :type 'function)
267
268 (defcustom nnmail-crosspost-link-function
269   (if (string-match "windows-nt\\|emx" (symbol-name system-type))
270       'copy-file
271     'add-name-to-file)
272   "*Function called to create a copy of a file.
273 This is `add-name-to-file' by default, which means that crossposts
274 will use hard links.  If your file system doesn't allow hard
275 links, you could set this variable to `copy-file' instead."
276   :group 'nnmail-files
277   :type '(radio (function-item add-name-to-file)
278                 (function-item copy-file)
279                 (function :tag "Other")))
280
281 (defcustom nnmail-read-incoming-hook
282   (if (eq system-type 'windows-nt)
283       '(nnheader-ms-strip-cr)
284     nil)
285   "*Hook that will be run after the incoming mail has been transferred.
286 The incoming mail is moved from the specified spool file (which normally is
287 something like \"/usr/spool/mail/$user\") to the user's home
288 directory.  This hook is called after the incoming mail box has been
289 emptied, and can be used to call any mail box programs you have
290 running (\"xwatch\", etc.)
291
292 Eg.
293
294 \(add-hook 'nnmail-read-incoming-hook
295           (lambda ()
296             (call-process \"/local/bin/mailsend\" nil nil nil
297                           \"read\"
298                           ;; The incoming mail box file.
299                           (expand-file-name (user-login-name)
300                                             rmail-spool-directory))))
301
302 If you have xwatch running, this will alert it that mail has been
303 read.
304
305 If you use `display-time', you could use something like this:
306
307 \(add-hook 'nnmail-read-incoming-hook
308           (lambda ()
309             ;; Update the displayed time, since that will clear out
310             ;; the flag that says you have mail.
311             (when (eq (process-status \"display-time\") 'run)
312               (display-time-filter display-time-process \"\"))))"
313   :group 'nnmail-prepare
314   :type 'hook)
315
316 (defcustom nnmail-prepare-incoming-hook nil
317   "Hook called before treating incoming mail.
318 The hook is run in a buffer with all the new, incoming mail."
319   :group 'nnmail-prepare
320   :type 'hook)
321
322 (defcustom nnmail-prepare-incoming-header-hook nil
323   "Hook called narrowed to the headers of each message.
324 This can be used to remove excessive spaces (and stuff like
325 that) from the headers before splitting and saving the messages."
326   :group 'nnmail-prepare
327   :type 'hook)
328
329 (defcustom nnmail-prepare-incoming-message-hook nil
330   "Hook called narrowed to each message."
331   :group 'nnmail-prepare
332   :type 'hook)
333
334 (defcustom nnmail-list-identifiers nil
335   "Regexp that matches list identifiers to be removed.
336 This can also be a list of regexps."
337   :group 'nnmail-prepare
338   :type '(choice (const :tag "none" nil)
339                  (regexp :value ".*")
340                  (repeat :value (".*") regexp)))
341
342 (defcustom nnmail-pre-get-new-mail-hook nil
343   "Hook called just before starting to handle new incoming mail."
344   :group 'nnmail-retrieve
345   :type 'hook)
346
347 (defcustom nnmail-post-get-new-mail-hook nil
348   "Hook called just after finishing handling new incoming mail."
349   :group 'nnmail-retrieve
350   :type 'hook)
351
352 (defcustom nnmail-split-hook nil
353   "Hook called before deciding where to split an article.
354 The functions in this hook are free to modify the buffer
355 contents in any way they choose -- the buffer contents are
356 discarded after running the split process."
357   :group 'nnmail-split
358   :type 'hook)
359
360 (defcustom nnmail-spool-hook nil
361   "*A hook called when a new article is spooled."
362   :version "22.1"
363   :group 'nnmail
364   :type 'hook)
365
366 (defcustom nnmail-large-newsgroup 50
367   "*The number of articles which indicates a large newsgroup or nil.