c602effb31a38c4ae0d0dccaddf0b95419297e4e
[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           ;; Fall back on "bogus" if all else fails.
1110           (unless group-art
1111             (setq group-art (list (cons "bogus" (funcall func "bogus"))))))
1112         ;; Produce a trace if non-empty.
1113         (when (and trace nnmail-split-trace)
1114           (let ((restore (current-buffer)))
1115             (nnheader-set-temp-buffer "*Split Trace*")
1116             (gnus-add-buffer)
1117             (dolist (trace (nreverse nnmail-split-trace))
1118               (prin1 trace (current-buffer))
1119               (insert "\n"))
1120             (goto-char (point-min))
1121             (gnus-configure-windows 'split-trace)
1122             (set-buffer restore)))
1123         (widen)
1124         ;; See whether the split methods returned `junk'.
1125         (if (equal group-art '(junk))
1126             nil
1127           ;; The article may be "cross-posted" to `junk'.  What
1128           ;; to do?  Just remove the `junk' spec.  Don't really
1129           ;; see anything else to do...
1130           (let (elem)
1131             (while (setq elem (car (memq 'junk group-art)))
1132               (setq group-art (delq elem group-art)))
1133             (nreverse group-art)))))))
1134
1135 (defun nnmail-insert-lines ()
1136   "Insert how many lines there are in the body of the mail.
1137 Return the number of characters in the body."
1138   (let (lines chars)
1139     (save-excursion
1140       (goto-char (point-min))
1141       (unless (search-forward "\n\n" nil t)
1142         (goto-char (point-max))
1143         (insert "\n"))
1144       (setq chars (- (point-max) (point)))
1145       (setq lines (count-lines (point) (point-max)))
1146       (forward-char -1)
1147       (save-excursion
1148         (when (re-search-backward "^Lines: " nil t)
1149           (delete-region (point) (progn (forward-line 1) (point)))))
1150       (beginning-of-line)
1151       (insert (format "Lines: %d\n" (max lines 0)))
1152       chars)))
1153
1154 (defun nnmail-insert-xref (group-alist)
1155   "Insert an Xref line based on the (group . article) alist."
1156   (save-excursion
1157     (goto-char (point-min))
1158     (unless (search-forward "\n\n" nil t)
1159       (goto-char (point-max))
1160       (insert "\n"))
1161     (forward-char -1)
1162     (when (re-search-backward "^Xref: " nil t)
1163       (delete-region (match-beginning 0)
1164                      (progn (forward-line 1) (point))))
1165     (insert (format "Xref: %s" (system-name)))
1166     (while group-alist
1167       (insert (format " %s:%d"
1168                       (mm-encode-coding-string
1169                        (caar group-alist)
1170                        nnmail-pathname-coding-system)
1171                       (cdar group-alist)))
1172       (setq group-alist (cdr group-alist)))
1173     (insert "\n")))
1174
1175 ;;; Message washing functions
1176
1177 (defun nnmail-remove-leading-whitespace ()
1178   "Remove excessive whitespace from all headers."
1179   (goto-char (point-min))
1180   (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1181     (replace-match "\\1" t)))
1182
1183 (defun nnmail-remove-list-identifiers ()
1184   "Remove list identifiers from Subject headers."
1185   (let ((regexp
1186          (if (consp nnmail-list-identifiers)
1187              (mapconcat 'identity nnmail-list-identifiers " *\\|")
1188            nnmail-list-identifiers)))
1189     (when regexp
1190       (goto-char (point-min))
1191       (while (re-search-forward
1192               (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1193               nil t)
1194         (delete-region (match-beginning 2) (match-end 0))
1195         (beginning-of-line))
1196       (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1197                                nil t)
1198         (delete-region (match-beginning 1) (match-end 1))
1199         (beginning-of-line)))))
1200
1201 (defun nnmail-remove-tabs ()
1202   "Translate TAB characters into SPACE characters."
1203   (subst-char-in-region (point-min) (point-max) ?\t ?  t))
1204
1205 (defun nnmail-fix-eudora-headers ()
1206   "Eudora has a broken References line, but an OK In-Reply-To."
1207   (goto-char (point-min))
1208   (when (re-search-forward "^X-Mailer:.*Eudora" nil t)
1209     (goto-char (point-min))
1210     (when (re-search-forward "^References:" nil t)
1211       (beginning-of-line)
1212       (insert "X-Gnus-Broken-Eudora-"))
1213     (goto-char (point-min))
1214     (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1215       (replace-match "\\1" t))))
1216
1217 (custom-add-option 'nnmail-prepare-incoming-header-hook
1218                    'nnmail-fix-eudora-headers)
1219
1220 ;;; Utility functions
1221
1222 (defun nnmail-do-request-post (accept-func &optional server)
1223   "Utility function to directly post a message to an nnmail-derived group.
1224 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1225 to actually put the message in the right group."
1226   (let ((success t))
1227     (dolist (mbx (message-unquote-tokens
1228                   (message-tokenize-header
1229                    (message-fetch-field "Newsgroups") ", ")) success)
1230       (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1231         (or (gnus-active to-newsgroup)
1232             (gnus-activate-group to-newsgroup)
1233             (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
1234                                        to-newsgroup))
1235                 (or (and (gnus-request-create-group
1236                           to-newsgroup gnus-command-method)
1237                          (gnus-activate-group to-newsgroup nil nil
1238                                               gnus-command-method))
1239                     (error "Couldn't create group %s" to-newsgroup)))
1240             (error "No such group: %s" to-newsgroup))
1241         (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1242           (setq success nil))))))
1243
1244 (defun nnmail-split-fancy ()
1245   "Fancy splitting method.
1246 See the documentation for the variable `nnmail-split-fancy' for details."
1247   (let ((syntab (syntax-table)))
1248     (unwind-protect
1249         (progn
1250           (set-syntax-table nnmail-split-fancy-syntax-table)
1251           (nnmail-split-it nnmail-split-fancy))
1252       (set-syntax-table syntab))))
1253
1254 (defvar nnmail-split-cache nil)
1255 ;; Alist of split expressions their equivalent regexps.
1256
1257 (defun nnmail-split-it (split)
1258   ;; Return a list of groups matching SPLIT.
1259   (let (cached-pair)
1260     (cond
1261      ;; nil split
1262      ((null split)
1263       nil)
1264
1265      ;; A group name.  Do the \& and \N subs into the string.
1266      ((stringp split)
1267       (when nnmail-split-tracing
1268         (push split nnmail-split-trace))
1269       (list (nnmail-expand-newtext split)))
1270
1271      ;; Junk the message.
1272      ((eq split 'junk)
1273       (when nnmail-split-tracing
1274         (push "junk" nnmail-split-trace))
1275       (list 'junk))
1276
1277      ;; Builtin & operation.
1278      ((eq (car split) '&)
1279       (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1280
1281      ;; Builtin | operation.
1282      ((eq (car split) '|)
1283       (let (done)
1284         (while (and (not done) (cdr split))
1285           (setq split (cdr split)
1286                 done (nnmail-split-it (car split))))
1287         done))
1288
1289      ;; Builtin : operation.
1290      ((eq (car split) ':)
1291       (when nnmail-split-tracing
1292         (push split nnmail-split-trace))
1293       (nnmail-split-it (save-excursion (eval (cdr split)))))
1294
1295      ;; Builtin ! operation.
1296      ((eq (car split) '!)
1297       (funcall (cadr split) (nnmail-split-it (caddr split))))
1298
1299      ;; Check the cache for the regexp for this split.
1300      ((setq cached-pair (assq split nnmail-split-cache))
1301       (let (split-result
1302             (end-point (point-max))
1303             (value (nth 1 split)))
1304         (if (symbolp value)
1305             (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1306         (while (and (goto-char end-point)
1307                     (re-search-backward (cdr cached-pair) nil t))
1308           (when nnmail-split-tracing
1309             (push split nnmail-split-trace))
1310           (let ((split-rest (cddr split))
1311                 (end (match-end 0))
1312                 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1313                 ;; So, start-of-value is the point just before the
1314                 ;; beginning of the value, whereas after-header-name
1315                 ;; is the point just after the field name.
1316                 (start-of-value (match-end 1))
1317                 (after-header-name (match-end 2)))
1318             ;; Start the next search just before the beginning of the
1319             ;; VALUE match.
1320             (setq end-point (1- start-of-value))
1321             ;; Handle - RESTRICTs
1322             (while (eq (car split-rest) '-)
1323               ;; RESTRICT must start after-header-name and
1324               ;; end after start-of-value, so that, for
1325               ;; (any "foo" - "x-foo" "foo.list")
1326               ;; we do not exclude foo.list just because
1327               ;; the header is: ``To: x-foo, foo''
1328               (goto-char end)
1329               (if (and (re-search-backward (cadr split-rest)
1330                                            after-header-name t)
1331                        (> (match-end 0) start-of-value))
1332                   (setq split-rest nil)
1333                 (setq split-rest (cddr split-rest))))
1334             (when split-rest
1335               (goto-char end)
1336               (let ((value (nth 1 split)))
1337                 (if (symbolp value)
1338                     (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1339                 ;; Someone might want to do a \N sub on this match, so get the
1340                 ;; correct match positions.
1341                 (re-search-backward value start-of-value))
1342               (dolist (sp (nnmail-split-it (car split-rest)))
1343                 (unless (member sp split-result)
1344                   (push sp split-result))))))
1345         split-result))
1346
1347      ;; Not in cache, compute a regexp for the field/value pair.
1348      (t
1349       (let* ((field (nth 0 split))
1350              (value (nth 1 split))
1351              partial-front
1352              partial-rear
1353              regexp)
1354         (if (symbolp value)
1355             (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1356         (if (and (>= (length value) 2)
1357                  (string= ".*" (substring value 0 2)))
1358             (setq value (substring value 2)
1359                   partial-front ""))
1360         ;; Same trick for the rear of the regexp
1361         (if (and (>= (length value) 2)
1362                  (string= ".*" (substring value -2)))
1363             (setq value (substring value 0 -2)
1364                   partial-rear ""))
1365         (when nnmail-split-fancy-match-partial-words
1366           (setq partial-front ""
1367                 partial-rear ""))
1368         (setq regexp (concat "^\\(\\("
1369                              (if (symbolp field)
1370                                  (cdr (assq field nnmail-split-abbrev-alist))
1371                                field)
1372                              "\\):.*\\)"
1373                              (or partial-front "\\<")
1374                              "\\("
1375                              value
1376                              "\\)"
1377                              (or partial-rear "\\>")))
1378         (push (cons split regexp) nnmail-split-cache)
1379         ;; Now that it's in the cache, just call nnmail-split-it again
1380     ;; on the same split, which will find it immediately in the cache.
1381         (nnmail-split-it split))))))
1382
1383 (defun nnmail-expand-newtext (newtext)
1384   (let ((len (length newtext))
1385         (pos 0)
1386         c expanded beg N did-expand)
1387     (while (< pos len)
1388       (setq beg pos)
1389       (while (and (< pos len)
1390                   (not (= (aref newtext pos) ?\\)))
1391         (setq pos (1+ pos)))
1392       (unless (= beg pos)
1393         (push (substring newtext beg pos) expanded))
1394       (when (< pos len)
1395         ;; We hit a \; expand it.
1396         (setq did-expand t
1397               pos (1+ pos)
1398               c (aref newtext pos))
1399         (if (not (or (= c ?\&)
1400                      (and (>= c ?1)
1401                           (<= c ?9))))
1402             ;; \ followed by some character we don't expand.
1403             (push (char-to-string c) expanded)
1404           ;; \& or \N
1405           (if (= c ?\&)
1406               (setq N 0)
1407             (setq N (- c ?0)))
1408           (when (match-beginning N)
1409             (push (buffer-substring (match-beginning N) (match-end N))
1410                   expanded))))
1411       (setq pos (1+ pos)))
1412     (if did-expand
1413         (apply 'concat (nreverse expanded))
1414       newtext)))
1415
1416 ;; Activate a backend only if it isn't already activated.
1417 ;; If FORCE, re-read the active file even if the backend is
1418 ;; already activated.
1419 (defun nnmail-activate (backend &optional force)
1420   (nnheader-init-server-buffer)
1421   (let (file timestamp file-time)
1422     (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1423             force
1424             (and (setq file (ignore-errors
1425                               (symbol-value (intern (format "%s-active-file"
1426                                                             backend)))))
1427                  (setq file-time (nth 5 (file-attributes file)))
1428                  (or (not
1429                       (setq timestamp
1430                             (condition-case ()
1431                                 (symbol-value (intern
1432                                                (format "%s-active-timestamp"
1433                                                        backend)))
1434                               (error 'none))))
1435                      (not (consp timestamp))
1436                      (equal timestamp '(0 0))
1437                      (> (nth 0 file-time) (nth 0 timestamp))
1438                      (and (= (nth 0 file-time) (nth 0 timestamp))
1439                           (> (nth 1 file-time) (nth 1 timestamp))))))
1440         (save-excursion
1441           (or (eq timestamp 'none)
1442               (set (intern (format "%s-active-timestamp" backend))
1443                    file-time))
1444           (funcall (intern (format "%s-request-list" backend)))))
1445     t))
1446
1447 (defun nnmail-message-id ()
1448   (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1449
1450 ;;;
1451 ;;; nnmail duplicate handling
1452 ;;;
1453
1454 (defvar nnmail-cache-buffer nil)
1455
1456 (defun nnmail-cache-open ()
1457   (if (or (not nnmail-treat-duplicates)
1458           (and nnmail-cache-buffer
1459                (buffer-name nnmail-cache-buffer)))
1460       ()                                ; The buffer is open.
1461     (save-excursion
1462       (set-buffer
1463        (setq nnmail-cache-buffer
1464              (get-buffer-create " *nnmail message-id cache*")))
1465       (gnus-add-buffer)
1466       (when (file-exists-p nnmail-message-id-cache-file)
1467         (nnheader-insert-file-contents nnmail-message-id-cache-file))
1468       (set-buffer-modified-p nil)
1469       (current-buffer))))
1470
1471 (defun nnmail-cache-close ()
1472   (when (and nnmail-cache-buffer
1473              nnmail-treat-duplicates
1474              (buffer-name nnmail-cache-buffer)
1475              (buffer-modified-p nnmail-cache-buffer))
1476     (save-excursion
1477       (set-buffer nnmail-cache-buffer)
1478       ;; Weed out the excess number of Message-IDs.
1479       (goto-char (point-max))
1480       (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1481         (progn
1482           (beginning-of-line)
1483           (delete-region (point-min) (point))))
1484       ;; Save the buffer.
1485       (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1486           (make-directory (file-name-directory nnmail-message-id-cache-file)
1487                           t))
1488       (nnmail-write-region (point-min) (point-max)
1489                            nnmail-message-id-cache-file nil 'silent)
1490       (set-buffer-modified-p nil)
1491       (setq nnmail-cache-buffer nil)
1492       (gnus-kill-buffer (current-buffer)))))
1493
1494 ;; Compiler directives.
1495 (defvar group)
1496 (defvar group-art-list)
1497 (defvar group-art)
1498 (defun nnmail-cache-insert (id grp &optional subject sender)
1499   (when (stringp id)
1500     ;; this will handle cases like `B r' where the group is nil
1501     (let ((grp (or grp gnus-newsgroup-name "UNKNOWN")))
1502       (run-hook-with-args 'nnmail-spool-hook 
1503                           id grp subject sender))
1504     (when nnmail-treat-duplicates
1505       ;; Store some information about the group this message is written
1506       ;; to.  This is passed in as the grp argument -- all locations this
1507       ;; has been called from have been checked and the group is available.
1508       ;; The only ambiguous case is nnmail-check-duplication which will only
1509       ;; pass the first (of possibly >1) group which matches. -Josh
1510       (unless (gnus-buffer-live-p nnmail-cache-buffer)
1511         (nnmail-cache-open))
1512       (save-excursion
1513         (set-buffer nnmail-cache-buffer)
1514         (goto-char (point-max))
1515         (if (and grp (not (string= "" grp))
1516                  (gnus-methods-equal-p gnus-command-method
1517                                        (nnmail-cache-primary-mail-backend)))
1518             (let ((regexp (if (consp nnmail-cache-ignore-groups)
1519                               (mapconcat 'identity nnmail-cache-ignore-groups
1520                                          "\\|")
1521                             nnmail-cache-ignore-groups)))
1522               (unless (and regexp (string-match regexp grp))
1523                 (insert id "\t" grp "\n")))
1524           (insert id "\n"))))))
1525   
1526 (defun nnmail-cache-primary-mail-backend ()
1527   (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1528         (be nil)
1529         (res nil)
1530         (get-new-mail nil))
1531     (while (and (null res) be-list)
1532       (setq be (car be-list))
1533       (setq be-list (cdr be-list))
1534       (when (and (gnus-method-option-p be 'respool)
1535                  (setq get-new-mail
1536                        (intern (format "%s-get-new-mail" (car be))))
1537                  (boundp get-new-mail)
1538                  (symbol-value get-new-mail))
1539         (setq res be)))
1540     res))
1541
1542 ;; Fetch the group name corresponding to the message id stored in the
1543 ;; cache.
1544 (defun nnmail-cache-fetch-group (id)
1545   (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1546     (save-excursion
1547       (set-buffer nnmail-cache-buffer)
1548       (goto-char (point-max))
1549       (when (search-backward id nil t)
1550         (beginning-of-line)
1551         (skip-chars-forward "^\n\r\t")
1552         (unless (looking-at "[\r\n]")
1553           (forward-char 1)
1554           (buffer-substring (point) (gnus-point-at-eol)))))))
1555
1556 ;; Function for nnmail-split-fancy: look up all references in the
1557 ;; cache and if a match is found, return that group.
1558 (defun nnmail-split-fancy-with-parent ()
1559   "Split this message into the same group as its parent.
1560 This function can be used as an entry in `nnmail-split-fancy', for
1561 example like this: (: nnmail-split-fancy-with-parent)
1562 For a message to be split, it looks for the parent message in the
1563 References or In-Reply-To header and then looks in the message id
1564 cache file (given by the variable `nnmail-message-id-cache-file') to
1565 see which group that message was put in.  This group is returned.
1566
1567 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1568   (let* ((refstr (or (message-fetch-field "references")
1569                      (message-fetch-field "in-reply-to")))
1570          (references nil)
1571          (res nil)
1572          (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1573                      (mapconcat
1574                       (lambda (x) (format "\\(%s\\)" x))
1575                       nnmail-split-fancy-with-parent-ignore-groups
1576                       "\\|")
1577                    nnmail-split-fancy-with-parent-ignore-groups)))
1578     (when refstr
1579       (setq references (nreverse (gnus-split-references refstr)))
1580       (unless (gnus-buffer-live-p nnmail-cache-buffer)
1581         (nnmail-cache-open))
1582       (mapcar (lambda (x)
1583                 (setq res (or (nnmail-cache-fetch-group x) res))
1584                 (when (or (member res '("delayed" "drafts" "queue"))
1585                           (and regexp res (string-match regexp res)))
1586                   (setq res nil)))
1587               references)
1588       res)))
1589
1590 (defun nnmail-cache-id-exists-p (id)
1591   (when nnmail-treat-duplicates
1592     (save-excursion
1593       (set-buffer nnmail-cache-buffer)
1594       (goto-char (point-max))
1595       (search-backward id nil t))))
1596
1597 (defun nnmail-fetch-field (header)
1598   (save-excursion
1599     (save-restriction
1600       (message-narrow-to-head)
1601       (message-fetch-field header))))
1602
1603 (defun nnmail-check-duplication (message-id func artnum-func)
1604   (run-hooks 'nnmail-prepare-incoming-message-hook)
1605   ;; If this is a duplicate message, then we do not save it.
1606   (let* ((duplication (nnmail-cache-id-exists-p message-id))
1607          (case-fold-search t)
1608          (action (when duplication
1609                    (cond
1610                     ((memq nnmail-treat-duplicates '(warn delete))
1611                      nnmail-treat-duplicates)
1612                     ((functionp nnmail-treat-duplicates)
1613                      (funcall nnmail-treat-duplicates message-id))
1614                     (t
1615                      nnmail-treat-duplicates))))
1616          group-art)
1617     ;; We insert a line that says what the mail source is.
1618     (let ((case-fold-search t))
1619       (goto-char (point-min))
1620       (re-search-forward "^message-id[ \t]*:" nil t)
1621       (beginning-of-line)
1622       (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1623
1624     ;; Let the backend save the article (or not).
1625     (cond
1626      ((not duplication)
1627       (funcall func (setq group-art
1628                           (nreverse (nnmail-article-group artnum-func))))
1629       (nnmail-cache-insert message-id (caar group-art)))
1630      ((eq action 'delete)
1631       (setq group-art nil))
1632      ((eq action 'warn)
1633       ;; We insert a warning.
1634       (let ((case-fold-search t))
1635         (goto-char (point-min))
1636         (re-search-forward "^message-id[ \t]*:" nil t)
1637         (beginning-of-line)
1638         (insert
1639          "Gnus-Warning: This is a duplicate of message " message-id "\n")
1640         (funcall func (setq group-art
1641                             (nreverse (nnmail-article-group artnum-func))))))
1642      (t
1643       (funcall func (setq group-art
1644                           (nreverse (nnmail-article-group artnum-func))))))
1645     ;; Add the group-art list to the history list.
1646     (if group-art
1647         (push group-art nnmail-split-history)
1648       (delete-region (point-min) (point-max)))))
1649
1650 ;;; Get new mail.
1651
1652 (defvar nnmail-fetched-sources nil)
1653
1654 (defun nnmail-get-value (&rest args)
1655   (let ((sym (intern (apply 'format args))))
1656     (when (boundp sym)
1657       (symbol-value sym))))
1658
1659 (defun nnmail-get-new-mail (method exit-func temp
1660                                    &optional group spool-func)
1661   "Read new incoming mail."
1662   (let* ((sources (or mail-sources
1663                       (if (listp nnmail-spool-file) nnmail-spool-file
1664                         (list nnmail-spool-file))))
1665          fetching-sources
1666          (group-in group)
1667          (i 0)
1668          (new 0)
1669          (total 0)
1670          incoming incomings source)
1671     (when (and (nnmail-get-value "%s-get-new-mail" method)
1672                sources)
1673       (while (setq source (pop sources))
1674         ;; Be compatible with old values.
1675         (cond
1676          ((stringp source)
1677           (setq source
1678                 (cond
1679                  ((string-match "^po:" source)
1680                   (list 'pop :user (substring source (match-end 0))))
1681                  ((file-directory-p source)
1682                   (list 'directory :path source))
1683                  (t
1684                   (list 'file :path source)))))
1685          ((eq source 'procmail)
1686           (message "Invalid value for nnmail-spool-file: `procmail'")
1687           nil))
1688         ;; Hack to only fetch the contents of a single group's spool file.
1689         (when (and (eq (car source) 'directory)
1690                    (null nnmail-scan-directory-mail-source-once)
1691                    group)
1692           (mail-source-bind (directory source)
1693             (setq source (append source
1694                                  (list
1695                                   :predicate
1696                                   (gnus-byte-compile
1697                                    `(lambda (file)
1698                                       (string-equal
1699                                        ,(concat group suffix)
1700                                        (file-name-nondirectory file)))))))))
1701         (when nnmail-fetched-sources
1702           (if (member source nnmail-fetched-sources)
1703               (setq source nil)
1704             (push source nnmail-fetched-sources)
1705             (push source fetching-sources)))))
1706     (when fetching-sources
1707       ;; We first activate all the groups.
1708       (nnmail-activate method)
1709       ;; Allow the user to hook.
1710       (run-hooks 'nnmail-pre-get-new-mail-hook)
1711       ;; Open the message-id cache.
1712       (nnmail-cache-open)
1713       ;; The we go through all the existing mail source specification
1714       ;; and fetch the mail from each.
1715       (while (setq source (pop fetching-sources))
1716         (nnheader-message 4 "%s: Reading incoming mail from %s..."
1717                           method (car source))
1718         (when (setq new
1719                     (mail-source-fetch
1720                      source
1721                      (gnus-byte-compile
1722                       `(lambda (file orig-file)
1723                          (nnmail-split-incoming
1724                           file ',(intern (format "%s-save-mail" method))
1725                           ',spool-func
1726                           (if (equal file orig-file)
1727                               nil
1728                             (nnmail-get-split-group orig-file ',source))
1729                           ',(intern (format "%s-active-number" method)))))))
1730           (incf total new)
1731           (incf i)))
1732       ;; If we did indeed read any incoming spools, we save all info.
1733       (if (zerop total)
1734           (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1735                             method (car source))
1736         (nnmail-save-active
1737          (nnmail-get-value "%s-group-alist" method)
1738          (nnmail-get-value "%s-active-file" method))
1739         (when exit-func
1740           (funcall exit-func))
1741         (run-hooks 'nnmail-read-incoming-hook)
1742         (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1743                           total))
1744       ;; Close the message-id cache.
1745       (nnmail-cache-close)
1746       ;; Allow the user to hook.
1747       (run-hooks 'nnmail-post-get-new-mail-hook))))
1748
1749 (defun nnmail-expired-article-p (group time force &optional inhibit)
1750   "Say whether an article that is TIME old in GROUP should be expired."
1751   (if force
1752       t
1753     (let ((days (or (and nnmail-expiry-wait-function
1754                          (funcall nnmail-expiry-wait-function group))
1755                     nnmail-expiry-wait)))
1756       (cond ((or (eq days 'never)
1757                  (and (not force)
1758                       inhibit))
1759              ;; This isn't an expirable group.
1760              nil)
1761             ((eq days 'immediate)
1762              ;; We expire all articles on sight.
1763              t)
1764             ((equal time '(0 0))
1765             ;; This is an ange-ftp group, and we don't have any dates.
1766              nil)
1767             ((numberp days)
1768              (setq days (days-to-time days))
1769              ;; Compare the time with the current time.
1770              (ignore-errors (time-less-p days (time-since time))))))))
1771
1772 (defun nnmail-expiry-target-group (target group)
1773   ;; Do not invoke this from nntp-server-buffer!  At least nnfolder clears
1774   ;; that buffer if the nnfolder group isn't selected.
1775   (let (nnmail-cache-accepted-message-ids)
1776     ;; Don't enter Message-IDs into cache.
1777     ;; Let users hack it in TARGET function.
1778     (when (functionp target)
1779       (setq target (funcall target group)))
1780     (unless (eq target 'delete)
1781       (when (or (gnus-request-group target)
1782                 (gnus-request-create-group target))
1783         (let ((group-art (gnus-request-accept-article target nil nil t)))
1784           (when (consp group-art)
1785             (gnus-group-mark-article-read target (cdr group-art))))))))
1786
1787 (defun nnmail-fancy-expiry-target (group)
1788   "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1789   (let* (header
1790          (case-fold-search nil)
1791          (from (or (message-fetch-field "from") ""))
1792          (to (or (message-fetch-field "to") ""))
1793          (date (date-to-time
1794                 (or (message-fetch-field "date") (current-time-string))))
1795          (target 'delete))
1796     (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1797       (setq header (car regexp-target-pair))
1798       (cond
1799        ;; If the header is to-from then match against the
1800        ;; To or From header
1801        ((and (equal header 'to-from)
1802              (or (string-match (cadr regexp-target-pair) from)
1803                  (and (string-match message-dont-reply-to-names from)
1804                       (string-match (cadr regexp-target-pair) to))))
1805         (setq target (format-time-string (caddr regexp-target-pair) date)))
1806        ((and (not (equal header 'to-from))
1807              (string-match (cadr regexp-target-pair)
1808                            (or
1809                             (message-fetch-field header)
1810                             "")))
1811         (setq target
1812               (format-time-string (caddr regexp-target-pair) date)))))))
1813
1814 (defun nnmail-check-syntax ()
1815   "Check (and modify) the syntax of the message in the current buffer."
1816   (save-restriction
1817     (message-narrow-to-head)
1818     (let ((case-fold-search t))
1819       (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1820         (insert "Message-ID: " (nnmail-message-id) "\n")))))
1821
1822 (defun nnmail-write-region (start end filename &optional append visit lockname)
1823   "Do a `write-region', and then set the file modes."
1824   (let ((coding-system-for-write nnmail-file-coding-system)
1825         (file-name-coding-system nnmail-pathname-coding-system))
1826     (write-region start end filename append visit lockname)
1827     (set-file-modes filename nnmail-default-file-modes)))
1828
1829 ;;;
1830 ;;; Status functions
1831 ;;;
1832
1833 (defun nnmail-replace-status (name value)
1834   "Make status NAME and VALUE part of the current status line."
1835   (save-restriction
1836     (message-narrow-to-head)
1837     (let ((status (nnmail-decode-status)))
1838       (setq status (delq (member name status) status))
1839       (when value
1840         (push (cons name value) status))
1841       (message-remove-header "status")
1842       (goto-char (point-max))
1843       (insert "Status: " (nnmail-encode-status status) "\n"))))
1844
1845 (defun nnmail-decode-status ()
1846   "Return a status-value alist from STATUS."
1847   (goto-char (point-min))
1848   (when (re-search-forward "^Status: " nil t)
1849     (let (name value status)
1850       (save-restriction
1851         ;; Narrow to the status.
1852         (narrow-to-region
1853          (point)
1854          (if (re-search-forward "^[^ \t]" nil t)
1855              (1- (point))
1856            (point-max)))
1857         ;; Go through all elements and add them to the list.
1858         (goto-char (point-min))
1859         (while (re-search-forward "[^ \t=]+" nil t)
1860           (setq name (match-string 0))
1861           (if (not (eq (char-after) ?=))
1862               ;; Implied "yes".
1863               (setq value "yes")
1864             (forward-char 1)
1865             (if (not (eq (char-after) ?\"))
1866                 (if (not (looking-at "[^ \t]"))
1867                     ;; Implied "no".
1868                     (setq value "no")
1869                   ;; Unquoted value.
1870                   (setq value (match-string 0))
1871                   (goto-char (match-end 0)))
1872               ;; Quoted value.
1873               (setq value (read (current-buffer)))))
1874           (push (cons name value) status)))
1875       status)))
1876
1877 (defun nnmail-encode-status (status)
1878   "Return a status string from STATUS."
1879   (mapconcat
1880    (lambda (elem)
1881      (concat
1882       (car elem) "="
1883       (if (string-match "[ \t]" (cdr elem))
1884           (prin1-to-string (cdr elem))
1885         (cdr elem))))
1886    status " "))
1887
1888 (defun nnmail-split-history ()
1889   "Generate an overview of where the last mail split put articles."
1890   (interactive)
1891   (unless nnmail-split-history
1892     (error "No current split history"))
1893   (with-output-to-temp-buffer "*nnmail split history*"
1894     (with-current-buffer standard-output
1895       (fundamental-mode))               ; for Emacs 20.4+
1896     (let ((history nnmail-split-history)
1897           elem)
1898       (while (setq elem (pop history))
1899         (princ (mapconcat (lambda (ga)
1900                             (concat (car ga) ":" (int-to-string (cdr ga))))
1901                           elem
1902                           ", "))
1903         (princ "\n")))))
1904
1905 (defun nnmail-purge-split-history (group)
1906   "Remove all instances of GROUP from `nnmail-split-history'."
1907   (let ((history nnmail-split-history))
1908     (while history
1909       (setcar history (gnus-remove-if (lambda (e) (string= (car e) group))
1910                                       (car history)))
1911       (pop history))
1912     (setq nnmail-split-history (delq nil nnmail-split-history))))
1913
1914 (defun nnmail-new-mail-p (group)
1915   "Say whether GROUP has new mail."
1916   (let ((his nnmail-split-history)
1917         found)
1918     (while his
1919       (when (assoc group (pop his))
1920         (setq found t
1921               his nil)))
1922     found))
1923
1924 (defun nnmail-within-headers-p ()
1925   "Check to see if point is within the headers of a unix mail message.
1926 Doesn't change point."
1927   (let ((pos (point)))
1928     (save-excursion
1929       (and (nnmail-search-unix-mail-delim-backward)
1930            (not (search-forward "\n\n" pos t))))))
1931
1932 (run-hooks 'nnmail-load-hook)
1933
1934 (provide 'nnmail)
1935
1936 ;;; nnmail.el ends here