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