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