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