1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
31 (require 'gnus) ; for macro gnus-kill-buffer, at least
36 (require 'mail-source)
40 (autoload 'gnus-add-buffer "gnus")
41 (autoload 'gnus-kill-buffer "gnus"))
44 "Reading mail with Gnus."
47 (defgroup nnmail-retrieve nil
48 "Retrieving new mail."
51 (defgroup nnmail-prepare nil
52 "Preparing (or mangling) new mail after retrival."
55 (defgroup nnmail-duplicate nil
56 "Handling of duplicate mail messages."
59 (defgroup nnmail-split nil
60 "Organizing the incoming mail in folders."
63 (defgroup nnmail-files nil
68 (defgroup nnmail-expire nil
72 (defgroup nnmail-procmail nil
73 "Interfacing with procmail and other mail agents."
76 (defgroup nnmail-various nil
77 "Various mail options."
80 (defcustom nnmail-split-methods '(("mail.misc" ""))
81 "*Incoming mail will be split according to this variable.
83 If you'd like, for instance, one mail group for mail from the
84 \"4ad-l\" mailing list, one group for junk mail and one for everything
85 else, you could do something like this:
87 (setq nnmail-split-methods
88 '((\"mail.4ad\" \"From:.*4ad\")
89 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
90 (\"mail.misc\" \"\")))
92 As you can see, this variable is a list of lists, where the first
93 element in each \"rule\" is the name of the group (which, by the way,
94 does not have to be called anything beginning with \"mail\",
95 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
96 nnmail will try to match on the header to find a fit.
98 The second element can also be a function. In that case, it will be
99 called narrowed to the headers with the first element of the rule as
100 the argument. It should return a non-nil value if it thinks that the
101 mail belongs in that group.
103 The last element should always have \"\" as the regexp.
105 This variable can also have a function as its value."
107 :type '(choice (repeat :tag "Alist" (group (string :tag "Name")
108 (choice regexp function)))
109 (function-item nnmail-split-fancy)
110 (function :tag "Other")))
112 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
113 (defcustom nnmail-crosspost t
114 "If non-nil, do crossposting if several split methods match the mail.
115 If nil, the first match found will be used."
119 (defcustom nnmail-split-fancy-with-parent-ignore-groups nil
120 "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
121 This can also be a list of regexps."
123 :type '(choice (const :tag "none" nil)
125 (repeat :value (".*") regexp)))
127 (defcustom nnmail-cache-ignore-groups nil
128 "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
129 This can also be a list of regexps."
131 :type '(choice (const :tag "none" nil)
133 (repeat :value (".*") regexp)))
135 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
136 (defcustom nnmail-keep-last-article nil
137 "If non-nil, nnmail will never delete/move a group's last article.
138 It can be marked expirable, so it will be deleted when it is no longer last.
140 You may need to set this variable if other programs are putting
141 new mail into folder numbers that Gnus has marked as expired."
142 :group 'nnmail-procmail
143 :group 'nnmail-various
146 (defcustom nnmail-use-long-file-names nil
147 "If non-nil the mail backends will use long file and directory names.
148 If nil, groups like \"mail.misc\" will end up in directories like
153 (defcustom nnmail-default-file-modes 384
154 "Set the mode bits of all new mail files to this integer."
158 (defcustom nnmail-expiry-wait 7
159 "*Expirable articles that are older than this will be expired.
160 This variable can either be a number (which will be interpreted as a
161 number of days) -- this doesn't have to be an integer. This variable
162 can also be `immediate' and `never'."
163 :group 'nnmail-expire
164 :type '(choice (const immediate)
168 (defcustom nnmail-expiry-wait-function nil
169 "Variable that holds function to specify how old articles should be before they are expired.
170 The function will be called with the name of the group that the expiry
171 is to be performed in, and it should return an integer that says how
172 many days an article can be stored before it is considered \"old\".
173 It can also return the values `never' and `immediate'.
177 \(setq nnmail-expiry-wait-function
179 (cond ((string-match \"private\" newsgroup) 31)
180 ((string-match \"junk\" newsgroup) 1)
181 ((string-match \"important\" newsgroup) 'never)
183 :group 'nnmail-expire
184 :type '(choice (const :tag "nnmail-expiry-wait" nil)
185 (function :format "%v" nnmail-)))
187 (defcustom nnmail-expiry-target 'delete
188 "*Variable that says where expired messages should end up.
189 The default value is `delete' (which says to delete the messages),
190 but it can also be a string or a function. If it is a string, expired
191 messages end up in that group. If it is a function, the function is
192 called in a buffer narrowed to the message in question. The function
193 receives one argument, the name of the group the message comes from.
194 The return value should be `delete' or a group name (a string)."
196 :group 'nnmail-expire
197 :type '(choice (const delete)
198 (function :format "%v" nnmail-)
201 (defcustom nnmail-fancy-expiry-targets nil
202 "Determine expiry target based on articles using fancy techniques.
204 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
205 `nnmail-expiry-target' is set to the function
206 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
207 the message will be expired to a group determined by invoking
208 `format-time-string' with TARGET used as the format string and the
209 time extracted from the articles' Date header (if missing the current
212 In the special cases that HEADER is the symbol `to-from', the regexp
213 will try to match against both the From and the To header.
217 \(setq nnmail-fancy-expiry-targets
218 '((to-from \"boss\" \"nnfolder:Work\")
219 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
220 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
222 In this case, articles containing the string \"boss\" in the To or the
223 From header will be expired to the group \"nnfolder:Work\";
224 articles containing the sting \"IMPORTANT\" in the Subject header will
225 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
226 everything else will be expired to \"nnfolder:Archive-YYYY\"."
227 :group 'nnmail-expire
228 :type '(repeat (list (choice :tag "Match against"
229 (string :tag "Header")
232 (string :tag "Target group format string"))))
234 (defcustom nnmail-cache-accepted-message-ids nil
235 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
236 If non-nil, also update the cache when copy or move articles."
240 (defcustom nnmail-spool-file '((file))
241 "*Where the mail backends will look for incoming mail.
242 This variable is a list of mail source specifiers.
243 This variable is obsolete; `mail-sources' should be used instead."
247 (defcustom nnmail-resplit-incoming nil
248 "*If non-nil, re-split incoming procmail sorted mail."
249 :group 'nnmail-procmail
252 (defcustom nnmail-scan-directory-mail-source-once nil
253 "*If non-nil, scan all incoming procmail sorted mails once.
254 It scans low-level sorted spools even when not required."
256 :group 'nnmail-procmail
259 (defcustom nnmail-delete-file-function 'delete-file
260 "Function called to delete files in some mail backends."
264 (defcustom nnmail-crosspost-link-function
265 (if (string-match "windows-nt\\|emx" (symbol-name system-type))
268 "*Function called to create a copy of a file.
269 This is `add-name-to-file' by default, which means that crossposts
270 will use hard links. If your file system doesn't allow hard
271 links, you could set this variable to `copy-file' instead."
273 :type '(radio (function-item add-name-to-file)
274 (function-item copy-file)
275 (function :tag "Other")))
277 (defcustom nnmail-read-incoming-hook
278 (if (eq system-type 'windows-nt)
279 '(nnheader-ms-strip-cr)
281 "*Hook that will be run after the incoming mail has been transferred.
282 The incoming mail is moved from the specified spool file (which normally is
283 something like \"/usr/spool/mail/$user\") to the user's home
284 directory. This hook is called after the incoming mail box has been
285 emptied, and can be used to call any mail box programs you have
286 running (\"xwatch\", etc.)
290 \(add-hook 'nnmail-read-incoming-hook
292 (call-process \"/local/bin/mailsend\" nil nil nil
293 \"read\" nnmail-spool-file)))
295 If you have xwatch running, this will alert it that mail has been
298 If you use `display-time', you could use something like this:
300 \(add-hook 'nnmail-read-incoming-hook
302 ;; Update the displayed time, since that will clear out
303 ;; the flag that says you have mail.
304 (when (eq (process-status \"display-time\") 'run)
305 (display-time-filter display-time-process \"\"))))"
306 :group 'nnmail-prepare
309 (defcustom nnmail-prepare-incoming-hook nil
310 "Hook called before treating incoming mail.
311 The hook is run in a buffer with all the new, incoming mail."
312 :group 'nnmail-prepare
315 (defcustom nnmail-prepare-incoming-header-hook nil
316 "Hook called narrowed to the headers of each message.
317 This can be used to remove excessive spaces (and stuff like
318 that) from the headers before splitting and saving the messages."
319 :group 'nnmail-prepare
322 (defcustom nnmail-prepare-incoming-message-hook nil
323 "Hook called narrowed to each message."
324 :group 'nnmail-prepare
327 (defcustom nnmail-list-identifiers nil
328 "Regexp that matches list identifiers to be removed.
329 This can also be a list of regexps."
330 :group 'nnmail-prepare
331 :type '(choice (const :tag "none" nil)
333 (repeat :value (".*") regexp)))
335 (defcustom nnmail-pre-get-new-mail-hook nil
336 "Hook called just before starting to handle new incoming mail."
337 :group 'nnmail-retrieve
340 (defcustom nnmail-post-get-new-mail-hook nil
341 "Hook called just after finishing handling new incoming mail."
342 :group 'nnmail-retrieve
345 (defcustom nnmail-split-hook nil
346 "Hook called before deciding where to split an article.
347 The functions in this hook are free to modify the buffer
348 contents in any way they choose -- the buffer contents are
349 discarded after running the split process."
353 (defcustom nnmail-spool-hook nil
354 "*A hook called when a new article is spooled."
358 (defcustom nnmail-large-newsgroup 50
359 "*The number of articles which indicates a large newsgroup or nil.
360 If the number of articles is greater than the value, verbose
361 messages will be shown to indicate the current status."
362 :group 'nnmail-various
363 :type '(choice (const :tag "infinite" nil)
364 (number :tag "count")))
366 (define-widget 'nnmail-lazy 'default
367 "Base widget for recursive datastructures.
369 This is copy of the `lazy' widget in Emacs 21.4 provided for compatibility."
371 :convert-widget 'widget-value-convert-widget
372 :value-create (lambda (widget)
373 (let ((value (widget-get widget :value))
374 (type (widget-get widget :type)))
375 (widget-put widget :children
376 (list (widget-create-child-value
377 widget (widget-convert type) value)))))
378 :value-delete 'widget-children-value-delete
379 :value-get (lambda (widget)
380 (widget-value (car (widget-get widget :children))))
381 :value-inline (lambda (widget)
382 (widget-apply (car (widget-get widget :children))
384 :default-get (lambda (widget)
386 (widget-convert (widget-get widget :type))))
387 :match (lambda (widget value)
388 (widget-apply (widget-convert (widget-get widget :type))
390 :validate (lambda (widget)
391 (widget-apply (car (widget-get widget :children)) :validate)))
393 (define-widget 'nnmail-split-fancy 'nnmail-lazy
394 "Widget for customizing splits in the variable of the same name."
396 :type '(menu-choice :value (any ".*value.*" "misc")
398 (string :tag "Destination")
399 (list :tag "Use first match (|)" :value (|)
401 (editable-list :inline t nnmail-split-fancy))
402 (list :tag "Use all matches (&)" :value (&)
404 (editable-list :inline t nnmail-split-fancy))
405 (list :tag "Function with fixed arguments (:)"
407 (const :format "" :value :)
409 (editable-list :inline t (sexp :tag "Arg"))
411 (list :tag "Function with split arguments (!)" :value (!)
414 (editable-list :inline t nnmail-split-fancy))
415 (list :tag "Field match"
420 (symbol :value mail))
422 (const :tag "Junk (delete mail)" junk)))
424 (defcustom nnmail-split-fancy "mail.misc"
425 "Incoming mail can be split according to this fancy variable.
426 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
428 The format of this variable is SPLIT, where SPLIT can be one of
431 GROUP: Mail will be stored in GROUP (a string).
433 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
434 field FIELD (a regexp) contains VALUE (a regexp), store the messages
435 as specified by SPLIT. If RESTRICT (a regexp) matches some string
436 after FIELD and before the end of the matched VALUE, return nil,
437 otherwise process SPLIT. Multiple RESTRICTs add up, further
438 restricting the possibility of processing SPLIT.
440 \(| SPLIT...): Process each SPLIT expression until one of them matches.
441 A SPLIT expression is said to match if it will cause the mail
442 message to be stored in one or more groups.
444 \(& SPLIT...): Process each SPLIT expression.
446 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
447 the buffer containing the message headers. The return value FUNCTION
448 should be a split, which is then recursively processed.
450 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
451 return value FUNCTION should be a split, which is then recursively
454 junk: Mail will be deleted. Use with care! Do not submerge in water!
456 (setq nnmail-split-fancy
457 '(| (\"Subject\" \"MAKE MONEY FAST\" junk)
458 ...other.rules.omitted...))
460 FIELD must match a complete field name. VALUE must match a complete
461 word according to the `nnmail-split-fancy-syntax-table' syntax table.
462 You can use \".*\" in the regexps to match partial field names or words.
464 FIELD and VALUE can also be lisp symbols, in that case they are expanded
465 as specified in `nnmail-split-abbrev-alist'.
467 GROUP can contain \\& and \\N which will substitute from matching
468 \\(\\) patterns in the previous VALUE.
472 \(setq nnmail-split-methods 'nnmail-split-fancy
474 ;; Messages from the mailer daemon are not crossposted to any of
475 ;; the ordinary groups. Warnings are put in a separate group
477 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
479 ;; Non-error messages are crossposted to all relevant
480 ;; groups, but we don't crosspost between the group for the
481 ;; (ding) list and the group for other (ding) related mail.
482 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
483 (\"subject\" \"ding\" \"ding.misc\"))
484 ;; Other mailing lists...
485 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
486 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
487 ;; Both lists below have the same suffix, so prevent
488 ;; cross-posting to mkpkg.list of messages posted only to
489 ;; the bugs- list, but allow cross-posting when the
490 ;; message was really cross-posted.
491 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
492 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
495 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
496 ;; Unmatched mail goes to the catch all group.
499 :type 'nnmail-split-fancy)
501 (defcustom nnmail-split-abbrev-alist
502 '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
503 (mail . "mailer-daemon\\|postmaster\\|uucp")
504 (to . "to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
505 (from . "from\\|sender\\|resent-from")
506 (nato . "to\\|cc\\|resent-to\\|resent-cc")
507 (naany . "from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
508 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
510 :type '(repeat (cons :format "%v" symbol regexp)))
512 (defcustom nnmail-message-id-cache-length 1000
513 "*The approximate number of Message-IDs nnmail will keep in its cache.
514 If this variable is nil, no checking on duplicate messages will be
516 :group 'nnmail-duplicate
517 :type '(choice (const :tag "disable" nil)
518 (integer :format "%v")))
520 (defcustom nnmail-message-id-cache-file "~/.nnmail-cache"
521 "*The file name of the nnmail Message-ID cache."
522 :group 'nnmail-duplicate
526 (defcustom nnmail-treat-duplicates 'warn
527 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
528 Three values are valid: nil, which means that nnmail is not to keep a
529 Message-ID cache; `warn', which means that nnmail should insert extra
530 headers to warn the user about the duplication (this is the default);
531 and `delete', which means that nnmail will delete duplicated mails.
533 This variable can also be a function. It will be called from a buffer
534 narrowed to the article in question with the Message-ID as a
535 parameter. It should return nil, `warn' or `delete'."
536 :group 'nnmail-duplicate
537 :type '(choice (const :tag "off" nil)
541 (defcustom nnmail-extra-headers '(To Newsgroups)
542 "*Extra headers to parse."
545 :type '(repeat symbol))
547 (defcustom nnmail-split-header-length-limit 512
548 "Header lines longer than this limit are excluded from the split function."
553 (defcustom nnmail-mail-splitting-charset nil
554 "Default charset to be used when splitting incoming mail."
558 (defcustom nnmail-mail-splitting-decodes nil
559 "Whether the nnmail splitting functionality should MIME decode headers."
563 (defcustom nnmail-split-fancy-match-partial-words nil
564 "Whether to match partial words when fancy splitting.
565 Normally, regexes given in `nnmail-split-fancy' are implicitly surrounded
566 by \"\\=\\<...\\>\". If this variable is true, they are not implicitly\
572 ;;; Internal variables.
574 (defvar nnmail-article-buffer " *nnmail incoming*"
575 "The buffer used for splitting incoming mails.")
577 (defvar nnmail-split-history nil
578 "List of group/article elements that say where the previous split put messages.")
580 (defvar nnmail-split-fancy-syntax-table
581 (let ((table (make-syntax-table)))
582 ;; support the %-hack
583 (modify-syntax-entry ?\% "." table)
585 "Syntax table used by `nnmail-split-fancy'.")
587 (defvar nnmail-prepare-save-mail-hook nil
588 "Hook called before saving mail.")
590 (defvar nnmail-split-tracing nil)
591 (defvar nnmail-split-trace nil)
595 (defun nnmail-request-post (&optional server)
596 (mail-send-and-exit nil))
598 (defvar nnmail-file-coding-system 'raw-text
599 "Coding system used in nnmail.")
601 (defvar nnmail-incoming-coding-system
602 mm-text-coding-system
603 "Coding system used in reading inbox")
605 (defvar nnmail-pathname-coding-system nil
606 "*Coding system for file name.")
608 (defun nnmail-find-file (file)
609 "Insert FILE in server buffer safely."
610 (set-buffer nntp-server-buffer)
611 (delete-region (point-min) (point-max))
612 (let ((format-alist nil)
613 (after-insert-file-functions nil))
615 (let ((coding-system-for-read nnmail-file-coding-system)
616 (auto-mode-alist (mm-auto-mode-alist))
617 (file-name-coding-system nnmail-pathname-coding-system))
618 (insert-file-contents file)
622 (defun nnmail-group-pathname (group dir &optional file)
623 "Make file name for GROUP."
625 (let ((dir (file-name-as-directory (expand-file-name dir))))
626 (setq group (nnheader-replace-duplicate-chars-in-string
627 (nnheader-replace-chars-in-string group ?/ ?_)
629 (setq group (nnheader-translate-file-chars group))
630 ;; If this directory exists, we use it directly.
631 (file-name-as-directory
632 (if (or nnmail-use-long-file-names
633 (file-directory-p (concat dir group)))
634 (expand-file-name group dir)
635 ;; If not, we translate dots into slashes.
637 (mm-encode-coding-string
638 (nnheader-replace-chars-in-string group ?. ?/)
639 nnmail-pathname-coding-system)
643 (defun nnmail-get-active ()
644 "Returns an assoc of group names and active ranges.
645 nn*-request-list should have been called before calling this function."
646 ;; Go through all groups from the active list.
648 (set-buffer nntp-server-buffer)
649 (nnmail-parse-active)))
651 (defun nnmail-parse-active ()
652 "Parse the active file in the current buffer and return an alist."
653 (goto-char (point-min))
654 (unless (re-search-forward "[\\\"]" nil t)
655 (goto-char (point-max))
656 (while (re-search-backward "[][';?()#]" nil t)
658 (goto-char (point-min))
659 (let ((buffer (current-buffer))
660 group-assoc group max min)
664 (narrow-to-region (point) (gnus-point-at-eol))
665 (setq group (read buffer))
666 (unless (stringp group)
667 (setq group (symbol-name group)))
668 (if (and (numberp (setq max (read buffer)))
669 (numberp (setq min (read buffer))))
670 (push (list group (cons min max))
677 (defvar nnmail-active-file-coding-system 'raw-text
678 "*Coding system for active file.")
680 (defun nnmail-save-active (group-assoc file-name)
681 "Save GROUP-ASSOC in ACTIVE-FILE."
682 (let ((coding-system-for-write nnmail-active-file-coding-system))
684 (with-temp-file file-name
685 (nnmail-generate-active group-assoc)))))
687 (defun nnmail-generate-active (alist)
688 "Generate an active file from group-alist ALIST."
691 (while (setq group (pop alist))
692 (insert (format "%S %d %d y\n" (intern (car group)) (cdadr group)
694 (goto-char (point-max))
695 (while (search-backward "\\." nil t)
698 (defun nnmail-get-split-group (file source)
699 "Find out whether this FILE is to be split into GROUP only.
700 If SOURCE is a directory spec, try to return the group name component."
701 (if (eq (car source) 'directory)
702 (let ((file (file-name-nondirectory file)))
703 (mail-source-bind (directory source)
704 (if (string-match (concat (regexp-quote suffix) "$") file)
705 (substring file 0 (match-beginning 0))
709 (defun nnmail-process-babyl-mail-format (func artnum-func)
710 (let ((case-fold-search t)
712 start message-id content-length do-search end)
714 (goto-char (point-min))
716 "
\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
717 (goto-char (match-end 0))
718 (delete-region (match-beginning 0) (match-end 0))
722 ;; Skip all the headers in case there are more "From "s...
723 (or (search-forward "\n\n" nil t)
724 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
725 (search-forward "
\1f\f"))
727 ;; Unquote the ">From " line, if any.
728 (goto-char (point-min))
729 (when (looking-at ">From ")
730 (replace-match "X-From-Line: ") )
731 (run-hooks 'nnmail-prepare-incoming-header-hook)
732 (goto-char (point-max))
733 ;; Find the Message-ID header.
735 (if (re-search-backward
736 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t)
737 (setq message-id (buffer-substring (match-beginning 1)
739 ;; There is no Message-ID here, so we create one.
741 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
743 (insert "Original-")))
745 (insert "Message-ID: " (setq message-id (nnmail-message-id))
747 ;; Look for a Content-Length header.
748 (if (not (save-excursion
749 (and (re-search-backward
750 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
751 (setq content-length (string-to-int
755 ;; We destroy the header, since none of
756 ;; the backends ever use it, and we do not
757 ;; want to confuse other mailers by having
758 ;; a (possibly) faulty header.
759 (progn (insert "X-") t))))
762 (if (or (= (+ (point) content-length) (point-max))
764 (goto-char (+ (point) content-length))
767 (goto-char (+ (point) content-length))
768 (setq do-search nil))
771 ;; Go to the beginning of the next article - or to the end
774 (if (re-search-forward "^
\1f" nil t)
775 (goto-char (match-beginning 0))
776 (goto-char (1- (point-max)))))
777 (delete-char 1) ; delete ^_
780 (narrow-to-region start (point))
781 (goto-char (point-min))
782 (nnmail-check-duplication message-id func artnum-func)
784 (setq end (point-max))))
788 (defsubst nnmail-search-unix-mail-delim ()
789 "Put point at the beginning of the next Unix mbox message."
790 ;; Algorithm used to find the next article in the
791 ;; brain-dead Unix mbox format:
793 ;; 1) Search for "^From ".
794 ;; 2) If we find it, then see whether the previous
795 ;; line is blank and the next line looks like a header.
796 ;; Then it's possible that this is a mail delim, and we use it.
797 (let ((case-fold-search nil)
800 (if (not (re-search-forward "^From " nil t))
804 (when (and (or (bobp)
807 (eq (char-after) ?\n)))
810 (while (looking-at ">From \\|From ")
812 (looking-at "[^ \n\t:]+[ \n\t]*:")))
813 (setq found 'yes)))))
817 (defun nnmail-search-unix-mail-delim-backward ()
818 "Put point at the beginning of the current Unix mbox message."
819 ;; Algorithm used to find the next article in the
820 ;; brain-dead Unix mbox format:
822 ;; 1) Search for "^From ".
823 ;; 2) If we find it, then see whether the previous
824 ;; line is blank and the next line looks like a header.
825 ;; Then it's possible that this is a mail delim, and we use it.
826 (let ((case-fold-search nil)
829 (if (not (re-search-backward "^From " nil t))
833 (when (and (or (bobp)
836 (eq (char-after) ?\n)))
839 (while (looking-at ">From \\|From ")
841 (looking-at "[^ \n\t:]+[ \n\t]*:")))
842 (setq found 'yes)))))
846 (defun nnmail-process-unix-mail-format (func artnum-func)
847 (let ((case-fold-search t)
849 start message-id content-length end skip head-end)
850 (goto-char (point-min))
851 (if (not (and (re-search-forward "^From " nil t)
852 (goto-char (match-beginning 0))))
853 ;; Possibly wrong format?
854 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
855 (if (buffer-file-name) "file" "buffer")
856 (or (buffer-file-name) (buffer-name)))
857 ;; Carry on until the bitter end.
861 ;; Find the end of the head.
864 (if (search-forward "\n\n" nil t)
866 ;; This will never happen, but just to be on the safe side --
867 ;; if there is no head-body delimiter, we search a bit manually.
868 (while (and (looking-at "From \\|[^ \t]+:")
872 ;; Find the Message-ID header.
873 (goto-char (point-min))
874 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
875 (setq message-id (match-string 1))
877 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
879 (insert "Original-")))
880 ;; There is no Message-ID here, so we create one.
882 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
883 ;; Look for a Content-Length header.
884 (goto-char (point-min))
885 (if (not (re-search-forward
886 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
887 (setq content-length nil)
888 (setq content-length (string-to-int (match-string 1)))
889 ;; We destroy the header, since none of the backends ever
890 ;; use it, and we do not want to confuse other mailers by
891 ;; having a (possibly) faulty header.
894 (run-hooks 'nnmail-prepare-incoming-header-hook)
895 ;; Find the end of this article.
896 (goto-char (point-max))
898 (setq head-end (point))
899 ;; We try the Content-Length value. The idea: skip over the header
900 ;; separator, then check what happens content-length bytes into the
901 ;; message body. This should be either the end of the buffer, the
902 ;; message separator or a blank line followed by the separator.
903 ;; The blank line should probably be deleted. If neither of the
904 ;; three is met, the content-length header is probably invalid.
907 (setq skip (+ (point) content-length))
909 (cond ((or (= skip (point-max))
910 (= (1+ skip) (point-max)))
911 (setq end (point-max)))
912 ((looking-at "From ")
914 ((looking-at "[ \t]*\n\\(From \\)")
915 (setq end (match-beginning 1)))
919 ;; No Content-Length, so we find the beginning of the next
920 ;; article or the end of the buffer.
922 (or (nnmail-search-unix-mail-delim)
923 (goto-char (point-max))))
924 ;; Allow the backend to save the article.
927 (narrow-to-region start (point))
928 (goto-char (point-min))
930 (nnmail-check-duplication message-id func artnum-func)
931 (setq end (point-max))))
935 (defun nnmail-process-mmdf-mail-format (func artnum-func)
936 (let ((delim "^\^A\^A\^A\^A$")
939 start message-id end)
940 (goto-char (point-min))
941 (if (not (and (re-search-forward delim nil t)
943 ;; Possibly wrong format?
944 (error "Error, unknown mail format! (Possibly corrupted.)")
945 ;; Carry on until the bitter end.
948 ;; Find the end of the head.
951 (if (search-forward "\n\n" nil t)
953 ;; This will never happen, but just to be on the safe side --
954 ;; if there is no head-body delimiter, we search a bit manually.
955 (while (and (looking-at "From \\|[^ \t]+:")
959 ;; Find the Message-ID header.
960 (goto-char (point-min))
961 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
962 (setq message-id (match-string 1))
963 ;; There is no Message-ID here, so we create one.
965 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
967 (insert "Original-")))
969 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
970 (run-hooks 'nnmail-prepare-incoming-header-hook)
971 ;; Find the end of this article.
972 (goto-char (point-max))
974 (if (re-search-forward delim nil t)
976 (goto-char (point-max)))
977 ;; Allow the backend to save the article.
980 (narrow-to-region start (point))
981 (goto-char (point-min))
983 (nnmail-check-duplication message-id func artnum-func)
984 (setq end (point-max))))
989 (defun nnmail-process-maildir-mail-format (func artnum-func)
990 ;; In a maildir, every file contains exactly one mail.
991 (let ((case-fold-search t)
993 (goto-char (point-min))
994 ;; Find the end of the head.
997 (if (search-forward "\n\n" nil t)
999 ;; This will never happen, but just to be on the safe side --
1000 ;; if there is no head-body delimiter, we search a bit manually.
1001 (while (and (looking-at "From \\|[^ \t]+:")
1005 ;; Find the Message-ID header.
1006 (goto-char (point-min))
1007 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
1008 (setq message-id (match-string 1))
1009 ;; There is no Message-ID here, so we create one.
1011 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
1013 (insert "Original-")))
1015 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
1016 (run-hooks 'nnmail-prepare-incoming-header-hook)
1017 ;; Allow the backend to save the article.
1020 (goto-char (point-min))
1021 (nnmail-check-duplication message-id func artnum-func))
1024 (defun nnmail-split-incoming (incoming func &optional exit-func
1026 "Go through the entire INCOMING file and pick out each individual mail.
1027 FUNC will be called with the buffer narrowed to each mail."
1028 (let ( ;; If this is a group-specific split, we bind the split
1029 ;; methods to just this group.
1030 (nnmail-split-methods (if (and group
1031 (not nnmail-resplit-incoming))
1032 (list (list group ""))
1033 nnmail-split-methods)))
1035 ;; Insert the incoming file.
1036 (set-buffer (get-buffer-create nnmail-article-buffer))
1038 (let ((coding-system-for-read nnmail-incoming-coding-system))
1039 (mm-insert-file-contents incoming))
1041 (if (zerop (buffer-size))
1043 (goto-char (point-min))
1044 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
1045 ;; Handle both babyl, MMDF and unix mail formats, since
1046 ;; movemail will use the former when fetching from a
1047 ;; mailbox, the latter when fetching from a file.
1048 (cond ((or (looking-at "\^L")
1049 (looking-at "BABYL OPTIONS:"))
1050 (nnmail-process-babyl-mail-format func artnum-func))
1051 ((looking-at "\^A\^A\^A\^A")
1052 (nnmail-process-mmdf-mail-format func artnum-func))
1053 ((looking-at "Return-Path:")
1054 (nnmail-process-maildir-mail-format func artnum-func))
1056 (nnmail-process-unix-mail-format func artnum-func))))
1058 (funcall exit-func))
1059 (kill-buffer (current-buffer))))))
1061 (defun nnmail-article-group (func &optional trace)
1062 "Look at the headers and return an alist of groups that match.
1063 FUNC will be called with the group name to determine the article number."
1064 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
1065 (obuf (current-buffer))
1066 group-art method grp)
1067 (if (and (sequencep methods)
1068 (= (length methods) 1))
1069 ;; If there is only just one group to put everything in, we
1070 ;; just return a list with just this one method in.
1072 (list (cons (caar methods) (funcall func (caar methods)))))
1073 ;; We do actual comparison.
1075 ;; Copy the article into the work buffer.
1076 (set-buffer nntp-server-buffer)
1078 (insert-buffer-substring obuf)
1079 ;; Narrow to headers.
1081 (goto-char (point-min))
1082 (if (search-forward "\n\n" nil t)
1085 (goto-char (point-min))
1086 ;; Decode MIME headers and charsets.
1087 (when nnmail-mail-splitting-decodes
1088 (let ((mail-parse-charset nnmail-mail-splitting-charset))
1089 (mail-decode-encoded-word-region (point-min) (point-max))))
1090 ;; Fold continuation lines.
1091 (goto-char (point-min))
1092 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1093 (replace-match " " t t))
1094 ;; Nuke pathologically long headers. Since Gnus applies
1095 ;; pathologically complex regexps to the buffer, lines
1096 ;; that are looong will take longer than the Universe's
1097 ;; existence to process.
1098 (goto-char (point-min))
1100 (unless (< (move-to-column nnmail-split-header-length-limit)
1101 nnmail-split-header-length-limit)
1102 (delete-region (point) (gnus-point-at-eol)))
1105 (goto-char (point-min))
1106 (run-hooks 'nnmail-split-hook)
1107 (when (setq nnmail-split-tracing trace)
1108 (setq nnmail-split-trace nil))
1109 (if (and (symbolp nnmail-split-methods)
1110 (fboundp nnmail-split-methods))
1113 ;; `nnmail-split-methods' is a function, so we
1114 ;; just call this function here and use the
1116 (or (funcall nnmail-split-methods)
1120 5 "Error in `nnmail-split-methods'; using `bogus' mail group")
1123 (setq split (gnus-remove-duplicates split))
1124 ;; The article may be "cross-posted" to `junk'. What
1125 ;; to do? Just remove the `junk' spec. Don't really
1126 ;; see anything else to do...
1128 (while (setq elem (car (memq 'junk split)))
1129 (setq split (delq elem split))))
1133 (lambda (group) (cons group (funcall func group)))
1135 ;; Go through the split methods to find a match.
1137 (or nnmail-crosspost
1139 (goto-char (point-max))
1140 (setq method (pop methods)
1143 (not (equal "" (nth 1 method))))
1146 (if (stringp (nth 1 method))
1147 (let ((expand (string-match "\\\\[0-9&]" grp))
1148 (pos (re-search-backward (cadr method)
1151 (setq grp (nnmail-expand-newtext grp)))
1153 ;; Function to say whether this is a match.
1154 (funcall (nth 1 method) grp)))
1155 ;; Don't enter the article into the same
1157 (not (assoc grp group-art)))
1158 (push (cons grp (funcall func grp))
1160 ;; This is the final group, which is used as a
1164 (list (cons (car method)
1165 (funcall func (car method))))))))
1166 ;; Fall back on "bogus" if all else fails.
1168 (setq group-art (list (cons "bogus" (funcall func "bogus"))))))
1169 ;; Produce a trace if non-empty.
1170 (when (and trace nnmail-split-trace)
1171 (let ((restore (current-buffer)))
1172 (nnheader-set-temp-buffer "*Split Trace*")
1174 (dolist (trace (nreverse nnmail-split-trace))
1175 (prin1 trace (current-buffer))
1177 (goto-char (point-min))
1178 (gnus-configure-windows 'split-trace)
1179 (set-buffer restore)))
1181 ;; See whether the split methods returned `junk'.
1182 (if (equal group-art '(junk))
1184 ;; The article may be "cross-posted" to `junk'. What
1185 ;; to do? Just remove the `junk' spec. Don't really
1186 ;; see anything else to do...
1188 (while (setq elem (car (memq 'junk group-art)))
1189 (setq group-art (delq elem group-art)))
1190 (nreverse group-art)))))))
1192 (defun nnmail-insert-lines ()
1193 "Insert how many lines there are in the body of the mail.
1194 Return the number of characters in the body."
1197 (goto-char (point-min))
1198 (unless (search-forward "\n\n" nil t)
1199 (goto-char (point-max))
1201 (setq chars (- (point-max) (point)))
1202 (setq lines (count-lines (point) (point-max)))
1205 (when (re-search-backward "^Lines: " nil t)
1206 (delete-region (point) (progn (forward-line 1) (point)))))
1208 (insert (format "Lines: %d\n" (max lines 0)))
1211 (defun nnmail-insert-xref (group-alist)
1212 "Insert an Xref line based on the (group . article) alist."
1214 (goto-char (point-min))
1215 (unless (search-forward "\n\n" nil t)
1216 (goto-char (point-max))
1219 (when (re-search-backward "^Xref: " nil t)
1220 (delete-region (match-beginning 0)
1221 (progn (forward-line 1) (point))))
1222 (insert (format "Xref: %s" (system-name)))
1224 (insert (format " %s:%d"
1225 (mm-encode-coding-string
1227 nnmail-pathname-coding-system)
1228 (cdar group-alist)))
1229 (setq group-alist (cdr group-alist)))
1232 ;;; Message washing functions
1234 (defun nnmail-remove-leading-whitespace ()
1235 "Remove excessive whitespace from all headers."
1236 (goto-char (point-min))
1237 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1238 (replace-match "\\1" t)))
1240 (defun nnmail-remove-list-identifiers ()
1241 "Remove list identifiers from Subject headers."
1243 (if (consp nnmail-list-identifiers)
1244 (mapconcat 'identity nnmail-list-identifiers " *\\|")
1245 nnmail-list-identifiers)))
1247 (goto-char (point-min))
1248 (while (re-search-forward
1249 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1251 (delete-region (match-beginning 2) (match-end 0))
1252 (beginning-of-line))
1253 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +"
1255 (delete-region (match-beginning 1) (match-end 1))
1256 (beginning-of-line)))))
1258 (defun nnmail-remove-tabs ()
1259 "Translate TAB characters into SPACE characters."
1260 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1262 (defun nnmail-fix-eudora-headers ()
1263 "Eudora has a broken References line, but an OK In-Reply-To."
1264 (goto-char (point-min))
1265 (when (re-search-forward "^X-Mailer:.*Eudora" nil t)
1266 (goto-char (point-min))
1267 (when (re-search-forward "^References:" nil t)
1269 (insert "X-Gnus-Broken-Eudora-"))
1270 (goto-char (point-min))
1271 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1272 (replace-match "\\1" t))))
1274 (custom-add-option 'nnmail-prepare-incoming-header-hook
1275 'nnmail-fix-eudora-headers)
1277 ;;; Utility functions
1279 (defun nnmail-do-request-post (accept-func &optional server)
1280 "Utility function to directly post a message to an nnmail-derived group.
1281 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1282 to actually put the message in the right group."
1284 (dolist (mbx (message-unquote-tokens
1285 (message-tokenize-header
1286 (message-fetch-field "Newsgroups") ", ")) success)
1287 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1288 (or (gnus-active to-newsgroup)
1289 (gnus-activate-group to-newsgroup)
1290 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1292 (or (and (gnus-request-create-group
1293 to-newsgroup gnus-command-method)
1294 (gnus-activate-group to-newsgroup nil nil
1295 gnus-command-method))
1296 (error "Couldn't create group %s" to-newsgroup)))
1297 (error "No such group: %s" to-newsgroup))
1298 (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1299 (setq success nil))))))
1301 (defun nnmail-split-fancy ()
1302 "Fancy splitting method.
1303 See the documentation for the variable `nnmail-split-fancy' for details."
1304 (let ((syntab (syntax-table)))
1307 (set-syntax-table nnmail-split-fancy-syntax-table)
1308 (nnmail-split-it nnmail-split-fancy))
1309 (set-syntax-table syntab))))
1311 (defvar nnmail-split-cache nil)
1312 ;; Alist of split expressions their equivalent regexps.
1314 (defun nnmail-split-it (split)
1315 ;; Return a list of groups matching SPLIT.
1322 ;; A group name. Do the \& and \N subs into the string.
1324 (when nnmail-split-tracing
1325 (push split nnmail-split-trace))
1326 (list (nnmail-expand-newtext split)))
1328 ;; Junk the message.
1330 (when nnmail-split-tracing
1331 (push "junk" nnmail-split-trace))
1334 ;; Builtin & operation.
1335 ((eq (car split) '&)
1336 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1338 ;; Builtin | operation.
1339 ((eq (car split) '|)
1341 (while (and (not done) (cdr split))
1342 (setq split (cdr split)
1343 done (nnmail-split-it (car split))))
1346 ;; Builtin : operation.
1347 ((eq (car split) ':)
1348 (when nnmail-split-tracing
1349 (push split nnmail-split-trace))
1350 (nnmail-split-it (save-excursion (eval (cdr split)))))
1352 ;; Builtin ! operation.
1353 ((eq (car split) '!)
1354 (funcall (cadr split) (nnmail-split-it (caddr split))))
1356 ;; Check the cache for the regexp for this split.
1357 ((setq cached-pair (assq split nnmail-split-cache))
1359 (end-point (point-max))
1360 (value (nth 1 split)))
1362 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1363 (while (and (goto-char end-point)
1364 (re-search-backward (cdr cached-pair) nil t))
1365 (when nnmail-split-tracing
1366 (push split nnmail-split-trace))
1367 (let ((split-rest (cddr split))
1369 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1370 ;; So, start-of-value is the point just before the
1371 ;; beginning of the value, whereas after-header-name
1372 ;; is the point just after the field name.
1373 (start-of-value (match-end 1))
1374 (after-header-name (match-end 2)))
1375 ;; Start the next search just before the beginning of the
1377 (setq end-point (1- start-of-value))
1378 ;; Handle - RESTRICTs
1379 (while (eq (car split-rest) '-)
1380 ;; RESTRICT must start after-header-name and
1381 ;; end after start-of-value, so that, for
1382 ;; (any "foo" - "x-foo" "foo.list")
1383 ;; we do not exclude foo.list just because
1384 ;; the header is: ``To: x-foo, foo''
1386 (if (and (re-search-backward (cadr split-rest)
1387 after-header-name t)
1388 (> (match-end 0) start-of-value))
1389 (setq split-rest nil)
1390 (setq split-rest (cddr split-rest))))
1393 (let ((value (nth 1 split)))
1395 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1396 ;; Someone might want to do a \N sub on this match, so get the
1397 ;; correct match positions.
1398 (re-search-backward value start-of-value))
1399 (dolist (sp (nnmail-split-it (car split-rest)))
1400 (unless (member sp split-result)
1401 (push sp split-result))))))
1404 ;; Not in cache, compute a regexp for the field/value pair.
1406 (let* ((field (nth 0 split))
1407 (value (nth 1 split))
1412 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1413 (if (and (>= (length value) 2)
1414 (string= ".*" (substring value 0 2)))
1415 (setq value (substring value 2)
1417 ;; Same trick for the rear of the regexp
1418 (if (and (>= (length value) 2)
1419 (string= ".*" (substring value -2)))
1420 (setq value (substring value 0 -2)
1422 (when nnmail-split-fancy-match-partial-words
1423 (setq partial-front ""
1425 (setq regexp (concat "^\\(\\("
1427 (cdr (assq field nnmail-split-abbrev-alist))
1430 (or partial-front "\\<")
1434 (or partial-rear "\\>")))
1435 (push (cons split regexp) nnmail-split-cache)
1436 ;; Now that it's in the cache, just call nnmail-split-it again
1437 ;; on the same split, which will find it immediately in the cache.
1438 (nnmail-split-it split))))))
1440 (defun nnmail-expand-newtext (newtext)
1441 (let ((len (length newtext))
1443 c expanded beg N did-expand)
1446 (while (and (< pos len)
1447 (not (= (aref newtext pos) ?\\)))
1448 (setq pos (1+ pos)))
1450 (push (substring newtext beg pos) expanded))
1452 ;; We hit a \; expand it.
1455 c (aref newtext pos))
1456 (if (not (or (= c ?\&)
1459 ;; \ followed by some character we don't expand.
1460 (push (char-to-string c) expanded)
1465 (when (match-beginning N)
1466 (push (buffer-substring (match-beginning N) (match-end N))
1468 (setq pos (1+ pos)))
1470 (apply 'concat (nreverse expanded))
1473 ;; Activate a backend only if it isn't already activated.
1474 ;; If FORCE, re-read the active file even if the backend is
1475 ;; already activated.
1476 (defun nnmail-activate (backend &optional force)
1477 (nnheader-init-server-buffer)
1478 (let (file timestamp file-time)
1479 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1481 (and (setq file (ignore-errors
1482 (symbol-value (intern (format "%s-active-file"
1484 (setq file-time (nth 5 (file-attributes file)))
1488 (symbol-value (intern
1489 (format "%s-active-timestamp"
1492 (not (consp timestamp))
1493 (equal timestamp '(0 0))
1494 (> (nth 0 file-time) (nth 0 timestamp))
1495 (and (= (nth 0 file-time) (nth 0 timestamp))
1496 (> (nth 1 file-time) (nth 1 timestamp))))))
1498 (or (eq timestamp 'none)
1499 (set (intern (format "%s-active-timestamp" backend))
1501 (funcall (intern (format "%s-request-list" backend)))))
1504 (defun nnmail-message-id ()
1505 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1508 ;;; nnmail duplicate handling
1511 (defvar nnmail-cache-buffer nil)
1513 (defun nnmail-cache-open ()
1514 (if (or (not nnmail-treat-duplicates)
1515 (and nnmail-cache-buffer
1516 (buffer-name nnmail-cache-buffer)))
1517 () ; The buffer is open.
1520 (setq nnmail-cache-buffer
1521 (get-buffer-create " *nnmail message-id cache*")))
1523 (when (file-exists-p nnmail-message-id-cache-file)
1524 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1525 (set-buffer-modified-p nil)
1528 (defun nnmail-cache-close ()
1529 (when (and nnmail-cache-buffer
1530 nnmail-treat-duplicates
1531 (buffer-name nnmail-cache-buffer)
1532 (buffer-modified-p nnmail-cache-buffer))
1534 (set-buffer nnmail-cache-buffer)
1535 ;; Weed out the excess number of Message-IDs.
1536 (goto-char (point-max))
1537 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1540 (delete-region (point-min) (point))))
1542 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1543 (make-directory (file-name-directory nnmail-message-id-cache-file)
1545 (nnmail-write-region (point-min) (point-max)
1546 nnmail-message-id-cache-file nil 'silent)
1547 (set-buffer-modified-p nil)
1548 (setq nnmail-cache-buffer nil)
1549 (gnus-kill-buffer (current-buffer)))))
1551 ;; Compiler directives.
1553 (defvar group-art-list)
1555 (defun nnmail-cache-insert (id grp &optional subject sender)
1557 ;; this will handle cases like `B r' where the group is nil
1558 (let ((grp (or grp gnus-newsgroup-name "UNKNOWN")))
1559 (run-hook-with-args 'nnmail-spool-hook
1560 id grp subject sender))
1561 (when nnmail-treat-duplicates
1562 ;; Store some information about the group this message is written
1563 ;; to. This is passed in as the grp argument -- all locations this
1564 ;; has been called from have been checked and the group is available.
1565 ;; The only ambiguous case is nnmail-check-duplication which will only
1566 ;; pass the first (of possibly >1) group which matches. -Josh
1567 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1568 (nnmail-cache-open))
1570 (set-buffer nnmail-cache-buffer)
1571 (goto-char (point-max))
1572 (if (and grp (not (string= "" grp))
1573 (gnus-methods-equal-p gnus-command-method
1574 (nnmail-cache-primary-mail-backend)))
1575 (let ((regexp (if (consp nnmail-cache-ignore-groups)
1576 (mapconcat 'identity nnmail-cache-ignore-groups
1578 nnmail-cache-ignore-groups)))
1579 (unless (and regexp (string-match regexp grp))
1580 (insert id "\t" grp "\n")))
1581 (insert id "\n"))))))
1583 (defun nnmail-cache-primary-mail-backend ()
1584 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1588 (while (and (null res) be-list)
1589 (setq be (car be-list))
1590 (setq be-list (cdr be-list))
1591 (when (and (gnus-method-option-p be 'respool)
1593 (intern (format "%s-get-new-mail" (car be))))
1594 (boundp get-new-mail)
1595 (symbol-value get-new-mail))
1599 ;; Fetch the group name corresponding to the message id stored in the
1601 (defun nnmail-cache-fetch-group (id)
1602 (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1604 (set-buffer nnmail-cache-buffer)
1605 (goto-char (point-max))
1606 (when (search-backward id nil t)
1608 (skip-chars-forward "^\n\r\t")
1609 (unless (looking-at "[\r\n]")
1611 (buffer-substring (point) (gnus-point-at-eol)))))))
1613 ;; Function for nnmail-split-fancy: look up all references in the
1614 ;; cache and if a match is found, return that group.
1615 (defun nnmail-split-fancy-with-parent ()
1616 "Split this message into the same group as its parent.
1617 This function can be used as an entry in `nnmail-split-fancy', for
1618 example like this: (: nnmail-split-fancy-with-parent)
1619 For a message to be split, it looks for the parent message in the
1620 References or In-Reply-To header and then looks in the message id
1621 cache file (given by the variable `nnmail-message-id-cache-file') to
1622 see which group that message was put in. This group is returned.
1624 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1625 (let* ((refstr (or (message-fetch-field "references")
1626 (message-fetch-field "in-reply-to")))
1629 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1631 (lambda (x) (format "\\(%s\\)" x))
1632 nnmail-split-fancy-with-parent-ignore-groups
1634 nnmail-split-fancy-with-parent-ignore-groups)))
1636 (setq references (nreverse (gnus-split-references refstr)))
1637 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1638 (nnmail-cache-open))
1640 (setq res (or (nnmail-cache-fetch-group x) res))
1641 (when (or (member res '("delayed" "drafts" "queue"))
1642 (and regexp res (string-match regexp res)))
1647 (defun nnmail-cache-id-exists-p (id)
1648 (when nnmail-treat-duplicates
1650 (set-buffer nnmail-cache-buffer)
1651 (goto-char (point-max))
1652 (search-backward id nil t))))
1654 (defun nnmail-fetch-field (header)
1657 (message-narrow-to-head)
1658 (message-fetch-field header))))
1660 (defun nnmail-check-duplication (message-id func artnum-func)
1661 (run-hooks 'nnmail-prepare-incoming-message-hook)
1662 ;; If this is a duplicate message, then we do not save it.
1663 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1664 (case-fold-search t)
1665 (action (when duplication
1667 ((memq nnmail-treat-duplicates '(warn delete))
1668 nnmail-treat-duplicates)
1669 ((functionp nnmail-treat-duplicates)
1670 (funcall nnmail-treat-duplicates message-id))
1672 nnmail-treat-duplicates))))
1674 ;; We insert a line that says what the mail source is.
1675 (let ((case-fold-search t))
1676 (goto-char (point-min))
1677 (re-search-forward "^message-id[ \t]*:" nil t)
1679 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1681 ;; Let the backend save the article (or not).
1684 (funcall func (setq group-art
1685 (nreverse (nnmail-article-group artnum-func))))
1686 (nnmail-cache-insert message-id (caar group-art)))
1687 ((eq action 'delete)
1688 (setq group-art nil))
1690 ;; We insert a warning.
1691 (let ((case-fold-search t))
1692 (goto-char (point-min))
1693 (re-search-forward "^message-id[ \t]*:" nil t)
1696 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1697 (funcall func (setq group-art
1698 (nreverse (nnmail-article-group artnum-func))))))
1700 (funcall func (setq group-art
1701 (nreverse (nnmail-article-group artnum-func))))))
1702 ;; Add the group-art list to the history list.
1704 (push group-art nnmail-split-history)
1705 (delete-region (point-min) (point-max)))))
1709 (defvar nnmail-fetched-sources nil)
1711 (defun nnmail-get-value (&rest args)
1712 (let ((sym (intern (apply 'format args))))
1714 (symbol-value sym))))
1716 (defun nnmail-get-new-mail (method exit-func temp
1717 &optional group spool-func)
1718 "Read new incoming mail."
1719 (let* ((sources (or mail-sources
1720 (if (listp nnmail-spool-file) nnmail-spool-file
1721 (list nnmail-spool-file))))
1727 incoming incomings source)
1728 (when (and (nnmail-get-value "%s-get-new-mail" method)
1730 (while (setq source (pop sources))
1731 ;; Be compatible with old values.
1736 ((string-match "^po:" source)
1737 (list 'pop :user (substring source (match-end 0))))
1738 ((file-directory-p source)
1739 (list 'directory :path source))
1741 (list 'file :path source)))))
1742 ((eq source 'procmail)
1743 (message "Invalid value for nnmail-spool-file: `procmail'")
1745 ;; Hack to only fetch the contents of a single group's spool file.
1746 (when (and (eq (car source) 'directory)
1747 (null nnmail-scan-directory-mail-source-once)
1749 (mail-source-bind (directory source)
1750 (setq source (append source
1756 ,(concat group suffix)
1757 (file-name-nondirectory file)))))))))
1758 (when nnmail-fetched-sources
1759 (if (member source nnmail-fetched-sources)
1761 (push source nnmail-fetched-sources)
1762 (push source fetching-sources)))))
1763 (when fetching-sources
1764 ;; We first activate all the groups.
1765 (nnmail-activate method)
1766 ;; Allow the user to hook.
1767 (run-hooks 'nnmail-pre-get-new-mail-hook)
1768 ;; Open the message-id cache.
1770 ;; The we go through all the existing mail source specification
1771 ;; and fetch the mail from each.
1772 (while (setq source (pop fetching-sources))
1773 (nnheader-message 4 "%s: Reading incoming mail from %s..."
1774 method (car source))
1779 `(lambda (file orig-file)
1780 (nnmail-split-incoming
1781 file ',(intern (format "%s-save-mail" method))
1783 (if (equal file orig-file)
1785 (nnmail-get-split-group orig-file ',source))
1786 ',(intern (format "%s-active-number" method)))))))
1789 ;; If we did indeed read any incoming spools, we save all info.
1791 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1792 method (car source))
1794 (nnmail-get-value "%s-group-alist" method)
1795 (nnmail-get-value "%s-active-file" method))
1797 (funcall exit-func))
1798 (run-hooks 'nnmail-read-incoming-hook)
1799 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1801 ;; Close the message-id cache.
1802 (nnmail-cache-close)
1803 ;; Allow the user to hook.
1804 (run-hooks 'nnmail-post-get-new-mail-hook))))
1806 (defun nnmail-expired-article-p (group time force &optional inhibit)
1807 "Say whether an article that is TIME old in GROUP should be expired."
1810 (let ((days (or (and nnmail-expiry-wait-function
1811 (funcall nnmail-expiry-wait-function group))
1812 nnmail-expiry-wait)))
1813 (cond ((or (eq days 'never)
1816 ;; This isn't an expirable group.
1818 ((eq days 'immediate)
1819 ;; We expire all articles on sight.
1821 ((equal time '(0 0))
1822 ;; This is an ange-ftp group, and we don't have any dates.
1825 (setq days (days-to-time days))
1826 ;; Compare the time with the current time.
1827 (ignore-errors (time-less-p days (time-since time))))))))
1829 (defun nnmail-expiry-target-group (target group)
1830 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1831 ;; that buffer if the nnfolder group isn't selected.
1832 (let (nnmail-cache-accepted-message-ids)
1833 ;; Don't enter Message-IDs into cache.
1834 ;; Let users hack it in TARGET function.
1835 (when (functionp target)
1836 (setq target (funcall target group)))
1837 (unless (eq target 'delete)
1838 (when (or (gnus-request-group target)
1839 (gnus-request-create-group target))
1840 (let ((group-art (gnus-request-accept-article target nil nil t)))
1841 (when (consp group-art)
1842 (gnus-group-mark-article-read target (cdr group-art))))))))
1844 (defun nnmail-fancy-expiry-target (group)
1845 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1847 (case-fold-search nil)
1848 (from (or (message-fetch-field "from") ""))
1849 (to (or (message-fetch-field "to") ""))
1851 (or (message-fetch-field "date") (current-time-string))))
1853 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1854 (setq header (car regexp-target-pair))
1856 ;; If the header is to-from then match against the
1857 ;; To or From header
1858 ((and (equal header 'to-from)
1859 (or (string-match (cadr regexp-target-pair) from)
1860 (and (string-match message-dont-reply-to-names from)
1861 (string-match (cadr regexp-target-pair) to))))
1862 (setq target (format-time-string (caddr regexp-target-pair) date)))
1863 ((and (not (equal header 'to-from))
1864 (string-match (cadr regexp-target-pair)
1866 (message-fetch-field header)
1869 (format-time-string (caddr regexp-target-pair) date)))))))
1871 (defun nnmail-check-syntax ()
1872 "Check (and modify) the syntax of the message in the current buffer."
1874 (message-narrow-to-head)
1875 (let ((case-fold-search t))
1876 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1877 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1879 (defun nnmail-write-region (start end filename &optional append visit lockname)
1880 "Do a `write-region', and then set the file modes."
1881 (let ((coding-system-for-write nnmail-file-coding-system)
1882 (file-name-coding-system nnmail-pathname-coding-system))
1883 (write-region start end filename append visit lockname)
1884 (set-file-modes filename nnmail-default-file-modes)))
1887 ;;; Status functions
1890 (defun nnmail-replace-status (name value)
1891 "Make status NAME and VALUE part of the current status line."
1893 (message-narrow-to-head)
1894 (let ((status (nnmail-decode-status)))
1895 (setq status (delq (member name status) status))
1897 (push (cons name value) status))
1898 (message-remove-header "status")
1899 (goto-char (point-max))
1900 (insert "Status: " (nnmail-encode-status status) "\n"))))
1902 (defun nnmail-decode-status ()
1903 "Return a status-value alist from STATUS."
1904 (goto-char (point-min))
1905 (when (re-search-forward "^Status: " nil t)
1906 (let (name value status)
1908 ;; Narrow to the status.
1911 (if (re-search-forward "^[^ \t]" nil t)
1914 ;; Go through all elements and add them to the list.
1915 (goto-char (point-min))
1916 (while (re-search-forward "[^ \t=]+" nil t)
1917 (setq name (match-string 0))
1918 (if (not (eq (char-after) ?=))
1922 (if (not (eq (char-after) ?\"))
1923 (if (not (looking-at "[^ \t]"))
1927 (setq value (match-string 0))
1928 (goto-char (match-end 0)))
1930 (setq value (read (current-buffer)))))
1931 (push (cons name value) status)))
1934 (defun nnmail-encode-status (status)
1935 "Return a status string from STATUS."
1940 (if (string-match "[ \t]" (cdr elem))
1941 (prin1-to-string (cdr elem))
1945 (defun nnmail-split-history ()
1946 "Generate an overview of where the last mail split put articles."
1948 (unless nnmail-split-history
1949 (error "No current split history"))
1950 (with-output-to-temp-buffer "*nnmail split history*"
1951 (with-current-buffer standard-output
1952 (fundamental-mode)) ; for Emacs 20.4+
1953 (let ((history nnmail-split-history)
1955 (while (setq elem (pop history))
1956 (princ (mapconcat (lambda (ga)
1957 (concat (car ga) ":" (int-to-string (cdr ga))))
1962 (defun nnmail-purge-split-history (group)
1963 "Remove all instances of GROUP from `nnmail-split-history'."
1964 (let ((history nnmail-split-history))
1966 (setcar history (gnus-remove-if (lambda (e) (string= (car e) group))
1969 (setq nnmail-split-history (delq nil nnmail-split-history))))
1971 (defun nnmail-new-mail-p (group)
1972 "Say whether GROUP has new mail."
1973 (let ((his nnmail-split-history)
1976 (when (assoc group (pop his))
1981 (defun nnmail-within-headers-p ()
1982 "Check to see if point is within the headers of a unix mail message.
1983 Doesn't change point."
1984 (let ((pos (point)))
1986 (and (nnmail-search-unix-mail-delim-backward)
1987 (not (search-forward "\n\n" pos t))))))
1989 (run-hooks 'nnmail-load-hook)
1993 ;;; nnmail.el ends here