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