1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news, mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, 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."
250 (make-obsolete-variable 'nnmail-spool-file
251 "This option is obsolete in Gnus 5.9. \
252 Use `mail-sources' instead.")
253 ;; revision 5.29 / p0-85 / Gnus 5.9
255 (defcustom nnmail-resplit-incoming nil
256 "*If non-nil, re-split incoming procmail sorted mail."
257 :group
'nnmail-procmail
260 (defcustom nnmail-scan-directory-mail-source-once nil
261 "*If non-nil, scan all incoming procmail sorted mails once.
262 It scans low-level sorted spools even when not required."
264 :group
'nnmail-procmail
267 (defcustom nnmail-delete-file-function
'delete-file
268 "Function called to delete files in some mail backends."
272 (defcustom nnmail-crosspost-link-function
273 (if (string-match "windows-nt\\|emx" (symbol-name system-type
))
276 "*Function called to create a copy of a file.
277 This is `add-name-to-file' by default, which means that crossposts
278 will use hard links. If your file system doesn't allow hard
279 links, you could set this variable to `copy-file' instead."
281 :type
'(radio (function-item add-name-to-file
)
282 (function-item copy-file
)
283 (function :tag
"Other")))
285 (defcustom nnmail-read-incoming-hook
286 (if (eq system-type
'windows-nt
)
287 '(nnheader-ms-strip-cr)
289 "*Hook that will be run after the incoming mail has been transferred.
290 The incoming mail is moved from the specified spool file (which normally is
291 something like \"/usr/spool/mail/$user\") to the user's home
292 directory. This hook is called after the incoming mail box has been
293 emptied, and can be used to call any mail box programs you have
294 running (\"xwatch\", etc.)
298 \(add-hook 'nnmail-read-incoming-hook
300 (call-process \"/local/bin/mailsend\" nil nil nil
301 \"read\" nnmail-spool-file)))
303 If you have xwatch running, this will alert it that mail has been
306 If you use `display-time', you could use something like this:
308 \(add-hook 'nnmail-read-incoming-hook
310 ;; Update the displayed time, since that will clear out
311 ;; the flag that says you have mail.
312 (when (eq (process-status \"display-time\") 'run)
313 (display-time-filter display-time-process \"\"))))"
314 :group
'nnmail-prepare
317 (defcustom nnmail-prepare-incoming-hook nil
318 "Hook called before treating incoming mail.
319 The hook is run in a buffer with all the new, incoming mail."
320 :group
'nnmail-prepare
323 (defcustom nnmail-prepare-incoming-header-hook nil
324 "Hook called narrowed to the headers of each message.
325 This can be used to remove excessive spaces (and stuff like
326 that) from the headers before splitting and saving the messages."
327 :group
'nnmail-prepare
330 (defcustom nnmail-prepare-incoming-message-hook nil
331 "Hook called narrowed to each message."
332 :group
'nnmail-prepare
335 (defcustom nnmail-list-identifiers nil
336 "Regexp that matches list identifiers to be removed.
337 This can also be a list of regexps."
338 :group
'nnmail-prepare
339 :type
'(choice (const :tag
"none" nil
)
341 (repeat :value
(".*") regexp
)))
343 (defcustom nnmail-pre-get-new-mail-hook nil
344 "Hook called just before starting to handle new incoming mail."
345 :group
'nnmail-retrieve
348 (defcustom nnmail-post-get-new-mail-hook nil
349 "Hook called just after finishing handling new incoming mail."
350 :group
'nnmail-retrieve
353 (defcustom nnmail-split-hook nil
354 "Hook called before deciding where to split an article.
355 The functions in this hook are free to modify the buffer
356 contents in any way they choose -- the buffer contents are
357 discarded after running the split process."
361 (defcustom nnmail-spool-hook nil
362 "*A hook called when a new article is spooled."
367 (defcustom nnmail-large-newsgroup
50
368 "*The number of articles which indicates a large newsgroup or nil.
369 If the number of articles is greater than the value, verbose
370 messages will be shown to indicate the current status."
371 :group
'nnmail-various
372 :type
'(choice (const :tag
"infinite" nil
)
373 (number :tag
"count")))
375 (define-widget 'nnmail-lazy
'default
376 "Base widget for recursive datastructures.
378 This is copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
380 :convert-widget
'widget-value-convert-widget
381 :value-create
(lambda (widget)
382 (let ((value (widget-get widget
:value
))
383 (type (widget-get widget
:type
)))
384 (widget-put widget
:children
385 (list (widget-create-child-value
386 widget
(widget-convert type
) value
)))))
387 :value-delete
'widget-children-value-delete
388 :value-get
(lambda (widget)
389 (widget-value (car (widget-get widget
:children
))))
390 :value-inline
(lambda (widget)
391 (widget-apply (car (widget-get widget
:children
))
393 :default-get
(lambda (widget)
395 (widget-convert (widget-get widget
:type
))))
396 :match
(lambda (widget value
)
397 (widget-apply (widget-convert (widget-get widget
:type
))
399 :validate
(lambda (widget)
400 (widget-apply (car (widget-get widget
:children
)) :validate
)))
402 (define-widget 'nnmail-split-fancy
'nnmail-lazy
403 "Widget for customizing splits in the variable of the same name."
405 :type
'(menu-choice :value
(any ".*value.*" "misc")
407 (string :tag
"Destination")
408 (list :tag
"Use first match (|)" :value
(|
)
410 (editable-list :inline t nnmail-split-fancy
))
411 (list :tag
"Use all matches (&)" :value
(&)
413 (editable-list :inline t nnmail-split-fancy
))
414 (list :tag
"Function with fixed arguments (:)"
416 (const :format
"" :value
:)
418 (editable-list :inline t
(sexp :tag
"Arg"))
420 (list :tag
"Function with split arguments (!)"
424 (editable-list :inline t nnmail-split-fancy
))
425 (list :tag
"Field match"
430 (symbol :value mail
))
437 (const :tag
"Junk (delete mail)" junk
)))
439 (defcustom nnmail-split-fancy
"mail.misc"
440 "Incoming mail can be split according to this fancy variable.
441 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
443 The format of this variable is SPLIT, where SPLIT can be one of
446 GROUP: Mail will be stored in GROUP (a string).
448 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
449 field FIELD (a regexp) contains VALUE (a regexp), store the messages
450 as specified by SPLIT. If RESTRICT (a regexp) matches some string
451 after FIELD and before the end of the matched VALUE, return nil,
452 otherwise process SPLIT. Multiple RESTRICTs add up, further
453 restricting the possibility of processing SPLIT.
455 \(| SPLIT...): Process each SPLIT expression until one of them matches.
456 A SPLIT expression is said to match if it will cause the mail
457 message to be stored in one or more groups.
459 \(& SPLIT...): Process each SPLIT expression.
461 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
462 the buffer containing the message headers. The return value FUNCTION
463 should be a split, which is then recursively processed.
465 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
466 return value FUNCTION should be a split, which is then recursively
469 junk: Mail will be deleted. Use with care! Do not submerge in water!
471 (setq nnmail-split-fancy
472 '(| (\"Subject\" \"MAKE MONEY FAST\" junk)
473 ...other.rules.omitted...))
475 FIELD must match a complete field name. VALUE must match a complete
476 word according to the `nnmail-split-fancy-syntax-table' syntax table.
477 You can use \".*\" in the regexps to match partial field names or words.
479 FIELD and VALUE can also be lisp symbols, in that case they are expanded
480 as specified in `nnmail-split-abbrev-alist'.
482 GROUP can contain \\& and \\N which will substitute from matching
483 \\(\\) patterns in the previous VALUE.
487 \(setq nnmail-split-methods 'nnmail-split-fancy
489 ;; Messages from the mailer daemon are not crossposted to any of
490 ;; the ordinary groups. Warnings are put in a separate group
492 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
494 ;; Non-error messages are crossposted to all relevant
495 ;; groups, but we don't crosspost between the group for the
496 ;; (ding) list and the group for other (ding) related mail.
497 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
498 (\"subject\" \"ding\" \"ding.misc\"))
499 ;; Other mailing lists...
500 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
501 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
502 ;; Both lists below have the same suffix, so prevent
503 ;; cross-posting to mkpkg.list of messages posted only to
504 ;; the bugs- list, but allow cross-posting when the
505 ;; message was really cross-posted.
506 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
507 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
510 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
511 ;; Unmatched mail goes to the catch all group.
514 :type
'nnmail-split-fancy
)
516 (defcustom nnmail-split-abbrev-alist
517 '((any .
"from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
518 (mail .
"mailer-daemon\\|postmaster\\|uucp")
519 (to .
"to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
520 (from .
"from\\|sender\\|resent-from")
521 (nato .
"to\\|cc\\|resent-to\\|resent-cc")
522 (naany .
"from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
523 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
525 :type
'(repeat (cons :format
"%v" symbol regexp
)))
527 (defcustom nnmail-message-id-cache-length
1000
528 "*The approximate number of Message-IDs nnmail will keep in its cache.
529 If this variable is nil, no checking on duplicate messages will be
531 :group
'nnmail-duplicate
532 :type
'(choice (const :tag
"disable" nil
)
533 (integer :format
"%v")))
535 (defcustom nnmail-message-id-cache-file
"~/.nnmail-cache"
536 "*The file name of the nnmail Message-ID cache."
537 :group
'nnmail-duplicate
541 (defcustom nnmail-treat-duplicates
'warn
542 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
543 Three values are valid: nil, which means that nnmail is not to keep a
544 Message-ID cache; `warn', which means that nnmail should insert extra
545 headers to warn the user about the duplication (this is the default);
546 and `delete', which means that nnmail will delete duplicated mails.
548 This variable can also be a function. It will be called from a buffer
549 narrowed to the article in question with the Message-ID as a
550 parameter. It should return nil, `warn' or `delete'."
551 :group
'nnmail-duplicate
552 :type
'(choice (const :tag
"off" nil
)
556 (defcustom nnmail-extra-headers
'(To Newsgroups
)
557 "*Extra headers to parse."
560 :type
'(repeat symbol
))
562 (defcustom nnmail-split-header-length-limit
2048
563 "Header lines longer than this limit are excluded from the split function."
568 (defcustom nnmail-mail-splitting-charset nil
569 "Default charset to be used when splitting incoming mail."
574 (defcustom nnmail-mail-splitting-decodes nil
575 "Whether the nnmail splitting functionality should MIME decode headers."
580 (defcustom nnmail-split-fancy-match-partial-words nil
581 "Whether to match partial words when fancy splitting.
582 Normally, regexes given in `nnmail-split-fancy' are implicitly surrounded
583 by \"\\=\\<...\\>\". If this variable is true, they are not implicitly\
590 (defcustom nnmail-split-lowercase-expanded t
591 "Whether to lowercase expanded entries (i.e. \\N) when splitting mails.
592 This avoids the creation of multiple groups when users send to an address
593 using different case (i.e. mailing-list@domain vs Mailing-List@Domain)."
598 ;;; Internal variables.
600 (defvar nnmail-article-buffer
" *nnmail incoming*"
601 "The buffer used for splitting incoming mails.")
603 (defvar nnmail-split-history nil
604 "List of group/article elements that say where the previous split put messages.")
606 (defvar nnmail-split-fancy-syntax-table
607 (let ((table (make-syntax-table)))
608 ;; support the %-hack
609 (modify-syntax-entry ?\%
"." table
)
611 "Syntax table used by `nnmail-split-fancy'.")
613 (defvar nnmail-prepare-save-mail-hook nil
614 "Hook called before saving mail.")
616 (defvar nnmail-split-tracing nil
)
617 (defvar nnmail-split-trace nil
)
621 (defun nnmail-request-post (&optional server
)
622 (mail-send-and-exit nil
))
624 (defvar nnmail-file-coding-system
'raw-text
625 "Coding system used in nnmail.")
627 (defvar nnmail-incoming-coding-system
628 mm-text-coding-system
629 "Coding system used in reading inbox")
631 (defvar nnmail-pathname-coding-system nil
632 "*Coding system for file name.")
634 (defun nnmail-find-file (file)
635 "Insert FILE in server buffer safely."
636 (set-buffer nntp-server-buffer
)
637 (delete-region (point-min) (point-max))
638 (let ((format-alist nil
)
639 (after-insert-file-functions nil
))
641 (let ((coding-system-for-read nnmail-file-coding-system
)
642 (auto-mode-alist (mm-auto-mode-alist))
643 (file-name-coding-system nnmail-pathname-coding-system
))
644 (insert-file-contents file
)
648 (defun nnmail-group-pathname (group dir
&optional file
)
649 "Make file name for GROUP."
651 (let ((dir (file-name-as-directory (expand-file-name dir
))))
652 (setq group
(nnheader-replace-duplicate-chars-in-string
653 (nnheader-replace-chars-in-string group ?
/ ?_
)
655 (setq group
(nnheader-translate-file-chars group
))
656 ;; If this directory exists, we use it directly.
657 (file-name-as-directory
658 (if (or nnmail-use-long-file-names
659 (file-directory-p (concat dir group
)))
660 (expand-file-name group dir
)
661 ;; If not, we translate dots into slashes.
663 (mm-encode-coding-string
664 (nnheader-replace-chars-in-string group ?. ?
/)
665 nnmail-pathname-coding-system
)
669 (defun nnmail-get-active ()
670 "Returns an assoc of group names and active ranges.
671 nn*-request-list should have been called before calling this function."
672 ;; Go through all groups from the active list.
674 (set-buffer nntp-server-buffer
)
675 (nnmail-parse-active)))
677 (defun nnmail-parse-active ()
678 "Parse the active file in the current buffer and return an alist."
679 (goto-char (point-min))
680 (unless (re-search-forward "[\\\"]" nil t
)
681 (goto-char (point-max))
682 (while (re-search-backward "[][';?()#]" nil t
)
684 (goto-char (point-min))
685 (let ((buffer (current-buffer))
686 group-assoc group max min
)
690 (narrow-to-region (point) (gnus-point-at-eol))
691 (setq group
(read buffer
))
692 (unless (stringp group
)
693 (setq group
(symbol-name group
)))
694 (if (and (numberp (setq max
(read buffer
)))
695 (numberp (setq min
(read buffer
))))
696 (push (list group
(cons min max
))
703 (defvar nnmail-active-file-coding-system
'raw-text
704 "*Coding system for active file.")
706 (defun nnmail-save-active (group-assoc file-name
)
707 "Save GROUP-ASSOC in ACTIVE-FILE."
708 (let ((coding-system-for-write nnmail-active-file-coding-system
))
710 (with-temp-file file-name
711 (nnmail-generate-active group-assoc
)))))
713 (defun nnmail-generate-active (alist)
714 "Generate an active file from group-alist ALIST."
717 (while (setq group
(pop alist
))
718 (insert (format "%S %d %d y\n" (intern (car group
)) (cdadr group
)
720 (goto-char (point-max))
721 (while (search-backward "\\." nil t
)
724 (defun nnmail-get-split-group (file source
)
725 "Find out whether this FILE is to be split into GROUP only.
726 If SOURCE is a directory spec, try to return the group name component."
727 (if (eq (car source
) 'directory
)
728 (let ((file (file-name-nondirectory file
)))
729 (mail-source-bind (directory source
)
730 (if (string-match (concat (regexp-quote suffix
) "$") file
)
731 (substring file
0 (match-beginning 0))
735 (defun nnmail-process-babyl-mail-format (func artnum-func
)
736 (let ((case-fold-search t
)
738 start message-id content-length do-search end
)
740 (goto-char (point-min))
742 "\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t
)
743 (goto-char (match-end 0))
744 (delete-region (match-beginning 0) (match-end 0))
748 ;; Skip all the headers in case there are more "From "s...
749 (or (search-forward "\n\n" nil t
)
750 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t
)
751 (search-forward "\x1f\f"))
753 ;; Unquote the ">From " line, if any.
754 (goto-char (point-min))
755 (when (looking-at ">From ")
756 (replace-match "X-From-Line: ") )
757 (run-hooks 'nnmail-prepare-incoming-header-hook
)
758 (goto-char (point-max))
759 ;; Find the Message-ID header.
761 (if (re-search-backward
762 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t
)
763 (setq message-id
(buffer-substring (match-beginning 1)
765 ;; There is no Message-ID here, so we create one.
767 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
769 (insert "Original-")))
771 (insert "Message-ID: " (setq message-id
(nnmail-message-id))
773 ;; Look for a Content-Length header.
774 (if (not (save-excursion
775 (and (re-search-backward
776 "^Content-Length:[ \t]*\\([0-9]+\\)" start t
)
777 (setq content-length
(string-to-number
781 ;; We destroy the header, since none of
782 ;; the backends ever use it, and we do not
783 ;; want to confuse other mailers by having
784 ;; a (possibly) faulty header.
785 (progn (insert "X-") t
))))
788 (if (or (= (+ (point) content-length
) (point-max))
790 (goto-char (+ (point) content-length
))
791 (looking-at "\x1f")))
793 (goto-char (+ (point) content-length
))
794 (setq do-search nil
))
797 ;; Go to the beginning of the next article - or to the end
800 (if (re-search-forward "^\x1f" nil t
)
801 (goto-char (match-beginning 0))
802 (goto-char (1- (point-max)))))
803 (delete-char 1) ; delete ^_
806 (narrow-to-region start
(point))
807 (goto-char (point-min))
808 (nnmail-check-duplication message-id func artnum-func
)
810 (setq end
(point-max))))
814 (defsubst nnmail-search-unix-mail-delim
()
815 "Put point at the beginning of the next Unix mbox message."
816 ;; Algorithm used to find the next article in the
817 ;; brain-dead Unix mbox format:
819 ;; 1) Search for "^From ".
820 ;; 2) If we find it, then see whether the previous
821 ;; line is blank and the next line looks like a header.
822 ;; Then it's possible that this is a mail delim, and we use it.
823 (let ((case-fold-search nil
)
826 (if (not (re-search-forward "^From " nil t
))
830 (when (and (or (bobp)
833 (eq (char-after) ?
\n)))
836 (while (looking-at ">From \\|From ")
838 (looking-at "[^ \n\t:]+[ \n\t]*:")))
839 (setq found
'yes
)))))
843 (defun nnmail-search-unix-mail-delim-backward ()
844 "Put point at the beginning of the current Unix mbox message."
845 ;; Algorithm used to find the next article in the
846 ;; brain-dead Unix mbox format:
848 ;; 1) Search for "^From ".
849 ;; 2) If we find it, then see whether the previous
850 ;; line is blank and the next line looks like a header.
851 ;; Then it's possible that this is a mail delim, and we use it.
852 (let ((case-fold-search nil
)
855 (if (not (re-search-backward "^From " nil t
))
859 (when (and (or (bobp)
862 (eq (char-after) ?
\n)))
865 (while (looking-at ">From \\|From ")
867 (looking-at "[^ \n\t:]+[ \n\t]*:")))
868 (setq found
'yes
)))))
872 (defun nnmail-process-unix-mail-format (func artnum-func
)
873 (let ((case-fold-search t
)
875 start message-id content-length end skip head-end
)
876 (goto-char (point-min))
877 (if (not (and (re-search-forward "^From " nil t
)
878 (goto-char (match-beginning 0))))
879 ;; Possibly wrong format?
880 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
881 (if (buffer-file-name) "file" "buffer")
882 (or (buffer-file-name) (buffer-name)))
883 ;; Carry on until the bitter end.
887 ;; Find the end of the head.
890 (if (search-forward "\n\n" nil t
)
892 ;; This will never happen, but just to be on the safe side --
893 ;; if there is no head-body delimiter, we search a bit manually.
894 (while (and (looking-at "From \\|[^ \t]+:")
898 ;; Find the Message-ID header.
899 (goto-char (point-min))
900 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t
)
901 (setq message-id
(match-string 1))
903 (when (re-search-forward "^Message-ID[ \t]*:" nil t
)
905 (insert "Original-")))
906 ;; There is no Message-ID here, so we create one.
908 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
909 ;; Look for a Content-Length header.
910 (goto-char (point-min))
911 (if (not (re-search-forward
912 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t
))
913 (setq content-length nil
)
914 (setq content-length
(string-to-number (match-string 1)))
915 ;; We destroy the header, since none of the backends ever
916 ;; use it, and we do not want to confuse other mailers by
917 ;; having a (possibly) faulty header.
920 (run-hooks 'nnmail-prepare-incoming-header-hook
)
921 ;; Find the end of this article.
922 (goto-char (point-max))
924 (setq head-end
(point))
925 ;; We try the Content-Length value. The idea: skip over the header
926 ;; separator, then check what happens content-length bytes into the
927 ;; message body. This should be either the end of the buffer, the
928 ;; message separator or a blank line followed by the separator.
929 ;; The blank line should probably be deleted. If neither of the
930 ;; three is met, the content-length header is probably invalid.
933 (setq skip
(+ (point) content-length
))
935 (cond ((or (= skip
(point-max))
936 (= (1+ skip
) (point-max)))
937 (setq end
(point-max)))
938 ((looking-at "From ")
940 ((looking-at "[ \t]*\n\\(From \\)")
941 (setq end
(match-beginning 1)))
945 ;; No Content-Length, so we find the beginning of the next
946 ;; article or the end of the buffer.
948 (or (nnmail-search-unix-mail-delim)
949 (goto-char (point-max))))
950 ;; Allow the backend to save the article.
953 (narrow-to-region start
(point))
954 (goto-char (point-min))
956 (nnmail-check-duplication message-id func artnum-func
)
957 (setq end
(point-max))))
961 (defun nnmail-process-mmdf-mail-format (func artnum-func
)
962 (let ((delim "^\^A\^A\^A\^A$")
965 start message-id end
)
966 (goto-char (point-min))
967 (if (not (and (re-search-forward delim nil t
)
969 ;; Possibly wrong format?
970 (error "Error, unknown mail format! (Possibly corrupted.)")
971 ;; Carry on until the bitter end.
974 ;; Find the end of the head.
977 (if (search-forward "\n\n" nil t
)
979 ;; This will never happen, but just to be on the safe side --
980 ;; if there is no head-body delimiter, we search a bit manually.
981 (while (and (looking-at "From \\|[^ \t]+:")
985 ;; Find the Message-ID header.
986 (goto-char (point-min))
987 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t
)
988 (setq message-id
(match-string 1))
989 ;; There is no Message-ID here, so we create one.
991 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
993 (insert "Original-")))
995 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
996 (run-hooks 'nnmail-prepare-incoming-header-hook
)
997 ;; Find the end of this article.
998 (goto-char (point-max))
1000 (if (re-search-forward delim nil t
)
1002 (goto-char (point-max)))
1003 ;; Allow the backend to save the article.
1006 (narrow-to-region start
(point))
1007 (goto-char (point-min))
1009 (nnmail-check-duplication message-id func artnum-func
)
1010 (setq end
(point-max))))
1015 (defun nnmail-process-maildir-mail-format (func artnum-func
)
1016 ;; In a maildir, every file contains exactly one mail.
1017 (let ((case-fold-search t
)
1019 (goto-char (point-min))
1020 ;; Find the end of the head.
1023 (if (search-forward "\n\n" nil t
)
1025 ;; This will never happen, but just to be on the safe side --
1026 ;; if there is no head-body delimiter, we search a bit manually.
1027 (while (and (looking-at "From \\|[^ \t]+:")
1031 ;; Find the Message-ID header.
1032 (goto-char (point-min))
1033 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t
)
1034 (setq message-id
(match-string 1))
1035 ;; There is no Message-ID here, so we create one.
1037 (when (re-search-backward "^Message-ID[ \t]*:" nil t
)
1039 (insert "Original-")))
1041 (insert "Message-ID: " (setq message-id
(nnmail-message-id)) "\n"))
1042 (run-hooks 'nnmail-prepare-incoming-header-hook
)
1043 ;; Allow the backend to save the article.
1046 (goto-char (point-min))
1047 (nnmail-check-duplication message-id func artnum-func
))
1050 (defun nnmail-split-incoming (incoming func
&optional exit-func
1052 "Go through the entire INCOMING file and pick out each individual mail.
1053 FUNC will be called with the buffer narrowed to each mail."
1054 (let ( ;; If this is a group-specific split, we bind the split
1055 ;; methods to just this group.
1056 (nnmail-split-methods (if (and group
1057 (not nnmail-resplit-incoming
))
1058 (list (list group
""))
1059 nnmail-split-methods
)))
1061 ;; Insert the incoming file.
1062 (set-buffer (get-buffer-create nnmail-article-buffer
))
1064 (let ((coding-system-for-read nnmail-incoming-coding-system
))
1065 (mm-insert-file-contents incoming
))
1067 (if (zerop (buffer-size))
1069 (goto-char (point-min))
1070 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook
))
1071 ;; Handle both babyl, MMDF and unix mail formats, since
1072 ;; movemail will use the former when fetching from a
1073 ;; mailbox, the latter when fetching from a file.
1074 (cond ((or (looking-at "\^L")
1075 (looking-at "BABYL OPTIONS:"))
1076 (nnmail-process-babyl-mail-format func artnum-func
))
1077 ((looking-at "\^A\^A\^A\^A")
1078 (nnmail-process-mmdf-mail-format func artnum-func
))
1079 ((looking-at "Return-Path:")
1080 (nnmail-process-maildir-mail-format func artnum-func
))
1082 (nnmail-process-unix-mail-format func artnum-func
))))
1084 (funcall exit-func
))
1085 (kill-buffer (current-buffer))))))
1087 (defun nnmail-article-group (func &optional trace
)
1088 "Look at the headers and return an alist of groups that match.
1089 FUNC will be called with the group name to determine the article number."
1090 (let ((methods (or nnmail-split-methods
'(("bogus" ""))))
1091 (obuf (current-buffer))
1092 group-art method grp
)
1093 (if (and (sequencep methods
)
1094 (= (length methods
) 1))
1095 ;; If there is only just one group to put everything in, we
1096 ;; just return a list with just this one method in.
1098 (list (cons (caar methods
) (funcall func
(caar methods
)))))
1099 ;; We do actual comparison.
1101 ;; Copy the article into the work buffer.
1102 (set-buffer nntp-server-buffer
)
1104 (insert-buffer-substring obuf
)
1105 ;; Narrow to headers.
1107 (goto-char (point-min))
1108 (if (search-forward "\n\n" nil t
)
1111 (goto-char (point-min))
1112 ;; Decode MIME headers and charsets.
1113 (when nnmail-mail-splitting-decodes
1114 (let ((mail-parse-charset nnmail-mail-splitting-charset
))
1115 (mail-decode-encoded-word-region (point-min) (point-max))))
1116 ;; Fold continuation lines.
1117 (goto-char (point-min))
1118 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t
)
1119 (replace-match " " t t
))
1120 ;; Nuke pathologically long headers. Since Gnus applies
1121 ;; pathologically complex regexps to the buffer, lines
1122 ;; that are looong will take longer than the Universe's
1123 ;; existence to process.
1124 (goto-char (point-min))
1126 (unless (< (move-to-column nnmail-split-header-length-limit
)
1127 nnmail-split-header-length-limit
)
1128 (delete-region (point) (gnus-point-at-eol)))
1131 (goto-char (point-min))
1132 (run-hooks 'nnmail-split-hook
)
1133 (when (setq nnmail-split-tracing trace
)
1134 (setq nnmail-split-trace nil
))
1135 (if (and (symbolp nnmail-split-methods
)
1136 (fboundp nnmail-split-methods
))
1138 (condition-case error-info
1139 ;; `nnmail-split-methods' is a function, so we
1140 ;; just call this function here and use the
1142 (or (funcall nnmail-split-methods
)
1146 5 "Error in `nnmail-split-methods'; using `bogus' mail group: %S" error-info
)
1149 (setq split
(mm-delete-duplicates split
))
1150 ;; The article may be "cross-posted" to `junk'. What
1151 ;; to do? Just remove the `junk' spec. Don't really
1152 ;; see anything else to do...
1154 (while (setq elem
(car (memq 'junk split
)))
1155 (setq split
(delq elem split
))))
1159 (lambda (group) (cons group
(funcall func group
)))
1161 ;; Go through the split methods to find a match.
1163 (or nnmail-crosspost
1165 (goto-char (point-max))
1166 (setq method
(pop methods
)
1169 (not (equal "" (nth 1 method
))))
1172 (if (stringp (nth 1 method
))
1173 (let ((expand (string-match "\\\\[0-9&]" grp
))
1174 (pos (re-search-backward (cadr method
)
1177 (setq grp
(nnmail-expand-newtext grp
)))
1179 ;; Function to say whether this is a match.
1180 (funcall (nth 1 method
) grp
)))
1181 ;; Don't enter the article into the same
1183 (not (assoc grp group-art
)))
1184 (push (cons grp
(funcall func grp
))
1186 ;; This is the final group, which is used as a
1190 (list (cons (car method
)
1191 (funcall func
(car method
))))))))
1192 ;; Fall back on "bogus" if all else fails.
1194 (setq group-art
(list (cons "bogus" (funcall func
"bogus"))))))
1195 ;; Produce a trace if non-empty.
1196 (when (and trace nnmail-split-trace
)
1197 (let ((restore (current-buffer)))
1198 (nnheader-set-temp-buffer "*Split Trace*")
1200 (dolist (trace (nreverse nnmail-split-trace
))
1201 (prin1 trace
(current-buffer))
1203 (goto-char (point-min))
1204 (gnus-configure-windows 'split-trace
)
1205 (set-buffer restore
)))
1207 ;; See whether the split methods returned `junk'.
1208 (if (equal group-art
'(junk))
1210 ;; The article may be "cross-posted" to `junk'. What
1211 ;; to do? Just remove the `junk' spec. Don't really
1212 ;; see anything else to do...
1214 (while (setq elem
(car (memq 'junk group-art
)))
1215 (setq group-art
(delq elem group-art
)))
1216 (nreverse group-art
)))))))
1218 (defun nnmail-insert-lines ()
1219 "Insert how many lines there are in the body of the mail.
1220 Return the number of characters in the body."
1223 (goto-char (point-min))
1224 (unless (search-forward "\n\n" nil t
)
1225 (goto-char (point-max))
1227 (setq chars
(- (point-max) (point)))
1228 (setq lines
(count-lines (point) (point-max)))
1231 (when (re-search-backward "^Lines: " nil t
)
1232 (delete-region (point) (progn (forward-line 1) (point)))))
1234 (insert (format "Lines: %d\n" (max lines
0)))
1237 (defun nnmail-insert-xref (group-alist)
1238 "Insert an Xref line based on the (group . article) alist."
1240 (goto-char (point-min))
1241 (unless (search-forward "\n\n" nil t
)
1242 (goto-char (point-max))
1245 (when (re-search-backward "^Xref: " nil t
)
1246 (delete-region (match-beginning 0)
1247 (progn (forward-line 1) (point))))
1248 (insert (format "Xref: %s" (system-name)))
1250 (insert (format " %s:%d"
1251 (mm-encode-coding-string
1253 nnmail-pathname-coding-system
)
1254 (cdar group-alist
)))
1255 (setq group-alist
(cdr group-alist
)))
1258 ;;; Message washing functions
1260 (defun nnmail-remove-leading-whitespace ()
1261 "Remove excessive whitespace from all headers."
1262 (goto-char (point-min))
1263 (while (re-search-forward "^\\([^ :]+: \\) +" nil t
)
1264 (replace-match "\\1" t
)))
1266 (defun nnmail-remove-list-identifiers ()
1267 "Remove list identifiers from Subject headers."
1269 (if (consp nnmail-list-identifiers
)
1270 (mapconcat 'identity nnmail-list-identifiers
" *\\|")
1271 nnmail-list-identifiers
)))
1273 (goto-char (point-min))
1274 (while (re-search-forward
1275 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp
" *\\)")
1277 (delete-region (match-beginning 2) (match-end 0))
1278 (beginning-of-line))
1279 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1281 (delete-region (match-beginning 1) (match-end 1))
1282 (beginning-of-line)))))
1284 (defun nnmail-remove-tabs ()
1285 "Translate TAB characters into SPACE characters."
1286 (subst-char-in-region (point-min) (point-max) ?
\t ? t
))
1288 (defun nnmail-fix-eudora-headers ()
1289 "Eudora has a broken References line, but an OK In-Reply-To."
1290 (goto-char (point-min))
1291 (when (re-search-forward "^X-Mailer:.*Eudora" nil t
)
1292 (goto-char (point-min))
1293 (when (re-search-forward "^References:" nil t
)
1295 (insert "X-Gnus-Broken-Eudora-"))
1296 (goto-char (point-min))
1297 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t
)
1298 (replace-match "\\1" t
))))
1300 (custom-add-option 'nnmail-prepare-incoming-header-hook
1301 'nnmail-fix-eudora-headers
)
1303 ;;; Utility functions
1305 (defun nnmail-do-request-post (accept-func &optional server
)
1306 "Utility function to directly post a message to an nnmail-derived group.
1307 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1308 to actually put the message in the right group."
1310 (dolist (mbx (message-unquote-tokens
1311 (message-tokenize-header
1312 (message-fetch-field "Newsgroups") ", ")) success
)
1313 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method
)))
1314 (or (gnus-active to-newsgroup
)
1315 (gnus-activate-group to-newsgroup
)
1316 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1318 (or (and (gnus-request-create-group
1319 to-newsgroup gnus-command-method
)
1320 (gnus-activate-group to-newsgroup nil nil
1321 gnus-command-method
))
1322 (error "Couldn't create group %s" to-newsgroup
)))
1323 (error "No such group: %s" to-newsgroup
))
1324 (unless (funcall accept-func mbx
(nth 1 gnus-command-method
))
1325 (setq success nil
))))))
1327 (defun nnmail-split-fancy ()
1328 "Fancy splitting method.
1329 See the documentation for the variable `nnmail-split-fancy' for details."
1330 (let ((syntab (syntax-table)))
1333 (set-syntax-table nnmail-split-fancy-syntax-table
)
1334 (nnmail-split-it nnmail-split-fancy
))
1335 (set-syntax-table syntab
))))
1337 (defvar nnmail-split-cache nil
)
1338 ;; Alist of split expressions their equivalent regexps.
1340 (defun nnmail-split-it (split)
1341 ;; Return a list of groups matching SPLIT.
1348 ;; A group name. Do the \& and \N subs into the string.
1350 (when nnmail-split-tracing
1351 (push split nnmail-split-trace
))
1352 (list (nnmail-expand-newtext split
)))
1354 ;; Junk the message.
1356 (when nnmail-split-tracing
1357 (push "junk" nnmail-split-trace
))
1360 ;; Builtin & operation.
1361 ((eq (car split
) '&)
1362 (apply 'nconc
(mapcar 'nnmail-split-it
(cdr split
))))
1364 ;; Builtin | operation.
1365 ((eq (car split
) '|
)
1367 (while (and (not done
) (cdr split
))
1368 (setq split
(cdr split
)
1369 done
(nnmail-split-it (car split
))))
1372 ;; Builtin : operation.
1373 ((eq (car split
) ':)
1374 (when nnmail-split-tracing
1375 (push split nnmail-split-trace
))
1376 (nnmail-split-it (save-excursion (eval (cdr split
)))))
1378 ;; Builtin ! operation.
1379 ((eq (car split
) '!)
1380 (funcall (cadr split
) (nnmail-split-it (caddr split
))))
1382 ;; Check the cache for the regexp for this split.
1383 ((setq cached-pair
(assq split nnmail-split-cache
))
1385 (end-point (point-max))
1386 (value (nth 1 split
)))
1388 (setq value
(cdr (assq value nnmail-split-abbrev-alist
))))
1389 (while (and (goto-char end-point
)
1390 (re-search-backward (cdr cached-pair
) nil t
))
1391 (when nnmail-split-tracing
1392 (push split nnmail-split-trace
))
1393 (let ((split-rest (cddr split
))
1395 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1396 ;; So, start-of-value is the point just before the
1397 ;; beginning of the value, whereas after-header-name
1398 ;; is the point just after the field name.
1399 (start-of-value (match-end 1))
1400 (after-header-name (match-end 2)))
1401 ;; Start the next search just before the beginning of the
1403 (setq end-point
(1- start-of-value
))
1404 ;; Handle - RESTRICTs
1405 (while (eq (car split-rest
) '-
)
1406 ;; RESTRICT must start after-header-name and
1407 ;; end after start-of-value, so that, for
1408 ;; (any "foo" - "x-foo" "foo.list")
1409 ;; we do not exclude foo.list just because
1410 ;; the header is: ``To: x-foo, foo''
1412 (if (and (re-search-backward (cadr split-rest
)
1413 after-header-name t
)
1414 (> (match-end 0) start-of-value
))
1415 (setq split-rest nil
)
1416 (setq split-rest
(cddr split-rest
))))
1419 (let ((value (nth 1 split
)))
1421 (setq value
(cdr (assq value nnmail-split-abbrev-alist
))))
1422 ;; Someone might want to do a \N sub on this match, so get the
1423 ;; correct match positions.
1424 (re-search-backward value start-of-value
))
1425 (dolist (sp (nnmail-split-it (car split-rest
)))
1426 (unless (member sp split-result
)
1427 (push sp split-result
))))))
1430 ;; Not in cache, compute a regexp for the field/value pair.
1432 (let ((field (nth 0 split
))
1433 (value (nth 1 split
))
1434 (split-rest (cddr split
))
1439 (setq value
(cdr (assq value nnmail-split-abbrev-alist
))))
1440 (if (and (>= (length value
) 2)
1441 (string= ".*" (substring value
0 2)))
1442 (setq value
(substring value
2)
1444 ;; Same trick for the rear of the regexp
1445 (if (and (>= (length value
) 2)
1446 (string= ".*" (substring value -
2)))
1447 (setq value
(substring value
0 -
2)
1449 ;; Invert the match-partial-words behavior if the optional
1450 ;; last element is specified.
1451 (while (eq (car split-rest
) '-
)
1452 (setq split-rest
(cddr split-rest
)))
1453 (when (if (cadr split-rest
)
1454 (not nnmail-split-fancy-match-partial-words
)
1455 nnmail-split-fancy-match-partial-words
)
1456 (setq partial-front
""
1458 (setq regexp
(concat "^\\(\\("
1460 (cdr (assq field nnmail-split-abbrev-alist
))
1463 (or partial-front
"\\<")
1467 (or partial-rear
"\\>")))
1468 (push (cons split regexp
) nnmail-split-cache
)
1469 ;; Now that it's in the cache, just call nnmail-split-it again
1470 ;; on the same split, which will find it immediately in the cache.
1471 (nnmail-split-it split
))))))
1473 (defun nnmail-expand-newtext (newtext)
1474 (let ((len (length newtext
))
1476 c expanded beg N did-expand
)
1479 (while (and (< pos len
)
1480 (not (= (aref newtext pos
) ?
\\)))
1481 (setq pos
(1+ pos
)))
1483 (push (substring newtext beg pos
) expanded
))
1485 ;; We hit a \; expand it.
1488 c
(aref newtext pos
))
1489 (if (not (or (= c ?\
&)
1492 ;; \ followed by some character we don't expand.
1493 (push (char-to-string c
) expanded
)
1498 (when (match-beginning N
)
1499 (push (if nnmail-split-lowercase-expanded
1500 (downcase (buffer-substring (match-beginning N
)
1502 (buffer-substring (match-beginning N
) (match-end N
)))
1504 (setq pos
(1+ pos
)))
1506 (apply 'concat
(nreverse expanded
))
1509 ;; Activate a backend only if it isn't already activated.
1510 ;; If FORCE, re-read the active file even if the backend is
1511 ;; already activated.
1512 (defun nnmail-activate (backend &optional force
)
1513 (nnheader-init-server-buffer)
1514 (let (file timestamp file-time
)
1515 (if (or (not (symbol-value (intern (format "%s-group-alist" backend
))))
1517 (and (setq file
(ignore-errors
1518 (symbol-value (intern (format "%s-active-file"
1520 (setq file-time
(nth 5 (file-attributes file
)))
1524 (symbol-value (intern
1525 (format "%s-active-timestamp"
1528 (not (consp timestamp
))
1529 (equal timestamp
'(0 0))
1530 (> (nth 0 file-time
) (nth 0 timestamp
))
1531 (and (= (nth 0 file-time
) (nth 0 timestamp
))
1532 (> (nth 1 file-time
) (nth 1 timestamp
))))))
1534 (or (eq timestamp
'none
)
1535 (set (intern (format "%s-active-timestamp" backend
))
1537 (funcall (intern (format "%s-request-list" backend
)))))
1540 (defun nnmail-message-id ()
1541 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1544 ;;; nnmail duplicate handling
1547 (defvar nnmail-cache-buffer nil
)
1549 (defun nnmail-cache-open ()
1550 (if (or (not nnmail-treat-duplicates
)
1551 (and nnmail-cache-buffer
1552 (buffer-name nnmail-cache-buffer
)))
1553 () ; The buffer is open.
1556 (setq nnmail-cache-buffer
1557 (get-buffer-create " *nnmail message-id cache*")))
1559 (when (file-exists-p nnmail-message-id-cache-file
)
1560 (nnheader-insert-file-contents nnmail-message-id-cache-file
))
1561 (set-buffer-modified-p nil
)
1564 (defun nnmail-cache-close ()
1565 (when (and nnmail-cache-buffer
1566 nnmail-treat-duplicates
1567 (buffer-name nnmail-cache-buffer
)
1568 (buffer-modified-p nnmail-cache-buffer
))
1570 (set-buffer nnmail-cache-buffer
)
1571 ;; Weed out the excess number of Message-IDs.
1572 (goto-char (point-max))
1573 (when (search-backward "\n" nil t nnmail-message-id-cache-length
)
1576 (delete-region (point-min) (point))))
1578 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file
))
1579 (make-directory (file-name-directory nnmail-message-id-cache-file
)
1581 (nnmail-write-region (point-min) (point-max)
1582 nnmail-message-id-cache-file nil
'silent
)
1583 (set-buffer-modified-p nil
)
1584 (setq nnmail-cache-buffer nil
)
1585 (gnus-kill-buffer (current-buffer)))))
1587 ;; Compiler directives.
1589 (defvar group-art-list
)
1591 (defun nnmail-cache-insert (id grp
&optional subject sender
)
1593 ;; this will handle cases like `B r' where the group is nil
1594 (let ((grp (or grp gnus-newsgroup-name
"UNKNOWN")))
1595 (run-hook-with-args 'nnmail-spool-hook
1596 id grp subject sender
))
1597 (when nnmail-treat-duplicates
1598 ;; Store some information about the group this message is written
1599 ;; to. This is passed in as the grp argument -- all locations this
1600 ;; has been called from have been checked and the group is available.
1601 ;; The only ambiguous case is nnmail-check-duplication which will only
1602 ;; pass the first (of possibly >1) group which matches. -Josh
1603 (unless (gnus-buffer-live-p nnmail-cache-buffer
)
1604 (nnmail-cache-open))
1606 (set-buffer nnmail-cache-buffer
)
1607 (goto-char (point-max))
1608 (if (and grp
(not (string= "" grp
))
1609 (gnus-methods-equal-p gnus-command-method
1610 (nnmail-cache-primary-mail-backend)))
1611 (let ((regexp (if (consp nnmail-cache-ignore-groups
)
1612 (mapconcat 'identity nnmail-cache-ignore-groups
1614 nnmail-cache-ignore-groups
)))
1615 (unless (and regexp
(string-match regexp grp
))
1616 (insert id
"\t" grp
"\n")))
1617 (insert id
"\n"))))))
1619 (defun nnmail-cache-primary-mail-backend ()
1620 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods
))
1624 (while (and (null res
) be-list
)
1625 (setq be
(car be-list
))
1626 (setq be-list
(cdr be-list
))
1627 (when (and (gnus-method-option-p be
'respool
)
1629 (intern (format "%s-get-new-mail" (car be
))))
1630 (boundp get-new-mail
)
1631 (symbol-value get-new-mail
))
1635 ;; Fetch the group name corresponding to the message id stored in the
1637 (defun nnmail-cache-fetch-group (id)
1638 (when (and nnmail-treat-duplicates nnmail-cache-buffer
)
1640 (set-buffer nnmail-cache-buffer
)
1641 (goto-char (point-max))
1642 (when (search-backward id nil t
)
1644 (skip-chars-forward "^\n\r\t")
1645 (unless (looking-at "[\r\n]")
1647 (buffer-substring (point) (gnus-point-at-eol)))))))
1649 ;; Function for nnmail-split-fancy: look up all references in the
1650 ;; cache and if a match is found, return that group.
1651 (defun nnmail-split-fancy-with-parent ()
1652 "Split this message into the same group as its parent.
1653 This function can be used as an entry in `nnmail-split-fancy', for
1654 example like this: (: nnmail-split-fancy-with-parent)
1655 For a message to be split, it looks for the parent message in the
1656 References or In-Reply-To header and then looks in the message id
1657 cache file (given by the variable `nnmail-message-id-cache-file') to
1658 see which group that message was put in. This group is returned.
1660 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1661 (let* ((refstr (or (message-fetch-field "references")
1662 (message-fetch-field "in-reply-to")))
1665 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups
)
1667 (lambda (x) (format "\\(%s\\)" x
))
1668 nnmail-split-fancy-with-parent-ignore-groups
1670 nnmail-split-fancy-with-parent-ignore-groups
)))
1672 (setq references
(nreverse (gnus-split-references refstr
)))
1673 (unless (gnus-buffer-live-p nnmail-cache-buffer
)
1674 (nnmail-cache-open))
1676 (setq res
(or (nnmail-cache-fetch-group x
) res
))
1677 (when (or (member res
'("delayed" "drafts" "queue"))
1678 (and regexp res
(string-match regexp res
)))
1683 (defun nnmail-cache-id-exists-p (id)
1684 (when nnmail-treat-duplicates
1686 (set-buffer nnmail-cache-buffer
)
1687 (goto-char (point-max))
1688 (search-backward id nil t
))))
1690 (defun nnmail-fetch-field (header)
1693 (message-narrow-to-head)
1694 (message-fetch-field header
))))
1696 (defun nnmail-check-duplication (message-id func artnum-func
)
1697 (run-hooks 'nnmail-prepare-incoming-message-hook
)
1698 ;; If this is a duplicate message, then we do not save it.
1699 (let* ((duplication (nnmail-cache-id-exists-p message-id
))
1700 (case-fold-search t
)
1701 (action (when duplication
1703 ((memq nnmail-treat-duplicates
'(warn delete
))
1704 nnmail-treat-duplicates
)
1705 ((functionp nnmail-treat-duplicates
)
1706 (funcall nnmail-treat-duplicates message-id
))
1708 nnmail-treat-duplicates
))))
1710 ;; We insert a line that says what the mail source is.
1711 (let ((case-fold-search t
))
1712 (goto-char (point-min))
1713 (re-search-forward "^message-id[ \t]*:" nil t
)
1715 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string
)))
1717 ;; Let the backend save the article (or not).
1720 (funcall func
(setq group-art
1721 (nreverse (nnmail-article-group artnum-func
))))
1722 (nnmail-cache-insert message-id
(caar group-art
)))
1723 ((eq action
'delete
)
1724 (setq group-art nil
))
1726 ;; We insert a warning.
1727 (let ((case-fold-search t
))
1728 (goto-char (point-min))
1729 (re-search-forward "^message-id[ \t]*:" nil t
)
1732 "Gnus-Warning: This is a duplicate of message " message-id
"\n")
1733 (funcall func
(setq group-art
1734 (nreverse (nnmail-article-group artnum-func
))))))
1736 (funcall func
(setq group-art
1737 (nreverse (nnmail-article-group artnum-func
))))))
1738 ;; Add the group-art list to the history list.
1740 (push group-art nnmail-split-history
)
1741 (delete-region (point-min) (point-max)))))
1745 (defvar nnmail-fetched-sources nil
)
1747 (defun nnmail-get-value (&rest args
)
1748 (let ((sym (intern (apply 'format args
))))
1750 (symbol-value sym
))))
1752 (defun nnmail-get-new-mail (method exit-func temp
1753 &optional group spool-func
)
1754 "Read new incoming mail."
1755 (let* ((sources (or mail-sources
1756 (if (listp nnmail-spool-file
)
1758 (list nnmail-spool-file
))))
1764 incoming incomings source
)
1765 (when (and (nnmail-get-value "%s-get-new-mail" method
)
1767 (while (setq source
(pop sources
))
1768 ;; Be compatible with old values.
1773 ((string-match "^po:" source
)
1774 (list 'pop
:user
(substring source
(match-end 0))))
1775 ((file-directory-p source
)
1776 (list 'directory
:path source
))
1778 (list 'file
:path source
)))))
1779 ((eq source
'procmail
)
1780 (message "Invalid value for nnmail-spool-file: `procmail'")
1782 ;; Hack to only fetch the contents of a single group's spool file.
1783 (when (and (eq (car source
) 'directory
)
1784 (null nnmail-scan-directory-mail-source-once
)
1786 (mail-source-bind (directory source
)
1787 (setq source
(append source
1793 ,(concat group suffix
)
1794 (file-name-nondirectory file
)))))))))
1795 (when nnmail-fetched-sources
1796 (if (member source nnmail-fetched-sources
)
1798 (push source nnmail-fetched-sources
)
1799 (push source fetching-sources
)))))
1800 (when fetching-sources
1801 ;; We first activate all the groups.
1802 (nnmail-activate method
)
1803 ;; Allow the user to hook.
1804 (run-hooks 'nnmail-pre-get-new-mail-hook
)
1805 ;; Open the message-id cache.
1807 ;; The we go through all the existing mail source specification
1808 ;; and fetch the mail from each.
1809 (while (setq source
(pop fetching-sources
))
1810 (nnheader-message 4 "%s: Reading incoming mail from %s..."
1811 method
(car source
))
1816 `(lambda (file orig-file
)
1817 (nnmail-split-incoming
1818 file
',(intern (format "%s-save-mail" method
))
1820 (if (equal file orig-file
)
1822 (nnmail-get-split-group orig-file
',source
))
1823 ',(intern (format "%s-active-number" method
)))))))
1826 ;; If we did indeed read any incoming spools, we save all info.
1828 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1829 method
(car source
))
1831 (nnmail-get-value "%s-group-alist" method
)
1832 (nnmail-get-value "%s-active-file" method
))
1834 (funcall exit-func
))
1835 (run-hooks 'nnmail-read-incoming-hook
)
1836 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1838 ;; Close the message-id cache.
1839 (nnmail-cache-close)
1840 ;; Allow the user to hook.
1841 (run-hooks 'nnmail-post-get-new-mail-hook
))))
1843 (defun nnmail-expired-article-p (group time force
&optional inhibit
)
1844 "Say whether an article that is TIME old in GROUP should be expired."
1847 (let ((days (or (and nnmail-expiry-wait-function
1848 (funcall nnmail-expiry-wait-function group
))
1849 nnmail-expiry-wait
)))
1850 (cond ((or (eq days
'never
)
1853 ;; This isn't an expirable group.
1855 ((eq days
'immediate
)
1856 ;; We expire all articles on sight.
1858 ((equal time
'(0 0))
1859 ;; This is an ange-ftp group, and we don't have any dates.
1862 (setq days
(days-to-time days
))
1863 ;; Compare the time with the current time.
1864 (ignore-errors (time-less-p days
(time-since time
))))))))
1866 (defun nnmail-expiry-target-group (target group
)
1867 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1868 ;; that buffer if the nnfolder group isn't selected.
1869 (let (nnmail-cache-accepted-message-ids)
1870 ;; Don't enter Message-IDs into cache.
1871 ;; Let users hack it in TARGET function.
1872 (when (functionp target
)
1873 (setq target
(funcall target group
)))
1874 (unless (eq target
'delete
)
1875 (when (or (gnus-request-group target
)
1876 (gnus-request-create-group target
))
1877 (let ((group-art (gnus-request-accept-article target nil nil t
)))
1878 (when (consp group-art
)
1879 (gnus-group-mark-article-read target
(cdr group-art
))))))))
1881 (defun nnmail-fancy-expiry-target (group)
1882 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1884 (case-fold-search nil
)
1885 (from (or (message-fetch-field "from") ""))
1886 (to (or (message-fetch-field "to") ""))
1887 (date (message-fetch-field "date"))
1893 (message "%s" (error-message-string err
))
1896 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets
) target
)
1897 (setq header
(car regexp-target-pair
))
1899 ;; If the header is to-from then match against the
1900 ;; To or From header
1901 ((and (equal header
'to-from
)
1902 (or (string-match (cadr regexp-target-pair
) from
)
1903 (and (string-match (cadr regexp-target-pair
) to
)
1904 (let ((rmail-dont-reply-to-names
1905 message-dont-reply-to-names
))
1906 (equal (rmail-dont-reply-to from
) "")))))
1907 (setq target
(format-time-string (caddr regexp-target-pair
) date
)))
1908 ((and (not (equal header
'to-from
))
1909 (string-match (cadr regexp-target-pair
)
1911 (message-fetch-field header
)
1914 (format-time-string (caddr regexp-target-pair
) date
)))))))
1916 (defun nnmail-check-syntax ()
1917 "Check (and modify) the syntax of the message in the current buffer."
1919 (message-narrow-to-head)
1920 (let ((case-fold-search t
))
1921 (unless (re-search-forward "^Message-ID[ \t]*:" nil t
)
1922 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1924 (defun nnmail-write-region (start end filename
&optional append visit lockname
)
1925 "Do a `write-region', and then set the file modes."
1926 (let ((coding-system-for-write nnmail-file-coding-system
)
1927 (file-name-coding-system nnmail-pathname-coding-system
))
1928 (write-region start end filename append visit lockname
)
1929 (set-file-modes filename nnmail-default-file-modes
)))
1932 ;;; Status functions
1935 (defun nnmail-replace-status (name value
)
1936 "Make status NAME and VALUE part of the current status line."
1938 (message-narrow-to-head)
1939 (let ((status (nnmail-decode-status)))
1940 (setq status
(delq (member name status
) status
))
1942 (push (cons name value
) status
))
1943 (message-remove-header "status")
1944 (goto-char (point-max))
1945 (insert "Status: " (nnmail-encode-status status
) "\n"))))
1947 (defun nnmail-decode-status ()
1948 "Return a status-value alist from STATUS."
1949 (goto-char (point-min))
1950 (when (re-search-forward "^Status: " nil t
)
1951 (let (name value status
)
1953 ;; Narrow to the status.
1956 (if (re-search-forward "^[^ \t]" nil t
)
1959 ;; Go through all elements and add them to the list.
1960 (goto-char (point-min))
1961 (while (re-search-forward "[^ \t=]+" nil t
)
1962 (setq name
(match-string 0))
1963 (if (not (eq (char-after) ?
=))
1967 (if (not (eq (char-after) ?
\"))
1968 (if (not (looking-at "[^ \t]"))
1972 (setq value
(match-string 0))
1973 (goto-char (match-end 0)))
1975 (setq value
(read (current-buffer)))))
1976 (push (cons name value
) status
)))
1979 (defun nnmail-encode-status (status)
1980 "Return a status string from STATUS."
1985 (if (string-match "[ \t]" (cdr elem
))
1986 (prin1-to-string (cdr elem
))
1990 (defun nnmail-split-history ()
1991 "Generate an overview of where the last mail split put articles."
1993 (unless nnmail-split-history
1994 (error "No current split history"))
1995 (with-output-to-temp-buffer "*nnmail split history*"
1996 (with-current-buffer standard-output
1997 (fundamental-mode)) ; for Emacs 20.4+
1998 (let ((history nnmail-split-history
)
2000 (while (setq elem
(pop history
))
2001 (princ (mapconcat (lambda (ga)
2002 (concat (car ga
) ":" (int-to-string (cdr ga
))))
2007 (defun nnmail-purge-split-history (group)
2008 "Remove all instances of GROUP from `nnmail-split-history'."
2009 (let ((history nnmail-split-history
))
2011 (setcar history
(gnus-remove-if (lambda (e) (string= (car e
) group
))
2014 (setq nnmail-split-history
(delq nil nnmail-split-history
))))
2016 (defun nnmail-new-mail-p (group)
2017 "Say whether GROUP has new mail."
2018 (let ((his nnmail-split-history
)
2021 (when (assoc group
(pop his
))
2026 (defun nnmail-within-headers-p ()
2027 "Check to see if point is within the headers of a unix mail message.
2028 Doesn't change point."
2029 (let ((pos (point)))
2031 (and (nnmail-search-unix-mail-delim-backward)
2032 (not (search-forward "\n\n" pos t
))))))
2034 (run-hooks 'nnmail-load-hook
)
2038 ;;; arch-tag: fe8f671a-50db-428a-bb5d-f00462f72ed7
2039 ;;; nnmail.el ends here