1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
3 ;; Copyright (C) 1995-2016 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
29 (require 'gnus
) ; for macro gnus-kill-buffer, at least
33 (require 'mail-source
)
37 (autoload 'gnus-add-buffer
"gnus")
38 (autoload 'gnus-kill-buffer
"gnus")
39 (autoload 'mail-send-and-exit
"sendmail" nil t
)
42 "Reading mail with Gnus."
45 (defgroup nnmail-retrieve nil
46 "Retrieving new mail."
49 (defgroup nnmail-prepare nil
50 "Preparing (or mangling) new mail after retrieval."
53 (defgroup nnmail-duplicate nil
54 "Handling of duplicate mail messages."
57 (defgroup nnmail-split nil
58 "Organizing the incoming mail in folders."
61 (defgroup nnmail-files nil
66 (defgroup nnmail-expire nil
70 (defgroup nnmail-procmail nil
71 "Interfacing with procmail and other mail agents."
74 (defgroup nnmail-various nil
75 "Various mail options."
78 (defcustom nnmail-split-methods
'(("mail.misc" ""))
79 "*Incoming mail will be split according to this variable.
81 If you'd like, for instance, one mail group for mail from the
82 \"4ad-l\" mailing list, one group for junk mail and one for everything
83 else, you could do something like this:
85 (setq nnmail-split-methods
86 \\='((\"mail.4ad\" \"From:.*4ad\")
87 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
88 (\"mail.misc\" \"\")))
90 As you can see, this variable is a list of lists, where the first
91 element in each \"rule\" is the name of the group (which, by the way,
92 does not have to be called anything beginning with \"mail\",
93 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
94 nnmail will try to match on the header to find a fit.
96 The second element can also be a function. In that case, it will be
97 called narrowed to the headers with the first element of the rule as
98 the argument. It should return a non-nil value if it thinks that the
99 mail belongs in that group.
101 The last element should always have \"\" as the regexp.
103 This variable can also have a function as its value, and it can
104 also have a fancy split method as its value. See
105 `nnmail-split-fancy' for an explanation of that syntax."
107 :type
'(choice (repeat :tag
"Alist" (group (string :tag
"Name")
108 (choice regexp function
)))
109 (function-item nnmail-split-fancy
)
110 (function :tag
"Other")))
112 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
113 (defcustom nnmail-crosspost t
114 "If non-nil, do crossposting if several split methods match the mail.
115 If nil, the first match found will be used."
119 (defcustom nnmail-split-fancy-with-parent-ignore-groups nil
120 "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
121 This can also be a list of regexps."
124 :type
'(choice (const :tag
"none" nil
)
126 (repeat :value
(".*") regexp
)))
128 (defcustom nnmail-cache-ignore-groups nil
129 "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
130 This can also be a list of regexps."
133 :type
'(choice (const :tag
"none" nil
)
135 (repeat :value
(".*") regexp
)))
137 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
138 (defcustom nnmail-keep-last-article nil
139 "If non-nil, nnmail will never delete/move a group's last article.
140 It can be marked expirable, so it will be deleted when it is no longer last.
142 You may need to set this variable if other programs are putting
143 new mail into folder numbers that Gnus has marked as expired."
144 :group
'nnmail-procmail
145 :group
'nnmail-various
148 (defcustom nnmail-use-long-file-names nil
149 "If non-nil the mail backends will use long file and directory names.
150 If nil, groups like \"mail.misc\" will end up in directories like
155 (defcustom nnmail-default-file-modes
384
156 "Set the mode bits of all new mail files to this integer."
160 (defcustom nnmail-expiry-wait
7
161 "*Expirable articles that are older than this will be expired.
162 This variable can either be a number (which will be interpreted as a
163 number of days) -- this doesn't have to be an integer. This variable
164 can also be `immediate' and `never'."
165 :group
'nnmail-expire
166 :type
'(choice (const immediate
)
170 (defcustom nnmail-expiry-wait-function nil
171 "Variable that holds function to specify how old articles should be before they are expired.
172 The function will be called with the name of the group that the expiry
173 is to be performed in, and it should return an integer that says how
174 many days an article can be stored before it is considered \"old\".
175 It can also return the values `never' and `immediate'.
179 \(setq nnmail-expiry-wait-function
181 (cond ((string-match \"private\" newsgroup) 31)
182 ((string-match \"junk\" newsgroup) 1)
183 ((string-match \"important\" newsgroup) \\='never)
185 :group
'nnmail-expire
186 :type
'(choice (const :tag
"nnmail-expiry-wait" nil
)
187 (function :format
"%v" nnmail-
)))
189 (defcustom nnmail-expiry-target
'delete
190 "*Variable that says where expired messages should end up.
191 The default value is `delete' (which says to delete the messages),
192 but it can also be a string or a function. If it is a string, expired
193 messages end up in that group. If it is a function, the function is
194 called in a buffer narrowed to the message in question. The function
195 receives one argument, the name of the group the message comes from.
196 The return value should be `delete' or a group name (a string)."
198 :group
'nnmail-expire
199 :type
'(choice (const delete
)
203 (defcustom nnmail-fancy-expiry-targets nil
204 "Determine expiry target based on articles using fancy techniques.
206 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
207 `nnmail-expiry-target' is set to the function
208 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
209 the message will be expired to a group determined by invoking
210 `format-time-string' with TARGET used as the format string and the
211 time extracted from the articles' Date header (if missing the current
214 In the special cases that HEADER is the symbol `to-from', the regexp
215 will try to match against both the From and the To header.
219 \(setq nnmail-fancy-expiry-targets
220 \\='((to-from \"boss\" \"nnfolder:Work\")
221 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
222 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
224 In this case, articles containing the string \"boss\" in the To or the
225 From header will be expired to the group \"nnfolder:Work\";
226 articles containing the string \"IMPORTANT\" in the Subject header will
227 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
228 everything else will be expired to \"nnfolder:Archive-YYYY\"."
230 :group
'nnmail-expire
231 :type
'(repeat (list (choice :tag
"Match against"
232 (string :tag
"Header")
235 (string :tag
"Target group format string"))))
237 (defcustom nnmail-cache-accepted-message-ids nil
238 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
239 If non-nil, also update the cache when copy or move articles."
243 (make-obsolete-variable 'nnmail-spool-file
'mail-sources
244 "Gnus 5.9 (Emacs 22.1)")
245 ;; revision 5.29 / p0-85 / Gnus 5.9
246 ;; Variable removed in No Gnus v0.7
248 (defcustom nnmail-resplit-incoming nil
249 "*If non-nil, re-split incoming procmail sorted mail."
250 :group
'nnmail-procmail
253 (defcustom nnmail-scan-directory-mail-source-once nil
254 "*If non-nil, scan all incoming procmail sorted mails once.
255 It scans low-level sorted spools even when not required."
257 :group
'nnmail-procmail
260 (defcustom nnmail-delete-file-function
'delete-file
261 "Function called to delete files in some mail backends."
265 (defcustom nnmail-crosspost-link-function
266 (if (string-match "windows-nt" (symbol-name system-type
))
269 "*Function called to create a copy of a file.
270 This is `add-name-to-file' by default, which means that crossposts
271 will use hard links. If your file system doesn't allow hard
272 links, you could set this variable to `copy-file' instead."
274 :type
'(radio (function-item add-name-to-file
)
275 (function-item copy-file
)
276 (function :tag
"Other")))
278 (defcustom nnmail-read-incoming-hook
279 (if (eq system-type
'windows-nt
)
280 '(nnheader-ms-strip-cr)
282 "*Hook that will be run after the incoming mail has been transferred.
283 The incoming mail is moved from the specified spool file (which normally is
284 something like \"/usr/spool/mail/$user\") to the user's home
285 directory. This hook is called after the incoming mail box has been
286 emptied, and can be used to call any mail box programs you have
287 running (\"xwatch\", etc.)
291 \(add-hook \\='nnmail-read-incoming-hook
293 (call-process \"/local/bin/mailsend\" nil nil nil
295 ;; The incoming mail box file.
296 (expand-file-name (user-login-name)
297 rmail-spool-directory))))
299 If you have xwatch running, this will alert it that mail has been
302 If you use `display-time', you could use something like this:
304 \(add-hook \\='nnmail-read-incoming-hook
306 ;; Update the displayed time, since that will clear out
307 ;; the flag that says you have mail.
308 (when (eq (process-status \"display-time\") \\='run)
309 (display-time-filter display-time-process \"\"))))"
310 :group
'nnmail-prepare
313 (defcustom nnmail-prepare-incoming-hook nil
314 "Hook called before treating incoming mail.
315 The hook is run in a buffer with all the new, incoming mail."
316 :group
'nnmail-prepare
319 (defcustom nnmail-prepare-incoming-header-hook nil
320 "Hook called narrowed to the headers of each message.
321 This can be used to remove excessive spaces (and stuff like
322 that) from the headers before splitting and saving the messages."
323 :group
'nnmail-prepare
326 (defcustom nnmail-prepare-incoming-message-hook nil
327 "Hook called narrowed to each message."
328 :group
'nnmail-prepare
331 (defcustom nnmail-list-identifiers nil
332 "Regexp that matches list identifiers to be removed.
333 This can also be a list of regexps."
334 :group
'nnmail-prepare
335 :type
'(choice (const :tag
"none" nil
)
337 (repeat :value
(".*") regexp
)))
339 (defcustom nnmail-pre-get-new-mail-hook nil
340 "Hook called just before starting to handle new incoming mail."
341 :group
'nnmail-retrieve
344 (defcustom nnmail-post-get-new-mail-hook nil
345 "Hook called just after finishing handling new incoming mail."
346 :group
'nnmail-retrieve
349 (defcustom nnmail-split-hook nil
350 "Hook called before deciding where to split an article.
351 The functions in this hook are free to modify the buffer
352 contents in any way they choose -- the buffer contents are
353 discarded after running the split process."
357 (defcustom nnmail-spool-hook nil
358 "*A hook called when a new article is spooled."
363 (defcustom nnmail-large-newsgroup
50
364 "*The number of articles which indicates a large newsgroup or nil.
365 If the number of articles is greater than the value, verbose
366 messages will be shown to indicate the current status."
367 :group
'nnmail-various
368 :type
'(choice (const :tag
"infinite" nil
)
369 (number :tag
"count")))
371 (define-widget 'nnmail-split-fancy
'lazy
372 "Widget for customizing splits in the variable of the same name."
374 :type
'(menu-choice :value
(any ".*value.*" "misc")
376 (string :tag
"Destination")
377 (list :tag
"Use first match (|)" :value
(|
)
379 (editable-list :inline t nnmail-split-fancy
))
380 (list :tag
"Use all matches (&)" :value
(&)
382 (editable-list :inline t nnmail-split-fancy
))
383 (list :tag
"Function with fixed arguments (:)"
385 (const :format
"" :value
:)
387 (editable-list :inline t
(sexp :tag
"Arg"))
389 (list :tag
"Function with split arguments (!)"
393 (editable-list :inline t nnmail-split-fancy
))
394 (list :tag
"Field match"
399 (symbol :value mail
))
406 (const :tag
"Junk (delete mail)" junk
)))
408 (defcustom nnmail-split-fancy
"mail.misc"
409 "Incoming mail can be split according to this fancy variable.
410 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
412 The format of this variable is SPLIT, where SPLIT can be one of
415 GROUP: Mail will be stored in GROUP (a string).
417 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
418 field FIELD (a regexp) contains VALUE (a regexp), store the messages
419 as specified by SPLIT. If RESTRICT (a regexp) matches some string
420 after FIELD and before the end of the matched VALUE, return nil,
421 otherwise process SPLIT. Multiple RESTRICTs add up, further
422 restricting the possibility of processing SPLIT.
424 \(| SPLIT...): Process each SPLIT expression until one of them matches.
425 A SPLIT expression is said to match if it will cause the mail
426 message to be stored in one or more groups.
428 \(& SPLIT...): Process each SPLIT expression.
430 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
431 the buffer containing the message headers. The return value FUNCTION
432 should be a split, which is then recursively processed.
434 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
435 return value FUNCTION should be a split, which is then recursively
438 junk: Mail will be deleted. Use with care! Do not submerge in water!
440 (setq nnmail-split-fancy
441 \\='(| (\"Subject\" \"MAKE MONEY FAST\" junk)
442 ...other.rules.omitted...))
444 FIELD must match a complete field name. VALUE must match a complete
445 word according to the `nnmail-split-fancy-syntax-table' syntax table.
446 You can use \".*\" in the regexps to match partial field names or words.
448 FIELD and VALUE can also be Lisp symbols, in that case they are expanded
449 as specified in `nnmail-split-abbrev-alist'.
451 GROUP can contain \\& and \\N which will substitute from matching
452 \\(\\) patterns in the previous VALUE.
456 \(setq nnmail-split-methods \\='nnmail-split-fancy
458 ;; Messages from the mailer daemon are not crossposted to any of
459 ;; the ordinary groups. Warnings are put in a separate group
461 \\='(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
463 ;; Non-error messages are crossposted to all relevant
464 ;; groups, but we don't crosspost between the group for the
465 ;; (ding) list and the group for other (ding) related mail.
466 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
467 (\"subject\" \"ding\" \"ding.misc\"))
468 ;; Other mailing lists...
469 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
470 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
471 ;; Both lists below have the same suffix, so prevent
472 ;; cross-posting to mkpkg.list of messages posted only to
473 ;; the bugs- list, but allow cross-posting when the
474 ;; message was really cross-posted.
475 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
476 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
479 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
480 ;; Unmatched mail goes to the catch all group.
483 :type
'nnmail-split-fancy
)
485 (defcustom nnmail-split-abbrev-alist
486 '((any .
"from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
487 (mail .
"mailer-daemon\\|postmaster\\|uucp")
488 (to .
"to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
489 (from .
"from\\|sender\\|resent-from")
490 (nato .
"to\\|cc\\|resent-to\\|resent-cc")
491 (naany .
"from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
492 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
494 :type
'(repeat (cons :format
"%v" symbol regexp
)))
496 (defcustom nnmail-message-id-cache-length
1000
497 "*The approximate number of Message-IDs nnmail will keep in its cache.
498 If this variable is nil, no checking on duplicate messages will be
500 :group
'nnmail-duplicate
501 :type
'(choice (const :tag
"disable" nil
)
502 (integer :format
"%v")))
504 (defcustom nnmail-message-id-cache-file
505 (nnheader-concat gnus-home-directory
".nnmail-cache")
506 "The file name of the nnmail Message-ID cache."
507 :group
'nnmail-duplicate
511 (defcustom nnmail-treat-duplicates
'warn
512 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
513 Three values are valid: nil, which means that nnmail is not to keep a
514 Message-ID cache; `warn', which means that nnmail should insert extra
515 headers to warn the user about the duplication (this is the default);
516 and `delete', which means that nnmail will delete duplicated mails.
518 This variable can also be a function. It will be called from a buffer
519 narrowed to the article in question with the Message-ID as a
520 parameter. It should return nil, `warn' or `delete'."
521 :group
'nnmail-duplicate
522 :type
'(choice (const :tag
"off" nil
)
526 (defcustom nnmail-extra-headers
'(To Newsgroups Cc
)
527 "Extra headers to parse.
528 In addition to the standard headers, these extra headers will be
529 included in NOV headers (and the like) when backends parse headers."
532 :type
'(repeat symbol
))
534 (defcustom nnmail-split-header-length-limit
2048
535 "Header lines longer than this limit are excluded from the split function."
540 (defcustom nnmail-mail-splitting-charset nil
541 "Default charset to be used when splitting incoming mail."
546 (defcustom nnmail-mail-splitting-decodes nil
547 "Whether the nnmail splitting functionality should MIME decode headers."
552 (defcustom nnmail-split-fancy-match-partial-words nil
553 "Whether to match partial words when fancy splitting.
554 Normally, regexes given in `nnmail-split-fancy' are implicitly surrounded
555 by \"\\=\\<...\\>\". If this variable is true, they are not implicitly\
562 (defcustom nnmail-split-lowercase-expanded t
563 "Whether to lowercase expanded entries (i.e. \\N) when splitting mails.
564 This avoids the creation of multiple groups when users send to an address
565 using different case (i.e. mailing-list@domain vs Mailing-List@Domain)."
570 ;;; Internal variables.
572 (defvar nnmail-article-buffer
" *nnmail incoming*"
573 "The buffer used for splitting incoming mails.")
575 (defvar nnmail-split-history nil
576 "List of group/article elements that say where the previous split put messages.")
578 (defvar nnmail-split-fancy-syntax-table
579 (let ((table (make-syntax-table)))
580 ;; support the %-hack
581 (modify-syntax-entry ?\%
"." table
)
583 "Syntax table used by `nnmail-split-fancy'.")
585 (defvar nnmail-prepare-save-mail-hook nil
586 "Hook called before saving mail.")
588 (defvar nnmail-split-tracing nil
)
589 (defvar nnmail-split-trace nil
)
590 (defvar nnmail-inhibit-default-split-group nil
)
594 (defun nnmail-request-post (&optional server
)
595 (mail-send-and-exit nil
))
597 (defvar nnmail-file-coding-system
'raw-text
598 "Coding system used in nnmail.")
600 (defvar nnmail-incoming-coding-system
601 mm-text-coding-system
602 "Coding system used in reading inbox")
604 (defvar nnmail-pathname-coding-system nil
605 "*Coding system for file name.")
607 (defun nnmail-find-file (file)
608 "Insert FILE in server buffer safely."
609 (set-buffer nntp-server-buffer
)
610 (delete-region (point-min) (point-max))
611 (let ((format-alist nil
)
612 (after-insert-file-functions nil
))
614 (let ((coding-system-for-read nnmail-file-coding-system
)
615 (auto-mode-alist (mm-auto-mode-alist))
616 (file-name-coding-system nnmail-pathname-coding-system
))
617 (insert-file-contents file
)
621 (defun nnmail-group-pathname (group dir
&optional file
)
622 "Make file name for GROUP."
624 (let ((dir (file-name-as-directory (expand-file-name dir
))))
625 (setq group
(nnheader-replace-duplicate-chars-in-string
626 (nnheader-replace-chars-in-string group ?
/ ?_
)
628 (setq group
(nnheader-translate-file-chars group
))
629 ;; If this directory exists, we use it directly.
630 (file-name-as-directory
631 (if (or nnmail-use-long-file-names
632 (file-directory-p (concat dir group
)))
633 (expand-file-name group dir
)
634 ;; If not, we translate dots into slashes.
636 (nnheader-replace-chars-in-string group ?. ?
/)
640 (defun nnmail-get-active ()
641 "Returns an assoc of group names and active ranges.
642 nn*-request-list should have been called before calling this function."
643 ;; Go through all groups from the active list.
644 (with-current-buffer nntp-server-buffer
645 (nnmail-parse-active)))
647 (defun nnmail-parse-active ()
648 "Parse the active file in the current buffer and return an alist."
649 (goto-char (point-min))
650 (unless (re-search-forward "[\\\"]" nil t
)
651 (goto-char (point-max))
652 (while (re-search-backward "[][';?()#]" nil t
)
654 (goto-char (point-min))
655 (let ((buffer (current-buffer))
656 group-assoc group max min
)
660 (narrow-to-region (point) (point-at-eol))
661 (setq group
(read buffer
))
662 (unless (stringp group
)
663 (setq group
(symbol-name group
)))
664 (if (and (numberp (setq max
(read buffer
)))
665 (numberp (setq min
(read buffer
))))
666 (push (list (string-as-unibyte group
) (cons min max
))
673 (defvar nnmail-active-file-coding-system
'raw-text
674 "*Coding system for active file.")
676 (defun nnmail-save-active (group-assoc file-name
)
677 "Save GROUP-ASSOC in ACTIVE-FILE."
678 (let ((coding-system-for-write nnmail-active-file-coding-system
))
680 (with-temp-file file-name
681 (mm-disable-multibyte)
682 (nnmail-generate-active group-assoc
)))))
684 (defun nnmail-generate-active (alist)
685 "Generate an active file from group-alist ALIST."
688 (while (setq group
(pop alist
))
689 (insert (format "%S %d %d y\n" (intern (car group
)) (cdadr group
)
691 (goto-char (point-max))
692 (while (search-backward "\\." nil t
)
695 (defun nnmail-get-split-group (file source
)
696 "Find out whether this FILE is to be split into GROUP only.
697 If SOURCE is a directory spec, try to return the group name component."
698 (if (eq (car source
) 'directory
)
699 (let ((file (file-name-nondirectory file
)))
700 (mail-source-bind (directory source
)
701 (if (string-match (concat (regexp-quote suffix
) "$") file
)
702 (substring file
0 (match-beginning 0))
706 (defun nnmail-process-babyl-mail-format (func artnum-func
)
707 (let ((case-fold-search t
)
709 start message-id content-length do-search end
)
711 (goto-char (point-min))
713 "\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t
)
714 (goto-char (match-end 0))
715 (delete-region (match-beginning 0) (match-end 0))
719 ;; Skip all the headers in case there are more "From "s...
720 (or (search-forward "\n\n" nil t
)
721 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t
)
722 (search-forward "\x1f\f"))
724 ;; Unquote the ">From " line, if any.
725 (goto-char (point-min))
726 (when (looking-at ">From ")
727 (replace-match "X-From-Line: ") )
728 (run-hooks 'nnmail-prepare-incoming-header-hook
)
729 (goto-char (point-max))
730 ;; Find the Message-ID header.
732 (if (re-search-backward
733 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t
)
734 (setq message-id
(buffer-substring (match-beginning 1)
736 ;; There is no Message-ID here, so we create one.
738 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
740 (insert "Original-")))
742 (insert "Message-ID: " (setq message-id
(nnmail-message-id))
744 ;; Look for a Content-Length header.
745 (if (not (save-excursion
746 (and (re-search-backward
747 "^Content-Length:[ \t]*\\([0-9]+\\)" start t
)
748 (setq content-length
(string-to-number
752 ;; We destroy the header, since none of
753 ;; the backends ever use it, and we do not
754 ;; want to confuse other mailers by having
755 ;; a (possibly) faulty header.
756 (progn (insert "X-") t
))))
759 (if (or (= (+ (point) content-length
) (point-max))
761 (goto-char (+ (point) content-length
))
762 (looking-at "\x1f")))
764 (goto-char (+ (point) content-length
))
765 (setq do-search nil
))
768 ;; Go to the beginning of the next article - or to the end
771 (if (re-search-forward "^\x1f" nil t
)
772 (goto-char (match-beginning 0))
773 (goto-char (1- (point-max)))))
774 (delete-char 1) ; delete ^_
777 (narrow-to-region start
(point))
778 (goto-char (point-min))
779 (nnmail-check-duplication message-id func artnum-func
)
781 (setq end
(point-max))))
785 (defsubst nnmail-search-unix-mail-delim
()
786 "Put point at the beginning of the next Unix mbox message."
787 ;; Algorithm used to find the next article in the
788 ;; brain-dead Unix mbox format:
790 ;; 1) Search for "^From ".
791 ;; 2) If we find it, then see whether the previous
792 ;; line is blank and the next line looks like a header.
793 ;; Then it's possible that this is a mail delim, and we use it.
794 (let ((case-fold-search nil
)
797 (if (not (re-search-forward "^From " nil t
))
801 (when (and (or (bobp)
804 (eq (char-after) ?
\n)))
807 (while (looking-at ">From \\|From ")
809 (looking-at "[^ \n\t:]+[ \n\t]*:")))
810 (setq found
'yes
)))))
814 (defun nnmail-search-unix-mail-delim-backward ()
815 "Put point at the beginning of the current 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-backward "^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-process-unix-mail-format (func artnum-func
)
844 (let ((case-fold-search t
)
846 start message-id content-length end skip head-end
)
847 (goto-char (point-min))
848 (if (not (and (re-search-forward "^From " nil t
)
849 (goto-char (match-beginning 0))))
850 ;; Possibly wrong format?
851 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
852 (if (buffer-file-name) "file" "buffer")
853 (or (buffer-file-name) (buffer-name)))
854 ;; Carry on until the bitter end.
858 ;; Find the end of the head.
861 (if (search-forward "\n\n" nil t
)
863 ;; This will never happen, but just to be on the safe side --
864 ;; if there is no head-body delimiter, we search a bit manually.
865 (while (and (looking-at "From \\|[^ \t]+:")
869 ;; Find the Message-ID header.
870 (goto-char (point-min))
871 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t
)
872 (setq message-id
(match-string 1))
874 (when (re-search-forward "^Message-ID[ \t]*:" nil t
)
876 (insert "Original-")))
877 ;; There is no Message-ID here, so we create one.
879 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
880 ;; Look for a Content-Length header.
881 (goto-char (point-min))
882 (if (not (re-search-forward
883 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t
))
884 (setq content-length nil
)
885 (setq content-length
(string-to-number (match-string 1)))
886 ;; We destroy the header, since none of the backends ever
887 ;; use it, and we do not want to confuse other mailers by
888 ;; having a (possibly) faulty header.
891 (run-hooks 'nnmail-prepare-incoming-header-hook
)
892 ;; Find the end of this article.
893 (goto-char (point-max))
895 (setq head-end
(point))
896 ;; We try the Content-Length value. The idea: skip over the header
897 ;; separator, then check what happens content-length bytes into the
898 ;; message body. This should be either the end of the buffer, the
899 ;; message separator or a blank line followed by the separator.
900 ;; The blank line should probably be deleted. If neither of the
901 ;; three is met, the content-length header is probably invalid.
904 (setq skip
(+ (point) content-length
))
906 (cond ((or (= skip
(point-max))
907 (= (1+ skip
) (point-max)))
908 (setq end
(point-max)))
909 ((looking-at "From ")
911 ((looking-at "[ \t]*\n\\(From \\)")
912 (setq end
(match-beginning 1)))
916 ;; No Content-Length, so we find the beginning of the next
917 ;; article or the end of the buffer.
919 (or (nnmail-search-unix-mail-delim)
920 (goto-char (point-max))))
921 ;; Allow the backend to save the article.
924 (narrow-to-region start
(point))
925 (goto-char (point-min))
927 (nnmail-check-duplication message-id func artnum-func
)
928 (setq end
(point-max))))
932 (defun nnmail-process-mmdf-mail-format (func artnum-func
&optional junk-func
)
933 (let ((delim "^\^A\^A\^A\^A$")
936 start message-id end
)
937 (goto-char (point-min))
938 (if (not (and (re-search-forward delim nil t
)
940 ;; Possibly wrong format?
941 (error "Error, unknown mail format! (Possibly corrupted.)")
942 ;; Carry on until the bitter end.
945 ;; Find the end of the head.
948 (if (search-forward "\n\n" nil t
)
950 ;; This will never happen, but just to be on the safe side --
951 ;; if there is no head-body delimiter, we search a bit manually.
952 (while (and (looking-at "From \\|[^ \t]+:")
956 ;; Find the Message-ID header.
957 (goto-char (point-min))
958 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t
)
959 (setq message-id
(match-string 1))
960 ;; There is no Message-ID here, so we create one.
962 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
964 (insert "Original-")))
966 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
967 (run-hooks 'nnmail-prepare-incoming-header-hook
)
968 ;; Find the end of this article.
969 (goto-char (point-max))
971 (if (re-search-forward delim nil t
)
973 (goto-char (point-max)))
974 ;; Allow the backend to save the article.
977 (narrow-to-region start
(point))
978 (goto-char (point-min))
980 (nnmail-check-duplication message-id func artnum-func junk-func
)
981 (setq end
(point-max))))
986 (defun nnmail-process-maildir-mail-format (func artnum-func
)
987 ;; In a maildir, every file contains exactly one mail.
988 (let ((case-fold-search t
)
990 (goto-char (point-min))
991 ;; Find the end of the head.
994 (if (search-forward "\n\n" nil t
)
996 ;; This will never happen, but just to be on the safe side --
997 ;; if there is no head-body delimiter, we search a bit manually.
998 (while (and (looking-at "From \\|[^ \t]+:")
1002 ;; Find the Message-ID header.
1003 (goto-char (point-min))
1004 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t
)
1005 (setq message-id
(match-string 1))
1006 ;; There is no Message-ID here, so we create one.
1008 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
1010 (insert "Original-")))
1012 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
1013 (run-hooks 'nnmail-prepare-incoming-header-hook
)
1014 ;; Allow the backend to save the article.
1017 (goto-char (point-min))
1018 (nnmail-check-duplication message-id func artnum-func
))
1021 (defvar nnmail-group-names-not-encoded-p nil
1022 "Non-nil means group names are not encoded.")
1024 (defun nnmail-split-incoming (incoming func
&optional exit-func
1025 group artnum-func junk-func
)
1026 "Go through the entire INCOMING file and pick out each individual mail.
1027 FUNC will be called with the buffer narrowed to each mail.
1028 INCOMING can also be a buffer object. In that case, the mail
1029 will be copied over from that buffer."
1030 (let ( ;; If this is a group-specific split, we bind the split
1031 ;; methods to just this group.
1032 (nnmail-split-methods (if (and group
1033 (not nnmail-resplit-incoming
))
1034 (list (list group
""))
1035 nnmail-split-methods
))
1036 (nnmail-group-names-not-encoded-p t
))
1037 ;; Insert the incoming file.
1038 (with-current-buffer (get-buffer-create nnmail-article-buffer
)
1040 (if (bufferp incoming
)
1041 (insert-buffer-substring incoming
)
1042 (let ((coding-system-for-read nnmail-incoming-coding-system
))
1043 (mm-insert-file-contents incoming
)))
1045 (if (zerop (buffer-size))
1047 (goto-char (point-min))
1048 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook
))
1049 ;; Handle both babyl, MMDF and unix mail formats, since
1050 ;; movemail will use the former when fetching from a
1051 ;; mailbox, the latter when fetching from a file.
1052 (cond ((or (looking-at "\^L")
1053 (looking-at "BABYL OPTIONS:"))
1054 (nnmail-process-babyl-mail-format func artnum-func
))
1055 ((looking-at "\^A\^A\^A\^A")
1056 (nnmail-process-mmdf-mail-format
1057 func artnum-func junk-func
))
1058 ((looking-at "Return-Path:")
1059 (nnmail-process-maildir-mail-format func artnum-func
))
1061 (nnmail-process-unix-mail-format func artnum-func
))))
1063 (funcall exit-func
))
1064 (kill-buffer (current-buffer))))))
1066 (defun nnmail-article-group (func &optional trace junk-func
)
1067 "Look at the headers and return an alist of groups that match.
1068 FUNC will be called with the group name to determine the article number."
1069 (let ((methods (or nnmail-split-methods
'(("bogus" ""))))
1070 (obuf (current-buffer))
1071 group-art method grp
)
1072 (if (and (sequencep methods
)
1073 (= (length methods
) 1)
1074 (not nnmail-inhibit-default-split-group
))
1075 ;; If there is only just one group to put everything in, we
1076 ;; just return a list with just this one method in.
1078 (list (cons (caar methods
) (funcall func
(caar methods
)))))
1079 ;; We do actual comparison.
1080 ;; Copy the article into the work buffer.
1081 (with-current-buffer nntp-server-buffer
1083 (insert-buffer-substring obuf
)
1084 ;; Narrow to headers.
1086 (goto-char (point-min))
1087 (if (search-forward "\n\n" nil t
)
1090 (goto-char (point-min))
1091 ;; Decode MIME headers and charsets.
1092 (when nnmail-mail-splitting-decodes
1093 (let ((mail-parse-charset nnmail-mail-splitting-charset
))
1094 (mail-decode-encoded-word-region (point-min) (point-max))))
1095 ;; Fold continuation lines.
1096 (goto-char (point-min))
1097 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t
)
1098 (replace-match " " t t
))
1099 ;; Nuke pathologically long headers. Since Gnus applies
1100 ;; pathologically complex regexps to the buffer, lines
1101 ;; that are looong will take longer than the Universe's
1102 ;; existence to process.
1103 (goto-char (point-min))
1105 (unless (< (move-to-column nnmail-split-header-length-limit
)
1106 nnmail-split-header-length-limit
)
1107 (delete-region (point) (point-at-eol)))
1110 (goto-char (point-min))
1111 (run-hooks 'nnmail-split-hook
)
1112 (when (setq nnmail-split-tracing trace
)
1113 (setq nnmail-split-trace nil
))
1114 (if (or (and (symbolp nnmail-split-methods
)
1115 (fboundp nnmail-split-methods
))
1116 (not (consp (car-safe nnmail-split-methods
)))
1117 (and (listp nnmail-split-methods
)
1118 ;; Not a regular split method, so it has to be a
1120 (not (let ((top-element (car-safe nnmail-split-methods
)))
1121 (and (= 2 (length top-element
))
1122 (stringp (nth 0 top-element
))
1123 (stringp (nth 1 top-element
)))))))
1124 (let* ((method-function
1125 (if (and (symbolp nnmail-split-methods
)
1126 (fboundp nnmail-split-methods
))
1127 nnmail-split-methods
1128 'nnmail-split-fancy
))
1130 (condition-case error-info
1131 ;; `nnmail-split-methods' is a function, so we
1132 ;; just call this function here and use the
1134 (or (funcall method-function
)
1135 (and (not nnmail-inhibit-default-split-group
)
1139 5 "Error in `nnmail-split-methods'; using `bogus' mail group: %S" error-info
)
1142 (setq split
(delete-dups split
))
1143 ;; The article may be "cross-posted" to `junk'. What
1144 ;; to do? Just remove the `junk' spec. Don't really
1145 ;; see anything else to do...
1146 (when (and (memq 'junk split
)
1148 (funcall junk-func
'junk
))
1149 (setq split
(delq 'junk split
))
1153 (lambda (group) (cons group
(funcall func group
)))
1155 ;; Go through the split methods to find a match.
1157 (or nnmail-crosspost
1159 (goto-char (point-max))
1160 (setq method
(pop methods
)
1163 (not (equal "" (nth 1 method
))))
1166 (if (stringp (nth 1 method
))
1167 (let ((expand (string-match "\\\\[0-9&]" grp
))
1168 (pos (re-search-backward (cadr method
)
1171 (setq grp
(nnmail-expand-newtext grp
)))
1173 ;; Function to say whether this is a match.
1174 (funcall (nth 1 method
) grp
)))
1175 ;; Don't enter the article into the same
1177 (not (assoc grp group-art
)))
1178 (push (cons grp
(funcall func grp
))
1180 ;; This is the final group, which is used as a
1182 (when (and (not group-art
)
1183 (or (equal "" (nth 1 method
))
1184 (not nnmail-inhibit-default-split-group
)))
1186 (list (cons (car method
)
1187 (funcall func
(car method
))))))))
1188 ;; Fall back on "bogus" if all else fails.
1189 (when (and (not group-art
)
1190 (not nnmail-inhibit-default-split-group
))
1191 (setq group-art
(list (cons "bogus" (funcall func
"bogus"))))))
1192 ;; Produce a trace if non-empty.
1193 (when (and trace nnmail-split-trace
)
1194 (let ((restore (current-buffer)))
1195 (nnheader-set-temp-buffer "*Split Trace*")
1197 (dolist (trace (nreverse nnmail-split-trace
))
1198 (prin1 trace
(current-buffer))
1200 (goto-char (point-min))
1201 (gnus-configure-windows 'split-trace
)
1202 (set-buffer restore
)))
1204 ;; See whether the split methods returned `junk'.
1205 (if (equal group-art
'(junk))
1207 ;; The article may be "cross-posted" to `junk'. What
1208 ;; to do? Just remove the `junk' spec. Don't really
1209 ;; see anything else to do...
1211 (while (setq elem
(car (memq 'junk group-art
)))
1212 (setq group-art
(delq elem group-art
)))
1213 (nreverse group-art
)))))))
1215 (defun nnmail-insert-lines ()
1216 "Insert how many lines there are in the body of the mail.
1217 Return the number of characters in the body."
1220 (goto-char (point-min))
1221 (unless (search-forward "\n\n" nil t
)
1222 (goto-char (point-max))
1224 (setq chars
(- (point-max) (point)))
1225 (setq lines
(count-lines (point) (point-max)))
1228 (when (re-search-backward "^Lines: " nil t
)
1229 (delete-region (point) (progn (forward-line 1) (point)))))
1231 (insert (format "Lines: %d\n" (max lines
0)))
1234 (defun nnmail-insert-xref (group-alist)
1235 "Insert an Xref line based on the (group . article) alist."
1237 (goto-char (point-min))
1238 (unless (search-forward "\n\n" nil t
)
1239 (goto-char (point-max))
1242 (when (re-search-backward "^Xref: " nil t
)
1243 (delete-region (match-beginning 0)
1244 (progn (forward-line 1) (point))))
1245 (insert (format "Xref: %s" (system-name)))
1247 (insert (if (mm-multibyte-p)
1248 (string-as-multibyte
1249 (format " %s:%d" (caar group-alist
) (cdar group-alist
)))
1251 (format " %s:%d" (caar group-alist
) (cdar group-alist
)))))
1252 (setq group-alist
(cdr group-alist
)))
1255 ;;; Message washing functions
1257 (defun nnmail-remove-leading-whitespace ()
1258 "Remove excessive whitespace from all headers."
1259 (goto-char (point-min))
1260 (while (re-search-forward "^\\([^ :]+: \\) +" nil t
)
1261 (replace-match "\\1" t
)))
1263 (defun nnmail-remove-list-identifiers ()
1264 "Remove list identifiers from Subject headers."
1266 (if (consp nnmail-list-identifiers
)
1267 (mapconcat 'identity nnmail-list-identifiers
" *\\|")
1268 nnmail-list-identifiers
)))
1270 (goto-char (point-min))
1271 (while (re-search-forward
1272 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp
" *\\)")
1274 (delete-region (match-beginning 2) (match-end 0))
1275 (beginning-of-line))
1276 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1278 (delete-region (match-beginning 1) (match-end 1))
1279 (beginning-of-line)))))
1281 (defun nnmail-remove-tabs ()
1282 "Translate TAB characters into SPACE characters."
1283 (subst-char-in-region (point-min) (point-max) ?
\t ? t
))
1285 (defcustom nnmail-broken-references-mailers
1286 "^X-Mailer:.*\\(Eudora\\|Pegasus\\)"
1287 "Header line matching mailer producing bogus References lines.
1288 See `nnmail-ignore-broken-references'."
1289 :group
'nnmail-prepare
1290 :version
"23.1" ;; No Gnus
1293 (defun nnmail-ignore-broken-references ()
1294 "Ignore the References line and use In-Reply-To
1296 Eudora has a broken References line, but an OK In-Reply-To."
1297 (goto-char (point-min))
1298 (when (re-search-forward nnmail-broken-references-mailers nil t
)
1299 (goto-char (point-min))
1300 (when (re-search-forward "^References:" nil t
)
1302 (insert "X-Gnus-Broken-Eudora-"))
1303 (goto-char (point-min))
1304 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t
)
1305 (replace-match "\\1" t
))))
1307 (defalias 'nnmail-fix-eudora-headers
'nnmail-ignore-broken-references
)
1308 (make-obsolete 'nnmail-fix-eudora-headers
'nnmail-ignore-broken-references
"Emacs 23.1")
1310 (custom-add-option 'nnmail-prepare-incoming-header-hook
1311 'nnmail-ignore-broken-references
)
1313 ;;; Utility functions
1315 (declare-function gnus-activate-group
"gnus-start"
1316 (group &optional scan dont-check method dont-sub-check
))
1318 (defun nnmail-do-request-post (accept-func &optional server
)
1319 "Utility function to directly post a message to an nnmail-derived group.
1320 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1321 to actually put the message in the right group."
1323 (dolist (mbx (message-unquote-tokens
1324 (message-tokenize-header
1325 (message-fetch-field "Newsgroups") ", ")) success
)
1326 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method
)))
1327 (or (gnus-active to-newsgroup
)
1328 (gnus-activate-group to-newsgroup
)
1329 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1331 (or (and (gnus-request-create-group
1332 to-newsgroup gnus-command-method
)
1333 (gnus-activate-group to-newsgroup nil nil
1334 gnus-command-method
))
1335 (error "Couldn't create group %s" to-newsgroup
)))
1336 (error "No such group: %s" to-newsgroup
))
1337 (unless (funcall accept-func mbx
(nth 1 gnus-command-method
))
1338 (setq success nil
))))))
1340 (defun nnmail-split-fancy ()
1341 "Fancy splitting method.
1342 See the documentation for the variable `nnmail-split-fancy' for details."
1343 (with-syntax-table nnmail-split-fancy-syntax-table
1344 (nnmail-split-it nnmail-split-fancy
)))
1346 (defvar nnmail-split-cache nil
)
1347 ;; Alist of split expressions their equivalent regexps.
1349 (defun nnmail-split-it (split)
1350 ;; Return a list of groups matching SPLIT.
1357 ;; A group name. Do the \& and \N subs into the string.
1359 (when nnmail-split-tracing
1360 (push split nnmail-split-trace
))
1361 (list (nnmail-expand-newtext split t
)))
1363 ;; Junk the message.
1365 (when nnmail-split-tracing
1366 (push "junk" nnmail-split-trace
))
1369 ;; Builtin & operation.
1370 ((eq (car split
) '&)
1371 (apply 'nconc
(mapcar 'nnmail-split-it
(cdr split
))))
1373 ;; Builtin | operation.
1374 ((eq (car split
) '|
)
1376 (while (and (not done
) (cdr split
))
1377 (setq split
(cdr split
)
1378 done
(nnmail-split-it (car split
))))
1381 ;; Builtin : operation.
1382 ((eq (car split
) ':)
1383 (when nnmail-split-tracing
1384 (push split nnmail-split-trace
))
1385 (nnmail-split-it (save-excursion (eval (cdr split
)))))
1387 ;; Builtin ! operation.
1388 ((eq (car split
) '!)
1389 (funcall (cadr split
) (nnmail-split-it (caddr split
))))
1391 ;; Check the cache for the regexp for this split.
1392 ((setq cached-pair
(assq split nnmail-split-cache
))
1395 (end-point (point-max))
1396 (value (nth 1 split
)))
1398 (setq value
(cdr (assq value nnmail-split-abbrev-alist
))))
1399 (while (and (goto-char end-point
)
1400 (re-search-backward (cdr cached-pair
) nil t
))
1401 (setq match-data
(match-data))
1402 (when nnmail-split-tracing
1403 (push split nnmail-split-trace
))
1404 (let ((split-rest (cddr split
))
1406 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1407 ;; So, start-of-value is the point just before the
1408 ;; beginning of the value, whereas after-header-name
1409 ;; is the point just after the field name.
1410 (start-of-value (match-end 1))
1411 (after-header-name (match-end 2)))
1412 ;; Start the next search just before the beginning of the
1414 (setq end-point
(1- start-of-value
))
1415 ;; Handle - RESTRICTs
1416 (while (eq (car split-rest
) '-
)
1417 ;; RESTRICT must start after-header-name and
1418 ;; end after start-of-value, so that, for
1419 ;; (any "foo" - "x-foo" "foo.list")
1420 ;; we do not exclude foo.list just because
1421 ;; the header is: ``To: x-foo, foo''
1423 (if (and (re-search-backward (cadr split-rest
)
1424 after-header-name t
)
1425 (> (match-end 0) start-of-value
))
1426 (setq split-rest nil
)
1427 (setq split-rest
(cddr split-rest
))))
1430 ;; Someone might want to do a \N sub on this match, so
1431 ;; restore the match data.
1432 (set-match-data match-data
)
1433 (dolist (sp (nnmail-split-it (car split-rest
)))
1434 (unless (member sp split-result
)
1435 (push sp split-result
))))))
1438 ;; Not in cache, compute a regexp for the field/value pair.
1440 (let ((field (nth 0 split
))
1441 (value (nth 1 split
))
1442 (split-rest (cddr split
))
1447 (setq value
(cdr (assq value nnmail-split-abbrev-alist
))))
1448 (if (and (>= (length value
) 2)
1449 (string= ".*" (substring value
0 2)))
1450 (setq value
(substring value
2)
1452 ;; Same trick for the rear of the regexp
1453 (if (and (>= (length value
) 2)
1454 (string= ".*" (substring value -
2)))
1455 (setq value
(substring value
0 -
2)
1457 ;; Invert the match-partial-words behavior if the optional
1458 ;; last element is specified.
1459 (while (eq (car split-rest
) '-
)
1460 (setq split-rest
(cddr split-rest
)))
1461 (when (if (cadr split-rest
)
1462 (not nnmail-split-fancy-match-partial-words
)
1463 nnmail-split-fancy-match-partial-words
)
1464 (setq partial-front
""
1466 (setq regexp
(concat "^\\(\\("
1468 (cdr (assq field nnmail-split-abbrev-alist
))
1471 (or partial-front
"\\<")
1475 (or partial-rear
"\\>")))
1476 (push (cons split regexp
) nnmail-split-cache
)
1477 ;; Now that it's in the cache, just call nnmail-split-it again
1478 ;; on the same split, which will find it immediately in the cache.
1479 (nnmail-split-it split
))))))
1481 (defun nnmail-expand-newtext (newtext &optional fancyp
)
1482 (let ((len (length newtext
))
1484 c expanded beg N did-expand
)
1487 (while (and (< pos len
)
1488 (not (= (aref newtext pos
) ?
\\)))
1489 (setq pos
(1+ pos
)))
1491 (push (substring newtext beg pos
) expanded
))
1493 ;; We hit a \; expand it.
1496 c
(aref newtext pos
))
1497 (if (not (or (= c ?\
&)
1500 ;; \ followed by some character we don't expand.
1501 (push (char-to-string c
) expanded
)
1506 ;; We wrapped the searches in parentheses, so we have to
1507 ;; add some parentheses here...
1510 (when (match-beginning N
)
1511 (push (if nnmail-split-lowercase-expanded
1512 (downcase (buffer-substring (match-beginning N
)
1514 (buffer-substring (match-beginning N
) (match-end N
)))
1516 (setq pos
(1+ pos
)))
1518 (apply 'concat
(nreverse expanded
))
1521 ;; Activate a backend only if it isn't already activated.
1522 ;; If FORCE, re-read the active file even if the backend is
1523 ;; already activated.
1524 (defun nnmail-activate (backend &optional force
)
1525 (nnheader-init-server-buffer)
1526 (let (file timestamp file-time
)
1527 (if (or (not (symbol-value (intern (format "%s-group-alist" backend
))))
1529 (and (setq file
(ignore-errors
1530 (symbol-value (intern (format "%s-active-file"
1532 (setq file-time
(nth 5 (file-attributes file
)))
1536 (symbol-value (intern
1537 (format "%s-active-timestamp"
1540 (not (consp timestamp
))
1541 (equal timestamp
'(0 0))
1542 (> (nth 0 file-time
) (nth 0 timestamp
))
1543 (and (= (nth 0 file-time
) (nth 0 timestamp
))
1544 (> (nth 1 file-time
) (nth 1 timestamp
))))))
1546 (or (eq timestamp
'none
)
1547 (set (intern (format "%s-active-timestamp" backend
))
1549 (funcall (intern (format "%s-request-list" backend
)))))
1552 (defun nnmail-message-id ()
1553 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1556 ;;; nnmail duplicate handling
1559 (defvar nnmail-cache-buffer nil
)
1561 (defun nnmail-cache-open ()
1562 (if (or (not nnmail-treat-duplicates
)
1563 (and nnmail-cache-buffer
1564 (buffer-name nnmail-cache-buffer
)))
1565 () ; The buffer is open.
1566 (with-current-buffer
1567 (setq nnmail-cache-buffer
1568 (get-buffer-create " *nnmail message-id cache*"))
1570 (when (file-exists-p nnmail-message-id-cache-file
)
1571 (nnheader-insert-file-contents nnmail-message-id-cache-file
))
1572 (set-buffer-modified-p nil
)
1575 (defun nnmail-cache-close ()
1576 (when (and nnmail-cache-buffer
1577 nnmail-treat-duplicates
1578 (buffer-name nnmail-cache-buffer
)
1579 (buffer-modified-p nnmail-cache-buffer
))
1580 (with-current-buffer nnmail-cache-buffer
1581 ;; Weed out the excess number of Message-IDs.
1582 (goto-char (point-max))
1583 (when (search-backward "\n" nil t nnmail-message-id-cache-length
)
1586 (delete-region (point-min) (point))))
1588 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file
))
1589 (make-directory (file-name-directory nnmail-message-id-cache-file
)
1591 (nnmail-write-region (point-min) (point-max)
1592 nnmail-message-id-cache-file nil
'silent
)
1593 (set-buffer-modified-p nil
)
1594 (setq nnmail-cache-buffer nil
)
1595 (gnus-kill-buffer (current-buffer)))))
1597 (defun nnmail-cache-insert (id grp
&optional subject sender
)
1599 ;; this will handle cases like `B r' where the group is nil
1600 (let ((grp (or grp gnus-newsgroup-name
"UNKNOWN")))
1601 (run-hook-with-args 'nnmail-spool-hook
1602 id grp subject sender
))
1603 (when nnmail-treat-duplicates
1604 ;; Store some information about the group this message is written
1605 ;; to. This is passed in as the grp argument -- all locations this
1606 ;; has been called from have been checked and the group is available.
1607 ;; The only ambiguous case is nnmail-check-duplication which will only
1608 ;; pass the first (of possibly >1) group which matches. -Josh
1609 (unless (gnus-buffer-live-p nnmail-cache-buffer
)
1610 (nnmail-cache-open))
1611 (with-current-buffer nnmail-cache-buffer
1612 (goto-char (point-max))
1613 (if (and grp
(not (string= "" grp
))
1614 (gnus-methods-equal-p gnus-command-method
1615 (nnmail-cache-primary-mail-backend)))
1616 (let ((regexp (if (consp nnmail-cache-ignore-groups
)
1617 (mapconcat 'identity nnmail-cache-ignore-groups
1619 nnmail-cache-ignore-groups
)))
1620 (unless (and regexp
(string-match regexp grp
))
1621 (insert id
"\t" grp
"\n")))
1622 (insert id
"\n"))))))
1624 (defun nnmail-cache-primary-mail-backend ()
1625 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods
))
1629 (while (and (null res
) be-list
)
1630 (setq be
(car be-list
))
1631 (setq be-list
(cdr be-list
))
1632 (when (and (gnus-method-option-p be
'respool
)
1634 (intern (format "%s-get-new-mail" (car be
))))
1635 (boundp get-new-mail
)
1636 (symbol-value get-new-mail
))
1640 ;; Fetch the group name corresponding to the message id stored in the
1642 (defun nnmail-cache-fetch-group (id)
1643 (when (and nnmail-treat-duplicates nnmail-cache-buffer
)
1644 (with-current-buffer nnmail-cache-buffer
1645 (goto-char (point-max))
1646 (when (search-backward id nil t
)
1648 (skip-chars-forward "^\n\r\t")
1649 (unless (looking-at "[\r\n]")
1651 (buffer-substring (point) (point-at-eol)))))))
1653 ;; Function for nnmail-split-fancy: look up all references in the
1654 ;; cache and if a match is found, return that group.
1655 (defun nnmail-split-fancy-with-parent ()
1656 "Split this message into the same group as its parent.
1657 This function can be used as an entry in `nnmail-split-fancy', for
1658 example like this: (: nnmail-split-fancy-with-parent)
1659 For a message to be split, it looks for the parent message in the
1660 References or In-Reply-To header and then looks in the message id
1661 cache file (given by the variable `nnmail-message-id-cache-file') to
1662 see which group that message was put in. This group is returned.
1664 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1665 (let* ((refstr (or (message-fetch-field "references")
1666 (message-fetch-field "in-reply-to")))
1669 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups
)
1671 (lambda (x) (format "\\(%s\\)" x
))
1672 nnmail-split-fancy-with-parent-ignore-groups
1674 nnmail-split-fancy-with-parent-ignore-groups
)))
1676 (setq references
(nreverse (gnus-split-references refstr
)))
1677 (unless (gnus-buffer-live-p nnmail-cache-buffer
)
1678 (nnmail-cache-open))
1679 (dolist (x references
)
1680 (setq res
(or (nnmail-cache-fetch-group x
) res
))
1681 (when (or (member res
'("delayed" "drafts" "queue"))
1682 (and regexp res
(string-match regexp res
)))
1686 (defun nnmail-cache-id-exists-p (id)
1687 (when nnmail-treat-duplicates
1688 (with-current-buffer nnmail-cache-buffer
1689 (goto-char (point-max))
1690 (search-backward id nil t
))))
1692 (defun nnmail-fetch-field (header)
1695 (message-narrow-to-head)
1696 (message-fetch-field header
))))
1698 (defun nnmail-check-duplication (message-id func artnum-func
1699 &optional junk-func
)
1700 (run-hooks 'nnmail-prepare-incoming-message-hook
)
1701 ;; If this is a duplicate message, then we do not save it.
1702 (let* ((duplication (nnmail-cache-id-exists-p message-id
))
1703 (case-fold-search t
)
1704 (action (when duplication
1706 ((memq nnmail-treat-duplicates
'(warn delete
))
1707 nnmail-treat-duplicates
)
1708 ((functionp nnmail-treat-duplicates
)
1709 (funcall nnmail-treat-duplicates message-id
))
1711 nnmail-treat-duplicates
))))
1713 ;; We insert a line that says what the mail source is.
1714 (let ((case-fold-search t
))
1715 (goto-char (point-min))
1716 (re-search-forward "^message-id[ \t]*:" nil t
)
1718 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string
)))
1720 ;; Let the backend save the article (or not).
1723 (funcall func
(setq group-art
1724 (nreverse (nnmail-article-group
1725 artnum-func nil junk-func
))))
1726 (nnmail-cache-insert message-id
(caar group-art
)))
1727 ((eq action
'delete
)
1728 (setq group-art nil
))
1730 ;; We insert a warning.
1731 (let ((case-fold-search t
))
1732 (goto-char (point-min))
1733 (re-search-forward "^message-id[ \t]*:" nil t
)
1736 "Gnus-Warning: This is a duplicate of message " message-id
"\n")
1737 (funcall func
(setq group-art
1738 (nreverse (nnmail-article-group artnum-func
))))))
1740 (funcall func
(setq group-art
1741 (nreverse (nnmail-article-group artnum-func
))))))
1742 ;; Add the group-art list to the history list.
1744 (push group-art nnmail-split-history
)
1745 (delete-region (point-min) (point-max)))))
1749 (defvar nnmail-fetched-sources nil
)
1751 (defun nnmail-get-value (&rest args
)
1752 (let ((sym (intern (apply 'format args
))))
1754 (symbol-value sym
))))
1756 (defun nnmail-get-new-mail (method exit-func temp
1757 &optional group spool-func
)
1758 "Read new incoming mail."
1759 (nnmail-get-new-mail-1 method exit-func temp group nil spool-func
))
1761 (defun nnmail-get-new-mail-per-group ()
1762 "Tell us whether the mail-sources specify that `nnmail-get-new-mail' should
1763 be called once per group or once for all groups."
1764 (or (assq 'group mail-sources
)
1765 (assq 'directory mail-sources
)))
1767 (defun nnmail-get-new-mail-1 (method exit-func temp
1768 group in-group spool-func
)
1769 (let* ((sources mail-sources
)
1775 (when (and (nnmail-get-value "%s-get-new-mail" method
)
1777 (while (setq source
(pop sources
))
1778 ;; Use group's parameter
1779 (when (and (eq (car source
) 'group
)
1783 (gnus-group-find-parameter
1784 (concat (symbol-name method
) ":" group
)
1786 (nnmail-get-new-mail-1 method exit-func temp
1787 group group spool-func
))
1789 ;; Hack to only fetch the contents of a single group's spool file.
1790 (when (and (eq (car source
) 'directory
)
1791 (null nnmail-scan-directory-mail-source-once
)
1793 (mail-source-bind (directory source
)
1794 (setq source
(append source
1800 ,(concat group suffix
)
1801 (file-name-nondirectory file
)))))))))
1802 (when nnmail-fetched-sources
1803 (if (member source nnmail-fetched-sources
)
1805 (push source nnmail-fetched-sources
)
1806 (push source fetching-sources
)))))
1807 (when fetching-sources
1808 ;; We first activate all the groups.
1809 (nnmail-activate method
)
1810 ;; Allow the user to hook.
1811 (run-hooks 'nnmail-pre-get-new-mail-hook
)
1812 ;; Open the message-id cache.
1814 ;; The we go through all the existing mail source specification
1815 ;; and fetch the mail from each.
1816 (while (setq source
(pop fetching-sources
))
1818 (condition-case cond
1822 `(lambda (file orig-file
)
1823 (nnmail-split-incoming
1824 file
',(intern (format "%s-save-mail" method
))
1827 (if (equal file orig-file
)
1829 (nnmail-get-split-group orig-file
1831 ',(intern (format "%s-active-number" method
))))))
1833 (message "Mail source %s failed: %s" source cond
)
1837 ;; If we did indeed read any incoming spools, we save all info.
1839 (when mail-source-plugged
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.
1857 If TIME is nil, then return the cutoff time for oldness instead."
1862 (let ((days (or (and nnmail-expiry-wait-function
1863 (funcall nnmail-expiry-wait-function group
))
1864 nnmail-expiry-wait
)))
1865 (cond ((or (eq days
'never
)
1868 ;; This isn't an expirable group.
1870 ((eq days
'immediate
)
1871 ;; We expire all articles on sight.
1875 ((equal time
'(0 0))
1876 ;; This is an ange-ftp group, and we don't have any dates.
1879 (setq days
(days-to-time days
))
1880 ;; Compare the time with the current time.
1882 (time-subtract (current-time) days
)
1883 (ignore-errors (time-less-p days
(time-since time
)))))))))
1885 (declare-function gnus-group-mark-article-read
"gnus-group" (group article
))
1887 (defun nnmail-expiry-target-group (target group
)
1888 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1889 ;; that buffer if the nnfolder group isn't selected.
1890 (let (nnmail-cache-accepted-message-ids)
1891 ;; Don't enter Message-IDs into cache.
1892 ;; Let users hack it in TARGET function.
1893 (when (functionp target
)
1894 (setq target
(funcall target group
)))
1895 (unless (eq target
'delete
)
1896 (when (or (gnus-request-group target nil nil
(gnus-get-info target
))
1897 (gnus-request-create-group target
))
1898 (let ((group-art (gnus-request-accept-article target nil nil t
)))
1899 (when (and (consp group-art
)
1901 (gnus-group-mark-article-read target
(cdr group-art
))))))))
1903 (defun nnmail-fancy-expiry-target (group)
1904 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1906 (case-fold-search nil
)
1907 (from (or (message-fetch-field "from") ""))
1908 (to (or (message-fetch-field "to") ""))
1909 (date (message-fetch-field "date"))
1915 (message "%s" (error-message-string err
))
1918 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets
) target
)
1919 (setq header
(car regexp-target-pair
))
1921 ;; If the header is to-from then match against the
1922 ;; To or From header
1923 ((and (equal header
'to-from
)
1924 (or (string-match (cadr regexp-target-pair
) from
)
1925 (and (string-match (cadr regexp-target-pair
) to
)
1926 (let ((mail-dont-reply-to-names
1927 (message-dont-reply-to-names)))
1928 (equal (if (fboundp 'rmail-dont-reply-to
)
1929 (rmail-dont-reply-to from
)
1930 (mail-dont-reply-to from
)) "")))))
1931 (setq target
(format-time-string (caddr regexp-target-pair
) date
)))
1932 ((and (not (equal header
'to-from
))
1933 (string-match (cadr regexp-target-pair
)
1935 (message-fetch-field header
)
1938 (format-time-string (caddr regexp-target-pair
) date
)))))))
1940 (defun nnmail-check-syntax ()
1941 "Check (and modify) the syntax of the message in the current buffer."
1943 (message-narrow-to-head)
1944 (let ((case-fold-search t
))
1945 (unless (re-search-forward "^Message-ID[ \t]*:" nil t
)
1946 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1948 (defun nnmail-write-region (start end filename
&optional append visit lockname
)
1949 "Do a `write-region', and then set the file modes."
1950 (let ((coding-system-for-write nnmail-file-coding-system
)
1951 (file-name-coding-system nnmail-pathname-coding-system
))
1952 (write-region start end filename append visit lockname
)
1953 (set-file-modes filename nnmail-default-file-modes
)))
1956 ;;; Status functions
1959 (defun nnmail-replace-status (name value
)
1960 "Make status NAME and VALUE part of the current status line."
1962 (message-narrow-to-head)
1963 (let ((status (nnmail-decode-status)))
1964 (setq status
(delq (member name status
) status
))
1966 (push (cons name value
) status
))
1967 (message-remove-header "status")
1968 (goto-char (point-max))
1969 (insert "Status: " (nnmail-encode-status status
) "\n"))))
1971 (defun nnmail-decode-status ()
1972 "Return a status-value alist from STATUS."
1973 (goto-char (point-min))
1974 (when (re-search-forward "^Status: " nil t
)
1975 (let (name value status
)
1977 ;; Narrow to the status.
1980 (if (re-search-forward "^[^ \t]" nil t
)
1983 ;; Go through all elements and add them to the list.
1984 (goto-char (point-min))
1985 (while (re-search-forward "[^ \t=]+" nil t
)
1986 (setq name
(match-string 0))
1987 (if (not (eq (char-after) ?
=))
1991 (if (not (eq (char-after) ?
\"))
1992 (if (not (looking-at "[^ \t]"))
1996 (setq value
(match-string 0))
1997 (goto-char (match-end 0)))
1999 (setq value
(read (current-buffer)))))
2000 (push (cons name value
) status
)))
2003 (defun nnmail-encode-status (status)
2004 "Return a status string from STATUS."
2009 (if (string-match "[ \t]" (cdr elem
))
2010 (prin1-to-string (cdr elem
))
2014 (defun nnmail-split-history ()
2015 "Generate an overview of where the last mail split put articles."
2017 (unless nnmail-split-history
2018 (error "No current split history"))
2019 (with-output-to-temp-buffer "*nnmail split history*"
2020 (with-current-buffer standard-output
2022 (dolist (elem nnmail-split-history
)
2023 (princ (mapconcat (lambda (ga)
2024 (concat (car ga
) ":" (int-to-string (cdr ga
))))
2029 (defun nnmail-purge-split-history (group)
2030 "Remove all instances of GROUP from `nnmail-split-history'."
2031 (let ((history nnmail-split-history
))
2033 (setcar history
(gnus-remove-if (lambda (e) (string= (car e
) group
))
2036 (setq nnmail-split-history
(delq nil nnmail-split-history
))))
2038 (defun nnmail-new-mail-p (group)
2039 "Say whether GROUP has new mail."
2040 (let ((his nnmail-split-history
)
2043 (when (assoc group
(pop his
))
2048 (defun nnmail-within-headers-p ()
2049 "Check to see if point is within the headers of a unix mail message.
2050 Doesn't change point."
2051 (let ((pos (point)))
2053 (and (nnmail-search-unix-mail-delim-backward)
2054 (not (search-forward "\n\n" pos t
))))))
2056 (run-hooks 'nnmail-load-hook
)
2060 ;;; nnmail.el ends here