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)
39 (autoload 'gnus-error "gnus-util")
40 (autoload 'gnus-buffer-live-p "gnus-util")
41 (autoload 'gnus-add-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 incomming 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") regexp))
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
120 `nnmail-split-fancy-with-parent'. This can also be a list of regexps."
122 :type '(choice (const :tag "none" nil)
124 (repeat :value (".*") regexp)))
126 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
127 (defcustom nnmail-keep-last-article nil
128 "If non-nil, nnmail will never delete/move a group's last article.
129 It can be marked expirable, so it will be deleted when it is no longer last.
131 You may need to set this variable if other programs are putting
132 new mail into folder numbers that Gnus has marked as expired."
133 :group 'nnmail-procmail
134 :group 'nnmail-various
137 (defcustom nnmail-use-long-file-names nil
138 "If non-nil the mail backends will use long file and directory names.
139 If nil, groups like \"mail.misc\" will end up in directories like
144 (defcustom nnmail-default-file-modes 384
145 "Set the mode bits of all new mail files to this integer."
149 (defcustom nnmail-expiry-wait 7
150 "*Expirable articles that are older than this will be expired.
151 This variable can either be a number (which will be interpreted as a
152 number of days) -- this doesn't have to be an integer. This variable
153 can also be `immediate' and `never'."
154 :group 'nnmail-expire
155 :type '(choice (const immediate)
159 (defcustom nnmail-expiry-wait-function nil
160 "Variable that holds function to specify how old articles should be before they are expired.
161 The function will be called with the name of the group that the
162 expiry is to be performed in, and it should return an integer that
163 says how many days an article can be stored before it is considered
164 \"old\". It can also return the values `never' and `immediate'.
168 \(setq nnmail-expiry-wait-function
170 (cond ((string-match \"private\" newsgroup) 31)
171 ((string-match \"junk\" newsgroup) 1)
172 ((string-match \"important\" newsgroup) 'never)
174 :group 'nnmail-expire
175 :type '(choice (const :tag "nnmail-expiry-wait" nil)
176 (function :format "%v" nnmail-)))
178 (defcustom nnmail-expiry-target 'delete
179 "*Variable that says where expired messages should end up.
180 The default value is `delete' (which says to delete the messages),
181 but it can also be a string or a function. If it is a string, expired
182 messages end up in that group. If it is a function, the function is
183 called in a buffer narrowed to the message in question. The function
184 receives one argument, the name of the group the message comes from.
185 The return value should be `delete' or a group name (a string)."
187 :group 'nnmail-expire
188 :type '(choice (const delete)
189 (function :format "%v" nnmail-)
192 (defcustom nnmail-fancy-expiry-targets nil
193 "Determine expiry target based on articles using fancy techniques.
195 This is a list of (\"HEADER\" \"REGEXP\" \"TARGET\") entries. If
196 `nnmail-expiry-target' is set to the function
197 `nnmail-fancy-expiry-target' and HEADER of the article matches REGEXP,
198 the message will be expired to a group determined by invoking
199 `format-time-string' with TARGET used as the format string and the
200 time extracted from the articles' Date header (if missing the current
203 In the special cases that HEADER is the symbol `to-from', the regexp
204 will try to match against both the From and the To header.
208 \(setq nnmail-fancy-expiry-targets
209 '((to-from \"boss\" \"nnfolder:Work\")
210 (\"Subject\" \"IMPORTANT\" \"nnfolder:IMPORTANT.%Y.%b\")
211 (\"from\" \".*\" \"nnfolder:Archive-%Y\")))
213 In this case, articles containing the string \"boss\" in the To or the
214 From header will be expired to the group \"nnfolder:Work\";
215 articles containing the sting \"IMPORTANT\" in the Subject header will
216 be expired to the group \"nnfolder:IMPORTANT.YYYY.MMM\"; and
217 everything else will be expired to \"nnfolder:Archive-YYYY\"."
218 :group 'nnmail-expire
219 :type '(repeat (list (choice :tag "Match against"
220 (string :tag "Header")
223 (string :tag "Target group format string"))))
225 (defcustom nnmail-cache-accepted-message-ids nil
226 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache.
227 If non-nil, also update the cache when copy or move articles."
231 (defcustom nnmail-spool-file '((file))
232 "*Where the mail backends will look for incoming mail.
233 This variable is a list of mail source specifiers.
234 This variable is obsolete; `mail-sources' should be used instead."
238 (defcustom nnmail-resplit-incoming nil
239 "*If non-nil, re-split incoming procmail sorted mail."
240 :group 'nnmail-procmail
243 (defcustom nnmail-scan-directory-mail-source-once nil
244 "*If non-nil, scan all incoming procmail sorted mails once.
245 It scans low-level sorted spools even when not required."
247 :group 'nnmail-procmail
250 (defcustom nnmail-delete-file-function 'delete-file
251 "Function called to delete files in some mail backends."
255 (defcustom nnmail-crosspost-link-function
256 (if (string-match "windows-nt\\|emx" (symbol-name system-type))
259 "*Function called to create a copy of a file.
260 This is `add-name-to-file' by default, which means that crossposts
261 will use hard links. If your file system doesn't allow hard
262 links, you could set this variable to `copy-file' instead."
264 :type '(radio (function-item add-name-to-file)
265 (function-item copy-file)
266 (function :tag "Other")))
268 (defcustom nnmail-read-incoming-hook
269 (if (eq system-type 'windows-nt)
270 '(nnheader-ms-strip-cr)
272 "*Hook that will be run after the incoming mail has been transferred.
273 The incoming mail is moved from the specified spool file (which normally is
274 something like \"/usr/spool/mail/$user\") to the user's home
275 directory. This hook is called after the incoming mail box has been
276 emptied, and can be used to call any mail box programs you have
277 running (\"xwatch\", etc.)
281 \(add-hook 'nnmail-read-incoming-hook
283 (call-process \"/local/bin/mailsend\" nil nil nil
284 \"read\" nnmail-spool-file)))
286 If you have xwatch running, this will alert it that mail has been
289 If you use `display-time', you could use something like this:
291 \(add-hook 'nnmail-read-incoming-hook
293 ;; Update the displayed time, since that will clear out
294 ;; the flag that says you have mail.
295 (when (eq (process-status \"display-time\") 'run)
296 (display-time-filter display-time-process \"\"))))"
297 :group 'nnmail-prepare
300 (defcustom nnmail-prepare-incoming-hook nil
301 "Hook called before treating incoming mail.
302 The hook is run in a buffer with all the new, incoming mail."
303 :group 'nnmail-prepare
306 (defcustom nnmail-prepare-incoming-header-hook nil
307 "Hook called narrowed to the headers of each message.
308 This can be used to remove excessive spaces (and stuff like
309 that) from the headers before splitting and saving the messages."
310 :group 'nnmail-prepare
313 (defcustom nnmail-prepare-incoming-message-hook nil
314 "Hook called narrowed to each message."
315 :group 'nnmail-prepare
318 (defcustom nnmail-list-identifiers nil
319 "Regexp that matches list identifiers to be removed.
320 This can also be a list of regexps."
321 :group 'nnmail-prepare
322 :type '(choice (const :tag "none" nil)
324 (repeat :value (".*") regexp)))
326 (defcustom nnmail-pre-get-new-mail-hook nil
327 "Hook called just before starting to handle new incoming mail."
328 :group 'nnmail-retrieve
331 (defcustom nnmail-post-get-new-mail-hook nil
332 "Hook called just after finishing handling new incoming mail."
333 :group 'nnmail-retrieve
336 (defcustom nnmail-split-hook nil
337 "Hook called before deciding where to split an article.
338 The functions in this hook are free to modify the buffer
339 contents in any way they choose -- the buffer contents are
340 discarded after running the split process."
344 (defcustom nnmail-large-newsgroup 50
345 "*The number of the articles which indicates a large newsgroup.
346 If the number of the articles is greater than the value, verbose
347 messages will be shown to indicate the current status."
348 :group 'nnmail-various
351 (defcustom nnmail-split-fancy "mail.misc"
352 "Incoming mail can be split according to this fancy variable.
353 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
355 The format of this variable is SPLIT, where SPLIT can be one of
358 GROUP: Mail will be stored in GROUP (a string).
360 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
361 field FIELD (a regexp) contains VALUE (a regexp), store the messages
362 as specified by SPLIT. If RESTRICT (a regexp) matches some string
363 after FIELD and before the end of the matched VALUE, return nil,
364 otherwise process SPLIT. Multiple RESTRICTs add up, further
365 restricting the possibility of processing SPLIT.
367 \(| SPLIT...): Process each SPLIT expression until one of them matches.
368 A SPLIT expression is said to match if it will cause the mail
369 message to be stored in one or more groups.
371 \(& SPLIT...): Process each SPLIT expression.
373 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
374 the buffer containing the message headers. The return value FUNCTION
375 should be a split, which is then recursively processed.
377 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
378 return value FUNCTION should be a split, which is then recursively
381 junk: Mail will be deleted. Use with care! Do not submerge in water!
383 (setq nnmail-split-fancy
384 '(| (\"Subject\" \"MAKE MONEY FAST\" junk)
385 ...other.rules.omitted...))
387 FIELD must match a complete field name. VALUE must match a complete
388 word according to the `nnmail-split-fancy-syntax-table' syntax table.
389 You can use \".*\" in the regexps to match partial field names or words.
391 FIELD and VALUE can also be lisp symbols, in that case they are expanded
392 as specified in `nnmail-split-abbrev-alist'.
394 GROUP can contain \\& and \\N which will substitute from matching
395 \\(\\) patterns in the previous VALUE.
399 \(setq nnmail-split-methods 'nnmail-split-fancy
401 ;; Messages from the mailer daemon are not crossposted to any of
402 ;; the ordinary groups. Warnings are put in a separate group
404 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
406 ;; Non-error messages are crossposted to all relevant
407 ;; groups, but we don't crosspost between the group for the
408 ;; (ding) list and the group for other (ding) related mail.
409 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
410 (\"subject\" \"ding\" \"ding.misc\"))
411 ;; Other mailing lists...
412 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
413 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
414 ;; Both lists below have the same suffix, so prevent
415 ;; cross-posting to mkpkg.list of messages posted only to
416 ;; the bugs- list, but allow cross-posting when the
417 ;; message was really cross-posted.
418 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
419 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
422 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
423 ;; Unmatched mail goes to the catch all group.
429 (defcustom nnmail-split-abbrev-alist
430 '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
431 (mail . "mailer-daemon\\|postmaster\\|uucp")
432 (to . "to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
433 (from . "from\\|sender\\|resent-from")
434 (nato . "to\\|cc\\|resent-to\\|resent-cc")
435 (naany . "from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
436 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
438 :type '(repeat (cons :format "%v" symbol regexp)))
440 (defcustom nnmail-message-id-cache-length 1000
441 "*The approximate number of Message-IDs nnmail will keep in its cache.
442 If this variable is nil, no checking on duplicate messages will be
444 :group 'nnmail-duplicate
445 :type '(choice (const :tag "disable" nil)
446 (integer :format "%v")))
448 (defcustom nnmail-message-id-cache-file "~/.nnmail-cache"
449 "*The file name of the nnmail Message-ID cache."
450 :group 'nnmail-duplicate
454 (defcustom nnmail-treat-duplicates 'warn
455 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
456 Three values are valid: nil, which means that nnmail is not to keep a
457 Message-ID cache; `warn', which means that nnmail should insert extra
458 headers to warn the user about the duplication (this is the default);
459 and `delete', which means that nnmail will delete duplicated mails.
461 This variable can also be a function. It will be called from a buffer
462 narrowed to the article in question with the Message-ID as a
463 parameter. It should return nil, `warn' or `delete'."
464 :group 'nnmail-duplicate
465 :type '(choice (const :tag "off" nil)
469 (defcustom nnmail-extra-headers '(To Newsgroups)
470 "*Extra headers to parse."
473 :type '(repeat symbol))
475 (defcustom nnmail-split-header-length-limit 512
476 "Header lines longer than this limit are excluded from the split function."
481 (defcustom nnmail-mail-splitting-charset nil
482 "Default charset to be used when splitting incoming mail."
486 ;;; Internal variables.
488 (defvar nnmail-article-buffer " *nnmail incoming*"
489 "The buffer used for splitting incoming mails.")
491 (defvar nnmail-split-history nil
492 "List of group/article elements that say where the previous split put messages.")
494 (defvar nnmail-split-fancy-syntax-table nil
495 "Syntax table used by `nnmail-split-fancy'.")
496 (unless (syntax-table-p nnmail-split-fancy-syntax-table)
497 (setq nnmail-split-fancy-syntax-table
498 (copy-syntax-table (standard-syntax-table)))
499 ;; support the %-hack
500 (modify-syntax-entry ?\% "." nnmail-split-fancy-syntax-table))
502 (defvar nnmail-prepare-save-mail-hook nil
503 "Hook called before saving mail.")
505 (defvar nnmail-split-tracing nil)
506 (defvar nnmail-split-trace nil)
510 (defconst nnmail-version "nnmail 1.0"
515 (defun nnmail-request-post (&optional server)
516 (mail-send-and-exit nil))
518 (defvar nnmail-file-coding-system 'raw-text
519 "Coding system used in nnmail.")
521 (defvar nnmail-incoming-coding-system
522 mm-text-coding-system
523 "Coding system used in reading inbox")
525 (defvar nnmail-pathname-coding-system nil
526 "*Coding system for pathname.")
528 (defun nnmail-find-file (file)
529 "Insert FILE in server buffer safely."
530 (set-buffer nntp-server-buffer)
531 (delete-region (point-min) (point-max))
532 (let ((format-alist nil)
533 (after-insert-file-functions nil))
535 (let ((coding-system-for-read nnmail-file-coding-system)
536 (auto-mode-alist (mm-auto-mode-alist))
537 (file-name-coding-system nnmail-pathname-coding-system))
538 (insert-file-contents file)
542 (defun nnmail-group-pathname (group dir &optional file)
543 "Make pathname for GROUP."
545 (let ((dir (file-name-as-directory (expand-file-name dir))))
546 (setq group (nnheader-replace-duplicate-chars-in-string
547 (nnheader-replace-chars-in-string group ?/ ?_)
549 (setq group (nnheader-translate-file-chars group))
550 ;; If this directory exists, we use it directly.
551 (file-name-as-directory
552 (if (or nnmail-use-long-file-names
553 (file-directory-p (concat dir group)))
554 (expand-file-name group dir)
555 ;; If not, we translate dots into slashes.
557 (mm-encode-coding-string
558 (nnheader-replace-chars-in-string group ?. ?/)
559 nnmail-pathname-coding-system)
563 (defun nnmail-get-active ()
564 "Returns an assoc of group names and active ranges.
565 nn*-request-list should have been called before calling this function."
566 ;; Go through all groups from the active list.
568 (set-buffer nntp-server-buffer)
569 (nnmail-parse-active)))
571 (defun nnmail-parse-active ()
572 "Parse the active file in the current buffer and return an alist."
573 (goto-char (point-min))
574 (unless (re-search-forward "[\\\"]" nil t)
575 (goto-char (point-max))
576 (while (re-search-backward "[][';?()#]" nil t)
578 (goto-char (point-min))
579 (let ((buffer (current-buffer))
580 group-assoc group max min)
584 (narrow-to-region (point) (gnus-point-at-eol))
585 (setq group (read buffer))
586 (unless (stringp group)
587 (setq group (symbol-name group)))
588 (if (and (numberp (setq max (read buffer)))
589 (numberp (setq min (read buffer))))
590 (push (list group (cons min max))
597 (defvar nnmail-active-file-coding-system 'raw-text
598 "*Coding system for active file.")
600 (defun nnmail-save-active (group-assoc file-name)
601 "Save GROUP-ASSOC in ACTIVE-FILE."
602 (let ((coding-system-for-write nnmail-active-file-coding-system))
604 (with-temp-file file-name
605 (nnmail-generate-active group-assoc)))))
607 (defun nnmail-generate-active (alist)
608 "Generate an active file from group-alist ALIST."
611 (while (setq group (pop alist))
612 (insert (format "%S %d %d y\n" (intern (car group)) (cdadr group)
614 (goto-char (point-max))
615 (while (search-backward "\\." nil t)
618 (defun nnmail-get-split-group (file source)
619 "Find out whether this FILE is to be split into GROUP only.
620 If SOURCE is a directory spec, try to return the group name component."
621 (if (eq (car source) 'directory)
622 (let ((file (file-name-nondirectory file)))
623 (mail-source-bind (directory source)
624 (if (string-match (concat (regexp-quote suffix) "$") file)
625 (substring file 0 (match-beginning 0))
629 (defun nnmail-process-babyl-mail-format (func artnum-func)
630 (let ((case-fold-search t)
632 start message-id content-length do-search end)
634 (goto-char (point-min))
636 "
\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
637 (goto-char (match-end 0))
638 (delete-region (match-beginning 0) (match-end 0))
642 ;; Skip all the headers in case there are more "From "s...
643 (or (search-forward "\n\n" nil t)
644 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
645 (search-forward "
\1f\f"))
647 ;; Unquote the ">From " line, if any.
648 (goto-char (point-min))
649 (when (looking-at ">From ")
650 (replace-match "X-From-Line: ") )
651 (run-hooks 'nnmail-prepare-incoming-header-hook)
652 (goto-char (point-max))
653 ;; Find the Message-ID header.
655 (if (re-search-backward
656 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t)
657 (setq message-id (buffer-substring (match-beginning 1)
659 ;; There is no Message-ID here, so we create one.
661 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
663 (insert "Original-")))
665 (insert "Message-ID: " (setq message-id (nnmail-message-id))
667 ;; Look for a Content-Length header.
668 (if (not (save-excursion
669 (and (re-search-backward
670 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
671 (setq content-length (string-to-int
675 ;; We destroy the header, since none of
676 ;; the backends ever use it, and we do not
677 ;; want to confuse other mailers by having
678 ;; a (possibly) faulty header.
679 (progn (insert "X-") t))))
682 (if (or (= (+ (point) content-length) (point-max))
684 (goto-char (+ (point) content-length))
687 (goto-char (+ (point) content-length))
688 (setq do-search nil))
691 ;; Go to the beginning of the next article - or to the end
694 (if (re-search-forward "^
\1f" nil t)
695 (goto-char (match-beginning 0))
696 (goto-char (1- (point-max)))))
697 (delete-char 1) ; delete ^_
700 (narrow-to-region start (point))
701 (goto-char (point-min))
702 (nnmail-check-duplication message-id func artnum-func)
704 (setq end (point-max))))
708 (defsubst nnmail-search-unix-mail-delim ()
709 "Put point at the beginning of the next Unix mbox message."
710 ;; Algorithm used to find the next article in the
711 ;; brain-dead Unix mbox format:
713 ;; 1) Search for "^From ".
714 ;; 2) If we find it, then see whether the previous
715 ;; line is blank and the next line looks like a header.
716 ;; Then it's possible that this is a mail delim, and we use it.
717 (let ((case-fold-search nil)
720 (if (not (re-search-forward "^From " nil t))
724 (when (and (or (bobp)
727 (eq (char-after) ?\n)))
730 (while (looking-at ">From \\|From ")
732 (looking-at "[^ \n\t:]+[ \n\t]*:")))
733 (setq found 'yes)))))
737 (defun nnmail-search-unix-mail-delim-backward ()
738 "Put point at the beginning of the current Unix mbox message."
739 ;; Algorithm used to find the next article in the
740 ;; brain-dead Unix mbox format:
742 ;; 1) Search for "^From ".
743 ;; 2) If we find it, then see whether the previous
744 ;; line is blank and the next line looks like a header.
745 ;; Then it's possible that this is a mail delim, and we use it.
746 (let ((case-fold-search nil)
749 (if (not (re-search-backward "^From " nil t))
753 (when (and (or (bobp)
756 (eq (char-after) ?\n)))
759 (while (looking-at ">From \\|From ")
761 (looking-at "[^ \n\t:]+[ \n\t]*:")))
762 (setq found 'yes)))))
766 (defun nnmail-process-unix-mail-format (func artnum-func)
767 (let ((case-fold-search t)
769 start message-id content-length end skip head-end)
770 (goto-char (point-min))
771 (if (not (and (re-search-forward "^From " nil t)
772 (goto-char (match-beginning 0))))
773 ;; Possibly wrong format?
774 (error "Error, unknown mail format! (Possibly corrupted.)")
775 ;; Carry on until the bitter end.
779 ;; Find the end of the head.
782 (if (search-forward "\n\n" nil t)
784 ;; This will never happen, but just to be on the safe side --
785 ;; if there is no head-body delimiter, we search a bit manually.
786 (while (and (looking-at "From \\|[^ \t]+:")
790 ;; Find the Message-ID header.
791 (goto-char (point-min))
792 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
793 (setq message-id (match-string 1))
795 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
797 (insert "Original-")))
798 ;; There is no Message-ID here, so we create one.
800 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
801 ;; Look for a Content-Length header.
802 (goto-char (point-min))
803 (if (not (re-search-forward
804 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
805 (setq content-length nil)
806 (setq content-length (string-to-int (match-string 1)))
807 ;; We destroy the header, since none of the backends ever
808 ;; use it, and we do not want to confuse other mailers by
809 ;; having a (possibly) faulty header.
812 (run-hooks 'nnmail-prepare-incoming-header-hook)
813 ;; Find the end of this article.
814 (goto-char (point-max))
816 (setq head-end (point))
817 ;; We try the Content-Length value. The idea: skip over the header
818 ;; separator, then check what happens content-length bytes into the
819 ;; message body. This should be either the end ot the buffer, the
820 ;; message separator or a blank line followed by the separator.
821 ;; The blank line should probably be deleted. If neither of the
822 ;; three is met, the content-length header is probably invalid.
825 (setq skip (+ (point) content-length))
827 (cond ((or (= skip (point-max))
828 (= (1+ skip) (point-max)))
829 (setq end (point-max)))
830 ((looking-at "From ")
832 ((looking-at "[ \t]*\n\\(From \\)")
833 (setq end (match-beginning 1)))
837 ;; No Content-Length, so we find the beginning of the next
838 ;; article or the end of the buffer.
840 (or (nnmail-search-unix-mail-delim)
841 (goto-char (point-max))))
842 ;; Allow the backend to save the article.
845 (narrow-to-region start (point))
846 (goto-char (point-min))
848 (nnmail-check-duplication message-id func artnum-func)
849 (setq end (point-max))))
853 (defun nnmail-process-mmdf-mail-format (func artnum-func)
854 (let ((delim "^\^A\^A\^A\^A$")
857 start message-id end)
858 (goto-char (point-min))
859 (if (not (and (re-search-forward delim nil t)
861 ;; Possibly wrong format?
862 (error "Error, unknown mail format! (Possibly corrupted.)")
863 ;; Carry on until the bitter end.
866 ;; Find the end of the head.
869 (if (search-forward "\n\n" nil t)
871 ;; This will never happen, but just to be on the safe side --
872 ;; if there is no head-body delimiter, we search a bit manually.
873 (while (and (looking-at "From \\|[^ \t]+:")
877 ;; Find the Message-ID header.
878 (goto-char (point-min))
879 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
880 (setq message-id (match-string 1))
881 ;; There is no Message-ID here, so we create one.
883 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
885 (insert "Original-")))
887 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
888 (run-hooks 'nnmail-prepare-incoming-header-hook)
889 ;; Find the end of this article.
890 (goto-char (point-max))
892 (if (re-search-forward delim nil t)
894 (goto-char (point-max)))
895 ;; Allow the backend to save the article.
898 (narrow-to-region start (point))
899 (goto-char (point-min))
901 (nnmail-check-duplication message-id func artnum-func)
902 (setq end (point-max))))
907 (defun nnmail-process-maildir-mail-format (func artnum-func)
908 ;; In a maildir, every file contains exactly one mail.
909 (let ((case-fold-search t)
911 (goto-char (point-min))
912 ;; Find the end of the head.
915 (if (search-forward "\n\n" nil t)
917 ;; This will never happen, but just to be on the safe side --
918 ;; if there is no head-body delimiter, we search a bit manually.
919 (while (and (looking-at "From \\|[^ \t]+:")
923 ;; Find the Message-ID header.
924 (goto-char (point-min))
925 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
926 (setq message-id (match-string 1))
927 ;; There is no Message-ID here, so we create one.
929 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
931 (insert "Original-")))
933 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
934 (run-hooks 'nnmail-prepare-incoming-header-hook)
935 ;; Allow the backend to save the article.
938 (goto-char (point-min))
939 (nnmail-check-duplication message-id func artnum-func))
942 (defun nnmail-split-incoming (incoming func &optional exit-func
944 "Go through the entire INCOMING file and pick out each individual mail.
945 FUNC will be called with the buffer narrowed to each mail."
946 (let ( ;; If this is a group-specific split, we bind the split
947 ;; methods to just this group.
948 (nnmail-split-methods (if (and group
949 (not nnmail-resplit-incoming))
950 (list (list group ""))
951 nnmail-split-methods)))
953 ;; Insert the incoming file.
954 (set-buffer (get-buffer-create nnmail-article-buffer))
956 (let ((coding-system-for-read nnmail-incoming-coding-system))
957 (mm-insert-file-contents incoming))
959 (if (zerop (buffer-size))
961 (goto-char (point-min))
962 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
963 ;; Handle both babyl, MMDF and unix mail formats, since
964 ;; movemail will use the former when fetching from a
965 ;; mailbox, the latter when fetching from a file.
966 (cond ((or (looking-at "\^L")
967 (looking-at "BABYL OPTIONS:"))
968 (nnmail-process-babyl-mail-format func artnum-func))
969 ((looking-at "\^A\^A\^A\^A")
970 (nnmail-process-mmdf-mail-format func artnum-func))
971 ((looking-at "Return-Path:")
972 (nnmail-process-maildir-mail-format func artnum-func))
974 (nnmail-process-unix-mail-format func artnum-func))))
977 (kill-buffer (current-buffer))))))
979 (defun nnmail-article-group (func &optional trace)
980 "Look at the headers and return an alist of groups that match.
981 FUNC will be called with the group name to determine the article number."
982 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
983 (obuf (current-buffer))
985 end group-art method grp)
986 (if (and (sequencep methods)
987 (= (length methods) 1))
988 ;; If there is only just one group to put everything in, we
989 ;; just return a list with just this one method in.
991 (list (cons (caar methods) (funcall func (caar methods)))))
992 ;; We do actual comparison.
996 (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
997 (set-buffer nntp-server-buffer)
999 ;; Copy the headers into the work buffer.
1000 (insert-buffer-substring obuf beg end)
1001 ;; Decode MIME headers and charsets.
1002 (let ((mail-parse-charset nnmail-mail-splitting-charset))
1003 (mail-decode-encoded-word-region (point-min) (point-max)))
1004 ;; Fold continuation lines.
1005 (goto-char (point-min))
1006 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1007 (replace-match " " t t))
1008 ;; Nuke pathologically long headers. Since Gnus applies
1009 ;; pathologically complex regexps to the buffer, lines
1010 ;; that are looong will take longer than the Universe's
1011 ;; existence to process.
1012 (goto-char (point-min))
1014 (unless (< (move-to-column nnmail-split-header-length-limit)
1015 nnmail-split-header-length-limit)
1016 (delete-region (point) (progn (end-of-line) (point))))
1019 (goto-char (point-min))
1020 (run-hooks 'nnmail-split-hook)
1021 (when (setq nnmail-split-tracing trace)
1022 (setq nnmail-split-trace nil))
1023 (if (and (symbolp nnmail-split-methods)
1024 (fboundp nnmail-split-methods))
1027 ;; `nnmail-split-methods' is a function, so we
1028 ;; just call this function here and use the
1030 (or (funcall nnmail-split-methods)
1034 5 "Error in `nnmail-split-methods'; using `bogus' mail group")
1037 (setq split (gnus-remove-duplicates split))
1038 ;; The article may be "cross-posted" to `junk'. What
1039 ;; to do? Just remove the `junk' spec. Don't really
1040 ;; see anything else to do...
1042 (while (setq elem (car (memq 'junk split)))
1043 (setq split (delq elem split))))
1047 (lambda (group) (cons group (funcall func group)))
1049 ;; Go through the split methods to find a match.
1051 (or nnmail-crosspost
1053 (goto-char (point-max))
1054 (setq method (pop methods)
1057 (not (equal "" (nth 1 method))))
1060 (if (stringp (nth 1 method))
1061 (let ((expand (string-match "\\\\[0-9&]" grp))
1062 (pos (re-search-backward (cadr method)
1065 (setq grp (nnmail-expand-newtext grp)))
1067 ;; Function to say whether this is a match.
1068 (funcall (nth 1 method) grp)))
1069 ;; Don't enter the article into the same
1071 (not (assoc grp group-art)))
1072 (push (cons grp (funcall func grp))
1074 ;; This is the final group, which is used as a
1078 (list (cons (car method)
1079 (funcall func (car method)))))))))
1080 ;; Produce a trace if non-empty.
1081 (when (and trace nnmail-split-trace)
1082 (let ((restore (current-buffer)))
1083 (nnheader-set-temp-buffer "*Split Trace*")
1085 (dolist (trace (nreverse nnmail-split-trace))
1086 (prin1 trace (current-buffer))
1088 (goto-char (point-min))
1089 (gnus-configure-windows 'split-trace)
1090 (set-buffer restore)))
1091 ;; See whether the split methods returned `junk'.
1092 (if (equal group-art '(junk))
1094 ;; The article may be "cross-posted" to `junk'. What
1095 ;; to do? Just remove the `junk' spec. Don't really
1096 ;; see anything else to do...
1098 (while (setq elem (car (memq 'junk group-art)))
1099 (setq group-art (delq elem group-art)))
1100 (nreverse group-art)))))))
1102 (defun nnmail-insert-lines ()
1103 "Insert how many lines there are in the body of the mail.
1104 Return the number of characters in the body."
1107 (goto-char (point-min))
1108 (unless (search-forward "\n\n" nil t)
1109 (goto-char (point-max))
1111 (setq chars (- (point-max) (point)))
1112 (setq lines (count-lines (point) (point-max)))
1115 (when (re-search-backward "^Lines: " nil t)
1116 (delete-region (point) (progn (forward-line 1) (point)))))
1118 (insert (format "Lines: %d\n" (max lines 0)))
1121 (defun nnmail-insert-xref (group-alist)
1122 "Insert an Xref line based on the (group . article) alist."
1124 (goto-char (point-min))
1125 (unless (search-forward "\n\n" nil t)
1126 (goto-char (point-max))
1129 (when (re-search-backward "^Xref: " nil t)
1130 (delete-region (match-beginning 0)
1131 (progn (forward-line 1) (point))))
1132 (insert (format "Xref: %s" (system-name)))
1134 (insert (format " %s:%d"
1135 (mm-encode-coding-string
1137 nnmail-pathname-coding-system)
1138 (cdar group-alist)))
1139 (setq group-alist (cdr group-alist)))
1142 ;;; Message washing functions
1144 (defun nnmail-remove-leading-whitespace ()
1145 "Remove excessive whitespace from all headers."
1146 (goto-char (point-min))
1147 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1148 (replace-match "\\1" t)))
1150 (defun nnmail-remove-list-identifiers ()
1151 "Remove list identifiers from Subject headers."
1153 (if (consp nnmail-list-identifiers)
1154 (mapconcat 'identity nnmail-list-identifiers " *\\|")
1155 nnmail-list-identifiers)))
1157 (goto-char (point-min))
1158 (while (re-search-forward
1159 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1161 (delete-region (match-beginning 2) (match-end 0))
1162 (beginning-of-line))
1163 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
1164 (delete-region (match-beginning 1) (match-end 1))
1165 (beginning-of-line)))))
1167 (defun nnmail-remove-tabs ()
1168 "Translate TAB characters into SPACE characters."
1169 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1171 (defun nnmail-fix-eudora-headers ()
1172 "Eudora has a broken References line, but an OK In-Reply-To."
1173 (goto-char (point-min))
1174 (when (re-search-forward "^X-Mailer:.*Eudora" nil t)
1175 (goto-char (point-min))
1176 (when (re-search-forward "^References:" nil t)
1178 (insert "X-Gnus-Broken-Eudora-"))
1179 (goto-char (point-min))
1180 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1181 (replace-match "\\1" t))))
1183 (custom-add-option 'nnmail-prepare-incoming-header-hook
1184 'nnmail-fix-eudora-headers)
1186 ;;; Utility functions
1188 (defun nnmail-do-request-post (accept-func &optional server)
1189 "Utility function to directly post a message to an nnmail-derived group.
1190 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1191 to actually put the message in the right group."
1193 (dolist (mbx (message-unquote-tokens
1194 (message-tokenize-header
1195 (message-fetch-field "Newsgroups") ", ")) success)
1196 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1197 (or (gnus-active to-newsgroup)
1198 (gnus-activate-group to-newsgroup)
1199 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1201 (or (and (gnus-request-create-group
1202 to-newsgroup gnus-command-method)
1203 (gnus-activate-group to-newsgroup nil nil
1204 gnus-command-method))
1205 (error "Couldn't create group %s" to-newsgroup)))
1206 (error "No such group: %s" to-newsgroup))
1207 (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1208 (setq success nil))))))
1210 (defun nnmail-split-fancy ()
1211 "Fancy splitting method.
1212 See the documentation for the variable `nnmail-split-fancy' for documentation."
1213 (let ((syntab (syntax-table)))
1216 (set-syntax-table nnmail-split-fancy-syntax-table)
1217 (nnmail-split-it nnmail-split-fancy))
1218 (set-syntax-table syntab))))
1220 (defvar nnmail-split-cache nil)
1221 ;; Alist of split expressions their equivalent regexps.
1223 (defun nnmail-split-it (split)
1224 ;; Return a list of groups matching SPLIT.
1231 ;; A group name. Do the \& and \N subs into the string.
1233 (when nnmail-split-tracing
1234 (push split nnmail-split-trace))
1235 (list (nnmail-expand-newtext split)))
1237 ;; Junk the message.
1239 (when nnmail-split-tracing
1240 (push "junk" nnmail-split-trace))
1243 ;; Builtin & operation.
1244 ((eq (car split) '&)
1245 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1247 ;; Builtin | operation.
1248 ((eq (car split) '|)
1250 (while (and (not done) (cdr split))
1251 (setq split (cdr split)
1252 done (nnmail-split-it (car split))))
1255 ;; Builtin : operation.
1256 ((eq (car split) ':)
1257 (nnmail-split-it (save-excursion (eval (cdr split)))))
1259 ;; Builtin ! operation.
1260 ((eq (car split) '!)
1261 (funcall (cadr split) (nnmail-split-it (caddr split))))
1263 ;; Check the cache for the regexp for this split.
1264 ((setq cached-pair (assq split nnmail-split-cache))
1266 (end-point (point-max))
1267 (value (nth 1 split)))
1269 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1270 (while (and (goto-char end-point)
1271 (re-search-backward (cdr cached-pair) nil t))
1272 (when nnmail-split-tracing
1273 (push split nnmail-split-trace))
1274 (let ((split-rest (cddr split))
1276 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\).
1277 ;; So, start-of-value is the point just before the
1278 ;; beginning of the value, whereas after-header-name
1279 ;; is the point just after the field name.
1280 (start-of-value (match-end 1))
1281 (after-header-name (match-end 2)))
1282 ;; Start the next search just before the beginning of the
1284 (setq end-point (1- start-of-value))
1285 ;; Handle - RESTRICTs
1286 (while (eq (car split-rest) '-)
1287 ;; RESTRICT must start after-header-name and
1288 ;; end after start-of-value, so that, for
1289 ;; (any "foo" - "x-foo" "foo.list")
1290 ;; we do not exclude foo.list just because
1291 ;; the header is: ``To: x-foo, foo''
1293 (if (and (re-search-backward (cadr split-rest)
1294 after-header-name t)
1295 (> (match-end 0) start-of-value))
1296 (setq split-rest nil)
1297 (setq split-rest (cddr split-rest))))
1300 (let ((value (nth 1 split)))
1302 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1303 ;; Someone might want to do a \N sub on this match, so get the
1304 ;; correct match positions.
1305 (re-search-backward value start-of-value))
1306 (dolist (sp (nnmail-split-it (car split-rest)))
1307 (unless (memq sp split-result)
1308 (push sp split-result))))))
1311 ;; Not in cache, compute a regexp for the field/value pair.
1313 (let* ((field (nth 0 split))
1314 (value (nth 1 split))
1315 partial-front regexp
1316 partial-rear regexp)
1318 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1319 (if (and (>= (length value) 2)
1320 (string= ".*" (substring value 0 2)))
1321 (setq value (substring value 2)
1323 ;; Same trick for the rear of the regexp
1324 (if (and (>= (length value) 2)
1325 (string= ".*" (substring value -2)))
1326 (setq value (substring value 0 -2)
1328 (setq regexp (concat "^\\(\\("
1330 (cdr (assq field nnmail-split-abbrev-alist))
1333 (or partial-front "\\<")
1337 (or partial-rear "\\>")))
1338 (push (cons split regexp) nnmail-split-cache)
1339 ;; Now that it's in the cache, just call nnmail-split-it again
1340 ;; on the same split, which will find it immediately in the cache.
1341 (nnmail-split-it split))))))
1343 (defun nnmail-expand-newtext (newtext)
1344 (let ((len (length newtext))
1346 c expanded beg N did-expand)
1349 (while (and (< pos len)
1350 (not (= (aref newtext pos) ?\\)))
1351 (setq pos (1+ pos)))
1353 (push (substring newtext beg pos) expanded))
1355 ;; We hit a \; expand it.
1358 c (aref newtext pos))
1359 (if (not (or (= c ?\&)
1362 ;; \ followed by some character we don't expand.
1363 (push (char-to-string c) expanded)
1368 (when (match-beginning N)
1369 (push (buffer-substring (match-beginning N) (match-end N))
1371 (setq pos (1+ pos)))
1373 (apply 'concat (nreverse expanded))
1376 ;; Activate a backend only if it isn't already activated.
1377 ;; If FORCE, re-read the active file even if the backend is
1378 ;; already activated.
1379 (defun nnmail-activate (backend &optional force)
1380 (nnheader-init-server-buffer)
1381 (let (file timestamp file-time)
1382 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1384 (and (setq file (ignore-errors
1385 (symbol-value (intern (format "%s-active-file"
1387 (setq file-time (nth 5 (file-attributes file)))
1391 (symbol-value (intern
1392 (format "%s-active-timestamp"
1395 (not (consp timestamp))
1396 (equal timestamp '(0 0))
1397 (> (nth 0 file-time) (nth 0 timestamp))
1398 (and (= (nth 0 file-time) (nth 0 timestamp))
1399 (> (nth 1 file-time) (nth 1 timestamp))))))
1401 (or (eq timestamp 'none)
1402 (set (intern (format "%s-active-timestamp" backend))
1404 (funcall (intern (format "%s-request-list" backend)))))
1407 (defun nnmail-message-id ()
1408 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1411 ;;; nnmail duplicate handling
1414 (defvar nnmail-cache-buffer nil)
1416 (defun nnmail-cache-open ()
1417 (if (or (not nnmail-treat-duplicates)
1418 (and nnmail-cache-buffer
1419 (buffer-name nnmail-cache-buffer)))
1420 () ; The buffer is open.
1423 (setq nnmail-cache-buffer
1424 (get-buffer-create " *nnmail message-id cache*")))
1426 (when (file-exists-p nnmail-message-id-cache-file)
1427 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1428 (set-buffer-modified-p nil)
1431 (defun nnmail-cache-close ()
1432 (when (and nnmail-cache-buffer
1433 nnmail-treat-duplicates
1434 (buffer-name nnmail-cache-buffer)
1435 (buffer-modified-p nnmail-cache-buffer))
1437 (set-buffer nnmail-cache-buffer)
1438 ;; Weed out the excess number of Message-IDs.
1439 (goto-char (point-max))
1440 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1443 (delete-region (point-min) (point))))
1445 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1446 (make-directory (file-name-directory nnmail-message-id-cache-file)
1448 (nnmail-write-region (point-min) (point-max)
1449 nnmail-message-id-cache-file nil 'silent)
1450 (set-buffer-modified-p nil)
1451 (setq nnmail-cache-buffer nil)
1452 (kill-buffer (current-buffer)))))
1454 ;; Compiler directives.
1456 (defvar group-art-list)
1458 (defun nnmail-cache-insert (id)
1459 (when nnmail-treat-duplicates
1460 ;; Store some information about the group this message is written
1461 ;; to. This function might have been called from various places.
1462 ;; Sometimes, a function up in the calling sequence has an
1463 ;; argument GROUP which is bound to a string, the group name. At
1464 ;; other times, there is a function up in the calling sequence
1465 ;; which has an argument GROUP-ART which is a list of pairs, and
1466 ;; the car of a pair is a group name. Should we check that the
1467 ;; length of the list is equal to 1? -- kai
1469 (cond ((and (boundp 'group) group)
1471 ((and (boundp 'group-art-list) group-art-list
1472 (listp group-art-list))
1473 (setq g (caar group-art-list)))
1474 ((and (boundp 'group-art) group-art (listp group-art))
1475 (setq g (caar group-art)))
1477 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1478 (nnmail-cache-open))
1480 (set-buffer nnmail-cache-buffer)
1481 (goto-char (point-max))
1482 (if (and g (not (string= "" g))
1483 (gnus-methods-equal-p gnus-command-method
1484 (nnmail-cache-primary-mail-backend)))
1485 (insert id "\t" g "\n")
1486 (insert id "\n"))))))
1488 (defun nnmail-cache-primary-mail-backend ()
1489 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1492 (while (and (null res) be-list)
1493 (setq be (car be-list))
1494 (setq be-list (cdr be-list))
1495 (when (and (gnus-method-option-p be 'respool)
1496 (eval (intern (format "%s-get-new-mail" (car be)))))
1500 ;; Fetch the group name corresponding to the message id stored in the
1502 (defun nnmail-cache-fetch-group (id)
1503 (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1505 (set-buffer nnmail-cache-buffer)
1506 (goto-char (point-max))
1507 (when (search-backward id nil t)
1509 (skip-chars-forward "^\n\r\t")
1512 (buffer-substring (point)
1513 (progn (end-of-line) (point))))))))
1515 ;; Function for nnmail-split-fancy: look up all references in the
1516 ;; cache and if a match is found, return that group.
1517 (defun nnmail-split-fancy-with-parent ()
1518 "Split this message into the same group as its parent.
1519 This function can be used as an entry in `nnmail-split-fancy', for
1520 example like this: (: nnmail-split-fancy)
1521 For a message to be split, it looks for the parent message in the
1522 References or In-Reply-To header and then looks in the message id
1523 cache file (given by the variable `nnmail-message-id-cache-file') to
1524 see which group that message was put in. This group is returned.
1526 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1527 (let* ((refstr (or (message-fetch-field "references")
1528 (message-fetch-field "in-reply-to")))
1531 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1533 (lambda (x) (format "\\(%s\\)" x))
1534 nnmail-split-fancy-with-parent-ignore-groups
1536 nnmail-split-fancy-with-parent-ignore-groups)))
1538 (setq references (nreverse (gnus-split-references refstr)))
1539 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1540 (nnmail-cache-open))
1542 (setq res (or (nnmail-cache-fetch-group x) res))
1543 (when (or (string= "drafts" res)
1544 (and regexp res (string-match regexp res)))
1549 (defun nnmail-cache-id-exists-p (id)
1550 (when nnmail-treat-duplicates
1552 (set-buffer nnmail-cache-buffer)
1553 (goto-char (point-max))
1554 (search-backward id nil t))))
1556 (defun nnmail-fetch-field (header)
1559 (message-narrow-to-head)
1560 (message-fetch-field header))))
1562 (defun nnmail-check-duplication (message-id func artnum-func)
1563 (run-hooks 'nnmail-prepare-incoming-message-hook)
1564 ;; If this is a duplicate message, then we do not save it.
1565 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1566 (case-fold-search t)
1567 (action (when duplication
1569 ((memq nnmail-treat-duplicates '(warn delete))
1570 nnmail-treat-duplicates)
1571 ((nnheader-functionp nnmail-treat-duplicates)
1572 (funcall nnmail-treat-duplicates message-id))
1574 nnmail-treat-duplicates))))
1576 ;; We insert a line that says what the mail source is.
1577 (let ((case-fold-search t))
1578 (goto-char (point-min))
1579 (re-search-forward "^message-id[ \t]*:" nil t)
1581 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1583 ;; Let the backend save the article (or not).
1586 (funcall func (setq group-art
1587 (nreverse (nnmail-article-group artnum-func))))
1588 (nnmail-cache-insert message-id))
1589 ((eq action 'delete)
1590 (setq group-art nil))
1592 ;; We insert a warning.
1593 (let ((case-fold-search t))
1594 (goto-char (point-min))
1595 (re-search-forward "^message-id[ \t]*:" nil t)
1598 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1599 (funcall func (setq group-art
1600 (nreverse (nnmail-article-group artnum-func))))))
1602 (funcall func (setq group-art
1603 (nreverse (nnmail-article-group artnum-func))))))
1604 ;; Add the group-art list to the history list.
1606 (push group-art nnmail-split-history)
1607 (delete-region (point-min) (point-max)))))
1611 (defvar nnmail-fetched-sources nil)
1613 (defun nnmail-get-value (&rest args)
1614 (let ((sym (intern (apply 'format args))))
1616 (symbol-value sym))))
1618 (defun nnmail-get-new-mail (method exit-func temp
1619 &optional group spool-func)
1620 "Read new incoming mail."
1621 (let* ((sources (or mail-sources
1622 (if (listp nnmail-spool-file) nnmail-spool-file
1623 (list nnmail-spool-file))))
1629 incoming incomings source)
1630 (when (and (nnmail-get-value "%s-get-new-mail" method)
1632 (while (setq source (pop sources))
1633 ;; Be compatible with old values.
1638 ((string-match "^po:" source)
1639 (list 'pop :user (substring source (match-end 0))))
1640 ((file-directory-p source)
1641 (list 'directory :path source))
1643 (list 'file :path source)))))
1644 ((eq source 'procmail)
1645 (message "Invalid value for nnmail-spool-file: `procmail'")
1647 ;; Hack to only fetch the contents of a single group's spool file.
1648 (when (and (eq (car source) 'directory)
1649 (null nnmail-scan-directory-mail-source-once)
1651 (mail-source-bind (directory source)
1652 (setq source (append source
1658 ,(concat group suffix)
1659 (file-name-nondirectory file)))))))))
1660 (when nnmail-fetched-sources
1661 (if (member source nnmail-fetched-sources)
1663 (push source nnmail-fetched-sources)
1664 (push source fetching-sources)))))
1665 (when fetching-sources
1666 ;; We first activate all the groups.
1667 (nnmail-activate method)
1668 ;; Allow the user to hook.
1669 (run-hooks 'nnmail-pre-get-new-mail-hook)
1670 ;; Open the message-id cache.
1672 ;; The we go through all the existing mail source specification
1673 ;; and fetch the mail from each.
1674 (while (setq source (pop fetching-sources))
1675 (nnheader-message 4 "%s: Reading incoming mail from %s..."
1676 method (car source))
1681 `(lambda (file orig-file)
1682 (nnmail-split-incoming
1683 file ',(intern (format "%s-save-mail" method))
1685 (if (equal file orig-file)
1687 (nnmail-get-split-group orig-file ',source))
1688 ',(intern (format "%s-active-number" method)))))))
1691 ;; If we did indeed read any incoming spools, we save all info.
1693 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1694 method (car source))
1696 (nnmail-get-value "%s-group-alist" method)
1697 (nnmail-get-value "%s-active-file" method))
1699 (funcall exit-func))
1700 (run-hooks 'nnmail-read-incoming-hook)
1701 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1703 ;; Close the message-id cache.
1704 (nnmail-cache-close)
1705 ;; Allow the user to hook.
1706 (run-hooks 'nnmail-post-get-new-mail-hook))))
1708 (defun nnmail-expired-article-p (group time force &optional inhibit)
1709 "Say whether an article that is TIME old in GROUP should be expired."
1712 (let ((days (or (and nnmail-expiry-wait-function
1713 (funcall nnmail-expiry-wait-function group))
1714 nnmail-expiry-wait)))
1715 (cond ((or (eq days 'never)
1718 ;; This isn't an expirable group.
1720 ((eq days 'immediate)
1721 ;; We expire all articles on sight.
1723 ((equal time '(0 0))
1724 ;; This is an ange-ftp group, and we don't have any dates.
1727 (setq days (days-to-time days))
1728 ;; Compare the time with the current time.
1729 (ignore-errors (time-less-p days (time-since time))))))))
1731 (defun nnmail-expiry-target-group (target group)
1732 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1733 ;; that buffer if the nnfolder group isn't selected.
1734 (let (nnmail-cache-accepted-message-ids)
1735 ;; Don't enter Message-IDs into cache.
1736 ;; Let users hack it in TARGET function.
1737 (when (nnheader-functionp target)
1738 (setq target (funcall target group)))
1739 (unless (eq target 'delete)
1740 (gnus-request-accept-article target nil nil t))))
1742 (defun nnmail-fancy-expiry-target (group)
1743 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1745 (case-fold-search nil)
1746 (from (or (message-fetch-field "from") ""))
1747 (to (or (message-fetch-field "to") ""))
1749 (or (message-fetch-field "date") (current-time-string))))
1751 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1752 (setq header (car regexp-target-pair))
1754 ;; If the header is to-from then match against the
1755 ;; To or From header
1756 ((and (equal header 'to-from)
1757 (or (string-match (cadr regexp-target-pair) from)
1758 (and (string-match message-dont-reply-to-names from)
1759 (string-match (cadr regexp-target-pair) to))))
1760 (setq target (format-time-string (caddr regexp-target-pair) date)))
1761 ((and (not (equal header 'to-from))
1762 (string-match (cadr regexp-target-pair)
1763 (message-fetch-field header)))
1765 (format-time-string (caddr regexp-target-pair) date)))))))
1767 (defun nnmail-check-syntax ()
1768 "Check (and modify) the syntax of the message in the current buffer."
1770 (message-narrow-to-head)
1771 (let ((case-fold-search t))
1772 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1773 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1775 (defun nnmail-write-region (start end filename &optional append visit lockname)
1776 "Do a `write-region', and then set the file modes."
1777 (let ((coding-system-for-write nnmail-file-coding-system)
1778 (file-name-coding-system nnmail-pathname-coding-system))
1779 (write-region start end filename append visit lockname)
1780 (set-file-modes filename nnmail-default-file-modes)))
1783 ;;; Status functions
1786 (defun nnmail-replace-status (name value)
1787 "Make status NAME and VALUE part of the current status line."
1789 (message-narrow-to-head)
1790 (let ((status (nnmail-decode-status)))
1791 (setq status (delq (member name status) status))
1793 (push (cons name value) status))
1794 (message-remove-header "status")
1795 (goto-char (point-max))
1796 (insert "Status: " (nnmail-encode-status status) "\n"))))
1798 (defun nnmail-decode-status ()
1799 "Return a status-value alist from STATUS."
1800 (goto-char (point-min))
1801 (when (re-search-forward "^Status: " nil t)
1802 (let (name value status)
1804 ;; Narrow to the status.
1807 (if (re-search-forward "^[^ \t]" nil t)
1810 ;; Go through all elements and add them to the list.
1811 (goto-char (point-min))
1812 (while (re-search-forward "[^ \t=]+" nil t)
1813 (setq name (match-string 0))
1814 (if (not (eq (char-after) ?=))
1818 (if (not (eq (char-after) ?\"))
1819 (if (not (looking-at "[^ \t]"))
1823 (setq value (match-string 0))
1824 (goto-char (match-end 0)))
1826 (setq value (read (current-buffer)))))
1827 (push (cons name value) status)))
1830 (defun nnmail-encode-status (status)
1831 "Return a status string from STATUS."
1836 (if (string-match "[ \t]" (cdr elem))
1837 (prin1-to-string (cdr elem))
1841 (defun nnmail-split-history ()
1842 "Generate an overview of where the last mail split put articles."
1844 (unless nnmail-split-history
1845 (error "No current split history"))
1846 (with-output-to-temp-buffer "*nnmail split history*"
1847 (with-current-buffer standard-output
1848 (fundamental-mode)) ; for Emacs 20.4+
1849 (let ((history nnmail-split-history)
1851 (while (setq elem (pop history))
1852 (princ (mapconcat (lambda (ga)
1853 (concat (car ga) ":" (int-to-string (cdr ga))))
1858 (defun nnmail-purge-split-history (group)
1859 "Remove all instances of GROUP from `nnmail-split-history'."
1860 (let ((history nnmail-split-history))
1862 (setcar history (gnus-delete-if (lambda (e) (string= (car e) group))
1865 (setq nnmail-split-history (delq nil nnmail-split-history))))
1867 (defun nnmail-new-mail-p (group)
1868 "Say whether GROUP has new mail."
1869 (let ((his nnmail-split-history)
1872 (when (assoc group (pop his))
1877 (defun nnmail-within-headers-p ()
1878 "Check to see if point is within the headers of a unix mail message.
1879 Doesn't change point."
1880 (let ((pos (point)))
1882 (and (nnmail-search-unix-mail-delim-backward)
1883 (not (search-forward "\n\n" pos t))))))
1885 (run-hooks 'nnmail-load-hook)
1889 ;;; nnmail.el ends here