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