Merge changes made in Gnus trunk.
[bpt/emacs.git] / lisp / gnus / nnmail.el
CommitLineData
eec82323 1;;; nnmail.el --- mail support functions for the Gnus mail backends
e84b4b86
TTN
2
3;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
114f9c96 4;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
eec82323 5
6748645f 6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
7;; Keywords: news, mail
8
9;; This file is part of GNU Emacs.
10
5e809f55 11;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 12;; it under the terms of the GNU General Public License as published by
5e809f55
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
eec82323
LMI
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
5e809f55 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
23
24;;; Commentary:
25
26;;; Code:
27
163a3c6a
GM
28;; For Emacs < 22.2.
29(eval-and-compile
30 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31
0df953b6
RS
32(eval-when-compile (require 'cl))
33
23f87bed 34(require 'gnus) ; for macro gnus-kill-buffer, at least
eec82323 35(require 'nnheader)
eec82323 36(require 'message)
6748645f 37(require 'gnus-util)
16409b0b
GM
38(require 'mail-source)
39(require 'mm-util)
163a3c6a 40(require 'gnus-int)
eec82323 41
8abf1b22
GM
42(autoload 'gnus-add-buffer "gnus")
43(autoload 'gnus-kill-buffer "gnus")
eec82323
LMI
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
e72c9bcc 54 "Preparing (or mangling) new mail after retrieval."
eec82323
LMI
55 :group 'nnmail)
56
57(defgroup nnmail-duplicate nil
58 "Handling of duplicate mail messages."
59 :group 'nnmail)
60
61(defgroup nnmail-split nil
8f688cb0 62 "Organizing the incoming mail in folders."
eec82323
LMI
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
23f87bed 82(defcustom nnmail-split-methods '(("mail.misc" ""))
6748645f 83 "*Incoming mail will be split according to this variable.
eec82323
LMI
84
85If 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
87else, you could do something like this:
88
89 (setq nnmail-split-methods
90 '((\"mail.4ad\" \"From:.*4ad\")
23f87bed
MB
91 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
92 (\"mail.misc\" \"\")))
eec82323
LMI
93
94As you can see, this variable is a list of lists, where the first
95element in each \"rule\" is the name of the group (which, by the way,
96does not have to be called anything beginning with \"mail\",
97\"yonka.zow\" is a fine, fine name), and the second is a regexp that
98nnmail will try to match on the header to find a fit.
99
100The second element can also be a function. In that case, it will be
101called narrowed to the headers with the first element of the rule as
102the argument. It should return a non-nil value if it thinks that the
103mail belongs in that group.
104
105The last element should always have \"\" as the regexp.
106
a3f57c41
G
107This variable can also have a function as its value, and it can
108also have a fancy split method as its value. See
109`nnmail-split-fancy' for an explanation of that syntax."
eec82323 110 :group 'nnmail-split
23f87bed
MB
111 :type '(choice (repeat :tag "Alist" (group (string :tag "Name")
112 (choice regexp function)))
eec82323
LMI
113 (function-item nnmail-split-fancy)
114 (function :tag "Other")))
115
116;; Suggested by Erik Selberg <speed@cs.washington.edu>.
117(defcustom nnmail-crosspost t
118 "If non-nil, do crossposting if several split methods match the mail.
119If nil, the first match found will be used."
120 :group 'nnmail-split
121 :type 'boolean)
122
23f87bed
MB
123(defcustom nnmail-split-fancy-with-parent-ignore-groups nil
124 "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
125This can also be a list of regexps."
bf247b6e 126 :version "22.1"
23f87bed
MB
127 :group 'nnmail-split
128 :type '(choice (const :tag "none" nil)
129 (regexp :value ".*")
130 (repeat :value (".*") regexp)))
131
132(defcustom nnmail-cache-ignore-groups nil
133 "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
134This can also be a list of regexps."
bf247b6e 135 :version "22.1"
23f87bed
MB
136 :group 'nnmail-split
137 :type '(choice (const :tag "none" nil)
138 (regexp :value ".*")
139 (repeat :value (".*") regexp)))
140
eec82323
LMI
141;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
142(defcustom nnmail-keep-last-article nil
a8151ef7
LMI
143 "If non-nil, nnmail will never delete/move a group's last article.
144It can be marked expirable, so it will be deleted when it is no longer last.
145
eec82323
LMI
146You may need to set this variable if other programs are putting
147new mail into folder numbers that Gnus has marked as expired."
148 :group 'nnmail-procmail
149 :group 'nnmail-various
150 :type 'boolean)
151
152(defcustom nnmail-use-long-file-names nil
153 "If non-nil the mail backends will use long file and directory names.
154If nil, groups like \"mail.misc\" will end up in directories like
155\"mail/misc/\"."
156 :group 'nnmail-files
157 :type 'boolean)
158
159(defcustom nnmail-default-file-modes 384
160 "Set the mode bits of all new mail files to this integer."
161 :group 'nnmail-files
162 :type 'integer)
163
164(defcustom nnmail-expiry-wait 7
165 "*Expirable articles that are older than this will be expired.
166This variable can either be a number (which will be interpreted as a
167number of days) -- this doesn't have to be an integer. This variable
168can also be `immediate' and `never'."
169 :group 'nnmail-expire
170 :type '(choice (const immediate)
23f87bed 171 (number :tag "days")
eec82323
LMI
172 (const never)))
173
174(defcustom nnmail-expiry-wait-function nil
175 "Variable that holds function to specify how old articles should be before they are expired.
23f87bed
MB
176The function will be called with the name of the group that the expiry
177is to be performed in, and it should return an integer that says how
178many days an article can be stored before it is considered \"old\".
179It can also return the values `never' and `immediate'.
eec82323
LMI
180
181Eg.:
182
183\(setq nnmail-expiry-wait-function
184 (lambda (newsgroup)
23f87bed
MB
185 (cond ((string-match \"private\" newsgroup) 31)
186 ((string-match \"junk\" newsgroup) 1)
eec82323
LMI
187 ((string-match \"important\" newsgroup) 'never)
188 (t 7))))"
189 :group 'nnmail-expire
190 :type '(choice (const :tag "nnmail-expiry-wait" nil)
191 (function :format "%v" nnmail-)))
192
16409b0b
GM
193(defcustom nnmail-expiry-target 'delete
194 "*Variable that says where expired messages should end up.
195The default value is `delete' (which says to delete the messages),
196but it can also be a string or a function. If it is a string, expired
197messages end up in that group. If it is a function, the function is
198called in a buffer narrowed to the message in question. The function
199receives one argument, the name of the group the message comes from.
200The return value should be `delete' or a group name (a string)."
4693ed4e 201 :version "21.1"
23f87bed
MB
202 :group 'nnmail-expire
203 :type '(choice (const delete)
9091f2d3 204 function
23f87bed
MB
205 string))
206
207(defcustom nnmail-fancy-expiry-targets nil
208 "Determine expiry target based on articles using fancy techniques.
209
210This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
211`nnmail-expiry-target' is set to the function
212`nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
213the message will be expired to a group determined by invoking
214`format-time-string' with TARGET used as the format string and the
215time extracted from the articles' Date header (if missing the current
216time is used).
217
218In the special cases that HEADER is the symbol `to-from', the regexp
219will try to match against both the From and the To header.
220
221Example:
222
223\(setq nnmail-fancy-expiry-targets
224 '((to-from \"boss\" \"nnfolder:Work\")
225 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
226 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
227
228In this case, articles containing the string \"boss\" in the To or the
229From header will be expired to the group \"nnfolder:Work\";
1d77b63e 230articles containing the string \"IMPORTANT\" in the Subject header will
23f87bed
MB
231be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
232everything else will be expired to \"nnfolder:Archive-YYYY\"."
bf247b6e 233 :version "22.1"
23f87bed
MB
234 :group 'nnmail-expire
235 :type '(repeat (list (choice :tag "Match against"
236 (string :tag "Header")
237 (const to-from))
238 regexp
239 (string :tag "Target group format string"))))
16409b0b 240
eec82323 241(defcustom nnmail-cache-accepted-message-ids nil
23f87bed
MB
242 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
243If non-nil, also update the cache when copy or move articles."
eec82323
LMI
244 :group 'nnmail
245 :type 'boolean)
246
265ac10b
SM
247(make-obsolete-variable 'nnmail-spool-file 'mail-sources
248 "Gnus 5.9 (Emacs 22.1)")
8aed9ac5 249;; revision 5.29 / p0-85 / Gnus 5.9
b890d447 250;; Variable removed in No Gnus v0.7
eec82323 251
16409b0b
GM
252(defcustom nnmail-resplit-incoming nil
253 "*If non-nil, re-split incoming procmail sorted mail."
eec82323
LMI
254 :group 'nnmail-procmail
255 :type 'boolean)
256
16409b0b
GM
257(defcustom nnmail-scan-directory-mail-source-once nil
258 "*If non-nil, scan all incoming procmail sorted mails once.
259It scans low-level sorted spools even when not required."
4693ed4e 260 :version "21.1"
eec82323
LMI
261 :group 'nnmail-procmail
262 :type 'boolean)
263
264(defcustom nnmail-delete-file-function 'delete-file
265 "Function called to delete files in some mail backends."
266 :group 'nnmail-files
267 :type 'function)
268
269(defcustom nnmail-crosspost-link-function
f5ec697d 270 (if (string-match "windows-nt" (symbol-name system-type))
eec82323
LMI
271 'copy-file
272 'add-name-to-file)
6748645f 273 "*Function called to create a copy of a file.
eec82323
LMI
274This is `add-name-to-file' by default, which means that crossposts
275will use hard links. If your file system doesn't allow hard
276links, you could set this variable to `copy-file' instead."
277 :group 'nnmail-files
278 :type '(radio (function-item add-name-to-file)
279 (function-item copy-file)
280 (function :tag "Other")))
281
eec82323
LMI
282(defcustom nnmail-read-incoming-hook
283 (if (eq system-type 'windows-nt)
284 '(nnheader-ms-strip-cr)
285 nil)
6748645f 286 "*Hook that will be run after the incoming mail has been transferred.
8b93df01 287The incoming mail is moved from the specified spool file (which normally is
eec82323
LMI
288something like \"/usr/spool/mail/$user\") to the user's home
289directory. This hook is called after the incoming mail box has been
290emptied, and can be used to call any mail box programs you have
291running (\"xwatch\", etc.)
292
293Eg.
294
295\(add-hook 'nnmail-read-incoming-hook
23f87bed
MB
296 (lambda ()
297 (call-process \"/local/bin/mailsend\" nil nil nil
01c52d31
MB
298 \"read\"
299 ;; The incoming mail box file.
300 (expand-file-name (user-login-name)
301 rmail-spool-directory))))
eec82323
LMI
302
303If you have xwatch running, this will alert it that mail has been
304read.
305
306If you use `display-time', you could use something like this:
307
308\(add-hook 'nnmail-read-incoming-hook
309 (lambda ()
310 ;; Update the displayed time, since that will clear out
311 ;; the flag that says you have mail.
312 (when (eq (process-status \"display-time\") 'run)
313 (display-time-filter display-time-process \"\"))))"
314 :group 'nnmail-prepare
315 :type 'hook)
316
eec82323
LMI
317(defcustom nnmail-prepare-incoming-hook nil
318 "Hook called before treating incoming mail.
319The hook is run in a buffer with all the new, incoming mail."
320 :group 'nnmail-prepare
321 :type 'hook)
322
323(defcustom nnmail-prepare-incoming-header-hook nil
324 "Hook called narrowed to the headers of each message.
325This can be used to remove excessive spaces (and stuff like
326that) from the headers before splitting and saving the messages."
327 :group 'nnmail-prepare
328 :type 'hook)
329
330(defcustom nnmail-prepare-incoming-message-hook nil
331 "Hook called narrowed to each message."
332 :group 'nnmail-prepare
333 :type 'hook)
334
335(defcustom nnmail-list-identifiers nil
336 "Regexp that matches list identifiers to be removed.
337This can also be a list of regexps."
338 :group 'nnmail-prepare
339 :type '(choice (const :tag "none" nil)
6748645f
LMI
340 (regexp :value ".*")
341 (repeat :value (".*") regexp)))
eec82323
LMI
342
343(defcustom nnmail-pre-get-new-mail-hook nil
344 "Hook called just before starting to handle new incoming mail."
345 :group 'nnmail-retrieve
346 :type 'hook)
347
348(defcustom nnmail-post-get-new-mail-hook nil
349 "Hook called just after finishing handling new incoming mail."
350 :group 'nnmail-retrieve
351 :type 'hook)
352
353(defcustom nnmail-split-hook nil
354 "Hook called before deciding where to split an article.
355The functions in this hook are free to modify the buffer
356contents in any way they choose -- the buffer contents are
357discarded after running the split process."
358 :group 'nnmail-split
359 :type 'hook)
360
23f87bed
MB
361(defcustom nnmail-spool-hook nil
362 "*A hook called when a new article is spooled."
bf247b6e 363 :version "22.1"
23f87bed
MB
364 :group 'nnmail
365 :type 'hook)
366
eec82323 367(defcustom nnmail-large-newsgroup 50
23f87bed
MB
368 "*The number of articles which indicates a large newsgroup or nil.
369If the number of articles is greater than the value, verbose
eec82323
LMI
370messages will be shown to indicate the current status."
371 :group 'nnmail-various
23f87bed
MB
372 :type '(choice (const :tag "infinite" nil)
373 (number :tag "count")))
374
375(define-widget 'nnmail-lazy 'default
376 "Base widget for recursive datastructures.
377
bf247b6e 378This is copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
23f87bed
MB
379 :format "%{%t%}: %v"
380 :convert-widget 'widget-value-convert-widget
381 :value-create (lambda (widget)
382 (let ((value (widget-get widget :value))
383 (type (widget-get widget :type)))
bf247b6e
KS
384 (widget-put widget :children
385 (list (widget-create-child-value
23f87bed
MB
386 widget (widget-convert type) value)))))
387 :value-delete 'widget-children-value-delete
388 :value-get (lambda (widget)
389 (widget-value (car (widget-get widget :children))))
390 :value-inline (lambda (widget)
391 (widget-apply (car (widget-get widget :children))
392 :value-inline))
393 :default-get (lambda (widget)
394 (widget-default-get
395 (widget-convert (widget-get widget :type))))
396 :match (lambda (widget value)
397 (widget-apply (widget-convert (widget-get widget :type))
398 :match value))
399 :validate (lambda (widget)
400 (widget-apply (car (widget-get widget :children)) :validate)))
401
402(define-widget 'nnmail-split-fancy 'nnmail-lazy
403 "Widget for customizing splits in the variable of the same name."
404 :tag "Split"
405 :type '(menu-choice :value (any ".*value.*" "misc")
406 :tag "Type"
407 (string :tag "Destination")
408 (list :tag "Use first match (|)" :value (|)
409 (const :format "" |)
410 (editable-list :inline t nnmail-split-fancy))
411 (list :tag "Use all matches (&)" :value (&)
412 (const :format "" &)
413 (editable-list :inline t nnmail-split-fancy))
414 (list :tag "Function with fixed arguments (:)"
01c52d31 415 :value (:)
23f87bed 416 (const :format "" :value :)
bf247b6e 417 function
23f87bed
MB
418 (editable-list :inline t (sexp :tag "Arg"))
419 )
420 (list :tag "Function with split arguments (!)"
01c52d31 421 :value (!)
23f87bed
MB
422 (const :format "" !)
423 function
424 (editable-list :inline t nnmail-split-fancy))
bf247b6e
KS
425 (list :tag "Field match"
426 (choice :tag "Field"
23f87bed
MB
427 regexp symbol)
428 (choice :tag "Match"
bf247b6e 429 regexp
23f87bed
MB
430 (symbol :value mail))
431 (repeat :inline t
432 :tag "Restrictions"
433 (group :inline t
434 (const :format "" -)
435 regexp))
436 nnmail-split-fancy)
437 (const :tag "Junk (delete mail)" junk)))
eec82323
LMI
438
439(defcustom nnmail-split-fancy "mail.misc"
440 "Incoming mail can be split according to this fancy variable.
441To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
442
6748645f 443The format of this variable is SPLIT, where SPLIT can be one of
eec82323
LMI
444the following:
445
446GROUP: Mail will be stored in GROUP (a string).
447
16409b0b 448\(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
a1506d29 449 field FIELD (a regexp) contains VALUE (a regexp), store the messages
16409b0b 450 as specified by SPLIT. If RESTRICT (a regexp) matches some string
f0529b5b 451 after FIELD and before the end of the matched VALUE, return nil,
16409b0b
GM
452 otherwise process SPLIT. Multiple RESTRICTs add up, further
453 restricting the possibility of processing SPLIT.
eec82323
LMI
454
455\(| SPLIT...): Process each SPLIT expression until one of them matches.
456 A SPLIT expression is said to match if it will cause the mail
457 message to be stored in one or more groups.
458
459\(& SPLIT...): Process each SPLIT expression.
460
461\(: FUNCTION optional args): Call FUNCTION with the optional args, in
462 the buffer containing the message headers. The return value FUNCTION
463 should be a split, which is then recursively processed.
464
16409b0b
GM
465\(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
466 return value FUNCTION should be a split, which is then recursively
467 processed.
468
23f87bed
MB
469junk: Mail will be deleted. Use with care! Do not submerge in water!
470 Example:
471 (setq nnmail-split-fancy
472 '(| (\"Subject\" \"MAKE MONEY FAST\" junk)
473 ...other.rules.omitted...))
474
eec82323
LMI
475FIELD must match a complete field name. VALUE must match a complete
476word according to the `nnmail-split-fancy-syntax-table' syntax table.
477You can use \".*\" in the regexps to match partial field names or words.
478
01c52d31 479FIELD and VALUE can also be Lisp symbols, in that case they are expanded
eec82323
LMI
480as specified in `nnmail-split-abbrev-alist'.
481
482GROUP can contain \\& and \\N which will substitute from matching
483\\(\\) patterns in the previous VALUE.
484
485Example:
486
487\(setq nnmail-split-methods 'nnmail-split-fancy
488 nnmail-split-fancy
489 ;; Messages from the mailer daemon are not crossposted to any of
490 ;; the ordinary groups. Warnings are put in a separate group
491 ;; from real errors.
492 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
493 \"mail.misc\"))
494 ;; Non-error messages are crossposted to all relevant
495 ;; groups, but we don't crosspost between the group for the
496 ;; (ding) list and the group for other (ding) related mail.
497 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
498 (\"subject\" \"ding\" \"ding.misc\"))
499 ;; Other mailing lists...
500 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
501 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
23f87bed
MB
502 ;; Both lists below have the same suffix, so prevent
503 ;; cross-posting to mkpkg.list of messages posted only to
504 ;; the bugs- list, but allow cross-posting when the
505 ;; message was really cross-posted.
506 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
507 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
508 ;;
eec82323
LMI
509 ;; People...
510 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
511 ;; Unmatched mail goes to the catch all group.
512 \"misc.misc\"))"
513 :group 'nnmail-split
23f87bed 514 :type 'nnmail-split-fancy)
eec82323
LMI
515
516(defcustom nnmail-split-abbrev-alist
517 '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
518 (mail . "mailer-daemon\\|postmaster\\|uucp")
519 (to . "to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
a8151ef7
LMI
520 (from . "from\\|sender\\|resent-from")
521 (nato . "to\\|cc\\|resent-to\\|resent-cc")
522 (naany . "from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
6748645f 523 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
eec82323
LMI
524 :group 'nnmail-split
525 :type '(repeat (cons :format "%v" symbol regexp)))
526
eec82323
LMI
527(defcustom nnmail-message-id-cache-length 1000
528 "*The approximate number of Message-IDs nnmail will keep in its cache.
529If this variable is nil, no checking on duplicate messages will be
530performed."
531 :group 'nnmail-duplicate
532 :type '(choice (const :tag "disable" nil)
533 (integer :format "%v")))
534
52bec650
MB
535(defcustom nnmail-message-id-cache-file
536 (nnheader-concat gnus-home-directory ".nnmail-cache")
537 "The file name of the nnmail Message-ID cache."
eec82323
LMI
538 :group 'nnmail-duplicate
539 :group 'nnmail-files
540 :type 'file)
541
542(defcustom nnmail-treat-duplicates 'warn
543 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
16409b0b 544Three values are valid: nil, which means that nnmail is not to keep a
eec82323
LMI
545Message-ID cache; `warn', which means that nnmail should insert extra
546headers to warn the user about the duplication (this is the default);
547and `delete', which means that nnmail will delete duplicated mails.
548
549This variable can also be a function. It will be called from a buffer
550narrowed to the article in question with the Message-ID as a
551parameter. It should return nil, `warn' or `delete'."
552 :group 'nnmail-duplicate
553 :type '(choice (const :tag "off" nil)
554 (const warn)
555 (const delete)))
556
23f87bed 557(defcustom nnmail-extra-headers '(To Newsgroups)
16409b0b 558 "*Extra headers to parse."
4693ed4e 559 :version "21.1"
16409b0b
GM
560 :group 'nnmail
561 :type '(repeat symbol))
562
ad136a7c 563(defcustom nnmail-split-header-length-limit 2048
16409b0b 564 "Header lines longer than this limit are excluded from the split function."
4693ed4e 565 :version "21.1"
16409b0b
GM
566 :group 'nnmail
567 :type 'integer)
568
23f87bed
MB
569(defcustom nnmail-mail-splitting-charset nil
570 "Default charset to be used when splitting incoming mail."
bf247b6e 571 :version "22.1"
23f87bed
MB
572 :group 'nnmail
573 :type 'symbol)
574
575(defcustom nnmail-mail-splitting-decodes nil
576 "Whether the nnmail splitting functionality should MIME decode headers."
bf247b6e 577 :version "22.1"
23f87bed
MB
578 :group 'nnmail
579 :type 'boolean)
580
581(defcustom nnmail-split-fancy-match-partial-words nil
582 "Whether to match partial words when fancy splitting.
583Normally, regexes given in `nnmail-split-fancy' are implicitly surrounded
584by \"\\=\\<...\\>\". If this variable is true, they are not implicitly\
585 surrounded
586by anything."
bf247b6e 587 :version "22.1"
23f87bed
MB
588 :group 'nnmail
589 :type 'boolean)
590
591(defcustom nnmail-split-lowercase-expanded t
592 "Whether to lowercase expanded entries (i.e. \\N) when splitting mails.
593This avoids the creation of multiple groups when users send to an address
594using different case (i.e. mailing-list@domain vs Mailing-List@Domain)."
bf247b6e 595 :version "22.1"
23f87bed
MB
596 :group 'nnmail
597 :type 'boolean)
598
eec82323
LMI
599;;; Internal variables.
600
23f87bed
MB
601(defvar nnmail-article-buffer " *nnmail incoming*"
602 "The buffer used for splitting incoming mails.")
603
eec82323
LMI
604(defvar nnmail-split-history nil
605 "List of group/article elements that say where the previous split put messages.")
606
23f87bed
MB
607(defvar nnmail-split-fancy-syntax-table
608 (let ((table (make-syntax-table)))
609 ;; support the %-hack
610 (modify-syntax-entry ?\% "." table)
611 table)
eec82323 612 "Syntax table used by `nnmail-split-fancy'.")
eec82323
LMI
613
614(defvar nnmail-prepare-save-mail-hook nil
615 "Hook called before saving mail.")
616
6748645f
LMI
617(defvar nnmail-split-tracing nil)
618(defvar nnmail-split-trace nil)
20a673b2 619(defvar nnmail-inhibit-default-split-group nil)
6748645f 620
eec82323
LMI
621\f
622
eec82323
LMI
623(defun nnmail-request-post (&optional server)
624 (mail-send-and-exit nil))
625
6748645f
LMI
626(defvar nnmail-file-coding-system 'raw-text
627 "Coding system used in nnmail.")
628
16409b0b
GM
629(defvar nnmail-incoming-coding-system
630 mm-text-coding-system
631 "Coding system used in reading inbox")
632
26b9f88d
MB
633(defvar nnmail-pathname-coding-system
634 ;; This causes Emacs 22.2 and 22.3 to issue a useless warning.
635 ;;(if (and (featurep 'xemacs) (featurep 'file-coding))
636 (if (featurep 'xemacs)
637 (if (featurep 'file-coding)
638 ;; Work around a bug in many XEmacs 21.5 betas.
639 ;; Cf. http://thread.gmane.org/gmane.emacs.gnus.general/68134
640 (setq file-name-coding-system (coding-system-aliasee 'file-name))))
35ef97a5 641 "*Coding system for file name.")
1613b43a 642
eec82323
LMI
643(defun nnmail-find-file (file)
644 "Insert FILE in server buffer safely."
645 (set-buffer nntp-server-buffer)
16409b0b 646 (delete-region (point-min) (point-max))
eec82323 647 (let ((format-alist nil)
23f87bed 648 (after-insert-file-functions nil))
eec82323 649 (condition-case ()
1613b43a 650 (let ((coding-system-for-read nnmail-file-coding-system)
16409b0b
GM
651 (auto-mode-alist (mm-auto-mode-alist))
652 (file-name-coding-system nnmail-pathname-coding-system))
1613b43a
KH
653 (insert-file-contents file)
654 t)
eec82323
LMI
655 (file-error nil))))
656
657(defun nnmail-group-pathname (group dir &optional file)
35ef97a5 658 "Make file name for GROUP."
eec82323
LMI
659 (concat
660 (let ((dir (file-name-as-directory (expand-file-name dir))))
16409b0b
GM
661 (setq group (nnheader-replace-duplicate-chars-in-string
662 (nnheader-replace-chars-in-string group ?/ ?_)
663 ?. ?_))
6748645f 664 (setq group (nnheader-translate-file-chars group))
eec82323 665 ;; If this directory exists, we use it directly.
16409b0b
GM
666 (file-name-as-directory
667 (if (or nnmail-use-long-file-names
668 (file-directory-p (concat dir group)))
669 (expand-file-name group dir)
670 ;; If not, we translate dots into slashes.
671 (expand-file-name
01c52d31 672 (nnheader-replace-chars-in-string group ?. ?/)
16409b0b 673 dir))))
eec82323
LMI
674 (or file "")))
675
eec82323
LMI
676(defun nnmail-get-active ()
677 "Returns an assoc of group names and active ranges.
678nn*-request-list should have been called before calling this function."
16409b0b 679 ;; Go through all groups from the active list.
20a673b2 680 (with-current-buffer nntp-server-buffer
16409b0b
GM
681 (nnmail-parse-active)))
682
683(defun nnmail-parse-active ()
684 "Parse the active file in the current buffer and return an alist."
685 (goto-char (point-min))
686 (unless (re-search-forward "[\\\"]" nil t)
687 (goto-char (point-max))
688 (while (re-search-backward "[][';?()#]" nil t)
689 (insert ?\\)))
690 (goto-char (point-min))
691 (let ((buffer (current-buffer))
692 group-assoc group max min)
693 (while (not (eobp))
694 (condition-case err
695 (progn
01c52d31 696 (narrow-to-region (point) (point-at-eol))
16409b0b
GM
697 (setq group (read buffer))
698 (unless (stringp group)
699 (setq group (symbol-name group)))
23f87bed
MB
700 (if (and (numberp (setq max (read buffer)))
701 (numberp (setq min (read buffer))))
1428d46b 702 (push (list (mm-string-as-unibyte group) (cons min max))
16409b0b
GM
703 group-assoc)))
704 (error nil))
705 (widen)
706 (forward-line 1))
eec82323
LMI
707 group-assoc))
708
16409b0b 709(defvar nnmail-active-file-coding-system 'raw-text
1613b43a
KH
710 "*Coding system for active file.")
711
eec82323
LMI
712(defun nnmail-save-active (group-assoc file-name)
713 "Save GROUP-ASSOC in ACTIVE-FILE."
1613b43a
KH
714 (let ((coding-system-for-write nnmail-active-file-coding-system))
715 (when file-name
16409b0b 716 (with-temp-file file-name
1428d46b 717 (mm-disable-multibyte)
1613b43a 718 (nnmail-generate-active group-assoc)))))
eec82323
LMI
719
720(defun nnmail-generate-active (alist)
721 "Generate an active file from group-alist ALIST."
722 (erase-buffer)
723 (let (group)
724 (while (setq group (pop alist))
16409b0b
GM
725 (insert (format "%S %d %d y\n" (intern (car group)) (cdadr group)
726 (caadr group))))
727 (goto-char (point-max))
728 (while (search-backward "\\." nil t)
729 (delete-char 1))))
eec82323 730
16409b0b 731(defun nnmail-get-split-group (file source)
eec82323 732 "Find out whether this FILE is to be split into GROUP only.
16409b0b
GM
733If SOURCE is a directory spec, try to return the group name component."
734 (if (eq (car source) 'directory)
735 (let ((file (file-name-nondirectory file)))
736 (mail-source-bind (directory source)
737 (if (string-match (concat (regexp-quote suffix) "$") file)
738 (substring file 0 (match-beginning 0))
739 nil)))
740 nil))
eec82323
LMI
741
742(defun nnmail-process-babyl-mail-format (func artnum-func)
743 (let ((case-fold-search t)
16409b0b 744 (count 0)
eec82323 745 start message-id content-length do-search end)
eec82323 746 (while (not (eobp))
6748645f 747 (goto-char (point-min))
eec82323
LMI
748 (re-search-forward
749 "\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
750 (goto-char (match-end 0))
751 (delete-region (match-beginning 0) (match-end 0))
752 (narrow-to-region
753 (setq start (point))
754 (progn
755 ;; Skip all the headers in case there are more "From "s...
756 (or (search-forward "\n\n" nil t)
757 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
758 (search-forward "\1f\f"))
759 (point)))
760 ;; Unquote the ">From " line, if any.
761 (goto-char (point-min))
762 (when (looking-at ">From ")
763 (replace-match "X-From-Line: ") )
764 (run-hooks 'nnmail-prepare-incoming-header-hook)
765 (goto-char (point-max))
766 ;; Find the Message-ID header.
767 (save-excursion
768 (if (re-search-backward
769 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t)
770 (setq message-id (buffer-substring (match-beginning 1)
771 (match-end 1)))
772 ;; There is no Message-ID here, so we create one.
773 (save-excursion
774 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
775 (beginning-of-line)
776 (insert "Original-")))
777 (forward-line -1)
778 (insert "Message-ID: " (setq message-id (nnmail-message-id))
779 "\n")))
780 ;; Look for a Content-Length header.
781 (if (not (save-excursion
782 (and (re-search-backward
783 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
e9bd5782 784 (setq content-length (string-to-number
eec82323
LMI
785 (buffer-substring
786 (match-beginning 1)
787 (match-end 1))))
788 ;; We destroy the header, since none of
789 ;; the backends ever use it, and we do not
790 ;; want to confuse other mailers by having
791 ;; a (possibly) faulty header.
792 (progn (insert "X-") t))))
793 (setq do-search t)
794 (widen)
795 (if (or (= (+ (point) content-length) (point-max))
796 (save-excursion
797 (goto-char (+ (point) content-length))
798 (looking-at "\1f")))
799 (progn
800 (goto-char (+ (point) content-length))
801 (setq do-search nil))
802 (setq do-search t)))
803 (widen)
804 ;; Go to the beginning of the next article - or to the end
805 ;; of the buffer.
806 (when do-search
807 (if (re-search-forward "^\1f" nil t)
808 (goto-char (match-beginning 0))
809 (goto-char (1- (point-max)))))
810 (delete-char 1) ; delete ^_
811 (save-excursion
812 (save-restriction
813 (narrow-to-region start (point))
814 (goto-char (point-min))
815 (nnmail-check-duplication message-id func artnum-func)
16409b0b 816 (incf count)
eec82323 817 (setq end (point-max))))
16409b0b
GM
818 (goto-char end))
819 count))
eec82323
LMI
820
821(defsubst nnmail-search-unix-mail-delim ()
822 "Put point at the beginning of the next Unix mbox message."
b96fb65c 823 ;; Algorithm used to find the next article in the
eec82323
LMI
824 ;; brain-dead Unix mbox format:
825 ;;
826 ;; 1) Search for "^From ".
827 ;; 2) If we find it, then see whether the previous
828 ;; line is blank and the next line looks like a header.
829 ;; Then it's possible that this is a mail delim, and we use it.
830 (let ((case-fold-search nil)
831 found)
832 (while (not found)
833 (if (not (re-search-forward "^From " nil t))
834 (setq found 'no)
835 (save-excursion
836 (beginning-of-line)
837 (when (and (or (bobp)
838 (save-excursion
839 (forward-line -1)
16409b0b 840 (eq (char-after) ?\n)))
eec82323
LMI
841 (save-excursion
842 (forward-line 1)
a8151ef7 843 (while (looking-at ">From \\|From ")
eec82323
LMI
844 (forward-line 1))
845 (looking-at "[^ \n\t:]+[ \n\t]*:")))
846 (setq found 'yes)))))
847 (beginning-of-line)
848 (eq found 'yes)))
849
850(defun nnmail-search-unix-mail-delim-backward ()
851 "Put point at the beginning of the current Unix mbox message."
b96fb65c 852 ;; Algorithm used to find the next article in the
eec82323
LMI
853 ;; brain-dead Unix mbox format:
854 ;;
855 ;; 1) Search for "^From ".
856 ;; 2) If we find it, then see whether the previous
857 ;; line is blank and the next line looks like a header.
858 ;; Then it's possible that this is a mail delim, and we use it.
859 (let ((case-fold-search nil)
860 found)
861 (while (not found)
862 (if (not (re-search-backward "^From " nil t))
863 (setq found 'no)
864 (save-excursion
865 (beginning-of-line)
866 (when (and (or (bobp)
867 (save-excursion
868 (forward-line -1)
16409b0b 869 (eq (char-after) ?\n)))
eec82323
LMI
870 (save-excursion
871 (forward-line 1)
a8151ef7 872 (while (looking-at ">From \\|From ")
eec82323
LMI
873 (forward-line 1))
874 (looking-at "[^ \n\t:]+[ \n\t]*:")))
875 (setq found 'yes)))))
876 (beginning-of-line)
877 (eq found 'yes)))
878
879(defun nnmail-process-unix-mail-format (func artnum-func)
880 (let ((case-fold-search t)
16409b0b 881 (count 0)
eec82323
LMI
882 start message-id content-length end skip head-end)
883 (goto-char (point-min))
884 (if (not (and (re-search-forward "^From " nil t)
885 (goto-char (match-beginning 0))))
886 ;; Possibly wrong format?
23f87bed
MB
887 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
888 (if (buffer-file-name) "file" "buffer")
889 (or (buffer-file-name) (buffer-name)))
eec82323
LMI
890 ;; Carry on until the bitter end.
891 (while (not (eobp))
892 (setq start (point)
893 end nil)
894 ;; Find the end of the head.
895 (narrow-to-region
896 start
897 (if (search-forward "\n\n" nil t)
898 (1- (point))
899 ;; This will never happen, but just to be on the safe side --
900 ;; if there is no head-body delimiter, we search a bit manually.
901 (while (and (looking-at "From \\|[^ \t]+:")
902 (not (eobp)))
903 (forward-line 1))
904 (point)))
905 ;; Find the Message-ID header.
906 (goto-char (point-min))
907 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
908 (setq message-id (match-string 1))
909 (save-excursion
910 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
911 (beginning-of-line)
912 (insert "Original-")))
913 ;; There is no Message-ID here, so we create one.
914 (forward-line 1)
915 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
916 ;; Look for a Content-Length header.
917 (goto-char (point-min))
918 (if (not (re-search-forward
919 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
920 (setq content-length nil)
e9bd5782 921 (setq content-length (string-to-number (match-string 1)))
eec82323
LMI
922 ;; We destroy the header, since none of the backends ever
923 ;; use it, and we do not want to confuse other mailers by
924 ;; having a (possibly) faulty header.
925 (beginning-of-line)
926 (insert "X-"))
927 (run-hooks 'nnmail-prepare-incoming-header-hook)
928 ;; Find the end of this article.
929 (goto-char (point-max))
930 (widen)
931 (setq head-end (point))
932 ;; We try the Content-Length value. The idea: skip over the header
933 ;; separator, then check what happens content-length bytes into the
8f688cb0 934 ;; message body. This should be either the end of the buffer, the
eec82323
LMI
935 ;; message separator or a blank line followed by the separator.
936 ;; The blank line should probably be deleted. If neither of the
937 ;; three is met, the content-length header is probably invalid.
938 (when content-length
939 (forward-line 1)
940 (setq skip (+ (point) content-length))
941 (goto-char skip)
942 (cond ((or (= skip (point-max))
943 (= (1+ skip) (point-max)))
944 (setq end (point-max)))
945 ((looking-at "From ")
946 (setq end skip))
947 ((looking-at "[ \t]*\n\\(From \\)")
948 (setq end (match-beginning 1)))
949 (t (setq end nil))))
950 (if end
951 (goto-char end)
952 ;; No Content-Length, so we find the beginning of the next
953 ;; article or the end of the buffer.
954 (goto-char head-end)
955 (or (nnmail-search-unix-mail-delim)
956 (goto-char (point-max))))
957 ;; Allow the backend to save the article.
958 (save-excursion
959 (save-restriction
960 (narrow-to-region start (point))
961 (goto-char (point-min))
16409b0b 962 (incf count)
eec82323
LMI
963 (nnmail-check-duplication message-id func artnum-func)
964 (setq end (point-max))))
16409b0b
GM
965 (goto-char end)))
966 count))
eec82323 967
b069e5a6 968(defun nnmail-process-mmdf-mail-format (func artnum-func &optional junk-func)
eec82323
LMI
969 (let ((delim "^\^A\^A\^A\^A$")
970 (case-fold-search t)
16409b0b 971 (count 0)
eec82323
LMI
972 start message-id end)
973 (goto-char (point-min))
974 (if (not (and (re-search-forward delim nil t)
975 (forward-line 1)))
976 ;; Possibly wrong format?
16409b0b 977 (error "Error, unknown mail format! (Possibly corrupted.)")
eec82323
LMI
978 ;; Carry on until the bitter end.
979 (while (not (eobp))
980 (setq start (point))
981 ;; Find the end of the head.
982 (narrow-to-region
983 start
984 (if (search-forward "\n\n" nil t)
985 (1- (point))
986 ;; This will never happen, but just to be on the safe side --
987 ;; if there is no head-body delimiter, we search a bit manually.
988 (while (and (looking-at "From \\|[^ \t]+:")
989 (not (eobp)))
990 (forward-line 1))
991 (point)))
992 ;; Find the Message-ID header.
993 (goto-char (point-min))
994 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
995 (setq message-id (match-string 1))
996 ;; There is no Message-ID here, so we create one.
997 (save-excursion
998 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
999 (beginning-of-line)
1000 (insert "Original-")))
1001 (forward-line 1)
1002 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
1003 (run-hooks 'nnmail-prepare-incoming-header-hook)
1004 ;; Find the end of this article.
1005 (goto-char (point-max))
1006 (widen)
1007 (if (re-search-forward delim nil t)
1008 (beginning-of-line)
1009 (goto-char (point-max)))
1010 ;; Allow the backend to save the article.
1011 (save-excursion
1012 (save-restriction
1013 (narrow-to-region start (point))
1014 (goto-char (point-min))
16409b0b 1015 (incf count)
b069e5a6 1016 (nnmail-check-duplication message-id func artnum-func junk-func)
eec82323
LMI
1017 (setq end (point-max))))
1018 (goto-char end)
16409b0b
GM
1019 (forward-line 2)))
1020 count))
1021
1022(defun nnmail-process-maildir-mail-format (func artnum-func)
1023 ;; In a maildir, every file contains exactly one mail.
1024 (let ((case-fold-search t)
1025 message-id)
1026 (goto-char (point-min))
1027 ;; Find the end of the head.
1028 (narrow-to-region
1029 (point-min)
1030 (if (search-forward "\n\n" nil t)
1031 (1- (point))
1032 ;; This will never happen, but just to be on the safe side --
1033 ;; if there is no head-body delimiter, we search a bit manually.
1034 (while (and (looking-at "From \\|[^ \t]+:")
1035 (not (eobp)))
1036 (forward-line 1))
1037 (point)))
1038 ;; Find the Message-ID header.
1039 (goto-char (point-min))
1040 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
1041 (setq message-id (match-string 1))
1042 ;; There is no Message-ID here, so we create one.
1043 (save-excursion
1044 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
1045 (beginning-of-line)
1046 (insert "Original-")))
1047 (forward-line 1)
1048 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
1049 (run-hooks 'nnmail-prepare-incoming-header-hook)
1050 ;; Allow the backend to save the article.
1051 (widen)
1052 (save-excursion
1053 (goto-char (point-min))
1054 (nnmail-check-duplication message-id func artnum-func))
1055 1))
eec82323 1056
01c52d31
MB
1057(defvar nnmail-group-names-not-encoded-p nil
1058 "Non-nil means group names are not encoded.")
1059
eec82323 1060(defun nnmail-split-incoming (incoming func &optional exit-func
b069e5a6 1061 group artnum-func junk-func)
eec82323 1062 "Go through the entire INCOMING file and pick out each individual mail.
20a673b2
KY
1063FUNC will be called with the buffer narrowed to each mail.
1064INCOMING can also be a buffer object. In that case, the mail
1065will be copied over from that buffer."
23f87bed 1066 (let ( ;; If this is a group-specific split, we bind the split
eec82323
LMI
1067 ;; methods to just this group.
1068 (nnmail-split-methods (if (and group
eec82323
LMI
1069 (not nnmail-resplit-incoming))
1070 (list (list group ""))
01c52d31
MB
1071 nnmail-split-methods))
1072 (nnmail-group-names-not-encoded-p t))
20a673b2
KY
1073 ;; Insert the incoming file.
1074 (with-current-buffer (get-buffer-create nnmail-article-buffer)
eec82323 1075 (erase-buffer)
20a673b2
KY
1076 (if (bufferp incoming)
1077 (insert-buffer-substring incoming)
1078 (let ((coding-system-for-read nnmail-incoming-coding-system))
1079 (mm-insert-file-contents incoming)))
16409b0b
GM
1080 (prog1
1081 (if (zerop (buffer-size))
1082 0
1083 (goto-char (point-min))
1084 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
1085 ;; Handle both babyl, MMDF and unix mail formats, since
1086 ;; movemail will use the former when fetching from a
1087 ;; mailbox, the latter when fetching from a file.
1088 (cond ((or (looking-at "\^L")
1089 (looking-at "BABYL OPTIONS:"))
1090 (nnmail-process-babyl-mail-format func artnum-func))
1091 ((looking-at "\^A\^A\^A\^A")
b069e5a6
G
1092 (nnmail-process-mmdf-mail-format
1093 func artnum-func junk-func))
16409b0b
GM
1094 ((looking-at "Return-Path:")
1095 (nnmail-process-maildir-mail-format func artnum-func))
1096 (t
1097 (nnmail-process-unix-mail-format func artnum-func))))
1098 (when exit-func
1099 (funcall exit-func))
1100 (kill-buffer (current-buffer))))))
eec82323 1101
b069e5a6 1102(defun nnmail-article-group (func &optional trace junk-func)
eec82323
LMI
1103 "Look at the headers and return an alist of groups that match.
1104FUNC will be called with the group name to determine the article number."
23f87bed 1105 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
eec82323 1106 (obuf (current-buffer))
23f87bed 1107 group-art method grp)
6748645f 1108 (if (and (sequencep methods)
20a673b2
KY
1109 (= (length methods) 1)
1110 (not nnmail-inhibit-default-split-group))
eec82323
LMI
1111 ;; If there is only just one group to put everything in, we
1112 ;; just return a list with just this one method in.
1113 (setq group-art
1114 (list (cons (caar methods) (funcall func (caar methods)))))
1115 ;; We do actual comparison.
20a673b2
KY
1116 ;; Copy the article into the work buffer.
1117 (with-current-buffer nntp-server-buffer
eec82323 1118 (erase-buffer)
23f87bed
MB
1119 (insert-buffer-substring obuf)
1120 ;; Narrow to headers.
1121 (narrow-to-region
1122 (goto-char (point-min))
1123 (if (search-forward "\n\n" nil t)
1124 (point)
1125 (point-max)))
1126 (goto-char (point-min))
1127 ;; Decode MIME headers and charsets.
1128 (when nnmail-mail-splitting-decodes
1129 (let ((mail-parse-charset nnmail-mail-splitting-charset))
1130 (mail-decode-encoded-word-region (point-min) (point-max))))
eec82323
LMI
1131 ;; Fold continuation lines.
1132 (goto-char (point-min))
1133 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1134 (replace-match " " t t))
6748645f
LMI
1135 ;; Nuke pathologically long headers. Since Gnus applies
1136 ;; pathologically complex regexps to the buffer, lines
1137 ;; that are looong will take longer than the Universe's
1138 ;; existence to process.
1139 (goto-char (point-min))
1140 (while (not (eobp))
16409b0b
GM
1141 (unless (< (move-to-column nnmail-split-header-length-limit)
1142 nnmail-split-header-length-limit)
01c52d31 1143 (delete-region (point) (point-at-eol)))
16409b0b 1144 (forward-line 1))
eec82323 1145 ;; Allow washing.
6748645f 1146 (goto-char (point-min))
eec82323 1147 (run-hooks 'nnmail-split-hook)
6748645f
LMI
1148 (when (setq nnmail-split-tracing trace)
1149 (setq nnmail-split-trace nil))
a3f57c41
G
1150 (if (or (and (symbolp nnmail-split-methods)
1151 (fboundp nnmail-split-methods))
1152 (and (listp nnmail-split-methods)
1153 ;; Not a regular split method, so it has to be a
1154 ;; fancy one.
1155 (not (let ((top-element (car-safe nnmail-split-methods)))
1156 (and (= 2 (length top-element))
1157 (stringp (nth 0 top-element))
1158 (stringp (nth 1 top-element)))))))
1159 (let* ((method-function
1160 (if (and (symbolp nnmail-split-methods)
1161 (fboundp nnmail-split-methods))
1162 nnmail-split-methods
1163 'nnmail-split-fancy))
1164 (split
1165 (condition-case error-info
1166 ;; `nnmail-split-methods' is a function, so we
1167 ;; just call this function here and use the
1168 ;; result.
1169 (or (funcall method-function)
1170 (and (not nnmail-inhibit-default-split-group)
1171 '("bogus")))
1172 (error
1173 (nnheader-message
1174 5 "Error in `nnmail-split-methods'; using `bogus' mail group: %S" error-info)
1175 (sit-for 1)
1176 '("bogus")))))
8753ddee 1177 (setq split (mm-delete-duplicates split))
a8151ef7
LMI
1178 ;; The article may be "cross-posted" to `junk'. What
1179 ;; to do? Just remove the `junk' spec. Don't really
1180 ;; see anything else to do...
b069e5a6
G
1181 (when (and (memq 'junk split)
1182 junk-func)
1183 (funcall junk-func 'junk))
1184 (setq split (delq 'junk split))
a8151ef7 1185 (when split
eec82323
LMI
1186 (setq group-art
1187 (mapcar
1188 (lambda (group) (cons group (funcall func group)))
1189 split))))
1190 ;; Go through the split methods to find a match.
6748645f
LMI
1191 (while (and methods
1192 (or nnmail-crosspost
1193 (not group-art)))
eec82323 1194 (goto-char (point-max))
6748645f 1195 (setq method (pop methods)
16409b0b 1196 grp (car method))
eec82323
LMI
1197 (if (or methods
1198 (not (equal "" (nth 1 method))))
1199 (when (and
1200 (ignore-errors
1201 (if (stringp (nth 1 method))
16409b0b
GM
1202 (let ((expand (string-match "\\\\[0-9&]" grp))
1203 (pos (re-search-backward (cadr method)
1204 nil t)))
1205 (and expand
1206 (setq grp (nnmail-expand-newtext grp)))
1207 pos)
eec82323 1208 ;; Function to say whether this is a match.
16409b0b 1209 (funcall (nth 1 method) grp)))
eec82323
LMI
1210 ;; Don't enter the article into the same
1211 ;; group twice.
16409b0b
GM
1212 (not (assoc grp group-art)))
1213 (push (cons grp (funcall func grp))
eec82323
LMI
1214 group-art))
1215 ;; This is the final group, which is used as a
1216 ;; catch-all.
20a673b2
KY
1217 (when (and (not group-art)
1218 (not nnmail-inhibit-default-split-group))
eec82323
LMI
1219 (setq group-art
1220 (list (cons (car method)
23f87bed
MB
1221 (funcall func (car method))))))))
1222 ;; Fall back on "bogus" if all else fails.
20a673b2
KY
1223 (when (and (not group-art)
1224 (not nnmail-inhibit-default-split-group))
23f87bed 1225 (setq group-art (list (cons "bogus" (funcall func "bogus"))))))
6748645f
LMI
1226 ;; Produce a trace if non-empty.
1227 (when (and trace nnmail-split-trace)
23f87bed 1228 (let ((restore (current-buffer)))
6748645f
LMI
1229 (nnheader-set-temp-buffer "*Split Trace*")
1230 (gnus-add-buffer)
23f87bed
MB
1231 (dolist (trace (nreverse nnmail-split-trace))
1232 (prin1 trace (current-buffer))
1233 (insert "\n"))
6748645f
LMI
1234 (goto-char (point-min))
1235 (gnus-configure-windows 'split-trace)
1236 (set-buffer restore)))
23f87bed 1237 (widen)
eec82323
LMI
1238 ;; See whether the split methods returned `junk'.
1239 (if (equal group-art '(junk))
1240 nil
a8151ef7
LMI
1241 ;; The article may be "cross-posted" to `junk'. What
1242 ;; to do? Just remove the `junk' spec. Don't really
1243 ;; see anything else to do...
1244 (let (elem)
1245 (while (setq elem (car (memq 'junk group-art)))
1246 (setq group-art (delq elem group-art)))
1247 (nreverse group-art)))))))
eec82323
LMI
1248
1249(defun nnmail-insert-lines ()
1250 "Insert how many lines there are in the body of the mail.
1251Return the number of characters in the body."
1252 (let (lines chars)
1253 (save-excursion
1254 (goto-char (point-min))
a1506d29 1255 (unless (search-forward "\n\n" nil t)
16409b0b
GM
1256 (goto-char (point-max))
1257 (insert "\n"))
1258 (setq chars (- (point-max) (point)))
1259 (setq lines (count-lines (point) (point-max)))
1260 (forward-char -1)
1261 (save-excursion
1262 (when (re-search-backward "^Lines: " nil t)
1263 (delete-region (point) (progn (forward-line 1) (point)))))
1264 (beginning-of-line)
1265 (insert (format "Lines: %d\n" (max lines 0)))
1266 chars)))
eec82323
LMI
1267
1268(defun nnmail-insert-xref (group-alist)
1269 "Insert an Xref line based on the (group . article) alist."
1270 (save-excursion
1271 (goto-char (point-min))
16409b0b
GM
1272 (unless (search-forward "\n\n" nil t)
1273 (goto-char (point-max))
1274 (insert "\n"))
1275 (forward-char -1)
1276 (when (re-search-backward "^Xref: " nil t)
1277 (delete-region (match-beginning 0)
1278 (progn (forward-line 1) (point))))
1279 (insert (format "Xref: %s" (system-name)))
1280 (while group-alist
01c52d31
MB
1281 (insert (if (mm-multibyte-p)
1282 (mm-string-as-multibyte
1283 (format " %s:%d" (caar group-alist) (cdar group-alist)))
1284 (mm-string-as-unibyte
1285 (format " %s:%d" (caar group-alist) (cdar group-alist)))))
16409b0b
GM
1286 (setq group-alist (cdr group-alist)))
1287 (insert "\n")))
eec82323
LMI
1288
1289;;; Message washing functions
1290
1291(defun nnmail-remove-leading-whitespace ()
1292 "Remove excessive whitespace from all headers."
1293 (goto-char (point-min))
1294 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1295 (replace-match "\\1" t)))
1296
1297(defun nnmail-remove-list-identifiers ()
1298 "Remove list identifiers from Subject headers."
23f87bed
MB
1299 (let ((regexp
1300 (if (consp nnmail-list-identifiers)
1301 (mapconcat 'identity nnmail-list-identifiers " *\\|")
1302 nnmail-list-identifiers)))
eec82323
LMI
1303 (when regexp
1304 (goto-char (point-min))
23f87bed
MB
1305 (while (re-search-forward
1306 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1307 nil t)
1308 (delete-region (match-beginning 2) (match-end 0))
1309 (beginning-of-line))
1310 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1311 nil t)
1312 (delete-region (match-beginning 1) (match-end 1))
1313 (beginning-of-line)))))
eec82323
LMI
1314
1315(defun nnmail-remove-tabs ()
1316 "Translate TAB characters into SPACE characters."
1317 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1318
01c52d31
MB
1319(defcustom nnmail-broken-references-mailers
1320 "^X-Mailer:.*\\(Eudora\\|Pegasus\\)"
1321 "Header line matching mailer producing bogus References lines.
1322See `nnmail-ignore-broken-references'."
1323 :group 'nnmail-prepare
330f707b 1324 :version "23.1" ;; No Gnus
01c52d31
MB
1325 :type 'regexp)
1326
1327(defun nnmail-ignore-broken-references ()
1328 "Ignore the References line and use In-Reply-To
1329
1330Eudora has a broken References line, but an OK In-Reply-To."
16409b0b 1331 (goto-char (point-min))
01c52d31 1332 (when (re-search-forward nnmail-broken-references-mailers nil t)
16409b0b
GM
1333 (goto-char (point-min))
1334 (when (re-search-forward "^References:" nil t)
1335 (beginning-of-line)
1336 (insert "X-Gnus-Broken-Eudora-"))
1337 (goto-char (point-min))
23f87bed
MB
1338 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1339 (replace-match "\\1" t))))
eec82323 1340
01c52d31 1341(defalias 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references)
265ac10b 1342(make-obsolete 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references "Emacs 23.1")
01c52d31 1343
16409b0b 1344(custom-add-option 'nnmail-prepare-incoming-header-hook
01c52d31 1345 'nnmail-ignore-broken-references)
eec82323 1346
16409b0b 1347;;; Utility functions
eec82323 1348
163a3c6a
GM
1349(declare-function gnus-activate-group "gnus-start"
1350 (group &optional scan dont-check method))
1351
23f87bed
MB
1352(defun nnmail-do-request-post (accept-func &optional server)
1353 "Utility function to directly post a message to an nnmail-derived group.
1354Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1355to actually put the message in the right group."
1356 (let ((success t))
1357 (dolist (mbx (message-unquote-tokens
1358 (message-tokenize-header
1359 (message-fetch-field "Newsgroups") ", ")) success)
1360 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1361 (or (gnus-active to-newsgroup)
1362 (gnus-activate-group to-newsgroup)
1363 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1364 to-newsgroup))
1365 (or (and (gnus-request-create-group
1366 to-newsgroup gnus-command-method)
1367 (gnus-activate-group to-newsgroup nil nil
1368 gnus-command-method))
1369 (error "Couldn't create group %s" to-newsgroup)))
1370 (error "No such group: %s" to-newsgroup))
1371 (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1372 (setq success nil))))))
1373
eec82323
LMI
1374(defun nnmail-split-fancy ()
1375 "Fancy splitting method.
23f87bed 1376See the documentation for the variable `nnmail-split-fancy' for details."
01c52d31
MB
1377 (with-syntax-table nnmail-split-fancy-syntax-table
1378 (nnmail-split-it nnmail-split-fancy)))
eec82323
LMI
1379
1380(defvar nnmail-split-cache nil)
1381;; Alist of split expressions their equivalent regexps.
1382
1383(defun nnmail-split-it (split)
1384 ;; Return a list of groups matching SPLIT.
6748645f
LMI
1385 (let (cached-pair)
1386 (cond
1387 ;; nil split
1388 ((null split)
1389 nil)
1390
1391 ;; A group name. Do the \& and \N subs into the string.
1392 ((stringp split)
1393 (when nnmail-split-tracing
23f87bed 1394 (push split nnmail-split-trace))
6748645f
LMI
1395 (list (nnmail-expand-newtext split)))
1396
1397 ;; Junk the message.
1398 ((eq split 'junk)
1399 (when nnmail-split-tracing
1400 (push "junk" nnmail-split-trace))
1401 (list 'junk))
1402
1403 ;; Builtin & operation.
1404 ((eq (car split) '&)
1405 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1406
1407 ;; Builtin | operation.
1408 ((eq (car split) '|)
1409 (let (done)
1410 (while (and (not done) (cdr split))
1411 (setq split (cdr split)
1412 done (nnmail-split-it (car split))))
1413 done))
1414
1415 ;; Builtin : operation.
1416 ((eq (car split) ':)
23f87bed
MB
1417 (when nnmail-split-tracing
1418 (push split nnmail-split-trace))
6748645f
LMI
1419 (nnmail-split-it (save-excursion (eval (cdr split)))))
1420
16409b0b
GM
1421 ;; Builtin ! operation.
1422 ((eq (car split) '!)
1423 (funcall (cadr split) (nnmail-split-it (caddr split))))
1424
6748645f
LMI
1425 ;; Check the cache for the regexp for this split.
1426 ((setq cached-pair (assq split nnmail-split-cache))
16409b0b
GM
1427 (let (split-result
1428 (end-point (point-max))
1429 (value (nth 1 split)))
1430 (if (symbolp value)
1431 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1432 (while (and (goto-char end-point)
1433 (re-search-backward (cdr cached-pair) nil t))
1434 (when nnmail-split-tracing
23f87bed 1435 (push split nnmail-split-trace))
16409b0b
GM
1436 (let ((split-rest (cddr split))
1437 (end (match-end 0))
23f87bed
MB
1438 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1439 ;; So, start-of-value is the point just before the
1440 ;; beginning of the value, whereas after-header-name
1441 ;; is the point just after the field name.
16409b0b
GM
1442 (start-of-value (match-end 1))
1443 (after-header-name (match-end 2)))
1444 ;; Start the next search just before the beginning of the
1445 ;; VALUE match.
1446 (setq end-point (1- start-of-value))
1447 ;; Handle - RESTRICTs
1448 (while (eq (car split-rest) '-)
1449 ;; RESTRICT must start after-header-name and
1450 ;; end after start-of-value, so that, for
1451 ;; (any "foo" - "x-foo" "foo.list")
1452 ;; we do not exclude foo.list just because
1453 ;; the header is: ``To: x-foo, foo''
1454 (goto-char end)
1455 (if (and (re-search-backward (cadr split-rest)
1456 after-header-name t)
1457 (> (match-end 0) start-of-value))
1458 (setq split-rest nil)
1459 (setq split-rest (cddr split-rest))))
1460 (when split-rest
1461 (goto-char end)
1462 (let ((value (nth 1 split)))
1463 (if (symbolp value)
1464 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1465 ;; Someone might want to do a \N sub on this match, so get the
1466 ;; correct match positions.
1467 (re-search-backward value start-of-value))
1468 (dolist (sp (nnmail-split-it (car split-rest)))
23f87bed 1469 (unless (member sp split-result)
16409b0b
GM
1470 (push sp split-result))))))
1471 split-result))
6748645f
LMI
1472
1473 ;; Not in cache, compute a regexp for the field/value pair.
1474 (t
61e66a15
MB
1475 (let ((field (nth 0 split))
1476 (value (nth 1 split))
1477 (split-rest (cddr split))
1478 partial-front
1479 partial-rear
1480 regexp)
16409b0b
GM
1481 (if (symbolp value)
1482 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1483 (if (and (>= (length value) 2)
1484 (string= ".*" (substring value 0 2)))
1485 (setq value (substring value 2)
23f87bed
MB
1486 partial-front ""))
1487 ;; Same trick for the rear of the regexp
1488 (if (and (>= (length value) 2)
1489 (string= ".*" (substring value -2)))
1490 (setq value (substring value 0 -2)
1491 partial-rear ""))
61e66a15
MB
1492 ;; Invert the match-partial-words behavior if the optional
1493 ;; last element is specified.
1494 (while (eq (car split-rest) '-)
1495 (setq split-rest (cddr split-rest)))
1496 (when (if (cadr split-rest)
1497 (not nnmail-split-fancy-match-partial-words)
1498 nnmail-split-fancy-match-partial-words)
23f87bed
MB
1499 (setq partial-front ""
1500 partial-rear ""))
16409b0b 1501 (setq regexp (concat "^\\(\\("
6748645f
LMI
1502 (if (symbolp field)
1503 (cdr (assq field nnmail-split-abbrev-alist))
1504 field)
16409b0b 1505 "\\):.*\\)"
23f87bed 1506 (or partial-front "\\<")
16409b0b
GM
1507 "\\("
1508 value
23f87bed
MB
1509 "\\)"
1510 (or partial-rear "\\>")))
6748645f
LMI
1511 (push (cons split regexp) nnmail-split-cache)
1512 ;; Now that it's in the cache, just call nnmail-split-it again
61e66a15 1513 ;; on the same split, which will find it immediately in the cache.
6748645f 1514 (nnmail-split-it split))))))
eec82323
LMI
1515
1516(defun nnmail-expand-newtext (newtext)
1517 (let ((len (length newtext))
1518 (pos 0)
1519 c expanded beg N did-expand)
1520 (while (< pos len)
1521 (setq beg pos)
1522 (while (and (< pos len)
1523 (not (= (aref newtext pos) ?\\)))
1524 (setq pos (1+ pos)))
1525 (unless (= beg pos)
1526 (push (substring newtext beg pos) expanded))
1527 (when (< pos len)
6748645f
LMI
1528 ;; We hit a \; expand it.
1529 (setq did-expand t
1530 pos (1+ pos)
1531 c (aref newtext pos))
eec82323
LMI
1532 (if (not (or (= c ?\&)
1533 (and (>= c ?1)
1534 (<= c ?9))))
6748645f 1535 ;; \ followed by some character we don't expand.
eec82323
LMI
1536 (push (char-to-string c) expanded)
1537 ;; \& or \N
1538 (if (= c ?\&)
1539 (setq N 0)
1540 (setq N (- c ?0)))
1541 (when (match-beginning N)
23f87bed
MB
1542 (push (if nnmail-split-lowercase-expanded
1543 (downcase (buffer-substring (match-beginning N)
1544 (match-end N)))
1545 (buffer-substring (match-beginning N) (match-end N)))
eec82323
LMI
1546 expanded))))
1547 (setq pos (1+ pos)))
1548 (if did-expand
1549 (apply 'concat (nreverse expanded))
1550 newtext)))
1551
eec82323
LMI
1552;; Activate a backend only if it isn't already activated.
1553;; If FORCE, re-read the active file even if the backend is
1554;; already activated.
1555(defun nnmail-activate (backend &optional force)
6748645f 1556 (nnheader-init-server-buffer)
eec82323
LMI
1557 (let (file timestamp file-time)
1558 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1559 force
1560 (and (setq file (ignore-errors
1561 (symbol-value (intern (format "%s-active-file"
1562 backend)))))
1563 (setq file-time (nth 5 (file-attributes file)))
1564 (or (not
1565 (setq timestamp
1566 (condition-case ()
1567 (symbol-value (intern
1568 (format "%s-active-timestamp"
1569 backend)))
1570 (error 'none))))
1571 (not (consp timestamp))
1572 (equal timestamp '(0 0))
1573 (> (nth 0 file-time) (nth 0 timestamp))
1574 (and (= (nth 0 file-time) (nth 0 timestamp))
1575 (> (nth 1 file-time) (nth 1 timestamp))))))
1576 (save-excursion
1577 (or (eq timestamp 'none)
1578 (set (intern (format "%s-active-timestamp" backend))
1579 file-time))
1580 (funcall (intern (format "%s-request-list" backend)))))
1581 t))
1582
1583(defun nnmail-message-id ()
1584 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1585
1586;;;
1587;;; nnmail duplicate handling
1588;;;
1589
1590(defvar nnmail-cache-buffer nil)
1591
1592(defun nnmail-cache-open ()
1593 (if (or (not nnmail-treat-duplicates)
1594 (and nnmail-cache-buffer
1595 (buffer-name nnmail-cache-buffer)))
1596 () ; The buffer is open.
20a673b2 1597 (with-current-buffer
eec82323 1598 (setq nnmail-cache-buffer
20a673b2 1599 (get-buffer-create " *nnmail message-id cache*"))
23f87bed 1600 (gnus-add-buffer)
eec82323
LMI
1601 (when (file-exists-p nnmail-message-id-cache-file)
1602 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1603 (set-buffer-modified-p nil)
1604 (current-buffer))))
1605
1606(defun nnmail-cache-close ()
1607 (when (and nnmail-cache-buffer
1608 nnmail-treat-duplicates
1609 (buffer-name nnmail-cache-buffer)
1610 (buffer-modified-p nnmail-cache-buffer))
20a673b2 1611 (with-current-buffer nnmail-cache-buffer
eec82323
LMI
1612 ;; Weed out the excess number of Message-IDs.
1613 (goto-char (point-max))
1614 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1615 (progn
1616 (beginning-of-line)
1617 (delete-region (point-min) (point))))
1618 ;; Save the buffer.
1619 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1620 (make-directory (file-name-directory nnmail-message-id-cache-file)
1621 t))
1622 (nnmail-write-region (point-min) (point-max)
1623 nnmail-message-id-cache-file nil 'silent)
1624 (set-buffer-modified-p nil)
1625 (setq nnmail-cache-buffer nil)
23f87bed 1626 (gnus-kill-buffer (current-buffer)))))
eec82323 1627
23f87bed
MB
1628(defun nnmail-cache-insert (id grp &optional subject sender)
1629 (when (stringp id)
1630 ;; this will handle cases like `B r' where the group is nil
1631 (let ((grp (or grp gnus-newsgroup-name "UNKNOWN")))
bf247b6e 1632 (run-hook-with-args 'nnmail-spool-hook
23f87bed
MB
1633 id grp subject sender))
1634 (when nnmail-treat-duplicates
1635 ;; Store some information about the group this message is written
1636 ;; to. This is passed in as the grp argument -- all locations this
1637 ;; has been called from have been checked and the group is available.
1638 ;; The only ambiguous case is nnmail-check-duplication which will only
1639 ;; pass the first (of possibly >1) group which matches. -Josh
16409b0b 1640 (unless (gnus-buffer-live-p nnmail-cache-buffer)
23f87bed 1641 (nnmail-cache-open))
20a673b2 1642 (with-current-buffer nnmail-cache-buffer
23f87bed
MB
1643 (goto-char (point-max))
1644 (if (and grp (not (string= "" grp))
1645 (gnus-methods-equal-p gnus-command-method
1646 (nnmail-cache-primary-mail-backend)))
1647 (let ((regexp (if (consp nnmail-cache-ignore-groups)
1648 (mapconcat 'identity nnmail-cache-ignore-groups
1649 "\\|")
1650 nnmail-cache-ignore-groups)))
1651 (unless (and regexp (string-match regexp grp))
1652 (insert id "\t" grp "\n")))
1653 (insert id "\n"))))))
bf247b6e 1654
16409b0b
GM
1655(defun nnmail-cache-primary-mail-backend ()
1656 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
23f87bed
MB
1657 (be nil)
1658 (res nil)
1659 (get-new-mail nil))
16409b0b
GM
1660 (while (and (null res) be-list)
1661 (setq be (car be-list))
1662 (setq be-list (cdr be-list))
1663 (when (and (gnus-method-option-p be 'respool)
23f87bed
MB
1664 (setq get-new-mail
1665 (intern (format "%s-get-new-mail" (car be))))
1666 (boundp get-new-mail)
1667 (symbol-value get-new-mail))
1668 (setq res be)))
16409b0b
GM
1669 res))
1670
1671;; Fetch the group name corresponding to the message id stored in the
1672;; cache.
1673(defun nnmail-cache-fetch-group (id)
1674 (when (and nnmail-treat-duplicates nnmail-cache-buffer)
20a673b2 1675 (with-current-buffer nnmail-cache-buffer
eec82323 1676 (goto-char (point-max))
16409b0b 1677 (when (search-backward id nil t)
23f87bed
MB
1678 (beginning-of-line)
1679 (skip-chars-forward "^\n\r\t")
1680 (unless (looking-at "[\r\n]")
1681 (forward-char 1)
01c52d31 1682 (buffer-substring (point) (point-at-eol)))))))
16409b0b
GM
1683
1684;; Function for nnmail-split-fancy: look up all references in the
1685;; cache and if a match is found, return that group.
1686(defun nnmail-split-fancy-with-parent ()
23f87bed
MB
1687 "Split this message into the same group as its parent.
1688This function can be used as an entry in `nnmail-split-fancy', for
1689example like this: (: nnmail-split-fancy-with-parent)
1690For a message to be split, it looks for the parent message in the
1691References or In-Reply-To header and then looks in the message id
1692cache file (given by the variable `nnmail-message-id-cache-file') to
1693see which group that message was put in. This group is returned.
1694
1695See the Info node `(gnus)Fancy Mail Splitting' for more details."
16409b0b 1696 (let* ((refstr (or (message-fetch-field "references")
23f87bed
MB
1697 (message-fetch-field "in-reply-to")))
1698 (references nil)
1699 (res nil)
1700 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1701 (mapconcat
1702 (lambda (x) (format "\\(%s\\)" x))
1703 nnmail-split-fancy-with-parent-ignore-groups
1704 "\\|")
1705 nnmail-split-fancy-with-parent-ignore-groups)))
16409b0b
GM
1706 (when refstr
1707 (setq references (nreverse (gnus-split-references refstr)))
1708 (unless (gnus-buffer-live-p nnmail-cache-buffer)
23f87bed 1709 (nnmail-cache-open))
01c52d31
MB
1710 (dolist (x references)
1711 (setq res (or (nnmail-cache-fetch-group x) res))
1712 (when (or (member res '("delayed" "drafts" "queue"))
1713 (and regexp res (string-match regexp res)))
1714 (setq res nil)))
16409b0b 1715 res)))
eec82323
LMI
1716
1717(defun nnmail-cache-id-exists-p (id)
1718 (when nnmail-treat-duplicates
20a673b2 1719 (with-current-buffer nnmail-cache-buffer
eec82323
LMI
1720 (goto-char (point-max))
1721 (search-backward id nil t))))
1722
1723(defun nnmail-fetch-field (header)
1724 (save-excursion
1725 (save-restriction
1726 (message-narrow-to-head)
1727 (message-fetch-field header))))
1728
b069e5a6
G
1729(defun nnmail-check-duplication (message-id func artnum-func
1730 &optional junk-func)
eec82323
LMI
1731 (run-hooks 'nnmail-prepare-incoming-message-hook)
1732 ;; If this is a duplicate message, then we do not save it.
1733 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1734 (case-fold-search t)
1735 (action (when duplication
1736 (cond
1737 ((memq nnmail-treat-duplicates '(warn delete))
1738 nnmail-treat-duplicates)
23f87bed 1739 ((functionp nnmail-treat-duplicates)
eec82323
LMI
1740 (funcall nnmail-treat-duplicates message-id))
1741 (t
1742 nnmail-treat-duplicates))))
1743 group-art)
16409b0b
GM
1744 ;; We insert a line that says what the mail source is.
1745 (let ((case-fold-search t))
1746 (goto-char (point-min))
1747 (re-search-forward "^message-id[ \t]*:" nil t)
1748 (beginning-of-line)
1749 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1750
eec82323
LMI
1751 ;; Let the backend save the article (or not).
1752 (cond
1753 ((not duplication)
eec82323 1754 (funcall func (setq group-art
b069e5a6
G
1755 (nreverse (nnmail-article-group
1756 artnum-func nil junk-func))))
23f87bed 1757 (nnmail-cache-insert message-id (caar group-art)))
eec82323
LMI
1758 ((eq action 'delete)
1759 (setq group-art nil))
1760 ((eq action 'warn)
1761 ;; We insert a warning.
1762 (let ((case-fold-search t))
1763 (goto-char (point-min))
1764 (re-search-forward "^message-id[ \t]*:" nil t)
1765 (beginning-of-line)
1766 (insert
1767 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1768 (funcall func (setq group-art
1769 (nreverse (nnmail-article-group artnum-func))))))
1770 (t
1771 (funcall func (setq group-art
1772 (nreverse (nnmail-article-group artnum-func))))))
1773 ;; Add the group-art list to the history list.
1774 (if group-art
1775 (push group-art nnmail-split-history)
1776 (delete-region (point-min) (point-max)))))
1777
1778;;; Get new mail.
1779
16409b0b
GM
1780(defvar nnmail-fetched-sources nil)
1781
eec82323
LMI
1782(defun nnmail-get-value (&rest args)
1783 (let ((sym (intern (apply 'format args))))
1784 (when (boundp sym)
1785 (symbol-value sym))))
1786
1787(defun nnmail-get-new-mail (method exit-func temp
a1da1e37 1788 &optional group spool-func)
eec82323 1789 "Read new incoming mail."
a1da1e37
MB
1790 (nnmail-get-new-mail-1 method exit-func temp group nil spool-func))
1791
1792(defun nnmail-get-new-mail-1 (method exit-func temp
1793 group in-group spool-func)
b890d447 1794 (let* ((sources mail-sources)
16409b0b 1795 fetching-sources
16409b0b
GM
1796 (i 0)
1797 (new 0)
1798 (total 0)
1799 incoming incomings source)
8b93df01
DL
1800 (when (and (nnmail-get-value "%s-get-new-mail" method)
1801 sources)
16409b0b 1802 (while (setq source (pop sources))
a1da1e37
MB
1803 ;; Use group's parameter
1804 (when (eq (car source) 'group)
1805 (let ((mail-sources
1806 (list
1807 (gnus-group-find-parameter
1808 (concat (symbol-name method) ":" group)
1809 'mail-source t))))
1810 (nnmail-get-new-mail-1 method exit-func temp
1811 group group spool-func))
1812 (setq source nil))
16409b0b
GM
1813 ;; Hack to only fetch the contents of a single group's spool file.
1814 (when (and (eq (car source) 'directory)
1815 (null nnmail-scan-directory-mail-source-once)
1816 group)
1817 (mail-source-bind (directory source)
1818 (setq source (append source
1819 (list
1820 :predicate
23f87bed
MB
1821 (gnus-byte-compile
1822 `(lambda (file)
1823 (string-equal
1824 ,(concat group suffix)
1825 (file-name-nondirectory file)))))))))
16409b0b
GM
1826 (when nnmail-fetched-sources
1827 (if (member source nnmail-fetched-sources)
1828 (setq source nil)
1829 (push source nnmail-fetched-sources)
1830 (push source fetching-sources)))))
1831 (when fetching-sources
eec82323
LMI
1832 ;; We first activate all the groups.
1833 (nnmail-activate method)
1834 ;; Allow the user to hook.
1835 (run-hooks 'nnmail-pre-get-new-mail-hook)
1836 ;; Open the message-id cache.
1837 (nnmail-cache-open)
16409b0b
GM
1838 ;; The we go through all the existing mail source specification
1839 ;; and fetch the mail from each.
1840 (while (setq source (pop fetching-sources))
16409b0b
GM
1841 (when (setq new
1842 (mail-source-fetch
1843 source
23f87bed
MB
1844 (gnus-byte-compile
1845 `(lambda (file orig-file)
1846 (nnmail-split-incoming
1847 file ',(intern (format "%s-save-mail" method))
1848 ',spool-func
a1da1e37
MB
1849 (or in-group
1850 (if (equal file orig-file)
1851 nil
1852 (nnmail-get-split-group orig-file ',source)))
23f87bed 1853 ',(intern (format "%s-active-number" method)))))))
16409b0b
GM
1854 (incf total new)
1855 (incf i)))
eec82323 1856 ;; If we did indeed read any incoming spools, we save all info.
16409b0b 1857 (if (zerop total)
8c3e17f8
LMI
1858 (when mail-source-plugged
1859 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1860 method (car source)))
e9d1aaff
KY
1861 (nnmail-save-active
1862 (nnmail-get-value "%s-group-alist" method)
1863 (nnmail-get-value "%s-active-file" method))
eec82323
LMI
1864 (when exit-func
1865 (funcall exit-func))
1866 (run-hooks 'nnmail-read-incoming-hook)
16409b0b
GM
1867 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1868 total))
eec82323
LMI
1869 ;; Close the message-id cache.
1870 (nnmail-cache-close)
1871 ;; Allow the user to hook.
16409b0b 1872 (run-hooks 'nnmail-post-get-new-mail-hook))))
eec82323
LMI
1873
1874(defun nnmail-expired-article-p (group time force &optional inhibit)
0617bb00
LMI
1875 "Say whether an article that is TIME old in GROUP should be expired.
1876If TIME is nil, then return the cutoff time for oldness instead."
eec82323 1877 (if force
0617bb00
LMI
1878 (if (null time)
1879 (current-time)
1880 t)
eec82323
LMI
1881 (let ((days (or (and nnmail-expiry-wait-function
1882 (funcall nnmail-expiry-wait-function group))
1883 nnmail-expiry-wait)))
1884 (cond ((or (eq days 'never)
1885 (and (not force)
1886 inhibit))
1887 ;; This isn't an expirable group.
1888 nil)
1889 ((eq days 'immediate)
1890 ;; We expire all articles on sight.
0617bb00
LMI
1891 (if (null time)
1892 (current-time)
1893 t))
eec82323 1894 ((equal time '(0 0))
23f87bed 1895 ;; This is an ange-ftp group, and we don't have any dates.
eec82323
LMI
1896 nil)
1897 ((numberp days)
16409b0b 1898 (setq days (days-to-time days))
eec82323 1899 ;; Compare the time with the current time.
0617bb00
LMI
1900 (if (null time)
1901 (time-subtract (current-time) days)
1902 (ignore-errors (time-less-p days (time-since time)))))))))
16409b0b 1903
163a3c6a
GM
1904(declare-function gnus-group-mark-article-read "gnus-group" (group article))
1905
16409b0b 1906(defun nnmail-expiry-target-group (target group)
23f87bed
MB
1907 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1908 ;; that buffer if the nnfolder group isn't selected.
1909 (let (nnmail-cache-accepted-message-ids)
1910 ;; Don't enter Message-IDs into cache.
1911 ;; Let users hack it in TARGET function.
1912 (when (functionp target)
1913 (setq target (funcall target group)))
1914 (unless (eq target 'delete)
1915 (when (or (gnus-request-group target)
1916 (gnus-request-create-group target))
1917 (let ((group-art (gnus-request-accept-article target nil nil t)))
1918 (when (consp group-art)
1919 (gnus-group-mark-article-read target (cdr group-art))))))))
1920
1921(defun nnmail-fancy-expiry-target (group)
1922 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1923 (let* (header
1924 (case-fold-search nil)
1925 (from (or (message-fetch-field "from") ""))
1926 (to (or (message-fetch-field "to") ""))
58090a8d 1927 (date (message-fetch-field "date"))
23f87bed 1928 (target 'delete))
58090a8d
MB
1929 (setq date (if date
1930 (condition-case err
1931 (date-to-time date)
1932 (error
1933 (message "%s" (error-message-string err))
1934 (current-time)))
1935 (current-time)))
23f87bed
MB
1936 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1937 (setq header (car regexp-target-pair))
1938 (cond
1939 ;; If the header is to-from then match against the
1940 ;; To or From header
1941 ((and (equal header 'to-from)
1942 (or (string-match (cadr regexp-target-pair) from)
5255e3ff
MB
1943 (and (string-match (cadr regexp-target-pair) to)
1944 (let ((rmail-dont-reply-to-names
01c52d31 1945 (message-dont-reply-to-names)))
5255e3ff 1946 (equal (rmail-dont-reply-to from) "")))))
23f87bed
MB
1947 (setq target (format-time-string (caddr regexp-target-pair) date)))
1948 ((and (not (equal header 'to-from))
1949 (string-match (cadr regexp-target-pair)
1950 (or
1951 (message-fetch-field header)
1952 "")))
1953 (setq target
1954 (format-time-string (caddr regexp-target-pair) date)))))))
eec82323
LMI
1955
1956(defun nnmail-check-syntax ()
1957 "Check (and modify) the syntax of the message in the current buffer."
1958 (save-restriction
1959 (message-narrow-to-head)
1960 (let ((case-fold-search t))
1961 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1962 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1963
1964(defun nnmail-write-region (start end filename &optional append visit lockname)
1965 "Do a `write-region', and then set the file modes."
1613b43a 1966 (let ((coding-system-for-write nnmail-file-coding-system)
16409b0b 1967 (file-name-coding-system nnmail-pathname-coding-system))
1613b43a
KH
1968 (write-region start end filename append visit lockname)
1969 (set-file-modes filename nnmail-default-file-modes)))
eec82323
LMI
1970
1971;;;
1972;;; Status functions
1973;;;
1974
1975(defun nnmail-replace-status (name value)
1976 "Make status NAME and VALUE part of the current status line."
1977 (save-restriction
1978 (message-narrow-to-head)
1979 (let ((status (nnmail-decode-status)))
1980 (setq status (delq (member name status) status))
1981 (when value
1982 (push (cons name value) status))
1983 (message-remove-header "status")
1984 (goto-char (point-max))
1985 (insert "Status: " (nnmail-encode-status status) "\n"))))
1986
1987(defun nnmail-decode-status ()
1988 "Return a status-value alist from STATUS."
1989 (goto-char (point-min))
1990 (when (re-search-forward "^Status: " nil t)
1991 (let (name value status)
1992 (save-restriction
1993 ;; Narrow to the status.
1994 (narrow-to-region
1995 (point)
1996 (if (re-search-forward "^[^ \t]" nil t)
1997 (1- (point))
1998 (point-max)))
1999 ;; Go through all elements and add them to the list.
2000 (goto-char (point-min))
2001 (while (re-search-forward "[^ \t=]+" nil t)
2002 (setq name (match-string 0))
16409b0b 2003 (if (not (eq (char-after) ?=))
eec82323
LMI
2004 ;; Implied "yes".
2005 (setq value "yes")
2006 (forward-char 1)
16409b0b 2007 (if (not (eq (char-after) ?\"))
eec82323
LMI
2008 (if (not (looking-at "[^ \t]"))
2009 ;; Implied "no".
2010 (setq value "no")
2011 ;; Unquoted value.
2012 (setq value (match-string 0))
2013 (goto-char (match-end 0)))
2014 ;; Quoted value.
2015 (setq value (read (current-buffer)))))
2016 (push (cons name value) status)))
2017 status)))
2018
2019(defun nnmail-encode-status (status)
2020 "Return a status string from STATUS."
2021 (mapconcat
2022 (lambda (elem)
2023 (concat
2024 (car elem) "="
2025 (if (string-match "[ \t]" (cdr elem))
2026 (prin1-to-string (cdr elem))
2027 (cdr elem))))
2028 status " "))
2029
2030(defun nnmail-split-history ()
2031 "Generate an overview of where the last mail split put articles."
2032 (interactive)
2033 (unless nnmail-split-history
2034 (error "No current split history"))
2035 (with-output-to-temp-buffer "*nnmail split history*"
16409b0b
GM
2036 (with-current-buffer standard-output
2037 (fundamental-mode)) ; for Emacs 20.4+
01c52d31 2038 (dolist (elem nnmail-split-history)
eec82323
LMI
2039 (princ (mapconcat (lambda (ga)
2040 (concat (car ga) ":" (int-to-string (cdr ga))))
2041 elem
2042 ", "))
01c52d31 2043 (princ "\n"))))
eec82323 2044
6748645f
LMI
2045(defun nnmail-purge-split-history (group)
2046 "Remove all instances of GROUP from `nnmail-split-history'."
2047 (let ((history nnmail-split-history))
2048 (while history
23f87bed 2049 (setcar history (gnus-remove-if (lambda (e) (string= (car e) group))
6748645f
LMI
2050 (car history)))
2051 (pop history))
2052 (setq nnmail-split-history (delq nil nnmail-split-history))))
2053
eec82323
LMI
2054(defun nnmail-new-mail-p (group)
2055 "Say whether GROUP has new mail."
2056 (let ((his nnmail-split-history)
2057 found)
2058 (while his
2059 (when (assoc group (pop his))
2060 (setq found t
2061 his nil)))
2062 found))
2063
6748645f
LMI
2064(defun nnmail-within-headers-p ()
2065 "Check to see if point is within the headers of a unix mail message.
2066Doesn't change point."
2067 (let ((pos (point)))
2068 (save-excursion
2069 (and (nnmail-search-unix-mail-delim-backward)
2070 (not (search-forward "\n\n" pos t))))))
2071
eec82323
LMI
2072(run-hooks 'nnmail-load-hook)
2073
2074(provide 'nnmail)
2075
2076;;; nnmail.el ends here