1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
35 (require 'mail-source)
39 (autoload 'gnus-add-buffer "gnus")
40 (autoload 'gnus-kill-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 expiry
170 is to be performed in, and it should return an integer that says how
171 many days an article can be stored before it is considered \"old\".
172 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-spool-hook nil
353 "*A hook called when a new article is spooled."
357 (defcustom nnmail-large-newsgroup 50
358 "*The number of the articles which indicates a large newsgroup or nil.
359 If the number of the articles is greater than the value, verbose
360 messages will be shown to indicate the current status."
361 :group 'nnmail-various
362 :type '(choice (const :tag "infinite" nil)
363 (number :tag "count")))
365 (defcustom nnmail-split-fancy "mail.misc"
366 "Incoming mail can be split according to this fancy variable.
367 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
369 The format of this variable is SPLIT, where SPLIT can be one of
372 GROUP: Mail will be stored in GROUP (a string).
374 \(FIELD VALUE [- RESTRICT [- RESTRICT [...]]] SPLIT): If the message
375 field FIELD (a regexp) contains VALUE (a regexp), store the messages
376 as specified by SPLIT. If RESTRICT (a regexp) matches some string
377 after FIELD and before the end of the matched VALUE, return nil,
378 otherwise process SPLIT. Multiple RESTRICTs add up, further
379 restricting the possibility of processing SPLIT.
381 \(| SPLIT...): Process each SPLIT expression until one of them matches.
382 A SPLIT expression is said to match if it will cause the mail
383 message to be stored in one or more groups.
385 \(& SPLIT...): Process each SPLIT expression.
387 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
388 the buffer containing the message headers. The return value FUNCTION
389 should be a split, which is then recursively processed.
391 \(! FUNCTION SPLIT): Call FUNCTION with the result of SPLIT. The
392 return value FUNCTION should be a split, which is then recursively
395 junk: Mail will be deleted. Use with care! Do not submerge in water!
397 (setq nnmail-split-fancy
398 '(| (\"Subject\" \"MAKE MONEY FAST\" junk)
399 ...other.rules.omitted...))
401 FIELD must match a complete field name. VALUE must match a complete
402 word according to the `nnmail-split-fancy-syntax-table' syntax table.
403 You can use \".*\" in the regexps to match partial field names or words.
405 FIELD and VALUE can also be lisp symbols, in that case they are expanded
406 as specified in `nnmail-split-abbrev-alist'.
408 GROUP can contain \\& and \\N which will substitute from matching
409 \\(\\) patterns in the previous VALUE.
413 \(setq nnmail-split-methods 'nnmail-split-fancy
415 ;; Messages from the mailer daemon are not crossposted to any of
416 ;; the ordinary groups. Warnings are put in a separate group
418 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
420 ;; Non-error messages are crossposted to all relevant
421 ;; groups, but we don't crosspost between the group for the
422 ;; (ding) list and the group for other (ding) related mail.
423 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
424 (\"subject\" \"ding\" \"ding.misc\"))
425 ;; Other mailing lists...
426 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
427 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
428 ;; Both lists below have the same suffix, so prevent
429 ;; cross-posting to mkpkg.list of messages posted only to
430 ;; the bugs- list, but allow cross-posting when the
431 ;; message was really cross-posted.
432 (any \"bugs-mypackage@somewhere\" \"mypkg.bugs\")
433 (any \"mypackage@somewhere\" - \"bugs-mypackage\" \"mypkg.list\")
436 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
437 ;; Unmatched mail goes to the catch all group.
443 (defcustom nnmail-split-abbrev-alist
444 '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
445 (mail . "mailer-daemon\\|postmaster\\|uucp")
446 (to . "to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
447 (from . "from\\|sender\\|resent-from")
448 (nato . "to\\|cc\\|resent-to\\|resent-cc")
449 (naany . "from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
450 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
452 :type '(repeat (cons :format "%v" symbol regexp)))
454 (defcustom nnmail-message-id-cache-length 1000
455 "*The approximate number of Message-IDs nnmail will keep in its cache.
456 If this variable is nil, no checking on duplicate messages will be
458 :group 'nnmail-duplicate
459 :type '(choice (const :tag "disable" nil)
460 (integer :format "%v")))
462 (defcustom nnmail-message-id-cache-file "~/.nnmail-cache"
463 "*The file name of the nnmail Message-ID cache."
464 :group 'nnmail-duplicate
468 (defcustom nnmail-treat-duplicates 'warn
469 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
470 Three values are valid: nil, which means that nnmail is not to keep a
471 Message-ID cache; `warn', which means that nnmail should insert extra
472 headers to warn the user about the duplication (this is the default);
473 and `delete', which means that nnmail will delete duplicated mails.
475 This variable can also be a function. It will be called from a buffer
476 narrowed to the article in question with the Message-ID as a
477 parameter. It should return nil, `warn' or `delete'."
478 :group 'nnmail-duplicate
479 :type '(choice (const :tag "off" nil)
483 (defcustom nnmail-extra-headers '(To Newsgroups)
484 "*Extra headers to parse."
487 :type '(repeat symbol))
489 (defcustom nnmail-split-header-length-limit 512
490 "Header lines longer than this limit are excluded from the split function."
495 (defcustom nnmail-mail-splitting-charset nil
496 "Default charset to be used when splitting incoming mail."
500 (defcustom nnmail-mail-splitting-decodes nil
501 "Whether the nnmail splitting functionality should MIME decode headers."
505 ;;; Internal variables.
507 (defvar nnmail-article-buffer " *nnmail incoming*"
508 "The buffer used for splitting incoming mails.")
510 (defvar nnmail-split-history nil
511 "List of group/article elements that say where the previous split put messages.")
513 (defvar nnmail-split-fancy-syntax-table nil
514 "Syntax table used by `nnmail-split-fancy'.")
515 (unless (syntax-table-p nnmail-split-fancy-syntax-table)
516 (setq nnmail-split-fancy-syntax-table
517 (copy-syntax-table (standard-syntax-table)))
518 ;; support the %-hack
519 (modify-syntax-entry ?\% "." nnmail-split-fancy-syntax-table))
521 (defvar nnmail-prepare-save-mail-hook nil
522 "Hook called before saving mail.")
524 (defvar nnmail-split-tracing nil)
525 (defvar nnmail-split-trace nil)
529 (defconst nnmail-version "nnmail 1.0"
534 (defun nnmail-request-post (&optional server)
535 (mail-send-and-exit nil))
537 (defvar nnmail-file-coding-system 'raw-text
538 "Coding system used in nnmail.")
540 (defvar nnmail-incoming-coding-system
541 mm-text-coding-system
542 "Coding system used in reading inbox")
544 (defvar nnmail-pathname-coding-system nil
545 "*Coding system for file name.")
547 (defun nnmail-find-file (file)
548 "Insert FILE in server buffer safely."
549 (set-buffer nntp-server-buffer)
550 (delete-region (point-min) (point-max))
551 (let ((format-alist nil)
552 (after-insert-file-functions nil))
554 (let ((coding-system-for-read nnmail-file-coding-system)
555 (auto-mode-alist (mm-auto-mode-alist))
556 (file-name-coding-system nnmail-pathname-coding-system))
557 (insert-file-contents file)
561 (defun nnmail-group-pathname (group dir &optional file)
562 "Make file name for GROUP."
564 (let ((dir (file-name-as-directory (expand-file-name dir))))
565 (setq group (nnheader-replace-duplicate-chars-in-string
566 (nnheader-replace-chars-in-string group ?/ ?_)
568 (setq group (nnheader-translate-file-chars group))
569 ;; If this directory exists, we use it directly.
570 (file-name-as-directory
571 (if (or nnmail-use-long-file-names
572 (file-directory-p (concat dir group)))
573 (expand-file-name group dir)
574 ;; If not, we translate dots into slashes.
576 (mm-encode-coding-string
577 (nnheader-replace-chars-in-string group ?. ?/)
578 nnmail-pathname-coding-system)
582 (defun nnmail-get-active ()
583 "Returns an assoc of group names and active ranges.
584 nn*-request-list should have been called before calling this function."
585 ;; Go through all groups from the active list.
587 (set-buffer nntp-server-buffer)
588 (nnmail-parse-active)))
590 (defun nnmail-parse-active ()
591 "Parse the active file in the current buffer and return an alist."
592 (goto-char (point-min))
593 (unless (re-search-forward "[\\\"]" nil t)
594 (goto-char (point-max))
595 (while (re-search-backward "[][';?()#]" nil t)
597 (goto-char (point-min))
598 (let ((buffer (current-buffer))
599 group-assoc group max min)
603 (narrow-to-region (point) (gnus-point-at-eol))
604 (setq group (read buffer))
605 (unless (stringp group)
606 (setq group (symbol-name group)))
607 (if (and (numberp (setq max (read buffer)))
608 (numberp (setq min (read buffer))))
609 (push (list group (cons min max))
616 (defvar nnmail-active-file-coding-system 'raw-text
617 "*Coding system for active file.")
619 (defun nnmail-save-active (group-assoc file-name)
620 "Save GROUP-ASSOC in ACTIVE-FILE."
621 (let ((coding-system-for-write nnmail-active-file-coding-system))
623 (with-temp-file file-name
624 (nnmail-generate-active group-assoc)))))
626 (defun nnmail-generate-active (alist)
627 "Generate an active file from group-alist ALIST."
630 (while (setq group (pop alist))
631 (insert (format "%S %d %d y\n" (intern (car group)) (cdadr group)
633 (goto-char (point-max))
634 (while (search-backward "\\." nil t)
637 (defun nnmail-get-split-group (file source)
638 "Find out whether this FILE is to be split into GROUP only.
639 If SOURCE is a directory spec, try to return the group name component."
640 (if (eq (car source) 'directory)
641 (let ((file (file-name-nondirectory file)))
642 (mail-source-bind (directory source)
643 (if (string-match (concat (regexp-quote suffix) "$") file)
644 (substring file 0 (match-beginning 0))
648 (defun nnmail-process-babyl-mail-format (func artnum-func)
649 (let ((case-fold-search t)
651 start message-id content-length do-search end)
653 (goto-char (point-min))
655 "
\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
656 (goto-char (match-end 0))
657 (delete-region (match-beginning 0) (match-end 0))
661 ;; Skip all the headers in case there are more "From "s...
662 (or (search-forward "\n\n" nil t)
663 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
664 (search-forward "
\1f\f"))
666 ;; Unquote the ">From " line, if any.
667 (goto-char (point-min))
668 (when (looking-at ">From ")
669 (replace-match "X-From-Line: ") )
670 (run-hooks 'nnmail-prepare-incoming-header-hook)
671 (goto-char (point-max))
672 ;; Find the Message-ID header.
674 (if (re-search-backward
675 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t)
676 (setq message-id (buffer-substring (match-beginning 1)
678 ;; There is no Message-ID here, so we create one.
680 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
682 (insert "Original-")))
684 (insert "Message-ID: " (setq message-id (nnmail-message-id))
686 ;; Look for a Content-Length header.
687 (if (not (save-excursion
688 (and (re-search-backward
689 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
690 (setq content-length (string-to-int
694 ;; We destroy the header, since none of
695 ;; the backends ever use it, and we do not
696 ;; want to confuse other mailers by having
697 ;; a (possibly) faulty header.
698 (progn (insert "X-") t))))
701 (if (or (= (+ (point) content-length) (point-max))
703 (goto-char (+ (point) content-length))
706 (goto-char (+ (point) content-length))
707 (setq do-search nil))
710 ;; Go to the beginning of the next article - or to the end
713 (if (re-search-forward "^
\1f" nil t)
714 (goto-char (match-beginning 0))
715 (goto-char (1- (point-max)))))
716 (delete-char 1) ; delete ^_
719 (narrow-to-region start (point))
720 (goto-char (point-min))
721 (nnmail-check-duplication message-id func artnum-func)
723 (setq end (point-max))))
727 (defsubst nnmail-search-unix-mail-delim ()
728 "Put point at the beginning of the next Unix mbox message."
729 ;; Algorithm used to find the next article in the
730 ;; brain-dead Unix mbox format:
732 ;; 1) Search for "^From ".
733 ;; 2) If we find it, then see whether the previous
734 ;; line is blank and the next line looks like a header.
735 ;; Then it's possible that this is a mail delim, and we use it.
736 (let ((case-fold-search nil)
739 (if (not (re-search-forward "^From " nil t))
743 (when (and (or (bobp)
746 (eq (char-after) ?\n)))
749 (while (looking-at ">From \\|From ")
751 (looking-at "[^ \n\t:]+[ \n\t]*:")))
752 (setq found 'yes)))))
756 (defun nnmail-search-unix-mail-delim-backward ()
757 "Put point at the beginning of the current Unix mbox message."
758 ;; Algorithm used to find the next article in the
759 ;; brain-dead Unix mbox format:
761 ;; 1) Search for "^From ".
762 ;; 2) If we find it, then see whether the previous
763 ;; line is blank and the next line looks like a header.
764 ;; Then it's possible that this is a mail delim, and we use it.
765 (let ((case-fold-search nil)
768 (if (not (re-search-backward "^From " nil t))
772 (when (and (or (bobp)
775 (eq (char-after) ?\n)))
778 (while (looking-at ">From \\|From ")
780 (looking-at "[^ \n\t:]+[ \n\t]*:")))
781 (setq found 'yes)))))
785 (defun nnmail-process-unix-mail-format (func artnum-func)
786 (let ((case-fold-search t)
788 start message-id content-length end skip head-end)
789 (goto-char (point-min))
790 (if (not (and (re-search-forward "^From " nil t)
791 (goto-char (match-beginning 0))))
792 ;; Possibly wrong format?
793 (error "Error, unknown mail format! (Possibly corrupted %s `%s'.)"
794 (if (buffer-file-name) "file" "buffer")
795 (or (buffer-file-name) (buffer-name)))
796 ;; Carry on until the bitter end.
800 ;; Find the end of the head.
803 (if (search-forward "\n\n" nil t)
805 ;; This will never happen, but just to be on the safe side --
806 ;; if there is no head-body delimiter, we search a bit manually.
807 (while (and (looking-at "From \\|[^ \t]+:")
811 ;; Find the Message-ID header.
812 (goto-char (point-min))
813 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
814 (setq message-id (match-string 1))
816 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
818 (insert "Original-")))
819 ;; There is no Message-ID here, so we create one.
821 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
822 ;; Look for a Content-Length header.
823 (goto-char (point-min))
824 (if (not (re-search-forward
825 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
826 (setq content-length nil)
827 (setq content-length (string-to-int (match-string 1)))
828 ;; We destroy the header, since none of the backends ever
829 ;; use it, and we do not want to confuse other mailers by
830 ;; having a (possibly) faulty header.
833 (run-hooks 'nnmail-prepare-incoming-header-hook)
834 ;; Find the end of this article.
835 (goto-char (point-max))
837 (setq head-end (point))
838 ;; We try the Content-Length value. The idea: skip over the header
839 ;; separator, then check what happens content-length bytes into the
840 ;; message body. This should be either the end of the buffer, the
841 ;; message separator or a blank line followed by the separator.
842 ;; The blank line should probably be deleted. If neither of the
843 ;; three is met, the content-length header is probably invalid.
846 (setq skip (+ (point) content-length))
848 (cond ((or (= skip (point-max))
849 (= (1+ skip) (point-max)))
850 (setq end (point-max)))
851 ((looking-at "From ")
853 ((looking-at "[ \t]*\n\\(From \\)")
854 (setq end (match-beginning 1)))
858 ;; No Content-Length, so we find the beginning of the next
859 ;; article or the end of the buffer.
861 (or (nnmail-search-unix-mail-delim)
862 (goto-char (point-max))))
863 ;; Allow the backend to save the article.
866 (narrow-to-region start (point))
867 (goto-char (point-min))
869 (nnmail-check-duplication message-id func artnum-func)
870 (setq end (point-max))))
874 (defun nnmail-process-mmdf-mail-format (func artnum-func)
875 (let ((delim "^\^A\^A\^A\^A$")
878 start message-id end)
879 (goto-char (point-min))
880 (if (not (and (re-search-forward delim nil t)
882 ;; Possibly wrong format?
883 (error "Error, unknown mail format! (Possibly corrupted.)")
884 ;; Carry on until the bitter end.
887 ;; Find the end of the head.
890 (if (search-forward "\n\n" nil t)
892 ;; This will never happen, but just to be on the safe side --
893 ;; if there is no head-body delimiter, we search a bit manually.
894 (while (and (looking-at "From \\|[^ \t]+:")
898 ;; Find the Message-ID header.
899 (goto-char (point-min))
900 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
901 (setq message-id (match-string 1))
902 ;; There is no Message-ID here, so we create one.
904 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
906 (insert "Original-")))
908 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
909 (run-hooks 'nnmail-prepare-incoming-header-hook)
910 ;; Find the end of this article.
911 (goto-char (point-max))
913 (if (re-search-forward delim nil t)
915 (goto-char (point-max)))
916 ;; Allow the backend to save the article.
919 (narrow-to-region start (point))
920 (goto-char (point-min))
922 (nnmail-check-duplication message-id func artnum-func)
923 (setq end (point-max))))
928 (defun nnmail-process-maildir-mail-format (func artnum-func)
929 ;; In a maildir, every file contains exactly one mail.
930 (let ((case-fold-search t)
932 (goto-char (point-min))
933 ;; Find the end of the head.
936 (if (search-forward "\n\n" nil t)
938 ;; This will never happen, but just to be on the safe side --
939 ;; if there is no head-body delimiter, we search a bit manually.
940 (while (and (looking-at "From \\|[^ \t]+:")
944 ;; Find the Message-ID header.
945 (goto-char (point-min))
946 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
947 (setq message-id (match-string 1))
948 ;; There is no Message-ID here, so we create one.
950 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
952 (insert "Original-")))
954 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
955 (run-hooks 'nnmail-prepare-incoming-header-hook)
956 ;; Allow the backend to save the article.
959 (goto-char (point-min))
960 (nnmail-check-duplication message-id func artnum-func))
963 (defun nnmail-split-incoming (incoming func &optional exit-func
965 "Go through the entire INCOMING file and pick out each individual mail.
966 FUNC will be called with the buffer narrowed to each mail."
967 (let ( ;; If this is a group-specific split, we bind the split
968 ;; methods to just this group.
969 (nnmail-split-methods (if (and group
970 (not nnmail-resplit-incoming))
971 (list (list group ""))
972 nnmail-split-methods)))
974 ;; Insert the incoming file.
975 (set-buffer (get-buffer-create nnmail-article-buffer))
977 (let ((coding-system-for-read nnmail-incoming-coding-system))
978 (mm-insert-file-contents incoming))
980 (if (zerop (buffer-size))
982 (goto-char (point-min))
983 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
984 ;; Handle both babyl, MMDF and unix mail formats, since
985 ;; movemail will use the former when fetching from a
986 ;; mailbox, the latter when fetching from a file.
987 (cond ((or (looking-at "\^L")
988 (looking-at "BABYL OPTIONS:"))
989 (nnmail-process-babyl-mail-format func artnum-func))
990 ((looking-at "\^A\^A\^A\^A")
991 (nnmail-process-mmdf-mail-format func artnum-func))
992 ((looking-at "Return-Path:")
993 (nnmail-process-maildir-mail-format func artnum-func))
995 (nnmail-process-unix-mail-format func artnum-func))))
998 (kill-buffer (current-buffer))))))
1000 (defun nnmail-article-group (func &optional trace)
1001 "Look at the headers and return an alist of groups that match.
1002 FUNC will be called with the group name to determine the article number."
1003 (let ((methods (or nnmail-split-methods '(("bogus" ""))))
1004 (obuf (current-buffer))
1005 group-art method grp)
1006 (if (and (sequencep methods)
1007 (= (length methods) 1))
1008 ;; If there is only just one group to put everything in, we
1009 ;; just return a list with just this one method in.
1011 (list (cons (caar methods) (funcall func (caar methods)))))
1012 ;; We do actual comparison.
1014 ;; Copy the article into the work buffer.
1015 (set-buffer nntp-server-buffer)
1017 (insert-buffer-substring obuf)
1018 ;; Narrow to headers.
1020 (goto-char (point-min))
1021 (if (search-forward "\n\n" nil t)
1024 (goto-char (point-min))
1025 ;; Decode MIME headers and charsets.
1026 (when nnmail-mail-splitting-decodes
1027 (let ((mail-parse-charset nnmail-mail-splitting-charset))
1028 (mail-decode-encoded-word-region (point-min) (point-max))))
1029 ;; Fold continuation lines.
1030 (goto-char (point-min))
1031 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1032 (replace-match " " t t))
1033 ;; Nuke pathologically long headers. Since Gnus applies
1034 ;; pathologically complex regexps to the buffer, lines
1035 ;; that are looong will take longer than the Universe's
1036 ;; existence to process.
1037 (goto-char (point-min))
1039 (unless (< (move-to-column nnmail-split-header-length-limit)
1040 nnmail-split-header-length-limit)
1041 (delete-region (point) (gnus-point-at-eol)))
1044 (goto-char (point-min))
1045 (run-hooks 'nnmail-split-hook)
1046 (when (setq nnmail-split-tracing trace)
1047 (setq nnmail-split-trace nil))
1048 (if (and (symbolp nnmail-split-methods)
1049 (fboundp nnmail-split-methods))