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