1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
3 ;; Copyright (C) 1995-2018 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 <https://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl-lib
))
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 (eq 'windows-nt 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 (list .
"list-id\\|list-post\\|x-mailing-list\||x-beenthere\\|x-loop"))
493 "Alist of abbreviations allowed in `nnmail-split-fancy'."
495 :type
'(repeat (cons :format
"%v" symbol regexp
)))
497 (defcustom nnmail-message-id-cache-length
1000
498 "The approximate number of Message-IDs nnmail will keep in its cache.
499 If this variable is nil, no checking on duplicate messages will be
501 :group
'nnmail-duplicate
502 :type
'(choice (const :tag
"disable" nil
)
503 (integer :format
"%v")))
505 (defcustom nnmail-message-id-cache-file
506 (nnheader-concat gnus-home-directory
".nnmail-cache")
507 "The file name of the nnmail Message-ID cache."
508 :group
'nnmail-duplicate
512 (defcustom nnmail-treat-duplicates
'warn
513 "If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
514 Three values are valid: nil, which means that nnmail is not to keep a
515 Message-ID cache; `warn', which means that nnmail should insert extra
516 headers to warn the user about the duplication (this is the default);
517 and `delete', which means that nnmail will delete duplicated mails.
519 This variable can also be a function. It will be called from a buffer
520 narrowed to the article in question with the Message-ID as a
521 parameter. It should return nil, `warn' or `delete'."
522 :group
'nnmail-duplicate
523 :type
'(choice (const :tag
"off" nil
)
527 (defcustom nnmail-extra-headers
'(To Newsgroups Cc
)
528 "Extra headers to parse.
529 In addition to the standard headers, these extra headers will be
530 included in NOV headers (and the like) when backends parse headers."
533 :type
'(repeat symbol
))
535 (defcustom nnmail-split-header-length-limit
2048
536 "Header lines longer than this limit are excluded from the split function."
541 (defcustom nnmail-mail-splitting-charset nil
542 "Default charset to be used when splitting incoming mail."
547 (defcustom nnmail-mail-splitting-decodes nil
548 "Whether the nnmail splitting functionality should MIME decode headers."
553 (defcustom nnmail-split-fancy-match-partial-words nil
554 "Whether to match partial words when fancy splitting.
555 Normally, regexes given in `nnmail-split-fancy' are implicitly surrounded
556 by \"\\=\\<...\\>\". If this variable is true, they are not implicitly\
563 (defcustom nnmail-split-lowercase-expanded t
564 "Whether to lowercase expanded entries (i.e. \\N) when splitting mails.
565 This avoids the creation of multiple groups when users send to an address
566 using different case (i.e. mailing-list@domain vs Mailing-List@Domain)."
571 ;;; Internal variables.
573 (defvar nnmail-article-buffer
" *nnmail incoming*"
574 "The buffer used for splitting incoming mails.")
576 (defvar nnmail-split-history nil
577 "List of group/article elements that say where the previous split put messages.")
579 (defvar nnmail-split-fancy-syntax-table
580 (let ((table (make-syntax-table)))
581 ;; support the %-hack
582 (modify-syntax-entry ?\%
"." table
)
584 "Syntax table used by `nnmail-split-fancy'.")
586 (defvar nnmail-prepare-save-mail-hook nil
587 "Hook called before saving mail.")
589 (defvar nnmail-split-tracing nil
)
590 (defvar nnmail-split-trace nil
)
591 (defvar nnmail-inhibit-default-split-group nil
)
595 (defun nnmail-request-post (&optional server
)
596 (mail-send-and-exit nil
))
598 (defvar nnmail-file-coding-system
'raw-text
599 "Coding system used in nnmail.")
601 (defvar nnmail-incoming-coding-system
602 mm-text-coding-system
603 "Coding system used in reading inbox")
605 (defcustom nnmail-pathname-coding-system nil
606 "Coding system for file name."
607 :group
'nnmail-various
608 :type
'coding-system
)
610 (defun nnmail-find-file (file)
611 "Insert FILE in server buffer safely."
612 (set-buffer nntp-server-buffer
)
613 (delete-region (point-min) (point-max))
614 (let ((format-alist nil
)
615 (after-insert-file-functions nil
))
617 (let ((coding-system-for-read nnmail-file-coding-system
)
618 (auto-mode-alist (mm-auto-mode-alist))
619 (file-name-coding-system nnmail-pathname-coding-system
))
620 (insert-file-contents file
)
624 (defun nnmail-group-pathname (group dir
&optional file
)
625 "Make file name for GROUP."
627 (let ((dir (file-name-as-directory (expand-file-name dir
))))
628 (setq group
(nnheader-replace-duplicate-chars-in-string
629 (nnheader-replace-chars-in-string group ?
/ ?_
)
631 (setq group
(nnheader-translate-file-chars group
))
632 ;; If this directory exists, we use it directly.
633 (file-name-as-directory
634 (if (or nnmail-use-long-file-names
635 (file-directory-p (concat dir group
)))
636 (expand-file-name group dir
)
637 ;; If not, we translate dots into slashes.
639 (nnheader-replace-chars-in-string group ?. ?
/)
643 (defun nnmail-get-active ()
644 "Returns an assoc of group names and active ranges.
645 nn*-request-list should have been called before calling this function."
646 ;; Go through all groups from the active list.
647 (with-current-buffer nntp-server-buffer
648 (nnmail-parse-active)))
650 (defun nnmail-parse-active ()
651 "Parse the active file in the current buffer and return an alist."
652 (goto-char (point-min))
653 (unless (re-search-forward "[\\\"]" nil t
)
654 (goto-char (point-max))
655 (while (re-search-backward "[][';?()#]" nil t
)
657 (goto-char (point-min))
658 (let ((buffer (current-buffer))
659 group-assoc group max min
)
663 (narrow-to-region (point) (point-at-eol))
664 (setq group
(read buffer
))
665 (unless (stringp group
)
666 (setq group
(symbol-name group
)))
667 (if (and (numberp (setq max
(read buffer
)))
668 (numberp (setq min
(read buffer
))))
669 (push (list group
(cons min max
))
676 (defcustom nnmail-active-file-coding-system
'raw-text
677 "Coding system for active file."
678 :group
'nnmail-various
679 :type
'coding-system
)
681 (defun nnmail-save-active (group-assoc file-name
)
682 "Save GROUP-ASSOC in ACTIVE-FILE."
683 (let ((coding-system-for-write nnmail-active-file-coding-system
))
685 (with-temp-file file-name
686 (mm-disable-multibyte)
687 (nnmail-generate-active group-assoc
)))))
689 (defun nnmail-generate-active (alist)
690 "Generate an active file from group-alist ALIST."
693 (while (setq group
(pop alist
))
694 (insert (format "%S %d %d y\n" (intern (car group
)) (cdadr group
)
696 (goto-char (point-max))
697 (while (search-backward "\\." nil t
)
700 (defun nnmail-get-split-group (file source
)
701 "Find out whether this FILE is to be split into GROUP only.
702 If SOURCE is a directory spec, try to return the group name component."
703 (if (eq (car source
) 'directory
)
704 (let ((file (file-name-nondirectory file
)))
705 (mail-source-bind (directory source
)
706 (if (string-match (concat (regexp-quote suffix
) "$") file
)
707 (substring file
0 (match-beginning 0))
711 (defun nnmail-process-babyl-mail-format (func artnum-func
)
712 (let ((case-fold-search t
)
714 start message-id content-length do-search end
)
716 (goto-char (point-min))
718 "\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t
)
719 (goto-char (match-end 0))
720 (delete-region (match-beginning 0) (match-end 0))
724 ;; Skip all the headers in case there are more "From "s...
725 (or (search-forward "\n\n" nil t
)
726 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t
)
727 (search-forward "\^_\^L"))
729 ;; Unquote the ">From " line, if any.
730 (goto-char (point-min))
731 (when (looking-at ">From ")
732 (replace-match "X-From-Line: ") )
733 (run-hooks 'nnmail-prepare-incoming-header-hook
)
734 (goto-char (point-max))
735 ;; Find the Message-ID header.
737 (if (re-search-backward
738 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t
)
739 (setq message-id
(buffer-substring (match-beginning 1)
741 ;; There is no Message-ID here, so we create one.
743 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
745 (insert "Original-")))
747 (insert "Message-ID: " (setq message-id
(nnmail-message-id))
749 ;; Look for a Content-Length header.
750 (if (not (save-excursion
751 (and (re-search-backward
752 "^Content-Length:[ \t]*\\([0-9]+\\)" start t
)
753 (setq content-length
(string-to-number
757 ;; We destroy the header, since none of
758 ;; the backends ever use it, and we do not
759 ;; want to confuse other mailers by having
760 ;; a (possibly) faulty header.
761 (progn (insert "X-") t
))))
764 (if (or (= (+ (point) content-length
) (point-max))
766 (goto-char (+ (point) content-length
))
769 (goto-char (+ (point) content-length
))
770 (setq do-search nil
))
773 ;; Go to the beginning of the next article - or to the end
776 (if (re-search-forward "^\^_" nil t
)
777 (goto-char (match-beginning 0))
778 (goto-char (1- (point-max)))))
779 (delete-char 1) ; delete ^_
782 (narrow-to-region start
(point))
783 (goto-char (point-min))
784 (nnmail-check-duplication message-id func artnum-func
)
786 (setq end
(point-max))))
790 (defsubst nnmail-search-unix-mail-delim
()
791 "Put point at the beginning of the next Unix mbox message."
792 ;; Algorithm used to find the next article in the
793 ;; brain-dead Unix mbox format:
795 ;; 1) Search for "^From ".
796 ;; 2) If we find it, then see whether the previous
797 ;; line is blank and the next line looks like a header.
798 ;; Then it's possible that this is a mail delim, and we use it.
799 (let ((case-fold-search nil
)
802 (if (not (re-search-forward "^From " nil t
))
806 (when (and (or (bobp)
809 (eq (char-after) ?
\n)))
812 (while (looking-at ">From \\|From ")
814 (looking-at "[^ \n\t:]+[ \n\t]*:")))
815 (setq found
'yes
)))))
819 (defun nnmail-search-unix-mail-delim-backward ()
820 "Put point at the beginning of the current Unix mbox message."
821 ;; Algorithm used to find the next article in the
822 ;; brain-dead Unix mbox format:
824 ;; 1) Search for "^From ".
825 ;; 2) If we find it, then see whether the previous
826 ;; line is blank and the next line looks like a header.
827 ;; Then it's possible that this is a mail delim, and we use it.
828 (let ((case-fold-search nil
)
831 (if (not (re-search-backward "^From " nil t
))
835 (when (and (or (bobp)
838 (eq (char-after) ?
\n)))
841 (while (looking-at ">From \\|From ")
843 (looking-at "[^ \n\t:]+[ \n\t]*:")))
844 (setq found
'yes
)))))
848 (defun nnmail-process-unix-mail-format (func artnum-func
)
849 (let ((case-fold-search t
)
851 start message-id content-length end skip head-end
)
852 (goto-char (point-min))
853 (if (not (and (re-search-forward "^From " nil t
)
854 (goto-char (match-beginning 0))))
855 ;; Possibly wrong format?
856 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
857 (if (buffer-file-name) "file" "buffer")
858 (or (buffer-file-name) (buffer-name)))
859 ;; Carry on until the bitter end.
863 ;; Find the end of the head.
866 (if (search-forward "\n\n" nil t
)
868 ;; This will never happen, but just to be on the safe side --
869 ;; if there is no head-body delimiter, we search a bit manually.
870 (while (and (looking-at "From \\|[^ \t]+:")
874 ;; Find the Message-ID header.
875 (goto-char (point-min))
876 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t
)
877 (setq message-id
(match-string 1))
879 (when (re-search-forward "^Message-ID[ \t]*:" nil t
)
881 (insert "Original-")))
882 ;; There is no Message-ID here, so we create one.
884 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
885 ;; Look for a Content-Length header.
886 (goto-char (point-min))
887 (if (not (re-search-forward
888 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t
))
889 (setq content-length nil
)
890 (setq content-length
(string-to-number (match-string 1)))
891 ;; We destroy the header, since none of the backends ever
892 ;; use it, and we do not want to confuse other mailers by
893 ;; having a (possibly) faulty header.
896 (run-hooks 'nnmail-prepare-incoming-header-hook
)
897 ;; Find the end of this article.
898 (goto-char (point-max))
900 (setq head-end
(point))
901 ;; We try the Content-Length value. The idea: skip over the header
902 ;; separator, then check what happens content-length bytes into the
903 ;; message body. This should be either the end of the buffer, the
904 ;; message separator or a blank line followed by the separator.
905 ;; The blank line should probably be deleted. If neither of the
906 ;; three is met, the content-length header is probably invalid.
909 (setq skip
(+ (point) content-length
))
911 (cond ((or (= skip
(point-max))
912 (= (1+ skip
) (point-max)))
913 (setq end
(point-max)))
914 ((looking-at "From ")
916 ((looking-at "[ \t]*\n\\(From \\)")
917 (setq end
(match-beginning 1)))
921 ;; No Content-Length, so we find the beginning of the next
922 ;; article or the end of the buffer.
924 (or (nnmail-search-unix-mail-delim)
925 (goto-char (point-max))))
926 ;; Allow the backend to save the article.
929 (narrow-to-region start
(point))
930 (goto-char (point-min))
932 (nnmail-check-duplication message-id func artnum-func
)
933 (setq end
(point-max))))
937 (defun nnmail-process-mmdf-mail-format (func artnum-func
&optional junk-func
)
938 (let ((delim "^\^A\^A\^A\^A$")
941 start message-id end
)
942 (goto-char (point-min))
943 (if (not (and (re-search-forward delim nil t
)
945 ;; Possibly wrong format?
946 (error "Error, unknown mail format! (Possibly corrupted.)")
947 ;; Carry on until the bitter end.
950 ;; Find the end of the head.
953 (if (search-forward "\n\n" nil t
)
955 ;; This will never happen, but just to be on the safe side --
956 ;; if there is no head-body delimiter, we search a bit manually.
957 (while (and (looking-at "From \\|[^ \t]+:")
961 ;; Find the Message-ID header.
962 (goto-char (point-min))
963 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t
)
964 (setq message-id
(match-string 1))
965 ;; There is no Message-ID here, so we create one.
967 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
969 (insert "Original-")))
971 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
972 (run-hooks 'nnmail-prepare-incoming-header-hook
)
973 ;; Find the end of this article.
974 (goto-char (point-max))
976 (if (re-search-forward delim nil t
)
978 (goto-char (point-max)))
979 ;; Allow the backend to save the article.
982 (narrow-to-region start
(point))
983 (goto-char (point-min))
985 (nnmail-check-duplication message-id func artnum-func junk-func
)
986 (setq end
(point-max))))
991 (defun nnmail-process-maildir-mail-format (func artnum-func
)
992 ;; In a maildir, every file contains exactly one mail.
993 (let ((case-fold-search t
)
995 (goto-char (point-min))
996 ;; Find the end of the head.
999 (if (search-forward "\n\n" nil t
)
1001 ;; This will never happen, but just to be on the safe side --
1002 ;; if there is no head-body delimiter, we search a bit manually.
1003 (while (and (looking-at "From \\|[^ \t]+:")
1007 ;; Find the Message-ID header.
1008 (goto-char (point-min))
1009 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t
)
1010 (setq message-id
(match-string 1))
1011 ;; There is no Message-ID here, so we create one.
1013 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
1015 (insert "Original-")))
1017 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
1018 (run-hooks 'nnmail-prepare-incoming-header-hook
)
1019 ;; Allow the backend to save the article.
1022 (goto-char (point-min))
1023 (nnmail-check-duplication message-id func artnum-func
))
1026 (defvar nnmail-group-names-not-encoded-p nil
1027 "Non-nil means group names are not encoded.")
1029 (defun nnmail-split-incoming (incoming func
&optional exit-func
1030 group artnum-func junk-func
)
1031 "Go through the entire INCOMING file and pick out each individual mail.
1032 FUNC will be called with the buffer narrowed to each mail.
1033 INCOMING can also be a buffer object. In that case, the mail
1034 will be copied over from that buffer."
1035 (let ( ;; If this is a group-specific split, we bind the split
1036 ;; methods to just this group.
1037 (nnmail-split-methods (if (and group
1038 (not nnmail-resplit-incoming
))
1039 (list (list group
""))
1040 nnmail-split-methods
))
1041 (nnmail-group-names-not-encoded-p t
))
1042 ;; Insert the incoming file.
1043 (with-current-buffer (get-buffer-create nnmail-article-buffer
)
1045 (if (bufferp incoming
)
1046 (insert-buffer-substring incoming
)
1047 (let ((coding-system-for-read nnmail-incoming-coding-system
))
1048 (mm-insert-file-contents incoming
)))
1050 (if (zerop (buffer-size))
1052 (goto-char (point-min))
1053 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook
))
1054 ;; Handle both babyl, MMDF and unix mail formats, since
1055 ;; movemail will use the former when fetching from a
1056 ;; mailbox, the latter when fetching from a file.
1057 (cond ((or (looking-at "\^L")
1058 (looking-at "BABYL OPTIONS:"))
1059 (nnmail-process-babyl-mail-format func artnum-func
))
1060 ((looking-at "\^A\^A\^A\^A")
1061 (nnmail-process-mmdf-mail-format
1062 func artnum-func junk-func
))
1063 ((looking-at "Return-Path:")
1064 (nnmail-process-maildir-mail-format func artnum-func
))
1066 (nnmail-process-unix-mail-format func artnum-func
))))
1068 (funcall exit-func
))
1069 (kill-buffer (current-buffer))))))
1071 (defun nnmail-article-group (func &optional trace junk-func
)
1072 "Look at the headers and return an alist of groups that match.
1073 FUNC will be called with the group name to determine the article number."
1074 (let ((methods (or nnmail-split-methods
'(("bogus" ""))))
1075 (obuf (current-buffer))
1076 group-art method grp
)
1077 (if (and (sequencep methods
)
1078 (= (length methods
) 1)
1079 (not nnmail-inhibit-default-split-group
))
1080 ;; If there is only just one group to put everything in, we
1081 ;; just return a list with just this one method in.
1083 (list (cons (caar methods
) (funcall func
(caar methods
)))))
1084 ;; We do actual comparison.
1085 ;; Copy the article into the work buffer.
1086 (with-current-buffer nntp-server-buffer
1088 (insert-buffer-substring obuf
)
1089 ;; Narrow to headers.
1091 (goto-char (point-min))
1092 (if (search-forward "\n\n" nil t
)
1095 (goto-char (point-min))
1096 ;; Decode MIME headers and charsets.
1097 (when nnmail-mail-splitting-decodes
1098 (let ((mail-parse-charset nnmail-mail-splitting-charset
))
1099 (mail-decode-encoded-word-region (point-min) (point-max))))
1100 ;; Fold continuation lines.
1101 (goto-char (point-min))
1102 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t
)
1103 (replace-match " " t t
))
1104 ;; Nuke pathologically long headers. Since Gnus applies
1105 ;; pathologically complex regexps to the buffer, lines
1106 ;; that are looong will take longer than the Universe's
1107 ;; existence to process.
1108 (goto-char (point-min))
1110 (unless (< (move-to-column nnmail-split-header-length-limit
)
1111 nnmail-split-header-length-limit
)
1112 (delete-region (point) (point-at-eol)))
1115 (goto-char (point-min))
1116 (run-hooks 'nnmail-split-hook
)
1117 (when (setq nnmail-split-tracing trace
)
1118 (setq nnmail-split-trace nil
))
1119 (if (or (and (symbolp nnmail-split-methods
)
1120 (fboundp nnmail-split-methods
))
1121 (not (consp (car-safe nnmail-split-methods
)))
1122 (and (listp nnmail-split-methods
)
1123 ;; Not a regular split method, so it has to be a
1125 (not (let ((top-element (car-safe nnmail-split-methods
)))
1126 (and (= 2 (length top-element
))
1127 (stringp (nth 0 top-element
))
1128 (stringp (nth 1 top-element
)))))))
1129 (let* ((method-function
1130 (if (and (symbolp nnmail-split-methods
)
1131 (fboundp nnmail-split-methods
))
1132 nnmail-split-methods
1133 'nnmail-split-fancy
))
1135 (condition-case error-info
1136 ;; `nnmail-split-methods' is a function, so we
1137 ;; just call this function here and use the
1139 (or (funcall method-function
)
1140 (and (not nnmail-inhibit-default-split-group
)
1144 5 "Error in `nnmail-split-methods'; using `bogus' mail group: %S" error-info
)
1147 (setq split
(delete-dups split
))
1148 ;; The article may be "cross-posted" to `junk'. What
1149 ;; to do? Just remove the `junk' spec. Don't really
1150 ;; see anything else to do...
1151 (when (and (memq 'junk split
)
1153 (funcall junk-func
'junk
))
1154 (setq split
(delq 'junk split
))
1158 (lambda (group) (cons group
(funcall func group
)))
1160 ;; Go through the split methods to find a match.
1162 (or nnmail-crosspost
1164 (goto-char (point-max))
1165 (setq method
(pop methods
)
1168 (not (equal "" (nth 1 method
))))
1171 (if (stringp (nth 1 method
))
1172 (let ((expand (string-match "\\\\[0-9&]" grp
))
1173 (pos (re-search-backward (cadr method
)
1176 (setq grp
(nnmail-expand-newtext grp
)))
1178 ;; Function to say whether this is a match.
1179 (funcall (nth 1 method
) grp
)))
1180 ;; Don't enter the article into the same
1182 (not (assoc grp group-art
)))
1183 (push (cons grp
(funcall func grp
))
1185 ;; This is the final group, which is used as a
1187 (when (and (not group-art
)
1188 (or (equal "" (nth 1 method
))
1189 (not nnmail-inhibit-default-split-group
)))
1191 (list (cons (car method
)
1192 (funcall func
(car method
))))))))
1193 ;; Fall back on "bogus" if all else fails.
1194 (when (and (not group-art
)
1195 (not nnmail-inhibit-default-split-group
))
1196 (setq group-art
(list (cons "bogus" (funcall func
"bogus"))))))
1197 ;; Produce a trace if non-empty.
1198 (when (and trace nnmail-split-trace
)
1199 (let ((restore (current-buffer)))
1200 (nnheader-set-temp-buffer "*Split Trace*")
1202 (dolist (trace (nreverse nnmail-split-trace
))
1203 (prin1 trace
(current-buffer))
1205 (goto-char (point-min))
1206 (gnus-configure-windows 'split-trace
)
1207 (set-buffer restore
)))
1209 ;; See whether the split methods returned `junk'.
1210 (if (equal group-art
'(junk))
1212 ;; The article may be "cross-posted" to `junk'. What
1213 ;; to do? Just remove the `junk' spec. Don't really
1214 ;; see anything else to do...
1216 (while (setq elem
(car (memq 'junk group-art
)))
1217 (setq group-art
(delq elem group-art
)))
1218 (nreverse group-art
)))))))
1220 (defun nnmail-insert-lines ()
1221 "Insert how many lines there are in the body of the mail.
1222 Return the number of characters in the body."
1225 (goto-char (point-min))
1226 (unless (search-forward "\n\n" nil t
)
1227 (goto-char (point-max))
1229 (setq chars
(- (point-max) (point)))
1230 (setq lines
(count-lines (point) (point-max)))
1233 (when (re-search-backward "^Lines: " nil t
)
1234 (delete-region (point) (progn (forward-line 1) (point)))))
1236 (insert (format "Lines: %d\n" (max lines
0)))
1239 (defun nnmail-insert-xref (group-alist)
1240 "Insert an Xref line based on the (group . article) alist."
1242 (goto-char (point-min))
1243 (unless (search-forward "\n\n" nil t
)
1244 (goto-char (point-max))
1247 (when (re-search-backward "^Xref: " nil t
)
1248 (delete-region (match-beginning 0)
1249 (progn (forward-line 1) (point))))
1250 (insert (format "Xref: %s" (system-name)))
1252 (insert (if (mm-multibyte-p)
1253 (format " %s:%d" (caar group-alist
) (cdar group-alist
))
1254 (encode-coding-string
1255 (format " %s:%d" (caar group-alist
) (cdar group-alist
))
1257 (setq group-alist
(cdr group-alist
)))
1260 ;;; Message washing functions
1262 (defun nnmail-remove-leading-whitespace ()
1263 "Remove excessive whitespace from all headers."
1264 (goto-char (point-min))
1265 (while (re-search-forward "^\\([^ :]+: \\) +" nil t
)
1266 (replace-match "\\1" t
)))
1268 (defun nnmail-remove-list-identifiers ()
1269 "Remove list identifiers from Subject headers."
1271 (if (consp nnmail-list-identifiers
)
1272 (mapconcat 'identity nnmail-list-identifiers
" *\\|")
1273 nnmail-list-identifiers
)))
1275 (goto-char (point-min))
1276 (while (re-search-forward
1277 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp
" *\\)")
1279 (delete-region (match-beginning 2) (match-end 0))
1280 (beginning-of-line))
1281 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1283 (delete-region (match-beginning 1) (match-end 1))
1284 (beginning-of-line)))))
1286 (defun nnmail-remove-tabs ()
1287 "Translate TAB characters into SPACE characters."
1288 (subst-char-in-region (point-min) (point-max) ?
\t ? t
))
1290 (defcustom nnmail-broken-references-mailers
1291 "^X-Mailer:.*\\(Eudora\\|Pegasus\\)"
1292 "Header line matching mailer producing bogus References lines.
1293 See `nnmail-ignore-broken-references'."
1294 :group
'nnmail-prepare
1295 :version
"23.1" ;; No Gnus
1298 (defun nnmail-ignore-broken-references ()
1299 "Ignore the References line and use In-Reply-To
1301 Eudora has a broken References line, but an OK In-Reply-To."
1302 (goto-char (point-min))
1303 (when (re-search-forward nnmail-broken-references-mailers nil t
)
1304 (goto-char (point-min))
1305 (when (re-search-forward "^References:" nil t
)
1307 (insert "X-Gnus-Broken-Eudora-"))
1308 (goto-char (point-min))
1309 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t
)
1310 (replace-match "\\1" t
))))
1312 (defalias 'nnmail-fix-eudora-headers
'nnmail-ignore-broken-references
)
1313 (make-obsolete 'nnmail-fix-eudora-headers
'nnmail-ignore-broken-references
"Emacs 23.1")
1315 (custom-add-option 'nnmail-prepare-incoming-header-hook
1316 'nnmail-ignore-broken-references
)
1318 ;;; Utility functions
1320 (declare-function gnus-activate-group
"gnus-start"
1321 (group &optional scan dont-check method dont-sub-check
))
1323 (defun nnmail-do-request-post (accept-func &optional server
)
1324 "Utility function to directly post a message to an nnmail-derived group.
1325 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1326 to actually put the message in the right group."
1328 (dolist (mbx (message-unquote-tokens
1329 (message-tokenize-header
1330 (message-fetch-field "Newsgroups") ", ")) success
)
1331 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method
)))
1332 (or (gnus-active to-newsgroup
)
1333 (gnus-activate-group to-newsgroup
)
1334 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1336 (or (and (gnus-request-create-group
1337 to-newsgroup gnus-command-method
)
1338 (gnus-activate-group to-newsgroup nil nil
1339 gnus-command-method
))
1340 (error "Couldn't create group %s" to-newsgroup
)))
1341 (error "No such group: %s" to-newsgroup
))
1342 (unless (funcall accept-func mbx
(nth 1 gnus-command-method
))
1343 (setq success nil
))))))
1345 (defun nnmail-split-fancy ()
1346 "Fancy splitting method.
1347 See the documentation for the variable `nnmail-split-fancy' for details."
1348 (with-syntax-table nnmail-split-fancy-syntax-table
1349 (nnmail-split-it nnmail-split-fancy
)))
1351 (defvar nnmail-split-cache nil
)
1352 ;; Alist of split expressions their equivalent regexps.
1354 (defun nnmail-split-it (split)
1355 ;; Return a list of groups matching SPLIT.
1362 ;; A group name. Do the \& and \N subs into the string.
1364 (when nnmail-split-tracing
1365 (push split nnmail-split-trace
))
1366 (list (nnmail-expand-newtext split t
)))
1368 ;; Junk the message.
1370 (when nnmail-split-tracing
1371 (push "junk" nnmail-split-trace
))
1374 ;; Builtin & operation.
1375 ((eq (car split
) '&)
1376 (mapcan 'nnmail-split-it
(cdr split
)))
1378 ;; Builtin | operation.
1379 ((eq (car split
) '|
)
1381 (while (and (not done
) (cdr split
))
1382 (setq split
(cdr split
)
1383 done
(nnmail-split-it (car split
))))
1386 ;; Builtin : operation.
1387 ((eq (car split
) ':)
1388 (when nnmail-split-tracing
1389 (push split nnmail-split-trace
))
1390 (nnmail-split-it (save-excursion (eval (cdr split
)))))
1392 ;; Builtin ! operation.
1393 ((eq (car split
) '!)
1394 (funcall (cadr split
) (nnmail-split-it (caddr split
))))
1396 ;; Check the cache for the regexp for this split.
1397 ((setq cached-pair
(assq split nnmail-split-cache
))
1400 (end-point (point-max))
1401 (value (nth 1 split
)))
1403 (setq value
(cdr (assq value nnmail-split-abbrev-alist
))))
1404 (while (and (goto-char end-point
)
1405 (re-search-backward (cdr cached-pair
) nil t
))
1406 (setq match-data
(match-data))
1407 (when nnmail-split-tracing
1408 (push split nnmail-split-trace
))
1409 (let ((split-rest (cddr split
))
1411 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1412 ;; So, start-of-value is the point just before the
1413 ;; beginning of the value, whereas after-header-name
1414 ;; is the point just after the field name.
1415 (start-of-value (match-end 1))
1416 (after-header-name (match-end 2)))
1417 ;; Start the next search just before the beginning of the
1419 (setq end-point
(1- start-of-value
))
1420 ;; Handle - RESTRICTs
1421 (while (eq (car split-rest
) '-
)
1422 ;; RESTRICT must start after-header-name and
1423 ;; end after start-of-value, so that, for
1424 ;; (any "foo" - "x-foo" "foo.list")
1425 ;; we do not exclude foo.list just because
1426 ;; the header is: ``To: x-foo, foo''
1428 (if (and (re-search-backward (cadr split-rest
)
1429 after-header-name t
)
1430 (> (match-end 0) start-of-value
))
1431 (setq split-rest nil
)
1432 (setq split-rest
(cddr split-rest
))))
1435 ;; Someone might want to do a \N sub on this match, so
1436 ;; restore the match data.
1437 (set-match-data match-data
)
1438 (dolist (sp (nnmail-split-it (car split-rest
)))
1439 (unless (member sp split-result
)
1440 (push sp split-result
))))))
1443 ;; Not in cache, compute a regexp for the field/value pair.
1445 (let ((field (nth 0 split
))
1446 (value (nth 1 split
))
1447 (split-rest (cddr split
))
1452 (setq value
(cdr (assq value nnmail-split-abbrev-alist
))))
1453 (if (and (>= (length value
) 2)
1454 (string= ".*" (substring value
0 2)))
1455 (setq value
(substring value
2)
1457 ;; Same trick for the rear of the regexp
1458 (if (and (>= (length value
) 2)
1459 (string= ".*" (substring value -
2)))
1460 (setq value
(substring value
0 -
2)
1462 ;; Invert the match-partial-words behavior if the optional
1463 ;; last element is specified.
1464 (while (eq (car split-rest
) '-
)
1465 (setq split-rest
(cddr split-rest
)))
1466 (when (if (cadr split-rest
)
1467 (not nnmail-split-fancy-match-partial-words
)
1468 nnmail-split-fancy-match-partial-words
)
1469 (setq partial-front
""
1471 (setq regexp
(concat "^\\(\\("
1473 (cdr (assq field nnmail-split-abbrev-alist
))
1476 (or partial-front
"\\<")
1480 (or partial-rear
"\\>")))
1481 (push (cons split regexp
) nnmail-split-cache
)
1482 ;; Now that it's in the cache, just call nnmail-split-it again
1483 ;; on the same split, which will find it immediately in the cache.
1484 (nnmail-split-it split
))))))
1486 (defun nnmail-expand-newtext (newtext &optional fancyp
)
1487 (let ((len (length newtext
))
1489 c expanded beg N did-expand
)
1492 (while (and (< pos len
)
1493 (not (= (aref newtext pos
) ?
\\)))
1494 (setq pos
(1+ pos
)))
1496 (push (substring newtext beg pos
) expanded
))
1498 ;; We hit a \; expand it.
1501 c
(aref newtext pos
))
1502 (if (not (or (= c ?\
&)
1505 ;; \ followed by some character we don't expand.
1506 (push (char-to-string c
) expanded
)
1511 ;; We wrapped the searches in parentheses, so we have to
1512 ;; add some parentheses here...
1515 (when (match-beginning N
)
1516 (push (if nnmail-split-lowercase-expanded
1517 (downcase (buffer-substring (match-beginning N
)
1519 (buffer-substring (match-beginning N
) (match-end N
)))
1521 (setq pos
(1+ pos
)))
1523 (apply 'concat
(nreverse expanded
))
1526 ;; Activate a backend only if it isn't already activated.
1527 ;; If FORCE, re-read the active file even if the backend is
1528 ;; already activated.
1529 (defun nnmail-activate (backend &optional force
)
1530 (nnheader-init-server-buffer)
1531 (let (file timestamp file-time
)
1532 (if (or (not (symbol-value (intern (format "%s-group-alist" backend
))))
1534 (and (setq file
(ignore-errors
1535 (symbol-value (intern (format "%s-active-file"
1537 (setq file-time
(nth 5 (file-attributes file
)))
1541 (symbol-value (intern
1542 (format "%s-active-timestamp"
1545 (not (consp timestamp
))
1546 (equal timestamp
'(0 0))
1547 (> (nth 0 file-time
) (nth 0 timestamp
))
1548 (and (= (nth 0 file-time
) (nth 0 timestamp
))
1549 (> (nth 1 file-time
) (nth 1 timestamp
))))))
1551 (or (eq timestamp
'none
)
1552 (set (intern (format "%s-active-timestamp" backend
))
1554 (funcall (intern (format "%s-request-list" backend
)))))
1557 (defun nnmail-message-id ()
1558 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1561 ;;; nnmail duplicate handling
1564 (defvar nnmail-cache-buffer nil
)
1566 (defun nnmail-cache-open ()
1567 (if (or (not nnmail-treat-duplicates
)
1568 (and nnmail-cache-buffer
1569 (buffer-name nnmail-cache-buffer
)))
1570 () ; The buffer is open.
1571 (with-current-buffer
1572 (setq nnmail-cache-buffer
1573 (get-buffer-create " *nnmail message-id cache*"))
1575 (when (file-exists-p nnmail-message-id-cache-file
)
1576 (nnheader-insert-file-contents nnmail-message-id-cache-file
))
1577 (set-buffer-modified-p nil
)
1580 (defun nnmail-cache-close ()
1581 (when (and nnmail-cache-buffer
1582 nnmail-treat-duplicates
1583 (buffer-name nnmail-cache-buffer
)
1584 (buffer-modified-p nnmail-cache-buffer
))
1585 (with-current-buffer nnmail-cache-buffer
1586 ;; Weed out the excess number of Message-IDs.
1587 (goto-char (point-max))
1588 (when (search-backward "\n" nil t nnmail-message-id-cache-length
)
1591 (delete-region (point-min) (point))))
1593 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file
))
1594 (make-directory (file-name-directory nnmail-message-id-cache-file
)
1596 (nnmail-write-region (point-min) (point-max)
1597 nnmail-message-id-cache-file nil
'silent
)
1598 (set-buffer-modified-p nil
)
1599 (setq nnmail-cache-buffer nil
)
1600 (gnus-kill-buffer (current-buffer)))))
1602 (defun nnmail-cache-insert (id grp
&optional subject sender
)
1604 ;; this will handle cases like `B r' where the group is nil
1605 (let ((grp (or grp gnus-newsgroup-name
"UNKNOWN")))
1606 (run-hook-with-args 'nnmail-spool-hook
1607 id grp subject sender
))
1608 (when nnmail-treat-duplicates
1609 ;; Store some information about the group this message is written
1610 ;; to. This is passed in as the grp argument -- all locations this
1611 ;; has been called from have been checked and the group is available.
1612 ;; The only ambiguous case is nnmail-check-duplication which will only
1613 ;; pass the first (of possibly >1) group which matches. -Josh
1614 (unless (gnus-buffer-live-p nnmail-cache-buffer
)
1615 (nnmail-cache-open))
1616 (with-current-buffer nnmail-cache-buffer
1617 (goto-char (point-max))
1618 (if (and grp
(not (string= "" grp
))
1619 (gnus-methods-equal-p gnus-command-method
1620 (nnmail-cache-primary-mail-backend)))
1621 (let ((regexp (if (consp nnmail-cache-ignore-groups
)
1622 (mapconcat 'identity nnmail-cache-ignore-groups
1624 nnmail-cache-ignore-groups
)))
1625 (unless (and regexp
(string-match regexp grp
))
1626 (insert id
"\t" grp
"\n")))
1627 (insert id
"\n"))))))
1629 (defun nnmail-cache-primary-mail-backend ()
1630 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods
))
1634 (while (and (null res
) be-list
)
1635 (setq be
(car be-list
))
1636 (setq be-list
(cdr be-list
))
1637 (when (and (gnus-method-option-p be
'respool
)
1639 (intern (format "%s-get-new-mail" (car be
))))
1640 (boundp get-new-mail
)
1641 (symbol-value get-new-mail
))
1645 ;; Fetch the group name corresponding to the message id stored in the
1647 (defun nnmail-cache-fetch-group (id)
1648 (when (and nnmail-treat-duplicates nnmail-cache-buffer
)
1649 (with-current-buffer nnmail-cache-buffer
1650 (goto-char (point-max))
1651 (when (search-backward id nil t
)
1653 (skip-chars-forward "^\n\r\t")
1654 (unless (looking-at "[\r\n]")
1656 (buffer-substring (point) (point-at-eol)))))))
1658 ;; Function for nnmail-split-fancy: look up all references in the
1659 ;; cache and if a match is found, return that group.
1660 (defun nnmail-split-fancy-with-parent ()
1661 "Split this message into the same group as its parent.
1662 This function can be used as an entry in `nnmail-split-fancy', for
1663 example like this: (: nnmail-split-fancy-with-parent)
1664 For a message to be split, it looks for the parent message in the
1665 References or In-Reply-To header and then looks in the message id
1666 cache file (given by the variable `nnmail-message-id-cache-file') to
1667 see which group that message was put in. This group is returned.
1669 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1670 (let* ((refstr (or (message-fetch-field "references")
1671 (message-fetch-field "in-reply-to")))
1674 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups
)
1676 (lambda (x) (format "\\(%s\\)" x
))
1677 nnmail-split-fancy-with-parent-ignore-groups
1679 nnmail-split-fancy-with-parent-ignore-groups
)))
1681 (setq references
(nreverse (gnus-split-references refstr
)))
1682 (unless (gnus-buffer-live-p nnmail-cache-buffer
)
1683 (nnmail-cache-open))
1684 (dolist (x references
)
1685 (setq res
(or (nnmail-cache-fetch-group x
) res
))
1686 (when (or (member res
'("delayed" "drafts" "queue"))
1687 (and regexp res
(string-match regexp res
)))
1691 (defun nnmail-cache-id-exists-p (id)
1692 (when nnmail-treat-duplicates
1693 (with-current-buffer nnmail-cache-buffer
1694 (goto-char (point-max))
1695 (search-backward id nil t
))))
1697 (defun nnmail-fetch-field (header)
1700 (message-narrow-to-head)
1701 (message-fetch-field header
))))
1703 (defun nnmail-check-duplication (message-id func artnum-func
1704 &optional junk-func
)
1705 (run-hooks 'nnmail-prepare-incoming-message-hook
)
1706 ;; If this is a duplicate message, then we do not save it.
1707 (let* ((duplication (nnmail-cache-id-exists-p message-id
))
1708 (case-fold-search t
)
1709 (action (when duplication
1711 ((memq nnmail-treat-duplicates
'(warn delete
))
1712 nnmail-treat-duplicates
)
1713 ((functionp nnmail-treat-duplicates
)
1714 (funcall nnmail-treat-duplicates message-id
))
1716 nnmail-treat-duplicates
))))
1718 ;; We insert a line that says what the mail source is.
1719 (let ((case-fold-search t
))
1720 (goto-char (point-min))
1721 (re-search-forward "^message-id[ \t]*:" nil t
)
1723 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string
)))
1725 ;; Let the backend save the article (or not).
1728 (funcall func
(setq group-art
1729 (nreverse (nnmail-article-group
1730 artnum-func nil junk-func
))))
1731 (nnmail-cache-insert message-id
(caar group-art
)))
1732 ((eq action
'delete
)
1733 (setq group-art nil
))
1735 ;; We insert a warning.
1736 (let ((case-fold-search t
))
1737 (goto-char (point-min))
1738 (re-search-forward "^message-id[ \t]*:" nil t
)
1741 "Gnus-Warning: This is a duplicate of message " message-id
"\n")
1742 (funcall func
(setq group-art
1743 (nreverse (nnmail-article-group artnum-func
))))))
1745 (funcall func
(setq group-art
1746 (nreverse (nnmail-article-group artnum-func
))))))
1747 ;; Add the group-art list to the history list.
1749 (push group-art nnmail-split-history
)
1750 (delete-region (point-min) (point-max)))))
1754 (defvar nnmail-fetched-sources nil
)
1756 (defun nnmail-get-value (&rest args
)
1757 (let ((sym (intern (apply 'format args
))))
1759 (symbol-value sym
))))
1761 (defun nnmail-get-new-mail (method exit-func temp
1762 &optional group spool-func
)
1763 "Read new incoming mail."
1764 (nnmail-get-new-mail-1 method exit-func temp group nil spool-func
))
1766 (defun nnmail-get-new-mail-per-group ()
1767 "Tell us whether the mail-sources specify that `nnmail-get-new-mail' should
1768 be called once per group or once for all groups."
1769 (or (assq 'group mail-sources
)
1770 (assq 'directory mail-sources
)))
1772 (defun nnmail-get-new-mail-1 (method exit-func temp
1773 group in-group spool-func
)
1774 (let* ((sources mail-sources
)
1780 (when (and (nnmail-get-value "%s-get-new-mail" method
)
1782 (while (setq source
(pop sources
))
1783 ;; Use group's parameter
1784 (when (and (eq (car source
) 'group
)
1788 (gnus-group-find-parameter
1789 (concat (symbol-name method
) ":" group
)
1791 (nnmail-get-new-mail-1 method exit-func temp
1792 group group spool-func
))
1794 ;; Hack to only fetch the contents of a single group's spool file.
1795 (when (and (eq (car source
) 'directory
)
1796 (null nnmail-scan-directory-mail-source-once
)
1798 (mail-source-bind (directory source
)
1799 (setq source
(append source
1805 ,(concat group suffix
)
1806 (file-name-nondirectory file
)))))))))
1807 (when nnmail-fetched-sources
1808 (if (member source nnmail-fetched-sources
)
1810 (push source nnmail-fetched-sources
)
1811 (push source fetching-sources
)))))
1812 (when fetching-sources
1813 ;; We first activate all the groups.
1814 (nnmail-activate method
)
1815 ;; Allow the user to hook.
1816 (run-hooks 'nnmail-pre-get-new-mail-hook
)
1817 ;; Open the message-id cache.
1819 ;; The we go through all the existing mail source specification
1820 ;; and fetch the mail from each.
1821 (while (setq source
(pop fetching-sources
))
1823 (condition-case cond
1827 `(lambda (file orig-file
)
1828 (nnmail-split-incoming
1829 file
',(intern (format "%s-save-mail" method
))
1832 (if (equal file orig-file
)
1834 (nnmail-get-split-group orig-file
1836 ',(intern (format "%s-active-number" method
))))))
1838 (message "Mail source %s failed: %s" source cond
)
1842 ;; If we did indeed read any incoming spools, we save all info.
1844 (when mail-source-plugged
1845 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1846 method
(car source
)))
1848 (nnmail-get-value "%s-group-alist" method
)
1849 (nnmail-get-value "%s-active-file" method
))
1851 (funcall exit-func
))
1852 (run-hooks 'nnmail-read-incoming-hook
)
1853 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1855 ;; Close the message-id cache.
1856 (nnmail-cache-close)
1857 ;; Allow the user to hook.
1858 (run-hooks 'nnmail-post-get-new-mail-hook
))))
1860 (defun nnmail-expired-article-p (group time force
&optional inhibit
)
1861 "Say whether an article that is TIME old in GROUP should be expired.
1862 If TIME is nil, then return the cutoff time for oldness instead."
1867 (let ((days (or (and nnmail-expiry-wait-function
1868 (funcall nnmail-expiry-wait-function group
))
1869 nnmail-expiry-wait
)))
1870 (cond ((or (eq days
'never
)
1873 ;; This isn't an expirable group.
1875 ((eq days
'immediate
)
1876 ;; We expire all articles on sight.
1880 ((equal time
'(0 0))
1881 ;; This is an ange-ftp group, and we don't have any dates.
1884 (setq days
(days-to-time days
))
1885 ;; Compare the time with the current time.
1887 (time-subtract nil days
)
1888 (ignore-errors (time-less-p days
(time-since time
)))))))))
1890 (declare-function gnus-group-mark-article-read
"gnus-group" (group article
))
1892 (defun nnmail-expiry-target-group (target group
)
1893 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1894 ;; that buffer if the nnfolder group isn't selected.
1895 (let (nnmail-cache-accepted-message-ids)
1896 ;; Don't enter Message-IDs into cache.
1897 ;; Let users hack it in TARGET function.
1898 (when (functionp target
)
1899 (setq target
(funcall target group
)))
1900 (unless (eq target
'delete
)
1901 (when (or (gnus-request-group target nil nil
(gnus-get-info target
))
1902 (gnus-request-create-group target
))
1903 (let ((group-art (gnus-request-accept-article target nil t t
)))
1904 (when (and (consp group-art
)
1906 (gnus-group-mark-article-read target
(cdr group-art
))))))))
1908 (defun nnmail-fancy-expiry-target (group)
1909 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1911 (case-fold-search nil
)
1912 (from (or (message-fetch-field "from") ""))
1913 (to (or (message-fetch-field "to") ""))
1914 (date (message-fetch-field "date"))
1920 (message "%s" (error-message-string err
))
1923 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets
) target
)
1924 (setq header
(car regexp-target-pair
))
1926 ;; If the header is to-from then match against the
1927 ;; To or From header
1928 ((and (equal header
'to-from
)
1929 (or (string-match (cadr regexp-target-pair
) from
)
1930 (and (string-match (cadr regexp-target-pair
) to
)
1931 (let ((mail-dont-reply-to-names
1932 (message-dont-reply-to-names)))
1933 (equal (if (fboundp 'rmail-dont-reply-to
)
1934 (rmail-dont-reply-to from
)
1935 (mail-dont-reply-to from
)) "")))))
1936 (setq target
(format-time-string (caddr regexp-target-pair
) date
)))
1937 ((and (not (equal header
'to-from
))
1938 (string-match (cadr regexp-target-pair
)
1940 (message-fetch-field header
)
1943 (format-time-string (caddr regexp-target-pair
) date
)))))))
1945 (defun nnmail-check-syntax ()
1946 "Check (and modify) the syntax of the message in the current buffer."
1948 (message-narrow-to-head)
1949 (let ((case-fold-search t
))
1950 (unless (re-search-forward "^Message-ID[ \t]*:" nil t
)
1951 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1953 (defun nnmail-write-region (start end filename
&optional append visit lockname
)
1954 "Do a `write-region', and then set the file modes."
1955 (let ((coding-system-for-write nnmail-file-coding-system
)
1956 (file-name-coding-system nnmail-pathname-coding-system
))
1957 (write-region start end filename append visit lockname
)
1958 (set-file-modes filename nnmail-default-file-modes
)))
1961 ;;; Status functions
1964 (defun nnmail-replace-status (name value
)
1965 "Make status NAME and VALUE part of the current status line."
1967 (message-narrow-to-head)
1968 (let ((status (nnmail-decode-status)))
1969 (setq status
(delq (member name status
) status
))
1971 (push (cons name value
) status
))
1972 (message-remove-header "status")
1973 (goto-char (point-max))
1974 (insert "Status: " (nnmail-encode-status status
) "\n"))))
1976 (defun nnmail-decode-status ()
1977 "Return a status-value alist from STATUS."
1978 (goto-char (point-min))
1979 (when (re-search-forward "^Status: " nil t
)
1980 (let (name value status
)
1982 ;; Narrow to the status.
1985 (if (re-search-forward "^[^ \t]" nil t
)
1988 ;; Go through all elements and add them to the list.
1989 (goto-char (point-min))
1990 (while (re-search-forward "[^ \t=]+" nil t
)
1991 (setq name
(match-string 0))
1992 (if (not (eq (char-after) ?
=))
1996 (if (not (eq (char-after) ?
\"))
1997 (if (not (looking-at "[^ \t]"))
2001 (setq value
(match-string 0))
2002 (goto-char (match-end 0)))
2004 (setq value
(read (current-buffer)))))
2005 (push (cons name value
) status
)))
2008 (defun nnmail-encode-status (status)
2009 "Return a status string from STATUS."
2014 (if (string-match "[ \t]" (cdr elem
))
2015 (prin1-to-string (cdr elem
))
2019 (defun nnmail-split-history ()
2020 "Generate an overview of where the last mail split put articles."
2022 (unless nnmail-split-history
2023 (error "No current split history"))
2024 (with-output-to-temp-buffer "*nnmail split history*"
2025 (with-current-buffer standard-output
2027 (dolist (elem nnmail-split-history
)
2028 (princ (mapconcat (lambda (ga)
2029 (concat (car ga
) ":" (int-to-string (cdr ga
))))
2034 (defun nnmail-purge-split-history (group)
2035 "Remove all instances of GROUP from `nnmail-split-history'."
2036 (let ((history nnmail-split-history
))
2038 (setcar history
(seq-remove (lambda (e) (string= (car e
) group
))
2041 (setq nnmail-split-history
(delq nil nnmail-split-history
))))
2043 (defun nnmail-new-mail-p (group)
2044 "Say whether GROUP has new mail."
2045 (let ((his nnmail-split-history
)
2048 (when (assoc group
(pop his
))
2053 (defun nnmail-within-headers-p ()
2054 "Check to see if point is within the headers of a unix mail message.
2055 Doesn't change point."
2056 (let ((pos (point)))
2058 (and (nnmail-search-unix-mail-delim-backward)
2059 (not (search-forward "\n\n" pos t
))))))
2061 (run-hooks 'nnmail-load-hook
)
2065 ;;; nnmail.el ends here