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