1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news, mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
32 (eval-when-compile (require 'cl))
34 (require 'gnus) ; for macro gnus-kill-buffer, at least
38 (require 'mail-source)
43 (autoload 'gnus-add-buffer "gnus")
44 (autoload 'gnus-kill-buffer "gnus"))
47 "Reading mail with Gnus."
50 (defgroup nnmail-retrieve nil
51 "Retrieving new mail."
54 (defgroup nnmail-prepare nil
55 "Preparing (or mangling) new mail after retrieval."
58 (defgroup nnmail-duplicate nil
59 "Handling of duplicate mail messages."
62 (defgroup nnmail-split nil
63 "Organizing the incoming mail in folders."
66 (defgroup nnmail-files nil
71 (defgroup nnmail-expire nil
75 (defgroup nnmail-procmail nil
76 "Interfacing with procmail and other mail agents."
79 (defgroup nnmail-various nil
80 "Various mail options."
83 (defcustom nnmail-split-methods '(("mail.misc" ""))
84 "*Incoming mail will be split according to this variable.
86 If you'd like, for instance, one mail group for mail from the
87 \"4ad-l\" mailing list, one group for junk mail and one for everything
88 else, you could do something like this:
90 (setq nnmail-split-methods
91 '((\"mail.4ad\" \"From:.*4ad\")
92 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
93 (\"mail.misc\" \"\")))
95 As you can see, this variable is a list of lists, where the first
96 element in each \"rule\" is the name of the group (which, by the way,
97 does not have to be called anything beginning with \"mail\",
98 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
99 nnmail will try to match on the header to find a fit.
101 The second element can also be a function. In that case, it will be
102 called narrowed to the headers with the first element of the rule as
103 the argument. It should return a non-nil value if it thinks that the
104 mail belongs in that group.
106 The last element should always have \"\" as the regexp.
108 This variable can also have a function as its value."
110 :type '(choice (repeat :tag "Alist" (group (string :tag "Name")
111 (choice regexp function)))
112 (function-item nnmail-split-fancy)
113 (function :tag "Other")))
115 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
116 (defcustom nnmail-crosspost t
117 "If non-nil, do crossposting if several split methods match the mail.
118 If nil, the first match found will be used."
122 (defcustom nnmail-split-fancy-with-parent-ignore-groups nil
123 "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
124 This can also be a list of regexps."
127 :type '(choice (const :tag "none" nil)
129 (repeat :value (".*") regexp)))
131 (defcustom nnmail-cache-ignore-groups nil
132 "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
133 This can also be a list of regexps."
136 :type '(choice (const :tag "none" nil)
138 (repeat :value (".*") regexp)))
140 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
141 (defcustom nnmail-keep-last-article nil
142 "If non-nil, nnmail will never delete/move a group's last article.
143 It can be marked expirable, so it will be deleted when it is no longer last.
145 You may need to set this variable if other programs are putting
146 new mail into folder numbers that Gnus has marked as expired."
147 :group 'nnmail-procmail
148 :group 'nnmail-various
151 (defcustom nnmail-use-long-file-names nil
152 "If non-nil the mail backends will use long file and directory names.
153 If nil, groups like \"mail.misc\" will end up in directories like
158 (defcustom nnmail-default-file-modes 384
159 "Set the mode bits of all new mail files to this integer."
163 (defcustom nnmail-expiry-wait 7
164 "*Expirable articles that are older than this will be expired.
165 This variable can either be a number (which will be interpreted as a
166 number of days) -- this doesn't have to be an integer. This variable
167 can also be `immediate' and `never'."
168 :group 'nnmail-expire
169 :type '(choice (const immediate)
173 (defcustom nnmail-expiry-wait-function nil
174 "Variable that holds function to specify how old articles should be before they are expired.
175 The function will be called with the name of the group that the expiry
176 is to be performed in, and it should return an integer that says how
177 many days an article can be stored before it is considered \"old\".
178 It can also return the values `never' and `immediate'.
182 \(setq nnmail-expiry-wait-function
184 (cond ((string-match \"private\" newsgroup) 31)
185 ((string-match \"junk\" newsgroup) 1)
186 ((string-match \"important\" newsgroup) 'never)
188 :group 'nnmail-expire
189 :type '(choice (const :tag "nnmail-expiry-wait" nil)
190 (function :format "%v" nnmail-)))
192 (defcustom nnmail-expiry-target 'delete
193 "*Variable that says where expired messages should end up.
194 The default value is `delete' (which says to delete the messages),
195 but it can also be a string or a function. If it is a string, expired
196 messages end up in that group. If it is a function, the function is
197 called in a buffer narrowed to the message in question. The function
198 receives one argument, the name of the group the message comes from.
199 The return value should be `delete' or a group name (a string)."
201 :group 'nnmail-expire
202 :type '(choice (const delete)
203 (function :format "%v" nnmail-)
206 (defcustom nnmail-fancy-expiry-targets nil
207 "Determine expiry target based on articles using fancy techniques.
209 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
210 `nnmail-expiry-target' is set to the function
211 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
212 the message will be expired to a group determined by invoking
213 `format-time-string' with TARGET used as the format string and the
214 time extracted from the articles' Date header (if missing the current
217 In the special cases that HEADER is the symbol `to-from', the regexp
218 will try to match against both the From and the To header.
222 \(setq nnmail-fancy-expiry-targets
223 '((to-from \"boss\" \"nnfolder:Work\")
224 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
225 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
227 In this case, articles containing the string \"boss\" in the To or the
228 From header will be expired to the group \"nnfolder:Work\";
229 articles containing the sting \"IMPORTANT\" in the Subject header will
230 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
231 everything else will be expired to \"nnfolder:Archive-YYYY\"."
233 :group 'nnmail-expire
234 :type '(repeat (list (choice :tag "Match against"
235 (string :tag "Header")
238 (string :tag "Target group format string"))))
240 (defcustom nnmail-cache-accepted-message-ids nil
241 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
242 If non-nil, also update the cache when copy or move articles."
246 (make-obsolete-variable 'nnmail-spool-file
247 "This option is obsolete in Gnus 5.9. \
248 Use `mail-sources' instead.")
249 ;; revision 5.29 / p0-85 / Gnus 5.9
250 ;; Variable removed in No Gnus v0.7
252 (defcustom nnmail-resplit-incoming nil
253 "*If non-nil, re-split incoming procmail sorted mail."
254 :group 'nnmail-procmail
257 (defcustom nnmail-scan-directory-mail-source-once nil
258 "*If non-nil, scan all incoming procmail sorted mails once.
259 It scans low-level sorted spools even when not required."
261 :group 'nnmail-procmail
264 (defcustom nnmail-delete-file-function 'delete-file
265 "Function called to delete files in some mail backends."
269 (defcustom nnmail-crosspost-link-function
270 (if (string-match "windows-nt\\|emx" (symbol-name system-type))
273 "*Function called to create a copy of a file.
274 This is `add-name-to-file' by default, which means that crossposts
275 will use hard links. If your file system doesn't allow hard
276 links, you could set this variable to `copy-file' instead."
278 :type '(radio (function-item add-name-to-file)
279 (function-item copy-file)
280 (function :tag "Other")))
282 (defcustom nnmail-read-incoming-hook
283 (if (eq system-type 'windows-nt)
284 '(nnheader-ms-strip-cr)
286 "*Hook that will be run after the incoming mail has been transferred.
287 The incoming mail is moved from the specified spool file (which normally is
288 something like \"/usr/spool/mail/$user\") to the user's home
289 directory. This hook is called after the incoming mail box has been
290 emptied, and can be used to call any mail box programs you have
291 running (\"xwatch\", etc.)
295 \(add-hook 'nnmail-read-incoming-hook
297 (call-process \"/local/bin/mailsend\" nil nil nil
299 ;; The incoming mail box file.
300 (expand-file-name (user-login-name)
301 rmail-spool-directory))))
303 If you have xwatch running, this will alert it that mail has been
306 If you use `display-time', you could use something like this:
308 \(add-hook 'nnmail-read-incoming-hook
310 ;; Update the displayed time, since that will clear out
311 ;; the flag that says you have mail.
312 (when (eq (process-status \"display-time\") 'run)
313 (display-time-filter display-time-process \"\"))))"
314 :group 'nnmail-prepare
317 (defcustom nnmail-prepare-incoming-hook nil
318 "Hook called before treating incoming mail.
319 The hook is run in a buffer with all the new, incoming mail."
320 :group 'nnmail-prepare
323 (defcustom nnmail-prepare-incoming-header-hook nil
324 "Hook called narrowed to the headers of each message.
325 This can be used to remove excessive spaces (and stuff like
326 that) from the headers before splitting and saving the messages."
327 :group 'nnmail-prepare
330 (defcustom nnmail-prepare-incoming-message-hook nil
331 "Hook called narrowed to each message."
332 :group 'nnmail-prepare
335 (defcustom nnmail-list-identifiers nil
336 "Regexp that matches list identifiers to be removed.
337 This can also be a list of regexps."
338 :group 'nnmail-prepare
339 :type '(choice (const :tag "none" nil)
341 (repeat :value (".*") regexp)))
343 (defcustom nnmail-pre-get-new-mail-hook nil
344 "Hook called just before starting to handle new incoming mail."
345 :group 'nnmail-retrieve
348 (defcustom nnmail-post-get-new-mail-hook nil
349 "Hook called just after finishing handling new incoming mail."
350 :group 'nnmail-retrieve
353 (defcustom nnmail-split-hook nil
354 "Hook called before deciding where to split an article.
355 The functions in this hook are free to modify the buffer
356 contents in any way they choose -- the buffer contents are
357 discarded after running the split process."
361 (defcustom nnmail-spool-hook nil
362 "*A hook called when a new article is spooled."
367 (defcustom nnmail-large-newsgroup 50
368 "*The number of articles which indicates a large newsgroup or nil.
369 If the number of articles is greater than the value, verbose
370 messages will be shown to indicate the current status."
371 :group 'nnmail-various
372 :type '(choice (const :tag "infinite" nil)
373 (number :tag "count")))
375 (define-widget 'nnmail-lazy 'default
376 "Base widget for recursive datastructures.
378 This is copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
380 :convert-widget 'widget-value-convert-widget
381 :value-create (lambda (widget)
382 (let ((value (widget-get widget :value))
383 (type (widget-get widget :type)))
384 (widget-put widget :children
385 (list (widget-create-child-value
386 widget (widget-convert type) value)))))
387 :value-delete 'widget-children-value-delete
388 :value-get (lambda (widget)
389 (widget-value (car (widget-get widget :children))))
390 :value-inline (lambda (widget)
391 (widget-apply (car (widget-get widget :children))
393 :default-get (lambda (widget)
395 (widget-convert (widget-get widget :type))))
396 :match (lambda (widget value)
397 (widget-apply (widget-convert (widget-get widget :type))
399 :validate (lambda (widget)
400 (widget-apply (car (widget-get widget :children)) :validate)))
402 (define-widget 'nnmail-split-fancy 'nnmail-lazy
403 "Widget for customizing splits in the variable of the same name."
405 :type '(menu-choice :value (any ".*value.*" "misc")
407 (string :tag "Destination")
408 (list :tag "Use first match (|)" :value (|)
410 (editable-list :inline t nnmail-split-fancy))
411 (list :tag "Use all matches (&)" :value (&)
413 (editable-list :inline t nnmail-split-fancy))
414 (list :tag "Function with fixed arguments (:)"
416 (const :format "" :value :)
418 (editable-list :inline t (sexp :tag "Arg"))
420 (list :tag "Function with split arguments (!)"
424 (editable-list :inline t nnmail-split-fancy))
425 (list :tag "Field match"
430 (symbol :value mail))
437 (const :tag "Junk (delete mail)" junk)))
439 (defcustom nnmail-split-fancy "mail.misc"
440 "Incoming mail can be split according to this fancy variable.
441 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
443 The format of this variable is SPLIT, where SPLIT can be one of
446 GROUP: Mail will be stored in GROUP (a string).
448 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
449 field FIELD (a regexp) contains VALUE (a regexp), store the messages
450 as specified by SPLIT. If RESTRICT (a regexp) matches some string
451 after FIELD and before the end of the matched VALUE, return nil,
452 otherwise process SPLIT. Multiple RESTRICTs add up, further
453 restricting the possibility of processing SPLIT.
455 \(| SPLIT...): Process each SPLIT expression until one of them matches.
456 A SPLIT expression is said to match if it will cause the mail
457 message to be stored in one or more groups.
459 \(& SPLIT...): Process each SPLIT expression.
461 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
462 the buffer containing the message headers. The return value FUNCTION
463 should be a split, which is then recursively processed.
465 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
466 return value FUNCTION should be a split, which is then recursively
469 junk: Mail will be deleted. Use with care! Do not submerge in water!
471 (setq nnmail-split-fancy
472 '(| (\"Subject\" \"MAKE MONEY FAST\" junk)
473 ...other.rules.omitted...))
475 FIELD must match a complete field name. VALUE must match a complete
476 word according to the `nnmail-split-fancy-syntax-table' syntax table.
477 You can use \".*\" in the regexps to match partial field names or words.
479 FIELD and VALUE can also be Lisp symbols, in that case they are expanded
480 as specified in `nnmail-split-abbrev-alist'.
482 GROUP can contain \\& and \\N which will substitute from matching
483 \\(\\) patterns in the previous VALUE.
487 \(setq nnmail-split-methods 'nnmail-split-fancy
489 ;; Messages from the mailer daemon are not crossposted to any of
490 ;; the ordinary groups. Warnings are put in a separate group
492 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
494 ;; Non-error messages are crossposted to all relevant
495 ;; groups, but we don't crosspost between the group for the
496 ;; (ding) list and the group for other (ding) related mail.
497 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
498 (\"subject\" \"ding\" \"ding.misc\"))
499 ;; Other mailing lists...
500 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
501 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
502 ;; Both lists below have the same suffix, so prevent
503 ;; cross-posting to mkpkg.list of messages posted only to
504 ;; the bugs- list, but allow cross-posting when the
505 ;; message was really cross-posted.
506 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
507 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
510 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
511 ;; Unmatched mail goes to the catch all group.
514 :type 'nnmail-split-fancy)
516 (defcustom nnmail-split-abbrev-alist
517 '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
518 (mail . "mailer-daemon\\|postmaster\\|uucp")
519 (to . "to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
520 (from . "from\\|sender\\|resent-from")
521 (nato . "to\\|cc\\|resent-to\\|resent-cc")
522 (naany . "from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
523 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
525 :type '(repeat (cons :format "%v" symbol regexp)))
527 (defcustom nnmail-message-id-cache-length 1000
528 "*The approximate number of Message-IDs nnmail will keep in its cache.
529 If this variable is nil, no checking on duplicate messages will be
531 :group 'nnmail-duplicate
532 :type '(choice (const :tag "disable" nil)
533 (integer :format "%v")))
535 (defcustom nnmail-message-id-cache-file
536 (nnheader-concat gnus-home-directory ".nnmail-cache")
537 "The file name of the nnmail Message-ID cache."
538 :group 'nnmail-duplicate
542 (defcustom nnmail-treat-duplicates 'warn
543 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
544 Three values are valid: nil, which means that nnmail is not to keep a
545 Message-ID cache; `warn', which means that nnmail should insert extra
546 headers to warn the user about the duplication (this is the default);
547 and `delete', which means that nnmail will delete duplicated mails.
549 This variable can also be a function. It will be called from a buffer
550 narrowed to the article in question with the Message-ID as a
551 parameter. It should return nil, `warn' or `delete'."
552 :group 'nnmail-duplicate
553 :type '(choice (const :tag "off" nil)
557 (defcustom nnmail-extra-headers '(To Newsgroups)
558 "*Extra headers to parse."
561 :type '(repeat symbol))
563 (defcustom nnmail-split-header-length-limit 2048
564 "Header lines longer than this limit are excluded from the split function."
569 (defcustom nnmail-mail-splitting-charset nil
570 "Default charset to be used when splitting incoming mail."
575 (defcustom nnmail-mail-splitting-decodes nil
576 "Whether the nnmail splitting functionality should MIME decode headers."
581 (defcustom nnmail-split-fancy-match-partial-words nil
582 "Whether to match partial words when fancy splitting.
583 Normally, regexes given in `nnmail-split-fancy' are implicitly surrounded
584 by \"\\=\\<...\\>\". If this variable is true, they are not implicitly\
591 (defcustom nnmail-split-lowercase-expanded t
592 "Whether to lowercase expanded entries (i.e. \\N) when splitting mails.
593 This avoids the creation of multiple groups when users send to an address
594 using different case (i.e. mailing-list@domain vs Mailing-List@Domain)."
599 ;;; Internal variables.
601 (defvar nnmail-article-buffer " *nnmail incoming*"
602 "The buffer used for splitting incoming mails.")
604 (defvar nnmail-split-history nil
605 "List of group/article elements that say where the previous split put messages.")
607 (defvar nnmail-split-fancy-syntax-table
608 (let ((table (make-syntax-table)))
609 ;; support the %-hack
610 (modify-syntax-entry ?\% "." table)
612 "Syntax table used by `nnmail-split-fancy'.")
614 (defvar nnmail-prepare-save-mail-hook nil
615 "Hook called before saving mail.")
617 (defvar nnmail-split-tracing nil)
618 (defvar nnmail-split-trace nil)
622 (defun nnmail-request-post (&optional server)
623 (mail-send-and-exit nil))
625 (defvar nnmail-file-coding-system 'raw-text
626 "Coding system used in nnmail.")
628 (defvar nnmail-incoming-coding-system
629 mm-text-coding-system
630 "Coding system used in reading inbox")
632 (defvar nnmail-pathname-coding-system nil
633 "*Coding system for file name.")
635 (defun nnmail-find-file (file)
636 "Insert FILE in server buffer safely."
637 (set-buffer nntp-server-buffer)
638 (delete-region (point-min) (point-max))
639 (let ((format-alist nil)
640 (after-insert-file-functions nil))
642 (let ((coding-system-for-read nnmail-file-coding-system)
643 (auto-mode-alist (mm-auto-mode-alist))
644 (file-name-coding-system nnmail-pathname-coding-system))
645 (insert-file-contents file)
649 (defun nnmail-group-pathname (group dir &optional file)
650 "Make file name for GROUP."
652 (let ((dir (file-name-as-directory (expand-file-name dir))))
653 (setq group (nnheader-replace-duplicate-chars-in-string
654 (nnheader-replace-chars-in-string group ?/ ?_)
656 (setq group (nnheader-translate-file-chars group))
657 ;; If this directory exists, we use it directly.
658 (file-name-as-directory
659 (if (or nnmail-use-long-file-names
660 (file-directory-p (concat dir group)))
661 (expand-file-name group dir)
662 ;; If not, we translate dots into slashes.
664 (nnheader-replace-chars-in-string group ?. ?/)
668 (defun nnmail-get-active ()
669 "Returns an assoc of group names and active ranges.
670 nn*-request-list should have been called before calling this function."
671 ;; Go through all groups from the active list.
673 (set-buffer nntp-server-buffer)
674 (nnmail-parse-active)))
676 (defun nnmail-parse-active ()
677 "Parse the active file in the current buffer and return an alist."
678 (goto-char (point-min))
679 (unless (re-search-forward "[\\\"]" nil t)
680 (goto-char (point-max))
681 (while (re-search-backward "[][';?()#]" nil t)
683 (goto-char (point-min))
684 (let ((buffer (current-buffer))
685 group-assoc group max min)
689 (narrow-to-region (point) (point-at-eol))
690 (setq group (read buffer))
691 (unless (stringp group)
692 (setq group (symbol-name group)))
693 (if (and (numberp (setq max (read buffer)))
694 (numberp (setq min (read buffer))))
695 (push (list (mm-string-as-unibyte group) (cons min max))
702 (defvar nnmail-active-file-coding-system 'raw-text
703 "*Coding system for active file.")
705 (defun nnmail-save-active (group-assoc file-name)
706 "Save GROUP-ASSOC in ACTIVE-FILE."
707 (let ((coding-system-for-write nnmail-active-file-coding-system))
709 (with-temp-file file-name
710 (mm-disable-multibyte)
711 (nnmail-generate-active group-assoc)))))
713 (defun nnmail-generate-active (alist)
714 "Generate an active file from group-alist ALIST."
717 (while (setq group (pop alist))
718 (insert (format "%S %d %d y\n" (intern (car group)) (cdadr group)
720 (goto-char (point-max))
721 (while (search-backward "\\." nil t)
724 (defun nnmail-get-split-group (file source)
725 "Find out whether this FILE is to be split into GROUP only.
726 If SOURCE is a directory spec, try to return the group name component."
727 (if (eq (car source) 'directory)
728 (let ((file (file-name-nondirectory file)))
729 (mail-source-bind (directory source)
730 (if (string-match (concat (regexp-quote suffix) "$") file)
731 (substring file 0 (match-beginning 0))
735 (defun nnmail-process-babyl-mail-format (func artnum-func)
736 (let ((case-fold-search t)
738 start message-id content-length do-search end)
740 (goto-char (point-min))
742 "
\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
743 (goto-char (match-end 0))
744 (delete-region (match-beginning 0) (match-end 0))
748 ;; Skip all the headers in case there are more "From "s...
749 (or (search-forward "\n\n" nil t)
750 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
751 (search-forward "
\1f\f"))
753 ;; Unquote the ">From " line, if any.
754 (goto-char (point-min))
755 (when (looking-at ">From ")
756 (replace-match "X-From-Line: ") )
757 (run-hooks 'nnmail-prepare-incoming-header-hook)
758 (goto-char (point-max))
759 ;; Find the Message-ID header.
761 (if (re-search-backward
762 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t)
763 (setq message-id (buffer-substring (match-beginning 1)
765 ;; There is no Message-ID here, so we create one.
767 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
769 (insert "Original-")))
771 (insert "Message-ID: " (setq message-id (nnmail-message-id))
773 ;; Look for a Content-Length header.
774 (if (not (save-excursion
775 (and (re-search-backward
776 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
777 (setq content-length (string-to-number
781 ;; We destroy the header, since none of
782 ;; the backends ever use it, and we do not
783 ;; want to confuse other mailers by having
784 ;; a (possibly) faulty header.
785 (progn (insert "X-") t))))
788 (if (or (= (+ (point) content-length) (point-max))
790 (goto-char (+ (point) content-length))
793 (goto-char (+ (point) content-length))
794 (setq do-search nil))
797 ;; Go to the beginning of the next article - or to the end
800 (if (re-search-forward "^
\1f" nil t)
801 (goto-char (match-beginning 0))
802 (goto-char (1- (point-max)))))
803 (delete-char 1) ; delete ^_
806 (narrow-to-region start (point))
807 (goto-char (point-min))
808 (nnmail-check-duplication message-id func artnum-func)
810 (setq end (point-max))))
814 (defsubst nnmail-search-unix-mail-delim ()
815 "Put point at the beginning of the next Unix mbox message."
816 ;; Algorithm used to find the next article in the
817 ;; brain-dead Unix mbox format:
819 ;; 1) Search for "^From ".
820 ;; 2) If we find it, then see whether the previous
821 ;; line is blank and the next line looks like a header.
822 ;; Then it's possible that this is a mail delim, and we use it.
823 (let ((case-fold-search nil)
826 (if (not (re-search-forward "^From " nil t))
830 (when (and (or (bobp)
833 (eq (char-after) ?\n)))
836 (while (looking-at ">From \\|From ")
838 (looking-at "[^ \n\t:]+[ \n\t]*:")))
839 (setq found 'yes)))))
843 (defun nnmail-search-unix-mail-delim-backward ()
844 "Put point at the beginning of the current Unix mbox message."
845 ;; Algorithm used to find the next article in the
846 ;; brain-dead Unix mbox format:
848 ;; 1) Search for "^From ".
849 ;; 2) If we find it, then see whether the previous
850 ;; line is blank and the next line looks like a header.
851 ;; Then it's possible that this is a mail delim, and we use it.
852 (let ((case-fold-search nil)
855 (if (not (re-search-backward "^From " nil t))
859 (when (and (or (bobp)
862 (eq (char-after) ?\n)))
865 (while (looking-at ">From \\|From ")
867 (looking-at "[^ \n\t:]+[ \n\t]*:")))
868 (setq found 'yes)))))
872 (defun nnmail-process-unix-mail-format (func artnum-func)
873 (let ((case-fold-search t)
875 start message-id content-length end skip head-end)
876 (goto-char (point-min))
877 (if (not (and (re-search-forward "^From " nil t)
878 (goto-char (match-beginning 0))))
879 ;; Possibly wrong format?
880 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
881 (if (buffer-file-name) "file" "buffer")
882 (or (buffer-file-name) (buffer-name)))
883 ;; Carry on until the bitter end.
887 ;; Find the end of the head.
890 (if (search-forward "\n\n" nil t)
892 ;; This will never happen, but just to be on the safe side --
893 ;; if there is no head-body delimiter, we search a bit manually.
894 (while (and (looking-at "From \\|[^ \t]+:")
898 ;; Find the Message-ID header.
899 (goto-char (point-min))
900 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
901 (setq message-id (match-string 1))
903 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
905 (insert "Original-")))
906 ;; There is no Message-ID here, so we create one.
908 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
909 ;; Look for a Content-Length header.
910 (goto-char (point-min))
911 (if (not (re-search-forward
912 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
913 (setq content-length nil)
914 (setq content-length (string-to-number (match-string 1)))
915 ;; We destroy the header, since none of the backends ever
916 ;; use it, and we do not want to confuse other mailers by
917 ;; having a (possibly) faulty header.
920 (run-hooks 'nnmail-prepare-incoming-header-hook)
921 ;; Find the end of this article.
922 (goto-char (point-max))
924 (setq head-end (point))
925 ;; We try the Content-Length value. The idea: skip over the header
926 ;; separator, then check what happens content-length bytes into the
927 ;; message body. This should be either the end of the buffer, the
928 ;; message separator or a blank line followed by the separator.
929 ;; The blank line should probably be deleted. If neither of the
930 ;; three is met, the content-length header is probably invalid.
933 (setq skip (+ (point) content-length))
935 (cond ((or (= skip (point-max))
936 (= (1+ skip) (point-max)))
937 (setq end (point-max)))
938 ((looking-at "From ")
940 ((looking-at "[ \t]*\n\\(From \\)")
941 (setq end (match-beginning 1)))
945 ;; No Content-Length, so we find the beginning of the next
946 ;; article or the end of the buffer.
948 (or (nnmail-search-unix-mail-delim)
949 (goto-char (point-max))))
950 ;; Allow the backend to save the article.
953 (narrow-to-region start (point))
954 (goto-char (point-min))
956 (nnmail-check-duplication message-id func artnum-func)
957 (setq end (point-max))))
961 (defun nnmail-process-mmdf-mail-format (func artnum-func)
962 (let ((delim "^\^A\^A\^A\^A$")
965 start message-id end)
966 (goto-char (point-min))
967 (if (not (and (re-search-forward delim nil t)
969 ;; Possibly wrong format?
970 (error "Error, unknown mail format! (Possibly corrupted.)")
971 ;; Carry on until the bitter end.
974 ;; Find the end of the head.
977 (if (search-forward "\n\n" nil t)
979 ;; This will never happen, but just to be on the safe side --
980 ;; if there is no head-body delimiter, we search a bit manually.
981 (while (and (looking-at "From \\|[^ \t]+:")
985 ;; Find the Message-ID header.
986 (goto-char (point-min))
987 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
988 (setq message-id (match-string 1))
989 ;; There is no Message-ID here, so we create one.
991 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
993 (insert "Original-")))
995 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
996 (run-hooks 'nnmail-prepare-incoming-header-hook)
997 ;; Find the end of this article.
998 (goto-char (point-max))
1000 (if (re-search-forward delim nil t)
1002 (goto-char (point-max)))
1003 ;; Allow the backend to save the article.
1006 (narrow-to-region start (point))
1007 (goto-char (point-min))
1009 (nnmail-check-duplication message-id func artnum-func)
1010 (setq end (point-max))))
1015 (defun nnmail-process-maildir-mail-format (func artnum-func)
1016 ;; In a maildir, every file contains exactly one mail.
1017 (let ((case-fold-search t)
1019 (goto-char (point-min))
1020 ;; Find the end of the head.
1023 (if (search-forward "\n\n" nil t)
1025 ;; This will never happen, but just to be on the safe side --
1026 ;; if there is no head-body delimiter, we search a bit manually.
1027 (while (and (looking-at "From \\|[^ \t]+:")
1031 ;; Find the Message-ID header.
1032 (goto-char (point-min))
1033 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
1034 (setq message-id (match-string 1))
1035 ;; There is no Message-ID here, so we create one.
1037 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
1039 (insert "Original-")))
1041 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
1042 (run-hooks 'nnmail-prepare-incoming-header-hook)
1043 ;; Allow the backend to save the article.
1046 (goto-char (point-min))
1047 (nnmail-check-duplication message-id func artnum-func))
1050 (defvar nnmail-group-names-not-encoded-p nil
1051 "Non-nil means group names are not encoded.")
1053 (defun nnmail-split-incoming (incoming func &optional exit-func
1055 "Go through the entire INCOMING file and pick out each individual mail.
1056 FUNC will be called with the buffer narrowed to each mail."
1057 (let ( ;; If this is a group-specific split, we bind the split
1058 ;; methods to just this group.
1059 (nnmail-split-methods (if (and group
1060 (not nnmail-resplit-incoming))
1061 (list (list group ""))
1062 nnmail-split-methods))
1063 (nnmail-group-names-not-encoded-p t))
1065 ;; Insert the incoming file.
1066 (set-buffer (get-buffer-create nnmail-article-buffer))
1068 (let ((coding-system-for-read nnmail-incoming-coding-system))
1069 (mm-insert-file-contents incoming))
1071 (if (zerop (buffer-size))
1073 (goto-char (point-min))
1074 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
1075 ;; Handle both babyl, MMDF and unix mail formats, since
1076 ;; movemail will use the former when fetching from a
1077 ;; mailbox, the latter when fetching from a file.
1078 (cond ((or (looking-at "\^L")
1079 (looking-at "BABYL OPTIONS:"))
1080 (nnmail-process-babyl-mail-format func artnum-func))
1081 ((looking-at "\^A\^A\^A\^A")
1082 (nnmail-process-mmdf-mail-format func artnum-func))
1083 ((looking-at "Return-Path:")
1084 (nnmail-process-maildir-mail-format func artnum-func))
1086 (nnmail-process-unix-mail-format func artnum-func))))
1088 (funcall exit-func))
1089 (kill-buffer (current-buffer))))))
1091 (defun nnmail-article-group (func &optional trace)
1092 "Look at the headers and return an alist of groups that match.
1093 FUNC will be called with the group name to determine the article number."
1094 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
1095 (obuf (current-buffer))
1096 group-art method grp)
1097 (if (and (sequencep methods)
1098 (= (length methods) 1))
1099 ;; If there is only just one group to put everything in, we
1100 ;; just return a list with just this one method in.
1102 (list (cons (caar methods) (funcall func (caar methods)))))
1103 ;; We do actual comparison.
1105 ;; Copy the article into the work buffer.
1106 (set-buffer nntp-server-buffer)
1108 (insert-buffer-substring obuf)
1109 ;; Narrow to headers.
1111 (goto-char (point-min))
1112 (if (search-forward "\n\n" nil t)
1115 (goto-char (point-min))
1116 ;; Decode MIME headers and charsets.
1117 (when nnmail-mail-splitting-decodes
1118 (let ((mail-parse-charset nnmail-mail-splitting-charset))
1119 (mail-decode-encoded-word-region (point-min) (point-max))))
1120 ;; Fold continuation lines.
1121 (goto-char (point-min))
1122 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1123 (replace-match " " t t))
1124 ;; Nuke pathologically long headers. Since Gnus applies
1125 ;; pathologically complex regexps to the buffer, lines
1126 ;; that are looong will take longer than the Universe's
1127 ;; existence to process.
1128 (goto-char (point-min))
1130 (unless (< (move-to-column nnmail-split-header-length-limit)
1131 nnmail-split-header-length-limit)
1132 (delete-region (point) (point-at-eol)))
1135 (goto-char (point-min))
1136 (run-hooks 'nnmail-split-hook)
1137 (when (setq nnmail-split-tracing trace)
1138 (setq nnmail-split-trace nil))
1139 (if (and (symbolp nnmail-split-methods)
1140 (fboundp nnmail-split-methods))
1142 (condition-case error-info
1143 ;; `nnmail-split-methods' is a function, so we
1144 ;; just call this function here and use the
1146 (or (funcall nnmail-split-methods)
1150 5 "Error in `nnmail-split-methods'; using `bogus' mail group: %S" error-info)
1153 (setq split (mm-delete-duplicates split))
1154 ;; The article may be "cross-posted" to `junk'. What
1155 ;; to do? Just remove the `junk' spec. Don't really
1156 ;; see anything else to do...
1158 (while (setq elem (car (memq 'junk split)))
1159 (setq split (delq elem split))))
1163 (lambda (group) (cons group (funcall func group)))
1165 ;; Go through the split methods to find a match.
1167 (or nnmail-crosspost
1169 (goto-char (point-max))
1170 (setq method (pop methods)
1173 (not (equal "" (nth 1 method))))
1176 (if (stringp (nth 1 method))
1177 (let ((expand (string-match "\\\\[0-9&]" grp))
1178 (pos (re-search-backward (cadr method)
1181 (setq grp (nnmail-expand-newtext grp)))
1183 ;; Function to say whether this is a match.
1184 (funcall (nth 1 method) grp)))
1185 ;; Don't enter the article into the same
1187 (not (assoc grp group-art)))
1188 (push (cons grp (funcall func grp))
1190 ;; This is the final group, which is used as a
1194 (list (cons (car method)
1195 (funcall func (car method))))))))
1196 ;; Fall back on "bogus" if all else fails.
1198 (setq group-art (list (cons "bogus" (funcall func "bogus"))))))
1199 ;; Produce a trace if non-empty.
1200 (when (and trace nnmail-split-trace)
1201 (let ((restore (current-buffer)))
1202 (nnheader-set-temp-buffer "*Split Trace*")
1204 (dolist (trace (nreverse nnmail-split-trace))
1205 (prin1 trace (current-buffer))
1207 (goto-char (point-min))
1208 (gnus-configure-windows 'split-trace)
1209 (set-buffer restore)))
1211 ;; See whether the split methods returned `junk'.
1212 (if (equal group-art '(junk))
1214 ;; The article may be "cross-posted" to `junk'. What
1215 ;; to do? Just remove the `junk' spec. Don't really
1216 ;; see anything else to do...
1218 (while (setq elem (car (memq 'junk group-art)))
1219 (setq group-art (delq elem group-art)))
1220 (nreverse group-art)))))))
1222 (defun nnmail-insert-lines ()
1223 "Insert how many lines there are in the body of the mail.
1224 Return the number of characters in the body."
1227 (goto-char (point-min))
1228 (unless (search-forward "\n\n" nil t)
1229 (goto-char (point-max))
1231 (setq chars (- (point-max) (point)))
1232 (setq lines (count-lines (point) (point-max)))
1235 (when (re-search-backward "^Lines: " nil t)
1236 (delete-region (point) (progn (forward-line 1) (point)))))
1238 (insert (format "Lines: %d\n" (max lines 0)))
1241 (defun nnmail-insert-xref (group-alist)
1242 "Insert an Xref line based on the (group . article) alist."
1244 (goto-char (point-min))
1245 (unless (search-forward "\n\n" nil t)
1246 (goto-char (point-max))
1249 (when (re-search-backward "^Xref: " nil t)
1250 (delete-region (match-beginning 0)
1251 (progn (forward-line 1) (point))))
1252 (insert (format "Xref: %s" (system-name)))
1254 (insert (if (mm-multibyte-p)
1255 (mm-string-as-multibyte
1256 (format " %s:%d" (caar group-alist) (cdar group-alist)))
1257 (mm-string-as-unibyte
1258 (format " %s:%d" (caar group-alist) (cdar group-alist)))))
1259 (setq group-alist (cdr group-alist)))
1262 ;;; Message washing functions
1264 (defun nnmail-remove-leading-whitespace ()
1265 "Remove excessive whitespace from all headers."
1266 (goto-char (point-min))
1267 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1268 (replace-match "\\1" t)))
1270 (defun nnmail-remove-list-identifiers ()
1271 "Remove list identifiers from Subject headers."
1273 (if (consp nnmail-list-identifiers)
1274 (mapconcat 'identity nnmail-list-identifiers " *\\|")
1275 nnmail-list-identifiers)))
1277 (goto-char (point-min))
1278 (while (re-search-forward
1279 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1281 (delete-region (match-beginning 2) (match-end 0))
1282 (beginning-of-line))
1283 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1285 (delete-region (match-beginning 1) (match-end 1))
1286 (beginning-of-line)))))
1288 (defun nnmail-remove-tabs ()
1289 "Translate TAB characters into SPACE characters."
1290 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1292 (defcustom nnmail-broken-references-mailers
1293 "^X-Mailer:.*\\(Eudora\\|Pegasus\\)"
1294 "Header line matching mailer producing bogus References lines.
1295 See `nnmail-ignore-broken-references'."
1296 :group 'nnmail-prepare
1297 :version "23.1" ;; No Gnus
1300 (defun nnmail-ignore-broken-references ()
1301 "Ignore the References line and use In-Reply-To
1303 Eudora has a broken References line, but an OK In-Reply-To."
1304 (goto-char (point-min))
1305 (when (re-search-forward nnmail-broken-references-mailers nil t)
1306 (goto-char (point-min))
1307 (when (re-search-forward "^References:" nil t)
1309 (insert "X-Gnus-Broken-Eudora-"))
1310 (goto-char (point-min))
1311 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1312 (replace-match "\\1" t))))
1314 (defalias 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references)
1315 (make-obsolete 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references)
1317 (custom-add-option 'nnmail-prepare-incoming-header-hook
1318 'nnmail-ignore-broken-references)
1320 ;;; Utility functions
1322 (declare-function gnus-activate-group "gnus-start"
1323 (group &optional scan dont-check method))
1325 (defun nnmail-do-request-post (accept-func &optional server)
1326 "Utility function to directly post a message to an nnmail-derived group.
1327 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1328 to actually put the message in the right group."
1330 (dolist (mbx (message-unquote-tokens
1331 (message-tokenize-header
1332 (message-fetch-field "Newsgroups") ", ")) success)
1333 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1334 (or (gnus-active to-newsgroup)
1335 (gnus-activate-group to-newsgroup)
1336 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1338 (or (and (gnus-request-create-group
1339 to-newsgroup gnus-command-method)
1340 (gnus-activate-group to-newsgroup nil nil
1341 gnus-command-method))
1342 (error "Couldn't create group %s" to-newsgroup)))
1343 (error "No such group: %s" to-newsgroup))
1344 (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1345 (setq success nil))))))
1347 (defun nnmail-split-fancy ()
1348 "Fancy splitting method.
1349 See the documentation for the variable `nnmail-split-fancy' for details."
1350 (with-syntax-table nnmail-split-fancy-syntax-table
1351 (nnmail-split-it nnmail-split-fancy)))
1353 (defvar nnmail-split-cache nil)
1354 ;; Alist of split expressions their equivalent regexps.
1356 (defun nnmail-split-it (split)
1357 ;; Return a list of groups matching SPLIT.
1364 ;; A group name. Do the \& and \N subs into the string.
1366 (when nnmail-split-tracing
1367 (push split nnmail-split-trace))
1368 (list (nnmail-expand-newtext split)))
1370 ;; Junk the message.
1372 (when nnmail-split-tracing
1373 (push "junk" nnmail-split-trace))
1376 ;; Builtin & operation.
1377 ((eq (car split) '&)
1378 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1380 ;; Builtin | operation.
1381 ((eq (car split) '|)
1383 (while (and (not done) (cdr split))
1384 (setq split (cdr split)
1385 done (nnmail-split-it (car split))))
1388 ;; Builtin : operation.
1389 ((eq (car split) ':)
1390 (when nnmail-split-tracing
1391 (push split nnmail-split-trace))
1392 (nnmail-split-it (save-excursion (eval (cdr split)))))
1394 ;; Builtin ! operation.
1395 ((eq (car split) '!)
1396 (funcall (cadr split) (nnmail-split-it (caddr split))))
1398 ;; Check the cache for the regexp for this split.
1399 ((setq cached-pair (assq split nnmail-split-cache))
1401 (end-point (point-max))
1402 (value (nth 1 split)))
1404 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1405 (while (and (goto-char end-point)
1406 (re-search-backward (cdr cached-pair) nil t))
1407 (when nnmail-split-tracing
1408 (push split nnmail-split-trace))
1409 (let ((split-rest (cddr split))
1411 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1412 ;; So, start-of-value is the point just before the
1413 ;; beginning of the value, whereas after-header-name
1414 ;; is the point just after the field name.
1415 (start-of-value (match-end 1))
1416 (after-header-name (match-end 2)))
1417 ;; Start the next search just before the beginning of the
1419 (setq end-point (1- start-of-value))
1420 ;; Handle - RESTRICTs
1421 (while (eq (car split-rest) '-)
1422 ;; RESTRICT must start after-header-name and
1423 ;; end after start-of-value, so that, for
1424 ;; (any "foo" - "x-foo" "foo.list")
1425 ;; we do not exclude foo.list just because
1426 ;; the header is: ``To: x-foo, foo''
1428 (if (and (re-search-backward (cadr split-rest)
1429 after-header-name t)
1430 (> (match-end 0) start-of-value))
1431 (setq split-rest nil)
1432 (setq split-rest (cddr split-rest))))
1435 (let ((value (nth 1 split)))
1437 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1438 ;; Someone might want to do a \N sub on this match, so get the
1439 ;; correct match positions.
1440 (re-search-backward value start-of-value))
1441 (dolist (sp (nnmail-split-it (car split-rest)))
1442 (unless (member sp split-result)
1443 (push sp split-result))))))
1446 ;; Not in cache, compute a regexp for the field/value pair.
1448 (let ((field (nth 0 split))
1449 (value (nth 1 split))
1450 (split-rest (cddr split))
1455 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1456 (if (and (>= (length value) 2)
1457 (string= ".*" (substring value 0 2)))
1458 (setq value (substring value 2)
1460 ;; Same trick for the rear of the regexp
1461 (if (and (>= (length value) 2)
1462 (string= ".*" (substring value -2)))
1463 (setq value (substring value 0 -2)
1465 ;; Invert the match-partial-words behavior if the optional
1466 ;; last element is specified.
1467 (while (eq (car split-rest) '-)
1468 (setq split-rest (cddr split-rest)))
1469 (when (if (cadr split-rest)
1470 (not nnmail-split-fancy-match-partial-words)
1471 nnmail-split-fancy-match-partial-words)
1472 (setq partial-front ""
1474 (setq regexp (concat "^\\(\\("
1476 (cdr (assq field nnmail-split-abbrev-alist))
1479 (or partial-front "\\<")
1483 (or partial-rear "\\>")))
1484 (push (cons split regexp) nnmail-split-cache)
1485 ;; Now that it's in the cache, just call nnmail-split-it again
1486 ;; on the same split, which will find it immediately in the cache.
1487 (nnmail-split-it split))))))
1489 (defun nnmail-expand-newtext (newtext)
1490 (let ((len (length newtext))
1492 c expanded beg N did-expand)
1495 (while (and (< pos len)
1496 (not (= (aref newtext pos) ?\\)))
1497 (setq pos (1+ pos)))
1499 (push (substring newtext beg pos) expanded))
1501 ;; We hit a \; expand it.
1504 c (aref newtext pos))
1505 (if (not (or (= c ?\&)
1508 ;; \ followed by some character we don't expand.
1509 (push (char-to-string c) expanded)
1514 (when (match-beginning N)
1515 (push (if nnmail-split-lowercase-expanded
1516 (downcase (buffer-substring (match-beginning N)
1518 (buffer-substring (match-beginning N) (match-end N)))
1520 (setq pos (1+ pos)))
1522 (apply 'concat (nreverse expanded))
1525 ;; Activate a backend only if it isn't already activated.
1526 ;; If FORCE, re-read the active file even if the backend is
1527 ;; already activated.
1528 (defun nnmail-activate (backend &optional force)
1529 (nnheader-init-server-buffer)
1530 (let (file timestamp file-time)
1531 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1533 (and (setq file (ignore-errors
1534 (symbol-value (intern (format "%s-active-file"
1536 (setq file-time (nth 5 (file-attributes file)))
1540 (symbol-value (intern
1541 (format "%s-active-timestamp"
1544 (not (consp timestamp))
1545 (equal timestamp '(0 0))
1546 (> (nth 0 file-time) (nth 0 timestamp))
1547 (and (= (nth 0 file-time) (nth 0 timestamp))
1548 (> (nth 1 file-time) (nth 1 timestamp))))))
1550 (or (eq timestamp 'none)
1551 (set (intern (format "%s-active-timestamp" backend))
1553 (funcall (intern (format "%s-request-list" backend)))))
1556 (defun nnmail-message-id ()
1557 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1560 ;;; nnmail duplicate handling
1563 (defvar nnmail-cache-buffer nil)
1565 (defun nnmail-cache-open ()
1566 (if (or (not nnmail-treat-duplicates)
1567 (and nnmail-cache-buffer
1568 (buffer-name nnmail-cache-buffer)))
1569 () ; The buffer is open.
1572 (setq nnmail-cache-buffer
1573 (get-buffer-create " *nnmail message-id cache*")))
1575 (when (file-exists-p nnmail-message-id-cache-file)
1576 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1577 (set-buffer-modified-p nil)
1580 (defun nnmail-cache-close ()
1581 (when (and nnmail-cache-buffer
1582 nnmail-treat-duplicates
1583 (buffer-name nnmail-cache-buffer)
1584 (buffer-modified-p nnmail-cache-buffer))
1586 (set-buffer nnmail-cache-buffer)
1587 ;; Weed out the excess number of Message-IDs.
1588 (goto-char (point-max))
1589 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1592 (delete-region (point-min) (point))))
1594 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1595 (make-directory (file-name-directory nnmail-message-id-cache-file)
1597 (nnmail-write-region (point-min) (point-max)
1598 nnmail-message-id-cache-file nil 'silent)
1599 (set-buffer-modified-p nil)
1600 (setq nnmail-cache-buffer nil)
1601 (gnus-kill-buffer (current-buffer)))))
1603 ;; Compiler directives.
1605 (defvar group-art-list)
1607 (defun nnmail-cache-insert (id grp &optional subject sender)
1609 ;; this will handle cases like `B r' where the group is nil
1610 (let ((grp (or grp gnus-newsgroup-name "UNKNOWN")))
1611 (run-hook-with-args 'nnmail-spool-hook
1612 id grp subject sender))
1613 (when nnmail-treat-duplicates
1614 ;; Store some information about the group this message is written
1615 ;; to. This is passed in as the grp argument -- all locations this
1616 ;; has been called from have been checked and the group is available.
1617 ;; The only ambiguous case is nnmail-check-duplication which will only
1618 ;; pass the first (of possibly >1) group which matches. -Josh
1619 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1620 (nnmail-cache-open))
1622 (set-buffer nnmail-cache-buffer)
1623 (goto-char (point-max))
1624 (if (and grp (not (string= "" grp))
1625 (gnus-methods-equal-p gnus-command-method
1626 (nnmail-cache-primary-mail-backend)))
1627 (let ((regexp (if (consp nnmail-cache-ignore-groups)
1628 (mapconcat 'identity nnmail-cache-ignore-groups
1630 nnmail-cache-ignore-groups)))
1631 (unless (and regexp (string-match regexp grp))
1632 (insert id "\t" grp "\n")))
1633 (insert id "\n"))))))
1635 (defun nnmail-cache-primary-mail-backend ()
1636 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1640 (while (and (null res) be-list)
1641 (setq be (car be-list))
1642 (setq be-list (cdr be-list))
1643 (when (and (gnus-method-option-p be 'respool)
1645 (intern (format "%s-get-new-mail" (car be))))
1646 (boundp get-new-mail)
1647 (symbol-value get-new-mail))
1651 ;; Fetch the group name corresponding to the message id stored in the
1653 (defun nnmail-cache-fetch-group (id)
1654 (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1656 (set-buffer nnmail-cache-buffer)
1657 (goto-char (point-max))
1658 (when (search-backward id nil t)
1660 (skip-chars-forward "^\n\r\t")
1661 (unless (looking-at "[\r\n]")
1663 (buffer-substring (point) (point-at-eol)))))))
1665 ;; Function for nnmail-split-fancy: look up all references in the
1666 ;; cache and if a match is found, return that group.
1667 (defun nnmail-split-fancy-with-parent ()
1668 "Split this message into the same group as its parent.
1669 This function can be used as an entry in `nnmail-split-fancy', for
1670 example like this: (: nnmail-split-fancy-with-parent)
1671 For a message to be split, it looks for the parent message in the
1672 References or In-Reply-To header and then looks in the message id
1673 cache file (given by the variable `nnmail-message-id-cache-file') to
1674 see which group that message was put in. This group is returned.
1676 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1677 (let* ((refstr (or (message-fetch-field "references")
1678 (message-fetch-field "in-reply-to")))
1681 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1683 (lambda (x) (format "\\(%s\\)" x))
1684 nnmail-split-fancy-with-parent-ignore-groups
1686 nnmail-split-fancy-with-parent-ignore-groups)))
1688 (setq references (nreverse (gnus-split-references refstr)))
1689 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1690 (nnmail-cache-open))
1691 (dolist (x references)
1692 (setq res (or (nnmail-cache-fetch-group x) res))
1693 (when (or (member res '("delayed" "drafts" "queue"))
1694 (and regexp res (string-match regexp res)))
1698 (defun nnmail-cache-id-exists-p (id)
1699 (when nnmail-treat-duplicates
1701 (set-buffer nnmail-cache-buffer)
1702 (goto-char (point-max))
1703 (search-backward id nil t))))
1705 (defun nnmail-fetch-field (header)
1708 (message-narrow-to-head)
1709 (message-fetch-field header))))
1711 (defun nnmail-check-duplication (message-id func artnum-func)
1712 (run-hooks 'nnmail-prepare-incoming-message-hook)
1713 ;; If this is a duplicate message, then we do not save it.
1714 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1715 (case-fold-search t)
1716 (action (when duplication
1718 ((memq nnmail-treat-duplicates '(warn delete))
1719 nnmail-treat-duplicates)
1720 ((functionp nnmail-treat-duplicates)
1721 (funcall nnmail-treat-duplicates message-id))
1723 nnmail-treat-duplicates))))
1725 ;; We insert a line that says what the mail source is.
1726 (let ((case-fold-search t))
1727 (goto-char (point-min))
1728 (re-search-forward "^message-id[ \t]*:" nil t)
1730 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1732 ;; Let the backend save the article (or not).
1735 (funcall func (setq group-art
1736 (nreverse (nnmail-article-group artnum-func))))
1737 (nnmail-cache-insert message-id (caar group-art)))
1738 ((eq action 'delete)
1739 (setq group-art nil))
1741 ;; We insert a warning.
1742 (let ((case-fold-search t))
1743 (goto-char (point-min))
1744 (re-search-forward "^message-id[ \t]*:" nil t)
1747 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1748 (funcall func (setq group-art
1749 (nreverse (nnmail-article-group artnum-func))))))
1751 (funcall func (setq group-art
1752 (nreverse (nnmail-article-group artnum-func))))))
1753 ;; Add the group-art list to the history list.
1755 (push group-art nnmail-split-history)
1756 (delete-region (point-min) (point-max)))))
1760 (defvar nnmail-fetched-sources nil)
1762 (defun nnmail-get-value (&rest args)
1763 (let ((sym (intern (apply 'format args))))
1765 (symbol-value sym))))
1767 (defun nnmail-get-new-mail (method exit-func temp
1768 &optional group spool-func)
1769 "Read new incoming mail."
1770 (nnmail-get-new-mail-1 method exit-func temp group nil spool-func))
1772 (defun nnmail-get-new-mail-1 (method exit-func temp
1773 group in-group spool-func)
1774 (let* ((sources mail-sources)
1779 incoming incomings source)
1780 (when (and (nnmail-get-value "%s-get-new-mail" method)
1782 (while (setq source (pop sources))
1783 ;; Use group's parameter
1784 (when (eq (car source) 'group)
1787 (gnus-group-find-parameter
1788 (concat (symbol-name method) ":" group)
1790 (nnmail-get-new-mail-1 method exit-func temp
1791 group group spool-func))
1793 ;; Hack to only fetch the contents of a single group's spool file.
1794 (when (and (eq (car source) 'directory)
1795 (null nnmail-scan-directory-mail-source-once)
1797 (mail-source-bind (directory source)
1798 (setq source (append source
1804 ,(concat group suffix)
1805 (file-name-nondirectory file)))))))))
1806 (when nnmail-fetched-sources
1807 (if (member source nnmail-fetched-sources)
1809 (push source nnmail-fetched-sources)
1810 (push source fetching-sources)))))
1811 (when fetching-sources
1812 ;; We first activate all the groups.
1813 (nnmail-activate method)
1814 ;; Allow the user to hook.
1815 (run-hooks 'nnmail-pre-get-new-mail-hook)
1816 ;; Open the message-id cache.
1818 ;; The we go through all the existing mail source specification
1819 ;; and fetch the mail from each.
1820 (while (setq source (pop fetching-sources))
1821 (nnheader-message 4 "%s: Reading incoming mail from %s..."
1822 method (car source))
1827 `(lambda (file orig-file)
1828 (nnmail-split-incoming
1829 file ',(intern (format "%s-save-mail" method))
1832 (if (equal file orig-file)
1834 (nnmail-get-split-group orig-file ',source)))
1835 ',(intern (format "%s-active-number" method)))))))
1838 ;; If we did indeed read any incoming spools, we save all info.
1840 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1841 method (car source))
1843 (nnmail-get-value "%s-group-alist" method)
1844 (nnmail-get-value "%s-active-file" method))
1846 (funcall exit-func))
1847 (run-hooks 'nnmail-read-incoming-hook)
1848 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1850 ;; Close the message-id cache.
1851 (nnmail-cache-close)
1852 ;; Allow the user to hook.
1853 (run-hooks 'nnmail-post-get-new-mail-hook))))
1855 (defun nnmail-expired-article-p (group time force &optional inhibit)
1856 "Say whether an article that is TIME old in GROUP should be expired."
1859 (let ((days (or (and nnmail-expiry-wait-function
1860 (funcall nnmail-expiry-wait-function group))
1861 nnmail-expiry-wait)))
1862 (cond ((or (eq days 'never)
1865 ;; This isn't an expirable group.
1867 ((eq days 'immediate)
1868 ;; We expire all articles on sight.
1870 ((equal time '(0 0))
1871 ;; This is an ange-ftp group, and we don't have any dates.
1874 (setq days (days-to-time days))
1875 ;; Compare the time with the current time.
1876 (ignore-errors (time-less-p days (time-since time))))))))
1878 (declare-function gnus-group-mark-article-read "gnus-group" (group article))
1880 (defun nnmail-expiry-target-group (target group)
1881 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1882 ;; that buffer if the nnfolder group isn't selected.
1883 (let (nnmail-cache-accepted-message-ids)
1884 ;; Don't enter Message-IDs into cache.
1885 ;; Let users hack it in TARGET function.
1886 (when (functionp target)
1887 (setq target (funcall target group)))
1888 (unless (eq target 'delete)
1889 (when (or (gnus-request-group target)
1890 (gnus-request-create-group target))
1891 (let ((group-art (gnus-request-accept-article target nil nil t)))
1892 (when (consp group-art)
1893 (gnus-group-mark-article-read target (cdr group-art))))))))
1895 (defun nnmail-fancy-expiry-target (group)
1896 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1898 (case-fold-search nil)
1899 (from (or (message-fetch-field "from") ""))
1900 (to (or (message-fetch-field "to") ""))
1901 (date (message-fetch-field "date"))
1907 (message "%s" (error-message-string err))
1910 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1911 (setq header (car regexp-target-pair))
1913 ;; If the header is to-from then match against the
1914 ;; To or From header
1915 ((and (equal header 'to-from)
1916 (or (string-match (cadr regexp-target-pair) from)
1917 (and (string-match (cadr regexp-target-pair) to)
1918 (let ((rmail-dont-reply-to-names
1919 (message-dont-reply-to-names)))
1920 (equal (rmail-dont-reply-to from) "")))))
1921 (setq target (format-time-string (caddr regexp-target-pair) date)))
1922 ((and (not (equal header 'to-from))
1923 (string-match (cadr regexp-target-pair)
1925 (message-fetch-field header)
1928 (format-time-string (caddr regexp-target-pair) date)))))))
1930 (defun nnmail-check-syntax ()
1931 "Check (and modify) the syntax of the message in the current buffer."
1933 (message-narrow-to-head)
1934 (let ((case-fold-search t))
1935 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1936 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1938 (defun nnmail-write-region (start end filename &optional append visit lockname)
1939 "Do a `write-region', and then set the file modes."
1940 (let ((coding-system-for-write nnmail-file-coding-system)
1941 (file-name-coding-system nnmail-pathname-coding-system))
1942 (write-region start end filename append visit lockname)
1943 (set-file-modes filename nnmail-default-file-modes)))
1946 ;;; Status functions
1949 (defun nnmail-replace-status (name value)
1950 "Make status NAME and VALUE part of the current status line."
1952 (message-narrow-to-head)
1953 (let ((status (nnmail-decode-status)))
1954 (setq status (delq (member name status) status))
1956 (push (cons name value) status))
1957 (message-remove-header "status")
1958 (goto-char (point-max))
1959 (insert "Status: " (nnmail-encode-status status) "\n"))))
1961 (defun nnmail-decode-status ()
1962 "Return a status-value alist from STATUS."
1963 (goto-char (point-min))
1964 (when (re-search-forward "^Status: " nil t)
1965 (let (name value status)
1967 ;; Narrow to the status.
1970 (if (re-search-forward "^[^ \t]" nil t)
1973 ;; Go through all elements and add them to the list.
1974 (goto-char (point-min))
1975 (while (re-search-forward "[^ \t=]+" nil t)
1976 (setq name (match-string 0))
1977 (if (not (eq (char-after) ?=))
1981 (if (not (eq (char-after) ?\"))
1982 (if (not (looking-at "[^ \t]"))
1986 (setq value (match-string 0))
1987 (goto-char (match-end 0)))
1989 (setq value (read (current-buffer)))))
1990 (push (cons name value) status)))
1993 (defun nnmail-encode-status (status)
1994 "Return a status string from STATUS."
1999 (if (string-match "[ \t]" (cdr elem))
2000 (prin1-to-string (cdr elem))
2004 (defun nnmail-split-history ()
2005 "Generate an overview of where the last mail split put articles."
2007 (unless nnmail-split-history
2008 (error "No current split history"))
2009 (with-output-to-temp-buffer "*nnmail split history*"
2010 (with-current-buffer standard-output
2011 (fundamental-mode)) ; for Emacs 20.4+
2012 (dolist (elem nnmail-split-history)
2013 (princ (mapconcat (lambda (ga)
2014 (concat (car ga) ":" (int-to-string (cdr ga))))
2019 (defun nnmail-purge-split-history (group)
2020 "Remove all instances of GROUP from `nnmail-split-history'."
2021 (let ((history nnmail-split-history))
2023 (setcar history (gnus-remove-if (lambda (e) (string= (car e) group))
2026 (setq nnmail-split-history (delq nil nnmail-split-history))))
2028 (defun nnmail-new-mail-p (group)
2029 "Say whether GROUP has new mail."
2030 (let ((his nnmail-split-history)
2033 (when (assoc group (pop his))
2038 (defun nnmail-within-headers-p ()
2039 "Check to see if point is within the headers of a unix mail message.
2040 Doesn't change point."
2041 (let ((pos (point)))
2043 (and (nnmail-search-unix-mail-delim-backward)
2044 (not (search-forward "\n\n" pos t))))))
2046 (run-hooks 'nnmail-load-hook)
2050 ;; arch-tag: fe8f671a-50db-428a-bb5d-f00462f72ed7
2051 ;;; nnmail.el ends here