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