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