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