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