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