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