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