fbef618ac878841c47ab5292af781120ac57e153
[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
37 (eval-and-compile
38   (autoload 'gnus-error "gnus-util")
39   (autoload 'gnus-buffer-live-p "gnus-util")
40   (autoload 'gnus-add-buffer "gnus"))
41
42 (defgroup nnmail nil
43   "Reading mail with Gnus."
44   :group 'gnus)
45
46 (defgroup nnmail-retrieve nil
47   "Retrieving new mail."
48   :group 'nnmail)
49
50 (defgroup nnmail-prepare nil
51   "Preparing (or mangling) new mail after retrival."
52   :group 'nnmail)
53
54 (defgroup nnmail-duplicate nil
55   "Handling of duplicate mail messages."
56   :group 'nnmail)
57
58 (defgroup nnmail-split nil
59   "Organizing the incomming mail in folders."
60   :group 'nnmail)
61
62 (defgroup nnmail-files nil
63   "Mail files."
64   :group 'gnus-files
65   :group 'nnmail)
66
67 (defgroup nnmail-expire nil
68   "Expiring old mail."
69   :group 'nnmail)
70
71 (defgroup nnmail-procmail nil
72   "Interfacing with procmail and other mail agents."
73   :group 'nnmail)
74
75 (defgroup nnmail-various nil
76   "Various mail options."
77   :group 'nnmail)
78
79 (defcustom nnmail-split-methods '(("mail.misc" ""))
80   "*Incoming mail will be split according to this variable.
81
82 If you'd like, for instance, one mail group for mail from the
83 \"4ad-l\" mailing list, one group for junk mail and one for everything
84 else, you could do something like this:
85
86  (setq nnmail-split-methods
87        '((\"mail.4ad\" \"From:.*4ad\")
88          (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
89          (\"mail.misc\" \"\")))
90
91 As you can see, this variable is a list of lists, where the first
92 element in each \"rule\" is the name of the group (which, by the way,
93 does not have to be called anything beginning with \"mail\",
94 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
95 nnmail will try to match on the header to find a fit.
96
97 The second element can also be a function.  In that case, it will be
98 called narrowed to the headers with the first element of the rule as
99 the argument.  It should return a non-nil value if it thinks that the
100 mail belongs in that group.
101
102 The last element should always have \"\" as the regexp.
103
104 This variable can also have a function as its value."
105   :group 'nnmail-split
106   :type '(choice (repeat :tag "Alist" (group (string :tag "Name") regexp))
107                  (function-item nnmail-split-fancy)
108                  (function :tag "Other")))
109
110 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
111 (defcustom nnmail-crosspost t
112   "If non-nil, do crossposting if several split methods match the mail.
113 If nil, the first match found will be used."
114   :group 'nnmail-split
115   :type 'boolean)
116
117 (defcustom nnmail-split-fancy-with-parent-ignore-groups nil
118   "Regexp that matches group names to be ignored when applying
119 `nnmail-split-fancy-with-parent'.  This can also be a list of regexps."
120   :group 'nnmail-split
121   :type '(choice (const :tag "none" nil)
122                  (regexp :value ".*")
123                  (repeat :value (".*") regexp)))
124
125 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
126 (defcustom nnmail-keep-last-article nil
127   "If non-nil, nnmail will never delete/move a group's last article.
128 It can be marked expirable, so it will be deleted when it is no longer last.
129
130 You may need to set this variable if other programs are putting
131 new mail into folder numbers that Gnus has marked as expired."
132   :group 'nnmail-procmail
133   :group 'nnmail-various
134   :type 'boolean)
135
136 (defcustom nnmail-use-long-file-names nil
137   "If non-nil the mail backends will use long file and directory names.
138 If nil, groups like \"mail.misc\" will end up in directories like
139 \"mail/misc/\"."
140   :group 'nnmail-files
141   :type 'boolean)
142
143 (defcustom nnmail-default-file-modes 384
144   "Set the mode bits of all new mail files to this integer."
145   :group 'nnmail-files
146   :type 'integer)
147
148 (defcustom nnmail-expiry-wait 7
149   "*Expirable articles that are older than this will be expired.
150 This variable can either be a number (which will be interpreted as a
151 number of days) -- this doesn't have to be an integer.  This variable
152 can also be `immediate' and `never'."
153   :group 'nnmail-expire
154   :type '(choice (const immediate)
155                  (number :tag "days")
156                  (const never)))
157
158 (defcustom nnmail-expiry-wait-function nil
159   "Variable that holds function to specify how old articles should be before they are expired.
160   The function will be called with the name of the group that the
161 expiry is to be performed in, and it should return an integer that
162 says how many days an article can be stored before it is considered
163 \"old\".  It can also return the values `never' and `immediate'.
164
165 Eg.:
166
167 \(setq nnmail-expiry-wait-function
168       (lambda (newsgroup)
169         (cond ((string-match \"private\" newsgroup) 31)
170               ((string-match \"junk\" newsgroup) 1)
171               ((string-match \"important\" newsgroup) 'never)
172               (t 7))))"
173   :group 'nnmail-expire
174   :type '(choice (const :tag "nnmail-expiry-wait" nil)
175                  (function :format "%v" nnmail-)))
176
177 (defcustom nnmail-expiry-target 'delete
178   "*Variable that says where expired messages should end up.
179 The default value is `delete' (which says to delete the messages),
180 but it can also be a string or a function.  If it is a string, expired
181 messages end up in that group.  If it is a function, the function is
182 called in a buffer narrowed to the message in question.  The function
183 receives one argument, the name of the group the message comes from.
184 The return value should be `delete' or a group name (a string)."
185   :version "21.1"
186   :group 'nnmail-expire
187   :type '(choice (const delete)
188                  (function :format "%v" nnmail-)
189                  string))
190
191 (defcustom nnmail-fancy-expiry-targets nil
192   "Determine expiry target based on articles using fancy techniques.
193
194 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries.  If
195 `nnmail-expiry-target' is set to the function
196 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
197 the message will be expired to a group determined by invoking
198 `format-time-string' with TARGET used as the format string and the
199 time extracted from the articles' Date header (if missing the current
200 time is used).
201
202 In the special cases that HEADER is the symbol `to-from', the regexp
203 will try to match against both the From and the To header.
204
205 Example:
206
207 \(setq nnmail-fancy-expiry-targets
208       '((to-from \"boss\" \"nnfolder:Work\")
209         (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
210         (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
211
212 In this case, articles containing the string \"boss\" in the To or the
213 From header will be expired to the group \"nnfolder:Work\";
214 articles containing the sting \"IMPORTANT\" in the Subject header will
215 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
216 everything else will be expired to \"nnfolder:Archive-YYYY\"."
217   :group 'nnmail-expire
218   :type '(repeat (list (choice :tag "Match against"
219                                (string :tag "Header")
220                                (const to-from))
221                        regexp
222                        (string :tag "Target group format string"))))
223
224 (defcustom nnmail-cache-accepted-message-ids nil
225   "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
226 If non-nil, also update the cache when copy or move articles."
227   :group 'nnmail
228   :type 'boolean)
229
230 (defcustom nnmail-spool-file '((file))
231   "*Where the mail backends will look for incoming mail.
232 This variable is a list of mail source specifiers.
233 This variable is obsolete; `mail-sources' should be used instead."
234   :group 'nnmail-files
235   :type 'sexp)
236
237 (defcustom nnmail-resplit-incoming nil
238   "*If non-nil, re-split incoming procmail sorted mail."
239   :group 'nnmail-procmail
240   :type 'boolean)
241
242 (defcustom nnmail-scan-directory-mail-source-once nil
243   "*If non-nil, scan all incoming procmail sorted mails once.
244 It scans low-level sorted spools even when not required."
245   :version "21.1"
246   :group 'nnmail-procmail
247   :type 'boolean)
248
249 (defcustom nnmail-delete-file-function 'delete-file
250   "Function called to delete files in some mail backends."
251   :group 'nnmail-files
252   :type 'function)
253
254 (defcustom nnmail-crosspost-link-function
255   (if (string-match "windows-nt\\|emx" (symbol-name system-type))
256       'copy-file
257     'add-name-to-file)
258   "*Function called to create a copy of a file.
259 This is `add-name-to-file' by default, which means that crossposts
260 will use hard links.  If your file system doesn't allow hard
261 links, you could set this variable to `copy-file' instead."
262   :group 'nnmail-files
263   :type '(radio (function-item add-name-to-file)
264                 (function-item copy-file)
265                 (function :tag "Other")))
266
267 (defcustom nnmail-read-incoming-hook
268   (if (eq system-type 'windows-nt)
269       '(nnheader-ms-strip-cr)
270     nil)
271   "*Hook that will be run after the incoming mail has been transferred.
272 The incoming mail is moved from the specified spool file (which normally is
273 something like \"/usr/spool/mail/$user\") to the user's home
274 directory.  This hook is called after the incoming mail box has been
275 emptied, and can be used to call any mail box programs you have
276 running (\"xwatch\", etc.)
277
278 Eg.
279
280 \(add-hook 'nnmail-read-incoming-hook
281           (lambda ()
282             (call-process \"/local/bin/mailsend\" nil nil nil
283                           \"read\" nnmail-spool-file)))
284
285 If you have xwatch running, this will alert it that mail has been
286 read.
287
288 If you use `display-time', you could use something like this:
289
290 \(add-hook 'nnmail-read-incoming-hook
291           (lambda ()
292             ;; Update the displayed time, since that will clear out
293             ;; the flag that says you have mail.
294             (when (eq (process-status \"display-time\") 'run)
295               (display-time-filter display-time-process \"\"))))"
296   :group 'nnmail-prepare
297   :type 'hook)
298
299 (defcustom nnmail-prepare-incoming-hook nil
300   "Hook called before treating incoming mail.
301 The hook is run in a buffer with all the new, incoming mail."
302   :group 'nnmail-prepare
303   :type 'hook)
304
305 (defcustom nnmail-prepare-incoming-header-hook nil
306   "Hook called narrowed to the headers of each message.
307 This can be used to remove excessive spaces (and stuff like
308 that) from the headers before splitting and saving the messages."
309   :group 'nnmail-prepare
310   :type 'hook)
311
312 (defcustom nnmail-prepare-incoming-message-hook nil
313   "Hook called narrowed to each message."
314   :group 'nnmail-prepare
315   :type 'hook)
316
317 (defcustom nnmail-list-identifiers nil
318   "Regexp that matches list identifiers to be removed.
319 This can also be a list of regexps."
320   :group 'nnmail-prepare
321   :type '(choice (const :tag "none" nil)
322                  (regexp :value ".*")
323                  (repeat :value (".*") regexp)))
324
325 (defcustom nnmail-pre-get-new-mail-hook nil
326   "Hook called just before starting to handle new incoming mail."
327   :group 'nnmail-retrieve
328   :type 'hook)
329
330 (defcustom nnmail-post-get-new-mail-hook nil
331   "Hook called just after finishing handling new incoming mail."
332   :group 'nnmail-retrieve
333   :type 'hook)
334
335 (defcustom nnmail-split-hook nil
336   "Hook called before deciding where to split an article.
337 The functions in this hook are free to modify the buffer
338 contents in any way they choose -- the buffer contents are
339 discarded after running the split process."
340   :group 'nnmail-split
341   :type 'hook)
342
343 (defcustom nnmail-large-newsgroup 50
344   "*The number of the articles which indicates a large newsgroup.
345 If the number of the articles is greater than the value, verbose
346 messages will be shown to indicate the current status."
347   :group 'nnmail-various
348   :type 'integer)
349
350 (defcustom nnmail-split-fancy "mail.misc"
351   "Incoming mail can be split according to this fancy variable.
352 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
353
354 The format of this variable is SPLIT, where SPLIT can be one of
355 the following:
356
357 GROUP: Mail will be stored in GROUP (a string).
358
359 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
360   field FIELD (a regexp) contains VALUE (a regexp), store the messages
361   as specified by SPLIT.  If RESTRICT (a regexp) matches some string
362   after FIELD and before the end of the matched VALUE, return nil,
363   otherwise process SPLIT.  Multiple RESTRICTs add up, further
364   restricting the possibility of processing SPLIT.
365
366 \(| SPLIT...): Process each SPLIT expression until one of them matches.
367   A SPLIT expression is said to match if it will cause the mail
368   message to be stored in one or more groups.
369
370 \(& SPLIT...): Process each SPLIT expression.
371
372 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
373   the buffer containing the message headers.  The return value FUNCTION
374   should be a split, which is then recursively processed.
375
376 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT.  The
377   return value FUNCTION should be a split, which is then recursively
378   processed.
379
380 junk: Mail will be deleted.  Use with care!  Do not submerge in water!
381   Example:
382   (setq nnmail-split-fancy
383         '(| (\"Subject\" \"MAKE MONEY FAST\" junk)
384             ...other.rules.omitted...))
385
386 FIELD must match a complete field name.  VALUE must match a complete
387 word according to the `nnmail-split-fancy-syntax-table' syntax table.
388 You can use \".*\" in the regexps to match partial field names or words.
389
390 FIELD and VALUE can also be lisp symbols, in that case they are expanded
391 as specified in `nnmail-split-abbrev-alist'.
392
393 GROUP can contain \\& and \\N which will substitute from matching
394 \\(\\) patterns in the previous VALUE.
395
396 Example:
397
398 \(setq nnmail-split-methods 'nnmail-split-fancy
399       nnmail-split-fancy
400       ;; Messages from the mailer daemon are not crossposted to any of
401       ;; the ordinary groups.  Warnings are put in a separate group
402       ;; from real errors.
403       '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
404                           \"mail.misc\"))
405           ;; Non-error messages are crossposted to all relevant
406           ;; groups, but we don't crosspost between the group for the
407           ;; (ding) list and the group for other (ding) related mail.
408           (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
409                 (\"subject\" \"ding\" \"ding.misc\"))
410              ;; Other mailing lists...
411              (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
412              (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
413              ;; Both lists below have the same suffix, so prevent
414              ;; cross-posting to mkpkg.list of messages posted only to
415              ;; the bugs- list, but allow cross-posting when the
416              ;; message was really cross-posted.
417              (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
418              (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
419              ;;
420              ;; People...
421              (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
422           ;; Unmatched mail goes to the catch all group.
423           \"misc.misc\"))"
424   :group 'nnmail-split
425   ;; Sigh!
426   :type 'sexp)
427
428 (defcustom nnmail-split-abbrev-alist
429   '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
430     (mail . "mailer-daemon\\|postmaster\\|uucp")
431     (to . "to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
432     (from . "from\\|sender\\|resent-from")
433     (nato . "to\\|cc\\|resent-to\\|resent-cc")
434     (naany . "from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
435   "*Alist of abbreviations allowed in `nnmail-split-fancy'."
436   :group 'nnmail-split
437   :type '(repeat (cons :format "%v" symbol regexp)))
438
439 (defcustom nnmail-message-id-cache-length 1000
440   "*The approximate number of Message-IDs nnmail will keep in its cache.
441 If this variable is nil, no checking on duplicate messages will be
442 performed."
443   :group 'nnmail-duplicate
444   :type '(choice (const :tag "disable" nil)
445                  (integer :format "%v")))
446
447 (defcustom nnmail-message-id-cache-file "~/.nnmail-cache"
448   "*The file name of the nnmail Message-ID cache."
449   :group 'nnmail-duplicate
450   :group 'nnmail-files
451   :type 'file)
452
453 (defcustom nnmail-treat-duplicates 'warn
454   "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
455 Three values are valid: nil, which means that nnmail is not to keep a
456 Message-ID cache; `warn', which means that nnmail should insert extra
457 headers to warn the user about the duplication (this is the default);
458 and `delete', which means that nnmail will delete duplicated mails.
459
460 This variable can also be a function.  It will be called from a buffer
461 narrowed to the article in question with the Message-ID as a
462 parameter.  It should return nil, `warn' or `delete'."
463   :group 'nnmail-duplicate
464   :type '(choice (const :tag "off" nil)
465                  (const warn)
466                  (const delete)))
467
468 (defcustom nnmail-extra-headers '(To Newsgroups)
469   "*Extra headers to parse."
470   :version "21.1"
471   :group 'nnmail
472   :type '(repeat symbol))
473
474 (defcustom nnmail-split-header-length-limit 512
475   "Header lines longer than this limit are excluded from the split function."
476   :version "21.1"
477   :group 'nnmail
478   :type 'integer)
479
480 (defcustom nnmail-mail-splitting-charset nil
481   "Default charset to be used when splitting incoming mail."
482   :group 'nnmail
483   :type 'symbol)
484
485 ;;; Internal variables.
486
487 (defvar nnmail-article-buffer " *nnmail incoming*"
488   "The buffer used for splitting incoming mails.")
489
490 (defvar nnmail-split-history nil
491   "List of group/article elements that say where the previous split put messages.")
492
493 (defvar nnmail-split-fancy-syntax-table nil
494   "Syntax table used by `nnmail-split-fancy'.")
495 (unless (syntax-table-p nnmail-split-fancy-syntax-table)
496   (setq nnmail-split-fancy-syntax-table
497         (copy-syntax-table (standard-syntax-table)))
498   ;; support the %-hack
499   (modify-syntax-entry ?\% "." nnmail-split-fancy-syntax-table))
500
501 (defvar nnmail-prepare-save-mail-hook nil
502   "Hook called before saving mail.")
503
504 (defvar nnmail-split-tracing nil)
505 (defvar nnmail-split-trace nil)
506
507 \f
508
509 (defconst nnmail-version "nnmail 1.0"
510   "nnmail version.")
511
512 \f
513
514 (defun nnmail-request-post (&optional server)
515   (mail-send-and-exit nil))
516
517 (defvar nnmail-file-coding-system 'raw-text
518   "Coding system used in nnmail.")
519
520 (defvar nnmail-incoming-coding-system
521   nnheader-text-coding-system
522   "Coding system used in reading inbox")
523
524 (defvar nnmail-pathname-coding-system nil
525   "*Coding system for pathname.")
526
527 (defun nnmail-find-file (file)
528   "Insert FILE in server buffer safely."
529   (set-buffer nntp-server-buffer)
530   (delete-region (point-min) (point-max))
531   (let ((format-alist nil)
532         (after-insert-file-functions nil))
533     (condition-case ()
534         (let ((auto-mode-alist (nnheader-auto-mode-alist))
535               (file-name-coding-system nnmail-pathname-coding-system)
536               (pathname-coding-system nnmail-pathname-coding-system))
537           (insert-file-contents-as-coding-system
538            nnmail-file-coding-system 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          (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         (output-coding-system 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 ((nnheader-file-coding-system nnmail-incoming-coding-system))
958         (nnheader-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         (mime-decode-header-in-region (point-min) (point-max)
1004                                       nnmail-mail-splitting-charset)
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                       (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\).  So,
1278                 ;; start-of-value is the the point just before the
1279                 ;; beginning of the value, whereas after-header-name is
1280                 ;; 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 (concat "\\<" 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)
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 ((and (boundp 'group) group)
1471              (setq g group))
1472             ((and (boundp 'group-art-list) group-art-list
1473                   (listp group-art-list))
1474              (setq g (caar group-art-list)))
1475             ((and (boundp 'group-art) group-art (listp group-art))
1476              (setq g (caar group-art)))
1477             (t (setq g "")))
1478       (unless (gnus-buffer-live-p nnmail-cache-buffer)
1479         (nnmail-cache-open))
1480       (save-excursion
1481         (set-buffer nnmail-cache-buffer)
1482         (goto-char (point-max))
1483         (if (and g (not (string= "" g))
1484                  (gnus-methods-equal-p gnus-command-method
1485                                        (nnmail-cache-primary-mail-backend)))
1486             (insert id "\t" g "\n")
1487           (insert id "\n"))))))
1488
1489 (defun nnmail-cache-primary-mail-backend ()
1490   (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1491         (be nil)
1492         (res nil))
1493     (while (and (null res) be-list)
1494       (setq be (car be-list))
1495       (setq be-list (cdr be-list))
1496       (when (and (gnus-method-option-p be 'respool)
1497                  (eval (intern (format "%s-get-new-mail" (car be)))))
1498         (setq res be)))
1499     res))
1500
1501 ;; Fetch the group name corresponding to the message id stored in the
1502 ;; cache.
1503 (defun nnmail-cache-fetch-group (id)
1504   (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1505     (save-excursion
1506       (set-buffer nnmail-cache-buffer)
1507       (goto-char (point-max))
1508       (when (search-backward id nil t)
1509         (beginning-of-line)
1510         (skip-chars-forward "^\n\r\t")
1511         (unless (eolp)
1512           (forward-char 1)
1513           (buffer-substring (point)
1514                             (progn (end-of-line) (point))))))))
1515
1516 ;; Function for nnmail-split-fancy: look up all references in the
1517 ;; cache and if a match is found, return that group.
1518 (defun nnmail-split-fancy-with-parent ()
1519   "Split this message into the same group as its parent.
1520 This function can be used as an entry in `nnmail-split-fancy', for
1521 example like this: (: nnmail-split-fancy)
1522 For a message to be split, it looks for the parent message in the
1523 References or In-Reply-To header and then looks in the message id
1524 cache file (given by the variable `nnmail-message-id-cache-file') to
1525 see which group that message was put in.  This group is returned.
1526
1527 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1528   (let* ((refstr (or (message-fetch-field "references")
1529                      (message-fetch-field "in-reply-to")))
1530          (references nil)
1531          (res nil)
1532          (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1533                      (mapconcat
1534                       (lambda (x) (format "\\(%s\\)" x))
1535                       nnmail-split-fancy-with-parent-ignore-groups
1536                       "\\|")
1537                    nnmail-split-fancy-with-parent-ignore-groups)))
1538     (when refstr
1539       (setq references (nreverse (gnus-split-references refstr)))
1540       (unless (gnus-buffer-live-p nnmail-cache-buffer)
1541         (nnmail-cache-open))
1542       (mapcar (lambda (x)
1543                 (setq res (or (nnmail-cache-fetch-group x) res))
1544                 (when (or (string= "drafts" res)
1545                           (and regexp res (string-match regexp res)))
1546                   (setq res nil)))
1547               references)
1548       res)))
1549
1550 (defun nnmail-cache-id-exists-p (id)
1551   (when nnmail-treat-duplicates
1552     (save-excursion
1553       (set-buffer nnmail-cache-buffer)
1554       (goto-char (point-max))
1555       (search-backward id nil t))))
1556
1557 (defun nnmail-fetch-field (header)
1558   (save-excursion
1559     (save-restriction
1560       (message-narrow-to-head)
1561       (message-fetch-field header))))
1562
1563 (defun nnmail-check-duplication (message-id func artnum-func)
1564   (run-hooks 'nnmail-prepare-incoming-message-hook)
1565   ;; If this is a duplicate message, then we do not save it.
1566   (let* ((duplication (nnmail-cache-id-exists-p message-id))
1567          (case-fold-search t)
1568          (action (when duplication
1569                    (cond
1570                     ((memq nnmail-treat-duplicates '(warn delete))
1571                      nnmail-treat-duplicates)
1572                     ((nnheader-functionp nnmail-treat-duplicates)
1573                      (funcall nnmail-treat-duplicates message-id))
1574                     (t
1575                      nnmail-treat-duplicates))))
1576          group-art)
1577     ;; We insert a line that says what the mail source is.
1578     (let ((case-fold-search t))
1579       (goto-char (point-min))
1580       (re-search-forward "^message-id[ \t]*:" nil t)
1581       (beginning-of-line)
1582       (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1583
1584     ;; Let the backend save the article (or not).
1585     (cond
1586      ((not duplication)
1587       (funcall func (setq group-art
1588                           (nreverse (nnmail-article-group artnum-func))))
1589       (nnmail-cache-insert message-id))
1590      ((eq action 'delete)
1591       (setq group-art nil))
1592      ((eq action 'warn)
1593       ;; We insert a warning.
1594       (let ((case-fold-search t))
1595         (goto-char (point-min))
1596         (re-search-forward "^message-id[ \t]*:" nil t)
1597         (beginning-of-line)
1598         (insert
1599          "Gnus-Warning: This is a duplicate of message " message-id "\n")
1600         (funcall func (setq group-art
1601                             (nreverse (nnmail-article-group artnum-func))))))
1602      (t
1603       (funcall func (setq group-art
1604                           (nreverse (nnmail-article-group artnum-func))))))
1605     ;; Add the group-art list to the history list.
1606     (if group-art
1607         (push group-art nnmail-split-history)
1608       (delete-region (point-min) (point-max)))))
1609
1610 ;;; Get new mail.
1611
1612 (defvar nnmail-fetched-sources nil)
1613
1614 (defun nnmail-get-value (&rest args)
1615   (let ((sym (intern (apply 'format args))))
1616     (when (boundp sym)
1617       (symbol-value sym))))
1618
1619 (defun nnmail-get-new-mail (method exit-func temp
1620                                    &optional group spool-func)
1621   "Read new incoming mail."
1622   (let* ((sources (or mail-sources
1623                       (if (listp nnmail-spool-file) nnmail-spool-file
1624                         (list nnmail-spool-file))))
1625          fetching-sources
1626          (group-in group)
1627          (i 0)
1628          (new 0)
1629          (total 0)
1630          incoming incomings source)
1631     (when (and (nnmail-get-value "%s-get-new-mail" method)
1632                sources)
1633       (while (setq source (pop sources))
1634         ;; Be compatible with old values.
1635         (cond
1636          ((stringp source)
1637           (setq source
1638                 (cond
1639                  ((string-match "^po:" source)
1640                   (list 'pop :user (substring source (match-end 0))))
1641                  ((file-directory-p source)
1642                   (list 'directory :path source))
1643                  (t
1644                   (list 'file :path source)))))
1645          ((eq source 'procmail)
1646           (message "Invalid value for nnmail-spool-file: `procmail'")
1647           nil))
1648         ;; Hack to only fetch the contents of a single group's spool file.
1649         (when (and (eq (car source) 'directory)
1650                    (null nnmail-scan-directory-mail-source-once)
1651                    group)
1652           (mail-source-bind (directory source)
1653             (setq source (append source
1654                                  (list
1655                                   :predicate
1656                                   (gnus-byte-compile
1657                                    `(lambda (file)
1658                                       (string-equal
1659                                        ,(concat group suffix)
1660                                        (file-name-nondirectory file)))))))))
1661         (when nnmail-fetched-sources
1662           (if (member source nnmail-fetched-sources)
1663               (setq source nil)
1664             (push source nnmail-fetched-sources)
1665             (push source fetching-sources)))))
1666     (when fetching-sources
1667       ;; We first activate all the groups.
1668       (nnmail-activate method)
1669       ;; Allow the user to hook.
1670       (run-hooks 'nnmail-pre-get-new-mail-hook)
1671       ;; Open the message-id cache.
1672       (nnmail-cache-open)
1673       ;; The we go through all the existing mail source specification
1674       ;; and fetch the mail from each.
1675       (while (setq source (pop fetching-sources))
1676         (nnheader-message 4 "%s: Reading incoming mail from %s..."
1677                           method (car source))
1678         (when (setq new
1679                     (mail-source-fetch
1680                      source
1681                      (gnus-byte-compile
1682                       `(lambda (file orig-file)
1683                          (nnmail-split-incoming
1684                           file ',(intern (format "%s-save-mail" method))
1685                           ',spool-func
1686                           (if (equal file orig-file)
1687                               nil
1688                             (nnmail-get-split-group orig-file ',source))
1689                           ',(intern (format "%s-active-number" method)))))))
1690           (incf total new)
1691           (incf i)))
1692       ;; If we did indeed read any incoming spools, we save all info.
1693       (if (zerop total)
1694           (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1695                             method (car source))
1696         (nnmail-save-active
1697          (nnmail-get-value "%s-group-alist" method)
1698          (nnmail-get-value "%s-active-file" method))
1699         (when exit-func
1700           (funcall exit-func))
1701         (run-hooks 'nnmail-read-incoming-hook)
1702         (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1703                           total))
1704       ;; Close the message-id cache.
1705       (nnmail-cache-close)
1706       ;; Allow the user to hook.
1707       (run-hooks 'nnmail-post-get-new-mail-hook))))
1708
1709 (defun nnmail-expired-article-p (group time force &optional inhibit)
1710   "Say whether an article that is TIME old in GROUP should be expired."
1711   (if force
1712       t
1713     (let ((days (or (and nnmail-expiry-wait-function
1714                          (funcall nnmail-expiry-wait-function group))
1715                     nnmail-expiry-wait)))
1716       (cond ((or (eq days 'never)
1717                  (and (not force)
1718                       inhibit))
1719              ;; This isn't an expirable group.
1720              nil)
1721             ((eq days 'immediate)
1722              ;; We expire all articles on sight.
1723              t)
1724             ((equal time '(0 0))
1725              ;; This is an ange-ftp group, and we don't have any dates.
1726              nil)
1727             ((numberp days)
1728              (setq days (days-to-time days))
1729              ;; Compare the time with the current time.
1730              (ignore-errors (time-less-p days (time-since time))))))))
1731
1732 (defun nnmail-expiry-target-group (target group)
1733   ;; Do not invoke this from nntp-server-buffer!  At least nnfolder clears
1734   ;; that buffer if the nnfolder group isn't selected.
1735   (let (nnmail-cache-accepted-message-ids)
1736     ;; Don't enter Message-IDs into cache.
1737     ;; Let users hack it in TARGET function.
1738     (when (nnheader-functionp target)
1739       (setq target (funcall target group)))
1740     (unless (eq target 'delete)
1741       (gnus-request-accept-article target nil nil t))))
1742
1743 (defun nnmail-fancy-expiry-target (group)
1744   "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1745   (let* (header
1746          (case-fold-search nil)
1747          (from (or (message-fetch-field "from") ""))
1748          (to (or (message-fetch-field "to") ""))
1749          (date (date-to-time
1750                 (or (message-fetch-field "date") (current-time-string))))
1751          (target 'delete))
1752     (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1753       (setq header (car regexp-target-pair))
1754       (cond
1755        ;; If the header is to-from then match against the
1756        ;; To or From header
1757        ((and (equal header 'to-from)
1758              (or (string-match (cadr regexp-target-pair) from)
1759                  (and (string-match message-dont-reply-to-names from)
1760                       (string-match (cadr regexp-target-pair) to))))
1761         (setq target (format-time-string (caddr regexp-target-pair) date)))
1762        ((and (not (equal header 'to-from))
1763              (string-match (cadr regexp-target-pair)
1764                            (message-fetch-field header)))
1765         (setq target
1766               (format-time-string (caddr regexp-target-pair) date)))))))
1767
1768 (defun nnmail-check-syntax ()
1769   "Check (and modify) the syntax of the message in the current buffer."
1770   (save-restriction
1771     (message-narrow-to-head)
1772     (let ((case-fold-search t))
1773       (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1774         (insert "Message-ID: " (nnmail-message-id) "\n")))))
1775
1776 (defun nnmail-write-region (start end filename &optional append visit lockname)
1777   "Do a `write-region', and then set the file modes."
1778   (let ((file-name-coding-system nnmail-pathname-coding-system)
1779         (pathname-coding-system nnmail-pathname-coding-system))
1780     (write-region-as-coding-system
1781      nnmail-file-coding-system start end filename append visit lockname)
1782     (set-file-modes filename nnmail-default-file-modes)))
1783
1784 ;;;
1785 ;;; Status functions
1786 ;;;
1787
1788 (defun nnmail-replace-status (name value)
1789   "Make status NAME and VALUE part of the current status line."
1790   (save-restriction
1791     (message-narrow-to-head)
1792     (let ((status (nnmail-decode-status)))
1793       (setq status (delq (member name status) status))
1794       (when value
1795         (push (cons name value) status))
1796       (message-remove-header "status")
1797       (goto-char (point-max))
1798       (insert "Status: " (nnmail-encode-status status) "\n"))))
1799
1800 (defun nnmail-decode-status ()
1801   "Return a status-value alist from STATUS."
1802   (goto-char (point-min))
1803   (when (re-search-forward "^Status: " nil t)
1804     (let (name value status)
1805       (save-restriction
1806         ;; Narrow to the status.
1807         (narrow-to-region
1808          (point)
1809          (if (re-search-forward "^[^ \t]" nil t)
1810              (1- (point))
1811            (point-max)))
1812         ;; Go through all elements and add them to the list.
1813         (goto-char (point-min))
1814         (while (re-search-forward "[^ \t=]+" nil t)
1815           (setq name (match-string 0))
1816           (if (not (eq (char-after) ?=))
1817               ;; Implied "yes".
1818               (setq value "yes")
1819             (forward-char 1)
1820             (if (not (eq (char-after) ?\"))
1821                 (if (not (looking-at "[^ \t]"))
1822                     ;; Implied "no".
1823                     (setq value "no")
1824                   ;; Unquoted value.
1825                   (setq value (match-string 0))
1826                   (goto-char (match-end 0)))
1827               ;; Quoted value.
1828               (setq value (read (current-buffer)))))
1829           (push (cons name value) status)))
1830       status)))
1831
1832 (defun nnmail-encode-status (status)
1833   "Return a status string from STATUS."
1834   (mapconcat
1835    (lambda (elem)
1836      (concat
1837       (car elem) "="
1838       (if (string-match "[ \t]" (cdr elem))
1839           (prin1-to-string (cdr elem))
1840         (cdr elem))))
1841    status " "))
1842
1843 (defun nnmail-split-history ()
1844   "Generate an overview of where the last mail split put articles."
1845   (interactive)
1846   (unless nnmail-split-history
1847     (error "No current split history"))
1848   (with-output-to-temp-buffer "*nnmail split history*"
1849     (with-current-buffer standard-output
1850       (fundamental-mode))               ; for Emacs 20.4+
1851     (let ((history nnmail-split-history)
1852           elem)
1853       (while (setq elem (pop history))
1854         (princ (mapconcat (lambda (ga)
1855                             (concat (car ga) ":" (int-to-string (cdr ga))))
1856                           elem
1857                           ", "))
1858         (princ "\n")))))
1859
1860 (defun nnmail-purge-split-history (group)
1861   "Remove all instances of GROUP from `nnmail-split-history'."
1862   (let ((history nnmail-split-history))
1863     (while history
1864       (setcar history (gnus-delete-if (lambda (e) (string= (car e) group))
1865                                       (car history)))
1866       (pop history))
1867     (setq nnmail-split-history (delq nil nnmail-split-history))))
1868
1869 (defun nnmail-new-mail-p (group)
1870   "Say whether GROUP has new mail."
1871   (let ((his nnmail-split-history)
1872         found)
1873     (while his
1874       (when (assoc group (pop his))
1875         (setq found t
1876               his nil)))
1877     found))
1878
1879 (defun nnmail-new-mail-numbers (group)
1880   "Say how many articles has been incorporated to GROUP."
1881   (let ((his (apply 'append nnmail-split-history))
1882         numbers)
1883     (while his
1884       (when (string= group (caar his))
1885         (push (cdar his) numbers))
1886       (setq his (cdr his)))
1887     numbers))
1888
1889 (defun nnmail-within-headers-p ()
1890   "Check to see if point is within the headers of a unix mail message.
1891 Doesn't change point."
1892   (let ((pos (point)))
1893     (save-excursion
1894       (and (nnmail-search-unix-mail-delim-backward)
1895            (not (search-forward "\n\n" pos t))))))
1896
1897 (run-hooks 'nnmail-load-hook)
1898
1899 (provide 'nnmail)
1900
1901 ;;; nnmail.el ends here