Fix bug#6820: Don't accept backslashes or quotes on compiler options.
[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\";
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
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
LMI
965
966(defun nnmail-process-mmdf-mail-format (func artnum-func)
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)
eec82323
LMI
1014 (nnmail-check-duplication message-id func artnum-func)
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
LMI
1058(defun nnmail-split-incoming (incoming func &optional exit-func
1059 group artnum-func)
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")
1090 (nnmail-process-mmdf-mail-format func artnum-func))
1091 ((looking-at "Return-Path:")
1092 (nnmail-process-maildir-mail-format func artnum-func))
1093 (t
1094 (nnmail-process-unix-mail-format func artnum-func))))
1095 (when exit-func
1096 (funcall exit-func))
1097 (kill-buffer (current-buffer))))))
eec82323 1098
6748645f 1099(defun nnmail-article-group (func &optional trace)
eec82323
LMI
1100 "Look at the headers and return an alist of groups that match.
1101FUNC will be called with the group name to determine the article number."
23f87bed 1102 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
eec82323 1103 (obuf (current-buffer))
23f87bed 1104 group-art method grp)
6748645f 1105 (if (and (sequencep methods)
20a673b2
KY
1106 (= (length methods) 1)
1107 (not nnmail-inhibit-default-split-group))
eec82323
LMI
1108 ;; If there is only just one group to put everything in, we
1109 ;; just return a list with just this one method in.
1110 (setq group-art
1111 (list (cons (caar methods) (funcall func (caar methods)))))
1112 ;; We do actual comparison.
20a673b2
KY
1113 ;; Copy the article into the work buffer.
1114 (with-current-buffer nntp-server-buffer
eec82323 1115 (erase-buffer)
23f87bed
MB
1116 (insert-buffer-substring obuf)
1117 ;; Narrow to headers.
1118 (narrow-to-region
1119 (goto-char (point-min))
1120 (if (search-forward "\n\n" nil t)
1121 (point)
1122 (point-max)))
1123 (goto-char (point-min))
1124 ;; Decode MIME headers and charsets.
1125 (when nnmail-mail-splitting-decodes
1126 (let ((mail-parse-charset nnmail-mail-splitting-charset))
1127 (mail-decode-encoded-word-region (point-min) (point-max))))
eec82323
LMI
1128 ;; Fold continuation lines.
1129 (goto-char (point-min))
1130 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1131 (replace-match " " t t))
6748645f
LMI
1132 ;; Nuke pathologically long headers. Since Gnus applies
1133 ;; pathologically complex regexps to the buffer, lines
1134 ;; that are looong will take longer than the Universe's
1135 ;; existence to process.
1136 (goto-char (point-min))
1137 (while (not (eobp))
16409b0b
GM
1138 (unless (< (move-to-column nnmail-split-header-length-limit)
1139 nnmail-split-header-length-limit)
01c52d31 1140 (delete-region (point) (point-at-eol)))
16409b0b 1141 (forward-line 1))
eec82323 1142 ;; Allow washing.
6748645f 1143 (goto-char (point-min))
eec82323 1144 (run-hooks 'nnmail-split-hook)
6748645f
LMI
1145 (when (setq nnmail-split-tracing trace)
1146 (setq nnmail-split-trace nil))
eec82323
LMI
1147 (if (and (symbolp nnmail-split-methods)
1148 (fboundp nnmail-split-methods))
1149 (let ((split
06dc8321 1150 (condition-case error-info
a8151ef7
LMI
1151 ;; `nnmail-split-methods' is a function, so we
1152 ;; just call this function here and use the
1153 ;; result.
eec82323 1154 (or (funcall nnmail-split-methods)
20a673b2
KY
1155 (and (not nnmail-inhibit-default-split-group)
1156 '("bogus")))
eec82323 1157 (error
23f87bed 1158 (nnheader-message
06dc8321 1159 5 "Error in `nnmail-split-methods'; using `bogus' mail group: %S" error-info)
eec82323
LMI
1160 (sit-for 1)
1161 '("bogus")))))
8753ddee 1162 (setq split (mm-delete-duplicates split))
a8151ef7
LMI
1163 ;; The article may be "cross-posted" to `junk'. What
1164 ;; to do? Just remove the `junk' spec. Don't really
1165 ;; see anything else to do...
1166 (let (elem)
1167 (while (setq elem (car (memq 'junk split)))
1168 (setq split (delq elem split))))
1169 (when split
eec82323
LMI
1170 (setq group-art
1171 (mapcar
1172 (lambda (group) (cons group (funcall func group)))
1173 split))))
1174 ;; Go through the split methods to find a match.
6748645f
LMI
1175 (while (and methods
1176 (or nnmail-crosspost
1177 (not group-art)))
eec82323 1178 (goto-char (point-max))
6748645f 1179 (setq method (pop methods)
16409b0b 1180 grp (car method))
eec82323
LMI
1181 (if (or methods
1182 (not (equal "" (nth 1 method))))
1183 (when (and
1184 (ignore-errors
1185 (if (stringp (nth 1 method))
16409b0b
GM
1186 (let ((expand (string-match "\\\\[0-9&]" grp))
1187 (pos (re-search-backward (cadr method)
1188 nil t)))
1189 (and expand
1190 (setq grp (nnmail-expand-newtext grp)))
1191 pos)
eec82323 1192 ;; Function to say whether this is a match.
16409b0b 1193 (funcall (nth 1 method) grp)))
eec82323
LMI
1194 ;; Don't enter the article into the same
1195 ;; group twice.
16409b0b
GM
1196 (not (assoc grp group-art)))
1197 (push (cons grp (funcall func grp))
eec82323
LMI
1198 group-art))
1199 ;; This is the final group, which is used as a
1200 ;; catch-all.
20a673b2
KY
1201 (when (and (not group-art)
1202 (not nnmail-inhibit-default-split-group))
eec82323
LMI
1203 (setq group-art
1204 (list (cons (car method)
23f87bed
MB
1205 (funcall func (car method))))))))
1206 ;; Fall back on "bogus" if all else fails.
20a673b2
KY
1207 (when (and (not group-art)
1208 (not nnmail-inhibit-default-split-group))
23f87bed 1209 (setq group-art (list (cons "bogus" (funcall func "bogus"))))))
6748645f
LMI
1210 ;; Produce a trace if non-empty.
1211 (when (and trace nnmail-split-trace)
23f87bed 1212 (let ((restore (current-buffer)))
6748645f
LMI
1213 (nnheader-set-temp-buffer "*Split Trace*")
1214 (gnus-add-buffer)
23f87bed
MB
1215 (dolist (trace (nreverse nnmail-split-trace))
1216 (prin1 trace (current-buffer))
1217 (insert "\n"))
6748645f
LMI
1218 (goto-char (point-min))
1219 (gnus-configure-windows 'split-trace)
1220 (set-buffer restore)))
23f87bed 1221 (widen)
eec82323
LMI
1222 ;; See whether the split methods returned `junk'.
1223 (if (equal group-art '(junk))
1224 nil
a8151ef7
LMI
1225 ;; The article may be "cross-posted" to `junk'. What
1226 ;; to do? Just remove the `junk' spec. Don't really
1227 ;; see anything else to do...
1228 (let (elem)
1229 (while (setq elem (car (memq 'junk group-art)))
1230 (setq group-art (delq elem group-art)))
1231 (nreverse group-art)))))))
eec82323
LMI
1232
1233(defun nnmail-insert-lines ()
1234 "Insert how many lines there are in the body of the mail.
1235Return the number of characters in the body."
1236 (let (lines chars)
1237 (save-excursion
1238 (goto-char (point-min))
a1506d29 1239 (unless (search-forward "\n\n" nil t)
16409b0b
GM
1240 (goto-char (point-max))
1241 (insert "\n"))
1242 (setq chars (- (point-max) (point)))
1243 (setq lines (count-lines (point) (point-max)))
1244 (forward-char -1)
1245 (save-excursion
1246 (when (re-search-backward "^Lines: " nil t)
1247 (delete-region (point) (progn (forward-line 1) (point)))))
1248 (beginning-of-line)
1249 (insert (format "Lines: %d\n" (max lines 0)))
1250 chars)))
eec82323
LMI
1251
1252(defun nnmail-insert-xref (group-alist)
1253 "Insert an Xref line based on the (group . article) alist."
1254 (save-excursion
1255 (goto-char (point-min))
16409b0b
GM
1256 (unless (search-forward "\n\n" nil t)
1257 (goto-char (point-max))
1258 (insert "\n"))
1259 (forward-char -1)
1260 (when (re-search-backward "^Xref: " nil t)
1261 (delete-region (match-beginning 0)
1262 (progn (forward-line 1) (point))))
1263 (insert (format "Xref: %s" (system-name)))
1264 (while group-alist
01c52d31
MB
1265 (insert (if (mm-multibyte-p)
1266 (mm-string-as-multibyte
1267 (format " %s:%d" (caar group-alist) (cdar group-alist)))
1268 (mm-string-as-unibyte
1269 (format " %s:%d" (caar group-alist) (cdar group-alist)))))
16409b0b
GM
1270 (setq group-alist (cdr group-alist)))
1271 (insert "\n")))
eec82323
LMI
1272
1273;;; Message washing functions
1274
1275(defun nnmail-remove-leading-whitespace ()
1276 "Remove excessive whitespace from all headers."
1277 (goto-char (point-min))
1278 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1279 (replace-match "\\1" t)))
1280
1281(defun nnmail-remove-list-identifiers ()
1282 "Remove list identifiers from Subject headers."
23f87bed
MB
1283 (let ((regexp
1284 (if (consp nnmail-list-identifiers)
1285 (mapconcat 'identity nnmail-list-identifiers " *\\|")
1286 nnmail-list-identifiers)))
eec82323
LMI
1287 (when regexp
1288 (goto-char (point-min))
23f87bed
MB
1289 (while (re-search-forward
1290 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1291 nil t)
1292 (delete-region (match-beginning 2) (match-end 0))
1293 (beginning-of-line))
1294 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1295 nil t)
1296 (delete-region (match-beginning 1) (match-end 1))
1297 (beginning-of-line)))))
eec82323
LMI
1298
1299(defun nnmail-remove-tabs ()
1300 "Translate TAB characters into SPACE characters."
1301 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1302
01c52d31
MB
1303(defcustom nnmail-broken-references-mailers
1304 "^X-Mailer:.*\\(Eudora\\|Pegasus\\)"
1305 "Header line matching mailer producing bogus References lines.
1306See `nnmail-ignore-broken-references'."
1307 :group 'nnmail-prepare
330f707b 1308 :version "23.1" ;; No Gnus
01c52d31
MB
1309 :type 'regexp)
1310
1311(defun nnmail-ignore-broken-references ()
1312 "Ignore the References line and use In-Reply-To
1313
1314Eudora has a broken References line, but an OK In-Reply-To."
16409b0b 1315 (goto-char (point-min))
01c52d31 1316 (when (re-search-forward nnmail-broken-references-mailers nil t)
16409b0b
GM
1317 (goto-char (point-min))
1318 (when (re-search-forward "^References:" nil t)
1319 (beginning-of-line)
1320 (insert "X-Gnus-Broken-Eudora-"))
1321 (goto-char (point-min))
23f87bed
MB
1322 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1323 (replace-match "\\1" t))))
eec82323 1324
01c52d31 1325(defalias 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references)
265ac10b 1326(make-obsolete 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references "Emacs 23.1")
01c52d31 1327
16409b0b 1328(custom-add-option 'nnmail-prepare-incoming-header-hook
01c52d31 1329 'nnmail-ignore-broken-references)
eec82323 1330
16409b0b 1331;;; Utility functions
eec82323 1332
163a3c6a
GM
1333(declare-function gnus-activate-group "gnus-start"
1334 (group &optional scan dont-check method))
1335
23f87bed
MB
1336(defun nnmail-do-request-post (accept-func &optional server)
1337 "Utility function to directly post a message to an nnmail-derived group.
1338Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1339to actually put the message in the right group."
1340 (let ((success t))
1341 (dolist (mbx (message-unquote-tokens
1342 (message-tokenize-header
1343 (message-fetch-field "Newsgroups") ", ")) success)
1344 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1345 (or (gnus-active to-newsgroup)
1346 (gnus-activate-group to-newsgroup)
1347 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1348 to-newsgroup))
1349 (or (and (gnus-request-create-group
1350 to-newsgroup gnus-command-method)
1351 (gnus-activate-group to-newsgroup nil nil
1352 gnus-command-method))
1353 (error "Couldn't create group %s" to-newsgroup)))
1354 (error "No such group: %s" to-newsgroup))
1355 (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1356 (setq success nil))))))
1357
eec82323
LMI
1358(defun nnmail-split-fancy ()
1359 "Fancy splitting method.
23f87bed 1360See the documentation for the variable `nnmail-split-fancy' for details."
01c52d31
MB
1361 (with-syntax-table nnmail-split-fancy-syntax-table
1362 (nnmail-split-it nnmail-split-fancy)))
eec82323
LMI
1363
1364(defvar nnmail-split-cache nil)
1365;; Alist of split expressions their equivalent regexps.
1366
1367(defun nnmail-split-it (split)
1368 ;; Return a list of groups matching SPLIT.
6748645f
LMI
1369 (let (cached-pair)
1370 (cond
1371 ;; nil split
1372 ((null split)
1373 nil)
1374
1375 ;; A group name. Do the \& and \N subs into the string.
1376 ((stringp split)
1377 (when nnmail-split-tracing
23f87bed 1378 (push split nnmail-split-trace))
6748645f
LMI
1379 (list (nnmail-expand-newtext split)))
1380
1381 ;; Junk the message.
1382 ((eq split 'junk)
1383 (when nnmail-split-tracing
1384 (push "junk" nnmail-split-trace))
1385 (list 'junk))
1386
1387 ;; Builtin & operation.
1388 ((eq (car split) '&)
1389 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1390
1391 ;; Builtin | operation.
1392 ((eq (car split) '|)
1393 (let (done)
1394 (while (and (not done) (cdr split))
1395 (setq split (cdr split)
1396 done (nnmail-split-it (car split))))
1397 done))
1398
1399 ;; Builtin : operation.
1400 ((eq (car split) ':)
23f87bed
MB
1401 (when nnmail-split-tracing
1402 (push split nnmail-split-trace))
6748645f
LMI
1403 (nnmail-split-it (save-excursion (eval (cdr split)))))
1404
16409b0b
GM
1405 ;; Builtin ! operation.
1406 ((eq (car split) '!)
1407 (funcall (cadr split) (nnmail-split-it (caddr split))))
1408
6748645f
LMI
1409 ;; Check the cache for the regexp for this split.
1410 ((setq cached-pair (assq split nnmail-split-cache))
16409b0b
GM
1411 (let (split-result
1412 (end-point (point-max))
1413 (value (nth 1 split)))
1414 (if (symbolp value)
1415 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1416 (while (and (goto-char end-point)
1417 (re-search-backward (cdr cached-pair) nil t))
1418 (when nnmail-split-tracing
23f87bed 1419 (push split nnmail-split-trace))
16409b0b
GM
1420 (let ((split-rest (cddr split))
1421 (end (match-end 0))
23f87bed
MB
1422 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1423 ;; So, start-of-value is the point just before the
1424 ;; beginning of the value, whereas after-header-name
1425 ;; is the point just after the field name.
16409b0b
GM
1426 (start-of-value (match-end 1))
1427 (after-header-name (match-end 2)))
1428 ;; Start the next search just before the beginning of the
1429 ;; VALUE match.
1430 (setq end-point (1- start-of-value))
1431 ;; Handle - RESTRICTs
1432 (while (eq (car split-rest) '-)
1433 ;; RESTRICT must start after-header-name and
1434 ;; end after start-of-value, so that, for
1435 ;; (any "foo" - "x-foo" "foo.list")
1436 ;; we do not exclude foo.list just because
1437 ;; the header is: ``To: x-foo, foo''
1438 (goto-char end)
1439 (if (and (re-search-backward (cadr split-rest)
1440 after-header-name t)
1441 (> (match-end 0) start-of-value))
1442 (setq split-rest nil)
1443 (setq split-rest (cddr split-rest))))
1444 (when split-rest
1445 (goto-char end)
1446 (let ((value (nth 1 split)))
1447 (if (symbolp value)
1448 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1449 ;; Someone might want to do a \N sub on this match, so get the
1450 ;; correct match positions.
1451 (re-search-backward value start-of-value))
1452 (dolist (sp (nnmail-split-it (car split-rest)))
23f87bed 1453 (unless (member sp split-result)
16409b0b
GM
1454 (push sp split-result))))))
1455 split-result))
6748645f
LMI
1456
1457 ;; Not in cache, compute a regexp for the field/value pair.
1458 (t
61e66a15
MB
1459 (let ((field (nth 0 split))
1460 (value (nth 1 split))
1461 (split-rest (cddr split))
1462 partial-front
1463 partial-rear
1464 regexp)
16409b0b
GM
1465 (if (symbolp value)
1466 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1467 (if (and (>= (length value) 2)
1468 (string= ".*" (substring value 0 2)))
1469 (setq value (substring value 2)
23f87bed
MB
1470 partial-front ""))
1471 ;; Same trick for the rear of the regexp
1472 (if (and (>= (length value) 2)
1473 (string= ".*" (substring value -2)))
1474 (setq value (substring value 0 -2)
1475 partial-rear ""))
61e66a15
MB
1476 ;; Invert the match-partial-words behavior if the optional
1477 ;; last element is specified.
1478 (while (eq (car split-rest) '-)
1479 (setq split-rest (cddr split-rest)))
1480 (when (if (cadr split-rest)
1481 (not nnmail-split-fancy-match-partial-words)
1482 nnmail-split-fancy-match-partial-words)
23f87bed
MB
1483 (setq partial-front ""
1484 partial-rear ""))
16409b0b 1485 (setq regexp (concat "^\\(\\("
6748645f
LMI
1486 (if (symbolp field)
1487 (cdr (assq field nnmail-split-abbrev-alist))
1488 field)
16409b0b 1489 "\\):.*\\)"
23f87bed 1490 (or partial-front "\\<")
16409b0b
GM
1491 "\\("
1492 value
23f87bed
MB
1493 "\\)"
1494 (or partial-rear "\\>")))
6748645f
LMI
1495 (push (cons split regexp) nnmail-split-cache)
1496 ;; Now that it's in the cache, just call nnmail-split-it again
61e66a15 1497 ;; on the same split, which will find it immediately in the cache.
6748645f 1498 (nnmail-split-it split))))))
eec82323
LMI
1499
1500(defun nnmail-expand-newtext (newtext)
1501 (let ((len (length newtext))
1502 (pos 0)
1503 c expanded beg N did-expand)
1504 (while (< pos len)
1505 (setq beg pos)
1506 (while (and (< pos len)
1507 (not (= (aref newtext pos) ?\\)))
1508 (setq pos (1+ pos)))
1509 (unless (= beg pos)
1510 (push (substring newtext beg pos) expanded))
1511 (when (< pos len)
6748645f
LMI
1512 ;; We hit a \; expand it.
1513 (setq did-expand t
1514 pos (1+ pos)
1515 c (aref newtext pos))
eec82323
LMI
1516 (if (not (or (= c ?\&)
1517 (and (>= c ?1)
1518 (<= c ?9))))
6748645f 1519 ;; \ followed by some character we don't expand.
eec82323
LMI
1520 (push (char-to-string c) expanded)
1521 ;; \& or \N
1522 (if (= c ?\&)
1523 (setq N 0)
1524 (setq N (- c ?0)))
1525 (when (match-beginning N)
23f87bed
MB
1526 (push (if nnmail-split-lowercase-expanded
1527 (downcase (buffer-substring (match-beginning N)
1528 (match-end N)))
1529 (buffer-substring (match-beginning N) (match-end N)))
eec82323
LMI
1530 expanded))))
1531 (setq pos (1+ pos)))
1532 (if did-expand
1533 (apply 'concat (nreverse expanded))
1534 newtext)))
1535
eec82323
LMI
1536;; Activate a backend only if it isn't already activated.
1537;; If FORCE, re-read the active file even if the backend is
1538;; already activated.
1539(defun nnmail-activate (backend &optional force)
6748645f 1540 (nnheader-init-server-buffer)
eec82323
LMI
1541 (let (file timestamp file-time)
1542 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1543 force
1544 (and (setq file (ignore-errors
1545 (symbol-value (intern (format "%s-active-file"
1546 backend)))))
1547 (setq file-time (nth 5 (file-attributes file)))
1548 (or (not
1549 (setq timestamp
1550 (condition-case ()
1551 (symbol-value (intern
1552 (format "%s-active-timestamp"
1553 backend)))
1554 (error 'none))))
1555 (not (consp timestamp))
1556 (equal timestamp '(0 0))
1557 (> (nth 0 file-time) (nth 0 timestamp))
1558 (and (= (nth 0 file-time) (nth 0 timestamp))
1559 (> (nth 1 file-time) (nth 1 timestamp))))))
1560 (save-excursion
1561 (or (eq timestamp 'none)
1562 (set (intern (format "%s-active-timestamp" backend))
1563 file-time))
1564 (funcall (intern (format "%s-request-list" backend)))))
1565 t))
1566
1567(defun nnmail-message-id ()
1568 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1569
1570;;;
1571;;; nnmail duplicate handling
1572;;;
1573
1574(defvar nnmail-cache-buffer nil)
1575
1576(defun nnmail-cache-open ()
1577 (if (or (not nnmail-treat-duplicates)
1578 (and nnmail-cache-buffer
1579 (buffer-name nnmail-cache-buffer)))
1580 () ; The buffer is open.
20a673b2 1581 (with-current-buffer
eec82323 1582 (setq nnmail-cache-buffer
20a673b2 1583 (get-buffer-create " *nnmail message-id cache*"))
23f87bed 1584 (gnus-add-buffer)
eec82323
LMI
1585 (when (file-exists-p nnmail-message-id-cache-file)
1586 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1587 (set-buffer-modified-p nil)
1588 (current-buffer))))
1589
1590(defun nnmail-cache-close ()
1591 (when (and nnmail-cache-buffer
1592 nnmail-treat-duplicates
1593 (buffer-name nnmail-cache-buffer)
1594 (buffer-modified-p nnmail-cache-buffer))
20a673b2 1595 (with-current-buffer nnmail-cache-buffer
eec82323
LMI
1596 ;; Weed out the excess number of Message-IDs.
1597 (goto-char (point-max))
1598 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1599 (progn
1600 (beginning-of-line)
1601 (delete-region (point-min) (point))))
1602 ;; Save the buffer.
1603 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1604 (make-directory (file-name-directory nnmail-message-id-cache-file)
1605 t))
1606 (nnmail-write-region (point-min) (point-max)
1607 nnmail-message-id-cache-file nil 'silent)
1608 (set-buffer-modified-p nil)
1609 (setq nnmail-cache-buffer nil)
23f87bed 1610 (gnus-kill-buffer (current-buffer)))))
eec82323 1611
16409b0b
GM
1612;; Compiler directives.
1613(defvar group)
1614(defvar group-art-list)
1615(defvar group-art)
23f87bed
MB
1616(defun nnmail-cache-insert (id grp &optional subject sender)
1617 (when (stringp id)
1618 ;; this will handle cases like `B r' where the group is nil
1619 (let ((grp (or grp gnus-newsgroup-name "UNKNOWN")))
bf247b6e 1620 (run-hook-with-args 'nnmail-spool-hook
23f87bed
MB
1621 id grp subject sender))
1622 (when nnmail-treat-duplicates
1623 ;; Store some information about the group this message is written
1624 ;; to. This is passed in as the grp argument -- all locations this
1625 ;; has been called from have been checked and the group is available.
1626 ;; The only ambiguous case is nnmail-check-duplication which will only
1627 ;; pass the first (of possibly >1) group which matches. -Josh
16409b0b 1628 (unless (gnus-buffer-live-p nnmail-cache-buffer)
23f87bed 1629 (nnmail-cache-open))
20a673b2 1630 (with-current-buffer nnmail-cache-buffer
23f87bed
MB
1631 (goto-char (point-max))
1632 (if (and grp (not (string= "" grp))
1633 (gnus-methods-equal-p gnus-command-method
1634 (nnmail-cache-primary-mail-backend)))
1635 (let ((regexp (if (consp nnmail-cache-ignore-groups)
1636 (mapconcat 'identity nnmail-cache-ignore-groups
1637 "\\|")
1638 nnmail-cache-ignore-groups)))
1639 (unless (and regexp (string-match regexp grp))
1640 (insert id "\t" grp "\n")))
1641 (insert id "\n"))))))
bf247b6e 1642
16409b0b
GM
1643(defun nnmail-cache-primary-mail-backend ()
1644 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
23f87bed
MB
1645 (be nil)
1646 (res nil)
1647 (get-new-mail nil))
16409b0b
GM
1648 (while (and (null res) be-list)
1649 (setq be (car be-list))
1650 (setq be-list (cdr be-list))
1651 (when (and (gnus-method-option-p be 'respool)
23f87bed
MB
1652 (setq get-new-mail
1653 (intern (format "%s-get-new-mail" (car be))))
1654 (boundp get-new-mail)
1655 (symbol-value get-new-mail))
1656 (setq res be)))
16409b0b
GM
1657 res))
1658
1659;; Fetch the group name corresponding to the message id stored in the
1660;; cache.
1661(defun nnmail-cache-fetch-group (id)
1662 (when (and nnmail-treat-duplicates nnmail-cache-buffer)
20a673b2 1663 (with-current-buffer nnmail-cache-buffer
eec82323 1664 (goto-char (point-max))
16409b0b 1665 (when (search-backward id nil t)
23f87bed
MB
1666 (beginning-of-line)
1667 (skip-chars-forward "^\n\r\t")
1668 (unless (looking-at "[\r\n]")
1669 (forward-char 1)
01c52d31 1670 (buffer-substring (point) (point-at-eol)))))))
16409b0b
GM
1671
1672;; Function for nnmail-split-fancy: look up all references in the
1673;; cache and if a match is found, return that group.
1674(defun nnmail-split-fancy-with-parent ()
23f87bed
MB
1675 "Split this message into the same group as its parent.
1676This function can be used as an entry in `nnmail-split-fancy', for
1677example like this: (: nnmail-split-fancy-with-parent)
1678For a message to be split, it looks for the parent message in the
1679References or In-Reply-To header and then looks in the message id
1680cache file (given by the variable `nnmail-message-id-cache-file') to
1681see which group that message was put in. This group is returned.
1682
1683See the Info node `(gnus)Fancy Mail Splitting' for more details."
16409b0b 1684 (let* ((refstr (or (message-fetch-field "references")
23f87bed
MB
1685 (message-fetch-field "in-reply-to")))
1686 (references nil)
1687 (res nil)
1688 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1689 (mapconcat
1690 (lambda (x) (format "\\(%s\\)" x))
1691 nnmail-split-fancy-with-parent-ignore-groups
1692 "\\|")
1693 nnmail-split-fancy-with-parent-ignore-groups)))
16409b0b
GM
1694 (when refstr
1695 (setq references (nreverse (gnus-split-references refstr)))
1696 (unless (gnus-buffer-live-p nnmail-cache-buffer)
23f87bed 1697 (nnmail-cache-open))
01c52d31
MB
1698 (dolist (x references)
1699 (setq res (or (nnmail-cache-fetch-group x) res))
1700 (when (or (member res '("delayed" "drafts" "queue"))
1701 (and regexp res (string-match regexp res)))
1702 (setq res nil)))
16409b0b 1703 res)))
eec82323
LMI
1704
1705(defun nnmail-cache-id-exists-p (id)
1706 (when nnmail-treat-duplicates
20a673b2 1707 (with-current-buffer nnmail-cache-buffer
eec82323
LMI
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))
16409b0b
GM
1827 (when (setq new
1828 (mail-source-fetch
1829 source
23f87bed
MB
1830 (gnus-byte-compile
1831 `(lambda (file orig-file)
1832 (nnmail-split-incoming
1833 file ',(intern (format "%s-save-mail" method))
1834 ',spool-func
a1da1e37
MB
1835 (or in-group
1836 (if (equal file orig-file)
1837 nil
1838 (nnmail-get-split-group orig-file ',source)))
23f87bed 1839 ',(intern (format "%s-active-number" method)))))))
16409b0b
GM
1840 (incf total new)
1841 (incf i)))
eec82323 1842 ;; If we did indeed read any incoming spools, we save all info.
16409b0b 1843 (if (zerop total)
8c3e17f8
LMI
1844 (when mail-source-plugged
1845 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1846 method (car source)))
e9d1aaff
KY
1847 (nnmail-save-active
1848 (nnmail-get-value "%s-group-alist" method)
1849 (nnmail-get-value "%s-active-file" method))
eec82323
LMI
1850 (when exit-func
1851 (funcall exit-func))
1852 (run-hooks 'nnmail-read-incoming-hook)
16409b0b
GM
1853 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1854 total))
eec82323
LMI
1855 ;; Close the message-id cache.
1856 (nnmail-cache-close)
1857 ;; Allow the user to hook.
16409b0b 1858 (run-hooks 'nnmail-post-get-new-mail-hook))))
eec82323
LMI
1859
1860(defun nnmail-expired-article-p (group time force &optional inhibit)
0617bb00
LMI
1861 "Say whether an article that is TIME old in GROUP should be expired.
1862If TIME is nil, then return the cutoff time for oldness instead."
eec82323 1863 (if force
0617bb00
LMI
1864 (if (null time)
1865 (current-time)
1866 t)
eec82323
LMI
1867 (let ((days (or (and nnmail-expiry-wait-function
1868 (funcall nnmail-expiry-wait-function group))
1869 nnmail-expiry-wait)))
1870 (cond ((or (eq days 'never)
1871 (and (not force)
1872 inhibit))
1873 ;; This isn't an expirable group.
1874 nil)
1875 ((eq days 'immediate)
1876 ;; We expire all articles on sight.
0617bb00
LMI
1877 (if (null time)
1878 (current-time)
1879 t))
eec82323 1880 ((equal time '(0 0))
23f87bed 1881 ;; This is an ange-ftp group, and we don't have any dates.
eec82323
LMI
1882 nil)
1883 ((numberp days)
16409b0b 1884 (setq days (days-to-time days))
eec82323 1885 ;; Compare the time with the current time.
0617bb00
LMI
1886 (if (null time)
1887 (time-subtract (current-time) days)
1888 (ignore-errors (time-less-p days (time-since time)))))))))
16409b0b 1889
163a3c6a
GM
1890(declare-function gnus-group-mark-article-read "gnus-group" (group article))
1891
16409b0b 1892(defun nnmail-expiry-target-group (target group)
23f87bed
MB
1893 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1894 ;; that buffer if the nnfolder group isn't selected.
1895 (let (nnmail-cache-accepted-message-ids)
1896 ;; Don't enter Message-IDs into cache.
1897 ;; Let users hack it in TARGET function.
1898 (when (functionp target)
1899 (setq target (funcall target group)))
1900 (unless (eq target 'delete)
1901 (when (or (gnus-request-group target)
1902 (gnus-request-create-group target))
1903 (let ((group-art (gnus-request-accept-article target nil nil t)))
1904 (when (consp group-art)
1905 (gnus-group-mark-article-read target (cdr group-art))))))))
1906
1907(defun nnmail-fancy-expiry-target (group)
1908 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1909 (let* (header
1910 (case-fold-search nil)
1911 (from (or (message-fetch-field "from") ""))
1912 (to (or (message-fetch-field "to") ""))
58090a8d 1913 (date (message-fetch-field "date"))
23f87bed 1914 (target 'delete))
58090a8d
MB
1915 (setq date (if date
1916 (condition-case err
1917 (date-to-time date)
1918 (error
1919 (message "%s" (error-message-string err))
1920 (current-time)))
1921 (current-time)))
23f87bed
MB
1922 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1923 (setq header (car regexp-target-pair))
1924 (cond
1925 ;; If the header is to-from then match against the
1926 ;; To or From header
1927 ((and (equal header 'to-from)
1928 (or (string-match (cadr regexp-target-pair) from)
5255e3ff
MB
1929 (and (string-match (cadr regexp-target-pair) to)
1930 (let ((rmail-dont-reply-to-names
01c52d31 1931 (message-dont-reply-to-names)))
5255e3ff 1932 (equal (rmail-dont-reply-to from) "")))))
23f87bed
MB
1933 (setq target (format-time-string (caddr regexp-target-pair) date)))
1934 ((and (not (equal header 'to-from))
1935 (string-match (cadr regexp-target-pair)
1936 (or
1937 (message-fetch-field header)
1938 "")))
1939 (setq target
1940 (format-time-string (caddr regexp-target-pair) date)))))))
eec82323
LMI
1941
1942(defun nnmail-check-syntax ()
1943 "Check (and modify) the syntax of the message in the current buffer."
1944 (save-restriction
1945 (message-narrow-to-head)
1946 (let ((case-fold-search t))
1947 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1948 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1949
1950(defun nnmail-write-region (start end filename &optional append visit lockname)
1951 "Do a `write-region', and then set the file modes."
1613b43a 1952 (let ((coding-system-for-write nnmail-file-coding-system)
16409b0b 1953 (file-name-coding-system nnmail-pathname-coding-system))
1613b43a
KH
1954 (write-region start end filename append visit lockname)
1955 (set-file-modes filename nnmail-default-file-modes)))
eec82323
LMI
1956
1957;;;
1958;;; Status functions
1959;;;
1960
1961(defun nnmail-replace-status (name value)
1962 "Make status NAME and VALUE part of the current status line."
1963 (save-restriction
1964 (message-narrow-to-head)
1965 (let ((status (nnmail-decode-status)))
1966 (setq status (delq (member name status) status))
1967 (when value
1968 (push (cons name value) status))
1969 (message-remove-header "status")
1970 (goto-char (point-max))
1971 (insert "Status: " (nnmail-encode-status status) "\n"))))
1972
1973(defun nnmail-decode-status ()
1974 "Return a status-value alist from STATUS."
1975 (goto-char (point-min))
1976 (when (re-search-forward "^Status: " nil t)
1977 (let (name value status)
1978 (save-restriction
1979 ;; Narrow to the status.
1980 (narrow-to-region
1981 (point)
1982 (if (re-search-forward "^[^ \t]" nil t)
1983 (1- (point))
1984 (point-max)))
1985 ;; Go through all elements and add them to the list.
1986 (goto-char (point-min))
1987 (while (re-search-forward "[^ \t=]+" nil t)
1988 (setq name (match-string 0))
16409b0b 1989 (if (not (eq (char-after) ?=))
eec82323
LMI
1990 ;; Implied "yes".
1991 (setq value "yes")
1992 (forward-char 1)
16409b0b 1993 (if (not (eq (char-after) ?\"))
eec82323
LMI
1994 (if (not (looking-at "[^ \t]"))
1995 ;; Implied "no".
1996 (setq value "no")
1997 ;; Unquoted value.
1998 (setq value (match-string 0))
1999 (goto-char (match-end 0)))
2000 ;; Quoted value.
2001 (setq value (read (current-buffer)))))
2002 (push (cons name value) status)))
2003 status)))
2004
2005(defun nnmail-encode-status (status)
2006 "Return a status string from STATUS."
2007 (mapconcat
2008 (lambda (elem)
2009 (concat
2010 (car elem) "="
2011 (if (string-match "[ \t]" (cdr elem))
2012 (prin1-to-string (cdr elem))
2013 (cdr elem))))
2014 status " "))
2015
2016(defun nnmail-split-history ()
2017 "Generate an overview of where the last mail split put articles."
2018 (interactive)
2019 (unless nnmail-split-history
2020 (error "No current split history"))
2021 (with-output-to-temp-buffer "*nnmail split history*"
16409b0b
GM
2022 (with-current-buffer standard-output
2023 (fundamental-mode)) ; for Emacs 20.4+
01c52d31 2024 (dolist (elem nnmail-split-history)
eec82323
LMI
2025 (princ (mapconcat (lambda (ga)
2026 (concat (car ga) ":" (int-to-string (cdr ga))))
2027 elem
2028 ", "))
01c52d31 2029 (princ "\n"))))
eec82323 2030
6748645f
LMI
2031(defun nnmail-purge-split-history (group)
2032 "Remove all instances of GROUP from `nnmail-split-history'."
2033 (let ((history nnmail-split-history))
2034 (while history
23f87bed 2035 (setcar history (gnus-remove-if (lambda (e) (string= (car e) group))
6748645f
LMI
2036 (car history)))
2037 (pop history))
2038 (setq nnmail-split-history (delq nil nnmail-split-history))))
2039
eec82323
LMI
2040(defun nnmail-new-mail-p (group)
2041 "Say whether GROUP has new mail."
2042 (let ((his nnmail-split-history)
2043 found)
2044 (while his
2045 (when (assoc group (pop his))
2046 (setq found t
2047 his nil)))
2048 found))
2049
6748645f
LMI
2050(defun nnmail-within-headers-p ()
2051 "Check to see if point is within the headers of a unix mail message.
2052Doesn't change point."
2053 (let ((pos (point)))
2054 (save-excursion
2055 (and (nnmail-search-unix-mail-delim-backward)
2056 (not (search-forward "\n\n" pos t))))))
2057
eec82323
LMI
2058(run-hooks 'nnmail-load-hook)
2059
2060(provide 'nnmail)
2061
2062;;; nnmail.el ends here