1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news, mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl
))
32 (require 'gnus
) ; for macro gnus-kill-buffer, at least
37 (require 'mail-source
)
41 (autoload 'gnus-add-buffer
"gnus")
42 (autoload 'gnus-kill-buffer
"gnus"))
45 "Reading mail with Gnus."
48 (defgroup nnmail-retrieve nil
49 "Retrieving new mail."
52 (defgroup nnmail-prepare nil
53 "Preparing (or mangling) new mail after retrieval."
56 (defgroup nnmail-duplicate nil
57 "Handling of duplicate mail messages."
60 (defgroup nnmail-split nil
61 "Organizing the incoming mail in folders."
64 (defgroup nnmail-files nil
69 (defgroup nnmail-expire nil
73 (defgroup nnmail-procmail nil
74 "Interfacing with procmail and other mail agents."
77 (defgroup nnmail-various nil
78 "Various mail options."
81 (defcustom nnmail-split-methods
'(("mail.misc" ""))
82 "*Incoming mail will be split according to this variable.
84 If you'd like, for instance, one mail group for mail from the
85 \"4ad-l\" mailing list, one group for junk mail and one for everything
86 else, you could do something like this:
88 (setq nnmail-split-methods
89 '((\"mail.4ad\" \"From:.*4ad\")
90 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
91 (\"mail.misc\" \"\")))
93 As you can see, this variable is a list of lists, where the first
94 element in each \"rule\" is the name of the group (which, by the way,
95 does not have to be called anything beginning with \"mail\",
96 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
97 nnmail will try to match on the header to find a fit.
99 The second element can also be a function. In that case, it will be
100 called narrowed to the headers with the first element of the rule as
101 the argument. It should return a non-nil value if it thinks that the
102 mail belongs in that group.
104 The last element should always have \"\" as the regexp.
106 This variable can also have a function as its value."
108 :type
'(choice (repeat :tag
"Alist" (group (string :tag
"Name")
109 (choice regexp function
)))
110 (function-item nnmail-split-fancy
)
111 (function :tag
"Other")))
113 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
114 (defcustom nnmail-crosspost t
115 "If non-nil, do crossposting if several split methods match the mail.
116 If nil, the first match found will be used."
120 (defcustom nnmail-split-fancy-with-parent-ignore-groups nil
121 "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
122 This can also be a list of regexps."
125 :type
'(choice (const :tag
"none" nil
)
127 (repeat :value
(".*") regexp
)))
129 (defcustom nnmail-cache-ignore-groups nil
130 "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
131 This can also be a list of regexps."
134 :type
'(choice (const :tag
"none" nil
)
136 (repeat :value
(".*") regexp
)))
138 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
139 (defcustom nnmail-keep-last-article nil
140 "If non-nil, nnmail will never delete/move a group's last article.
141 It can be marked expirable, so it will be deleted when it is no longer last.
143 You may need to set this variable if other programs are putting
144 new mail into folder numbers that Gnus has marked as expired."
145 :group
'nnmail-procmail
146 :group
'nnmail-various
149 (defcustom nnmail-use-long-file-names nil
150 "If non-nil the mail backends will use long file and directory names.
151 If nil, groups like \"mail.misc\" will end up in directories like
156 (defcustom nnmail-default-file-modes
384
157 "Set the mode bits of all new mail files to this integer."
161 (defcustom nnmail-expiry-wait
7
162 "*Expirable articles that are older than this will be expired.
163 This variable can either be a number (which will be interpreted as a
164 number of days) -- this doesn't have to be an integer. This variable
165 can also be `immediate' and `never'."
166 :group
'nnmail-expire
167 :type
'(choice (const immediate
)
171 (defcustom nnmail-expiry-wait-function nil
172 "Variable that holds function to specify how old articles should be before they are expired.
173 The function will be called with the name of the group that the expiry
174 is to be performed in, and it should return an integer that says how
175 many days an article can be stored before it is considered \"old\".
176 It can also return the values `never' and `immediate'.
180 \(setq nnmail-expiry-wait-function
182 (cond ((string-match \"private\" newsgroup) 31)
183 ((string-match \"junk\" newsgroup) 1)
184 ((string-match \"important\" newsgroup) 'never)
186 :group
'nnmail-expire
187 :type
'(choice (const :tag
"nnmail-expiry-wait" nil
)
188 (function :format
"%v" nnmail-
)))
190 (defcustom nnmail-expiry-target
'delete
191 "*Variable that says where expired messages should end up.
192 The default value is `delete' (which says to delete the messages),
193 but it can also be a string or a function. If it is a string, expired
194 messages end up in that group. If it is a function, the function is
195 called in a buffer narrowed to the message in question. The function
196 receives one argument, the name of the group the message comes from.
197 The return value should be `delete' or a group name (a string)."
199 :group
'nnmail-expire
200 :type
'(choice (const delete
)
201 (function :format
"%v" nnmail-
)
204 (defcustom nnmail-fancy-expiry-targets nil
205 "Determine expiry target based on articles using fancy techniques.
207 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
208 `nnmail-expiry-target' is set to the function
209 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
210 the message will be expired to a group determined by invoking
211 `format-time-string' with TARGET used as the format string and the
212 time extracted from the articles' Date header (if missing the current
215 In the special cases that HEADER is the symbol `to-from', the regexp
216 will try to match against both the From and the To header.
220 \(setq nnmail-fancy-expiry-targets
221 '((to-from \"boss\" \"nnfolder:Work\")
222 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
223 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
225 In this case, articles containing the string \"boss\" in the To or the
226 From header will be expired to the group \"nnfolder:Work\";
227 articles containing the sting \"IMPORTANT\" in the Subject header will
228 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
229 everything else will be expired to \"nnfolder:Archive-YYYY\"."
231 :group
'nnmail-expire
232 :type
'(repeat (list (choice :tag
"Match against"
233 (string :tag
"Header")
236 (string :tag
"Target group format string"))))
238 (defcustom nnmail-cache-accepted-message-ids nil
239 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
240 If non-nil, also update the cache when copy or move articles."
244 (defcustom nnmail-spool-file
'((file))
245 "*Where the mail backends will look for incoming mail.
246 This variable is a list of mail source specifiers.
247 This variable is obsolete; `mail-sources' should be used instead."
251 (defcustom nnmail-resplit-incoming nil
252 "*If non-nil, re-split incoming procmail sorted mail."
253 :group
'nnmail-procmail
256 (defcustom nnmail-scan-directory-mail-source-once nil
257 "*If non-nil, scan all incoming procmail sorted mails once.
258 It scans low-level sorted spools even when not required."
260 :group
'nnmail-procmail
263 (defcustom nnmail-delete-file-function
'delete-file
264 "Function called to delete files in some mail backends."
268 (defcustom nnmail-crosspost-link-function
269 (if (string-match "windows-nt\\|emx" (symbol-name system-type
))
272 "*Function called to create a copy of a file.
273 This is `add-name-to-file' by default, which means that crossposts
274 will use hard links. If your file system doesn't allow hard
275 links, you could set this variable to `copy-file' instead."
277 :type
'(radio (function-item add-name-to-file
)
278 (function-item copy-file
)
279 (function :tag
"Other")))
281 (defcustom nnmail-read-incoming-hook
282 (if (eq system-type
'windows-nt
)
283 '(nnheader-ms-strip-cr)
285 "*Hook that will be run after the incoming mail has been transferred.
286 The incoming mail is moved from the specified spool file (which normally is
287 something like \"/usr/spool/mail/$user\") to the user's home
288 directory. This hook is called after the incoming mail box has been
289 emptied, and can be used to call any mail box programs you have
290 running (\"xwatch\", etc.)
294 \(add-hook 'nnmail-read-incoming-hook
296 (call-process \"/local/bin/mailsend\" nil nil nil
297 \"read\" nnmail-spool-file)))
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-lazy
'default
372 "Base widget for recursive datastructures.
374 This is copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
376 :convert-widget
'widget-value-convert-widget
377 :value-create
(lambda (widget)
378 (let ((value (widget-get widget
:value
))
379 (type (widget-get widget
:type
)))
380 (widget-put widget
:children
381 (list (widget-create-child-value
382 widget
(widget-convert type
) value
)))))
383 :value-delete
'widget-children-value-delete
384 :value-get
(lambda (widget)
385 (widget-value (car (widget-get widget
:children
))))
386 :value-inline
(lambda (widget)
387 (widget-apply (car (widget-get widget
:children
))
389 :default-get
(lambda (widget)
391 (widget-convert (widget-get widget
:type
))))
392 :match
(lambda (widget value
)
393 (widget-apply (widget-convert (widget-get widget
:type
))
395 :validate
(lambda (widget)
396 (widget-apply (car (widget-get widget
:children
)) :validate
)))
398 (define-widget 'nnmail-split-fancy
'nnmail-lazy
399 "Widget for customizing splits in the variable of the same name."
401 :type
'(menu-choice :value
(any ".*value.*" "misc")
403 (string :tag
"Destination")
404 (list :tag
"Use first match (|)" :value
(|
)
406 (editable-list :inline t nnmail-split-fancy
))
407 (list :tag
"Use all matches (&)" :value
(&)
409 (editable-list :inline t nnmail-split-fancy
))
410 (list :tag
"Function with fixed arguments (:)"
412 (const :format
"" :value
:)
414 (editable-list :inline t
(sexp :tag
"Arg"))
416 (list :tag
"Function with split arguments (!)"
420 (editable-list :inline t nnmail-split-fancy
))
421 (list :tag
"Field match"
426 (symbol :value mail
))
433 (const :tag
"Junk (delete mail)" junk
)))
435 (defcustom nnmail-split-fancy
"mail.misc"
436 "Incoming mail can be split according to this fancy variable.
437 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
439 The format of this variable is SPLIT, where SPLIT can be one of
442 GROUP: Mail will be stored in GROUP (a string).
444 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
445 field FIELD (a regexp) contains VALUE (a regexp), store the messages
446 as specified by SPLIT. If RESTRICT (a regexp) matches some string
447 after FIELD and before the end of the matched VALUE, return nil,
448 otherwise process SPLIT. Multiple RESTRICTs add up, further
449 restricting the possibility of processing SPLIT.
451 \(| SPLIT...): Process each SPLIT expression until one of them matches.
452 A SPLIT expression is said to match if it will cause the mail
453 message to be stored in one or more groups.
455 \(& SPLIT...): Process each SPLIT expression.
457 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
458 the buffer containing the message headers. The return value FUNCTION
459 should be a split, which is then recursively processed.
461 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
462 return value FUNCTION should be a split, which is then recursively
465 junk: Mail will be deleted. Use with care! Do not submerge in water!
467 (setq nnmail-split-fancy
468 '(| (\"Subject\" \"MAKE MONEY FAST\" junk)
469 ...other.rules.omitted...))
471 FIELD must match a complete field name. VALUE must match a complete
472 word according to the `nnmail-split-fancy-syntax-table' syntax table.
473 You can use \".*\" in the regexps to match partial field names or words.
475 FIELD and VALUE can also be lisp symbols, in that case they are expanded
476 as specified in `nnmail-split-abbrev-alist'.
478 GROUP can contain \\& and \\N which will substitute from matching
479 \\(\\) patterns in the previous VALUE.
483 \(setq nnmail-split-methods 'nnmail-split-fancy
485 ;; Messages from the mailer daemon are not crossposted to any of
486 ;; the ordinary groups. Warnings are put in a separate group
488 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
490 ;; Non-error messages are crossposted to all relevant
491 ;; groups, but we don't crosspost between the group for the
492 ;; (ding) list and the group for other (ding) related mail.
493 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
494 (\"subject\" \"ding\" \"ding.misc\"))
495 ;; Other mailing lists...
496 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
497 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
498 ;; Both lists below have the same suffix, so prevent
499 ;; cross-posting to mkpkg.list of messages posted only to
500 ;; the bugs- list, but allow cross-posting when the
501 ;; message was really cross-posted.
502 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
503 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
506 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
507 ;; Unmatched mail goes to the catch all group.
510 :type
'nnmail-split-fancy
)
512 (defcustom nnmail-split-abbrev-alist
513 '((any .
"from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
514 (mail .
"mailer-daemon\\|postmaster\\|uucp")
515 (to .
"to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
516 (from .
"from\\|sender\\|resent-from")
517 (nato .
"to\\|cc\\|resent-to\\|resent-cc")
518 (naany .
"from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
519 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
521 :type
'(repeat (cons :format
"%v" symbol regexp
)))
523 (defcustom nnmail-message-id-cache-length
1000
524 "*The approximate number of Message-IDs nnmail will keep in its cache.
525 If this variable is nil, no checking on duplicate messages will be
527 :group
'nnmail-duplicate
528 :type
'(choice (const :tag
"disable" nil
)
529 (integer :format
"%v")))
531 (defcustom nnmail-message-id-cache-file
"~/.nnmail-cache"
532 "*The file name of the nnmail Message-ID cache."
533 :group
'nnmail-duplicate
537 (defcustom nnmail-treat-duplicates
'warn
538 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
539 Three values are valid: nil, which means that nnmail is not to keep a
540 Message-ID cache; `warn', which means that nnmail should insert extra
541 headers to warn the user about the duplication (this is the default);
542 and `delete', which means that nnmail will delete duplicated mails.
544 This variable can also be a function. It will be called from a buffer
545 narrowed to the article in question with the Message-ID as a
546 parameter. It should return nil, `warn' or `delete'."
547 :group
'nnmail-duplicate
548 :type
'(choice (const :tag
"off" nil
)
552 (defcustom nnmail-extra-headers
'(To Newsgroups
)
553 "*Extra headers to parse."
556 :type
'(repeat symbol
))
558 (defcustom nnmail-split-header-length-limit
2048
559 "Header lines longer than this limit are excluded from the split function."
564 (defcustom nnmail-mail-splitting-charset nil
565 "Default charset to be used when splitting incoming mail."
570 (defcustom nnmail-mail-splitting-decodes nil
571 "Whether the nnmail splitting functionality should MIME decode headers."
576 (defcustom nnmail-split-fancy-match-partial-words nil
577 "Whether to match partial words when fancy splitting.
578 Normally, regexes given in `nnmail-split-fancy' are implicitly surrounded
579 by \"\\=\\<...\\>\". If this variable is true, they are not implicitly\
586 (defcustom nnmail-split-lowercase-expanded t
587 "Whether to lowercase expanded entries (i.e. \\N) when splitting mails.
588 This avoids the creation of multiple groups when users send to an address
589 using different case (i.e. mailing-list@domain vs Mailing-List@Domain)."
594 ;;; Internal variables.
596 (defvar nnmail-article-buffer
" *nnmail incoming*"
597 "The buffer used for splitting incoming mails.")
599 (defvar nnmail-split-history nil
600 "List of group/article elements that say where the previous split put messages.")
602 (defvar nnmail-split-fancy-syntax-table
603 (let ((table (make-syntax-table)))
604 ;; support the %-hack
605 (modify-syntax-entry ?\%
"." table
)
607 "Syntax table used by `nnmail-split-fancy'.")
609 (defvar nnmail-prepare-save-mail-hook nil
610 "Hook called before saving mail.")
612 (defvar nnmail-split-tracing nil
)
613 (defvar nnmail-split-trace nil
)
617 (defun nnmail-request-post (&optional server
)
618 (mail-send-and-exit nil
))
620 (defvar nnmail-file-coding-system
'raw-text
621 "Coding system used in nnmail.")
623 (defvar nnmail-incoming-coding-system
624 mm-text-coding-system
625 "Coding system used in reading inbox")
627 (defvar nnmail-pathname-coding-system nil
628 "*Coding system for file name.")
630 (defun nnmail-find-file (file)
631 "Insert FILE in server buffer safely."
632 (set-buffer nntp-server-buffer
)
633 (delete-region (point-min) (point-max))
634 (let ((format-alist nil
)
635 (after-insert-file-functions nil
))
637 (let ((coding-system-for-read nnmail-file-coding-system
)
638 (auto-mode-alist (mm-auto-mode-alist))
639 (file-name-coding-system nnmail-pathname-coding-system
))
640 (insert-file-contents file
)
644 (defun nnmail-group-pathname (group dir
&optional file
)
645 "Make file name for GROUP."
647 (let ((dir (file-name-as-directory (expand-file-name dir
))))
648 (setq group
(nnheader-replace-duplicate-chars-in-string
649 (nnheader-replace-chars-in-string group ?
/ ?_
)
651 (setq group
(nnheader-translate-file-chars group
))
652 ;; If this directory exists, we use it directly.
653 (file-name-as-directory
654 (if (or nnmail-use-long-file-names
655 (file-directory-p (concat dir group
)))
656 (expand-file-name group dir
)
657 ;; If not, we translate dots into slashes.
659 (mm-encode-coding-string
660 (nnheader-replace-chars-in-string group ?. ?
/)
661 nnmail-pathname-coding-system
)
665 (defun nnmail-get-active ()
666 "Returns an assoc of group names and active ranges.
667 nn*-request-list should have been called before calling this function."
668 ;; Go through all groups from the active list.
670 (set-buffer nntp-server-buffer
)
671 (nnmail-parse-active)))
673 (defun nnmail-parse-active ()
674 "Parse the active file in the current buffer and return an alist."
675 (goto-char (point-min))
676 (unless (re-search-forward "[\\\"]" nil t
)
677 (goto-char (point-max))
678 (while (re-search-backward "[][';?()#]" nil t
)
680 (goto-char (point-min))
681 (let ((buffer (current-buffer))
682 group-assoc group max min
)
686 (narrow-to-region (point) (gnus-point-at-eol))
687 (setq group
(read buffer
))
688 (unless (stringp group
)
689 (setq group
(symbol-name group
)))
690 (if (and (numberp (setq max
(read buffer
)))
691 (numberp (setq min
(read buffer
))))
692 (push (list group
(cons min max
))
699 (defvar nnmail-active-file-coding-system
'raw-text
700 "*Coding system for active file.")
702 (defun nnmail-save-active (group-assoc file-name
)
703 "Save GROUP-ASSOC in ACTIVE-FILE."
704 (let ((coding-system-for-write nnmail-active-file-coding-system
))
706 (with-temp-file file-name
707 (nnmail-generate-active group-assoc
)))))
709 (defun nnmail-generate-active (alist)
710 "Generate an active file from group-alist ALIST."
713 (while (setq group
(pop alist
))
714 (insert (format "%S %d %d y\n" (intern (car group
)) (cdadr group
)
716 (goto-char (point-max))
717 (while (search-backward "\\." nil t
)
720 (defun nnmail-get-split-group (file source
)
721 "Find out whether this FILE is to be split into GROUP only.
722 If SOURCE is a directory spec, try to return the group name component."
723 (if (eq (car source
) 'directory
)
724 (let ((file (file-name-nondirectory file
)))
725 (mail-source-bind (directory source
)
726 (if (string-match (concat (regexp-quote suffix
) "$") file
)
727 (substring file
0 (match-beginning 0))
731 (defun nnmail-process-babyl-mail-format (func artnum-func
)
732 (let ((case-fold-search t
)
734 start message-id content-length do-search end
)
736 (goto-char (point-min))
738 "\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t
)
739 (goto-char (match-end 0))
740 (delete-region (match-beginning 0) (match-end 0))
744 ;; Skip all the headers in case there are more "From "s...
745 (or (search-forward "\n\n" nil t
)
746 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t
)
747 (search-forward "\x1f\f"))
749 ;; Unquote the ">From " line, if any.
750 (goto-char (point-min))
751 (when (looking-at ">From ")
752 (replace-match "X-From-Line: ") )
753 (run-hooks 'nnmail-prepare-incoming-header-hook
)
754 (goto-char (point-max))
755 ;; Find the Message-ID header.
757 (if (re-search-backward
758 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t
)
759 (setq message-id
(buffer-substring (match-beginning 1)
761 ;; There is no Message-ID here, so we create one.
763 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
765 (insert "Original-")))
767 (insert "Message-ID: " (setq message-id
(nnmail-message-id))
769 ;; Look for a Content-Length header.
770 (if (not (save-excursion
771 (and (re-search-backward
772 "^Content-Length:[ \t]*\\([0-9]+\\)" start t
)
773 (setq content-length
(string-to-number
777 ;; We destroy the header, since none of
778 ;; the backends ever use it, and we do not
779 ;; want to confuse other mailers by having
780 ;; a (possibly) faulty header.
781 (progn (insert "X-") t
))))
784 (if (or (= (+ (point) content-length
) (point-max))
786 (goto-char (+ (point) content-length
))
787 (looking-at "\x1f")))
789 (goto-char (+ (point) content-length
))
790 (setq do-search nil
))
793 ;; Go to the beginning of the next article - or to the end
796 (if (re-search-forward "^\x1f" nil t
)
797 (goto-char (match-beginning 0))
798 (goto-char (1- (point-max)))))
799 (delete-char 1) ; delete ^_
802 (narrow-to-region start
(point))
803 (goto-char (point-min))
804 (nnmail-check-duplication message-id func artnum-func
)
806 (setq end
(point-max))))
810 (defsubst nnmail-search-unix-mail-delim
()
811 "Put point at the beginning of the next Unix mbox message."
812 ;; Algorithm used to find the next article in the
813 ;; brain-dead Unix mbox format:
815 ;; 1) Search for "^From ".
816 ;; 2) If we find it, then see whether the previous
817 ;; line is blank and the next line looks like a header.
818 ;; Then it's possible that this is a mail delim, and we use it.
819 (let ((case-fold-search nil
)
822 (if (not (re-search-forward "^From " nil t
))
826 (when (and (or (bobp)
829 (eq (char-after) ?
\n)))
832 (while (looking-at ">From \\|From ")
834 (looking-at "[^ \n\t:]+[ \n\t]*:")))
835 (setq found
'yes
)))))
839 (defun nnmail-search-unix-mail-delim-backward ()
840 "Put point at the beginning of the current Unix mbox message."
841 ;; Algorithm used to find the next article in the
842 ;; brain-dead Unix mbox format:
844 ;; 1) Search for "^From ".
845 ;; 2) If we find it, then see whether the previous
846 ;; line is blank and the next line looks like a header.
847 ;; Then it's possible that this is a mail delim, and we use it.
848 (let ((case-fold-search nil
)
851 (if (not (re-search-backward "^From " nil t
))
855 (when (and (or (bobp)
858 (eq (char-after) ?
\n)))
861 (while (looking-at ">From \\|From ")
863 (looking-at "[^ \n\t:]+[ \n\t]*:")))
864 (setq found
'yes
)))))
868 (defun nnmail-process-unix-mail-format (func artnum-func
)
869 (let ((case-fold-search t
)
871 start message-id content-length end skip head-end
)
872 (goto-char (point-min))
873 (if (not (and (re-search-forward "^From " nil t
)
874 (goto-char (match-beginning 0))))
875 ;; Possibly wrong format?
876 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
877 (if (buffer-file-name) "file" "buffer")
878 (or (buffer-file-name) (buffer-name)))
879 ;; Carry on until the bitter end.
883 ;; Find the end of the head.
886 (if (search-forward "\n\n" nil t
)
888 ;; This will never happen, but just to be on the safe side --
889 ;; if there is no head-body delimiter, we search a bit manually.
890 (while (and (looking-at "From \\|[^ \t]+:")
894 ;; Find the Message-ID header.
895 (goto-char (point-min))
896 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t
)
897 (setq message-id
(match-string 1))
899 (when (re-search-forward "^Message-ID[ \t]*:" nil t
)
901 (insert "Original-")))
902 ;; There is no Message-ID here, so we create one.
904 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
905 ;; Look for a Content-Length header.
906 (goto-char (point-min))
907 (if (not (re-search-forward
908 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t
))
909 (setq content-length nil
)
910 (setq content-length
(string-to-number (match-string 1)))
911 ;; We destroy the header, since none of the backends ever
912 ;; use it, and we do not want to confuse other mailers by
913 ;; having a (possibly) faulty header.
916 (run-hooks 'nnmail-prepare-incoming-header-hook
)
917 ;; Find the end of this article.
918 (goto-char (point-max))
920 (setq head-end
(point))
921 ;; We try the Content-Length value. The idea: skip over the header
922 ;; separator, then check what happens content-length bytes into the
923 ;; message body. This should be either the end of the buffer, the
924 ;; message separator or a blank line followed by the separator.
925 ;; The blank line should probably be deleted. If neither of the
926 ;; three is met, the content-length header is probably invalid.
929 (setq skip
(+ (point) content-length
))
931 (cond ((or (= skip
(point-max))
932 (= (1+ skip
) (point-max)))
933 (setq end
(point-max)))
934 ((looking-at "From ")
936 ((looking-at "[ \t]*\n\\(From \\)")
937 (setq end
(match-beginning 1)))
941 ;; No Content-Length, so we find the beginning of the next
942 ;; article or the end of the buffer.
944 (or (nnmail-search-unix-mail-delim)
945 (goto-char (point-max))))
946 ;; Allow the backend to save the article.
949 (narrow-to-region start
(point))
950 (goto-char (point-min))
952 (nnmail-check-duplication message-id func artnum-func
)
953 (setq end
(point-max))))
957 (defun nnmail-process-mmdf-mail-format (func artnum-func
)
958 (let ((delim "^\^A\^A\^A\^A$")
961 start message-id end
)
962 (goto-char (point-min))
963 (if (not (and (re-search-forward delim nil t
)
965 ;; Possibly wrong format?
966 (error "Error, unknown mail format! (Possibly corrupted.)")
967 ;; Carry on until the bitter end.
970 ;; Find the end of the head.
973 (if (search-forward "\n\n" nil t
)
975 ;; This will never happen, but just to be on the safe side --
976 ;; if there is no head-body delimiter, we search a bit manually.
977 (while (and (looking-at "From \\|[^ \t]+:")
981 ;; Find the Message-ID header.
982 (goto-char (point-min))
983 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t
)
984 (setq message-id
(match-string 1))
985 ;; There is no Message-ID here, so we create one.
987 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
989 (insert "Original-")))
991 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
992 (run-hooks 'nnmail-prepare-incoming-header-hook
)
993 ;; Find the end of this article.
994 (goto-char (point-max))
996 (if (re-search-forward delim nil t
)
998 (goto-char (point-max)))
999 ;; Allow the backend to save the article.
1002 (narrow-to-region start
(point))
1003 (goto-char (point-min))
1005 (nnmail-check-duplication message-id func artnum-func
)
1006 (setq end
(point-max))))
1011 (defun nnmail-process-maildir-mail-format (func artnum-func
)
1012 ;; In a maildir, every file contains exactly one mail.
1013 (let ((case-fold-search t
)
1015 (goto-char (point-min))
1016 ;; Find the end of the head.
1019 (if (search-forward "\n\n" nil t
)
1021 ;; This will never happen, but just to be on the safe side --
1022 ;; if there is no head-body delimiter, we search a bit manually.
1023 (while (and (looking-at "From \\|[^ \t]+:")
1027 ;; Find the Message-ID header.
1028 (goto-char (point-min))
1029 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t
)
1030 (setq message-id
(match-string 1))
1031 ;; There is no Message-ID here, so we create one.
1033 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
1035 (insert "Original-")))
1037 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
1038 (run-hooks 'nnmail-prepare-incoming-header-hook
)
1039 ;; Allow the backend to save the article.
1042 (goto-char (point-min))
1043 (nnmail-check-duplication message-id func artnum-func
))
1046 (defun nnmail-split-incoming (incoming func
&optional exit-func
1048 "Go through the entire INCOMING file and pick out each individual mail.
1049 FUNC will be called with the buffer narrowed to each mail."
1050 (let ( ;; If this is a group-specific split, we bind the split
1051 ;; methods to just this group.
1052 (nnmail-split-methods (if (and group
1053 (not nnmail-resplit-incoming
))
1054 (list (list group
""))
1055 nnmail-split-methods
)))
1057 ;; Insert the incoming file.
1058 (set-buffer (get-buffer-create nnmail-article-buffer
))
1060 (let ((coding-system-for-read nnmail-incoming-coding-system
))
1061 (mm-insert-file-contents incoming
))
1063 (if (zerop (buffer-size))
1065 (goto-char (point-min))
1066 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook
))
1067 ;; Handle both babyl, MMDF and unix mail formats, since
1068 ;; movemail will use the former when fetching from a
1069 ;; mailbox, the latter when fetching from a file.
1070 (cond ((or (looking-at "\^L")
1071 (looking-at "BABYL OPTIONS:"))
1072 (nnmail-process-babyl-mail-format func artnum-func
))
1073 ((looking-at "\^A\^A\^A\^A")
1074 (nnmail-process-mmdf-mail-format func artnum-func
))
1075 ((looking-at "Return-Path:")
1076 (nnmail-process-maildir-mail-format func artnum-func
))
1078 (nnmail-process-unix-mail-format func artnum-func
))))
1080 (funcall exit-func
))
1081 (kill-buffer (current-buffer))))))
1083 (defun nnmail-article-group (func &optional trace
)
1084 "Look at the headers and return an alist of groups that match.
1085 FUNC will be called with the group name to determine the article number."
1086 (let ((methods (or nnmail-split-methods
'(("bogus" ""))))
1087 (obuf (current-buffer))
1088 group-art method grp
)
1089 (if (and (sequencep methods
)
1090 (= (length methods
) 1))
1091 ;; If there is only just one group to put everything in, we
1092 ;; just return a list with just this one method in.
1094 (list (cons (caar methods
) (funcall func
(caar methods
)))))
1095 ;; We do actual comparison.
1097 ;; Copy the article into the work buffer.
1098 (set-buffer nntp-server-buffer
)
1100 (insert-buffer-substring obuf
)
1101 ;; Narrow to headers.
1103 (goto-char (point-min))
1104 (if (search-forward "\n\n" nil t
)
1107 (goto-char (point-min))
1108 ;; Decode MIME headers and charsets.
1109 (when nnmail-mail-splitting-decodes
1110 (let ((mail-parse-charset nnmail-mail-splitting-charset
))
1111 (mail-decode-encoded-word-region (point-min) (point-max))))
1112 ;; Fold continuation lines.
1113 (goto-char (point-min))
1114 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t
)
1115 (replace-match " " t t
))
1116 ;; Nuke pathologically long headers. Since Gnus applies
1117 ;; pathologically complex regexps to the buffer, lines
1118 ;; that are looong will take longer than the Universe's
1119 ;; existence to process.
1120 (goto-char (point-min))
1122 (unless (< (move-to-column nnmail-split-header-length-limit
)
1123 nnmail-split-header-length-limit
)
1124 (delete-region (point) (gnus-point-at-eol)))
1127 (goto-char (point-min))
1128 (run-hooks 'nnmail-split-hook
)
1129 (when (setq nnmail-split-tracing trace
)
1130 (setq nnmail-split-trace nil
))
1131 (if (and (symbolp nnmail-split-methods
)
1132 (fboundp nnmail-split-methods
))
1135 ;; `nnmail-split-methods' is a function, so we
1136 ;; just call this function here and use the
1138 (or (funcall nnmail-split-methods
)
1142 5 "Error in `nnmail-split-methods'; using `bogus' mail group")
1145 (setq split
(mm-delete-duplicates split
))
1146 ;; The article may be "cross-posted" to `junk'. What
1147 ;; to do? Just remove the `junk' spec. Don't really
1148 ;; see anything else to do...
1150 (while (setq elem
(car (memq 'junk split
)))
1151 (setq split
(delq elem split
))))
1155 (lambda (group) (cons group
(funcall func group
)))
1157 ;; Go through the split methods to find a match.
1159 (or nnmail-crosspost
1161 (goto-char (point-max))
1162 (setq method
(pop methods
)
1165 (not (equal "" (nth 1 method
))))
1168 (if (stringp (nth 1 method
))
1169 (let ((expand (string-match "\\\\[0-9&]" grp
))
1170 (pos (re-search-backward (cadr method
)
1173 (setq grp
(nnmail-expand-newtext grp
)))
1175 ;; Function to say whether this is a match.
1176 (funcall (nth 1 method
) grp
)))
1177 ;; Don't enter the article into the same
1179 (not (assoc grp group-art
)))
1180 (push (cons grp
(funcall func grp
))
1182 ;; This is the final group, which is used as a
1186 (list (cons (car method
)
1187 (funcall func
(car method
))))))))
1188 ;; Fall back on "bogus" if all else fails.
1190 (setq group-art
(list (cons "bogus" (funcall func
"bogus"))))))
1191 ;; Produce a trace if non-empty.
1192 (when (and trace nnmail-split-trace
)
1193 (let ((restore (current-buffer)))
1194 (nnheader-set-temp-buffer "*Split Trace*")
1196 (dolist (trace (nreverse nnmail-split-trace
))
1197 (prin1 trace
(current-buffer))
1199 (goto-char (point-min))
1200 (gnus-configure-windows 'split-trace
)
1201 (set-buffer restore
)))
1203 ;; See whether the split methods returned `junk'.
1204 (if (equal group-art
'(junk))
1206 ;; The article may be "cross-posted" to `junk'. What
1207 ;; to do? Just remove the `junk' spec. Don't really
1208 ;; see anything else to do...
1210 (while (setq elem
(car (memq 'junk group-art
)))
1211 (setq group-art
(delq elem group-art
)))
1212 (nreverse group-art
)))))))
1214 (defun nnmail-insert-lines ()
1215 "Insert how many lines there are in the body of the mail.
1216 Return the number of characters in the body."
1219 (goto-char (point-min))
1220 (unless (search-forward "\n\n" nil t
)
1221 (goto-char (point-max))
1223 (setq chars
(- (point-max) (point)))
1224 (setq lines
(count-lines (point) (point-max)))
1227 (when (re-search-backward "^Lines: " nil t
)
1228 (delete-region (point) (progn (forward-line 1) (point)))))
1230 (insert (format "Lines: %d\n" (max lines
0)))
1233 (defun nnmail-insert-xref (group-alist)
1234 "Insert an Xref line based on the (group . article) alist."
1236 (goto-char (point-min))
1237 (unless (search-forward "\n\n" nil t
)
1238 (goto-char (point-max))
1241 (when (re-search-backward "^Xref: " nil t
)
1242 (delete-region (match-beginning 0)
1243 (progn (forward-line 1) (point))))
1244 (insert (format "Xref: %s" (system-name)))
1246 (insert (format " %s:%d"
1247 (mm-encode-coding-string
1249 nnmail-pathname-coding-system
)
1250 (cdar group-alist
)))
1251 (setq group-alist
(cdr group-alist
)))
1254 ;;; Message washing functions
1256 (defun nnmail-remove-leading-whitespace ()
1257 "Remove excessive whitespace from all headers."
1258 (goto-char (point-min))
1259 (while (re-search-forward "^\\([^ :]+: \\) +" nil t
)
1260 (replace-match "\\1" t
)))
1262 (defun nnmail-remove-list-identifiers ()
1263 "Remove list identifiers from Subject headers."
1265 (if (consp nnmail-list-identifiers
)
1266 (mapconcat 'identity nnmail-list-identifiers
" *\\|")
1267 nnmail-list-identifiers
)))
1269 (goto-char (point-min))
1270 (while (re-search-forward
1271 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp
" *\\)")
1273 (delete-region (match-beginning 2) (match-end 0))
1274 (beginning-of-line))
1275 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1277 (delete-region (match-beginning 1) (match-end 1))
1278 (beginning-of-line)))))
1280 (defun nnmail-remove-tabs ()
1281 "Translate TAB characters into SPACE characters."
1282 (subst-char-in-region (point-min) (point-max) ?
\t ? t
))
1284 (defun nnmail-fix-eudora-headers ()
1285 "Eudora has a broken References line, but an OK In-Reply-To."
1286 (goto-char (point-min))
1287 (when (re-search-forward "^X-Mailer:.*Eudora" nil t
)
1288 (goto-char (point-min))
1289 (when (re-search-forward "^References:" nil t
)
1291 (insert "X-Gnus-Broken-Eudora-"))
1292 (goto-char (point-min))
1293 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t
)
1294 (replace-match "\\1" t
))))
1296 (custom-add-option 'nnmail-prepare-incoming-header-hook
1297 'nnmail-fix-eudora-headers
)
1299 ;;; Utility functions
1301 (defun nnmail-do-request-post (accept-func &optional server
)
1302 "Utility function to directly post a message to an nnmail-derived group.
1303 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1304 to actually put the message in the right group."
1306 (dolist (mbx (message-unquote-tokens
1307 (message-tokenize-header
1308 (message-fetch-field "Newsgroups") ", ")) success
)
1309 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method
)))
1310 (or (gnus-active to-newsgroup
)
1311 (gnus-activate-group to-newsgroup
)
1312 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1314 (or (and (gnus-request-create-group
1315 to-newsgroup gnus-command-method
)
1316 (gnus-activate-group to-newsgroup nil nil
1317 gnus-command-method
))
1318 (error "Couldn't create group %s" to-newsgroup
)))
1319 (error "No such group: %s" to-newsgroup
))
1320 (unless (funcall accept-func mbx
(nth 1 gnus-command-method
))
1321 (setq success nil
))))))
1323 (defun nnmail-split-fancy ()
1324 "Fancy splitting method.
1325 See the documentation for the variable `nnmail-split-fancy' for details."
1326 (let ((syntab (syntax-table)))
1329 (set-syntax-table nnmail-split-fancy-syntax-table
)
1330 (nnmail-split-it nnmail-split-fancy
))
1331 (set-syntax-table syntab
))))
1333 (defvar nnmail-split-cache nil
)
1334 ;; Alist of split expressions their equivalent regexps.
1336 (defun nnmail-split-it (split)
1337 ;; Return a list of groups matching SPLIT.
1344 ;; A group name. Do the \& and \N subs into the string.
1346 (when nnmail-split-tracing
1347 (push split nnmail-split-trace
))
1348 (list (nnmail-expand-newtext split
)))
1350 ;; Junk the message.
1352 (when nnmail-split-tracing
1353 (push "junk" nnmail-split-trace
))
1356 ;; Builtin & operation.
1357 ((eq (car split
) '&)
1358 (apply 'nconc
(mapcar 'nnmail-split-it
(cdr split
))))
1360 ;; Builtin | operation.
1361 ((eq (car split
) '|
)
1363 (while (and (not done
) (cdr split
))
1364 (setq split
(cdr split
)
1365 done
(nnmail-split-it (car split
))))
1368 ;; Builtin : operation.
1369 ((eq (car split
) ':)
1370 (when nnmail-split-tracing
1371 (push split nnmail-split-trace
))
1372 (nnmail-split-it (save-excursion (eval (cdr split
)))))
1374 ;; Builtin ! operation.
1375 ((eq (car split
) '!)
1376 (funcall (cadr split
) (nnmail-split-it (caddr split
))))
1378 ;; Check the cache for the regexp for this split.
1379 ((setq cached-pair
(assq split nnmail-split-cache
))
1381 (end-point (point-max))
1382 (value (nth 1 split
)))
1384 (setq value
(cdr (assq value nnmail-split-abbrev-alist
))))
1385 (while (and (goto-char end-point
)
1386 (re-search-backward (cdr cached-pair
) nil t
))
1387 (when nnmail-split-tracing
1388 (push split nnmail-split-trace
))
1389 (let ((split-rest (cddr split
))
1391 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1392 ;; So, start-of-value is the point just before the
1393 ;; beginning of the value, whereas after-header-name
1394 ;; is the point just after the field name.
1395 (start-of-value (match-end 1))
1396 (after-header-name (match-end 2)))
1397 ;; Start the next search just before the beginning of the
1399 (setq end-point
(1- start-of-value
))
1400 ;; Handle - RESTRICTs
1401 (while (eq (car split-rest
) '-
)
1402 ;; RESTRICT must start after-header-name and
1403 ;; end after start-of-value, so that, for
1404 ;; (any "foo" - "x-foo" "foo.list")
1405 ;; we do not exclude foo.list just because
1406 ;; the header is: ``To: x-foo, foo''
1408 (if (and (re-search-backward (cadr split-rest
)
1409 after-header-name t
)
1410 (> (match-end 0) start-of-value
))
1411 (setq split-rest nil
)
1412 (setq split-rest
(cddr split-rest
))))
1415 (let ((value (nth 1 split
)))
1417 (setq value
(cdr (assq value nnmail-split-abbrev-alist
))))
1418 ;; Someone might want to do a \N sub on this match, so get the
1419 ;; correct match positions.
1420 (re-search-backward value start-of-value
))
1421 (dolist (sp (nnmail-split-it (car split-rest
)))
1422 (unless (member sp split-result
)
1423 (push sp split-result
))))))
1426 ;; Not in cache, compute a regexp for the field/value pair.
1428 (let* ((field (nth 0 split
))
1429 (value (nth 1 split
))
1434 (setq value
(cdr (assq value nnmail-split-abbrev-alist
))))
1435 (if (and (>= (length value
) 2)
1436 (string= ".*" (substring value
0 2)))
1437 (setq value
(substring value
2)
1439 ;; Same trick for the rear of the regexp
1440 (if (and (>= (length value
) 2)
1441 (string= ".*" (substring value -
2)))
1442 (setq value
(substring value
0 -
2)
1444 (when nnmail-split-fancy-match-partial-words
1445 (setq partial-front
""
1447 (setq regexp
(concat "^\\(\\("
1449 (cdr (assq field nnmail-split-abbrev-alist
))
1452 (or partial-front
"\\<")
1456 (or partial-rear
"\\>")))
1457 (push (cons split regexp
) nnmail-split-cache
)
1458 ;; Now that it's in the cache, just call nnmail-split-it again
1459 ;; on the same split, which will find it immediately in the cache.
1460 (nnmail-split-it split
))))))
1462 (defun nnmail-expand-newtext (newtext)
1463 (let ((len (length newtext
))
1465 c expanded beg N did-expand
)
1468 (while (and (< pos len
)
1469 (not (= (aref newtext pos
) ?
\\)))
1470 (setq pos
(1+ pos
)))
1472 (push (substring newtext beg pos
) expanded
))
1474 ;; We hit a \; expand it.
1477 c
(aref newtext pos
))
1478 (if (not (or (= c ?\
&)
1481 ;; \ followed by some character we don't expand.
1482 (push (char-to-string c
) expanded
)
1487 (when (match-beginning N
)
1488 (push (if nnmail-split-lowercase-expanded
1489 (downcase (buffer-substring (match-beginning N
)
1491 (buffer-substring (match-beginning N
) (match-end N
)))
1493 (setq pos
(1+ pos
)))
1495 (apply 'concat
(nreverse expanded
))
1498 ;; Activate a backend only if it isn't already activated.
1499 ;; If FORCE, re-read the active file even if the backend is
1500 ;; already activated.
1501 (defun nnmail-activate (backend &optional force
)
1502 (nnheader-init-server-buffer)
1503 (let (file timestamp file-time
)
1504 (if (or (not (symbol-value (intern (format "%s-group-alist" backend
))))
1506 (and (setq file
(ignore-errors
1507 (symbol-value (intern (format "%s-active-file"
1509 (setq file-time
(nth 5 (file-attributes file
)))
1513 (symbol-value (intern
1514 (format "%s-active-timestamp"
1517 (not (consp timestamp
))
1518 (equal timestamp
'(0 0))
1519 (> (nth 0 file-time
) (nth 0 timestamp
))
1520 (and (= (nth 0 file-time
) (nth 0 timestamp
))
1521 (> (nth 1 file-time
) (nth 1 timestamp
))))))
1523 (or (eq timestamp
'none
)
1524 (set (intern (format "%s-active-timestamp" backend
))
1526 (funcall (intern (format "%s-request-list" backend
)))))
1529 (defun nnmail-message-id ()
1530 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1533 ;;; nnmail duplicate handling
1536 (defvar nnmail-cache-buffer nil
)
1538 (defun nnmail-cache-open ()
1539 (if (or (not nnmail-treat-duplicates
)
1540 (and nnmail-cache-buffer
1541 (buffer-name nnmail-cache-buffer
)))
1542 () ; The buffer is open.
1545 (setq nnmail-cache-buffer
1546 (get-buffer-create " *nnmail message-id cache*")))
1548 (when (file-exists-p nnmail-message-id-cache-file
)
1549 (nnheader-insert-file-contents nnmail-message-id-cache-file
))
1550 (set-buffer-modified-p nil
)
1553 (defun nnmail-cache-close ()
1554 (when (and nnmail-cache-buffer
1555 nnmail-treat-duplicates
1556 (buffer-name nnmail-cache-buffer
)
1557 (buffer-modified-p nnmail-cache-buffer
))
1559 (set-buffer nnmail-cache-buffer
)
1560 ;; Weed out the excess number of Message-IDs.
1561 (goto-char (point-max))
1562 (when (search-backward "\n" nil t nnmail-message-id-cache-length
)
1565 (delete-region (point-min) (point))))
1567 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file
))
1568 (make-directory (file-name-directory nnmail-message-id-cache-file
)
1570 (nnmail-write-region (point-min) (point-max)
1571 nnmail-message-id-cache-file nil
'silent
)
1572 (set-buffer-modified-p nil
)
1573 (setq nnmail-cache-buffer nil
)
1574 (gnus-kill-buffer (current-buffer)))))
1576 ;; Compiler directives.
1578 (defvar group-art-list
)
1580 (defun nnmail-cache-insert (id grp
&optional subject sender
)
1582 ;; this will handle cases like `B r' where the group is nil
1583 (let ((grp (or grp gnus-newsgroup-name
"UNKNOWN")))
1584 (run-hook-with-args 'nnmail-spool-hook
1585 id grp subject sender
))
1586 (when nnmail-treat-duplicates
1587 ;; Store some information about the group this message is written
1588 ;; to. This is passed in as the grp argument -- all locations this
1589 ;; has been called from have been checked and the group is available.
1590 ;; The only ambiguous case is nnmail-check-duplication which will only
1591 ;; pass the first (of possibly >1) group which matches. -Josh
1592 (unless (gnus-buffer-live-p nnmail-cache-buffer
)
1593 (nnmail-cache-open))
1595 (set-buffer nnmail-cache-buffer
)
1596 (goto-char (point-max))
1597 (if (and grp
(not (string= "" grp
))
1598 (gnus-methods-equal-p gnus-command-method
1599 (nnmail-cache-primary-mail-backend)))
1600 (let ((regexp (if (consp nnmail-cache-ignore-groups
)
1601 (mapconcat 'identity nnmail-cache-ignore-groups
1603 nnmail-cache-ignore-groups
)))
1604 (unless (and regexp
(string-match regexp grp
))
1605 (insert id
"\t" grp
"\n")))
1606 (insert id
"\n"))))))
1608 (defun nnmail-cache-primary-mail-backend ()
1609 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods
))
1613 (while (and (null res
) be-list
)
1614 (setq be
(car be-list
))
1615 (setq be-list
(cdr be-list
))
1616 (when (and (gnus-method-option-p be
'respool
)
1618 (intern (format "%s-get-new-mail" (car be
))))
1619 (boundp get-new-mail
)
1620 (symbol-value get-new-mail
))
1624 ;; Fetch the group name corresponding to the message id stored in the
1626 (defun nnmail-cache-fetch-group (id)
1627 (when (and nnmail-treat-duplicates nnmail-cache-buffer
)
1629 (set-buffer nnmail-cache-buffer
)
1630 (goto-char (point-max))
1631 (when (search-backward id nil t
)
1633 (skip-chars-forward "^\n\r\t")
1634 (unless (looking-at "[\r\n]")
1636 (buffer-substring (point) (gnus-point-at-eol)))))))
1638 ;; Function for nnmail-split-fancy: look up all references in the
1639 ;; cache and if a match is found, return that group.
1640 (defun nnmail-split-fancy-with-parent ()
1641 "Split this message into the same group as its parent.
1642 This function can be used as an entry in `nnmail-split-fancy', for
1643 example like this: (: nnmail-split-fancy-with-parent)
1644 For a message to be split, it looks for the parent message in the
1645 References or In-Reply-To header and then looks in the message id
1646 cache file (given by the variable `nnmail-message-id-cache-file') to
1647 see which group that message was put in. This group is returned.
1649 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1650 (let* ((refstr (or (message-fetch-field "references")
1651 (message-fetch-field "in-reply-to")))
1654 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups
)
1656 (lambda (x) (format "\\(%s\\)" x
))
1657 nnmail-split-fancy-with-parent-ignore-groups
1659 nnmail-split-fancy-with-parent-ignore-groups
)))
1661 (setq references
(nreverse (gnus-split-references refstr
)))
1662 (unless (gnus-buffer-live-p nnmail-cache-buffer
)
1663 (nnmail-cache-open))
1665 (setq res
(or (nnmail-cache-fetch-group x
) res
))
1666 (when (or (member res
'("delayed" "drafts" "queue"))
1667 (and regexp res
(string-match regexp res
)))
1672 (defun nnmail-cache-id-exists-p (id)
1673 (when nnmail-treat-duplicates
1675 (set-buffer nnmail-cache-buffer
)
1676 (goto-char (point-max))
1677 (search-backward id nil t
))))
1679 (defun nnmail-fetch-field (header)
1682 (message-narrow-to-head)
1683 (message-fetch-field header
))))
1685 (defun nnmail-check-duplication (message-id func artnum-func
)
1686 (run-hooks 'nnmail-prepare-incoming-message-hook
)
1687 ;; If this is a duplicate message, then we do not save it.
1688 (let* ((duplication (nnmail-cache-id-exists-p message-id
))
1689 (case-fold-search t
)
1690 (action (when duplication
1692 ((memq nnmail-treat-duplicates
'(warn delete
))
1693 nnmail-treat-duplicates
)
1694 ((functionp nnmail-treat-duplicates
)
1695 (funcall nnmail-treat-duplicates message-id
))
1697 nnmail-treat-duplicates
))))
1699 ;; We insert a line that says what the mail source is.
1700 (let ((case-fold-search t
))
1701 (goto-char (point-min))
1702 (re-search-forward "^message-id[ \t]*:" nil t
)
1704 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string
)))
1706 ;; Let the backend save the article (or not).
1709 (funcall func
(setq group-art
1710 (nreverse (nnmail-article-group artnum-func
))))
1711 (nnmail-cache-insert message-id
(caar group-art
)))
1712 ((eq action
'delete
)
1713 (setq group-art nil
))
1715 ;; We insert a warning.
1716 (let ((case-fold-search t
))
1717 (goto-char (point-min))
1718 (re-search-forward "^message-id[ \t]*:" nil t
)
1721 "Gnus-Warning: This is a duplicate of message " message-id
"\n")
1722 (funcall func
(setq group-art
1723 (nreverse (nnmail-article-group artnum-func
))))))
1725 (funcall func
(setq group-art
1726 (nreverse (nnmail-article-group artnum-func
))))))
1727 ;; Add the group-art list to the history list.
1729 (push group-art nnmail-split-history
)
1730 (delete-region (point-min) (point-max)))))
1734 (defvar nnmail-fetched-sources nil
)
1736 (defun nnmail-get-value (&rest args
)
1737 (let ((sym (intern (apply 'format args
))))
1739 (symbol-value sym
))))
1741 (defun nnmail-get-new-mail (method exit-func temp
1742 &optional group spool-func
)
1743 "Read new incoming mail."
1744 (let* ((sources (or mail-sources
1745 (if (listp nnmail-spool-file
) nnmail-spool-file
1746 (list nnmail-spool-file
))))
1752 incoming incomings source
)
1753 (when (and (nnmail-get-value "%s-get-new-mail" method
)
1755 (while (setq source
(pop sources
))
1756 ;; Be compatible with old values.
1761 ((string-match "^po:" source
)
1762 (list 'pop
:user
(substring source
(match-end 0))))
1763 ((file-directory-p source
)
1764 (list 'directory
:path source
))
1766 (list 'file
:path source
)))))
1767 ((eq source
'procmail
)
1768 (message "Invalid value for nnmail-spool-file: `procmail'")
1770 ;; Hack to only fetch the contents of a single group's spool file.
1771 (when (and (eq (car source
) 'directory
)
1772 (null nnmail-scan-directory-mail-source-once
)
1774 (mail-source-bind (directory source
)
1775 (setq source
(append source
1781 ,(concat group suffix
)
1782 (file-name-nondirectory file
)))))))))
1783 (when nnmail-fetched-sources
1784 (if (member source nnmail-fetched-sources
)
1786 (push source nnmail-fetched-sources
)
1787 (push source fetching-sources
)))))
1788 (when fetching-sources
1789 ;; We first activate all the groups.
1790 (nnmail-activate method
)
1791 ;; Allow the user to hook.
1792 (run-hooks 'nnmail-pre-get-new-mail-hook
)
1793 ;; Open the message-id cache.
1795 ;; The we go through all the existing mail source specification
1796 ;; and fetch the mail from each.
1797 (while (setq source
(pop fetching-sources
))
1798 (nnheader-message 4 "%s: Reading incoming mail from %s..."
1799 method
(car source
))
1804 `(lambda (file orig-file
)
1805 (nnmail-split-incoming
1806 file
',(intern (format "%s-save-mail" method
))
1808 (if (equal file orig-file
)
1810 (nnmail-get-split-group orig-file
',source
))
1811 ',(intern (format "%s-active-number" method
)))))))
1814 ;; If we did indeed read any incoming spools, we save all info.
1816 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1817 method
(car source
))
1819 (nnmail-get-value "%s-group-alist" method
)
1820 (nnmail-get-value "%s-active-file" method
))
1822 (funcall exit-func
))
1823 (run-hooks 'nnmail-read-incoming-hook
)
1824 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1826 ;; Close the message-id cache.
1827 (nnmail-cache-close)
1828 ;; Allow the user to hook.
1829 (run-hooks 'nnmail-post-get-new-mail-hook
))))
1831 (defun nnmail-expired-article-p (group time force
&optional inhibit
)
1832 "Say whether an article that is TIME old in GROUP should be expired."
1835 (let ((days (or (and nnmail-expiry-wait-function
1836 (funcall nnmail-expiry-wait-function group
))
1837 nnmail-expiry-wait
)))
1838 (cond ((or (eq days
'never
)
1841 ;; This isn't an expirable group.
1843 ((eq days
'immediate
)
1844 ;; We expire all articles on sight.
1846 ((equal time
'(0 0))
1847 ;; This is an ange-ftp group, and we don't have any dates.
1850 (setq days
(days-to-time days
))
1851 ;; Compare the time with the current time.
1852 (ignore-errors (time-less-p days
(time-since time
))))))))
1854 (defun nnmail-expiry-target-group (target group
)
1855 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1856 ;; that buffer if the nnfolder group isn't selected.
1857 (let (nnmail-cache-accepted-message-ids)
1858 ;; Don't enter Message-IDs into cache.
1859 ;; Let users hack it in TARGET function.
1860 (when (functionp target
)
1861 (setq target
(funcall target group
)))
1862 (unless (eq target
'delete
)
1863 (when (or (gnus-request-group target
)
1864 (gnus-request-create-group target
))
1865 (let ((group-art (gnus-request-accept-article target nil nil t
)))
1866 (when (consp group-art
)
1867 (gnus-group-mark-article-read target
(cdr group-art
))))))))
1869 (defun nnmail-fancy-expiry-target (group)
1870 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1872 (case-fold-search nil
)
1873 (from (or (message-fetch-field "from") ""))
1874 (to (or (message-fetch-field "to") ""))
1875 (date (message-fetch-field "date"))
1881 (message "%s" (error-message-string err
))
1884 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets
) target
)
1885 (setq header
(car regexp-target-pair
))
1887 ;; If the header is to-from then match against the
1888 ;; To or From header
1889 ((and (equal header
'to-from
)
1890 (or (string-match (cadr regexp-target-pair
) from
)
1891 (and (string-match message-dont-reply-to-names from
)
1892 (string-match (cadr regexp-target-pair
) to
))))
1893 (setq target
(format-time-string (caddr regexp-target-pair
) date
)))
1894 ((and (not (equal header
'to-from
))
1895 (string-match (cadr regexp-target-pair
)
1897 (message-fetch-field header
)
1900 (format-time-string (caddr regexp-target-pair
) date
)))))))
1902 (defun nnmail-check-syntax ()
1903 "Check (and modify) the syntax of the message in the current buffer."
1905 (message-narrow-to-head)
1906 (let ((case-fold-search t
))
1907 (unless (re-search-forward "^Message-ID[ \t]*:" nil t
)
1908 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1910 (defun nnmail-write-region (start end filename
&optional append visit lockname
)
1911 "Do a `write-region', and then set the file modes."
1912 (let ((coding-system-for-write nnmail-file-coding-system
)
1913 (file-name-coding-system nnmail-pathname-coding-system
))
1914 (write-region start end filename append visit lockname
)
1915 (set-file-modes filename nnmail-default-file-modes
)))
1918 ;;; Status functions
1921 (defun nnmail-replace-status (name value
)
1922 "Make status NAME and VALUE part of the current status line."
1924 (message-narrow-to-head)
1925 (let ((status (nnmail-decode-status)))
1926 (setq status
(delq (member name status
) status
))
1928 (push (cons name value
) status
))
1929 (message-remove-header "status")
1930 (goto-char (point-max))
1931 (insert "Status: " (nnmail-encode-status status
) "\n"))))
1933 (defun nnmail-decode-status ()
1934 "Return a status-value alist from STATUS."
1935 (goto-char (point-min))
1936 (when (re-search-forward "^Status: " nil t
)
1937 (let (name value status
)
1939 ;; Narrow to the status.
1942 (if (re-search-forward "^[^ \t]" nil t
)
1945 ;; Go through all elements and add them to the list.
1946 (goto-char (point-min))
1947 (while (re-search-forward "[^ \t=]+" nil t
)
1948 (setq name
(match-string 0))
1949 (if (not (eq (char-after) ?
=))
1953 (if (not (eq (char-after) ?
\"))
1954 (if (not (looking-at "[^ \t]"))
1958 (setq value
(match-string 0))
1959 (goto-char (match-end 0)))
1961 (setq value
(read (current-buffer)))))
1962 (push (cons name value
) status
)))
1965 (defun nnmail-encode-status (status)
1966 "Return a status string from STATUS."
1971 (if (string-match "[ \t]" (cdr elem
))
1972 (prin1-to-string (cdr elem
))
1976 (defun nnmail-split-history ()
1977 "Generate an overview of where the last mail split put articles."
1979 (unless nnmail-split-history
1980 (error "No current split history"))
1981 (with-output-to-temp-buffer "*nnmail split history*"
1982 (with-current-buffer standard-output
1983 (fundamental-mode)) ; for Emacs 20.4+
1984 (let ((history nnmail-split-history
)
1986 (while (setq elem
(pop history
))
1987 (princ (mapconcat (lambda (ga)
1988 (concat (car ga
) ":" (int-to-string (cdr ga
))))
1993 (defun nnmail-purge-split-history (group)
1994 "Remove all instances of GROUP from `nnmail-split-history'."
1995 (let ((history nnmail-split-history
))
1997 (setcar history
(gnus-remove-if (lambda (e) (string= (car e
) group
))
2000 (setq nnmail-split-history
(delq nil nnmail-split-history
))))
2002 (defun nnmail-new-mail-p (group)
2003 "Say whether GROUP has new mail."
2004 (let ((his nnmail-split-history
)
2007 (when (assoc group
(pop his
))
2012 (defun nnmail-within-headers-p ()
2013 "Check to see if point is within the headers of a unix mail message.
2014 Doesn't change point."
2015 (let ((pos (point)))
2017 (and (nnmail-search-unix-mail-delim-backward)
2018 (not (search-forward "\n\n" pos t
))))))
2020 (run-hooks 'nnmail-load-hook
)
2024 ;;; arch-tag: fe8f671a-50db-428a-bb5d-f00462f72ed7
2025 ;;; nnmail.el ends here