* gnus-sum.el (gnus-summary-goto-subject): Error on non-numerical
[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 ((restore (current-buffer)))
1075             (nnheader-set-temp-buffer "*Split Trace*")
1076             (gnus-add-buffer)
1077             (dolist (trace (nreverse nnmail-split-trace))
1078               (insert trace "\n"))
1079             (goto-char (point-min))
1080             (gnus-configure-windows 'split-trace)
1081             (set-buffer restore)))
1082         ;; See whether the split methods returned `junk'.
1083         (if (equal group-art '(junk))
1084             nil
1085           ;; The article may be "cross-posted" to `junk'.  What
1086           ;; to do?  Just remove the `junk' spec.  Don't really
1087           ;; see anything else to do...
1088           (let (elem)
1089             (while (setq elem (car (memq 'junk group-art)))
1090               (setq group-art (delq elem group-art)))
1091             (nreverse group-art)))))))
1092
1093 (defun nnmail-insert-lines ()
1094   "Insert how many lines there are in the body of the mail.
1095 Return the number of characters in the body."
1096   (let (lines chars)
1097     (save-excursion
1098       (goto-char (point-min))
1099       (unless (search-forward "\n\n" nil t)
1100         (goto-char (point-max))
1101         (insert "\n"))
1102       (setq chars (- (point-max) (point)))
1103       (setq lines (count-lines (point) (point-max)))
1104       (forward-char -1)
1105       (save-excursion
1106         (when (re-search-backward "^Lines: " nil t)
1107           (delete-region (point) (progn (forward-line 1) (point)))))
1108       (beginning-of-line)
1109       (insert (format "Lines: %d\n" (max lines 0)))
1110       chars)))
1111
1112 (defun nnmail-insert-xref (group-alist)
1113   "Insert an Xref line based on the (group . article) alist."
1114   (save-excursion
1115     (goto-char (point-min))
1116     (unless (search-forward "\n\n" nil t)
1117       (goto-char (point-max))
1118       (insert "\n"))
1119     (forward-char -1)
1120     (when (re-search-backward "^Xref: " nil t)
1121       (delete-region (match-beginning 0)
1122                      (progn (forward-line 1) (point))))
1123     (insert (format "Xref: %s" (system-name)))
1124     (while group-alist
1125       (insert (format " %s:%d"
1126                       (mm-encode-coding-string
1127                        (caar group-alist)
1128                        nnmail-pathname-coding-system)
1129                       (cdar group-alist)))
1130       (setq group-alist (cdr group-alist)))
1131     (insert "\n")))
1132
1133 ;;; Message washing functions
1134
1135 (defun nnmail-remove-leading-whitespace ()
1136   "Remove excessive whitespace from all headers."
1137   (goto-char (point-min))
1138   (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1139     (replace-match "\\1" t)))
1140
1141 (defun nnmail-remove-list-identifiers ()
1142   "Remove list identifiers from Subject headers."
1143   (let ((regexp
1144          (if (consp nnmail-list-identifiers)
1145              (mapconcat 'identity nnmail-list-identifiers " *\\|")
1146            nnmail-list-identifiers)))
1147     (when regexp
1148       (goto-char (point-min))
1149       (while (re-search-forward
1150               (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1151               nil t)
1152         (delete-region (match-beginning 2) (match-end 0))
1153         (beginning-of-line))
1154       (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
1155         (delete-region (match-beginning 1) (match-end 1))
1156         (beginning-of-line)))))
1157
1158 (defun nnmail-remove-tabs ()
1159   "Translate TAB characters into SPACE characters."
1160   (subst-char-in-region (point-min) (point-max) ?\t ?  t))
1161
1162 (defun nnmail-fix-eudora-headers ()
1163   "Eudora has a broken References line, but an OK In-Reply-To."
1164   (goto-char (point-min))
1165   (when (re-search-forward "^X-Mailer:.*Eudora" nil t)
1166     (goto-char (point-min))
1167     (when (re-search-forward "^References:" nil t)
1168       (beginning-of-line)
1169       (insert "X-Gnus-Broken-Eudora-"))
1170     (goto-char (point-min))
1171     (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1172       (replace-match "\\1" t))))
1173
1174 (custom-add-option 'nnmail-prepare-incoming-header-hook
1175                    'nnmail-fix-eudora-headers)
1176
1177 ;;; Utility functions
1178
1179 (defun nnmail-do-request-post (accept-func &optional server)
1180   "Utility function to directly post a message to an nnmail-derived group.
1181 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1182 to actually put the message in the right group."
1183   (let ((success t))
1184     (dolist (mbx (message-unquote-tokens
1185                   (message-tokenize-header
1186                    (message-fetch-field "Newsgroups") ", ")) success)
1187       (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1188         (or (gnus-active to-newsgroup)
1189             (gnus-activate-group to-newsgroup)
1190             (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
1191                                        to-newsgroup))
1192                 (or (and (gnus-request-create-group
1193                           to-newsgroup gnus-command-method)
1194                          (gnus-activate-group to-newsgroup nil nil
1195                                               gnus-command-method))
1196                     (error "Couldn't create group %s" to-newsgroup)))
1197             (error "No such group: %s" to-newsgroup))
1198         (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1199           (setq success nil))))))
1200
1201 (defun nnmail-split-fancy ()
1202   "Fancy splitting method.
1203 See the documentation for the variable `nnmail-split-fancy' for documentation."
1204   (let ((syntab (syntax-table)))
1205     (unwind-protect
1206         (progn
1207           (set-syntax-table nnmail-split-fancy-syntax-table)
1208           (nnmail-split-it nnmail-split-fancy))
1209       (set-syntax-table syntab))))
1210
1211 (defvar nnmail-split-cache nil)
1212 ;; Alist of split expressions their equivalent regexps.
1213
1214 (defun nnmail-split-it (split)
1215   ;; Return a list of groups matching SPLIT.
1216   (let (cached-pair)
1217     (cond
1218      ;; nil split
1219      ((null split)
1220       nil)
1221
1222      ;; A group name.  Do the \& and \N subs into the string.
1223      ((stringp split)
1224       (when nnmail-split-tracing
1225         (push (format "\"%s\"" split) nnmail-split-trace))
1226       (list (nnmail-expand-newtext split)))
1227
1228      ;; Junk the message.
1229      ((eq split 'junk)
1230       (when nnmail-split-tracing
1231         (push "junk" nnmail-split-trace))
1232       (list 'junk))
1233
1234      ;; Builtin & operation.
1235      ((eq (car split) '&)
1236       (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1237
1238      ;; Builtin | operation.
1239      ((eq (car split) '|)
1240       (let (done)
1241         (while (and (not done) (cdr split))
1242           (setq split (cdr split)
1243                 done (nnmail-split-it (car split))))
1244         done))
1245
1246      ;; Builtin : operation.
1247      ((eq (car split) ':)
1248       (nnmail-split-it (save-excursion (eval (cdr split)))))
1249
1250      ;; Builtin ! operation.
1251      ((eq (car split) '!)
1252       (funcall (cadr split) (nnmail-split-it (caddr split))))
1253
1254      ;; Check the cache for the regexp for this split.
1255      ((setq cached-pair (assq split nnmail-split-cache))
1256       (let (split-result
1257             (end-point (point-max))
1258             (value (nth 1 split)))
1259         (if (symbolp value)
1260             (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1261         (while (and (goto-char end-point)
1262                     (re-search-backward (cdr cached-pair) nil t))
1263           (when nnmail-split-tracing
1264             (push (cdr cached-pair) nnmail-split-trace))
1265           (let ((split-rest (cddr split))
1266                 (end (match-end 0))
1267                 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1268                 ;; So, start-of-value is the point just before the
1269                 ;; beginning of the value, whereas after-header-name
1270                 ;; is the point just after the field name.
1271                 (start-of-value (match-end 1))
1272                 (after-header-name (match-end 2)))
1273             ;; Start the next search just before the beginning of the
1274             ;; VALUE match.
1275             (setq end-point (1- start-of-value))
1276             ;; Handle - RESTRICTs
1277             (while (eq (car split-rest) '-)
1278               ;; RESTRICT must start after-header-name and
1279               ;; end after start-of-value, so that, for
1280               ;; (any "foo" - "x-foo" "foo.list")
1281               ;; we do not exclude foo.list just because
1282               ;; the header is: ``To: x-foo, foo''
1283               (goto-char end)
1284               (if (and (re-search-backward (cadr split-rest)
1285                                            after-header-name t)
1286                        (> (match-end 0) start-of-value))
1287                   (setq split-rest nil)
1288                 (setq split-rest (cddr split-rest))))
1289             (when split-rest
1290               (goto-char end)
1291               (let ((value (nth 1 split)))
1292                 (if (symbolp value)
1293                     (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1294         ;; Someone might want to do a \N sub on this match, so get the
1295                 ;; correct match positions.
1296                 (re-search-backward value start-of-value))
1297               (dolist (sp (nnmail-split-it (car split-rest)))
1298                 (unless (memq sp split-result)
1299                   (push sp split-result))))))
1300         split-result))
1301
1302      ;; Not in cache, compute a regexp for the field/value pair.
1303      (t
1304       (let* ((field (nth 0 split))
1305              (value (nth 1 split))
1306              partial-front regexp
1307              partial-rear  regexp)
1308         (if (symbolp value)
1309             (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1310         (if (and (>= (length value) 2)
1311                  (string= ".*" (substring value 0 2)))
1312             (setq value (substring value 2)
1313                   partial-front ""))
1314         ;; Same trick for the rear of the regexp
1315         (if (and (>= (length value) 2)
1316                  (string= ".*" (substring value -2)))
1317             (setq value (substring value 0 -2)
1318                   partial-rear ""))
1319         (setq regexp (concat "^\\(\\("
1320                              (if (symbolp field)
1321                                  (cdr (assq field nnmail-split-abbrev-alist))
1322                                field)
1323                              "\\):.*\\)"
1324                              (or partial-front "\\<")
1325                              "\\("
1326                              value
1327                              "\\)"
1328                              (or partial-rear "\\>")))
1329         (push (cons split regexp) nnmail-split-cache)
1330         ;; Now that it's in the cache, just call nnmail-split-it again
1331     ;; on the same split, which will find it immediately in the cache.
1332         (nnmail-split-it split))))))
1333
1334 (defun nnmail-expand-newtext (newtext)
1335   (let ((len (length newtext))
1336         (pos 0)
1337         c expanded beg N did-expand)
1338     (while (< pos len)
1339       (setq beg pos)
1340       (while (and (< pos len)
1341                   (not (= (aref newtext pos) ?\\)))
1342         (setq pos (1+ pos)))
1343       (unless (= beg pos)
1344         (push (substring newtext beg pos) expanded))
1345       (when (< pos len)
1346         ;; We hit a \; expand it.
1347         (setq did-expand t
1348               pos (1+ pos)
1349               c (aref newtext pos))
1350         (if (not (or (= c ?\&)
1351                      (and (>= c ?1)
1352                           (<= c ?9))))
1353             ;; \ followed by some character we don't expand.
1354             (push (char-to-string c) expanded)
1355           ;; \& or \N
1356           (if (= c ?\&)
1357               (setq N 0)
1358             (setq N (- c ?0)))
1359           (when (match-beginning N)
1360             (push (buffer-substring (match-beginning N) (match-end N))
1361                   expanded))))
1362       (setq pos (1+ pos)))
1363     (if did-expand
1364         (apply 'concat (nreverse expanded))
1365       newtext)))
1366
1367 ;; Activate a backend only if it isn't already activated.
1368 ;; If FORCE, re-read the active file even if the backend is
1369 ;; already activated.
1370 (defun nnmail-activate (backend &optional force)
1371   (nnheader-init-server-buffer)
1372   (let (file timestamp file-time)
1373     (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1374             force
1375             (and (setq file (ignore-errors
1376                               (symbol-value (intern (format "%s-active-file"
1377                                                             backend)))))
1378                  (setq file-time (nth 5 (file-attributes file)))
1379                  (or (not
1380                       (setq timestamp
1381                             (condition-case ()
1382                                 (symbol-value (intern
1383                                                (format "%s-active-timestamp"
1384                                                        backend)))
1385                               (error 'none))))
1386                      (not (consp timestamp))
1387                      (equal timestamp '(0 0))
1388                      (> (nth 0 file-time) (nth 0 timestamp))
1389                      (and (= (nth 0 file-time) (nth 0 timestamp))
1390                           (> (nth 1 file-time) (nth 1 timestamp))))))
1391         (save-excursion
1392           (or (eq timestamp 'none)
1393               (set (intern (format "%s-active-timestamp" backend))
1394                    file-time))
1395           (funcall (intern (format "%s-request-list" backend)))))
1396     t))
1397
1398 (defun nnmail-message-id ()
1399   (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1400
1401 ;;;
1402 ;;; nnmail duplicate handling
1403 ;;;
1404
1405 (defvar nnmail-cache-buffer nil)
1406
1407 (defun nnmail-cache-open ()
1408   (if (or (not nnmail-treat-duplicates)
1409           (and nnmail-cache-buffer
1410                (buffer-name nnmail-cache-buffer)))
1411       ()                                ; The buffer is open.
1412     (save-excursion
1413       (set-buffer
1414        (setq nnmail-cache-buffer
1415              (get-buffer-create " *nnmail message-id cache*")))
1416       (gnus-add-buffer)
1417       (when (file-exists-p nnmail-message-id-cache-file)
1418         (nnheader-insert-file-contents nnmail-message-id-cache-file))
1419       (set-buffer-modified-p nil)
1420       (current-buffer))))
1421
1422 (defun nnmail-cache-close ()
1423   (when (and nnmail-cache-buffer
1424              nnmail-treat-duplicates
1425              (buffer-name nnmail-cache-buffer)
1426              (buffer-modified-p nnmail-cache-buffer))
1427     (save-excursion
1428       (set-buffer nnmail-cache-buffer)
1429       ;; Weed out the excess number of Message-IDs.
1430       (goto-char (point-max))
1431       (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1432         (progn
1433           (beginning-of-line)
1434           (delete-region (point-min) (point))))
1435       ;; Save the buffer.
1436       (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1437           (make-directory (file-name-directory nnmail-message-id-cache-file)
1438                           t))
1439       (nnmail-write-region (point-min) (point-max)
1440                            nnmail-message-id-cache-file nil 'silent)
1441       (set-buffer-modified-p nil)
1442       (setq nnmail-cache-buffer nil)
1443       (kill-buffer (current-buffer)))))
1444
1445 ;; Compiler directives.
1446 (defvar group)
1447 (defvar group-art-list)
1448 (defvar group-art)
1449 (defun nnmail-cache-insert (id)
1450   (when nnmail-treat-duplicates
1451     ;; Store some information about the group this message is written
1452     ;; to.  This function might have been called from various places.
1453     ;; Sometimes, a function up in the calling sequence has an
1454     ;; argument GROUP which is bound to a string, the group name.  At
1455     ;; other times, there is a function up in the calling sequence
1456     ;; which has an argument GROUP-ART which is a list of pairs, and
1457     ;; the car of a pair is a group name.  Should we check that the
1458     ;; length of the list is equal to 1? -- kai
1459     (let ((g nil))
1460       (cond ((and (boundp 'group) group)
1461              (setq g group))
1462             ((and (boundp 'group-art-list) group-art-list
1463                   (listp group-art-list))
1464              (setq g (caar group-art-list)))
1465             ((and (boundp 'group-art) group-art (listp group-art))
1466              (setq g (caar group-art)))
1467             (t (setq g "")))
1468       (unless (gnus-buffer-live-p nnmail-cache-buffer)
1469         (nnmail-cache-open))
1470       (save-excursion
1471         (set-buffer nnmail-cache-buffer)
1472         (goto-char (point-max))
1473         (if (and g (not (string= "" g))
1474                  (gnus-methods-equal-p gnus-command-method
1475                                        (nnmail-cache-primary-mail-backend)))
1476             (insert id "\t" g "\n")
1477           (insert id "\n"))))))
1478
1479 (defun nnmail-cache-primary-mail-backend ()
1480   (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1481         (be nil)
1482         (res nil))
1483     (while (and (null res) be-list)
1484       (setq be (car be-list))
1485       (setq be-list (cdr be-list))
1486       (when (and (gnus-method-option-p be 'respool)
1487                  (eval (intern (format "%s-get-new-mail" (car be)))))
1488         (setq res be)))
1489     res))
1490
1491 ;; Fetch the group name corresponding to the message id stored in the
1492 ;; cache.
1493 (defun nnmail-cache-fetch-group (id)
1494   (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1495     (save-excursion
1496       (set-buffer nnmail-cache-buffer)
1497       (goto-char (point-max))
1498       (when (search-backward id nil t)
1499         (beginning-of-line)
1500         (skip-chars-forward "^\n\r\t")
1501         (unless (eolp)
1502           (forward-char 1)
1503           (buffer-substring (point)
1504                             (progn (end-of-line) (point))))))))
1505
1506 ;; Function for nnmail-split-fancy: look up all references in the
1507 ;; cache and if a match is found, return that group.
1508 (defun nnmail-split-fancy-with-parent ()
1509   "Split this message into the same group as its parent.
1510 This function can be used as an entry in `nnmail-split-fancy', for
1511 example like this: (: nnmail-split-fancy)
1512 For a message to be split, it looks for the parent message in the
1513 References or In-Reply-To header and then looks in the message id
1514 cache file (given by the variable `nnmail-message-id-cache-file') to
1515 see which group that message was put in.  This group is returned.
1516
1517 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1518   (let* ((refstr (or (message-fetch-field "references")
1519                      (message-fetch-field "in-reply-to")))
1520          (references nil)
1521          (res nil)
1522          (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1523                      (mapconcat
1524                       (lambda (x) (format "\\(%s\\)" x))
1525                       nnmail-split-fancy-with-parent-ignore-groups
1526                       "\\|")
1527                    nnmail-split-fancy-with-parent-ignore-groups)))
1528     (when refstr
1529       (setq references (nreverse (gnus-split-references refstr)))
1530       (unless (gnus-buffer-live-p nnmail-cache-buffer)
1531         (nnmail-cache-open))
1532       (mapcar (lambda (x)
1533                 (setq res (or (nnmail-cache-fetch-group x) res))
1534                 (when (or (string= "drafts" res)
1535                           (and regexp res (string-match regexp res)))
1536                   (setq res nil)))
1537               references)
1538       res)))
1539
1540 (defun nnmail-cache-id-exists-p (id)
1541   (when nnmail-treat-duplicates
1542     (save-excursion
1543       (set-buffer nnmail-cache-buffer)
1544       (goto-char (point-max))
1545       (search-backward id nil t))))
1546
1547 (defun nnmail-fetch-field (header)
1548   (save-excursion
1549     (save-restriction
1550       (message-narrow-to-head)
1551       (message-fetch-field header))))
1552
1553 (defun nnmail-check-duplication (message-id func artnum-func)
1554   (run-hooks 'nnmail-prepare-incoming-message-hook)
1555   ;; If this is a duplicate message, then we do not save it.
1556   (let* ((duplication (nnmail-cache-id-exists-p message-id))
1557          (case-fold-search t)
1558          (action (when duplication
1559                    (cond
1560                     ((memq nnmail-treat-duplicates '(warn delete))
1561                      nnmail-treat-duplicates)
1562                     ((nnheader-functionp nnmail-treat-duplicates)
1563                      (funcall nnmail-treat-duplicates message-id))
1564                     (t
1565                      nnmail-treat-duplicates))))
1566          group-art)
1567     ;; We insert a line that says what the mail source is.
1568     (let ((case-fold-search t))
1569       (goto-char (point-min))
1570       (re-search-forward "^message-id[ \t]*:" nil t)
1571       (beginning-of-line)
1572       (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1573
1574     ;; Let the backend save the article (or not).
1575     (cond
1576      ((not duplication)
1577       (funcall func (setq group-art
1578                           (nreverse (nnmail-article-group artnum-func))))
1579       (nnmail-cache-insert message-id))
1580      ((eq action 'delete)
1581       (setq group-art nil))
1582      ((eq action 'warn)
1583       ;; We insert a warning.
1584       (let ((case-fold-search t))
1585         (goto-char (point-min))
1586         (re-search-forward "^message-id[ \t]*:" nil t)
1587         (beginning-of-line)
1588         (insert
1589          "Gnus-Warning: This is a duplicate of message " message-id "\n")
1590         (funcall func (setq group-art
1591                             (nreverse (nnmail-article-group artnum-func))))))
1592      (t
1593       (funcall func (setq group-art
1594                           (nreverse (nnmail-article-group artnum-func))))))
1595     ;; Add the group-art list to the history list.
1596     (if group-art
1597         (push group-art nnmail-split-history)
1598       (delete-region (point-min) (point-max)))))
1599
1600 ;;; Get new mail.
1601
1602 (defvar nnmail-fetched-sources nil)
1603
1604 (defun nnmail-get-value (&rest args)
1605   (let ((sym (intern (apply 'format args))))
1606     (when (boundp sym)
1607       (symbol-value sym))))
1608
1609 (defun nnmail-get-new-mail (method exit-func temp
1610                                    &optional group spool-func)
1611   "Read new incoming mail."
1612   (let* ((sources (or mail-sources
1613                       (if (listp nnmail-spool-file) nnmail-spool-file
1614                         (list nnmail-spool-file))))
1615          fetching-sources
1616          (group-in group)
1617          (i 0)
1618          (new 0)
1619          (total 0)
1620          incoming incomings source)
1621     (when (and (nnmail-get-value "%s-get-new-mail" method)
1622                sources)
1623       (while (setq source (pop sources))
1624         ;; Be compatible with old values.
1625         (cond
1626          ((stringp source)
1627           (setq source
1628                 (cond
1629                  ((string-match "^po:" source)
1630                   (list 'pop :user (substring source (match-end 0))))
1631                  ((file-directory-p source)
1632                   (list 'directory :path source))
1633                  (t
1634                   (list 'file :path source)))))
1635          ((eq source 'procmail)
1636           (message "Invalid value for nnmail-spool-file: `procmail'")
1637           nil))
1638         ;; Hack to only fetch the contents of a single group's spool file.
1639         (when (and (eq (car source) 'directory)
1640                    (null nnmail-scan-directory-mail-source-once)
1641                    group)
1642           (mail-source-bind (directory source)
1643             (setq source (append source
1644                                  (list
1645                                   :predicate
1646                                   (gnus-byte-compile
1647                                    `(lambda (file)
1648                                       (string-equal
1649                                        ,(concat group suffix)
1650                                        (file-name-nondirectory file)))))))))
1651         (when nnmail-fetched-sources
1652           (if (member source nnmail-fetched-sources)
1653               (setq source nil)
1654             (push source nnmail-fetched-sources)
1655             (push source fetching-sources)))))
1656     (when fetching-sources
1657       ;; We first activate all the groups.
1658       (nnmail-activate method)
1659       ;; Allow the user to hook.
1660       (run-hooks 'nnmail-pre-get-new-mail-hook)
1661       ;; Open the message-id cache.
1662       (nnmail-cache-open)
1663       ;; The we go through all the existing mail source specification
1664       ;; and fetch the mail from each.
1665       (while (setq source (pop fetching-sources))
1666         (nnheader-message 4 "%s: Reading incoming mail from %s..."
1667                           method (car source))
1668         (when (setq new
1669                     (mail-source-fetch
1670                      source
1671                      (gnus-byte-compile
1672                       `(lambda (file orig-file)
1673                          (nnmail-split-incoming
1674                           file ',(intern (format "%s-save-mail" method))
1675                           ',spool-func
1676                           (if (equal file orig-file)
1677                               nil
1678                             (nnmail-get-split-group orig-file ',source))
1679                           ',(intern (format "%s-active-number" method)))))))
1680           (incf total new)
1681           (incf i)))
1682       ;; If we did indeed read any incoming spools, we save all info.
1683       (if (zerop total)
1684           (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1685                             method (car source))
1686         (nnmail-save-active
1687          (nnmail-get-value "%s-group-alist" method)
1688          (nnmail-get-value "%s-active-file" method))
1689         (when exit-func
1690           (funcall exit-func))
1691         (run-hooks 'nnmail-read-incoming-hook)
1692         (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1693                           total))
1694       ;; Close the message-id cache.
1695       (nnmail-cache-close)
1696       ;; Allow the user to hook.
1697       (run-hooks 'nnmail-post-get-new-mail-hook))))
1698
1699 (defun nnmail-expired-article-p (group time force &optional inhibit)
1700   "Say whether an article that is TIME old in GROUP should be expired."
1701   (if force
1702       t
1703     (let ((days (or (and nnmail-expiry-wait-function
1704                          (funcall nnmail-expiry-wait-function group))
1705                     nnmail-expiry-wait)))
1706       (cond ((or (eq days 'never)
1707                  (and (not force)
1708                       inhibit))
1709              ;; This isn't an expirable group.
1710              nil)
1711             ((eq days 'immediate)
1712              ;; We expire all articles on sight.
1713              t)
1714             ((equal time '(0 0))
1715             ;; This is an ange-ftp group, and we don't have any dates.
1716              nil)
1717             ((numberp days)
1718              (setq days (days-to-time days))
1719              ;; Compare the time with the current time.
1720              (ignore-errors (time-less-p days (time-since time))))))))
1721
1722 (defun nnmail-expiry-target-group (target group)
1723   ;; Do not invoke this from nntp-server-buffer!  At least nnfolder clears
1724   ;; that buffer if the nnfolder group isn't selected.
1725   (let (nnmail-cache-accepted-message-ids)
1726     ;; Don't enter Message-IDs into cache.
1727     ;; Let users hack it in TARGET function.
1728     (when (nnheader-functionp target)
1729       (setq target (funcall target group)))
1730     (unless (eq target 'delete)
1731       (gnus-request-accept-article target nil nil t))))
1732
1733 (defun nnmail-fancy-expiry-target (group)
1734   "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1735   (let* (header
1736          (case-fold-search nil)
1737          (from (or (message-fetch-field "from") ""))
1738          (to (or (message-fetch-field "to") ""))
1739          (date (date-to-time
1740                 (or (message-fetch-field "date") (current-time-string))))
1741          (target 'delete))
1742     (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1743       (setq header (car regexp-target-pair))
1744       (cond
1745        ;; If the header is to-from then match against the
1746        ;; To or From header
1747        ((and (equal header 'to-from)
1748              (or (string-match (cadr regexp-target-pair) from)
1749                  (and (string-match message-dont-reply-to-names from)
1750                       (string-match (cadr regexp-target-pair) to))))
1751         (setq target (format-time-string (caddr regexp-target-pair) date)))
1752        ((and (not (equal header 'to-from))
1753              (string-match (cadr regexp-target-pair)
1754                            (message-fetch-field header)))
1755         (setq target
1756               (format-time-string (caddr regexp-target-pair) date)))))))
1757
1758 (defun nnmail-check-syntax ()
1759   "Check (and modify) the syntax of the message in the current buffer."
1760   (save-restriction
1761     (message-narrow-to-head)
1762     (let ((case-fold-search t))
1763       (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1764         (insert "Message-ID: " (nnmail-message-id) "\n")))))
1765
1766 (defun nnmail-write-region (start end filename &optional append visit lockname)
1767   "Do a `write-region', and then set the file modes."
1768   (let ((coding-system-for-write nnmail-file-coding-system)
1769         (file-name-coding-system nnmail-pathname-coding-system))
1770     (write-region start end filename append visit lockname)
1771     (set-file-modes filename nnmail-default-file-modes)))
1772
1773 ;;;
1774 ;;; Status functions
1775 ;;;
1776
1777 (defun nnmail-replace-status (name value)
1778   "Make status NAME and VALUE part of the current status line."
1779   (save-restriction
1780     (message-narrow-to-head)
1781     (let ((status (nnmail-decode-status)))
1782       (setq status (delq (member name status) status))
1783       (when value
1784         (push (cons name value) status))
1785       (message-remove-header "status")
1786       (goto-char (point-max))
1787       (insert "Status: " (nnmail-encode-status status) "\n"))))
1788
1789 (defun nnmail-decode-status ()
1790   "Return a status-value alist from STATUS."
1791   (goto-char (point-min))
1792   (when (re-search-forward "^Status: " nil t)
1793     (let (name value status)
1794       (save-restriction
1795         ;; Narrow to the status.
1796         (narrow-to-region
1797          (point)
1798          (if (re-search-forward "^[^ \t]" nil t)
1799              (1- (point))
1800            (point-max)))
1801         ;; Go through all elements and add them to the list.
1802         (goto-char (point-min))
1803         (while (re-search-forward "[^ \t=]+" nil t)
1804           (setq name (match-string 0))
1805           (if (not (eq (char-after) ?=))
1806               ;; Implied "yes".
1807               (setq value "yes")
1808             (forward-char 1)
1809             (if (not (eq (char-after) ?\"))
1810                 (if (not (looking-at "[^ \t]"))
1811                     ;; Implied "no".
1812                     (setq value "no")
1813                   ;; Unquoted value.
1814                   (setq value (match-string 0))
1815                   (goto-char (match-end 0)))
1816               ;; Quoted value.
1817               (setq value (read (current-buffer)))))
1818           (push (cons name value) status)))
1819       status)))
1820
1821 (defun nnmail-encode-status (status)
1822   "Return a status string from STATUS."
1823   (mapconcat
1824    (lambda (elem)
1825      (concat
1826       (car elem) "="
1827       (if (string-match "[ \t]" (cdr elem))
1828           (prin1-to-string (cdr elem))
1829         (cdr elem))))
1830    status " "))
1831
1832 (defun nnmail-split-history ()
1833   "Generate an overview of where the last mail split put articles."
1834   (interactive)
1835   (unless nnmail-split-history
1836     (error "No current split history"))
1837   (with-output-to-temp-buffer "*nnmail split history*"
1838     (with-current-buffer standard-output
1839       (fundamental-mode))               ; for Emacs 20.4+
1840     (let ((history nnmail-split-history)
1841           elem)
1842       (while (setq elem (pop history))
1843         (princ (mapconcat (lambda (ga)
1844                             (concat (car ga) ":" (int-to-string (cdr ga))))
1845                           elem
1846                           ", "))
1847         (princ "\n")))))
1848
1849 (defun nnmail-purge-split-history (group)
1850   "Remove all instances of GROUP from `nnmail-split-history'."
1851   (let ((history nnmail-split-history))
1852     (while history
1853       (setcar history (gnus-delete-if (lambda (e) (string= (car e) group))
1854                                       (car history)))
1855       (pop history))
1856     (setq nnmail-split-history (delq nil nnmail-split-history))))
1857
1858 (defun nnmail-new-mail-p (group)
1859   "Say whether GROUP has new mail."
1860   (let ((his nnmail-split-history)
1861         found)
1862     (while his
1863       (when (assoc group (pop his))
1864         (setq found t
1865               his nil)))
1866     found))
1867
1868 (defun nnmail-within-headers-p ()
1869   "Check to see if point is within the headers of a unix mail message.
1870 Doesn't change point."
1871   (let ((pos (point)))
1872     (save-excursion
1873       (and (nnmail-search-unix-mail-delim-backward)
1874            (not (search-forward "\n\n" pos t))))))
1875
1876 (run-hooks 'nnmail-load-hook)
1877
1878 (provide 'nnmail)
1879
1880 ;;; nnmail.el ends here