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