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