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