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