Merge from gnus--rel--5.10
[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,
e3fe4da0 4;; 2004, 2005, 2006, 2007, 2008 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
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
5a9dffec 13;; the Free Software Foundation; either version 3, or (at your option)
eec82323
LMI
14;; 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; see the file COPYING. If not, write to the
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
eec82323
LMI
25
26;;; Commentary:
27
28;;; Code:
29
0df953b6
RS
30(eval-when-compile (require 'cl))
31
23f87bed 32(require 'gnus) ; for macro gnus-kill-buffer, at least
eec82323 33(require 'nnheader)
eec82323 34(require 'message)
eec82323 35(require 'custom)
6748645f 36(require 'gnus-util)
16409b0b
GM
37(require 'mail-source)
38(require 'mm-util)
eec82323
LMI
39
40(eval-and-compile
23f87bed
MB
41 (autoload 'gnus-add-buffer "gnus")
42 (autoload 'gnus-kill-buffer "gnus"))
eec82323
LMI
43
44(defgroup nnmail nil
45 "Reading mail with Gnus."
46 :group 'gnus)
47
48(defgroup nnmail-retrieve nil
49 "Retrieving new mail."
50 :group 'nnmail)
51
52(defgroup nnmail-prepare nil
e72c9bcc 53 "Preparing (or mangling) new mail after retrieval."
eec82323
LMI
54 :group 'nnmail)
55
56(defgroup nnmail-duplicate nil
57 "Handling of duplicate mail messages."
58 :group 'nnmail)
59
60(defgroup nnmail-split nil
8f688cb0 61 "Organizing the incoming mail in folders."
eec82323
LMI
62 :group 'nnmail)
63
64(defgroup nnmail-files nil
65 "Mail files."
66 :group 'gnus-files
67 :group 'nnmail)
68
69(defgroup nnmail-expire nil
70 "Expiring old mail."
71 :group 'nnmail)
72
73(defgroup nnmail-procmail nil
74 "Interfacing with procmail and other mail agents."
75 :group 'nnmail)
76
77(defgroup nnmail-various nil
78 "Various mail options."
79 :group 'nnmail)
80
23f87bed 81(defcustom nnmail-split-methods '(("mail.misc" ""))
6748645f 82 "*Incoming mail will be split according to this variable.
eec82323
LMI
83
84If you'd like, for instance, one mail group for mail from the
85\"4ad-l\" mailing list, one group for junk mail and one for everything
86else, you could do something like this:
87
88 (setq nnmail-split-methods
89 '((\"mail.4ad\" \"From:.*4ad\")
23f87bed
MB
90 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
91 (\"mail.misc\" \"\")))
eec82323
LMI
92
93As you can see, this variable is a list of lists, where the first
94element in each \"rule\" is the name of the group (which, by the way,
95does not have to be called anything beginning with \"mail\",
96\"yonka.zow\" is a fine, fine name), and the second is a regexp that
97nnmail will try to match on the header to find a fit.
98
99The second element can also be a function. In that case, it will be
100called narrowed to the headers with the first element of the rule as
101the argument. It should return a non-nil value if it thinks that the
102mail belongs in that group.
103
104The last element should always have \"\" as the regexp.
105
106This variable can also have a function as its value."
107 :group 'nnmail-split
23f87bed
MB
108 :type '(choice (repeat :tag "Alist" (group (string :tag "Name")
109 (choice regexp function)))
eec82323
LMI
110 (function-item nnmail-split-fancy)
111 (function :tag "Other")))
112
113;; Suggested by Erik Selberg <speed@cs.washington.edu>.
114(defcustom nnmail-crosspost t
115 "If non-nil, do crossposting if several split methods match the mail.
116If nil, the first match found will be used."
117 :group 'nnmail-split
118 :type 'boolean)
119
23f87bed
MB
120(defcustom nnmail-split-fancy-with-parent-ignore-groups nil
121 "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
122This can also be a list of regexps."
bf247b6e 123 :version "22.1"
23f87bed
MB
124 :group 'nnmail-split
125 :type '(choice (const :tag "none" nil)
126 (regexp :value ".*")
127 (repeat :value (".*") regexp)))
128
129(defcustom nnmail-cache-ignore-groups nil
130 "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
131This can also be a list of regexps."
bf247b6e 132 :version "22.1"
23f87bed
MB
133 :group 'nnmail-split
134 :type '(choice (const :tag "none" nil)
135 (regexp :value ".*")
136 (repeat :value (".*") regexp)))
137
eec82323
LMI
138;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
139(defcustom nnmail-keep-last-article nil
a8151ef7
LMI
140 "If non-nil, nnmail will never delete/move a group's last article.
141It can be marked expirable, so it will be deleted when it is no longer last.
142
eec82323
LMI
143You may need to set this variable if other programs are putting
144new mail into folder numbers that Gnus has marked as expired."
145 :group 'nnmail-procmail
146 :group 'nnmail-various
147 :type 'boolean)
148
149(defcustom nnmail-use-long-file-names nil
150 "If non-nil the mail backends will use long file and directory names.
151If nil, groups like \"mail.misc\" will end up in directories like
152\"mail/misc/\"."
153 :group 'nnmail-files
154 :type 'boolean)
155
156(defcustom nnmail-default-file-modes 384
157 "Set the mode bits of all new mail files to this integer."
158 :group 'nnmail-files
159 :type 'integer)
160
161(defcustom nnmail-expiry-wait 7
162 "*Expirable articles that are older than this will be expired.
163This variable can either be a number (which will be interpreted as a
164number of days) -- this doesn't have to be an integer. This variable
165can also be `immediate' and `never'."
166 :group 'nnmail-expire
167 :type '(choice (const immediate)
23f87bed 168 (number :tag "days")
eec82323
LMI
169 (const never)))
170
171(defcustom nnmail-expiry-wait-function nil
172 "Variable that holds function to specify how old articles should be before they are expired.
23f87bed
MB
173The function will be called with the name of the group that the expiry
174is to be performed in, and it should return an integer that says how
175many days an article can be stored before it is considered \"old\".
176It can also return the values `never' and `immediate'.
eec82323
LMI
177
178Eg.:
179
180\(setq nnmail-expiry-wait-function
181 (lambda (newsgroup)
23f87bed
MB
182 (cond ((string-match \"private\" newsgroup) 31)
183 ((string-match \"junk\" newsgroup) 1)
eec82323
LMI
184 ((string-match \"important\" newsgroup) 'never)
185 (t 7))))"
186 :group 'nnmail-expire
187 :type '(choice (const :tag "nnmail-expiry-wait" nil)
188 (function :format "%v" nnmail-)))
189
16409b0b
GM
190(defcustom nnmail-expiry-target 'delete
191 "*Variable that says where expired messages should end up.
192The default value is `delete' (which says to delete the messages),
193but it can also be a string or a function. If it is a string, expired
194messages end up in that group. If it is a function, the function is
195called in a buffer narrowed to the message in question. The function
196receives one argument, the name of the group the message comes from.
197The return value should be `delete' or a group name (a string)."
4693ed4e 198 :version "21.1"
23f87bed
MB
199 :group 'nnmail-expire
200 :type '(choice (const delete)
201 (function :format "%v" nnmail-)
202 string))
203
204(defcustom nnmail-fancy-expiry-targets nil
205 "Determine expiry target based on articles using fancy techniques.
206
207This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
208`nnmail-expiry-target' is set to the function
209`nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
210the message will be expired to a group determined by invoking
211`format-time-string' with TARGET used as the format string and the
212time extracted from the articles' Date header (if missing the current
213time is used).
214
215In the special cases that HEADER is the symbol `to-from', the regexp
216will try to match against both the From and the To header.
217
218Example:
219
220\(setq nnmail-fancy-expiry-targets
221 '((to-from \"boss\" \"nnfolder:Work\")
222 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
223 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
224
225In this case, articles containing the string \"boss\" in the To or the
226From header will be expired to the group \"nnfolder:Work\";
227articles containing the sting \"IMPORTANT\" in the Subject header will
228be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
229everything else will be expired to \"nnfolder:Archive-YYYY\"."
bf247b6e 230 :version "22.1"
23f87bed
MB
231 :group 'nnmail-expire
232 :type '(repeat (list (choice :tag "Match against"
233 (string :tag "Header")
234 (const to-from))
235 regexp
236 (string :tag "Target group format string"))))
16409b0b 237
eec82323 238(defcustom nnmail-cache-accepted-message-ids nil
23f87bed
MB
239 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
240If non-nil, also update the cache when copy or move articles."
eec82323
LMI
241 :group 'nnmail
242 :type 'boolean)
243
9a075f5e 244(defcustom nnmail-spool-file '((file))
6748645f 245 "*Where the mail backends will look for incoming mail.
16409b0b
GM
246This variable is a list of mail source specifiers.
247This variable is obsolete; `mail-sources' should be used instead."
eec82323 248 :group 'nnmail-files
16409b0b 249 :type 'sexp)
8aed9ac5
RS
250(make-obsolete-variable 'nnmail-spool-file
251 "This option is obsolete in Gnus 5.9. \
252Use `mail-sources' instead.")
253;; revision 5.29 / p0-85 / Gnus 5.9
eec82323 254
16409b0b
GM
255(defcustom nnmail-resplit-incoming nil
256 "*If non-nil, re-split incoming procmail sorted mail."
eec82323
LMI
257 :group 'nnmail-procmail
258 :type 'boolean)
259
16409b0b
GM
260(defcustom nnmail-scan-directory-mail-source-once nil
261 "*If non-nil, scan all incoming procmail sorted mails once.
262It scans low-level sorted spools even when not required."
4693ed4e 263 :version "21.1"
eec82323
LMI
264 :group 'nnmail-procmail
265 :type 'boolean)
266
267(defcustom nnmail-delete-file-function 'delete-file
268 "Function called to delete files in some mail backends."
269 :group 'nnmail-files
270 :type 'function)
271
272(defcustom nnmail-crosspost-link-function
6748645f 273 (if (string-match "windows-nt\\|emx" (symbol-name system-type))
eec82323
LMI
274 'copy-file
275 'add-name-to-file)
6748645f 276 "*Function called to create a copy of a file.
eec82323
LMI
277This is `add-name-to-file' by default, which means that crossposts
278will use hard links. If your file system doesn't allow hard
279links, you could set this variable to `copy-file' instead."
280 :group 'nnmail-files
281 :type '(radio (function-item add-name-to-file)
282 (function-item copy-file)
283 (function :tag "Other")))
284
eec82323
LMI
285(defcustom nnmail-read-incoming-hook
286 (if (eq system-type 'windows-nt)
287 '(nnheader-ms-strip-cr)
288 nil)
6748645f 289 "*Hook that will be run after the incoming mail has been transferred.
8b93df01 290The incoming mail is moved from the specified spool file (which normally is
eec82323
LMI
291something like \"/usr/spool/mail/$user\") to the user's home
292directory. This hook is called after the incoming mail box has been
293emptied, and can be used to call any mail box programs you have
294running (\"xwatch\", etc.)
295
296Eg.
297
298\(add-hook 'nnmail-read-incoming-hook
23f87bed
MB
299 (lambda ()
300 (call-process \"/local/bin/mailsend\" nil nil nil
301 \"read\" nnmail-spool-file)))
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 (:)"
415 :value (: nil)
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 (!)"
421 :value (! nil)
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
479FIELD and VALUE can also be lisp symbols, in that case they are expanded
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
20ca5ee4
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)
619
eec82323
LMI
620\f
621
eec82323
LMI
622(defun nnmail-request-post (&optional server)
623 (mail-send-and-exit nil))
624
6748645f
LMI
625(defvar nnmail-file-coding-system 'raw-text
626 "Coding system used in nnmail.")
627
16409b0b
GM
628(defvar nnmail-incoming-coding-system
629 mm-text-coding-system
630 "Coding system used in reading inbox")
631
47e77e9f 632(defvar nnmail-pathname-coding-system nil
35ef97a5 633 "*Coding system for file name.")
1613b43a 634
eec82323
LMI
635(defun nnmail-find-file (file)
636 "Insert FILE in server buffer safely."
637 (set-buffer nntp-server-buffer)
16409b0b 638 (delete-region (point-min) (point-max))
eec82323 639 (let ((format-alist nil)
23f87bed 640 (after-insert-file-functions nil))
eec82323 641 (condition-case ()
1613b43a 642 (let ((coding-system-for-read nnmail-file-coding-system)
16409b0b
GM
643 (auto-mode-alist (mm-auto-mode-alist))
644 (file-name-coding-system nnmail-pathname-coding-system))
1613b43a
KH
645 (insert-file-contents file)
646 t)
eec82323
LMI
647 (file-error nil))))
648
649(defun nnmail-group-pathname (group dir &optional file)
35ef97a5 650 "Make file name for GROUP."
eec82323
LMI
651 (concat
652 (let ((dir (file-name-as-directory (expand-file-name dir))))
16409b0b
GM
653 (setq group (nnheader-replace-duplicate-chars-in-string
654 (nnheader-replace-chars-in-string group ?/ ?_)
655 ?. ?_))
6748645f 656 (setq group (nnheader-translate-file-chars group))
eec82323 657 ;; If this directory exists, we use it directly.
16409b0b
GM
658 (file-name-as-directory
659 (if (or nnmail-use-long-file-names
660 (file-directory-p (concat dir group)))
661 (expand-file-name group dir)
662 ;; If not, we translate dots into slashes.
663 (expand-file-name
664 (mm-encode-coding-string
665 (nnheader-replace-chars-in-string group ?. ?/)
666 nnmail-pathname-coding-system)
667 dir))))
eec82323
LMI
668 (or file "")))
669
eec82323
LMI
670(defun nnmail-get-active ()
671 "Returns an assoc of group names and active ranges.
672nn*-request-list should have been called before calling this function."
16409b0b
GM
673 ;; Go through all groups from the active list.
674 (save-excursion
675 (set-buffer nntp-server-buffer)
676 (nnmail-parse-active)))
677
678(defun nnmail-parse-active ()
679 "Parse the active file in the current buffer and return an alist."
680 (goto-char (point-min))
681 (unless (re-search-forward "[\\\"]" nil t)
682 (goto-char (point-max))
683 (while (re-search-backward "[][';?()#]" nil t)
684 (insert ?\\)))
685 (goto-char (point-min))
686 (let ((buffer (current-buffer))
687 group-assoc group max min)
688 (while (not (eobp))
689 (condition-case err
690 (progn
691 (narrow-to-region (point) (gnus-point-at-eol))
692 (setq group (read buffer))
693 (unless (stringp group)
694 (setq group (symbol-name group)))
23f87bed
MB
695 (if (and (numberp (setq max (read buffer)))
696 (numberp (setq min (read buffer))))
16409b0b
GM
697 (push (list group (cons min max))
698 group-assoc)))
699 (error nil))
700 (widen)
701 (forward-line 1))
eec82323
LMI
702 group-assoc))
703
16409b0b 704(defvar nnmail-active-file-coding-system 'raw-text
1613b43a
KH
705 "*Coding system for active file.")
706
eec82323
LMI
707(defun nnmail-save-active (group-assoc file-name)
708 "Save GROUP-ASSOC in ACTIVE-FILE."
1613b43a
KH
709 (let ((coding-system-for-write nnmail-active-file-coding-system))
710 (when file-name
16409b0b 711 (with-temp-file file-name
1613b43a 712 (nnmail-generate-active group-assoc)))))
eec82323
LMI
713
714(defun nnmail-generate-active (alist)
715 "Generate an active file from group-alist ALIST."
716 (erase-buffer)
717 (let (group)
718 (while (setq group (pop alist))
16409b0b
GM
719 (insert (format "%S %d %d y\n" (intern (car group)) (cdadr group)
720 (caadr group))))
721 (goto-char (point-max))
722 (while (search-backward "\\." nil t)
723 (delete-char 1))))
eec82323 724
16409b0b 725(defun nnmail-get-split-group (file source)
eec82323 726 "Find out whether this FILE is to be split into GROUP only.
16409b0b
GM
727If SOURCE is a directory spec, try to return the group name component."
728 (if (eq (car source) 'directory)
729 (let ((file (file-name-nondirectory file)))
730 (mail-source-bind (directory source)
731 (if (string-match (concat (regexp-quote suffix) "$") file)
732 (substring file 0 (match-beginning 0))
733 nil)))
734 nil))
eec82323
LMI
735
736(defun nnmail-process-babyl-mail-format (func artnum-func)
737 (let ((case-fold-search t)
16409b0b 738 (count 0)
eec82323 739 start message-id content-length do-search end)
eec82323 740 (while (not (eobp))
6748645f 741 (goto-char (point-min))
eec82323
LMI
742 (re-search-forward
743 "\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
744 (goto-char (match-end 0))
745 (delete-region (match-beginning 0) (match-end 0))
746 (narrow-to-region
747 (setq start (point))
748 (progn
749 ;; Skip all the headers in case there are more "From "s...
750 (or (search-forward "\n\n" nil t)
751 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
752 (search-forward "\1f\f"))
753 (point)))
754 ;; Unquote the ">From " line, if any.
755 (goto-char (point-min))
756 (when (looking-at ">From ")
757 (replace-match "X-From-Line: ") )
758 (run-hooks 'nnmail-prepare-incoming-header-hook)
759 (goto-char (point-max))
760 ;; Find the Message-ID header.
761 (save-excursion
762 (if (re-search-backward
763 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t)
764 (setq message-id (buffer-substring (match-beginning 1)
765 (match-end 1)))
766 ;; There is no Message-ID here, so we create one.
767 (save-excursion
768 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
769 (beginning-of-line)
770 (insert "Original-")))
771 (forward-line -1)
772 (insert "Message-ID: " (setq message-id (nnmail-message-id))
773 "\n")))
774 ;; Look for a Content-Length header.
775 (if (not (save-excursion
776 (and (re-search-backward
777 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
e9bd5782 778 (setq content-length (string-to-number
eec82323
LMI
779 (buffer-substring
780 (match-beginning 1)
781 (match-end 1))))
782 ;; We destroy the header, since none of
783 ;; the backends ever use it, and we do not
784 ;; want to confuse other mailers by having
785 ;; a (possibly) faulty header.
786 (progn (insert "X-") t))))
787 (setq do-search t)
788 (widen)
789 (if (or (= (+ (point) content-length) (point-max))
790 (save-excursion
791 (goto-char (+ (point) content-length))
792 (looking-at "\1f")))
793 (progn
794 (goto-char (+ (point) content-length))
795 (setq do-search nil))
796 (setq do-search t)))
797 (widen)
798 ;; Go to the beginning of the next article - or to the end
799 ;; of the buffer.
800 (when do-search
801 (if (re-search-forward "^\1f" nil t)
802 (goto-char (match-beginning 0))
803 (goto-char (1- (point-max)))))
804 (delete-char 1) ; delete ^_
805 (save-excursion
806 (save-restriction
807 (narrow-to-region start (point))
808 (goto-char (point-min))
809 (nnmail-check-duplication message-id func artnum-func)
16409b0b 810 (incf count)
eec82323 811 (setq end (point-max))))
16409b0b
GM
812 (goto-char end))
813 count))
eec82323
LMI
814
815(defsubst nnmail-search-unix-mail-delim ()
816 "Put point at the beginning of the next Unix mbox message."
b96fb65c 817 ;; Algorithm used to find the next article in the
eec82323
LMI
818 ;; brain-dead Unix mbox format:
819 ;;
820 ;; 1) Search for "^From ".
821 ;; 2) If we find it, then see whether the previous
822 ;; line is blank and the next line looks like a header.
823 ;; Then it's possible that this is a mail delim, and we use it.
824 (let ((case-fold-search nil)
825 found)
826 (while (not found)
827 (if (not (re-search-forward "^From " nil t))
828 (setq found 'no)
829 (save-excursion
830 (beginning-of-line)
831 (when (and (or (bobp)
832 (save-excursion
833 (forward-line -1)
16409b0b 834 (eq (char-after) ?\n)))
eec82323
LMI
835 (save-excursion
836 (forward-line 1)
a8151ef7 837 (while (looking-at ">From \\|From ")
eec82323
LMI
838 (forward-line 1))
839 (looking-at "[^ \n\t:]+[ \n\t]*:")))
840 (setq found 'yes)))))
841 (beginning-of-line)
842 (eq found 'yes)))
843
844(defun nnmail-search-unix-mail-delim-backward ()
845 "Put point at the beginning of the current Unix mbox message."
b96fb65c 846 ;; Algorithm used to find the next article in the
eec82323
LMI
847 ;; brain-dead Unix mbox format:
848 ;;
849 ;; 1) Search for "^From ".
850 ;; 2) If we find it, then see whether the previous
851 ;; line is blank and the next line looks like a header.
852 ;; Then it's possible that this is a mail delim, and we use it.
853 (let ((case-fold-search nil)
854 found)
855 (while (not found)
856 (if (not (re-search-backward "^From " nil t))
857 (setq found 'no)
858 (save-excursion
859 (beginning-of-line)
860 (when (and (or (bobp)
861 (save-excursion
862 (forward-line -1)
16409b0b 863 (eq (char-after) ?\n)))
eec82323
LMI
864 (save-excursion
865 (forward-line 1)
a8151ef7 866 (while (looking-at ">From \\|From ")
eec82323
LMI
867 (forward-line 1))
868 (looking-at "[^ \n\t:]+[ \n\t]*:")))
869 (setq found 'yes)))))
870 (beginning-of-line)
871 (eq found 'yes)))
872
873(defun nnmail-process-unix-mail-format (func artnum-func)
874 (let ((case-fold-search t)
16409b0b 875 (count 0)
eec82323
LMI
876 start message-id content-length end skip head-end)
877 (goto-char (point-min))
878 (if (not (and (re-search-forward "^From " nil t)
879 (goto-char (match-beginning 0))))
880 ;; Possibly wrong format?
23f87bed
MB
881 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
882 (if (buffer-file-name) "file" "buffer")
883 (or (buffer-file-name) (buffer-name)))
eec82323
LMI
884 ;; Carry on until the bitter end.
885 (while (not (eobp))
886 (setq start (point)
887 end nil)
888 ;; Find the end of the head.
889 (narrow-to-region
890 start
891 (if (search-forward "\n\n" nil t)
892 (1- (point))
893 ;; This will never happen, but just to be on the safe side --
894 ;; if there is no head-body delimiter, we search a bit manually.
895 (while (and (looking-at "From \\|[^ \t]+:")
896 (not (eobp)))
897 (forward-line 1))
898 (point)))
899 ;; Find the Message-ID header.
900 (goto-char (point-min))
901 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
902 (setq message-id (match-string 1))
903 (save-excursion
904 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
905 (beginning-of-line)
906 (insert "Original-")))
907 ;; There is no Message-ID here, so we create one.
908 (forward-line 1)
909 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
910 ;; Look for a Content-Length header.
911 (goto-char (point-min))
912 (if (not (re-search-forward
913 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
914 (setq content-length nil)
e9bd5782 915 (setq content-length (string-to-number (match-string 1)))
eec82323
LMI
916 ;; We destroy the header, since none of the backends ever
917 ;; use it, and we do not want to confuse other mailers by
918 ;; having a (possibly) faulty header.
919 (beginning-of-line)
920 (insert "X-"))
921 (run-hooks 'nnmail-prepare-incoming-header-hook)
922 ;; Find the end of this article.
923 (goto-char (point-max))
924 (widen)
925 (setq head-end (point))
926 ;; We try the Content-Length value. The idea: skip over the header
927 ;; separator, then check what happens content-length bytes into the
8f688cb0 928 ;; message body. This should be either the end of the buffer, the
eec82323
LMI
929 ;; message separator or a blank line followed by the separator.
930 ;; The blank line should probably be deleted. If neither of the
931 ;; three is met, the content-length header is probably invalid.
932 (when content-length
933 (forward-line 1)
934 (setq skip (+ (point) content-length))
935 (goto-char skip)
936 (cond ((or (= skip (point-max))
937 (= (1+ skip) (point-max)))
938 (setq end (point-max)))
939 ((looking-at "From ")
940 (setq end skip))
941 ((looking-at "[ \t]*\n\\(From \\)")
942 (setq end (match-beginning 1)))
943 (t (setq end nil))))
944 (if end
945 (goto-char end)
946 ;; No Content-Length, so we find the beginning of the next
947 ;; article or the end of the buffer.
948 (goto-char head-end)
949 (or (nnmail-search-unix-mail-delim)
950 (goto-char (point-max))))
951 ;; Allow the backend to save the article.
952 (save-excursion
953 (save-restriction
954 (narrow-to-region start (point))
955 (goto-char (point-min))
16409b0b 956 (incf count)
eec82323
LMI
957 (nnmail-check-duplication message-id func artnum-func)
958 (setq end (point-max))))
16409b0b
GM
959 (goto-char end)))
960 count))
eec82323
LMI
961
962(defun nnmail-process-mmdf-mail-format (func artnum-func)
963 (let ((delim "^\^A\^A\^A\^A$")
964 (case-fold-search t)
16409b0b 965 (count 0)
eec82323
LMI
966 start message-id end)
967 (goto-char (point-min))
968 (if (not (and (re-search-forward delim nil t)
969 (forward-line 1)))
970 ;; Possibly wrong format?
16409b0b 971 (error "Error, unknown mail format! (Possibly corrupted.)")
eec82323
LMI
972 ;; Carry on until the bitter end.
973 (while (not (eobp))
974 (setq start (point))
975 ;; Find the end of the head.
976 (narrow-to-region
977 start
978 (if (search-forward "\n\n" nil t)
979 (1- (point))
980 ;; This will never happen, but just to be on the safe side --
981 ;; if there is no head-body delimiter, we search a bit manually.
982 (while (and (looking-at "From \\|[^ \t]+:")
983 (not (eobp)))
984 (forward-line 1))
985 (point)))
986 ;; Find the Message-ID header.
987 (goto-char (point-min))
988 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
989 (setq message-id (match-string 1))
990 ;; There is no Message-ID here, so we create one.
991 (save-excursion
992 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
993 (beginning-of-line)
994 (insert "Original-")))
995 (forward-line 1)
996 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
997 (run-hooks 'nnmail-prepare-incoming-header-hook)
998 ;; Find the end of this article.
999 (goto-char (point-max))
1000 (widen)
1001 (if (re-search-forward delim nil t)
1002 (beginning-of-line)
1003 (goto-char (point-max)))
1004 ;; Allow the backend to save the article.
1005 (save-excursion
1006 (save-restriction
1007 (narrow-to-region start (point))
1008 (goto-char (point-min))
16409b0b 1009 (incf count)
eec82323
LMI
1010 (nnmail-check-duplication message-id func artnum-func)
1011 (setq end (point-max))))
1012 (goto-char end)
16409b0b
GM
1013 (forward-line 2)))
1014 count))
1015
1016(defun nnmail-process-maildir-mail-format (func artnum-func)
1017 ;; In a maildir, every file contains exactly one mail.
1018 (let ((case-fold-search t)
1019 message-id)
1020 (goto-char (point-min))
1021 ;; Find the end of the head.
1022 (narrow-to-region
1023 (point-min)
1024 (if (search-forward "\n\n" nil t)
1025 (1- (point))
1026 ;; This will never happen, but just to be on the safe side --
1027 ;; if there is no head-body delimiter, we search a bit manually.
1028 (while (and (looking-at "From \\|[^ \t]+:")
1029 (not (eobp)))
1030 (forward-line 1))
1031 (point)))
1032 ;; Find the Message-ID header.
1033 (goto-char (point-min))
1034 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
1035 (setq message-id (match-string 1))
1036 ;; There is no Message-ID here, so we create one.
1037 (save-excursion
1038 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
1039 (beginning-of-line)
1040 (insert "Original-")))
1041 (forward-line 1)
1042 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
1043 (run-hooks 'nnmail-prepare-incoming-header-hook)
1044 ;; Allow the backend to save the article.
1045 (widen)
1046 (save-excursion
1047 (goto-char (point-min))
1048 (nnmail-check-duplication message-id func artnum-func))
1049 1))
eec82323
LMI
1050
1051(defun nnmail-split-incoming (incoming func &optional exit-func
1052 group artnum-func)
1053 "Go through the entire INCOMING file and pick out each individual mail.
1054FUNC will be called with the buffer narrowed to each mail."
23f87bed 1055 (let ( ;; If this is a group-specific split, we bind the split
eec82323
LMI
1056 ;; methods to just this group.
1057 (nnmail-split-methods (if (and group
eec82323
LMI
1058 (not nnmail-resplit-incoming))
1059 (list (list group ""))
1060 nnmail-split-methods)))
1061 (save-excursion
1062 ;; Insert the incoming file.
23f87bed 1063 (set-buffer (get-buffer-create nnmail-article-buffer))
eec82323 1064 (erase-buffer)
16409b0b
GM
1065 (let ((coding-system-for-read nnmail-incoming-coding-system))
1066 (mm-insert-file-contents incoming))
1067 (prog1
1068 (if (zerop (buffer-size))
1069 0
1070 (goto-char (point-min))
1071 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
1072 ;; Handle both babyl, MMDF and unix mail formats, since
1073 ;; movemail will use the former when fetching from a
1074 ;; mailbox, the latter when fetching from a file.
1075 (cond ((or (looking-at "\^L")
1076 (looking-at "BABYL OPTIONS:"))
1077 (nnmail-process-babyl-mail-format func artnum-func))
1078 ((looking-at "\^A\^A\^A\^A")
1079 (nnmail-process-mmdf-mail-format func artnum-func))
1080 ((looking-at "Return-Path:")
1081 (nnmail-process-maildir-mail-format func artnum-func))
1082 (t
1083 (nnmail-process-unix-mail-format func artnum-func))))
1084 (when exit-func
1085 (funcall exit-func))
1086 (kill-buffer (current-buffer))))))
eec82323 1087
6748645f 1088(defun nnmail-article-group (func &optional trace)
eec82323
LMI
1089 "Look at the headers and return an alist of groups that match.
1090FUNC will be called with the group name to determine the article number."
23f87bed 1091 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
eec82323 1092 (obuf (current-buffer))
23f87bed 1093 group-art method grp)
6748645f
LMI
1094 (if (and (sequencep methods)
1095 (= (length methods) 1))
eec82323
LMI
1096 ;; If there is only just one group to put everything in, we
1097 ;; just return a list with just this one method in.
1098 (setq group-art
1099 (list (cons (caar methods) (funcall func (caar methods)))))
1100 ;; We do actual comparison.
1101 (save-excursion
23f87bed 1102 ;; Copy the article into the work buffer.
eec82323
LMI
1103 (set-buffer nntp-server-buffer)
1104 (erase-buffer)
23f87bed
MB
1105 (insert-buffer-substring obuf)
1106 ;; Narrow to headers.
1107 (narrow-to-region
1108 (goto-char (point-min))
1109 (if (search-forward "\n\n" nil t)
1110 (point)
1111 (point-max)))
1112 (goto-char (point-min))
1113 ;; Decode MIME headers and charsets.
1114 (when nnmail-mail-splitting-decodes
1115 (let ((mail-parse-charset nnmail-mail-splitting-charset))
1116 (mail-decode-encoded-word-region (point-min) (point-max))))
eec82323
LMI
1117 ;; Fold continuation lines.
1118 (goto-char (point-min))
1119 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1120 (replace-match " " t t))
6748645f
LMI
1121 ;; Nuke pathologically long headers. Since Gnus applies
1122 ;; pathologically complex regexps to the buffer, lines
1123 ;; that are looong will take longer than the Universe's
1124 ;; existence to process.
1125 (goto-char (point-min))
1126 (while (not (eobp))
16409b0b
GM
1127 (unless (< (move-to-column nnmail-split-header-length-limit)
1128 nnmail-split-header-length-limit)
23f87bed 1129 (delete-region (point) (gnus-point-at-eol)))
16409b0b 1130 (forward-line 1))
eec82323 1131 ;; Allow washing.
6748645f 1132 (goto-char (point-min))
eec82323 1133 (run-hooks 'nnmail-split-hook)
6748645f
LMI
1134 (when (setq nnmail-split-tracing trace)
1135 (setq nnmail-split-trace nil))
eec82323
LMI
1136 (if (and (symbolp nnmail-split-methods)
1137 (fboundp nnmail-split-methods))
1138 (let ((split
06dc8321 1139 (condition-case error-info
a8151ef7
LMI
1140 ;; `nnmail-split-methods' is a function, so we
1141 ;; just call this function here and use the
1142 ;; result.
eec82323
LMI
1143 (or (funcall nnmail-split-methods)
1144 '("bogus"))
1145 (error
23f87bed 1146 (nnheader-message
06dc8321 1147 5 "Error in `nnmail-split-methods'; using `bogus' mail group: %S" error-info)
eec82323
LMI
1148 (sit-for 1)
1149 '("bogus")))))
8753ddee 1150 (setq split (mm-delete-duplicates split))
a8151ef7
LMI
1151 ;; The article may be "cross-posted" to `junk'. What
1152 ;; to do? Just remove the `junk' spec. Don't really
1153 ;; see anything else to do...
1154 (let (elem)
1155 (while (setq elem (car (memq 'junk split)))
1156 (setq split (delq elem split))))
1157 (when split
eec82323
LMI
1158 (setq group-art
1159 (mapcar
1160 (lambda (group) (cons group (funcall func group)))
1161 split))))
1162 ;; Go through the split methods to find a match.
6748645f
LMI
1163 (while (and methods
1164 (or nnmail-crosspost
1165 (not group-art)))
eec82323 1166 (goto-char (point-max))
6748645f 1167 (setq method (pop methods)
16409b0b 1168 grp (car method))
eec82323
LMI
1169 (if (or methods
1170 (not (equal "" (nth 1 method))))
1171 (when (and
1172 (ignore-errors
1173 (if (stringp (nth 1 method))
16409b0b
GM
1174 (let ((expand (string-match "\\\\[0-9&]" grp))
1175 (pos (re-search-backward (cadr method)
1176 nil t)))
1177 (and expand
1178 (setq grp (nnmail-expand-newtext grp)))
1179 pos)
eec82323 1180 ;; Function to say whether this is a match.
16409b0b 1181 (funcall (nth 1 method) grp)))
eec82323
LMI
1182 ;; Don't enter the article into the same
1183 ;; group twice.
16409b0b
GM
1184 (not (assoc grp group-art)))
1185 (push (cons grp (funcall func grp))
eec82323
LMI
1186 group-art))
1187 ;; This is the final group, which is used as a
1188 ;; catch-all.
1189 (unless group-art
1190 (setq group-art
1191 (list (cons (car method)
23f87bed
MB
1192 (funcall func (car method))))))))
1193 ;; Fall back on "bogus" if all else fails.
1194 (unless group-art
1195 (setq group-art (list (cons "bogus" (funcall func "bogus"))))))
6748645f
LMI
1196 ;; Produce a trace if non-empty.
1197 (when (and trace nnmail-split-trace)
23f87bed 1198 (let ((restore (current-buffer)))
6748645f
LMI
1199 (nnheader-set-temp-buffer "*Split Trace*")
1200 (gnus-add-buffer)
23f87bed
MB
1201 (dolist (trace (nreverse nnmail-split-trace))
1202 (prin1 trace (current-buffer))
1203 (insert "\n"))
6748645f
LMI
1204 (goto-char (point-min))
1205 (gnus-configure-windows 'split-trace)
1206 (set-buffer restore)))
23f87bed 1207 (widen)
eec82323
LMI
1208 ;; See whether the split methods returned `junk'.
1209 (if (equal group-art '(junk))
1210 nil
a8151ef7
LMI
1211 ;; The article may be "cross-posted" to `junk'. What
1212 ;; to do? Just remove the `junk' spec. Don't really
1213 ;; see anything else to do...
1214 (let (elem)
1215 (while (setq elem (car (memq 'junk group-art)))
1216 (setq group-art (delq elem group-art)))
1217 (nreverse group-art)))))))
eec82323
LMI
1218
1219(defun nnmail-insert-lines ()
1220 "Insert how many lines there are in the body of the mail.
1221Return the number of characters in the body."
1222 (let (lines chars)
1223 (save-excursion
1224 (goto-char (point-min))
a1506d29 1225 (unless (search-forward "\n\n" nil t)
16409b0b
GM
1226 (goto-char (point-max))
1227 (insert "\n"))
1228 (setq chars (- (point-max) (point)))
1229 (setq lines (count-lines (point) (point-max)))
1230 (forward-char -1)
1231 (save-excursion
1232 (when (re-search-backward "^Lines: " nil t)
1233 (delete-region (point) (progn (forward-line 1) (point)))))
1234 (beginning-of-line)
1235 (insert (format "Lines: %d\n" (max lines 0)))
1236 chars)))
eec82323
LMI
1237
1238(defun nnmail-insert-xref (group-alist)
1239 "Insert an Xref line based on the (group . article) alist."
1240 (save-excursion
1241 (goto-char (point-min))
16409b0b
GM
1242 (unless (search-forward "\n\n" nil t)
1243 (goto-char (point-max))
1244 (insert "\n"))
1245 (forward-char -1)
1246 (when (re-search-backward "^Xref: " nil t)
1247 (delete-region (match-beginning 0)
1248 (progn (forward-line 1) (point))))
1249 (insert (format "Xref: %s" (system-name)))
1250 (while group-alist
1251 (insert (format " %s:%d"
1252 (mm-encode-coding-string
1253 (caar group-alist)
1254 nnmail-pathname-coding-system)
1255 (cdar group-alist)))
1256 (setq group-alist (cdr group-alist)))
1257 (insert "\n")))
eec82323
LMI
1258
1259;;; Message washing functions
1260
1261(defun nnmail-remove-leading-whitespace ()
1262 "Remove excessive whitespace from all headers."
1263 (goto-char (point-min))
1264 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1265 (replace-match "\\1" t)))
1266
1267(defun nnmail-remove-list-identifiers ()
1268 "Remove list identifiers from Subject headers."
23f87bed
MB
1269 (let ((regexp
1270 (if (consp nnmail-list-identifiers)
1271 (mapconcat 'identity nnmail-list-identifiers " *\\|")
1272 nnmail-list-identifiers)))
eec82323
LMI
1273 (when regexp
1274 (goto-char (point-min))
23f87bed
MB
1275 (while (re-search-forward
1276 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1277 nil t)
1278 (delete-region (match-beginning 2) (match-end 0))
1279 (beginning-of-line))
1280 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1281 nil t)
1282 (delete-region (match-beginning 1) (match-end 1))
1283 (beginning-of-line)))))
eec82323
LMI
1284
1285(defun nnmail-remove-tabs ()
1286 "Translate TAB characters into SPACE characters."
1287 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1288
16409b0b
GM
1289(defun nnmail-fix-eudora-headers ()
1290 "Eudora has a broken References line, but an OK In-Reply-To."
1291 (goto-char (point-min))
1292 (when (re-search-forward "^X-Mailer:.*Eudora" nil t)
1293 (goto-char (point-min))
1294 (when (re-search-forward "^References:" nil t)
1295 (beginning-of-line)
1296 (insert "X-Gnus-Broken-Eudora-"))
1297 (goto-char (point-min))
23f87bed
MB
1298 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1299 (replace-match "\\1" t))))
eec82323 1300
16409b0b
GM
1301(custom-add-option 'nnmail-prepare-incoming-header-hook
1302 'nnmail-fix-eudora-headers)
eec82323 1303
16409b0b 1304;;; Utility functions
eec82323 1305
23f87bed
MB
1306(defun nnmail-do-request-post (accept-func &optional server)
1307 "Utility function to directly post a message to an nnmail-derived group.
1308Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1309to actually put the message in the right group."
1310 (let ((success t))
1311 (dolist (mbx (message-unquote-tokens
1312 (message-tokenize-header
1313 (message-fetch-field "Newsgroups") ", ")) success)
1314 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1315 (or (gnus-active to-newsgroup)
1316 (gnus-activate-group to-newsgroup)
1317 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1318 to-newsgroup))
1319 (or (and (gnus-request-create-group
1320 to-newsgroup gnus-command-method)
1321 (gnus-activate-group to-newsgroup nil nil
1322 gnus-command-method))
1323 (error "Couldn't create group %s" to-newsgroup)))
1324 (error "No such group: %s" to-newsgroup))
1325 (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1326 (setq success nil))))))
1327
eec82323
LMI
1328(defun nnmail-split-fancy ()
1329 "Fancy splitting method.
23f87bed 1330See the documentation for the variable `nnmail-split-fancy' for details."
eec82323
LMI
1331 (let ((syntab (syntax-table)))
1332 (unwind-protect
1333 (progn
1334 (set-syntax-table nnmail-split-fancy-syntax-table)
1335 (nnmail-split-it nnmail-split-fancy))
1336 (set-syntax-table syntab))))
1337
1338(defvar nnmail-split-cache nil)
1339;; Alist of split expressions their equivalent regexps.
1340
1341(defun nnmail-split-it (split)
1342 ;; Return a list of groups matching SPLIT.
6748645f
LMI
1343 (let (cached-pair)
1344 (cond
1345 ;; nil split
1346 ((null split)
1347 nil)
1348
1349 ;; A group name. Do the \& and \N subs into the string.
1350 ((stringp split)
1351 (when nnmail-split-tracing
23f87bed 1352 (push split nnmail-split-trace))
6748645f
LMI
1353 (list (nnmail-expand-newtext split)))
1354
1355 ;; Junk the message.
1356 ((eq split 'junk)
1357 (when nnmail-split-tracing
1358 (push "junk" nnmail-split-trace))
1359 (list 'junk))
1360
1361 ;; Builtin & operation.
1362 ((eq (car split) '&)
1363 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1364
1365 ;; Builtin | operation.
1366 ((eq (car split) '|)
1367 (let (done)
1368 (while (and (not done) (cdr split))
1369 (setq split (cdr split)
1370 done (nnmail-split-it (car split))))
1371 done))
1372
1373 ;; Builtin : operation.
1374 ((eq (car split) ':)
23f87bed
MB
1375 (when nnmail-split-tracing
1376 (push split nnmail-split-trace))
6748645f
LMI
1377 (nnmail-split-it (save-excursion (eval (cdr split)))))
1378
16409b0b
GM
1379 ;; Builtin ! operation.
1380 ((eq (car split) '!)
1381 (funcall (cadr split) (nnmail-split-it (caddr split))))
1382
6748645f
LMI
1383 ;; Check the cache for the regexp for this split.
1384 ((setq cached-pair (assq split nnmail-split-cache))
16409b0b
GM
1385 (let (split-result
1386 (end-point (point-max))
1387 (value (nth 1 split)))
1388 (if (symbolp value)
1389 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1390 (while (and (goto-char end-point)
1391 (re-search-backward (cdr cached-pair) nil t))
1392 (when nnmail-split-tracing
23f87bed 1393 (push split nnmail-split-trace))
16409b0b
GM
1394 (let ((split-rest (cddr split))
1395 (end (match-end 0))
23f87bed
MB
1396 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1397 ;; So, start-of-value is the point just before the
1398 ;; beginning of the value, whereas after-header-name
1399 ;; is the point just after the field name.
16409b0b
GM
1400 (start-of-value (match-end 1))
1401 (after-header-name (match-end 2)))
1402 ;; Start the next search just before the beginning of the
1403 ;; VALUE match.
1404 (setq end-point (1- start-of-value))
1405 ;; Handle - RESTRICTs
1406 (while (eq (car split-rest) '-)
1407 ;; RESTRICT must start after-header-name and
1408 ;; end after start-of-value, so that, for
1409 ;; (any "foo" - "x-foo" "foo.list")
1410 ;; we do not exclude foo.list just because
1411 ;; the header is: ``To: x-foo, foo''
1412 (goto-char end)
1413 (if (and (re-search-backward (cadr split-rest)
1414 after-header-name t)
1415 (> (match-end 0) start-of-value))
1416 (setq split-rest nil)
1417 (setq split-rest (cddr split-rest))))
1418 (when split-rest
1419 (goto-char end)
1420 (let ((value (nth 1 split)))
1421 (if (symbolp value)
1422 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1423 ;; Someone might want to do a \N sub on this match, so get the
1424 ;; correct match positions.
1425 (re-search-backward value start-of-value))
1426 (dolist (sp (nnmail-split-it (car split-rest)))
23f87bed 1427 (unless (member sp split-result)
16409b0b
GM
1428 (push sp split-result))))))
1429 split-result))
6748645f
LMI
1430
1431 ;; Not in cache, compute a regexp for the field/value pair.
1432 (t
61e66a15
MB
1433 (let ((field (nth 0 split))
1434 (value (nth 1 split))
1435 (split-rest (cddr split))
1436 partial-front
1437 partial-rear
1438 regexp)
16409b0b
GM
1439 (if (symbolp value)
1440 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1441 (if (and (>= (length value) 2)
1442 (string= ".*" (substring value 0 2)))
1443 (setq value (substring value 2)
23f87bed
MB
1444 partial-front ""))
1445 ;; Same trick for the rear of the regexp
1446 (if (and (>= (length value) 2)
1447 (string= ".*" (substring value -2)))
1448 (setq value (substring value 0 -2)
1449 partial-rear ""))
61e66a15
MB
1450 ;; Invert the match-partial-words behavior if the optional
1451 ;; last element is specified.
1452 (while (eq (car split-rest) '-)
1453 (setq split-rest (cddr split-rest)))
1454 (when (if (cadr split-rest)
1455 (not nnmail-split-fancy-match-partial-words)
1456 nnmail-split-fancy-match-partial-words)
23f87bed
MB
1457 (setq partial-front ""
1458 partial-rear ""))
16409b0b 1459 (setq regexp (concat "^\\(\\("
6748645f
LMI
1460 (if (symbolp field)
1461 (cdr (assq field nnmail-split-abbrev-alist))
1462 field)
16409b0b 1463 "\\):.*\\)"
23f87bed 1464 (or partial-front "\\<")
16409b0b
GM
1465 "\\("
1466 value
23f87bed
MB
1467 "\\)"
1468 (or partial-rear "\\>")))
6748645f
LMI
1469 (push (cons split regexp) nnmail-split-cache)
1470 ;; Now that it's in the cache, just call nnmail-split-it again
61e66a15 1471 ;; on the same split, which will find it immediately in the cache.
6748645f 1472 (nnmail-split-it split))))))
eec82323
LMI
1473
1474(defun nnmail-expand-newtext (newtext)
1475 (let ((len (length newtext))
1476 (pos 0)
1477 c expanded beg N did-expand)
1478 (while (< pos len)
1479 (setq beg pos)
1480 (while (and (< pos len)
1481 (not (= (aref newtext pos) ?\\)))
1482 (setq pos (1+ pos)))
1483 (unless (= beg pos)
1484 (push (substring newtext beg pos) expanded))
1485 (when (< pos len)
6748645f
LMI
1486 ;; We hit a \; expand it.
1487 (setq did-expand t
1488 pos (1+ pos)
1489 c (aref newtext pos))
eec82323
LMI
1490 (if (not (or (= c ?\&)
1491 (and (>= c ?1)
1492 (<= c ?9))))
6748645f 1493 ;; \ followed by some character we don't expand.
eec82323
LMI
1494 (push (char-to-string c) expanded)
1495 ;; \& or \N
1496 (if (= c ?\&)
1497 (setq N 0)
1498 (setq N (- c ?0)))
1499 (when (match-beginning N)
23f87bed
MB
1500 (push (if nnmail-split-lowercase-expanded
1501 (downcase (buffer-substring (match-beginning N)
1502 (match-end N)))
1503 (buffer-substring (match-beginning N) (match-end N)))
eec82323
LMI
1504 expanded))))
1505 (setq pos (1+ pos)))
1506 (if did-expand
1507 (apply 'concat (nreverse expanded))
1508 newtext)))
1509
eec82323
LMI
1510;; Activate a backend only if it isn't already activated.
1511;; If FORCE, re-read the active file even if the backend is
1512;; already activated.
1513(defun nnmail-activate (backend &optional force)
6748645f 1514 (nnheader-init-server-buffer)
eec82323
LMI
1515 (let (file timestamp file-time)
1516 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1517 force
1518 (and (setq file (ignore-errors
1519 (symbol-value (intern (format "%s-active-file"
1520 backend)))))
1521 (setq file-time (nth 5 (file-attributes file)))
1522 (or (not
1523 (setq timestamp
1524 (condition-case ()
1525 (symbol-value (intern
1526 (format "%s-active-timestamp"
1527 backend)))
1528 (error 'none))))
1529 (not (consp timestamp))
1530 (equal timestamp '(0 0))
1531 (> (nth 0 file-time) (nth 0 timestamp))
1532 (and (= (nth 0 file-time) (nth 0 timestamp))
1533 (> (nth 1 file-time) (nth 1 timestamp))))))
1534 (save-excursion
1535 (or (eq timestamp 'none)
1536 (set (intern (format "%s-active-timestamp" backend))
1537 file-time))
1538 (funcall (intern (format "%s-request-list" backend)))))
1539 t))
1540
1541(defun nnmail-message-id ()
1542 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1543
1544;;;
1545;;; nnmail duplicate handling
1546;;;
1547
1548(defvar nnmail-cache-buffer nil)
1549
1550(defun nnmail-cache-open ()
1551 (if (or (not nnmail-treat-duplicates)
1552 (and nnmail-cache-buffer
1553 (buffer-name nnmail-cache-buffer)))
1554 () ; The buffer is open.
1555 (save-excursion
1556 (set-buffer
1557 (setq nnmail-cache-buffer
1558 (get-buffer-create " *nnmail message-id cache*")))
23f87bed 1559 (gnus-add-buffer)
eec82323
LMI
1560 (when (file-exists-p nnmail-message-id-cache-file)
1561 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1562 (set-buffer-modified-p nil)
1563 (current-buffer))))
1564
1565(defun nnmail-cache-close ()
1566 (when (and nnmail-cache-buffer
1567 nnmail-treat-duplicates
1568 (buffer-name nnmail-cache-buffer)
1569 (buffer-modified-p nnmail-cache-buffer))
1570 (save-excursion
1571 (set-buffer nnmail-cache-buffer)
1572 ;; Weed out the excess number of Message-IDs.
1573 (goto-char (point-max))
1574 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1575 (progn
1576 (beginning-of-line)
1577 (delete-region (point-min) (point))))
1578 ;; Save the buffer.
1579 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1580 (make-directory (file-name-directory nnmail-message-id-cache-file)
1581 t))
1582 (nnmail-write-region (point-min) (point-max)
1583 nnmail-message-id-cache-file nil 'silent)
1584 (set-buffer-modified-p nil)
1585 (setq nnmail-cache-buffer nil)
23f87bed 1586 (gnus-kill-buffer (current-buffer)))))
eec82323 1587
16409b0b
GM
1588;; Compiler directives.
1589(defvar group)
1590(defvar group-art-list)
1591(defvar group-art)
23f87bed
MB
1592(defun nnmail-cache-insert (id grp &optional subject sender)
1593 (when (stringp id)
1594 ;; this will handle cases like `B r' where the group is nil
1595 (let ((grp (or grp gnus-newsgroup-name "UNKNOWN")))
bf247b6e 1596 (run-hook-with-args 'nnmail-spool-hook
23f87bed
MB
1597 id grp subject sender))
1598 (when nnmail-treat-duplicates
1599 ;; Store some information about the group this message is written
1600 ;; to. This is passed in as the grp argument -- all locations this
1601 ;; has been called from have been checked and the group is available.
1602 ;; The only ambiguous case is nnmail-check-duplication which will only
1603 ;; pass the first (of possibly >1) group which matches. -Josh
16409b0b 1604 (unless (gnus-buffer-live-p nnmail-cache-buffer)
23f87bed 1605 (nnmail-cache-open))
16409b0b 1606 (save-excursion
23f87bed
MB
1607 (set-buffer nnmail-cache-buffer)
1608 (goto-char (point-max))
1609 (if (and grp (not (string= "" grp))
1610 (gnus-methods-equal-p gnus-command-method
1611 (nnmail-cache-primary-mail-backend)))
1612 (let ((regexp (if (consp nnmail-cache-ignore-groups)
1613 (mapconcat 'identity nnmail-cache-ignore-groups
1614 "\\|")
1615 nnmail-cache-ignore-groups)))
1616 (unless (and regexp (string-match regexp grp))
1617 (insert id "\t" grp "\n")))
1618 (insert id "\n"))))))
bf247b6e 1619
16409b0b
GM
1620(defun nnmail-cache-primary-mail-backend ()
1621 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
23f87bed
MB
1622 (be nil)
1623 (res nil)
1624 (get-new-mail nil))
16409b0b
GM
1625 (while (and (null res) be-list)
1626 (setq be (car be-list))
1627 (setq be-list (cdr be-list))
1628 (when (and (gnus-method-option-p be 'respool)
23f87bed
MB
1629 (setq get-new-mail
1630 (intern (format "%s-get-new-mail" (car be))))
1631 (boundp get-new-mail)
1632 (symbol-value get-new-mail))
1633 (setq res be)))
16409b0b
GM
1634 res))
1635
1636;; Fetch the group name corresponding to the message id stored in the
1637;; cache.
1638(defun nnmail-cache-fetch-group (id)
1639 (when (and nnmail-treat-duplicates nnmail-cache-buffer)
eec82323
LMI
1640 (save-excursion
1641 (set-buffer nnmail-cache-buffer)
1642 (goto-char (point-max))
16409b0b 1643 (when (search-backward id nil t)
23f87bed
MB
1644 (beginning-of-line)
1645 (skip-chars-forward "^\n\r\t")
1646 (unless (looking-at "[\r\n]")
1647 (forward-char 1)
1648 (buffer-substring (point) (gnus-point-at-eol)))))))
16409b0b
GM
1649
1650;; Function for nnmail-split-fancy: look up all references in the
1651;; cache and if a match is found, return that group.
1652(defun nnmail-split-fancy-with-parent ()
23f87bed
MB
1653 "Split this message into the same group as its parent.
1654This function can be used as an entry in `nnmail-split-fancy', for
1655example like this: (: nnmail-split-fancy-with-parent)
1656For a message to be split, it looks for the parent message in the
1657References or In-Reply-To header and then looks in the message id
1658cache file (given by the variable `nnmail-message-id-cache-file') to
1659see which group that message was put in. This group is returned.
1660
1661See the Info node `(gnus)Fancy Mail Splitting' for more details."
16409b0b 1662 (let* ((refstr (or (message-fetch-field "references")
23f87bed
MB
1663 (message-fetch-field "in-reply-to")))
1664 (references nil)
1665 (res nil)
1666 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1667 (mapconcat
1668 (lambda (x) (format "\\(%s\\)" x))
1669 nnmail-split-fancy-with-parent-ignore-groups
1670 "\\|")
1671 nnmail-split-fancy-with-parent-ignore-groups)))
16409b0b
GM
1672 (when refstr
1673 (setq references (nreverse (gnus-split-references refstr)))
1674 (unless (gnus-buffer-live-p nnmail-cache-buffer)
23f87bed 1675 (nnmail-cache-open))
16409b0b 1676 (mapcar (lambda (x)
23f87bed
MB
1677 (setq res (or (nnmail-cache-fetch-group x) res))
1678 (when (or (member res '("delayed" "drafts" "queue"))
1679 (and regexp res (string-match regexp res)))
1680 (setq res nil)))
1681 references)
16409b0b 1682 res)))
eec82323
LMI
1683
1684(defun nnmail-cache-id-exists-p (id)
1685 (when nnmail-treat-duplicates
1686 (save-excursion
1687 (set-buffer nnmail-cache-buffer)
1688 (goto-char (point-max))
1689 (search-backward id nil t))))
1690
1691(defun nnmail-fetch-field (header)
1692 (save-excursion
1693 (save-restriction
1694 (message-narrow-to-head)
1695 (message-fetch-field header))))
1696
1697(defun nnmail-check-duplication (message-id func artnum-func)
1698 (run-hooks 'nnmail-prepare-incoming-message-hook)
1699 ;; If this is a duplicate message, then we do not save it.
1700 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1701 (case-fold-search t)
1702 (action (when duplication
1703 (cond
1704 ((memq nnmail-treat-duplicates '(warn delete))
1705 nnmail-treat-duplicates)
23f87bed 1706 ((functionp nnmail-treat-duplicates)
eec82323
LMI
1707 (funcall nnmail-treat-duplicates message-id))
1708 (t
1709 nnmail-treat-duplicates))))
1710 group-art)
16409b0b
GM
1711 ;; We insert a line that says what the mail source is.
1712 (let ((case-fold-search t))
1713 (goto-char (point-min))
1714 (re-search-forward "^message-id[ \t]*:" nil t)
1715 (beginning-of-line)
1716 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1717
eec82323
LMI
1718 ;; Let the backend save the article (or not).
1719 (cond
1720 ((not duplication)
eec82323 1721 (funcall func (setq group-art
16409b0b 1722 (nreverse (nnmail-article-group artnum-func))))
23f87bed 1723 (nnmail-cache-insert message-id (caar group-art)))
eec82323
LMI
1724 ((eq action 'delete)
1725 (setq group-art nil))
1726 ((eq action 'warn)
1727 ;; We insert a warning.
1728 (let ((case-fold-search t))
1729 (goto-char (point-min))
1730 (re-search-forward "^message-id[ \t]*:" nil t)
1731 (beginning-of-line)
1732 (insert
1733 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1734 (funcall func (setq group-art
1735 (nreverse (nnmail-article-group artnum-func))))))
1736 (t
1737 (funcall func (setq group-art
1738 (nreverse (nnmail-article-group artnum-func))))))
1739 ;; Add the group-art list to the history list.
1740 (if group-art
1741 (push group-art nnmail-split-history)
1742 (delete-region (point-min) (point-max)))))
1743
1744;;; Get new mail.
1745
16409b0b
GM
1746(defvar nnmail-fetched-sources nil)
1747
eec82323
LMI
1748(defun nnmail-get-value (&rest args)
1749 (let ((sym (intern (apply 'format args))))
1750 (when (boundp sym)
1751 (symbol-value sym))))
1752
1753(defun nnmail-get-new-mail (method exit-func temp
1754 &optional group spool-func)
1755 "Read new incoming mail."
16409b0b 1756 (let* ((sources (or mail-sources
8aed9ac5
RS
1757 (if (listp nnmail-spool-file)
1758 nnmail-spool-file
16409b0b
GM
1759 (list nnmail-spool-file))))
1760 fetching-sources
eec82323 1761 (group-in group)
16409b0b
GM
1762 (i 0)
1763 (new 0)
1764 (total 0)
1765 incoming incomings source)
8b93df01
DL
1766 (when (and (nnmail-get-value "%s-get-new-mail" method)
1767 sources)
16409b0b
GM
1768 (while (setq source (pop sources))
1769 ;; Be compatible with old values.
1770 (cond
1771 ((stringp source)
1772 (setq source
1773 (cond
1774 ((string-match "^po:" source)
1775 (list 'pop :user (substring source (match-end 0))))
1776 ((file-directory-p source)
1777 (list 'directory :path source))
1778 (t
1779 (list 'file :path source)))))
1780 ((eq source 'procmail)
1781 (message "Invalid value for nnmail-spool-file: `procmail'")
1782 nil))
1783 ;; Hack to only fetch the contents of a single group's spool file.
1784 (when (and (eq (car source) 'directory)
1785 (null nnmail-scan-directory-mail-source-once)
1786 group)
1787 (mail-source-bind (directory source)
1788 (setq source (append source
1789 (list
1790 :predicate
23f87bed
MB
1791 (gnus-byte-compile
1792 `(lambda (file)
1793 (string-equal
1794 ,(concat group suffix)
1795 (file-name-nondirectory file)))))))))
16409b0b
GM
1796 (when nnmail-fetched-sources
1797 (if (member source nnmail-fetched-sources)
1798 (setq source nil)
1799 (push source nnmail-fetched-sources)
1800 (push source fetching-sources)))))
1801 (when fetching-sources
eec82323
LMI
1802 ;; We first activate all the groups.
1803 (nnmail-activate method)
1804 ;; Allow the user to hook.
1805 (run-hooks 'nnmail-pre-get-new-mail-hook)
1806 ;; Open the message-id cache.
1807 (nnmail-cache-open)
16409b0b
GM
1808 ;; The we go through all the existing mail source specification
1809 ;; and fetch the mail from each.
1810 (while (setq source (pop fetching-sources))
1811 (nnheader-message 4 "%s: Reading incoming mail from %s..."
1812 method (car source))
1813 (when (setq new
1814 (mail-source-fetch
1815 source
23f87bed
MB
1816 (gnus-byte-compile
1817 `(lambda (file orig-file)
1818 (nnmail-split-incoming
1819 file ',(intern (format "%s-save-mail" method))
1820 ',spool-func
1821 (if (equal file orig-file)
1822 nil
1823 (nnmail-get-split-group orig-file ',source))
1824 ',(intern (format "%s-active-number" method)))))))
16409b0b
GM
1825 (incf total new)
1826 (incf i)))
eec82323 1827 ;; If we did indeed read any incoming spools, we save all info.
16409b0b
GM
1828 (if (zerop total)
1829 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1830 method (car source))
eec82323
LMI
1831 (nnmail-save-active
1832 (nnmail-get-value "%s-group-alist" method)
1833 (nnmail-get-value "%s-active-file" method))
1834 (when exit-func
1835 (funcall exit-func))
1836 (run-hooks 'nnmail-read-incoming-hook)
16409b0b
GM
1837 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1838 total))
eec82323
LMI
1839 ;; Close the message-id cache.
1840 (nnmail-cache-close)
1841 ;; Allow the user to hook.
16409b0b 1842 (run-hooks 'nnmail-post-get-new-mail-hook))))
eec82323
LMI
1843
1844(defun nnmail-expired-article-p (group time force &optional inhibit)
1845 "Say whether an article that is TIME old in GROUP should be expired."
1846 (if force
1847 t
1848 (let ((days (or (and nnmail-expiry-wait-function
1849 (funcall nnmail-expiry-wait-function group))
1850 nnmail-expiry-wait)))
1851 (cond ((or (eq days 'never)
1852 (and (not force)
1853 inhibit))
1854 ;; This isn't an expirable group.
1855 nil)
1856 ((eq days 'immediate)
1857 ;; We expire all articles on sight.
1858 t)
1859 ((equal time '(0 0))
23f87bed 1860 ;; This is an ange-ftp group, and we don't have any dates.
eec82323
LMI
1861 nil)
1862 ((numberp days)
16409b0b 1863 (setq days (days-to-time days))
eec82323 1864 ;; Compare the time with the current time.
16409b0b
GM
1865 (ignore-errors (time-less-p days (time-since time))))))))
1866
1867(defun nnmail-expiry-target-group (target group)
23f87bed
MB
1868 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1869 ;; that buffer if the nnfolder group isn't selected.
1870 (let (nnmail-cache-accepted-message-ids)
1871 ;; Don't enter Message-IDs into cache.
1872 ;; Let users hack it in TARGET function.
1873 (when (functionp target)
1874 (setq target (funcall target group)))
1875 (unless (eq target 'delete)
1876 (when (or (gnus-request-group target)
1877 (gnus-request-create-group target))
1878 (let ((group-art (gnus-request-accept-article target nil nil t)))
1879 (when (consp group-art)
1880 (gnus-group-mark-article-read target (cdr group-art))))))))
1881
1882(defun nnmail-fancy-expiry-target (group)
1883 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1884 (let* (header
1885 (case-fold-search nil)
1886 (from (or (message-fetch-field "from") ""))
1887 (to (or (message-fetch-field "to") ""))
58090a8d 1888 (date (message-fetch-field "date"))
23f87bed 1889 (target 'delete))
58090a8d
MB
1890 (setq date (if date
1891 (condition-case err
1892 (date-to-time date)
1893 (error
1894 (message "%s" (error-message-string err))
1895 (current-time)))
1896 (current-time)))
23f87bed
MB
1897 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1898 (setq header (car regexp-target-pair))
1899 (cond
1900 ;; If the header is to-from then match against the
1901 ;; To or From header
1902 ((and (equal header 'to-from)
1903 (or (string-match (cadr regexp-target-pair) from)
5255e3ff
MB
1904 (and (string-match (cadr regexp-target-pair) to)
1905 (let ((rmail-dont-reply-to-names
1906 message-dont-reply-to-names))
1907 (equal (rmail-dont-reply-to from) "")))))
23f87bed
MB
1908 (setq target (format-time-string (caddr regexp-target-pair) date)))
1909 ((and (not (equal header 'to-from))
1910 (string-match (cadr regexp-target-pair)
1911 (or
1912 (message-fetch-field header)
1913 "")))
1914 (setq target
1915 (format-time-string (caddr regexp-target-pair) date)))))))
eec82323
LMI
1916
1917(defun nnmail-check-syntax ()
1918 "Check (and modify) the syntax of the message in the current buffer."
1919 (save-restriction
1920 (message-narrow-to-head)
1921 (let ((case-fold-search t))
1922 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1923 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1924
1925(defun nnmail-write-region (start end filename &optional append visit lockname)
1926 "Do a `write-region', and then set the file modes."
1613b43a 1927 (let ((coding-system-for-write nnmail-file-coding-system)
16409b0b 1928 (file-name-coding-system nnmail-pathname-coding-system))
1613b43a
KH
1929 (write-region start end filename append visit lockname)
1930 (set-file-modes filename nnmail-default-file-modes)))
eec82323
LMI
1931
1932;;;
1933;;; Status functions
1934;;;
1935
1936(defun nnmail-replace-status (name value)
1937 "Make status NAME and VALUE part of the current status line."
1938 (save-restriction
1939 (message-narrow-to-head)
1940 (let ((status (nnmail-decode-status)))
1941 (setq status (delq (member name status) status))
1942 (when value
1943 (push (cons name value) status))
1944 (message-remove-header "status")
1945 (goto-char (point-max))
1946 (insert "Status: " (nnmail-encode-status status) "\n"))))
1947
1948(defun nnmail-decode-status ()
1949 "Return a status-value alist from STATUS."
1950 (goto-char (point-min))
1951 (when (re-search-forward "^Status: " nil t)
1952 (let (name value status)
1953 (save-restriction
1954 ;; Narrow to the status.
1955 (narrow-to-region
1956 (point)
1957 (if (re-search-forward "^[^ \t]" nil t)
1958 (1- (point))
1959 (point-max)))
1960 ;; Go through all elements and add them to the list.
1961 (goto-char (point-min))
1962 (while (re-search-forward "[^ \t=]+" nil t)
1963 (setq name (match-string 0))
16409b0b 1964 (if (not (eq (char-after) ?=))
eec82323
LMI
1965 ;; Implied "yes".
1966 (setq value "yes")
1967 (forward-char 1)
16409b0b 1968 (if (not (eq (char-after) ?\"))
eec82323
LMI
1969 (if (not (looking-at "[^ \t]"))
1970 ;; Implied "no".
1971 (setq value "no")
1972 ;; Unquoted value.
1973 (setq value (match-string 0))
1974 (goto-char (match-end 0)))
1975 ;; Quoted value.
1976 (setq value (read (current-buffer)))))
1977 (push (cons name value) status)))
1978 status)))
1979
1980(defun nnmail-encode-status (status)
1981 "Return a status string from STATUS."
1982 (mapconcat
1983 (lambda (elem)
1984 (concat
1985 (car elem) "="
1986 (if (string-match "[ \t]" (cdr elem))
1987 (prin1-to-string (cdr elem))
1988 (cdr elem))))
1989 status " "))
1990
1991(defun nnmail-split-history ()
1992 "Generate an overview of where the last mail split put articles."
1993 (interactive)
1994 (unless nnmail-split-history
1995 (error "No current split history"))
1996 (with-output-to-temp-buffer "*nnmail split history*"
16409b0b
GM
1997 (with-current-buffer standard-output
1998 (fundamental-mode)) ; for Emacs 20.4+
eec82323
LMI
1999 (let ((history nnmail-split-history)
2000 elem)
2001 (while (setq elem (pop history))
2002 (princ (mapconcat (lambda (ga)
2003 (concat (car ga) ":" (int-to-string (cdr ga))))
2004 elem
2005 ", "))
2006 (princ "\n")))))
2007
6748645f
LMI
2008(defun nnmail-purge-split-history (group)
2009 "Remove all instances of GROUP from `nnmail-split-history'."
2010 (let ((history nnmail-split-history))
2011 (while history
23f87bed 2012 (setcar history (gnus-remove-if (lambda (e) (string= (car e) group))
6748645f
LMI
2013 (car history)))
2014 (pop history))
2015 (setq nnmail-split-history (delq nil nnmail-split-history))))
2016
eec82323
LMI
2017(defun nnmail-new-mail-p (group)
2018 "Say whether GROUP has new mail."
2019 (let ((his nnmail-split-history)
2020 found)
2021 (while his
2022 (when (assoc group (pop his))
2023 (setq found t
2024 his nil)))
2025 found))
2026
6748645f
LMI
2027(defun nnmail-within-headers-p ()
2028 "Check to see if point is within the headers of a unix mail message.
2029Doesn't change point."
2030 (let ((pos (point)))
2031 (save-excursion
2032 (and (nnmail-search-unix-mail-delim-backward)
2033 (not (search-forward "\n\n" pos t))))))
2034
eec82323
LMI
2035(run-hooks 'nnmail-load-hook)
2036
2037(provide 'nnmail)
2038
ab5796a9 2039;;; arch-tag: fe8f671a-50db-428a-bb5d-f00462f72ed7
eec82323 2040;;; nnmail.el ends here