1c51c62828374b0780b328ea4473215336a24970
[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 pathname.")
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 pathname 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.)")
791       ;; Carry on until the bitter end.
792       (while (not (eobp))
793         (setq start (point)
794               end nil)
795         ;; Find the end of the head.
796         (narrow-to-region
797          start
798          (if (search-forward "\n\n" nil t)
799              (1- (point))
800            ;; This will never happen, but just to be on the safe side --
801            ;; if there is no head-body delimiter, we search a bit manually.
802            (while (and (looking-at "From \\|[^ \t]+:")
803                        (not (eobp)))
804              (forward-line 1))
805            (point)))
806         ;; Find the Message-ID header.
807         (goto-char (point-min))
808         (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
809             (setq message-id (match-string 1))
810           (save-excursion
811             (when (re-search-forward "^Message-ID[ \t]*:" nil t)
812               (beginning-of-line)
813               (insert "Original-")))
814           ;; There is no Message-ID here, so we create one.
815           (forward-line 1)
816           (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
817         ;; Look for a Content-Length header.
818         (goto-char (point-min))
819         (if (not (re-search-forward
820                   "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
821             (setq content-length nil)
822           (setq content-length (string-to-int (match-string 1)))
823           ;; We destroy the header, since none of the backends ever
824           ;; use it, and we do not want to confuse other mailers by
825           ;; having a (possibly) faulty header.
826           (beginning-of-line)
827           (insert "X-"))
828         (run-hooks 'nnmail-prepare-incoming-header-hook)
829         ;; Find the end of this article.
830         (goto-char (point-max))
831         (widen)
832         (setq head-end (point))
833         ;; We try the Content-Length value.  The idea: skip over the header
834         ;; separator, then check what happens content-length bytes into the
835         ;; message body.  This should be either the end of the buffer, the
836         ;; message separator or a blank line followed by the separator.
837         ;; The blank line should probably be deleted.  If neither of the
838         ;; three is met, the content-length header is probably invalid.
839         (when content-length
840           (forward-line 1)
841           (setq skip (+ (point) content-length))
842           (goto-char skip)
843           (cond ((or (= skip (point-max))
844                      (= (1+ skip) (point-max)))
845                  (setq end (point-max)))
846                 ((looking-at "From ")
847                  (setq end skip))
848                 ((looking-at "[ \t]*\n\\(From \\)")
849                  (setq end (match-beginning 1)))
850                 (t (setq end nil))))
851         (if end
852             (goto-char end)
853           ;; No Content-Length, so we find the beginning of the next
854           ;; article or the end of the buffer.
855           (goto-char head-end)
856           (or (nnmail-search-unix-mail-delim)
857               (goto-char (point-max))))
858         ;; Allow the backend to save the article.
859         (save-excursion
860           (save-restriction
861             (narrow-to-region start (point))
862             (goto-char (point-min))
863             (incf count)
864             (nnmail-check-duplication message-id func artnum-func)
865             (setq end (point-max))))
866         (goto-char end)))
867     count))
868
869 (defun nnmail-process-mmdf-mail-format (func artnum-func)
870   (let ((delim "^\^A\^A\^A\^A$")
871         (case-fold-search t)
872         (count 0)
873         start message-id end)
874     (goto-char (point-min))
875     (if (not (and (re-search-forward delim nil t)
876                   (forward-line 1)))
877         ;; Possibly wrong format?
878         (error "Error, unknown mail format! (Possibly corrupted.)")
879       ;; Carry on until the bitter end.
880       (while (not (eobp))
881         (setq start (point))
882         ;; Find the end of the head.
883         (narrow-to-region
884          start
885          (if (search-forward "\n\n" nil t)
886              (1- (point))
887            ;; This will never happen, but just to be on the safe side --
888            ;; if there is no head-body delimiter, we search a bit manually.
889            (while (and (looking-at "From \\|[^ \t]+:")
890                        (not (eobp)))
891              (forward-line 1))
892            (point)))
893         ;; Find the Message-ID header.
894         (goto-char (point-min))
895         (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
896             (setq message-id (match-string 1))
897           ;; There is no Message-ID here, so we create one.
898           (save-excursion
899             (when (re-search-backward "^Message-ID[ \t]*:" nil t)
900               (beginning-of-line)
901               (insert "Original-")))
902           (forward-line 1)
903           (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
904         (run-hooks 'nnmail-prepare-incoming-header-hook)
905         ;; Find the end of this article.
906         (goto-char (point-max))
907         (widen)
908         (if (re-search-forward delim nil t)
909             (beginning-of-line)
910           (goto-char (point-max)))
911         ;; Allow the backend to save the article.
912         (save-excursion
913           (save-restriction
914             (narrow-to-region start (point))
915             (goto-char (point-min))
916             (incf count)
917             (nnmail-check-duplication message-id func artnum-func)
918             (setq end (point-max))))
919         (goto-char end)
920         (forward-line 2)))
921     count))
922
923 (defun nnmail-process-maildir-mail-format (func artnum-func)
924   ;; In a maildir, every file contains exactly one mail.
925   (let ((case-fold-search t)
926         message-id)
927     (goto-char (point-min))
928     ;; Find the end of the head.
929     (narrow-to-region
930      (point-min)
931      (if (search-forward "\n\n" nil t)
932          (1- (point))
933        ;; This will never happen, but just to be on the safe side --
934        ;; if there is no head-body delimiter, we search a bit manually.
935        (while (and (looking-at "From \\|[^ \t]+:")
936                    (not (eobp)))
937          (forward-line 1))
938        (point)))
939     ;; Find the Message-ID header.
940     (goto-char (point-min))
941     (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
942         (setq message-id (match-string 1))
943       ;; There is no Message-ID here, so we create one.
944       (save-excursion
945         (when (re-search-backward "^Message-ID[ \t]*:" nil t)
946           (beginning-of-line)
947           (insert "Original-")))
948       (forward-line 1)
949       (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
950     (run-hooks 'nnmail-prepare-incoming-header-hook)
951     ;; Allow the backend to save the article.
952     (widen)
953     (save-excursion
954       (goto-char (point-min))
955       (nnmail-check-duplication message-id func artnum-func))
956     1))
957
958 (defun nnmail-split-incoming (incoming func &optional exit-func
959                                        group artnum-func)
960   "Go through the entire INCOMING file and pick out each individual mail.
961 FUNC will be called with the buffer narrowed to each mail."
962   (let (;; If this is a group-specific split, we bind the split
963         ;; methods to just this group.
964         (nnmail-split-methods (if (and group
965                                        (not nnmail-resplit-incoming))
966                                   (list (list group ""))
967                                 nnmail-split-methods)))
968     (save-excursion
969       ;; Insert the incoming file.
970       (set-buffer (get-buffer-create nnmail-article-buffer))
971       (erase-buffer)
972       (let ((nnheader-file-coding-system nnmail-incoming-coding-system))
973         (nnheader-insert-file-contents incoming))
974       (prog1
975           (if (zerop (buffer-size))
976               0
977             (goto-char (point-min))
978             (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
979             ;; Handle both babyl, MMDF and unix mail formats, since
980             ;; movemail will use the former when fetching from a
981             ;; mailbox, the latter when fetching from a file.
982             (cond ((or (looking-at "\^L")
983                        (looking-at "BABYL OPTIONS:"))
984                    (nnmail-process-babyl-mail-format func artnum-func))
985                   ((looking-at "\^A\^A\^A\^A")
986                    (nnmail-process-mmdf-mail-format func artnum-func))
987                   ((looking-at "Return-Path:")
988                    (nnmail-process-maildir-mail-format func artnum-func))
989                   (t
990                    (nnmail-process-unix-mail-format func artnum-func))))
991         (when exit-func
992           (funcall exit-func))
993         (kill-buffer (current-buffer))))))
994
995 (defun nnmail-article-group (func &optional trace)
996   "Look at the headers and return an alist of groups that match.
997 FUNC will be called with the group name to determine the article number."
998   (let ((methods (or nnmail-split-methods '(("bogus" ""))))
999         (obuf (current-buffer))
1000         (beg (point-min))
1001         end group-art method grp)
1002     (if (and (sequencep methods)
1003              (= (length methods) 1))
1004         ;; If there is only just one group to put everything in, we
1005         ;; just return a list with just this one method in.
1006         (setq group-art
1007               (list (cons (caar methods) (funcall func (caar methods)))))
1008       ;; We do actual comparison.
1009       (save-excursion
1010         ;; Find headers.
1011         (goto-char beg)
1012         (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
1013         (set-buffer nntp-server-buffer)
1014         (erase-buffer)
1015         ;; Copy the headers into the work buffer.
1016         (insert-buffer-substring obuf beg end)
1017         ;; Decode MIME headers and charsets.
1018         (when nnmail-mail-splitting-decodes
1019           (mime-decode-header-in-region (point-min) (point-max)
1020                                         nnmail-mail-splitting-charset))
1021         ;; Fold continuation lines.
1022         (goto-char (point-min))
1023         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1024           (replace-match " " t t))
1025         ;; Nuke pathologically long headers.  Since Gnus applies
1026         ;; pathologically complex regexps to the buffer, lines
1027         ;; that are looong will take longer than the Universe's
1028         ;; existence to process.
1029         (goto-char (point-min))
1030         (while (not (eobp))
1031           (unless (< (move-to-column nnmail-split-header-length-limit)
1032                      nnmail-split-header-length-limit)
1033             (delete-region (point) (progn (end-of-line) (point))))
1034           (forward-line 1))
1035         ;; Allow washing.
1036         (goto-char (point-min))
1037         (run-hooks 'nnmail-split-hook)
1038         (when (setq nnmail-split-tracing trace)
1039           (setq nnmail-split-trace nil))
1040         (if (and (symbolp nnmail-split-methods)
1041                  (fboundp nnmail-split-methods))
1042             (let ((split
1043                    (condition-case nil
1044                        ;; `nnmail-split-methods' is a function, so we
1045                        ;; just call this function here and use the
1046                        ;; result.
1047                        (or (funcall nnmail-split-methods)
1048                            '("bogus"))
1049                      (error
1050                       (nnheader-message
1051                        5 "Error in `nnmail-split-methods'; using `bogus' mail group")
1052                       (sit-for 1)
1053                       '("bogus")))))
1054               (setq split (gnus-remove-duplicates split))
1055               ;; The article may be "cross-posted" to `junk'.  What
1056               ;; to do?  Just remove the `junk' spec.  Don't really
1057               ;; see anything else to do...
1058               (let (elem)
1059                 (while (setq elem (car (memq 'junk split)))
1060                   (setq split (delq elem split))))
1061               (when split
1062                 (setq group-art
1063                       (mapcar
1064                        (lambda (group) (cons group (funcall func group)))
1065                        split))))
1066           ;; Go through the split methods to find a match.
1067           (while (and methods
1068                       (or nnmail-crosspost
1069                           (not group-art)))
1070             (goto-char (point-max))
1071             (setq method (pop methods)
1072                   grp (car method))
1073             (if (or methods
1074                     (not (equal "" (nth 1 method))))
1075                 (when (and
1076                        (ignore-errors
1077                          (if (stringp (nth 1 method))
1078                              (let ((expand (string-match "\\\\[0-9&]" grp))
1079                                    (pos (re-search-backward (cadr method)
1080                                                             nil t)))
1081                                (and expand
1082                                     (setq grp (nnmail-expand-newtext grp)))
1083                                pos)
1084                            ;; Function to say whether this is a match.
1085                            (funcall (nth 1 method) grp)))
1086                        ;; Don't enter the article into the same
1087                        ;; group twice.
1088                        (not (assoc grp group-art)))
1089                   (push (cons grp (funcall func grp))
1090                         group-art))
1091               ;; This is the final group, which is used as a
1092               ;; catch-all.
1093               (unless group-art
1094                 (setq group-art
1095                       (list (cons (car method)
1096                                   (funcall func (car method)))))))))
1097         ;; Produce a trace if non-empty.
1098         (when (and trace nnmail-split-trace)
1099           (let ((restore (current-buffer)))
1100             (nnheader-set-temp-buffer "*Split Trace*")
1101             (gnus-add-buffer)
1102             (dolist (trace (nreverse nnmail-split-trace))
1103               (prin1 trace (current-buffer))
1104               (insert "\n"))
1105             (goto-char (point-min))
1106             (gnus-configure-windows 'split-trace)
1107             (set-buffer restore)))
1108         ;; See whether the split methods returned `junk'.
1109         (if (equal group-art '(junk))
1110             nil
1111           ;; The article may be "cross-posted" to `junk'.  What
1112           ;; to do?  Just remove the `junk' spec.  Don't really
1113           ;; see anything else to do...
1114           (let (elem)
1115             (while (setq elem (car (memq 'junk group-art)))
1116               (setq group-art (delq elem group-art)))
1117             (nreverse group-art)))))))
1118
1119 (defun nnmail-insert-lines ()
1120   "Insert how many lines there are in the body of the mail.
1121 Return the number of characters in the body."
1122   (let (lines chars)
1123     (save-excursion
1124       (goto-char (point-min))
1125       (unless (search-forward "\n\n" nil t)
1126         (goto-char (point-max))
1127         (insert "\n"))
1128       (setq chars (- (point-max) (point)))
1129       (setq lines (count-lines (point) (point-max)))
1130       (forward-char -1)
1131       (save-excursion
1132         (when (re-search-backward "^Lines: " nil t)
1133           (delete-region (point) (progn (forward-line 1) (point)))))
1134       (beginning-of-line)
1135       (insert (format "Lines: %d\n" (max lines 0)))
1136       chars)))
1137
1138 (defun nnmail-insert-xref (group-alist)
1139   "Insert an Xref line based on the (group . article) alist."
1140   (save-excursion
1141     (goto-char (point-min))
1142     (unless (search-forward "\n\n" nil t)
1143       (goto-char (point-max))
1144       (insert "\n"))
1145     (forward-char -1)
1146     (when (re-search-backward "^Xref: " nil t)
1147       (delete-region (match-beginning 0)
1148                      (progn (forward-line 1) (point))))
1149     (insert (format "Xref: %s" (system-name)))
1150     (while group-alist
1151       (insert (format " %s:%d"
1152                       (encode-coding-string
1153                        (caar group-alist)
1154                        nnmail-pathname-coding-system)
1155                       (cdar group-alist)))
1156       (setq group-alist (cdr group-alist)))
1157     (insert "\n")))
1158
1159 ;;; Message washing functions
1160
1161 (defun nnmail-remove-leading-whitespace ()
1162   "Remove excessive whitespace from all headers."
1163   (goto-char (point-min))
1164   (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1165     (replace-match "\\1" t)))
1166
1167 (defun nnmail-remove-list-identifiers ()
1168   "Remove list identifiers from Subject headers."
1169   (let ((regexp
1170          (if (consp nnmail-list-identifiers)
1171              (mapconcat 'identity nnmail-list-identifiers " *\\|")
1172            nnmail-list-identifiers)))
1173     (when regexp
1174       (goto-char (point-min))
1175       (while (re-search-forward
1176               (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1177               nil t)
1178         (delete-region (match-beginning 2) (match-end 0))
1179         (beginning-of-line))
1180       (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
1181         (delete-region (match-beginning 1) (match-end 1))
1182         (beginning-of-line)))))
1183
1184 (defun nnmail-remove-tabs ()
1185   "Translate TAB characters into SPACE characters."
1186   (subst-char-in-region (point-min) (point-max) ?\t ?  t))
1187
1188 (defun nnmail-fix-eudora-headers ()
1189   "Eudora has a broken References line, but an OK In-Reply-To."
1190   (goto-char (point-min))
1191   (when (re-search-forward "^X-Mailer:.*Eudora" nil t)
1192     (goto-char (point-min))
1193     (when (re-search-forward "^References:" nil t)
1194       (beginning-of-line)
1195       (insert "X-Gnus-Broken-Eudora-"))
1196     (goto-char (point-min))
1197     (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1198       (replace-match "\\1" t))))
1199
1200 (custom-add-option 'nnmail-prepare-incoming-header-hook
1201                    'nnmail-fix-eudora-headers)
1202
1203 ;;; Utility functions
1204
1205 (defun nnmail-do-request-post (accept-func &optional server)
1206   "Utility function to directly post a message to an nnmail-derived group.
1207 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1208 to actually put the message in the right group."
1209   (let ((success t))
1210     (dolist (mbx (message-unquote-tokens
1211                   (message-tokenize-header
1212                    (message-fetch-field "Newsgroups") ", ")) success)
1213       (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1214         (or (gnus-active to-newsgroup)
1215             (gnus-activate-group to-newsgroup)
1216             (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
1217                                        to-newsgroup))
1218                 (or (and (gnus-request-create-group
1219                           to-newsgroup gnus-command-method)
1220                          (gnus-activate-group to-newsgroup nil nil
1221                                               gnus-command-method))
1222                     (error "Couldn't create group %s" to-newsgroup)))
1223             (error "No such group: %s" to-newsgroup))
1224         (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1225           (setq success nil))))))
1226
1227 (defun nnmail-split-fancy ()
1228   "Fancy splitting method.
1229 See the documentation for the variable `nnmail-split-fancy' for details."
1230   (let ((syntab (syntax-table)))
1231     (unwind-protect
1232         (progn
1233           (set-syntax-table nnmail-split-fancy-syntax-table)
1234           (nnmail-split-it nnmail-split-fancy))
1235       (set-syntax-table syntab))))
1236
1237 (defvar nnmail-split-cache nil)
1238 ;; Alist of split expressions their equivalent regexps.
1239
1240 (defun nnmail-split-it (split)
1241   ;; Return a list of groups matching SPLIT.
1242   (let (cached-pair)
1243     (cond
1244      ;; nil split
1245      ((null split)
1246       nil)
1247
1248      ;; A group name.  Do the \& and \N subs into the string.
1249      ((stringp split)
1250       (when nnmail-split-tracing
1251         (push split nnmail-split-trace))
1252       (list (nnmail-expand-newtext split)))
1253
1254      ;; Junk the message.
1255      ((eq split 'junk)
1256       (when nnmail-split-tracing
1257         (push "junk" nnmail-split-trace))
1258       (list 'junk))
1259
1260      ;; Builtin & operation.
1261      ((eq (car split) '&)
1262       (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1263
1264      ;; Builtin | operation.
1265      ((eq (car split) '|)
1266       (let (done)
1267         (while (and (not done) (cdr split))
1268           (setq split (cdr split)
1269                 done (nnmail-split-it (car split))))
1270         done))
1271
1272      ;; Builtin : operation.
1273      ((eq (car split) ':)
1274       (when nnmail-split-tracing
1275         (push split nnmail-split-trace))
1276       (nnmail-split-it (save-excursion (eval (cdr split)))))
1277
1278      ;; Builtin ! operation.
1279      ((eq (car split) '!)
1280       (funcall (cadr split) (nnmail-split-it (caddr split))))
1281
1282      ;; Check the cache for the regexp for this split.
1283      ((setq cached-pair (assq split nnmail-split-cache))
1284       (let (split-result
1285             (end-point (point-max))
1286             (value (nth 1 split)))
1287         (if (symbolp value)
1288             (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1289         (while (and (goto-char end-point)
1290                     (re-search-backward (cdr cached-pair) nil t))
1291           (when nnmail-split-tracing
1292             (push split nnmail-split-trace))
1293           (let ((split-rest (cddr split))
1294                 (end (match-end 0))
1295                 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).  So,
1296                 ;; start-of-value is the the point just before the
1297                 ;; beginning of the value, whereas after-header-name is
1298                 ;; the point just after the field name.
1299                 (start-of-value (match-end 1))
1300                 (after-header-name (match-end 2)))
1301             ;; Start the next search just before the beginning of the
1302             ;; VALUE match.
1303             (setq end-point (1- start-of-value))
1304             ;; Handle - RESTRICTs
1305             (while (eq (car split-rest) '-)
1306               ;; RESTRICT must start after-header-name and
1307               ;; end after start-of-value, so that, for
1308               ;; (any "foo" - "x-foo" "foo.list")
1309               ;; we do not exclude foo.list just because
1310               ;; the header is: ``To: x-foo, foo''
1311               (goto-char end)
1312               (if (and (re-search-backward (cadr split-rest)
1313                                            after-header-name t)
1314                        (> (match-end 0) start-of-value))
1315                   (setq split-rest nil)
1316                 (setq split-rest (cddr split-rest))))
1317             (when split-rest
1318               (goto-char end)
1319               (let ((value (nth 1 split)))
1320                 (if (symbolp value)
1321                     (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1322                 ;; Someone might want to do a \N sub on this match, so get the
1323                 ;; correct match positions.
1324                 (re-search-backward (concat "\\<" value "\\>") start-of-value))
1325               (dolist (sp (nnmail-split-it (car split-rest)))
1326                 (unless (memq sp split-result)
1327                   (push sp split-result))))))
1328         split-result))
1329
1330      ;; Not in cache, compute a regexp for the field/value pair.
1331      (t
1332       (let* ((field (nth 0 split))
1333              (value (nth 1 split))
1334              partial-front regexp
1335              partial-rear  regexp)
1336         (if (symbolp value)
1337             (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1338         (if (and (>= (length value) 2)
1339                  (string= ".*" (substring value 0 2)))
1340             (setq value (substring value 2)
1341                   partial-front ""))
1342         ;; Same trick for the rear of the regexp
1343         (if (and (>= (length value) 2)
1344                  (string= ".*" (substring value -2)))
1345             (setq value (substring value 0 -2)
1346                   partial-rear ""))
1347         (setq regexp (concat "^\\(\\("
1348                              (if (symbolp field)
1349                                  (cdr (assq field nnmail-split-abbrev-alist))
1350                                field)
1351                              "\\):.*\\)"
1352                              (or partial-front "\\<")
1353                              "\\("
1354                              value
1355                              "\\)"
1356                              (or partial-rear "\\>")))
1357         (push (cons split regexp) nnmail-split-cache)
1358         ;; Now that it's in the cache, just call nnmail-split-it again
1359         ;; on the same split, which will find it immediately in the cache.
1360         (nnmail-split-it split))))))
1361
1362 (defun nnmail-expand-newtext (newtext)
1363   (let ((len (length newtext))
1364         (pos 0)
1365         c expanded beg N did-expand)
1366     (while (< pos len)
1367       (setq beg pos)
1368       (while (and (< pos len)
1369                   (not (= (aref newtext pos) ?\\)))
1370         (setq pos (1+ pos)))
1371       (unless (= beg pos)
1372         (push (substring newtext beg pos) expanded))
1373       (when (< pos len)
1374         ;; We hit a \; expand it.
1375         (setq did-expand t
1376               pos (1+ pos)
1377               c (aref newtext pos))
1378         (if (not (or (= c ?\&)
1379                      (and (>= c ?1)
1380                           (<= c ?9))))
1381             ;; \ followed by some character we don't expand.
1382             (push (char-to-string c) expanded)
1383           ;; \& or \N
1384           (if (= c ?\&)
1385               (setq N 0)
1386             (setq N (- c ?0)))
1387           (when (match-beginning N)
1388             (push (buffer-substring (match-beginning N) (match-end N))
1389                   expanded))))
1390       (setq pos (1+ pos)))
1391     (if did-expand
1392         (apply 'concat (nreverse expanded))
1393       newtext)))
1394
1395 ;; Activate a backend only if it isn't already activated.
1396 ;; If FORCE, re-read the active file even if the backend is
1397 ;; already activated.
1398 (defun nnmail-activate (backend &optional force)
1399   (nnheader-init-server-buffer)
1400   (let (file timestamp file-time)
1401     (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1402             force
1403             (and (setq file (ignore-errors
1404                               (symbol-value (intern (format "%s-active-file"
1405                                                             backend)))))
1406                  (setq file-time (nth 5 (file-attributes file)))
1407                  (or (not
1408                       (setq timestamp
1409                             (condition-case ()
1410                                 (symbol-value (intern
1411                                                (format "%s-active-timestamp"
1412                                                        backend)))
1413                               (error 'none))))
1414                      (not (consp timestamp))
1415                      (equal timestamp '(0 0))
1416                      (> (nth 0 file-time) (nth 0 timestamp))
1417                      (and (= (nth 0 file-time) (nth 0 timestamp))
1418                           (> (nth 1 file-time) (nth 1 timestamp))))))
1419         (save-excursion
1420           (or (eq timestamp 'none)
1421               (set (intern (format "%s-active-timestamp" backend))
1422                    file-time))
1423           (funcall (intern (format "%s-request-list" backend)))))
1424     t))
1425
1426 (defun nnmail-message-id ()
1427   (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1428
1429 ;;;
1430 ;;; nnmail duplicate handling
1431 ;;;
1432
1433 (defvar nnmail-cache-buffer nil)
1434
1435 (defun nnmail-cache-open ()
1436   (if (or (not nnmail-treat-duplicates)
1437           (and nnmail-cache-buffer
1438                (buffer-name nnmail-cache-buffer)))
1439       ()                                ; The buffer is open.
1440     (save-excursion
1441       (set-buffer
1442        (setq nnmail-cache-buffer
1443              (get-buffer-create " *nnmail message-id cache*")))
1444       (gnus-add-buffer)
1445       (when (file-exists-p nnmail-message-id-cache-file)
1446         (nnheader-insert-file-contents nnmail-message-id-cache-file))
1447       (set-buffer-modified-p nil)
1448       (current-buffer))))
1449
1450 (defun nnmail-cache-close ()
1451   (when (and nnmail-cache-buffer
1452              nnmail-treat-duplicates
1453              (buffer-name nnmail-cache-buffer)
1454              (buffer-modified-p nnmail-cache-buffer))
1455     (save-excursion
1456       (set-buffer nnmail-cache-buffer)
1457       ;; Weed out the excess number of Message-IDs.
1458       (goto-char (point-max))
1459       (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1460         (progn
1461           (beginning-of-line)
1462           (delete-region (point-min) (point))))
1463       ;; Save the buffer.
1464       (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1465           (make-directory (file-name-directory nnmail-message-id-cache-file)
1466                           t))
1467       (nnmail-write-region (point-min) (point-max)
1468                            nnmail-message-id-cache-file nil 'silent)
1469       (set-buffer-modified-p nil)
1470       (setq nnmail-cache-buffer nil)
1471       (kill-buffer (current-buffer)))))
1472
1473 ;; Compiler directives.
1474 (defvar group)
1475 (defvar group-art-list)
1476 (defvar group-art)
1477 (defun nnmail-cache-insert (id grp)
1478   (when nnmail-treat-duplicates
1479     ;; Store some information about the group this message is written
1480     ;; to.  This is passed in as the grp argument -- all locations this
1481     ;; has been called from have been checked and the group is available.
1482     ;; The only ambiguous case is nnmail-check-duplication which will only
1483     ;; pass the first (of possibly >1) group which matches. -Josh
1484     (unless (gnus-buffer-live-p nnmail-cache-buffer)
1485       (nnmail-cache-open))
1486     (save-excursion
1487       (set-buffer nnmail-cache-buffer)
1488       (goto-char (point-max))
1489       (if (and grp (not (string= "" grp))
1490                (gnus-methods-equal-p gnus-command-method
1491                                      (nnmail-cache-primary-mail-backend)))
1492           (let ((regexp (if (consp nnmail-cache-ignore-groups)
1493                             (mapconcat 'identity nnmail-cache-ignore-groups
1494                                        "\\|")
1495                           nnmail-cache-ignore-groups)))
1496             (unless (and regexp (string-match regexp grp))
1497               (insert id "\t" grp "\n")))
1498         (insert id "\n")))))
1499
1500 (defun nnmail-cache-primary-mail-backend ()
1501   (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1502         (be nil)
1503         (res nil))
1504     (while (and (null res) be-list)
1505       (setq be (car be-list))
1506       (setq be-list (cdr be-list))
1507       (when (and (gnus-method-option-p be 'respool)
1508                  (eval (intern (format "%s-get-new-mail" (car be)))))
1509         (setq res be)))
1510     res))
1511
1512 ;; Fetch the group name corresponding to the message id stored in the
1513 ;; cache.
1514 (defun nnmail-cache-fetch-group (id)
1515   (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1516     (save-excursion
1517       (set-buffer nnmail-cache-buffer)
1518       (goto-char (point-max))
1519       (when (search-backward id nil t)
1520         (beginning-of-line)
1521         (skip-chars-forward "^\n\r\t")
1522         (unless (looking-at "[\r\n]")
1523           (forward-char 1)
1524           (buffer-substring (point)
1525                             (progn (end-of-line) (point))))))))
1526
1527 ;; Function for nnmail-split-fancy: look up all references in the
1528 ;; cache and if a match is found, return that group.
1529 (defun nnmail-split-fancy-with-parent ()
1530   "Split this message into the same group as its parent.
1531 This function can be used as an entry in `nnmail-split-fancy', for
1532 example like this: (: nnmail-split-fancy)
1533 For a message to be split, it looks for the parent message in the
1534 References or In-Reply-To header and then looks in the message id
1535 cache file (given by the variable `nnmail-message-id-cache-file') to
1536 see which group that message was put in.  This group is returned.
1537
1538 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1539   (let* ((refstr (or (message-fetch-field "references")
1540                      (message-fetch-field "in-reply-to")))
1541          (references nil)
1542          (res nil)
1543          (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1544                      (mapconcat
1545                       (lambda (x) (format "\\(%s\\)" x))
1546                       nnmail-split-fancy-with-parent-ignore-groups
1547                       "\\|")
1548                    nnmail-split-fancy-with-parent-ignore-groups)))
1549     (when refstr
1550       (setq references (nreverse (gnus-split-references refstr)))
1551       (unless (gnus-buffer-live-p nnmail-cache-buffer)
1552         (nnmail-cache-open))
1553       (mapcar (lambda (x)
1554                 (setq res (or (nnmail-cache-fetch-group x) res))
1555                 (when (or (member res '("delayed" "drafts" "queue"))
1556                           (and regexp res (string-match regexp res)))
1557                   (setq res nil)))
1558               references)
1559       res)))
1560
1561 (defun nnmail-cache-id-exists-p (id)
1562   (when nnmail-treat-duplicates
1563     (save-excursion
1564       (set-buffer nnmail-cache-buffer)
1565       (goto-char (point-max))
1566       (search-backward id nil t))))
1567
1568 (defun nnmail-fetch-field (header)
1569   (save-excursion
1570     (save-restriction
1571       (message-narrow-to-head)
1572       (message-fetch-field header))))
1573
1574 (defun nnmail-check-duplication (message-id func artnum-func)
1575   (run-hooks 'nnmail-prepare-incoming-message-hook)
1576   ;; If this is a duplicate message, then we do not save it.
1577   (let* ((duplication (nnmail-cache-id-exists-p message-id))
1578          (case-fold-search t)
1579          (action (when duplication
1580                    (cond
1581                     ((memq nnmail-treat-duplicates '(warn delete))
1582                      nnmail-treat-duplicates)
1583                     ((nnheader-functionp nnmail-treat-duplicates)
1584                      (funcall nnmail-treat-duplicates message-id))
1585                     (t
1586                      nnmail-treat-duplicates))))
1587          group-art)
1588     ;; We insert a line that says what the mail source is.
1589     (let ((case-fold-search t))
1590       (goto-char (point-min))
1591       (re-search-forward "^message-id[ \t]*:" nil t)
1592       (beginning-of-line)
1593       (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1594
1595     ;; Let the backend save the article (or not).
1596     (cond
1597      ((not duplication)
1598       (funcall func (setq group-art
1599                           (nreverse (nnmail-article-group artnum-func))))
1600       (nnmail-cache-insert message-id (caar group-art)))
1601      ((eq action 'delete)
1602       (setq group-art nil))
1603      ((eq action 'warn)
1604       ;; We insert a warning.
1605       (let ((case-fold-search t))
1606         (goto-char (point-min))
1607         (re-search-forward "^message-id[ \t]*:" nil t)
1608         (beginning-of-line)
1609         (insert
1610          "Gnus-Warning: This is a duplicate of message " message-id "\n")
1611         (funcall func (setq group-art
1612                             (nreverse (nnmail-article-group artnum-func))))))
1613      (t
1614       (funcall func (setq group-art
1615                           (nreverse (nnmail-article-group artnum-func))))))
1616     ;; Add the group-art list to the history list.
1617     (if group-art
1618         (push group-art nnmail-split-history)
1619       (delete-region (point-min) (point-max)))))
1620
1621 ;;; Get new mail.
1622
1623 (defvar nnmail-fetched-sources nil)
1624
1625 (defun nnmail-get-value (&rest args)
1626   (let ((sym (intern (apply 'format args))))
1627     (when (boundp sym)
1628       (symbol-value sym))))
1629
1630 (defun nnmail-get-new-mail (method exit-func temp
1631                                    &optional group spool-func)
1632   "Read new incoming mail."
1633   (let* ((sources (or mail-sources
1634                       (if (listp nnmail-spool-file) nnmail-spool-file
1635                         (list nnmail-spool-file))))
1636          fetching-sources
1637          (group-in group)
1638          (i 0)
1639          (new 0)
1640          (total 0)
1641          incoming incomings source)
1642     (when (and (nnmail-get-value "%s-get-new-mail" method)
1643                sources)
1644       (while (setq source (pop sources))
1645         ;; Be compatible with old values.
1646         (cond
1647          ((stringp source)
1648           (setq source
1649                 (cond
1650                  ((string-match "^po:" source)
1651                   (list 'pop :user (substring source (match-end 0))))
1652                  ((file-directory-p source)
1653                   (list 'directory :path source))
1654                  (t
1655                   (list 'file :path source)))))
1656          ((eq source 'procmail)
1657           (message "Invalid value for nnmail-spool-file: `procmail'")
1658           nil))
1659         ;; Hack to only fetch the contents of a single group's spool file.
1660         (when (and (eq (car source) 'directory)
1661                    (null nnmail-scan-directory-mail-source-once)
1662                    group)
1663           (mail-source-bind (directory source)
1664             (setq source (append source
1665                                  (list
1666                                   :predicate
1667                                   (gnus-byte-compile
1668                                    `(lambda (file)
1669                                       (string-equal
1670                                        ,(concat group suffix)
1671                                        (file-name-nondirectory file)))))))))
1672         (when nnmail-fetched-sources
1673           (if (member source nnmail-fetched-sources)
1674               (setq source nil)
1675             (push source nnmail-fetched-sources)
1676             (push source fetching-sources)))))
1677     (when fetching-sources
1678       ;; We first activate all the groups.
1679       (nnmail-activate method)
1680       ;; Allow the user to hook.
1681       (run-hooks 'nnmail-pre-get-new-mail-hook)
1682       ;; Open the message-id cache.
1683       (nnmail-cache-open)
1684       ;; The we go through all the existing mail source specification
1685       ;; and fetch the mail from each.
1686       (while (setq source (pop fetching-sources))
1687         (nnheader-message 4 "%s: Reading incoming mail from %s..."
1688                           method (car source))
1689         (when (setq new
1690                     (mail-source-fetch
1691                      source
1692                      (gnus-byte-compile
1693                       `(lambda (file orig-file)
1694                          (nnmail-split-incoming
1695                           file ',(intern (format "%s-save-mail" method))
1696                           ',spool-func
1697                           (if (equal file orig-file)
1698                               nil
1699                             (nnmail-get-split-group orig-file ',source))
1700                           ',(intern (format "%s-active-number" method)))))))
1701           (incf total new)
1702           (incf i)))
1703       ;; If we did indeed read any incoming spools, we save all info.
1704       (if (zerop total)
1705           (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1706                             method (car source))
1707         (nnmail-save-active
1708          (nnmail-get-value "%s-group-alist" method)
1709          (nnmail-get-value "%s-active-file" method))
1710         (when exit-func
1711           (funcall exit-func))
1712         (run-hooks 'nnmail-read-incoming-hook)
1713         (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1714                           total))
1715       ;; Close the message-id cache.
1716       (nnmail-cache-close)
1717       ;; Allow the user to hook.
1718       (run-hooks 'nnmail-post-get-new-mail-hook))))
1719
1720 (defun nnmail-expired-article-p (group time force &optional inhibit)
1721   "Say whether an article that is TIME old in GROUP should be expired."
1722   (if force
1723       t
1724     (let ((days (or (and nnmail-expiry-wait-function
1725                          (funcall nnmail-expiry-wait-function group))
1726                     nnmail-expiry-wait)))
1727       (cond ((or (eq days 'never)
1728                  (and (not force)
1729                       inhibit))
1730              ;; This isn't an expirable group.
1731              nil)
1732             ((eq days 'immediate)
1733              ;; We expire all articles on sight.
1734              t)
1735             ((equal time '(0 0))
1736              ;; This is an ange-ftp group, and we don't have any dates.
1737              nil)
1738             ((numberp days)
1739              (setq days (days-to-time days))
1740              ;; Compare the time with the current time.
1741              (ignore-errors (time-less-p days (time-since time))))))))
1742
1743 (defun nnmail-expiry-target-group (target group)
1744   ;; Do not invoke this from nntp-server-buffer!  At least nnfolder clears
1745   ;; that buffer if the nnfolder group isn't selected.
1746   (let (nnmail-cache-accepted-message-ids)
1747     ;; Don't enter Message-IDs into cache.
1748     ;; Let users hack it in TARGET function.
1749     (when (nnheader-functionp target)
1750       (setq target (funcall target group)))
1751     (unless (eq target 'delete)
1752       (let ((group-art (gnus-request-accept-article target nil nil t)))
1753         (when (consp group-art)
1754           (gnus-group-mark-article-read target (cdr group-art)))))))
1755
1756 (defun nnmail-fancy-expiry-target (group)
1757   "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1758   (let* (header
1759          (case-fold-search nil)
1760          (from (or (message-fetch-field "from") ""))
1761          (to (or (message-fetch-field "to") ""))
1762          (date (date-to-time
1763                 (or (message-fetch-field "date") (current-time-string))))
1764          (target 'delete))
1765     (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1766       (setq header (car regexp-target-pair))
1767       (cond
1768        ;; If the header is to-from then match against the
1769        ;; To or From header
1770        ((and (equal header 'to-from)
1771              (or (string-match (cadr regexp-target-pair) from)
1772                  (and (string-match message-dont-reply-to-names from)
1773                       (string-match (cadr regexp-target-pair) to))))
1774         (setq target (format-time-string (caddr regexp-target-pair) date)))
1775        ((and (not (equal header 'to-from))
1776              (string-match (cadr regexp-target-pair)
1777                            (or
1778                             (message-fetch-field header)
1779                             "")))
1780         (setq target
1781               (format-time-string (caddr regexp-target-pair) date)))))))
1782
1783 (defun nnmail-check-syntax ()
1784   "Check (and modify) the syntax of the message in the current buffer."
1785   (save-restriction
1786     (message-narrow-to-head)
1787     (let ((case-fold-search t))
1788       (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1789         (insert "Message-ID: " (nnmail-message-id) "\n")))))
1790
1791 (defun nnmail-write-region (start end filename &optional append visit lockname)
1792   "Do a `write-region', and then set the file modes."
1793   (let ((file-name-coding-system nnmail-pathname-coding-system)
1794         (pathname-coding-system nnmail-pathname-coding-system))
1795     (write-region-as-coding-system
1796      nnmail-file-coding-system start end filename append visit lockname)
1797     (set-file-modes filename nnmail-default-file-modes)))
1798
1799 ;;;
1800 ;;; Status functions
1801 ;;;
1802
1803 (defun nnmail-replace-status (name value)
1804   "Make status NAME and VALUE part of the current status line."
1805   (save-restriction
1806     (message-narrow-to-head)
1807     (let ((status (nnmail-decode-status)))
1808       (setq status (delq (member name status) status))
1809       (when value
1810         (push (cons name value) status))
1811       (message-remove-header "status")
1812       (goto-char (point-max))
1813       (insert "Status: " (nnmail-encode-status status) "\n"))))
1814
1815 (defun nnmail-decode-status ()
1816   "Return a status-value alist from STATUS."
1817   (goto-char (point-min))
1818   (when (re-search-forward "^Status: " nil t)
1819     (let (name value status)
1820       (save-restriction
1821         ;; Narrow to the status.
1822         (narrow-to-region
1823          (point)
1824          (if (re-search-forward "^[^ \t]" nil t)
1825              (1- (point))
1826            (point-max)))
1827         ;; Go through all elements and add them to the list.
1828         (goto-char (point-min))
1829         (while (re-search-forward "[^ \t=]+" nil t)
1830           (setq name (match-string 0))
1831           (if (not (eq (char-after) ?=))
1832               ;; Implied "yes".
1833               (setq value "yes")
1834             (forward-char 1)
1835             (if (not (eq (char-after) ?\"))
1836                 (if (not (looking-at "[^ \t]"))
1837                     ;; Implied "no".
1838                     (setq value "no")
1839                   ;; Unquoted value.
1840                   (setq value (match-string 0))
1841                   (goto-char (match-end 0)))
1842               ;; Quoted value.
1843               (setq value (read (current-buffer)))))
1844           (push (cons name value) status)))
1845       status)))
1846
1847 (defun nnmail-encode-status (status)
1848   "Return a status string from STATUS."
1849   (mapconcat
1850    (lambda (elem)
1851      (concat
1852       (car elem) "="
1853       (if (string-match "[ \t]" (cdr elem))
1854           (prin1-to-string (cdr elem))
1855         (cdr elem))))
1856    status " "))
1857
1858 (defun nnmail-split-history ()
1859   "Generate an overview of where the last mail split put articles."
1860   (interactive)
1861   (unless nnmail-split-history
1862     (error "No current split history"))
1863   (with-output-to-temp-buffer "*nnmail split history*"
1864     (with-current-buffer standard-output
1865       (fundamental-mode))               ; for Emacs 20.4+
1866     (let ((history nnmail-split-history)
1867           elem)
1868       (while (setq elem (pop history))
1869         (princ (mapconcat (lambda (ga)
1870                             (concat (car ga) ":" (int-to-string (cdr ga))))
1871                           elem
1872                           ", "))
1873         (princ "\n")))))
1874
1875 (defun nnmail-purge-split-history (group)
1876   "Remove all instances of GROUP from `nnmail-split-history'."
1877   (let ((history nnmail-split-history))
1878     (while history
1879       (setcar history (gnus-delete-if (lambda (e) (string= (car e) group))
1880                                       (car history)))
1881       (pop history))
1882     (setq nnmail-split-history (delq nil nnmail-split-history))))
1883
1884 (defun nnmail-new-mail-p (group)
1885   "Say whether GROUP has new mail."
1886   (let ((his nnmail-split-history)
1887         found)
1888     (while his
1889       (when (assoc group (pop his))
1890         (setq found t
1891               his nil)))
1892     found))
1893
1894 (defun nnmail-new-mail-numbers (group)
1895   "Say how many articles has been incorporated to GROUP."
1896   (let ((his (apply 'append nnmail-split-history))
1897         numbers)
1898     (while his
1899       (when (string= group (caar his))
1900         (push (cdar his) numbers))
1901       (setq his (cdr his)))
1902     numbers))
1903
1904 (defun nnmail-within-headers-p ()
1905   "Check to see if point is within the headers of a unix mail message.
1906 Doesn't change point."
1907   (let ((pos (point)))
1908     (save-excursion
1909       (and (nnmail-search-unix-mail-delim-backward)
1910            (not (search-forward "\n\n" pos t))))))
1911
1912 (run-hooks 'nnmail-load-hook)
1913
1914 (provide 'nnmail)
1915
1916 ;;; nnmail.el ends here