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