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, 2009, 2010 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/>.
28 ;; For Emacs <22.2 and XEmacs.
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)
42 (autoload 'gnus-add-buffer "gnus")
43 (autoload 'gnus-kill-buffer "gnus")
46 "Reading mail with Gnus."
49 (defgroup nnmail-retrieve nil
50 "Retrieving new mail."
53 (defgroup nnmail-prepare nil
54 "Preparing (or mangling) new mail after retrieval."
57 (defgroup nnmail-duplicate nil
58 "Handling of duplicate mail messages."
61 (defgroup nnmail-split nil
62 "Organizing the incoming mail in folders."
65 (defgroup nnmail-files nil
70 (defgroup nnmail-expire nil
74 (defgroup nnmail-procmail nil
75 "Interfacing with procmail and other mail agents."
78 (defgroup nnmail-various nil
79 "Various mail options."
82 (defcustom nnmail-split-methods '(("mail.misc" ""))
83 "*Incoming mail will be split according to this variable.
85 If you'd like, for instance, one mail group for mail from the
86 \"4ad-l\" mailing list, one group for junk mail and one for everything
87 else, you could do something like this:
89 (setq nnmail-split-methods
90 '((\"mail.4ad\" \"From:.*4ad\")
91 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
92 (\"mail.misc\" \"\")))
94 As you can see, this variable is a list of lists, where the first
95 element in each \"rule\" is the name of the group (which, by the way,
96 does not have to be called anything beginning with \"mail\",
97 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
98 nnmail will try to match on the header to find a fit.
100 The second element can also be a function. In that case, it will be
101 called narrowed to the headers with the first element of the rule as
102 the argument. It should return a non-nil value if it thinks that the
103 mail belongs in that group.
105 The last element should always have \"\" as the regexp.
107 This variable can also have a function as its value, and it can
108 also have a fancy split method as its value. See
109 `nnmail-split-fancy' for an explanation of that syntax."
111 :type '(choice (repeat :tag "Alist" (group (string :tag "Name")
112 (choice regexp function)))
113 (function-item nnmail-split-fancy)
114 (function :tag "Other")))
116 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
117 (defcustom nnmail-crosspost t
118 "If non-nil, do crossposting if several split methods match the mail.
119 If nil, the first match found will be used."
123 (defcustom nnmail-split-fancy-with-parent-ignore-groups nil
124 "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
125 This can also be a list of regexps."
128 :type '(choice (const :tag "none" nil)
130 (repeat :value (".*") regexp)))
132 (defcustom nnmail-cache-ignore-groups nil
133 "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
134 This can also be a list of regexps."
137 :type '(choice (const :tag "none" nil)
139 (repeat :value (".*") regexp)))
141 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
142 (defcustom nnmail-keep-last-article nil
143 "If non-nil, nnmail will never delete/move a group's last article.
144 It can be marked expirable, so it will be deleted when it is no longer last.
146 You may need to set this variable if other programs are putting
147 new mail into folder numbers that Gnus has marked as expired."
148 :group 'nnmail-procmail
149 :group 'nnmail-various
152 (defcustom nnmail-use-long-file-names nil
153 "If non-nil the mail backends will use long file and directory names.
154 If nil, groups like \"mail.misc\" will end up in directories like
159 (defcustom nnmail-default-file-modes 384
160 "Set the mode bits of all new mail files to this integer."
164 (defcustom nnmail-expiry-wait 7
165 "*Expirable articles that are older than this will be expired.
166 This variable can either be a number (which will be interpreted as a
167 number of days) -- this doesn't have to be an integer. This variable
168 can also be `immediate' and `never'."
169 :group 'nnmail-expire
170 :type '(choice (const immediate)
174 (defcustom nnmail-expiry-wait-function nil
175 "Variable that holds function to specify how old articles should be before they are expired.
176 The function will be called with the name of the group that the expiry
177 is to be performed in, and it should return an integer that says how
178 many days an article can be stored before it is considered \"old\".
179 It can also return the values `never' and `immediate'.
183 \(setq nnmail-expiry-wait-function
185 (cond ((string-match \"private\" newsgroup) 31)
186 ((string-match \"junk\" newsgroup) 1)
187 ((string-match \"important\" newsgroup) 'never)
189 :group 'nnmail-expire
190 :type '(choice (const :tag "nnmail-expiry-wait" nil)
191 (function :format "%v" nnmail-)))
193 (defcustom nnmail-expiry-target 'delete
194 "*Variable that says where expired messages should end up.
195 The default value is `delete' (which says to delete the messages),
196 but it can also be a string or a function. If it is a string, expired
197 messages end up in that group. If it is a function, the function is
198 called in a buffer narrowed to the message in question. The function
199 receives one argument, the name of the group the message comes from.
200 The return value should be `delete' or a group name (a string)."
202 :group 'nnmail-expire
203 :type '(choice (const delete)
207 (defcustom nnmail-fancy-expiry-targets nil
208 "Determine expiry target based on articles using fancy techniques.
210 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
211 `nnmail-expiry-target' is set to the function
212 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
213 the message will be expired to a group determined by invoking
214 `format-time-string' with TARGET used as the format string and the
215 time extracted from the articles' Date header (if missing the current
218 In the special cases that HEADER is the symbol `to-from', the regexp
219 will try to match against both the From and the To header.
223 \(setq nnmail-fancy-expiry-targets
224 '((to-from \"boss\" \"nnfolder:Work\")
225 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
226 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
228 In this case, articles containing the string \"boss\" in the To or the
229 From header will be expired to the group \"nnfolder:Work\";
230 articles containing the string \"IMPORTANT\" in the Subject header will
231 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
232 everything else will be expired to \"nnfolder:Archive-YYYY\"."
234 :group 'nnmail-expire
235 :type '(repeat (list (choice :tag "Match against"
236 (string :tag "Header")
239 (string :tag "Target group format string"))))
241 (defcustom nnmail-cache-accepted-message-ids nil
242 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
243 If non-nil, also update the cache when copy or move articles."
247 (make-obsolete-variable 'nnmail-spool-file 'mail-sources
248 "Gnus 5.9 (Emacs 22.1)")
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" (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)
619 (defvar nnmail-inhibit-default-split-group nil)
623 (defun nnmail-request-post (&optional server)
624 (mail-send-and-exit nil))
626 (defvar nnmail-file-coding-system 'raw-text
627 "Coding system used in nnmail.")
629 (defvar nnmail-incoming-coding-system
630 mm-text-coding-system
631 "Coding system used in reading inbox")
633 (defvar nnmail-pathname-coding-system
634 ;; This causes Emacs 22.2 and 22.3 to issue a useless warning.
635 ;;(if (and (featurep 'xemacs) (featurep 'file-coding))
636 (if (featurep 'xemacs)
637 (if (featurep 'file-coding)
638 ;; Work around a bug in many XEmacs 21.5 betas.
639 ;; Cf. http://thread.gmane.org/gmane.emacs.gnus.general/68134
640 (setq file-name-coding-system (coding-system-aliasee 'file-name))))
641 "*Coding system for file name.")
643 (defun nnmail-find-file (file)
644 "Insert FILE in server buffer safely."
645 (set-buffer nntp-server-buffer)
646 (delete-region (point-min) (point-max))
647 (let ((format-alist nil)
648 (after-insert-file-functions nil))
650 (let ((coding-system-for-read nnmail-file-coding-system)
651 (auto-mode-alist (mm-auto-mode-alist))
652 (file-name-coding-system nnmail-pathname-coding-system))
653 (insert-file-contents file)
657 (defun nnmail-group-pathname (group dir &optional file)
658 "Make file name for GROUP."
660 (let ((dir (file-name-as-directory (expand-file-name dir))))
661 (setq group (nnheader-replace-duplicate-chars-in-string
662 (nnheader-replace-chars-in-string group ?/ ?_)
664 (setq group (nnheader-translate-file-chars group))
665 ;; If this directory exists, we use it directly.
666 (file-name-as-directory
667 (if (or nnmail-use-long-file-names
668 (file-directory-p (concat dir group)))
669 (expand-file-name group dir)
670 ;; If not, we translate dots into slashes.
672 (nnheader-replace-chars-in-string group ?. ?/)
676 (defun nnmail-get-active ()
677 "Returns an assoc of group names and active ranges.
678 nn*-request-list should have been called before calling this function."
679 ;; Go through all groups from the active list.
680 (with-current-buffer nntp-server-buffer
681 (nnmail-parse-active)))
683 (defun nnmail-parse-active ()
684 "Parse the active file in the current buffer and return an alist."
685 (goto-char (point-min))
686 (unless (re-search-forward "[\\\"]" nil t)
687 (goto-char (point-max))
688 (while (re-search-backward "[][';?()#]" nil t)
690 (goto-char (point-min))
691 (let ((buffer (current-buffer))
692 group-assoc group max min)
696 (narrow-to-region (point) (point-at-eol))
697 (setq group (read buffer))
698 (unless (stringp group)
699 (setq group (symbol-name group)))
700 (if (and (numberp (setq max (read buffer)))
701 (numberp (setq min (read buffer))))
702 (push (list (mm-string-as-unibyte group) (cons min max))
709 (defvar nnmail-active-file-coding-system 'raw-text
710 "*Coding system for active file.")
712 (defun nnmail-save-active (group-assoc file-name)
713 "Save GROUP-ASSOC in ACTIVE-FILE."
714 (let ((coding-system-for-write nnmail-active-file-coding-system))
716 (with-temp-file file-name
717 (mm-disable-multibyte)
718 (nnmail-generate-active group-assoc)))))
720 (defun nnmail-generate-active (alist)
721 "Generate an active file from group-alist ALIST."
724 (while (setq group (pop alist))
725 (insert (format "%S %d %d y\n" (intern (car group)) (cdadr group)
727 (goto-char (point-max))
728 (while (search-backward "\\." nil t)
731 (defun nnmail-get-split-group (file source)
732 "Find out whether this FILE is to be split into GROUP only.
733 If SOURCE is a directory spec, try to return the group name component."
734 (if (eq (car source) 'directory)
735 (let ((file (file-name-nondirectory file)))
736 (mail-source-bind (directory source)
737 (if (string-match (concat (regexp-quote suffix) "$") file)
738 (substring file 0 (match-beginning 0))
742 (defun nnmail-process-babyl-mail-format (func artnum-func)
743 (let ((case-fold-search t)
745 start message-id content-length do-search end)
747 (goto-char (point-min))
749 "
\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
750 (goto-char (match-end 0))
751 (delete-region (match-beginning 0) (match-end 0))
755 ;; Skip all the headers in case there are more "From "s...
756 (or (search-forward "\n\n" nil t)
757 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
758 (search-forward "
\1f\f"))
760 ;; Unquote the ">From " line, if any.
761 (goto-char (point-min))
762 (when (looking-at ">From ")
763 (replace-match "X-From-Line: ") )
764 (run-hooks 'nnmail-prepare-incoming-header-hook)
765 (goto-char (point-max))
766 ;; Find the Message-ID header.
768 (if (re-search-backward
769 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t)
770 (setq message-id (buffer-substring (match-beginning 1)
772 ;; There is no Message-ID here, so we create one.
774 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
776 (insert "Original-")))
778 (insert "Message-ID: " (setq message-id (nnmail-message-id))
780 ;; Look for a Content-Length header.
781 (if (not (save-excursion
782 (and (re-search-backward
783 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
784 (setq content-length (string-to-number
788 ;; We destroy the header, since none of
789 ;; the backends ever use it, and we do not
790 ;; want to confuse other mailers by having
791 ;; a (possibly) faulty header.
792 (progn (insert "X-") t))))
795 (if (or (= (+ (point) content-length) (point-max))
797 (goto-char (+ (point) content-length))
800 (goto-char (+ (point) content-length))
801 (setq do-search nil))
804 ;; Go to the beginning of the next article - or to the end
807 (if (re-search-forward "^
\1f" nil t)
808 (goto-char (match-beginning 0))
809 (goto-char (1- (point-max)))))
810 (delete-char 1) ; delete ^_
813 (narrow-to-region start (point))
814 (goto-char (point-min))
815 (nnmail-check-duplication message-id func artnum-func)
817 (setq end (point-max))))
821 (defsubst nnmail-search-unix-mail-delim ()
822 "Put point at the beginning of the next Unix mbox message."
823 ;; Algorithm used to find the next article in the
824 ;; brain-dead Unix mbox format:
826 ;; 1) Search for "^From ".
827 ;; 2) If we find it, then see whether the previous
828 ;; line is blank and the next line looks like a header.
829 ;; Then it's possible that this is a mail delim, and we use it.
830 (let ((case-fold-search nil)
833 (if (not (re-search-forward "^From " nil t))
837 (when (and (or (bobp)
840 (eq (char-after) ?\n)))
843 (while (looking-at ">From \\|From ")
845 (looking-at "[^ \n\t:]+[ \n\t]*:")))
846 (setq found 'yes)))))
850 (defun nnmail-search-unix-mail-delim-backward ()
851 "Put point at the beginning of the current Unix mbox message."
852 ;; Algorithm used to find the next article in the
853 ;; brain-dead Unix mbox format:
855 ;; 1) Search for "^From ".
856 ;; 2) If we find it, then see whether the previous
857 ;; line is blank and the next line looks like a header.
858 ;; Then it's possible that this is a mail delim, and we use it.
859 (let ((case-fold-search nil)
862 (if (not (re-search-backward "^From " nil t))
866 (when (and (or (bobp)
869 (eq (char-after) ?\n)))
872 (while (looking-at ">From \\|From ")
874 (looking-at "[^ \n\t:]+[ \n\t]*:")))
875 (setq found 'yes)))))
879 (defun nnmail-process-unix-mail-format (func artnum-func)
880 (let ((case-fold-search t)
882 start message-id content-length end skip head-end)
883 (goto-char (point-min))
884 (if (not (and (re-search-forward "^From " nil t)
885 (goto-char (match-beginning 0))))
886 ;; Possibly wrong format?
887 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
888 (if (buffer-file-name) "file" "buffer")
889 (or (buffer-file-name) (buffer-name)))
890 ;; Carry on until the bitter end.
894 ;; Find the end of the head.
897 (if (search-forward "\n\n" nil t)
899 ;; This will never happen, but just to be on the safe side --
900 ;; if there is no head-body delimiter, we search a bit manually.
901 (while (and (looking-at "From \\|[^ \t]+:")
905 ;; Find the Message-ID header.
906 (goto-char (point-min))
907 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
908 (setq message-id (match-string 1))
910 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
912 (insert "Original-")))
913 ;; There is no Message-ID here, so we create one.
915 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
916 ;; Look for a Content-Length header.
917 (goto-char (point-min))
918 (if (not (re-search-forward
919 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
920 (setq content-length nil)
921 (setq content-length (string-to-number (match-string 1)))
922 ;; We destroy the header, since none of the backends ever
923 ;; use it, and we do not want to confuse other mailers by
924 ;; having a (possibly) faulty header.
927 (run-hooks 'nnmail-prepare-incoming-header-hook)
928 ;; Find the end of this article.
929 (goto-char (point-max))
931 (setq head-end (point))
932 ;; We try the Content-Length value. The idea: skip over the header
933 ;; separator, then check what happens content-length bytes into the
934 ;; message body. This should be either the end of the buffer, the
935 ;; message separator or a blank line followed by the separator.
936 ;; The blank line should probably be deleted. If neither of the
937 ;; three is met, the content-length header is probably invalid.
940 (setq skip (+ (point) content-length))
942 (cond ((or (= skip (point-max))
943 (= (1+ skip) (point-max)))
944 (setq end (point-max)))
945 ((looking-at "From ")
947 ((looking-at "[ \t]*\n\\(From \\)")
948 (setq end (match-beginning 1)))
952 ;; No Content-Length, so we find the beginning of the next
953 ;; article or the end of the buffer.
955 (or (nnmail-search-unix-mail-delim)
956 (goto-char (point-max))))
957 ;; Allow the backend to save the article.
960 (narrow-to-region start (point))
961 (goto-char (point-min))
963 (nnmail-check-duplication message-id func artnum-func)
964 (setq end (point-max))))
968 (defun nnmail-process-mmdf-mail-format (func artnum-func &optional junk-func)
969 (let ((delim "^\^A\^A\^A\^A$")
972 start message-id end)
973 (goto-char (point-min))
974 (if (not (and (re-search-forward delim nil t)
976 ;; Possibly wrong format?
977 (error "Error, unknown mail format! (Possibly corrupted.)")
978 ;; Carry on until the bitter end.
981 ;; Find the end of the head.
984 (if (search-forward "\n\n" nil t)
986 ;; This will never happen, but just to be on the safe side --
987 ;; if there is no head-body delimiter, we search a bit manually.
988 (while (and (looking-at "From \\|[^ \t]+:")
992 ;; Find the Message-ID header.
993 (goto-char (point-min))
994 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
995 (setq message-id (match-string 1))
996 ;; There is no Message-ID here, so we create one.
998 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
1000 (insert "Original-")))
1002 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
1003 (run-hooks 'nnmail-prepare-incoming-header-hook)
1004 ;; Find the end of this article.
1005 (goto-char (point-max))
1007 (if (re-search-forward delim nil t)
1009 (goto-char (point-max)))
1010 ;; Allow the backend to save the article.
1013 (narrow-to-region start (point))
1014 (goto-char (point-min))
1016 (nnmail-check-duplication message-id func artnum-func junk-func)
1017 (setq end (point-max))))
1022 (defun nnmail-process-maildir-mail-format (func artnum-func)
1023 ;; In a maildir, every file contains exactly one mail.
1024 (let ((case-fold-search t)
1026 (goto-char (point-min))
1027 ;; Find the end of the head.
1030 (if (search-forward "\n\n" nil t)
1032 ;; This will never happen, but just to be on the safe side --
1033 ;; if there is no head-body delimiter, we search a bit manually.
1034 (while (and (looking-at "From \\|[^ \t]+:")
1038 ;; Find the Message-ID header.
1039 (goto-char (point-min))
1040 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
1041 (setq message-id (match-string 1))
1042 ;; There is no Message-ID here, so we create one.
1044 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
1046 (insert "Original-")))
1048 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
1049 (run-hooks 'nnmail-prepare-incoming-header-hook)
1050 ;; Allow the backend to save the article.
1053 (goto-char (point-min))
1054 (nnmail-check-duplication message-id func artnum-func))
1057 (defvar nnmail-group-names-not-encoded-p nil
1058 "Non-nil means group names are not encoded.")
1060 (defun nnmail-split-incoming (incoming func &optional exit-func
1061 group artnum-func junk-func)
1062 "Go through the entire INCOMING file and pick out each individual mail.
1063 FUNC will be called with the buffer narrowed to each mail.
1064 INCOMING can also be a buffer object. In that case, the mail
1065 will be copied over from that buffer."
1066 (let ( ;; If this is a group-specific split, we bind the split
1067 ;; methods to just this group.
1068 (nnmail-split-methods (if (and group
1069 (not nnmail-resplit-incoming))
1070 (list (list group ""))
1071 nnmail-split-methods))
1072 (nnmail-group-names-not-encoded-p t))
1073 ;; Insert the incoming file.
1074 (with-current-buffer (get-buffer-create nnmail-article-buffer)
1076 (if (bufferp incoming)
1077 (insert-buffer-substring incoming)
1078 (let ((coding-system-for-read nnmail-incoming-coding-system))
1079 (mm-insert-file-contents incoming)))
1081 (if (zerop (buffer-size))
1083 (goto-char (point-min))
1084 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
1085 ;; Handle both babyl, MMDF and unix mail formats, since
1086 ;; movemail will use the former when fetching from a
1087 ;; mailbox, the latter when fetching from a file.
1088 (cond ((or (looking-at "\^L")
1089 (looking-at "BABYL OPTIONS:"))
1090 (nnmail-process-babyl-mail-format func artnum-func))
1091 ((looking-at "\^A\^A\^A\^A")
1092 (nnmail-process-mmdf-mail-format
1093 func artnum-func junk-func))
1094 ((looking-at "Return-Path:")
1095 (nnmail-process-maildir-mail-format func artnum-func))
1097 (nnmail-process-unix-mail-format func artnum-func))))
1099 (funcall exit-func))
1100 (kill-buffer (current-buffer))))))
1102 (defun nnmail-article-group (func &optional trace junk-func)
1103 "Look at the headers and return an alist of groups that match.
1104 FUNC will be called with the group name to determine the article number."
1105 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
1106 (obuf (current-buffer))
1107 group-art method grp)
1108 (if (and (sequencep methods)
1109 (= (length methods) 1)
1110 (not nnmail-inhibit-default-split-group))
1111 ;; If there is only just one group to put everything in, we
1112 ;; just return a list with just this one method in.
1114 (list (cons (caar methods) (funcall func (caar methods)))))
1115 ;; We do actual comparison.
1116 ;; Copy the article into the work buffer.
1117 (with-current-buffer nntp-server-buffer
1119 (insert-buffer-substring obuf)
1120 ;; Narrow to headers.
1122 (goto-char (point-min))
1123 (if (search-forward "\n\n" nil t)
1126 (goto-char (point-min))
1127 ;; Decode MIME headers and charsets.
1128 (when nnmail-mail-splitting-decodes
1129 (let ((mail-parse-charset nnmail-mail-splitting-charset))
1130 (mail-decode-encoded-word-region (point-min) (point-max))))
1131 ;; Fold continuation lines.
1132 (goto-char (point-min))
1133 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1134 (replace-match " " t t))
1135 ;; Nuke pathologically long headers. Since Gnus applies
1136 ;; pathologically complex regexps to the buffer, lines
1137 ;; that are looong will take longer than the Universe's
1138 ;; existence to process.
1139 (goto-char (point-min))
1141 (unless (< (move-to-column nnmail-split-header-length-limit)
1142 nnmail-split-header-length-limit)
1143 (delete-region (point) (point-at-eol)))
1146 (goto-char (point-min))
1147 (run-hooks 'nnmail-split-hook)
1148 (when (setq nnmail-split-tracing trace)
1149 (setq nnmail-split-trace nil))
1150 (if (or (and (symbolp nnmail-split-methods)
1151 (fboundp nnmail-split-methods))
1152 (and (listp nnmail-split-methods)
1153 ;; Not a regular split method, so it has to be a
1155 (not (let ((top-element (car-safe nnmail-split-methods)))
1156 (and (= 2 (length top-element))
1157 (stringp (nth 0 top-element))
1158 (stringp (nth 1 top-element)))))))
1159 (let* ((method-function
1160 (if (and (symbolp nnmail-split-methods)
1161 (fboundp nnmail-split-methods))
1162 nnmail-split-methods
1163 'nnmail-split-fancy))
1165 (condition-case error-info
1166 ;; `nnmail-split-methods' is a function, so we
1167 ;; just call this function here and use the
1169 (or (funcall method-function)
1170 (and (not nnmail-inhibit-default-split-group)
1174 5 "Error in `nnmail-split-methods'; using `bogus' mail group: %S" error-info)
1177 (setq split (mm-delete-duplicates split))
1178 ;; The article may be "cross-posted" to `junk'. What
1179 ;; to do? Just remove the `junk' spec. Don't really
1180 ;; see anything else to do...
1181 (when (and (memq 'junk split)
1183 (funcall junk-func 'junk))
1184 (setq split (delq 'junk split))
1188 (lambda (group) (cons group (funcall func group)))
1190 ;; Go through the split methods to find a match.
1192 (or nnmail-crosspost
1194 (goto-char (point-max))
1195 (setq method (pop methods)
1198 (not (equal "" (nth 1 method))))
1201 (if (stringp (nth 1 method))
1202 (let ((expand (string-match "\\\\[0-9&]" grp))
1203 (pos (re-search-backward (cadr method)
1206 (setq grp (nnmail-expand-newtext grp)))
1208 ;; Function to say whether this is a match.
1209 (funcall (nth 1 method) grp)))
1210 ;; Don't enter the article into the same
1212 (not (assoc grp group-art)))
1213 (push (cons grp (funcall func grp))
1215 ;; This is the final group, which is used as a
1217 (when (and (not group-art)
1218 (not nnmail-inhibit-default-split-group))
1220 (list (cons (car method)
1221 (funcall func (car method))))))))
1222 ;; Fall back on "bogus" if all else fails.
1223 (when (and (not group-art)
1224 (not nnmail-inhibit-default-split-group))
1225 (setq group-art (list (cons "bogus" (funcall func "bogus"))))))
1226 ;; Produce a trace if non-empty.
1227 (when (and trace nnmail-split-trace)
1228 (let ((restore (current-buffer)))
1229 (nnheader-set-temp-buffer "*Split Trace*")
1231 (dolist (trace (nreverse nnmail-split-trace))
1232 (prin1 trace (current-buffer))
1234 (goto-char (point-min))
1235 (gnus-configure-windows 'split-trace)
1236 (set-buffer restore)))
1238 ;; See whether the split methods returned `junk'.
1239 (if (equal group-art '(junk))
1241 ;; The article may be "cross-posted" to `junk'. What
1242 ;; to do? Just remove the `junk' spec. Don't really
1243 ;; see anything else to do...
1245 (while (setq elem (car (memq 'junk group-art)))
1246 (setq group-art (delq elem group-art)))
1247 (nreverse group-art)))))))
1249 (defun nnmail-insert-lines ()
1250 "Insert how many lines there are in the body of the mail.
1251 Return the number of characters in the body."
1254 (goto-char (point-min))
1255 (unless (search-forward "\n\n" nil t)
1256 (goto-char (point-max))
1258 (setq chars (- (point-max) (point)))
1259 (setq lines (count-lines (point) (point-max)))
1262 (when (re-search-backward "^Lines: " nil t)
1263 (delete-region (point) (progn (forward-line 1) (point)))))
1265 (insert (format "Lines: %d\n" (max lines 0)))
1268 (defun nnmail-insert-xref (group-alist)
1269 "Insert an Xref line based on the (group . article) alist."
1271 (goto-char (point-min))
1272 (unless (search-forward "\n\n" nil t)
1273 (goto-char (point-max))
1276 (when (re-search-backward "^Xref: " nil t)
1277 (delete-region (match-beginning 0)
1278 (progn (forward-line 1) (point))))
1279 (insert (format "Xref: %s" (system-name)))
1281 (insert (if (mm-multibyte-p)
1282 (mm-string-as-multibyte
1283 (format " %s:%d" (caar group-alist) (cdar group-alist)))
1284 (mm-string-as-unibyte
1285 (format " %s:%d" (caar group-alist) (cdar group-alist)))))
1286 (setq group-alist (cdr group-alist)))
1289 ;;; Message washing functions
1291 (defun nnmail-remove-leading-whitespace ()
1292 "Remove excessive whitespace from all headers."
1293 (goto-char (point-min))
1294 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1295 (replace-match "\\1" t)))
1297 (defun nnmail-remove-list-identifiers ()
1298 "Remove list identifiers from Subject headers."
1300 (if (consp nnmail-list-identifiers)
1301 (mapconcat 'identity nnmail-list-identifiers " *\\|")
1302 nnmail-list-identifiers)))
1304 (goto-char (point-min))
1305 (while (re-search-forward
1306 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1308 (delete-region (match-beginning 2) (match-end 0))
1309 (beginning-of-line))
1310 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1312 (delete-region (match-beginning 1) (match-end 1))
1313 (beginning-of-line)))))
1315 (defun nnmail-remove-tabs ()
1316 "Translate TAB characters into SPACE characters."
1317 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1319 (defcustom nnmail-broken-references-mailers
1320 "^X-Mailer:.*\\(Eudora\\|Pegasus\\)"
1321 "Header line matching mailer producing bogus References lines.
1322 See `nnmail-ignore-broken-references'."
1323 :group 'nnmail-prepare
1324 :version "23.1" ;; No Gnus
1327 (defun nnmail-ignore-broken-references ()
1328 "Ignore the References line and use In-Reply-To
1330 Eudora has a broken References line, but an OK In-Reply-To."
1331 (goto-char (point-min))
1332 (when (re-search-forward nnmail-broken-references-mailers nil t)
1333 (goto-char (point-min))
1334 (when (re-search-forward "^References:" nil t)
1336 (insert "X-Gnus-Broken-Eudora-"))
1337 (goto-char (point-min))
1338 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1339 (replace-match "\\1" t))))
1341 (defalias 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references)
1342 (make-obsolete 'nnmail-fix-eudora-headers 'nnmail-ignore-broken-references "Emacs 23.1")
1344 (custom-add-option 'nnmail-prepare-incoming-header-hook
1345 'nnmail-ignore-broken-references)
1347 ;;; Utility functions
1349 (declare-function gnus-activate-group "gnus-start"
1350 (group &optional scan dont-check method))
1352 (defun nnmail-do-request-post (accept-func &optional server)
1353 "Utility function to directly post a message to an nnmail-derived group.
1354 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1355 to actually put the message in the right group."
1357 (dolist (mbx (message-unquote-tokens
1358 (message-tokenize-header
1359 (message-fetch-field "Newsgroups") ", ")) success)
1360 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1361 (or (gnus-active to-newsgroup)
1362 (gnus-activate-group to-newsgroup)
1363 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1365 (or (and (gnus-request-create-group
1366 to-newsgroup gnus-command-method)
1367 (gnus-activate-group to-newsgroup nil nil
1368 gnus-command-method))
1369 (error "Couldn't create group %s" to-newsgroup)))
1370 (error "No such group: %s" to-newsgroup))
1371 (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1372 (setq success nil))))))
1374 (defun nnmail-split-fancy ()
1375 "Fancy splitting method.
1376 See the documentation for the variable `nnmail-split-fancy' for details."
1377 (with-syntax-table nnmail-split-fancy-syntax-table
1378 (nnmail-split-it nnmail-split-fancy)))
1380 (defvar nnmail-split-cache nil)
1381 ;; Alist of split expressions their equivalent regexps.
1383 (defun nnmail-split-it (split)
1384 ;; Return a list of groups matching SPLIT.
1391 ;; A group name. Do the \& and \N subs into the string.
1393 (when nnmail-split-tracing
1394 (push split nnmail-split-trace))
1395 (list (nnmail-expand-newtext split)))
1397 ;; Junk the message.
1399 (when nnmail-split-tracing
1400 (push "junk" nnmail-split-trace))
1403 ;; Builtin & operation.
1404 ((eq (car split) '&)
1405 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1407 ;; Builtin | operation.
1408 ((eq (car split) '|)
1410 (while (and (not done) (cdr split))
1411 (setq split (cdr split)
1412 done (nnmail-split-it (car split))))
1415 ;; Builtin : operation.
1416 ((eq (car split) ':)
1417 (when nnmail-split-tracing
1418 (push split nnmail-split-trace))
1419 (nnmail-split-it (save-excursion (eval (cdr split)))))
1421 ;; Builtin ! operation.
1422 ((eq (car split) '!)
1423 (funcall (cadr split) (nnmail-split-it (caddr split))))
1425 ;; Check the cache for the regexp for this split.
1426 ((setq cached-pair (assq split nnmail-split-cache))
1428 (end-point (point-max))
1429 (value (nth 1 split)))
1431 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1432 (while (and (goto-char end-point)
1433 (re-search-backward (cdr cached-pair) nil t))
1434 (when nnmail-split-tracing
1435 (push split nnmail-split-trace))
1436 (let ((split-rest (cddr split))
1438 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1439 ;; So, start-of-value is the point just before the
1440 ;; beginning of the value, whereas after-header-name
1441 ;; is the point just after the field name.
1442 (start-of-value (match-end 1))
1443 (after-header-name (match-end 2)))
1444 ;; Start the next search just before the beginning of the
1446 (setq end-point (1- start-of-value))
1447 ;; Handle - RESTRICTs
1448 (while (eq (car split-rest) '-)
1449 ;; RESTRICT must start after-header-name and
1450 ;; end after start-of-value, so that, for
1451 ;; (any "foo" - "x-foo" "foo.list")
1452 ;; we do not exclude foo.list just because
1453 ;; the header is: ``To: x-foo, foo''
1455 (if (and (re-search-backward (cadr split-rest)
1456 after-header-name t)
1457 (> (match-end 0) start-of-value))
1458 (setq split-rest nil)
1459 (setq split-rest (cddr split-rest))))
1462 (let ((value (nth 1 split)))
1464 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1465 ;; Someone might want to do a \N sub on this match, so get the
1466 ;; correct match positions.
1467 (re-search-backward value start-of-value))
1468 (dolist (sp (nnmail-split-it (car split-rest)))
1469 (unless (member sp split-result)
1470 (push sp split-result))))))
1473 ;; Not in cache, compute a regexp for the field/value pair.
1475 (let ((field (nth 0 split))
1476 (value (nth 1 split))
1477 (split-rest (cddr split))
1482 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1483 (if (and (>= (length value) 2)
1484 (string= ".*" (substring value 0 2)))
1485 (setq value (substring value 2)
1487 ;; Same trick for the rear of the regexp
1488 (if (and (>= (length value) 2)
1489 (string= ".*" (substring value -2)))
1490 (setq value (substring value 0 -2)
1492 ;; Invert the match-partial-words behavior if the optional
1493 ;; last element is specified.
1494 (while (eq (car split-rest) '-)
1495 (setq split-rest (cddr split-rest)))
1496 (when (if (cadr split-rest)
1497 (not nnmail-split-fancy-match-partial-words)
1498 nnmail-split-fancy-match-partial-words)
1499 (setq partial-front ""
1501 (setq regexp (concat "^\\(\\("
1503 (cdr (assq field nnmail-split-abbrev-alist))
1506 (or partial-front "\\<")
1510 (or partial-rear "\\>")))
1511 (push (cons split regexp) nnmail-split-cache)
1512 ;; Now that it's in the cache, just call nnmail-split-it again
1513 ;; on the same split, which will find it immediately in the cache.
1514 (nnmail-split-it split))))))
1516 (defun nnmail-expand-newtext (newtext)
1517 (let ((len (length newtext))
1519 c expanded beg N did-expand)
1522 (while (and (< pos len)
1523 (not (= (aref newtext pos) ?\\)))
1524 (setq pos (1+ pos)))
1526 (push (substring newtext beg pos) expanded))
1528 ;; We hit a \; expand it.
1531 c (aref newtext pos))
1532 (if (not (or (= c ?\&)
1535 ;; \ followed by some character we don't expand.
1536 (push (char-to-string c) expanded)
1541 (when (match-beginning N)
1542 (push (if nnmail-split-lowercase-expanded
1543 (downcase (buffer-substring (match-beginning N)
1545 (buffer-substring (match-beginning N) (match-end N)))
1547 (setq pos (1+ pos)))
1549 (apply 'concat (nreverse expanded))
1552 ;; Activate a backend only if it isn't already activated.
1553 ;; If FORCE, re-read the active file even if the backend is
1554 ;; already activated.
1555 (defun nnmail-activate (backend &optional force)
1556 (nnheader-init-server-buffer)
1557 (let (file timestamp file-time)
1558 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1560 (and (setq file (ignore-errors
1561 (symbol-value (intern (format "%s-active-file"
1563 (setq file-time (nth 5 (file-attributes file)))
1567 (symbol-value (intern
1568 (format "%s-active-timestamp"
1571 (not (consp timestamp))
1572 (equal timestamp '(0 0))
1573 (> (nth 0 file-time) (nth 0 timestamp))
1574 (and (= (nth 0 file-time) (nth 0 timestamp))
1575 (> (nth 1 file-time) (nth 1 timestamp))))))
1577 (or (eq timestamp 'none)
1578 (set (intern (format "%s-active-timestamp" backend))
1580 (funcall (intern (format "%s-request-list" backend)))))
1583 (defun nnmail-message-id ()
1584 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1587 ;;; nnmail duplicate handling
1590 (defvar nnmail-cache-buffer nil)
1592 (defun nnmail-cache-open ()
1593 (if (or (not nnmail-treat-duplicates)
1594 (and nnmail-cache-buffer
1595 (buffer-name nnmail-cache-buffer)))
1596 () ; The buffer is open.
1597 (with-current-buffer
1598 (setq nnmail-cache-buffer
1599 (get-buffer-create " *nnmail message-id cache*"))
1601 (when (file-exists-p nnmail-message-id-cache-file)
1602 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1603 (set-buffer-modified-p nil)
1606 (defun nnmail-cache-close ()
1607 (when (and nnmail-cache-buffer
1608 nnmail-treat-duplicates
1609 (buffer-name nnmail-cache-buffer)
1610 (buffer-modified-p nnmail-cache-buffer))
1611 (with-current-buffer nnmail-cache-buffer
1612 ;; Weed out the excess number of Message-IDs.
1613 (goto-char (point-max))
1614 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1617 (delete-region (point-min) (point))))
1619 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1620 (make-directory (file-name-directory nnmail-message-id-cache-file)
1622 (nnmail-write-region (point-min) (point-max)
1623 nnmail-message-id-cache-file nil 'silent)
1624 (set-buffer-modified-p nil)
1625 (setq nnmail-cache-buffer nil)
1626 (gnus-kill-buffer (current-buffer)))))
1628 (defun nnmail-cache-insert (id grp &optional subject sender)
1630 ;; this will handle cases like `B r' where the group is nil
1631 (let ((grp (or grp gnus-newsgroup-name "UNKNOWN")))
1632 (run-hook-with-args 'nnmail-spool-hook
1633 id grp subject sender))
1634 (when nnmail-treat-duplicates
1635 ;; Store some information about the group this message is written
1636 ;; to. This is passed in as the grp argument -- all locations this
1637 ;; has been called from have been checked and the group is available.
1638 ;; The only ambiguous case is nnmail-check-duplication which will only
1639 ;; pass the first (of possibly >1) group which matches. -Josh
1640 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1641 (nnmail-cache-open))
1642 (with-current-buffer nnmail-cache-buffer
1643 (goto-char (point-max))
1644 (if (and grp (not (string= "" grp))
1645 (gnus-methods-equal-p gnus-command-method
1646 (nnmail-cache-primary-mail-backend)))
1647 (let ((regexp (if (consp nnmail-cache-ignore-groups)
1648 (mapconcat 'identity nnmail-cache-ignore-groups
1650 nnmail-cache-ignore-groups)))
1651 (unless (and regexp (string-match regexp grp))
1652 (insert id "\t" grp "\n")))
1653 (insert id "\n"))))))
1655 (defun nnmail-cache-primary-mail-backend ()
1656 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1660 (while (and (null res) be-list)
1661 (setq be (car be-list))
1662 (setq be-list (cdr be-list))
1663 (when (and (gnus-method-option-p be 'respool)
1665 (intern (format "%s-get-new-mail" (car be))))
1666 (boundp get-new-mail)
1667 (symbol-value get-new-mail))
1671 ;; Fetch the group name corresponding to the message id stored in the
1673 (defun nnmail-cache-fetch-group (id)
1674 (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1675 (with-current-buffer nnmail-cache-buffer
1676 (goto-char (point-max))
1677 (when (search-backward id nil t)
1679 (skip-chars-forward "^\n\r\t")
1680 (unless (looking-at "[\r\n]")
1682 (buffer-substring (point) (point-at-eol)))))))
1684 ;; Function for nnmail-split-fancy: look up all references in the
1685 ;; cache and if a match is found, return that group.
1686 (defun nnmail-split-fancy-with-parent ()
1687 "Split this message into the same group as its parent.
1688 This function can be used as an entry in `nnmail-split-fancy', for
1689 example like this: (: nnmail-split-fancy-with-parent)
1690 For a message to be split, it looks for the parent message in the
1691 References or In-Reply-To header and then looks in the message id
1692 cache file (given by the variable `nnmail-message-id-cache-file') to
1693 see which group that message was put in. This group is returned.
1695 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1696 (let* ((refstr (or (message-fetch-field "references")
1697 (message-fetch-field "in-reply-to")))
1700 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1702 (lambda (x) (format "\\(%s\\)" x))
1703 nnmail-split-fancy-with-parent-ignore-groups
1705 nnmail-split-fancy-with-parent-ignore-groups)))
1707 (setq references (nreverse (gnus-split-references refstr)))
1708 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1709 (nnmail-cache-open))
1710 (dolist (x references)
1711 (setq res (or (nnmail-cache-fetch-group x) res))
1712 (when (or (member res '("delayed" "drafts" "queue"))
1713 (and regexp res (string-match regexp res)))
1717 (defun nnmail-cache-id-exists-p (id)
1718 (when nnmail-treat-duplicates
1719 (with-current-buffer nnmail-cache-buffer
1720 (goto-char (point-max))
1721 (search-backward id nil t))))
1723 (defun nnmail-fetch-field (header)
1726 (message-narrow-to-head)
1727 (message-fetch-field header))))
1729 (defun nnmail-check-duplication (message-id func artnum-func
1730 &optional junk-func)
1731 (run-hooks 'nnmail-prepare-incoming-message-hook)
1732 ;; If this is a duplicate message, then we do not save it.
1733 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1734 (case-fold-search t)
1735 (action (when duplication
1737 ((memq nnmail-treat-duplicates '(warn delete))
1738 nnmail-treat-duplicates)
1739 ((functionp nnmail-treat-duplicates)
1740 (funcall nnmail-treat-duplicates message-id))
1742 nnmail-treat-duplicates))))
1744 ;; We insert a line that says what the mail source is.
1745 (let ((case-fold-search t))
1746 (goto-char (point-min))
1747 (re-search-forward "^message-id[ \t]*:" nil t)
1749 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1751 ;; Let the backend save the article (or not).
1754 (funcall func (setq group-art
1755 (nreverse (nnmail-article-group
1756 artnum-func nil junk-func))))
1757 (nnmail-cache-insert message-id (caar group-art)))
1758 ((eq action 'delete)
1759 (setq group-art nil))
1761 ;; We insert a warning.
1762 (let ((case-fold-search t))
1763 (goto-char (point-min))
1764 (re-search-forward "^message-id[ \t]*:" nil t)
1767 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1768 (funcall func (setq group-art
1769 (nreverse (nnmail-article-group artnum-func))))))
1771 (funcall func (setq group-art
1772 (nreverse (nnmail-article-group artnum-func))))))
1773 ;; Add the group-art list to the history list.
1775 (push group-art nnmail-split-history)
1776 (delete-region (point-min) (point-max)))))
1780 (defvar nnmail-fetched-sources nil)
1782 (defun nnmail-get-value (&rest args)
1783 (let ((sym (intern (apply 'format args))))
1785 (symbol-value sym))))
1787 (defun nnmail-get-new-mail (method exit-func temp
1788 &optional group spool-func)
1789 "Read new incoming mail."
1790 (nnmail-get-new-mail-1 method exit-func temp group nil spool-func))
1792 (defun nnmail-get-new-mail-1 (method exit-func temp
1793 group in-group spool-func)
1794 (let* ((sources mail-sources)
1799 incoming incomings source)
1800 (when (and (nnmail-get-value "%s-get-new-mail" method)
1802 (while (setq source (pop sources))
1803 ;; Use group's parameter
1804 (when (eq (car source) 'group)
1807 (gnus-group-find-parameter
1808 (concat (symbol-name method) ":" group)
1810 (nnmail-get-new-mail-1 method exit-func temp
1811 group group spool-func))
1813 ;; Hack to only fetch the contents of a single group's spool file.
1814 (when (and (eq (car source) 'directory)
1815 (null nnmail-scan-directory-mail-source-once)
1817 (mail-source-bind (directory source)
1818 (setq source (append source
1824 ,(concat group suffix)
1825 (file-name-nondirectory file)))))))))
1826 (when nnmail-fetched-sources
1827 (if (member source nnmail-fetched-sources)
1829 (push source nnmail-fetched-sources)
1830 (push source fetching-sources)))))
1831 (when fetching-sources
1832 ;; We first activate all the groups.
1833 (nnmail-activate method)
1834 ;; Allow the user to hook.
1835 (run-hooks 'nnmail-pre-get-new-mail-hook)
1836 ;; Open the message-id cache.
1838 ;; The we go through all the existing mail source specification
1839 ;; and fetch the mail from each.
1840 (while (setq source (pop fetching-sources))
1845 `(lambda (file orig-file)
1846 (nnmail-split-incoming
1847 file ',(intern (format "%s-save-mail" method))
1850 (if (equal file orig-file)
1852 (nnmail-get-split-group orig-file ',source)))
1853 ',(intern (format "%s-active-number" method)))))))
1856 ;; If we did indeed read any incoming spools, we save all info.
1858 (when mail-source-plugged
1859 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1860 method (car source)))
1862 (nnmail-get-value "%s-group-alist" method)
1863 (nnmail-get-value "%s-active-file" method))
1865 (funcall exit-func))
1866 (run-hooks 'nnmail-read-incoming-hook)
1867 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1869 ;; Close the message-id cache.
1870 (nnmail-cache-close)
1871 ;; Allow the user to hook.
1872 (run-hooks 'nnmail-post-get-new-mail-hook))))
1874 (defun nnmail-expired-article-p (group time force &optional inhibit)
1875 "Say whether an article that is TIME old in GROUP should be expired.
1876 If TIME is nil, then return the cutoff time for oldness instead."
1881 (let ((days (or (and nnmail-expiry-wait-function
1882 (funcall nnmail-expiry-wait-function group))
1883 nnmail-expiry-wait)))
1884 (cond ((or (eq days 'never)
1887 ;; This isn't an expirable group.
1889 ((eq days 'immediate)
1890 ;; We expire all articles on sight.
1894 ((equal time '(0 0))
1895 ;; This is an ange-ftp group, and we don't have any dates.
1898 (setq days (days-to-time days))
1899 ;; Compare the time with the current time.
1901 (time-subtract (current-time) days)
1902 (ignore-errors (time-less-p days (time-since time)))))))))
1904 (declare-function gnus-group-mark-article-read "gnus-group" (group article))
1906 (defun nnmail-expiry-target-group (target group)
1907 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1908 ;; that buffer if the nnfolder group isn't selected.
1909 (let (nnmail-cache-accepted-message-ids)
1910 ;; Don't enter Message-IDs into cache.
1911 ;; Let users hack it in TARGET function.
1912 (when (functionp target)
1913 (setq target (funcall target group)))
1914 (unless (eq target 'delete)
1915 (when (or (gnus-request-group target)
1916 (gnus-request-create-group target))
1917 (let ((group-art (gnus-request-accept-article target nil t t)))
1918 (when (consp group-art)
1919 (gnus-group-mark-article-read target (cdr group-art))))))))
1921 (defun nnmail-fancy-expiry-target (group)
1922 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1924 (case-fold-search nil)
1925 (from (or (message-fetch-field "from") ""))
1926 (to (or (message-fetch-field "to") ""))
1927 (date (message-fetch-field "date"))
1933 (message "%s" (error-message-string err))
1936 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1937 (setq header (car regexp-target-pair))
1939 ;; If the header is to-from then match against the
1940 ;; To or From header
1941 ((and (equal header 'to-from)
1942 (or (string-match (cadr regexp-target-pair) from)
1943 (and (string-match (cadr regexp-target-pair) to)
1944 (let ((rmail-dont-reply-to-names
1945 (message-dont-reply-to-names)))
1946 (equal (rmail-dont-reply-to from) "")))))
1947 (setq target (format-time-string (caddr regexp-target-pair) date)))
1948 ((and (not (equal header 'to-from))
1949 (string-match (cadr regexp-target-pair)
1951 (message-fetch-field header)
1954 (format-time-string (caddr regexp-target-pair) date)))))))
1956 (defun nnmail-check-syntax ()
1957 "Check (and modify) the syntax of the message in the current buffer."
1959 (message-narrow-to-head)
1960 (let ((case-fold-search t))
1961 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1962 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1964 (defun nnmail-write-region (start end filename &optional append visit lockname)
1965 "Do a `write-region', and then set the file modes."
1966 (let ((coding-system-for-write nnmail-file-coding-system)
1967 (file-name-coding-system nnmail-pathname-coding-system))
1968 (write-region start end filename append visit lockname)
1969 (set-file-modes filename nnmail-default-file-modes)))
1972 ;;; Status functions
1975 (defun nnmail-replace-status (name value)
1976 "Make status NAME and VALUE part of the current status line."
1978 (message-narrow-to-head)
1979 (let ((status (nnmail-decode-status)))
1980 (setq status (delq (member name status) status))
1982 (push (cons name value) status))
1983 (message-remove-header "status")
1984 (goto-char (point-max))
1985 (insert "Status: " (nnmail-encode-status status) "\n"))))
1987 (defun nnmail-decode-status ()
1988 "Return a status-value alist from STATUS."
1989 (goto-char (point-min))
1990 (when (re-search-forward "^Status: " nil t)
1991 (let (name value status)
1993 ;; Narrow to the status.
1996 (if (re-search-forward "^[^ \t]" nil t)
1999 ;; Go through all elements and add them to the list.
2000 (goto-char (point-min))
2001 (while (re-search-forward "[^ \t=]+" nil t)
2002 (setq name (match-string 0))
2003 (if (not (eq (char-after) ?=))
2007 (if (not (eq (char-after) ?\"))
2008 (if (not (looking-at "[^ \t]"))
2012 (setq value (match-string 0))
2013 (goto-char (match-end 0)))
2015 (setq value (read (current-buffer)))))
2016 (push (cons name value) status)))
2019 (defun nnmail-encode-status (status)
2020 "Return a status string from STATUS."
2025 (if (string-match "[ \t]" (cdr elem))
2026 (prin1-to-string (cdr elem))
2030 (defun nnmail-split-history ()
2031 "Generate an overview of where the last mail split put articles."
2033 (unless nnmail-split-history
2034 (error "No current split history"))
2035 (with-output-to-temp-buffer "*nnmail split history*"
2036 (with-current-buffer standard-output
2037 (fundamental-mode)) ; for Emacs 20.4+
2038 (dolist (elem nnmail-split-history)
2039 (princ (mapconcat (lambda (ga)
2040 (concat (car ga) ":" (int-to-string (cdr ga))))
2045 (defun nnmail-purge-split-history (group)
2046 "Remove all instances of GROUP from `nnmail-split-history'."
2047 (let ((history nnmail-split-history))
2049 (setcar history (gnus-remove-if (lambda (e) (string= (car e) group))
2052 (setq nnmail-split-history (delq nil nnmail-split-history))))
2054 (defun nnmail-new-mail-p (group)
2055 "Say whether GROUP has new mail."
2056 (let ((his nnmail-split-history)
2059 (when (assoc group (pop his))
2064 (defun nnmail-within-headers-p ()
2065 "Check to see if point is within the headers of a unix mail message.
2066 Doesn't change point."
2067 (let ((pos (point)))
2069 (and (nnmail-search-unix-mail-delim-backward)
2070 (not (search-forward "\n\n" pos t))))))
2072 (run-hooks 'nnmail-load-hook)
2076 ;;; nnmail.el ends here