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