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