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 file name.")
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 file name 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 %s `%s'.)"
791 (if (buffer-file-name) "file" "buffer")
792 (or (buffer-file-name) (buffer-name)))
793 ;; Carry on until the bitter end.
797 ;; Find the end of the head.
800 (if (search-forward "\n\n" nil t)
802 ;; This will never happen, but just to be on the safe side --
803 ;; if there is no head-body delimiter, we search a bit manually.
804 (while (and (looking-at "From \\|[^ \t]+:")
808 ;; Find the Message-ID header.
809 (goto-char (point-min))
810 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
811 (setq message-id (match-string 1))
813 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
815 (insert "Original-")))
816 ;; There is no Message-ID here, so we create one.
818 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
819 ;; Look for a Content-Length header.
820 (goto-char (point-min))
821 (if (not (re-search-forward
822 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
823 (setq content-length nil)
824 (setq content-length (string-to-int (match-string 1)))
825 ;; We destroy the header, since none of the backends ever
826 ;; use it, and we do not want to confuse other mailers by
827 ;; having a (possibly) faulty header.
830 (run-hooks 'nnmail-prepare-incoming-header-hook)
831 ;; Find the end of this article.
832 (goto-char (point-max))
834 (setq head-end (point))
835 ;; We try the Content-Length value. The idea: skip over the header
836 ;; separator, then check what happens content-length bytes into the
837 ;; message body. This should be either the end of the buffer, the
838 ;; message separator or a blank line followed by the separator.
839 ;; The blank line should probably be deleted. If neither of the
840 ;; three is met, the content-length header is probably invalid.
843 (setq skip (+ (point) content-length))
845 (cond ((or (= skip (point-max))
846 (= (1+ skip) (point-max)))
847 (setq end (point-max)))
848 ((looking-at "From ")
850 ((looking-at "[ \t]*\n\\(From \\)")
851 (setq end (match-beginning 1)))
855 ;; No Content-Length, so we find the beginning of the next
856 ;; article or the end of the buffer.
858 (or (nnmail-search-unix-mail-delim)
859 (goto-char (point-max))))
860 ;; Allow the backend to save the article.
863 (narrow-to-region start (point))
864 (goto-char (point-min))
866 (nnmail-check-duplication message-id func artnum-func)
867 (setq end (point-max))))
871 (defun nnmail-process-mmdf-mail-format (func artnum-func)
872 (let ((delim "^\^A\^A\^A\^A$")
875 start message-id end)
876 (goto-char (point-min))
877 (if (not (and (re-search-forward delim nil t)
879 ;; Possibly wrong format?
880 (error "Error, unknown mail format! (Possibly corrupted.)")
881 ;; Carry on until the bitter end.
884 ;; Find the end of the head.
887 (if (search-forward "\n\n" nil t)
889 ;; This will never happen, but just to be on the safe side --
890 ;; if there is no head-body delimiter, we search a bit manually.
891 (while (and (looking-at "From \\|[^ \t]+:")
895 ;; Find the Message-ID header.
896 (goto-char (point-min))
897 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
898 (setq message-id (match-string 1))
899 ;; There is no Message-ID here, so we create one.
901 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
903 (insert "Original-")))
905 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
906 (run-hooks 'nnmail-prepare-incoming-header-hook)
907 ;; Find the end of this article.
908 (goto-char (point-max))
910 (if (re-search-forward delim nil t)
912 (goto-char (point-max)))
913 ;; Allow the backend to save the article.
916 (narrow-to-region start (point))
917 (goto-char (point-min))
919 (nnmail-check-duplication message-id func artnum-func)
920 (setq end (point-max))))
925 (defun nnmail-process-maildir-mail-format (func artnum-func)
926 ;; In a maildir, every file contains exactly one mail.
927 (let ((case-fold-search t)
929 (goto-char (point-min))
930 ;; Find the end of the head.
933 (if (search-forward "\n\n" nil t)
935 ;; This will never happen, but just to be on the safe side --
936 ;; if there is no head-body delimiter, we search a bit manually.
937 (while (and (looking-at "From \\|[^ \t]+:")
941 ;; Find the Message-ID header.
942 (goto-char (point-min))
943 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
944 (setq message-id (match-string 1))
945 ;; There is no Message-ID here, so we create one.
947 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
949 (insert "Original-")))
951 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
952 (run-hooks 'nnmail-prepare-incoming-header-hook)
953 ;; Allow the backend to save the article.
956 (goto-char (point-min))
957 (nnmail-check-duplication message-id func artnum-func))
960 (defun nnmail-split-incoming (incoming func &optional exit-func
962 "Go through the entire INCOMING file and pick out each individual mail.
963 FUNC will be called with the buffer narrowed to each mail."
964 (let (;; If this is a group-specific split, we bind the split
965 ;; methods to just this group.
966 (nnmail-split-methods (if (and group
967 (not nnmail-resplit-incoming))
968 (list (list group ""))
969 nnmail-split-methods)))
971 ;; Insert the incoming file.
972 (set-buffer (get-buffer-create nnmail-article-buffer))
974 (let ((nnheader-file-coding-system nnmail-incoming-coding-system))
975 (nnheader-insert-file-contents incoming))
977 (if (zerop (buffer-size))
979 (goto-char (point-min))
980 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
981 ;; Handle both babyl, MMDF and unix mail formats, since
982 ;; movemail will use the former when fetching from a
983 ;; mailbox, the latter when fetching from a file.
984 (cond ((or (looking-at "\^L")
985 (looking-at "BABYL OPTIONS:"))
986 (nnmail-process-babyl-mail-format func artnum-func))
987 ((looking-at "\^A\^A\^A\^A")
988 (nnmail-process-mmdf-mail-format func artnum-func))
989 ((looking-at "Return-Path:")
990 (nnmail-process-maildir-mail-format func artnum-func))
992 (nnmail-process-unix-mail-format func artnum-func))))
995 (kill-buffer (current-buffer))))))
997 (defun nnmail-article-group (func &optional trace)
998 "Look at the headers and return an alist of groups that match.
999 FUNC will be called with the group name to determine the article number."
1000 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
1001 (obuf (current-buffer))
1002 group-art method grp)
1003 (if (and (sequencep methods)
1004 (= (length methods) 1))
1005 ;; If there is only just one group to put everything in, we
1006 ;; just return a list with just this one method in.
1008 (list (cons (caar methods) (funcall func (caar methods)))))
1009 ;; We do actual comparison.
1011 ;; Copy the article into the work buffer.
1012 (set-buffer nntp-server-buffer)
1014 (insert-buffer-substring obuf)
1015 ;; Narrow to headers.
1017 (goto-char (point-min))
1018 (if (search-forward "\n\n" nil t)
1021 (goto-char (point-min))
1022 ;; Decode MIME headers and charsets.
1023 (when nnmail-mail-splitting-decodes
1024 (mime-decode-header-in-region (point-min) (point-max)
1025 nnmail-mail-splitting-charset))
1026 ;; Fold continuation lines.
1027 (goto-char (point-min))
1028 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1029 (replace-match " " t t))
1030 ;; Nuke pathologically long headers. Since Gnus applies
1031 ;; pathologically complex regexps to the buffer, lines
1032 ;; that are looong will take longer than the Universe's
1033 ;; existence to process.
1034 (goto-char (point-min))
1036 (unless (< (move-to-column nnmail-split-header-length-limit)
1037 nnmail-split-header-length-limit)
1038 (delete-region (point) (progn (end-of-line) (point))))
1041 (goto-char (point-min))
1042 (run-hooks 'nnmail-split-hook)
1043 (when (setq nnmail-split-tracing trace)
1044 (setq nnmail-split-trace nil))
1045 (if (and (symbolp nnmail-split-methods)
1046 (fboundp nnmail-split-methods))
1049 ;; `nnmail-split-methods' is a function, so we
1050 ;; just call this function here and use the
1052 (or (funcall nnmail-split-methods)
1056 5 "Error in `nnmail-split-methods'; using `bogus' mail group")
1059 (setq split (gnus-remove-duplicates split))
1060 ;; The article may be "cross-posted" to `junk'. What
1061 ;; to do? Just remove the `junk' spec. Don't really
1062 ;; see anything else to do...
1064 (while (setq elem (car (memq 'junk split)))
1065 (setq split (delq elem split))))
1069 (lambda (group) (cons group (funcall func group)))
1071 ;; Go through the split methods to find a match.
1073 (or nnmail-crosspost
1075 (goto-char (point-max))
1076 (setq method (pop methods)
1079 (not (equal "" (nth 1 method))))
1082 (if (stringp (nth 1 method))
1083 (let ((expand (string-match "\\\\[0-9&]" grp))
1084 (pos (re-search-backward (cadr method)
1087 (setq grp (nnmail-expand-newtext grp)))
1089 ;; Function to say whether this is a match.
1090 (funcall (nth 1 method) grp)))
1091 ;; Don't enter the article into the same
1093 (not (assoc grp group-art)))
1094 (push (cons grp (funcall func grp))
1096 ;; This is the final group, which is used as a
1100 (list (cons (car method)
1101 (funcall func (car method)))))))))
1102 ;; Produce a trace if non-empty.
1103 (when (and trace nnmail-split-trace)
1104 (let ((restore (current-buffer)))
1105 (nnheader-set-temp-buffer "*Split Trace*")
1107 (dolist (trace (nreverse nnmail-split-trace))
1108 (prin1 trace (current-buffer))
1110 (goto-char (point-min))
1111 (gnus-configure-windows 'split-trace)
1112 (set-buffer restore)))
1114 ;; See whether the split methods returned `junk'.
1115 (if (equal group-art '(junk))
1117 ;; The article may be "cross-posted" to `junk'. What
1118 ;; to do? Just remove the `junk' spec. Don't really
1119 ;; see anything else to do...
1121 (while (setq elem (car (memq 'junk group-art)))
1122 (setq group-art (delq elem group-art)))
1123 (nreverse group-art)))))))
1125 (defun nnmail-insert-lines ()
1126 "Insert how many lines there are in the body of the mail.
1127 Return the number of characters in the body."
1130 (goto-char (point-min))
1131 (unless (search-forward "\n\n" nil t)
1132 (goto-char (point-max))
1134 (setq chars (- (point-max) (point)))
1135 (setq lines (count-lines (point) (point-max)))
1138 (when (re-search-backward "^Lines: " nil t)
1139 (delete-region (point) (progn (forward-line 1) (point)))))
1141 (insert (format "Lines: %d\n" (max lines 0)))
1144 (defun nnmail-insert-xref (group-alist)
1145 "Insert an Xref line based on the (group . article) alist."
1147 (goto-char (point-min))
1148 (unless (search-forward "\n\n" nil t)
1149 (goto-char (point-max))
1152 (when (re-search-backward "^Xref: " nil t)
1153 (delete-region (match-beginning 0)
1154 (progn (forward-line 1) (point))))
1155 (insert (format "Xref: %s" (system-name)))
1157 (insert (format " %s:%d"
1158 (encode-coding-string
1160 nnmail-pathname-coding-system)
1161 (cdar group-alist)))
1162 (setq group-alist (cdr group-alist)))
1165 ;;; Message washing functions
1167 (defun nnmail-remove-leading-whitespace ()
1168 "Remove excessive whitespace from all headers."
1169 (goto-char (point-min))
1170 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1171 (replace-match "\\1" t)))
1173 (defun nnmail-remove-list-identifiers ()
1174 "Remove list identifiers from Subject headers."
1176 (if (consp nnmail-list-identifiers)
1177 (mapconcat 'identity nnmail-list-identifiers " *\\|")
1178 nnmail-list-identifiers)))
1180 (goto-char (point-min))
1181 (while (re-search-forward
1182 (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1184 (delete-region (match-beginning 2) (match-end 0))
1185 (beginning-of-line))
1186 (when (re-search-forward "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
1187 (delete-region (match-beginning 1) (match-end 1))
1188 (beginning-of-line)))))
1190 (defun nnmail-remove-tabs ()
1191 "Translate TAB characters into SPACE characters."
1192 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1194 (defun nnmail-fix-eudora-headers ()
1195 "Eudora has a broken References line, but an OK In-Reply-To."
1196 (goto-char (point-min))
1197 (when (re-search-forward "^X-Mailer:.*Eudora" nil t)
1198 (goto-char (point-min))
1199 (when (re-search-forward "^References:" nil t)
1201 (insert "X-Gnus-Broken-Eudora-"))
1202 (goto-char (point-min))
1203 (when (re-search-forward "^\\(In-Reply-To:[^\n]+\\)\n[ \t]+" nil t)
1204 (replace-match "\\1" t))))
1206 (custom-add-option 'nnmail-prepare-incoming-header-hook
1207 'nnmail-fix-eudora-headers)
1209 ;;; Utility functions
1211 (defun nnmail-do-request-post (accept-func &optional server)
1212 "Utility function to directly post a message to an nnmail-derived group.
1213 Calls ACCEPT-FUNC (which should be `nnchoke-request-accept-article')
1214 to actually put the message in the right group."
1216 (dolist (mbx (message-unquote-tokens
1217 (message-tokenize-header
1218 (message-fetch-field "Newsgroups") ", ")) success)
1219 (let ((to-newsgroup (gnus-group-prefixed-name mbx gnus-command-method)))
1220 (or (gnus-active to-newsgroup)
1221 (gnus-activate-group to-newsgroup)
1222 (if (gnus-y-or-n-p (format "No such group: %s. Create it? "
1224 (or (and (gnus-request-create-group
1225 to-newsgroup gnus-command-method)
1226 (gnus-activate-group to-newsgroup nil nil
1227 gnus-command-method))
1228 (error "Couldn't create group %s" to-newsgroup)))
1229 (error "No such group: %s" to-newsgroup))
1230 (unless (funcall accept-func mbx (nth 1 gnus-command-method))
1231 (setq success nil))))))
1233 (defun nnmail-split-fancy ()
1234 "Fancy splitting method.
1235 See the documentation for the variable `nnmail-split-fancy' for details."
1236 (let ((syntab (syntax-table)))
1239 (set-syntax-table nnmail-split-fancy-syntax-table)
1240 (nnmail-split-it nnmail-split-fancy))
1241 (set-syntax-table syntab))))
1243 (defvar nnmail-split-cache nil)
1244 ;; Alist of split expressions their equivalent regexps.
1246 (defun nnmail-split-it (split)
1247 ;; Return a list of groups matching SPLIT.
1254 ;; A group name. Do the \& and \N subs into the string.
1256 (when nnmail-split-tracing
1257 (push split nnmail-split-trace))
1258 (list (nnmail-expand-newtext split)))
1260 ;; Junk the message.
1262 (when nnmail-split-tracing
1263 (push "junk" nnmail-split-trace))
1266 ;; Builtin & operation.
1267 ((eq (car split) '&)
1268 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1270 ;; Builtin | operation.
1271 ((eq (car split) '|)
1273 (while (and (not done) (cdr split))
1274 (setq split (cdr split)
1275 done (nnmail-split-it (car split))))
1278 ;; Builtin : operation.
1279 ((eq (car split) ':)
1280 (when nnmail-split-tracing
1281 (push split nnmail-split-trace))
1282 (nnmail-split-it (save-excursion (eval (cdr split)))))
1284 ;; Builtin ! operation.
1285 ((eq (car split) '!)
1286 (funcall (cadr split) (nnmail-split-it (caddr split))))
1288 ;; Check the cache for the regexp for this split.
1289 ((setq cached-pair (assq split nnmail-split-cache))
1291 (end-point (point-max))
1292 (value (nth 1 split)))
1294 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1295 (while (and (goto-char end-point)
1296 (re-search-backward (cdr cached-pair) nil t))
1297 (when nnmail-split-tracing
1298 (push split nnmail-split-trace))
1299 (let ((split-rest (cddr split))
1301 ;; The searched regexp is \(\(FIELD\).*\)\(VALUE\). So,
1302 ;; start-of-value is the the point just before the
1303 ;; beginning of the value, whereas after-header-name is
1304 ;; the point just after the field name.
1305 (start-of-value (match-end 1))
1306 (after-header-name (match-end 2)))
1307 ;; Start the next search just before the beginning of the
1309 (setq end-point (1- start-of-value))
1310 ;; Handle - RESTRICTs
1311 (while (eq (car split-rest) '-)
1312 ;; RESTRICT must start after-header-name and
1313 ;; end after start-of-value, so that, for
1314 ;; (any "foo" - "x-foo" "foo.list")
1315 ;; we do not exclude foo.list just because
1316 ;; the header is: ``To: x-foo, foo''
1318 (if (and (re-search-backward (cadr split-rest)
1319 after-header-name t)
1320 (> (match-end 0) start-of-value))
1321 (setq split-rest nil)
1322 (setq split-rest (cddr split-rest))))
1325 (let ((value (nth 1 split)))
1327 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1328 ;; Someone might want to do a \N sub on this match, so get the
1329 ;; correct match positions.
1330 (re-search-backward (concat "\\<" value "\\>") start-of-value))
1331 (dolist (sp (nnmail-split-it (car split-rest)))
1332 (unless (memq sp split-result)
1333 (push sp split-result))))))
1336 ;; Not in cache, compute a regexp for the field/value pair.
1338 (let* ((field (nth 0 split))
1339 (value (nth 1 split))
1340 partial-front regexp
1341 partial-rear regexp)
1343 (setq value (cdr (assq value nnmail-split-abbrev-alist))))
1344 (if (and (>= (length value) 2)
1345 (string= ".*" (substring value 0 2)))
1346 (setq value (substring value 2)
1348 ;; Same trick for the rear of the regexp
1349 (if (and (>= (length value) 2)
1350 (string= ".*" (substring value -2)))
1351 (setq value (substring value 0 -2)
1353 (setq regexp (concat "^\\(\\("
1355 (cdr (assq field nnmail-split-abbrev-alist))
1358 (or partial-front "\\<")
1362 (or partial-rear "\\>")))
1363 (push (cons split regexp) nnmail-split-cache)
1364 ;; Now that it's in the cache, just call nnmail-split-it again
1365 ;; on the same split, which will find it immediately in the cache.
1366 (nnmail-split-it split))))))
1368 (defun nnmail-expand-newtext (newtext)
1369 (let ((len (length newtext))
1371 c expanded beg N did-expand)
1374 (while (and (< pos len)
1375 (not (= (aref newtext pos) ?\\)))
1376 (setq pos (1+ pos)))
1378 (push (substring newtext beg pos) expanded))
1380 ;; We hit a \; expand it.
1383 c (aref newtext pos))
1384 (if (not (or (= c ?\&)
1387 ;; \ followed by some character we don't expand.
1388 (push (char-to-string c) expanded)
1393 (when (match-beginning N)
1394 (push (buffer-substring (match-beginning N) (match-end N))
1396 (setq pos (1+ pos)))
1398 (apply 'concat (nreverse expanded))
1401 ;; Activate a backend only if it isn't already activated.
1402 ;; If FORCE, re-read the active file even if the backend is
1403 ;; already activated.
1404 (defun nnmail-activate (backend &optional force)
1405 (nnheader-init-server-buffer)
1406 (let (file timestamp file-time)
1407 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1409 (and (setq file (ignore-errors
1410 (symbol-value (intern (format "%s-active-file"
1412 (setq file-time (nth 5 (file-attributes file)))
1416 (symbol-value (intern
1417 (format "%s-active-timestamp"
1420 (not (consp timestamp))
1421 (equal timestamp '(0 0))
1422 (> (nth 0 file-time) (nth 0 timestamp))
1423 (and (= (nth 0 file-time) (nth 0 timestamp))
1424 (> (nth 1 file-time) (nth 1 timestamp))))))
1426 (or (eq timestamp 'none)
1427 (set (intern (format "%s-active-timestamp" backend))
1429 (funcall (intern (format "%s-request-list" backend)))))
1432 (defun nnmail-message-id ()
1433 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1436 ;;; nnmail duplicate handling
1439 (defvar nnmail-cache-buffer nil)
1441 (defun nnmail-cache-open ()
1442 (if (or (not nnmail-treat-duplicates)
1443 (and nnmail-cache-buffer
1444 (buffer-name nnmail-cache-buffer)))
1445 () ; The buffer is open.
1448 (setq nnmail-cache-buffer
1449 (get-buffer-create " *nnmail message-id cache*")))
1451 (when (file-exists-p nnmail-message-id-cache-file)
1452 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1453 (set-buffer-modified-p nil)
1456 (defun nnmail-cache-close ()
1457 (when (and nnmail-cache-buffer
1458 nnmail-treat-duplicates
1459 (buffer-name nnmail-cache-buffer)
1460 (buffer-modified-p nnmail-cache-buffer))
1462 (set-buffer nnmail-cache-buffer)
1463 ;; Weed out the excess number of Message-IDs.
1464 (goto-char (point-max))
1465 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1468 (delete-region (point-min) (point))))
1470 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1471 (make-directory (file-name-directory nnmail-message-id-cache-file)
1473 (nnmail-write-region (point-min) (point-max)
1474 nnmail-message-id-cache-file nil 'silent)
1475 (set-buffer-modified-p nil)
1476 (setq nnmail-cache-buffer nil)
1477 (kill-buffer (current-buffer)))))
1479 ;; Compiler directives.
1481 (defvar group-art-list)
1483 (defun nnmail-cache-insert (id grp)
1484 (when nnmail-treat-duplicates
1485 ;; Store some information about the group this message is written
1486 ;; to. This is passed in as the grp argument -- all locations this
1487 ;; has been called from have been checked and the group is available.
1488 ;; The only ambiguous case is nnmail-check-duplication which will only
1489 ;; pass the first (of possibly >1) group which matches. -Josh
1490 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1491 (nnmail-cache-open))
1493 (set-buffer nnmail-cache-buffer)
1494 (goto-char (point-max))
1495 (if (and grp (not (string= "" grp))
1496 (gnus-methods-equal-p gnus-command-method
1497 (nnmail-cache-primary-mail-backend)))
1498 (let ((regexp (if (consp nnmail-cache-ignore-groups)
1499 (mapconcat 'identity nnmail-cache-ignore-groups
1501 nnmail-cache-ignore-groups)))
1502 (unless (and regexp (string-match regexp grp))
1503 (insert id "\t" grp "\n")))
1504 (insert id "\n")))))
1506 (defun nnmail-cache-primary-mail-backend ()
1507 (let ((be-list (cons gnus-select-method gnus-secondary-select-methods))
1510 (while (and (null res) be-list)
1511 (setq be (car be-list))
1512 (setq be-list (cdr be-list))
1513 (when (and (gnus-method-option-p be 'respool)
1514 (eval (intern (format "%s-get-new-mail" (car be)))))
1518 ;; Fetch the group name corresponding to the message id stored in the
1520 (defun nnmail-cache-fetch-group (id)
1521 (when (and nnmail-treat-duplicates nnmail-cache-buffer)
1523 (set-buffer nnmail-cache-buffer)
1524 (goto-char (point-max))
1525 (when (search-backward id nil t)
1527 (skip-chars-forward "^\n\r\t")
1528 (unless (looking-at "[\r\n]")
1530 (buffer-substring (point)
1531 (progn (end-of-line) (point))))))))
1533 ;; Function for nnmail-split-fancy: look up all references in the
1534 ;; cache and if a match is found, return that group.
1535 (defun nnmail-split-fancy-with-parent ()
1536 "Split this message into the same group as its parent.
1537 This function can be used as an entry in `nnmail-split-fancy', for
1538 example like this: (: nnmail-split-fancy)
1539 For a message to be split, it looks for the parent message in the
1540 References or In-Reply-To header and then looks in the message id
1541 cache file (given by the variable `nnmail-message-id-cache-file') to
1542 see which group that message was put in. This group is returned.
1544 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1545 (let* ((refstr (or (message-fetch-field "references")
1546 (message-fetch-field "in-reply-to")))
1549 (regexp (if (consp nnmail-split-fancy-with-parent-ignore-groups)
1551 (lambda (x) (format "\\(%s\\)" x))
1552 nnmail-split-fancy-with-parent-ignore-groups
1554 nnmail-split-fancy-with-parent-ignore-groups)))
1556 (setq references (nreverse (gnus-split-references refstr)))
1557 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1558 (nnmail-cache-open))
1560 (setq res (or (nnmail-cache-fetch-group x) res))
1561 (when (or (member res '("delayed" "drafts" "queue"))
1562 (and regexp res (string-match regexp res)))
1567 (defun nnmail-cache-id-exists-p (id)
1568 (when nnmail-treat-duplicates
1570 (set-buffer nnmail-cache-buffer)
1571 (goto-char (point-max))
1572 (search-backward id nil t))))
1574 (defun nnmail-fetch-field (header)
1577 (message-narrow-to-head)
1578 (message-fetch-field header))))
1580 (defun nnmail-check-duplication (message-id func artnum-func)
1581 (run-hooks 'nnmail-prepare-incoming-message-hook)
1582 ;; If this is a duplicate message, then we do not save it.
1583 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1584 (case-fold-search t)
1585 (action (when duplication
1587 ((memq nnmail-treat-duplicates '(warn delete))
1588 nnmail-treat-duplicates)
1589 ((nnheader-functionp nnmail-treat-duplicates)
1590 (funcall nnmail-treat-duplicates message-id))
1592 nnmail-treat-duplicates))))
1594 ;; We insert a line that says what the mail source is.
1595 (let ((case-fold-search t))
1596 (goto-char (point-min))
1597 (re-search-forward "^message-id[ \t]*:" nil t)
1599 (insert (format "X-Gnus-Mail-Source: %s\n" mail-source-string)))
1601 ;; Let the backend save the article (or not).
1604 (funcall func (setq group-art
1605 (nreverse (nnmail-article-group artnum-func))))
1606 (nnmail-cache-insert message-id (caar group-art)))
1607 ((eq action 'delete)
1608 (setq group-art nil))
1610 ;; We insert a warning.
1611 (let ((case-fold-search t))
1612 (goto-char (point-min))
1613 (re-search-forward "^message-id[ \t]*:" nil t)
1616 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1617 (funcall func (setq group-art
1618 (nreverse (nnmail-article-group artnum-func))))))
1620 (funcall func (setq group-art
1621 (nreverse (nnmail-article-group artnum-func))))))
1622 ;; Add the group-art list to the history list.
1624 (push group-art nnmail-split-history)
1625 (delete-region (point-min) (point-max)))))
1629 (defvar nnmail-fetched-sources nil)
1631 (defun nnmail-get-value (&rest args)
1632 (let ((sym (intern (apply 'format args))))
1634 (symbol-value sym))))
1636 (defun nnmail-get-new-mail (method exit-func temp
1637 &optional group spool-func)
1638 "Read new incoming mail."
1639 (let* ((sources (or mail-sources
1640 (if (listp nnmail-spool-file) nnmail-spool-file
1641 (list nnmail-spool-file))))
1647 incoming incomings source)
1648 (when (and (nnmail-get-value "%s-get-new-mail" method)
1650 (while (setq source (pop sources))
1651 ;; Be compatible with old values.
1656 ((string-match "^po:" source)
1657 (list 'pop :user (substring source (match-end 0))))
1658 ((file-directory-p source)
1659 (list 'directory :path source))
1661 (list 'file :path source)))))
1662 ((eq source 'procmail)
1663 (message "Invalid value for nnmail-spool-file: `procmail'")
1665 ;; Hack to only fetch the contents of a single group's spool file.
1666 (when (and (eq (car source) 'directory)
1667 (null nnmail-scan-directory-mail-source-once)
1669 (mail-source-bind (directory source)
1670 (setq source (append source
1676 ,(concat group suffix)
1677 (file-name-nondirectory file)))))))))
1678 (when nnmail-fetched-sources
1679 (if (member source nnmail-fetched-sources)
1681 (push source nnmail-fetched-sources)
1682 (push source fetching-sources)))))
1683 (when fetching-sources
1684 ;; We first activate all the groups.
1685 (nnmail-activate method)
1686 ;; Allow the user to hook.
1687 (run-hooks 'nnmail-pre-get-new-mail-hook)
1688 ;; Open the message-id cache.
1690 ;; The we go through all the existing mail source specification
1691 ;; and fetch the mail from each.
1692 (while (setq source (pop fetching-sources))
1693 (nnheader-message 4 "%s: Reading incoming mail from %s..."
1694 method (car source))
1699 `(lambda (file orig-file)
1700 (nnmail-split-incoming
1701 file ',(intern (format "%s-save-mail" method))
1703 (if (equal file orig-file)
1705 (nnmail-get-split-group orig-file ',source))
1706 ',(intern (format "%s-active-number" method)))))))
1709 ;; If we did indeed read any incoming spools, we save all info.
1711 (nnheader-message 4 "%s: Reading incoming mail (no new mail)...done"
1712 method (car source))
1714 (nnmail-get-value "%s-group-alist" method)
1715 (nnmail-get-value "%s-active-file" method))
1717 (funcall exit-func))
1718 (run-hooks 'nnmail-read-incoming-hook)
1719 (nnheader-message 4 "%s: Reading incoming mail (%d new)...done" method
1721 ;; Close the message-id cache.
1722 (nnmail-cache-close)
1723 ;; Allow the user to hook.
1724 (run-hooks 'nnmail-post-get-new-mail-hook))))
1726 (defun nnmail-expired-article-p (group time force &optional inhibit)
1727 "Say whether an article that is TIME old in GROUP should be expired."
1730 (let ((days (or (and nnmail-expiry-wait-function
1731 (funcall nnmail-expiry-wait-function group))
1732 nnmail-expiry-wait)))
1733 (cond ((or (eq days 'never)
1736 ;; This isn't an expirable group.
1738 ((eq days 'immediate)
1739 ;; We expire all articles on sight.
1741 ((equal time '(0 0))
1742 ;; This is an ange-ftp group, and we don't have any dates.
1745 (setq days (days-to-time days))
1746 ;; Compare the time with the current time.
1747 (ignore-errors (time-less-p days (time-since time))))))))
1749 (defun nnmail-expiry-target-group (target group)
1750 ;; Do not invoke this from nntp-server-buffer! At least nnfolder clears
1751 ;; that buffer if the nnfolder group isn't selected.
1752 (let (nnmail-cache-accepted-message-ids)
1753 ;; Don't enter Message-IDs into cache.
1754 ;; Let users hack it in TARGET function.
1755 (when (nnheader-functionp target)
1756 (setq target (funcall target group)))
1757 (unless (eq target 'delete)
1758 (when (or (gnus-request-group target)
1759 (gnus-request-create-group target))
1760 (let ((group-art (gnus-request-accept-article target nil nil t)))
1761 (when (consp group-art)
1762 (gnus-group-mark-article-read target (cdr group-art))))))))
1764 (defun nnmail-fancy-expiry-target (group)
1765 "Returns a target expiry group determined by `nnmail-fancy-expiry-targets'."
1767 (case-fold-search nil)
1768 (from (or (message-fetch-field "from") ""))
1769 (to (or (message-fetch-field "to") ""))
1771 (or (message-fetch-field "date") (current-time-string))))
1773 (dolist (regexp-target-pair (reverse nnmail-fancy-expiry-targets) target)
1774 (setq header (car regexp-target-pair))
1776 ;; If the header is to-from then match against the
1777 ;; To or From header
1778 ((and (equal header 'to-from)
1779 (or (string-match (cadr regexp-target-pair) from)
1780 (and (string-match message-dont-reply-to-names from)
1781 (string-match (cadr regexp-target-pair) to))))
1782 (setq target (format-time-string (caddr regexp-target-pair) date)))
1783 ((and (not (equal header 'to-from))
1784 (string-match (cadr regexp-target-pair)
1786 (message-fetch-field header)
1789 (format-time-string (caddr regexp-target-pair) date)))))))
1791 (defun nnmail-check-syntax ()
1792 "Check (and modify) the syntax of the message in the current buffer."
1794 (message-narrow-to-head)
1795 (let ((case-fold-search t))
1796 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1797 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1799 (defun nnmail-write-region (start end filename &optional append visit lockname)
1800 "Do a `write-region', and then set the file modes."
1801 (let ((file-name-coding-system nnmail-pathname-coding-system)
1802 (pathname-coding-system nnmail-pathname-coding-system))
1803 (write-region-as-coding-system
1804 nnmail-file-coding-system start end filename append visit lockname)
1805 (set-file-modes filename nnmail-default-file-modes)))
1808 ;;; Status functions
1811 (defun nnmail-replace-status (name value)
1812 "Make status NAME and VALUE part of the current status line."
1814 (message-narrow-to-head)
1815 (let ((status (nnmail-decode-status)))
1816 (setq status (delq (member name status) status))
1818 (push (cons name value) status))
1819 (message-remove-header "status")
1820 (goto-char (point-max))
1821 (insert "Status: " (nnmail-encode-status status) "\n"))))
1823 (defun nnmail-decode-status ()
1824 "Return a status-value alist from STATUS."
1825 (goto-char (point-min))
1826 (when (re-search-forward "^Status: " nil t)
1827 (let (name value status)
1829 ;; Narrow to the status.
1832 (if (re-search-forward "^[^ \t]" nil t)
1835 ;; Go through all elements and add them to the list.
1836 (goto-char (point-min))
1837 (while (re-search-forward "[^ \t=]+" nil t)
1838 (setq name (match-string 0))
1839 (if (not (eq (char-after) ?=))
1843 (if (not (eq (char-after) ?\"))
1844 (if (not (looking-at "[^ \t]"))
1848 (setq value (match-string 0))
1849 (goto-char (match-end 0)))
1851 (setq value (read (current-buffer)))))
1852 (push (cons name value) status)))
1855 (defun nnmail-encode-status (status)
1856 "Return a status string from STATUS."
1861 (if (string-match "[ \t]" (cdr elem))
1862 (prin1-to-string (cdr elem))
1866 (defun nnmail-split-history ()
1867 "Generate an overview of where the last mail split put articles."
1869 (unless nnmail-split-history
1870 (error "No current split history"))
1871 (with-output-to-temp-buffer "*nnmail split history*"
1872 (with-current-buffer standard-output
1873 (fundamental-mode)) ; for Emacs 20.4+
1874 (let ((history nnmail-split-history)
1876 (while (setq elem (pop history))
1877 (princ (mapconcat (lambda (ga)
1878 (concat (car ga) ":" (int-to-string (cdr ga))))
1883 (defun nnmail-purge-split-history (group)
1884 "Remove all instances of GROUP from `nnmail-split-history'."
1885 (let ((history nnmail-split-history))
1887 (setcar history (gnus-delete-if (lambda (e) (string= (car e) group))
1890 (setq nnmail-split-history (delq nil nnmail-split-history))))
1892 (defun nnmail-new-mail-p (group)
1893 "Say whether GROUP has new mail."
1894 (let ((his nnmail-split-history)
1897 (when (assoc group (pop his))
1902 (defun nnmail-new-mail-numbers (group)
1903 "Say how many articles has been incorporated to GROUP."
1904 (let ((his (apply 'append nnmail-split-history))
1907 (when (string= group (caar his))
1908 (push (cdar his) numbers))
1909 (setq his (cdr his)))
1912 (defun nnmail-within-headers-p ()
1913 "Check to see if point is within the headers of a unix mail message.
1914 Doesn't change point."
1915 (let ((pos (point)))
1917 (and (nnmail-search-unix-mail-delim-backward)
1918 (not (search-forward "\n\n" pos t))))))
1920 (run-hooks 'nnmail-load-hook)
1924 ;;; nnmail.el ends here