1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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))
35 (require 'mail-source)
38 (autoload 'gnus-error "gnus-util")
39 (autoload 'gnus-buffer-live-p "gnus-util")
40 (autoload 'gnus-add-buffer "gnus"))
43 "Reading mail with Gnus."
46 (defgroup nnmail-retrieve nil
47 "Retrieving new mail."
50 (defgroup nnmail-prepare nil
51 "Preparing (or mangling) new mail after retrival."
54 (defgroup nnmail-duplicate nil
55 "Handling of duplicate mail messages."
58 (defgroup nnmail-split nil
59 "Organizing the incoming mail in folders."
62 (defgroup nnmail-files nil
67 (defgroup nnmail-expire nil
71 (defgroup nnmail-procmail nil
72 "Interfacing with procmail and other mail agents."
75 (defgroup nnmail-various nil
76 "Various mail options."
79 (defcustom nnmail-split-methods '(("mail.misc" ""))
80 "*Incoming mail will be split according to this variable.
82 If you'd like, for instance, one mail group for mail from the
83 \"4ad-l\" mailing list, one group for junk mail and one for everything
84 else, you could do something like this:
86 (setq nnmail-split-methods
87 '((\"mail.4ad\" \"From:.*4ad\")
88 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
89 (\"mail.misc\" \"\")))
91 As you can see, this variable is a list of lists, where the first
92 element in each \"rule\" is the name of the group (which, by the way,
93 does not have to be called anything beginning with \"mail\",
94 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
95 nnmail will try to match on the header to find a fit.
97 The second element can also be a function. In that case, it will be
98 called narrowed to the headers with the first element of the rule as
99 the argument. It should return a non-nil value if it thinks that the
100 mail belongs in that group.
102 The last element should always have \"\" as the regexp.
104 This variable can also have a function as its value."
106 :type '(choice (repeat :tag "Alist" (group (string :tag "Name")
107 (choice regexp function)))
108 (function-item nnmail-split-fancy)
109 (function :tag "Other")))
111 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
112 (defcustom nnmail-crosspost t
113 "If non-nil, do crossposting if several split methods match the mail.
114 If nil, the first match found will be used."
118 (defcustom nnmail-split-fancy-with-parent-ignore-groups nil
119 "Regexp that matches group names to be ignored when applying `nnmail-split-fancy-with-parent'.
120 This can also be a list of regexps."
122 :type '(choice (const :tag "none" nil)
124 (repeat :value (".*") regexp)))
126 (defcustom nnmail-cache-ignore-groups nil
127 "Regexp that matches group names to be ignored when inserting message ids into the cache (`nnmail-cache-insert').
128 This can also be a list of regexps."
130 :type '(choice (const :tag "none" nil)
132 (repeat :value (".*") regexp)))
134 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
135 (defcustom nnmail-keep-last-article nil
136 "If non-nil, nnmail will never delete/move a group's last article.
137 It can be marked expirable, so it will be deleted when it is no longer last.
139 You may need to set this variable if other programs are putting
140 new mail into folder numbers that Gnus has marked as expired."
141 :group 'nnmail-procmail
142 :group 'nnmail-various
145 (defcustom nnmail-use-long-file-names nil
146 "If non-nil the mail backends will use long file and directory names.
147 If nil, groups like \"mail.misc\" will end up in directories like
152 (defcustom nnmail-default-file-modes 384
153 "Set the mode bits of all new mail files to this integer."
157 (defcustom nnmail-expiry-wait 7
158 "*Expirable articles that are older than this will be expired.
159 This variable can either be a number (which will be interpreted as a
160 number of days) -- this doesn't have to be an integer. This variable
161 can also be `immediate' and `never'."
162 :group 'nnmail-expire
163 :type '(choice (const immediate)
167 (defcustom nnmail-expiry-wait-function nil
168 "Variable that holds function to specify how old articles should be before they are expired.
169 The function will be called with the name of the group that the
170 expiry is to be performed in, and it should return an integer that
171 says how many days an article can be stored before it is considered
172 \"old\". It can also return the values `never' and `immediate'.
176 \(setq nnmail-expiry-wait-function
178 (cond ((string-match \"private\" newsgroup) 31)
179 ((string-match \"junk\" newsgroup) 1)
180 ((string-match \"important\" newsgroup) 'never)
182 :group 'nnmail-expire
183 :type '(choice (const :tag "nnmail-expiry-wait" nil)
184 (function :format "%v" nnmail-)))
186 (defcustom nnmail-expiry-target 'delete
187 "*Variable that says where expired messages should end up.
188 The default value is `delete' (which says to delete the messages),
189 but it can also be a string or a function. If it is a string, expired
190 messages end up in that group. If it is a function, the function is
191 called in a buffer narrowed to the message in question. The function
192 receives one argument, the name of the group the message comes from.
193 The return value should be `delete' or a group name (a string)."
195 :group 'nnmail-expire
196 :type '(choice (const delete)
197 (function :format "%v" nnmail-)
200 (defcustom nnmail-fancy-expiry-targets nil
201 "Determine expiry target based on articles using fancy techniques.
203 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
204 `nnmail-expiry-target' is set to the function
205 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
206 the message will be expired to a group determined by invoking
207 `format-time-string' with TARGET used as the format string and the
208 time extracted from the articles' Date header (if missing the current
211 In the special cases that HEADER is the symbol `to-from', the regexp
212 will try to match against both the From and the To header.
216 \(setq nnmail-fancy-expiry-targets
217 '((to-from \"boss\" \"nnfolder:Work\")
218 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
219 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
221 In this case, articles containing the string \"boss\" in the To or the
222 From header will be expired to the group \"nnfolder:Work\";
223 articles containing the sting \"IMPORTANT\" in the Subject header will
224 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
225 everything else will be expired to \"nnfolder:Archive-YYYY\"."
226 :group 'nnmail-expire
227 :type '(repeat (list (choice :tag "Match against"
228 (string :tag "Header")
231 (string :tag "Target group format string"))))
233 (defcustom nnmail-cache-accepted-message-ids nil
234 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
235 If non-nil, also update the cache when copy or move articles."
239 (defcustom nnmail-spool-file '((file))
240 "*Where the mail backends will look for incoming mail.
241 This variable is a list of mail source specifiers.
242 This variable is obsolete; `mail-sources' should be used instead."
246 (defcustom nnmail-resplit-incoming nil
247 "*If non-nil, re-split incoming procmail sorted mail."
248 :group 'nnmail-procmail
251 (defcustom nnmail-scan-directory-mail-source-once nil
252 "*If non-nil, scan all incoming procmail sorted mails once.
253 It scans low-level sorted spools even when not required."
255 :group 'nnmail-procmail
258 (defcustom nnmail-delete-file-function 'delete-file
259 "Function called to delete files in some mail backends."
263 (defcustom nnmail-crosspost-link-function
264 (if (string-match "windows-nt\\|emx" (symbol-name system-type))
267 "*Function called to create a copy of a file.
268 This is `add-name-to-file' by default, which means that crossposts
269 will use hard links. If your file system doesn't allow hard
270 links, you could set this variable to `copy-file' instead."
272 :type '(radio (function-item add-name-to-file)
273 (function-item copy-file)
274 (function :tag "Other")))
276 (defcustom nnmail-read-incoming-hook
277 (if (eq system-type 'windows-nt)
278 '(nnheader-ms-strip-cr)
280 "*Hook that will be run after the incoming mail has been transferred.
281 The incoming mail is moved from the specified spool file (which normally is
282 something like \"/usr/spool/mail/$user\") to the user's home
283 directory. This hook is called after the incoming mail box has been
284 emptied, and can be used to call any mail box programs you have
285 running (\"xwatch\", etc.)
289 \(add-hook 'nnmail-read-incoming-hook
291 (call-process \"/local/bin/mailsend\" nil nil nil
292 \"read\" nnmail-spool-file)))
294 If you have xwatch running, this will alert it that mail has been
297 If you use `display-time', you could use something like this:
299 \(add-hook 'nnmail-read-incoming-hook
301 ;; Update the displayed time, since that will clear out
302 ;; the flag that says you have mail.
303 (when (eq (process-status \"display-time\") 'run)
304 (display-time-filter display-time-process \"\"))))"
305 :group 'nnmail-prepare
308 (defcustom nnmail-prepare-incoming-hook nil
309 "Hook called before treating incoming mail.
310 The hook is run in a buffer with all the new, incoming mail."
311 :group 'nnmail-prepare
314 (defcustom nnmail-prepare-incoming-header-hook nil
315 "Hook called narrowed to the headers of each message.
316 This can be used to remove excessive spaces (and stuff like
317 that) from the headers before splitting and saving the messages."
318 :group 'nnmail-prepare
321 (defcustom nnmail-prepare-incoming-message-hook nil
322 "Hook called narrowed to each message."
323 :group 'nnmail-prepare
326 (defcustom nnmail-list-identifiers nil
327 "Regexp that matches list identifiers to be removed.
328 This can also be a list of regexps."
329 :group 'nnmail-prepare
330 :type '(choice (const :tag "none" nil)
332 (repeat :value (".*") regexp)))
334 (defcustom nnmail-pre-get-new-mail-hook nil
335 "Hook called just before starting to handle new incoming mail."
336 :group 'nnmail-retrieve
339 (defcustom nnmail-post-get-new-mail-hook nil
340 "Hook called just after finishing handling new incoming mail."
341 :group 'nnmail-retrieve
344 (defcustom nnmail-split-hook nil
345 "Hook called before deciding where to split an article.
346 The functions in this hook are free to modify the buffer
347 contents in any way they choose -- the buffer contents are
348 discarded after running the split process."
352 (defcustom nnmail-large-newsgroup 50
353 "*The number of the articles which indicates a large newsgroup or nil.
354 If the number of the articles is greater than the value, verbose
355 messages will be shown to indicate the current status."
356 :group 'nnmail-various
357 :type '(choice (const :tag "infinite" nil)
358 (number :tag "count")))
360 (defcustom nnmail-split-fancy "mail.misc"
361 "Incoming mail can be split according to this fancy variable.
362 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
364 The format of this variable is SPLIT, where SPLIT can be one of
367 GROUP: Mail will be stored in GROUP (a string).
369 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
370 field FIELD (a regexp) contains VALUE (a regexp), store the messages
371 as specified by SPLIT. If RESTRICT (a regexp) matches some string
372 after FIELD and before the end of the matched VALUE, return nil,
373 otherwise process SPLIT. Multiple RESTRICTs add up, further
374 restricting the possibility of processing SPLIT.
376 \(| SPLIT...): Process each SPLIT expression until one of them matches.
377 A SPLIT expression is said to match if it will cause the mail
378 message to be stored in one or more groups.
380 \(& SPLIT...): Process each SPLIT expression.
382 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
383 the buffer containing the message headers. The return value FUNCTION
384 should be a split, which is then recursively processed.
386 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
387 return value FUNCTION should be a split, which is then recursively
390 junk: Mail will be deleted. Use with care! Do not submerge in water!
392 (setq nnmail-split-fancy
393 '(| (\"Subject\" \"MAKE MONEY FAST\" junk)
394 ...other.rules.omitted...))
396 FIELD must match a complete field name. VALUE must match a complete
397 word according to the `nnmail-split-fancy-syntax-table' syntax table.
398 You can use \".*\" in the regexps to match partial field names or words.
400 FIELD and VALUE can also be lisp symbols, in that case they are expanded
401 as specified in `nnmail-split-abbrev-alist'.
403 GROUP can contain \\& and \\N which will substitute from matching
404 \\(\\) patterns in the previous VALUE.
408 \(setq nnmail-split-methods 'nnmail-split-fancy
410 ;; Messages from the mailer daemon are not crossposted to any of
411 ;; the ordinary groups. Warnings are put in a separate group
413 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
415 ;; Non-error messages are crossposted to all relevant
416 ;; groups, but we don't crosspost between the group for the
417 ;; (ding) list and the group for other (ding) related mail.
418 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
419 (\"subject\" \"ding\" \"ding.misc\"))
420 ;; Other mailing lists...
421 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
422 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
423 ;; Both lists below have the same suffix, so prevent
424 ;; cross-posting to mkpkg.list of messages posted only to
425 ;; the bugs- list, but allow cross-posting when the
426 ;; message was really cross-posted.
427 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
428 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
431 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
432 ;; Unmatched mail goes to the catch all group.
438 (defcustom nnmail-split-abbrev-alist
439 '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
440 (mail . "mailer-daemon\\|postmaster\\|uucp")
441 (to . "to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
442 (from . "from\\|sender\\|resent-from")
443 (nato . "to\\|cc\\|resent-to\\|resent-cc")
444 (naany . "from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
445 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
447 :type '(repeat (cons :format "%v" symbol regexp)))
449 (defcustom nnmail-message-id-cache-length 1000
450 "*The approximate number of Message-IDs nnmail will keep in its cache.
451 If this variable is nil, no checking on duplicate messages will be
453 :group 'nnmail-duplicate
454 :type '(choice (const :tag "disable" nil)
455 (integer :format "%v")))
457 (defcustom nnmail-message-id-cache-file "~/.nnmail-cache"
458 "*The file name of the nnmail Message-ID cache."
459 :group 'nnmail-duplicate
463 (defcustom nnmail-treat-duplicates 'warn
464 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
465 Three values are valid: nil, which means that nnmail is not to keep a
466 Message-ID cache; `warn', which means that nnmail should insert extra
467 headers to warn the user about the duplication (this is the default);
468 and `delete', which means that nnmail will delete duplicated mails.
470 This variable can also be a function. It will be called from a buffer
471 narrowed to the article in question with the Message-ID as a
472 parameter. It should return nil, `warn' or `delete'."
473 :group 'nnmail-duplicate
474 :type '(choice (const :tag "off" nil)
478 (defcustom nnmail-extra-headers '(To Newsgroups)
479 "*Extra headers to parse."
482 :type '(repeat symbol))
484 (defcustom nnmail-split-header-length-limit 512
485 "Header lines longer than this limit are excluded from the split function."
490 (defcustom nnmail-mail-splitting-charset nil
491 "Default charset to be used when splitting incoming mail."
495 (defcustom nnmail-mail-splitting-decodes t
496 "Whether the nnmail splitting functionality should MIME decode headers."
500 ;;; Internal variables.
502 (defvar nnmail-article-buffer " *nnmail incoming*"
503 "The buffer used for splitting incoming mails.")
505 (defvar nnmail-split-history nil
506 "List of group/article elements that say where the previous split put messages.")
508 (defvar nnmail-split-fancy-syntax-table nil
509 "Syntax table used by `nnmail-split-fancy'.")
510 (unless (syntax-table-p nnmail-split-fancy-syntax-table)
511 (setq nnmail-split-fancy-syntax-table
512 (copy-syntax-table (standard-syntax-table)))
513 ;; support the %-hack
514 (modify-syntax-entry ?\% "." nnmail-split-fancy-syntax-table))
516 (defvar nnmail-prepare-save-mail-hook nil
517 "Hook called before saving mail.")
519 (defvar nnmail-split-tracing nil)
520 (defvar nnmail-split-trace nil)
524 (defconst nnmail-version "nnmail 1.0"
529 (defun nnmail-request-post (&optional server)
530 (mail-send-and-exit nil))
532 (defvar nnmail-file-coding-system 'raw-text
533 "Coding system used in nnmail.")
535 (defvar nnmail-incoming-coding-system
536 nnheader-text-coding-system
537 "Coding system used in reading inbox")
539 (defvar nnmail-pathname-coding-system nil
540 "*Coding system for pathname.")
542 (defun nnmail-find-file (file)
543 "Insert FILE in server buffer safely."
544 (set-buffer nntp-server-buffer)
545 (delete-region (point-min) (point-max))
546 (let ((format-alist nil)
547 (after-insert-file-functions nil))
549 (let ((auto-mode-alist (nnheader-auto-mode-alist))
550 (file-name-coding-system nnmail-pathname-coding-system)
551 (pathname-coding-system nnmail-pathname-coding-system))
552 (insert-file-contents-as-coding-system
553 nnmail-file-coding-system file)
557 (defun nnmail-group-pathname (group dir &optional file)
558 "Make pathname for GROUP."
560 (let ((dir (file-name-as-directory (expand-file-name dir))))
561 (setq group (nnheader-replace-duplicate-chars-in-string
562 (nnheader-replace-chars-in-string group ?/ ?_)
564 (setq group (nnheader-translate-file-chars group))
565 ;; If this directory exists, we use it directly.
566 (file-name-as-directory
567 (if (or nnmail-use-long-file-names
568 (file-directory-p (concat dir group)))
569 (expand-file-name group dir)
570 ;; If not, we translate dots into slashes.
572 (encode-coding-string
573 (nnheader-replace-chars-in-string group ?. ?/)
574 nnmail-pathname-coding-system)
578 (defun nnmail-get-active ()
579 "Returns an assoc of group names and active ranges.
580 nn*-request-list should have been called before calling this function."
581 ;; Go through all groups from the active list.
583 (set-buffer nntp-server-buffer)
584 (nnmail-parse-active)))
586 (defun nnmail-parse-active ()
587 "Parse the active file in the current buffer and return an alist."
588 (goto-char (point-min))
589 (unless (re-search-forward "[\\\"]" nil t)
590 (goto-char (point-max))
591 (while (re-search-backward "[][';?()#]" nil t)
593 (goto-char (point-min))
594 (let ((buffer (current-buffer))
595 group-assoc group max min)
599 (narrow-to-region (point) (gnus-point-at-eol))
600 (setq group (read buffer))
601 (unless (stringp group)
602 (setq group (symbol-name group)))
603 (if (and (numberp (setq max (read buffer)))
604 (numberp (setq min (read buffer))))
605 (push (list group (cons min max))
612 (defvar nnmail-active-file-coding-system 'raw-text
613 "*Coding system for active file.")
615 (defun nnmail-save-active (group-assoc file-name)
616 "Save GROUP-ASSOC in ACTIVE-FILE."
617 (let ((coding-system-for-write nnmail-active-file-coding-system)
618 (output-coding-system nnmail-active-file-coding-system))
620 (with-temp-file file-name
621 (nnmail-generate-active group-assoc)))))
623 (defun nnmail-generate-active (alist)
624 "Generate an active file from group-alist ALIST."
627 (while (setq group (pop alist))
628 (insert (format "%S %d %d y\n" (intern (car group)) (cdadr group)
630 (goto-char (point-max))
631 (while (search-backward "\\." nil t)
634 (defun nnmail-get-split-group (file source)
635 "Find out whether this FILE is to be split into GROUP only.
636 If SOURCE is a directory spec, try to return the group name component."
637 (if (eq (car source) 'directory)
638 (let ((file (file-name-nondirectory file)))
639 (mail-source-bind (directory source)
640 (if (string-match (concat (regexp-quote suffix) "$") file)
641 (substring file 0 (match-beginning 0))
645 (defun nnmail-process-babyl-mail-format (func artnum-func)
646 (let ((case-fold-search t)
648 start message-id content-length do-search end)
650 (goto-char (point-min))
652 "
\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
653 (goto-char (match-end 0))
654 (delete-region (match-beginning 0) (match-end 0))
658 ;; Skip all the headers in case there are more "From "s...
659 (or (search-forward "\n\n" nil t)
660 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
661 (search-forward "
\1f\f"))
663 ;; Unquote the ">From " line, if any.
664 (goto-char (point-min))
665 (when (looking-at ">From ")
666 (replace-match "X-From-Line: ") )
667 (run-hooks 'nnmail-prepare-incoming-header-hook)
668 (goto-char (point-max))
669 ;; Find the Message-ID header.
671 (if (re-search-backward
672 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t)
673 (setq message-id (buffer-substring (match-beginning 1)
675 ;; There is no Message-ID here, so we create one.
677 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
679 (insert "Original-")))
681 (insert "Message-ID: " (setq message-id (nnmail-message-id))
683 ;; Look for a Content-Length header.
684 (if (not (save-excursion
685 (and (re-search-backward
686 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
687 (setq content-length (string-to-int
691 ;; We destroy the header, since none of
692 ;; the backends ever use it, and we do not
693 ;; want to confuse other mailers by having
694 ;; a (possibly) faulty header.
695 (progn (insert "X-") t))))
698 (if (or (= (+ (point) content-length) (point-max))
700 (goto-char (+ (point) content-length))
703 (goto-char (+ (point) content-length))
704 (setq do-search nil))
707 ;; Go to the beginning of the next article - or to the end
710 (if (re-search-forward "^
\1f" nil t)
711 (goto-char (match-beginning 0))
712 (goto-char (1- (point-max)))))
713 (delete-char 1) ; delete ^_
716 (narrow-to-region start (point))
717 (goto-char (point-min))
718 (nnmail-check-duplication message-id func artnum-func)
720 (setq end (point-max))))
724 (defsubst nnmail-search-unix-mail-delim ()
725 "Put point at the beginning of the next Unix mbox message."
726 ;; Algorithm used to find the next article in the
727 ;; brain-dead Unix mbox format:
729 ;; 1) Search for "^From ".
730 ;; 2) If we find it, then see whether the previous
731 ;; line is blank and the next line looks like a header.
732 ;; Then it's possible that this is a mail delim, and we use it.
733 (let ((case-fold-search nil)
736 (if (not (re-search-forward "^From " nil t))
740 (when (and (or (bobp)
743 (eq (char-after) ?\n)))
746 (while (looking-at ">From \\|From ")
748 (looking-at "[^ \n\t:]+[ \n\t]*:")))
749 (setq found 'yes)))))
753 (defun nnmail-search-unix-mail-delim-backward ()
754 "Put point at the beginning of the current Unix mbox message."
755 ;; Algorithm used to find the next article in the
756 ;; brain-dead Unix mbox format:
758 ;; 1) Search for "^From ".
759 ;; 2) If we find it, then see whether the previous
760 ;; line is blank and the next line looks like a header.
761 ;; Then it's possible that this is a mail delim, and we use it.
762 (let ((case-fold-search nil)
765 (if (not (re-search-backward "^From " nil t))
769 (when (and (or (bobp)
772 (eq (char-after) ?\n)))
775 (while (looking-at ">From \\|From ")
777 (looking-at "[^ \n\t:]+[ \n\t]*:")))
778 (setq found 'yes)))))
782 (defun nnmail-process-unix-mail-format (func artnum-func)
783 (let ((case-fold-search t)
785 start message-id content-length end skip head-end)
786 (goto-char (point-min))
787 (if (not (and (re-search-forward "^From " nil t)
788 (goto-char (match-beginning 0))))
789 ;; Possibly wrong format?
790 (error "Error, unknown mail format! (Possibly corrupted.)")
791 ;; Carry on until the bitter end.
795 ;; Find the end of the head.
798 (if (search-forward "\n\n" nil t)
800 ;; This will never happen, but just to be on the safe side --
801 ;; if there is no head-body delimiter, we search a bit manually.
802 (while (and (looking-at "From \\|[^ \t]+:")
806 ;; Find the Message-ID header.
807 (goto-char (point-min))
808 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
809 (setq message-id (match-string 1))
811 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
813 (insert "Original-")))
814 ;; There is no Message-ID here, so we create one.
816 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
817 ;; Look for a Content-Length header.
818 (goto-char (point-min))
819 (if (not (re-search-forward
820 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
821 (setq content-length nil)
822 (setq content-length (string-to-int (match-string 1)))
823 ;; We destroy the header, since none of the backends ever
824 ;; use it, and we do not want to confuse other mailers by
825 ;; having a (possibly) faulty header.
828 (run-hooks 'nnmail-prepare-incoming-header-hook)
829 ;; Find the end of this article.
830 (goto-char (point-max))
832 (setq head-end (point))
833 ;; We try the Content-Length value. The idea: skip over the header
834 ;; separator, then check what happens content-length bytes into the
835 ;; message body. This should be either the end of the buffer, the
836 ;; message separator or a blank line followed by the separator.
837 ;; The blank line should probably be deleted. If neither of the
838 ;; three is met, the content-length header is probably invalid.
841 (setq skip (+ (point) content-length))
843 (cond ((or (= skip (point-max))
844 (= (1+ skip) (point-max)))
845 (setq end (point-max)))
846 ((looking-at "From ")
848 ((looking-at "[ \t]*\n\\(From \\)")
849 (setq end (match-beginning 1)))
853 ;; No Content-Length, so we find the beginning of the next
854 ;; article or the end of the buffer.
856 (or (nnmail-search-unix-mail-delim)
857 (goto-char (point-max))))
858 ;; Allow the backend to save the article.
861 (narrow-to-region start (point))
862 (goto-char (point-min))
864 (nnmail-check-duplication message-id func artnum-func)
865 (setq end (point-max))))
869 (defun nnmail-process-mmdf-mail-format (func artnum-func)
870 (let ((delim "^\^A\^A\^A\^A$")
873 start message-id end)
874 (goto-char (point-min))
875 (if (not (and (re-search-forward delim nil t)
877 ;; Possibly wrong format?
878 (error "Error, unknown mail format! (Possibly corrupted.)")
879 ;; Carry on until the bitter end.
882 ;; Find the end of the head.
885 (if (search-forward "\n\n" nil t)
887 ;; This will never happen, but just to be on the safe side --
888 ;; if there is no head-body delimiter, we search a bit manually.
889 (while (and (looking-at "From \\|[^ \t]+:")
893 ;; Find the Message-ID header.
894 (goto-char (point-min))
895 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
896 (setq message-id (match-string 1))
897 ;; There is no Message-ID here, so we create one.
899 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
901 (insert "Original-")))
903 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
904 (run-hooks 'nnmail-prepare-incoming-header-hook)
905 ;; Find the end of this article.
906 (goto-char (point-max))
908 (if (re-search-forward delim nil t)
910 (goto-char (point-max)))
911 ;; Allow the backend to save the article.
914 (narrow-to-region start (point))
915 (goto-char (point-min))
917 (nnmail-check-duplication message-id func artnum-func)
918 (setq end (point-max))))
923 (defun nnmail-process-maildir-mail-format (func artnum-func)
924 ;; In a maildir, every file contains exactly one mail.
925 (let ((case-fold-search t)
927 (goto-char (point-min))
928 ;; Find the end of the head.
931 (if (search-forward "\n\n" nil t)
933 ;; This will never happen, but just to be on the safe side --
934 ;; if there is no head-body delimiter, we search a bit manually.
935 (while (and (looking-at "From \\|[^ \t]+:")
939 ;; Find the Message-ID header.
940 (goto-char (point-min))
941 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
942 (setq message-id (match-string 1))
943 ;; There is no Message-ID here, so we create one.
945 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
947 (insert "Original-")))
949 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
950 (run-hooks 'nnmail-prepare-incoming-header-hook)
951 ;; Allow the backend to save the article.
954 (goto-char (point-min))
955 (nnmail-check-duplication message-id func artnum-func))
958 (defun nnmail-split-incoming (incoming func &optional exit-func
960 "Go through the entire INCOMING file and pick out each individual mail.
961 FUNC will be called with the buffer narrowed to each mail."
962 (let (;; If this is a group-specific split, we bind the split
963 ;; methods to just this group.
964 (nnmail-split-methods (if (and group
965 (not nnmail-resplit-incoming))
966 (list (list group ""))
967 nnmail-split-methods)))
969 ;; Insert the incoming file.
970 (set-buffer (get-buffer-create nnmail-article-buffer))
972 (let ((nnheader-file-coding-system nnmail-incoming-coding-system))
973 (nnheader-insert-file-contents incoming))
975 (if (zerop (buffer-size))
977 (goto-char (point-min))
978 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
979 ;; Handle both babyl, MMDF and unix mail formats, since
980 ;; movemail will use the former when fetching from a
981 ;; mailbox, the latter when fetching from a file.
982 (cond ((or (looking-at "\^L")
983 (looking-at "BABYL OPTIONS:"))
984 (nnmail-process-babyl-mail-format func artnum-func))
985 ((looking-at "\^A\^A\^A\^A")
986 (nnmail-process-mmdf-mail-format func artnum-func))
987 ((looking-at "Return-Path:")
988 (nnmail-process-maildir-mail-format func artnum-func))
990 (nnmail-process-unix-mail-format func artnum-func))))
993 (kill-buffer (current-buffer))))))
995 (defun nnmail-article-group (func &optional trace)
996 "Look at the headers and return an alist of groups that match.
997 FUNC will be called with the group name to determine the article number."
998 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
999 (obuf (current-buffer))
1001 end group-art method grp)
1002 (if (and (sequencep methods)
1003 (= (length methods) 1))
1004 ;; If there is only just one group to put everything in, we
1005 ;; just return a list with just this one method in.
1007 (list (cons (caar methods) (funcall func (caar methods)))))
1008 ;; We do actual comparison.
1012 (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
1013 (set-buffer nntp-server-buffer)
1015 ;; Copy the headers into the work buffer.
1016 (insert-buffer-substring obuf beg end)
1017 ;; Decode MIME headers and charsets.
1018 (when nnmail-mail-splitting-decodes
1019 (mime-decode-header-in-region (point-min) (point-max)
1020 nnmail-mail-splitting-charset))
1021 ;; Fold continuation lines.
1022 (goto-char (point-min))
1023 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1024 (replace-match " " t t))
1025 ;; Nuke pathologically long headers. Since Gnus applies
1026 ;; pathologically complex regexps to the buffer, lines
1027 ;; that are looong will take longer than the Universe's
1028 ;; existence to process.
1029 (goto-char (point-min))
1031 (unless (< (move-to-column nnmail-split-header-length-limit)
1032 nnmail-split-header-length-limit)
1033 (delete-region (point) (progn (end-of-line) (point))))
1036 (goto-char (point-min))
1037 (run-hooks 'nnmail-split-hook)
1038 (when (setq nnmail-split-tracing trace)
1039 (setq nnmail-split-trace nil))
1040 (if (and (symbolp nnmail-split-methods)
1041 (fboundp nnmail-split-methods))
1044 ;; `nnmail-split-methods' is a function, so we
1045 ;; just call this function here and use the
1047 (or (funcall nnmail-split-methods)
1051 5 "Error in `nnmail-split-methods'; using `bogus' mail group")
1054 (setq split (gnus-remove-duplicates split))
1055 ;; The article may be "cross-posted" to `junk'. What
1056 ;; to do? Just remove the `junk' spec. Don't really
1057 ;; see anything else to do...
1059 (while (setq elem (car (memq 'junk split)))
1060 (setq split (delq elem split))))
1064 (lambda (group) (cons group (funcall func group)))
1066 ;; Go through the split methods to find a match.
1068 (or nnmail-crosspost
1070 (goto-char (point-max))
1071 (setq method (pop methods)
1074 (not (equal "" (nth 1 method))))
1077 (if (stringp (nth 1 method))
1078 (let ((expand (string-match "\\\\[0-9&]" grp))
1079 (pos (re-search-backward (cadr method)
1082 (setq grp (nnmail-expand-newtext grp)))
1084 ;; Function to say whether this is a match.
1085 (funcall (nth 1 method) grp)))
1086 ;; Don't enter the article into the same
1088 (not (assoc grp group-art)))
1089 (push (cons grp (funcall func grp))
1091 ;; This is the final group, which is used as a
1095 (list (cons (car method)
1096 (funcall func (car method)))))))))
1097 ;; Produce a trace if non-empty.
1098 (when (and trace nnmail-split-trace)
1099 (let ((restore (current-buffer)))
1100 (nnheader-set-temp-buffer "*Split Trace*")
1102 (dolist (trace (nreverse nnmail-split-trace))
1103 (prin1 trace (current-buffer))
1105 (goto-char (point-min))
1106 (gnus-configure-windows 'split-trace)
1107 (set-buffer restore)))
1108 ;; See whether the split methods returned `junk'.
1109 (if (equal group-art '(junk))
1111 ;; The article may be "cross-posted" to `junk'. What
1112 ;; to do? Just remove the `junk' spec. Don't really
1113 ;; see anything else to do...
1115 (while (setq elem (car (memq 'junk group-art)))
1116 (setq group-art (delq elem group-art)))
1117 (nreverse group-art)))))))
1119 (defun nnmail-insert-lines ()
1120 "Insert how many lines there are in the body of the mail.
1121 Return the number of characters in the body."
1124 (goto-char (point-min))
1125 (unless (search-forward "\n\n" nil t)
1126 (goto-char (point-max))
1128 (setq chars (- (point-max) (point)))
1129 (setq lines (count-lines (point) (point-max)))
1132 (when (re-search-backward "^Lines: " nil t)
1133 (delete-region (point) (progn (forward-line 1) (point)))))
1135 (insert (format "Lines: %d\n" (max lines 0)))
1138 (defun nnmail-insert-xref (group-alist)
1139 "Insert an Xref line based on the (group . article) alist."
1141 (goto-char (point-min))
1142 (unless (search-forward "\n\n" nil t)
1143 (goto-char (point-max))
1146 (when (re-search-backward "^Xref: " nil t)
1147 (delete-region (match-beginning 0)
1148 (progn (forward-line 1) (point))))
1149 (insert (format "Xref: %s" (system-name)))
1151 (insert (format " %s:%d"
1152 (encode-coding-string
1154 nnmail-pathname-coding-system)
1155 (cdar group-alist)))
1156 (setq group-alist (cdr group-alist)))
1159 ;;; Message washing functions
1161 (defun nnmail-remove-leading-whitespace ()
1162 "Remove excessive whitespace from all headers."
1163 (goto-char (point-min))
1164 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1165 (replace-match "\\1" t)))
1167 (defun nnmail-remove-list-identifiers ()
1168 "Remove list identifiers from Subject headers."
1170 (if (consp nnmail-list-identifiers)
1171 (mapconcat 'identity nnmail-list-identifiers " *\\|")
1172 nnmail-list-identifiers)))
1174 (goto-char (point-min))
1175 (while (re-search-forward
1176 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1178 (delete-region (match-beginning 2) (match-end 0))
1179 (beginning-of-line))
1180 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
1181 (delete-region (match-beginning 1) (match-end 1))
1182 (beginning-of-line)))))
1184 (defun nnmail-remove-tabs ()
1185 "Translate TAB characters into SPACE characters."
1186 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1188 (defun nnmail-fix-eudora-headers ()
1189 "Eudora has a broken References line, but an OK In-Reply-To."
1190 (goto-char (point-min))
1191 (when (re-search-forward "^X-Mailer:.*Eudora" nil t)
1192 (goto-char (point-min))
1193 (when (re-search-forward "^References:" nil t)
1195 (insert "X-Gnus-Broken-Eudora-"))
1196 (goto-char (point-min))
1197 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1198 (replace-match "\\1" t))))
1200 (custom-add-option 'nnmail-prepare-incoming-header-hook
1201 'nnmail-fix-eudora-headers)
1203 ;;; Utility functions
1205 (defun nnmail-do-request-post (accept-func &optional server)
1206 "Utility function to directly post a message to an nnmail-derived group.
1207 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1208 to actually put the message in the right group."
1210 (dolist (mbx (message-unquote-tokens
1211 (message-tokenize-header
1212 (message-fetch-field "Newsgroups") ", ")) success)
1213 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1214 (or (gnus-active to-newsgroup)
1215 (gnus-activate-group to-newsgroup)
1216 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1218 (or (and (gnus-request-create-group
1219 to-newsgroup gnus-command-method)
1220 (gnus-activate-group to-newsgroup nil nil
1221 gnus-command-method))
1222 (error "Couldn't create group %s" to-newsgroup)))
1223 (error "No such group: %s" to-newsgroup))
1224 (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1225 (setq success nil))))))
1227 (defun nnmail-split-fancy ()
1228 "Fancy splitting method.
1229 See the documentation for the variable `nnmail-split-fancy' for details."
1230 (let ((syntab (syntax-table)))
1233 (set-syntax-table nnmail-split-fancy-syntax-table)
1234 (nnmail-split-it nnmail-split-fancy))
1235 (set-syntax-table syntab))))
1237 (defvar nnmail-split-cache nil)
1238 ;; Alist of split expressions their equivalent regexps.
1240 (defun nnmail-split-it (split)
1241 ;; Return a list of groups matching SPLIT.
1248 ;; A group name. Do the \& and \N subs into the string.
1250 (when nnmail-split-tracing
1251 (push split nnmail-split-trace))
1252 (list (nnmail-expand-newtext split)))
1254 ;; Junk the message.
1256 (when nnmail-split-tracing
1257 (push "junk" nnmail-split-trace))
1260 ;; Builtin & operation.
1261 ((eq (car split) '&)
1262 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1264 ;; Builtin | operation.
1265 ((eq (car split) '|)
1267 (while (and (not done) (cdr split))
1268 (setq split (cdr split)
1269 done (nnmail-split-it (car split))))
1272 ;; Builtin : operation.
1273 ((eq (car split) ':)
1274 (when nnmail-split-tracing
1275 (push split nnmail-split-trace))
1276 (nnmail-split-it (save-excursion (eval (cdr split)))))
1278 ;; Builtin ! operation.
1279 ((eq (car split) '!)
1280 (funcall (cadr split) (nnmail-split-it (caddr split))))
1282 ;; Check the cache for the regexp for this split.
1283 ((setq cached-pair (assq split nnmail-split-cache))
1285 (end-point (point-max))
1286 (value (nth 1 split)))
1288 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1289 (while (and (goto-char end-point)
1290 (re-search-backward (cdr cached-pair) nil t))
1291 (when nnmail-split-tracing
1292 (push split nnmail-split-trace))
1293 (let ((split-rest (cddr split))
1295 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\). So,
1296 ;; start-of-value is the the point just before the
1297 ;; beginning of the value, whereas after-header-name is
1298 ;; the point just after the field name.
1299 (start-of-value (match-end 1))
1300 (after-header-name (match-end 2)))
1301 ;; Start the next search just before the beginning of the
1303 (setq end-point (1- start-of-value))
1304 ;; Handle - RESTRICTs
1305 (while (eq (car split-rest) '-)
1306 ;; RESTRICT must start after-header-name and
1307 ;; end after start-of-value, so that, for
1308 ;; (any "foo" - "x-foo" "foo.list")
1309 ;; we do not exclude foo.list just because
1310 ;; the header is: ``To: x-foo, foo''
1312 (if (and (re-search-backward (cadr split-rest)
1313 after-header-name t)
1314 (> (match-end 0) start-of-value))
1315 (setq split-rest nil)
1316 (setq split-rest (cddr split-rest))))
1319 (let ((value (nth 1 split)))
1321 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1322 ;; Someone might want to do a \N sub on this match, so get the
1323 ;; correct match positions.
1324 (re-search-backward (concat "\\<" value "\\>") start-of-value))
1325 (dolist (sp (nnmail-split-it (car split-rest)))
1326 (unless (memq sp split-result)
1327 (push sp split-result))))))
1330 ;; Not in cache, compute a regexp for the field/value pair.
1332 (let* ((field (nth 0 split))
1333 (value (nth 1 split))
1334 partial-front regexp
1335 partial-rear regexp)
1337 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1338 (if (and (>= (length value) 2)
1339 (string= ".*" (substring value 0 2)))
1340 (setq value (substring value 2)
1342 ;; Same trick for the rear of the regexp
1343 (if (and (>= (length value) 2)
1344 (string= ".*" (substring value -2)))
1345 (setq value (substring value 0 -2)
1347 (setq regexp (concat "^\\(\\("
1349 (cdr (assq field nnmail-split-abbrev-alist))
1352 (or partial-front "\\<")
1356 (or partial-rear "\\>")))
1357 (push (cons split regexp) nnmail-split-cache)
1358 ;; Now that it's in the cache, just call nnmail-split-it again
1359 ;; on the same split, which will find it immediately in the cache.
1360 (nnmail-split-it split))))))
1362 (defun nnmail-expand-newtext (newtext)
1363 (let ((len (length newtext))
1365 c expanded beg N did-expand)
1368 (while (and (< pos len)
1369 (not (= (aref newtext pos) ?\\)))
1370 (setq pos (1+ pos)))
1372 (push (substring newtext beg pos) expanded))
1374 ;; We hit a \; expand it.
1377 c (aref newtext pos))
1378 (if (not (or (= c ?\&)
1381 ;; \ followed by some character we don't expand.
1382 (push (char-to-string c) expanded)
1387 (when (match-beginning N)
1388 (push (buffer-substring (match-beginning N) (match-end N))
1390 (setq pos (1+ pos)))
1392 (apply 'concat (nreverse expanded))
1395 ;; Activate a backend only if it isn't already activated.
1396 ;; If FORCE, re-read the active file even if the backend is
1397 ;; already activated.
1398 (defun nnmail-activate (backend &optional force)
1399 (nnheader-init-server-buffer)
1400 (let (file timestamp file-time)
1401 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1403 (and (setq file (ignore-errors
1404 (symbol-value (intern (format "%s-active-file"
1406 (setq file-time (nth 5 (file-attributes file)))
1410 (symbol-value (intern
1411 (format "%s-active-timestamp"
1414 (not (consp timestamp))
1415 (equal timestamp '(0 0))
1416 (> (nth 0 file-time) (nth 0 timestamp))
1417 (and (= (nth 0 file-time) (nth 0 timestamp))
1418 (> (nth 1 file-time) (nth 1 timestamp))))))
1420 (or (eq timestamp 'none)
1421 (set (intern (format "%s-active-timestamp" backend))
1423 (funcall (intern (format "%s-request-list" backend)))))
1426 (defun nnmail-message-id ()
1427 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1430 ;;; nnmail duplicate handling
1433 (defvar nnmail-cache-buffer nil)
1435 (defun nnmail-cache-open ()
1436 (if (or (not nnmail-treat-duplicates)
1437 (and nnmail-cache-buffer
1438 (buffer-name nnmail-cache-buffer)))
1439 () ; The buffer is open.
1442 (setq nnmail-cache-buffer
1443 (get-buffer-create " *nnmail message-id cache*")))
1445 (when (file-exists-p nnmail-message-id-cache-file)
1446 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1447 (set-buffer-modified-p nil)
1450 (defun nnmail-cache-close ()
1451 (when (and nnmail-cache-buffer
1452 nnmail-treat-duplicates
1453 (buffer-name nnmail-cache-buffer)
1454 (buffer-modified-p nnmail-cache-buffer))
1456 (set-buffer nnmail-cache-buffer)
1457 ;; Weed out the excess number of Message-IDs.
1458 (goto-char (point-max))
1459 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1462 (delete-region (point-min) (point))))
1464 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1465 (make-directory (file-name-directory nnmail-message-id-cache-file)
1467 (nnmail-write-region (point-min) (point-max)
1468 nnmail-message-id-cache-file nil 'silent)
1469 (set-buffer-modified-p nil)
1470 (setq nnmail-cache-buffer nil)
1471 (kill-buffer (current-buffer)))))
1473 ;; Compiler directives.
1475 (defvar group-art-list)
1477 (defun nnmail-cache-insert (id grp)
1478 (when nnmail-treat-duplicates
1479 ;; Store some information about the group this message is written
1480 ;; to. This is passed in as the grp argument -- all locations this
1481 ;; has been called from have been checked and the group is available.
1482 ;; The only ambiguous case is nnmail-check-duplication which will only
1483 ;; pass the first (of possibly >1) group which matches. -Josh
1484 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1485 (nnmail-cache-open))
1487 (set-buffer nnmail-cache-buffer)
1488 (goto-char (point-max))
1489 (if (and grp (not (string= "" grp))
1490 (gnus-methods-equal-p gnus-command-method
1491 (nnmail-cache-primary-mail-backend)))
1492 (let ((regexp (if (consp nnmail-cache-ignore-groups)
1493 (mapconcat 'identity nnmail-cache-ignore-groups
1495 nnmail-cache-ignore-groups)))
1496 (unless (and regexp (string-match regexp grp))
1497 (insert id "\t" grp "\n")))
1498 (insert id "\n")))))
1500 (defun nnmail-cache-primary-mail-backend ()
1501 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1504 (while (and (null res) be-list)
1505 (setq be (car be-list))
1506 (setq be-list (cdr be-list))
1507 (when (and (gnus-method-option-p be 'respool)
1508 (eval (intern (format "%s-get-new-mail" (car be)))))
1512 ;; Fetch the group name corresponding to the message id stored in the
1514 (defun nnmail-cache-fetch-group (id)
1515 (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1517 (set-buffer nnmail-cache-buffer)
1518 (goto-char (point-max))
1519 (when (search-backward id nil t)
1521 (skip-chars-forward "^\n\r\t")
1522 (unless (looking-at "[\r\n]")
1524 (buffer-substring (point)
1525 (progn (end-of-line) (point))))))))
1527 ;; Function for nnmail-split-fancy: look up all references in the
1528 ;; cache and if a match is found, return that group.
1529 (defun nnmail-split-fancy-with-parent ()
1530 "Split this message into the same group as its parent.
1531 This function can be used as an entry in `nnmail-split-fancy', for
1532 example like this: (: nnmail-split-fancy)
1533 For a message to be split, it looks for the parent message in the
1534 References or In-Reply-To header and then looks in the message id
1535 cache file (given by the variable `nnmail-message-id-cache-file') to
1536 see which group that message was put in. This group is returned.
1538 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1539 (let* ((refstr (or (message-fetch-field "references")
1540 (message-fetch-field "in-reply-to")))
1543 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1545 (lambda (x) (format "\\(%s\\)" x))
1546 nnmail-split-fancy-with-parent-ignore-groups
1548 nnmail-split-fancy-with-parent-ignore-groups)))
1550 (setq references (nreverse (gnus-split-references refstr)))
1551 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1552 (nnmail-cache-open))
1554 (setq res (or (nnmail-cache-fetch-group x) res))
1555 (when (or (member res '("delayed" "drafts" "queue"))
1556 (and regexp res (string-match regexp res)))
1561 (defun nnmail-cache-id-exists-p (id)
1562 (when nnmail-treat-duplicates
1564 (set-buffer nnmail-cache-buffer)
1565 (goto-char (point-max))
1566 (search-backward id nil t))))
1568 (defun nnmail-fetch-field (header)
1571 (message-narrow-to-head)
1572 (message-fetch-field header))))
1574 (defun nnmail-check-duplication (message-id func artnum-func)
1575 (run-hooks 'nnmail-prepare-incoming-message-hook)
1576 ;; If this is a duplicate message, then we do not save it.
1577 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1578 (case-fold-search t)
1579 (action (when duplication
1581 ((memq nnmail-treat-duplicates '(warn delete))
1582 nnmail-treat-duplicates)
1583 ((nnheader-functionp nnmail-treat-duplicates)
1584 (funcall nnmail-treat-duplicates message-id))
1586 nnmail-treat-duplicates))))
1588 ;; We insert a line that says what the mail source is.
1589 (let ((case-fold-search t))
1590 (goto-char (point-min))
1591 (re-search-forward "^message-id[ \t]*:" nil t)
1593 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1595 ;; Let the backend save the article (or not).
1598 (funcall func (setq group-art
1599 (nreverse (nnmail-article-group artnum-func))))
1600 (nnmail-cache-insert message-id (caar group-art)))
1601 ((eq action 'delete)
1602 (setq group-art nil))
1604 ;; We insert a warning.
1605 (let ((case-fold-search t))
1606 (goto-char (point-min))
1607 (re-search-forward "^message-id[ \t]*:" nil t)
1610 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1611 (funcall func (setq group-art
1612 (nreverse (nnmail-article-group artnum-func))))))
1614 (funcall func (setq group-art
1615 (nreverse (nnmail-article-group artnum-func))))))
1616 ;; Add the group-art list to the history list.
1618 (push group-art nnmail-split-history)
1619 (delete-region (point-min) (point-max)))))
1623 (defvar nnmail-fetched-sources nil)
1625 (defun nnmail-get-value (&rest args)
1626 (let ((sym (intern (apply 'format args))))
1628 (symbol-value sym))))
1630 (defun nnmail-get-new-mail (method exit-func temp
1631 &optional group spool-func)
1632 "Read new incoming mail."
1633 (let* ((sources (or mail-sources
1634 (if (listp nnmail-spool-file) nnmail-spool-file
1635 (list nnmail-spool-file))))
1641 incoming incomings source)
1642 (when (and (nnmail-get-value "%s-get-new-mail" method)
1644 (while (setq source (pop sources))
1645 ;; Be compatible with old values.
1650 ((string-match "^po:" source)
1651 (list 'pop :user (substring source (match-end 0))))
1652 ((file-directory-p source)
1653 (list 'directory :path source))
1655 (list 'file :path source)))))
1656 ((eq source 'procmail)
1657 (message "Invalid value for nnmail-spool-file: `procmail'")
1659 ;; Hack to only fetch the contents of a single group's spool file.
1660 (when (and (eq (car source) 'directory)
1661 (null nnmail-scan-directory-mail-source-once)
1663 (mail-source-bind (directory source)
1664 (setq source (append source
1670 ,(concat group suffix)
1671 (file-name-nondirectory file)))))))))
1672 (when nnmail-fetched-sources
1673 (if (member source nnmail-fetched-sources)
1675 (push source nnmail-fetched-sources)
1676 (push source fetching-sources)))))
1677 (when fetching-sources
1678 ;; We first activate all the groups.
1679 (nnmail-activate method)
1680 ;; Allow the user to hook.
1681 (run-hooks 'nnmail-pre-get-new-mail-hook)
1682 ;; Open the message-id cache.
1684 ;; The we go through all the existing mail source specification
1685 ;; and fetch the mail from each.
1686 (while (setq source (pop fetching-sources))
1687 (nnheader-message 4 "%s: Reading incoming mail from %s..."
1688 method (car source))
1693 `(lambda (file orig-file)
1694 (nnmail-split-incoming
1695 file ',(intern (format "%s-save-mail" method))
1697 (if (equal file orig-file)
1699 (nnmail-get-split-group orig-file ',source))
1700 ',(intern (format "%s-active-number" method)))))))
1703 ;; If we did indeed read any incoming spools, we save all info.
1705 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1706 method (car source))
1708 (nnmail-get-value "%s-group-alist" method)
1709 (nnmail-get-value "%s-active-file" method))
1711 (funcall exit-func))
1712 (run-hooks 'nnmail-read-incoming-hook)
1713 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1715 ;; Close the message-id cache.
1716 (nnmail-cache-close)
1717 ;; Allow the user to hook.
1718 (run-hooks 'nnmail-post-get-new-mail-hook))))
1720 (defun nnmail-expired-article-p (group time force &optional inhibit)
1721 "Say whether an article that is TIME old in GROUP should be expired."
1724 (let ((days (or (and nnmail-expiry-wait-function
1725 (funcall nnmail-expiry-wait-function group))
1726 nnmail-expiry-wait)))
1727 (cond ((or (eq days 'never)
1730 ;; This isn't an expirable group.
1732 ((eq days 'immediate)
1733 ;; We expire all articles on sight.
1735 ((equal time '(0 0))
1736 ;; This is an ange-ftp group, and we don't have any dates.
1739 (setq days (days-to-time days))
1740 ;; Compare the time with the current time.
1741 (ignore-errors (time-less-p days (time-since time))))))))
1743 (defun nnmail-expiry-target-group (target group)
1744 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1745 ;; that buffer if the nnfolder group isn't selected.
1746 (let (nnmail-cache-accepted-message-ids)
1747 ;; Don't enter Message-IDs into cache.
1748 ;; Let users hack it in TARGET function.
1749 (when (nnheader-functionp target)
1750 (setq target (funcall target group)))
1751 (unless (eq target 'delete)
1752 (let ((group-art (gnus-request-accept-article target nil nil t)))
1753 (when (consp group-art)
1754 (gnus-group-mark-article-read target (cdr group-art)))))))
1756 (defun nnmail-fancy-expiry-target (group)
1757 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1759 (case-fold-search nil)
1760 (from (or (message-fetch-field "from") ""))
1761 (to (or (message-fetch-field "to") ""))
1763 (or (message-fetch-field "date") (current-time-string))))
1765 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1766 (setq header (car regexp-target-pair))
1768 ;; If the header is to-from then match against the
1769 ;; To or From header
1770 ((and (equal header 'to-from)
1771 (or (string-match (cadr regexp-target-pair) from)
1772 (and (string-match message-dont-reply-to-names from)
1773 (string-match (cadr regexp-target-pair) to))))
1774 (setq target (format-time-string (caddr regexp-target-pair) date)))
1775 ((and (not (equal header 'to-from))
1776 (string-match (cadr regexp-target-pair)
1778 (message-fetch-field header)
1781 (format-time-string (caddr regexp-target-pair) date)))))))
1783 (defun nnmail-check-syntax ()
1784 "Check (and modify) the syntax of the message in the current buffer."
1786 (message-narrow-to-head)
1787 (let ((case-fold-search t))
1788 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1789 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1791 (defun nnmail-write-region (start end filename &optional append visit lockname)
1792 "Do a `write-region', and then set the file modes."
1793 (let ((file-name-coding-system nnmail-pathname-coding-system)
1794 (pathname-coding-system nnmail-pathname-coding-system))
1795 (write-region-as-coding-system
1796 nnmail-file-coding-system start end filename append visit lockname)
1797 (set-file-modes filename nnmail-default-file-modes)))
1800 ;;; Status functions
1803 (defun nnmail-replace-status (name value)
1804 "Make status NAME and VALUE part of the current status line."
1806 (message-narrow-to-head)
1807 (let ((status (nnmail-decode-status)))
1808 (setq status (delq (member name status) status))
1810 (push (cons name value) status))
1811 (message-remove-header "status")
1812 (goto-char (point-max))
1813 (insert "Status: " (nnmail-encode-status status) "\n"))))
1815 (defun nnmail-decode-status ()
1816 "Return a status-value alist from STATUS."
1817 (goto-char (point-min))
1818 (when (re-search-forward "^Status: " nil t)
1819 (let (name value status)
1821 ;; Narrow to the status.
1824 (if (re-search-forward "^[^ \t]" nil t)
1827 ;; Go through all elements and add them to the list.
1828 (goto-char (point-min))
1829 (while (re-search-forward "[^ \t=]+" nil t)
1830 (setq name (match-string 0))
1831 (if (not (eq (char-after) ?=))
1835 (if (not (eq (char-after) ?\"))
1836 (if (not (looking-at "[^ \t]"))
1840 (setq value (match-string 0))
1841 (goto-char (match-end 0)))
1843 (setq value (read (current-buffer)))))
1844 (push (cons name value) status)))
1847 (defun nnmail-encode-status (status)
1848 "Return a status string from STATUS."
1853 (if (string-match "[ \t]" (cdr elem))
1854 (prin1-to-string (cdr elem))
1858 (defun nnmail-split-history ()
1859 "Generate an overview of where the last mail split put articles."
1861 (unless nnmail-split-history
1862 (error "No current split history"))
1863 (with-output-to-temp-buffer "*nnmail split history*"
1864 (with-current-buffer standard-output
1865 (fundamental-mode)) ; for Emacs 20.4+
1866 (let ((history nnmail-split-history)
1868 (while (setq elem (pop history))
1869 (princ (mapconcat (lambda (ga)
1870 (concat (car ga) ":" (int-to-string (cdr ga))))
1875 (defun nnmail-purge-split-history (group)
1876 "Remove all instances of GROUP from `nnmail-split-history'."
1877 (let ((history nnmail-split-history))
1879 (setcar history (gnus-delete-if (lambda (e) (string= (car e) group))
1882 (setq nnmail-split-history (delq nil nnmail-split-history))))
1884 (defun nnmail-new-mail-p (group)
1885 "Say whether GROUP has new mail."
1886 (let ((his nnmail-split-history)
1889 (when (assoc group (pop his))
1894 (defun nnmail-new-mail-numbers (group)
1895 "Say how many articles has been incorporated to GROUP."
1896 (let ((his (apply 'append nnmail-split-history))
1899 (when (string= group (caar his))
1900 (push (cdar his) numbers))
1901 (setq his (cdr his)))
1904 (defun nnmail-within-headers-p ()
1905 "Check to see if point is within the headers of a unix mail message.
1906 Doesn't change point."
1907 (let ((pos (point)))
1909 (and (nnmail-search-unix-mail-delim-backward)
1910 (not (search-forward "\n\n" pos t))))))
1912 (run-hooks 'nnmail-load-hook)
1916 ;;; nnmail.el ends here