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