1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news, mail
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
36 (autoload 'gnus-error "gnus-util")
37 (autoload 'gnus-buffer-live-p "gnus-util"))
40 "Reading mail with Gnus."
43 (defgroup nnmail-retrieve nil
44 "Retrieving new mail."
47 (defgroup nnmail-prepare nil
48 "Preparing (or mangling) new mail after retrival."
51 (defgroup nnmail-duplicate nil
52 "Handling of duplicate mail messages."
55 (defgroup nnmail-split nil
56 "Organizing the incomming mail in folders."
59 (defgroup nnmail-files nil
64 (defgroup nnmail-expire nil
68 (defgroup nnmail-procmail nil
69 "Interfacing with procmail and other mail agents."
72 (defgroup nnmail-various nil
73 "Various mail options."
76 (defcustom nnmail-split-methods
78 "*Incoming mail will be split according to this variable.
80 If you'd like, for instance, one mail group for mail from the
81 \"4ad-l\" mailing list, one group for junk mail and one for everything
82 else, you could do something like this:
84 (setq nnmail-split-methods
85 '((\"mail.4ad\" \"From:.*4ad\")
86 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
87 (\"mail.misc\" \"\")))
89 As you can see, this variable is a list of lists, where the first
90 element in each \"rule\" is the name of the group (which, by the way,
91 does not have to be called anything beginning with \"mail\",
92 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
93 nnmail will try to match on the header to find a fit.
95 The second element can also be a function. In that case, it will be
96 called narrowed to the headers with the first element of the rule as
97 the argument. It should return a non-nil value if it thinks that the
98 mail belongs in that group.
100 The last element should always have \"\" as the regexp.
102 This variable can also have a function as its value."
104 :type '(choice (repeat :tag "Alist" (group (string :tag "Name") regexp))
105 (function-item nnmail-split-fancy)
106 (function :tag "Other")))
108 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
109 (defcustom nnmail-crosspost t
110 "If non-nil, do crossposting if several split methods match the mail.
111 If nil, the first match found will be used."
115 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
116 (defcustom nnmail-keep-last-article nil
117 "If non-nil, nnmail will never delete/move a group's last article.
118 It can be marked expirable, so it will be deleted when it is no longer last.
120 You may need to set this variable if other programs are putting
121 new mail into folder numbers that Gnus has marked as expired."
122 :group 'nnmail-procmail
123 :group 'nnmail-various
126 (defcustom nnmail-use-long-file-names nil
127 "If non-nil the mail backends will use long file and directory names.
128 If nil, groups like \"mail.misc\" will end up in directories like
133 (defcustom nnmail-default-file-modes 384
134 "Set the mode bits of all new mail files to this integer."
138 (defcustom nnmail-expiry-wait 7
139 "*Expirable articles that are older than this will be expired.
140 This variable can either be a number (which will be interpreted as a
141 number of days) -- this doesn't have to be an integer. This variable
142 can also be `immediate' and `never'."
143 :group 'nnmail-expire
144 :type '(choice (const immediate)
145 (integer :tag "days")
148 (defcustom nnmail-expiry-wait-function nil
149 "Variable that holds function to specify how old articles should be before they are expired.
150 The function will be called with the name of the group that the
151 expiry is to be performed in, and it should return an integer that
152 says how many days an article can be stored before it is considered
153 \"old\". It can also return the values `never' and `immediate'.
157 \(setq nnmail-expiry-wait-function
159 (cond ((string-match \"private\" newsgroup) 31)
160 ((string-match \"junk\" newsgroup) 1)
161 ((string-match \"important\" newsgroup) 'never)
163 :group 'nnmail-expire
164 :type '(choice (const :tag "nnmail-expiry-wait" nil)
165 (function :format "%v" nnmail-)))
167 (defcustom nnmail-cache-accepted-message-ids nil
168 "If non-nil, put Message-IDs of Gcc'd articles into the duplicate cache."
172 (defcustom nnmail-spool-file
174 (concat "/usr/spool/mail/" (user-login-name)))
175 "*Where the mail backends will look for incoming mail.
176 This variable is \"/usr/spool/mail/$user\" by default.
177 If this variable is nil, no mail backends will read incoming mail.
178 If this variable is a list, all files mentioned in this list will be
179 used as incoming mailboxes.
180 If this variable is a directory (i. e., it's name ends with a \"/\"),
181 treat all files in that directory as incoming spool files."
183 :type '(choice (file :tag "File")
184 (repeat :tag "Files" file)))
186 (defcustom nnmail-crash-box "~/.gnus-crash-box"
187 "File where Gnus will store mail while processing it."
191 (defcustom nnmail-use-procmail nil
192 "*If non-nil, the mail backends will look in `nnmail-procmail-directory' for spool files.
193 The file(s) in `nnmail-spool-file' will also be read."
194 :group 'nnmail-procmail
197 (defcustom nnmail-procmail-directory "~/incoming/"
198 "*When using procmail (and the like), incoming mail is put in this directory.
199 The Gnus mail backends will read the mail from this directory."
200 :group 'nnmail-procmail
203 (defcustom nnmail-procmail-suffix "\\.spool"
204 "*Suffix of files created by procmail (and the like).
205 This variable might be a suffix-regexp to match the suffixes of
206 several files - eg. \".spool[0-9]*\"."
207 :group 'nnmail-procmail
210 (defcustom nnmail-resplit-incoming nil
211 "*If non-nil, re-split incoming procmail sorted mail."
212 :group 'nnmail-procmail
215 (defcustom nnmail-delete-file-function 'delete-file
216 "Function called to delete files in some mail backends."
220 (defcustom nnmail-crosspost-link-function
221 (if (string-match "windows-nt\\|emx" (symbol-name system-type))
224 "*Function called to create a copy of a file.
225 This is `add-name-to-file' by default, which means that crossposts
226 will use hard links. If your file system doesn't allow hard
227 links, you could set this variable to `copy-file' instead."
229 :type '(radio (function-item add-name-to-file)
230 (function-item copy-file)
231 (function :tag "Other")))
233 (defcustom nnmail-movemail-program "movemail"
234 "*A command to be executed to move mail from the inbox.
235 The default is \"movemail\".
237 This can also be a function. In that case, the function will be
238 called with two parameters -- the name of the INBOX file, and the file
241 :group 'nnmail-retrieve
244 (defcustom nnmail-movemail-args nil
245 "*Extra arguments to give to `nnmail-movemail-program' to move mail from the inbox.
248 :group 'nnmail-retrieve
251 (defcustom nnmail-pop-password-required nil
252 "*Non-nil if a password is required when reading mail using POP."
253 :group 'nnmail-retrieve
256 (defcustom nnmail-read-incoming-hook
257 (if (eq system-type 'windows-nt)
258 '(nnheader-ms-strip-cr)
260 "*Hook that will be run after the incoming mail has been transferred.
261 The incoming mail is moved from `nnmail-spool-file' (which normally is
262 something like \"/usr/spool/mail/$user\") to the user's home
263 directory. This hook is called after the incoming mail box has been
264 emptied, and can be used to call any mail box programs you have
265 running (\"xwatch\", etc.)
269 \(add-hook 'nnmail-read-incoming-hook
271 (start-process \"mailsend\" nil
272 \"/local/bin/mailsend\" \"read\" \"mbox\")))
274 If you have xwatch running, this will alert it that mail has been
277 If you use `display-time', you could use something like this:
279 \(add-hook 'nnmail-read-incoming-hook
281 ;; Update the displayed time, since that will clear out
282 ;; the flag that says you have mail.
283 (when (eq (process-status \"display-time\") 'run)
284 (display-time-filter display-time-process \"\"))))"
285 :group 'nnmail-prepare
288 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
289 (defcustom nnmail-prepare-incoming-hook nil
290 "Hook called before treating incoming mail.
291 The hook is run in a buffer with all the new, incoming mail."
292 :group 'nnmail-prepare
295 (defcustom nnmail-prepare-incoming-header-hook nil
296 "Hook called narrowed to the headers of each message.
297 This can be used to remove excessive spaces (and stuff like
298 that) from the headers before splitting and saving the messages."
299 :group 'nnmail-prepare
302 (defcustom nnmail-prepare-incoming-message-hook nil
303 "Hook called narrowed to each message."
304 :group 'nnmail-prepare
307 (defcustom nnmail-list-identifiers nil
308 "Regexp that matches list identifiers to be removed.
309 This can also be a list of regexps."
310 :group 'nnmail-prepare
311 :type '(choice (const :tag "none" nil)
313 (repeat :value (".*") regexp)))
315 (defcustom nnmail-pre-get-new-mail-hook nil
316 "Hook called just before starting to handle new incoming mail."
317 :group 'nnmail-retrieve
320 (defcustom nnmail-post-get-new-mail-hook nil
321 "Hook called just after finishing handling new incoming mail."
322 :group 'nnmail-retrieve
325 (defcustom nnmail-split-hook nil
326 "Hook called before deciding where to split an article.
327 The functions in this hook are free to modify the buffer
328 contents in any way they choose -- the buffer contents are
329 discarded after running the split process."
333 ;; Suggested by Mejia Pablo J <pjm9806@usl.edu>.
334 (defcustom nnmail-tmp-directory nil
335 "*If non-nil, use this directory for temporary storage.
336 Used when reading incoming mail."
338 :group 'nnmail-retrieve
339 :type '(choice (const :tag "default" nil)
340 (directory :format "%v")))
342 (defcustom nnmail-large-newsgroup 50
343 "*The number of the articles which indicates a large newsgroup.
344 If the number of the articles is greater than the value, verbose
345 messages will be shown to indicate the current status."
346 :group 'nnmail-various
349 (defcustom nnmail-split-fancy "mail.misc"
350 "Incoming mail can be split according to this fancy variable.
351 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
353 The format of this variable is SPLIT, where SPLIT can be one of
356 GROUP: Mail will be stored in GROUP (a string).
358 \(FIELD VALUE SPLIT): If the message field FIELD (a regexp) contains
359 VALUE (a regexp), store the messages as specified by SPLIT.
361 \(| SPLIT...): Process each SPLIT expression until one of them matches.
362 A SPLIT expression is said to match if it will cause the mail
363 message to be stored in one or more groups.
365 \(& SPLIT...): Process each SPLIT expression.
367 \(: FUNCTION optional args): Call FUNCTION with the optional args, in
368 the buffer containing the message headers. The return value FUNCTION
369 should be a split, which is then recursively processed.
371 FIELD must match a complete field name. VALUE must match a complete
372 word according to the `nnmail-split-fancy-syntax-table' syntax table.
373 You can use \".*\" in the regexps to match partial field names or words.
375 FIELD and VALUE can also be lisp symbols, in that case they are expanded
376 as specified in `nnmail-split-abbrev-alist'.
378 GROUP can contain \\& and \\N which will substitute from matching
379 \\(\\) patterns in the previous VALUE.
383 \(setq nnmail-split-methods 'nnmail-split-fancy
385 ;; Messages from the mailer daemon are not crossposted to any of
386 ;; the ordinary groups. Warnings are put in a separate group
388 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
390 ;; Non-error messages are crossposted to all relevant
391 ;; groups, but we don't crosspost between the group for the
392 ;; (ding) list and the group for other (ding) related mail.
393 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
394 (\"subject\" \"ding\" \"ding.misc\"))
395 ;; Other mailing lists...
396 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
397 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
399 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
400 ;; Unmatched mail goes to the catch all group.
406 (defcustom nnmail-split-abbrev-alist
407 '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
408 (mail . "mailer-daemon\\|postmaster\\|uucp")
409 (to . "to\\|cc\\|apparently-to\\|resent-to\\|resent-cc")
410 (from . "from\\|sender\\|resent-from")
411 (nato . "to\\|cc\\|resent-to\\|resent-cc")
412 (naany . "from\\|to\\|cc\\|sender\\|resent-from\\|resent-to\\|resent-cc"))
413 "*Alist of abbreviations allowed in `nnmail-split-fancy'."
415 :type '(repeat (cons :format "%v" symbol regexp)))
417 (defcustom nnmail-delete-incoming nil
418 "*If non-nil, the mail backends will delete incoming files after
420 :group 'nnmail-retrieve
423 (defcustom nnmail-message-id-cache-length 1000
424 "*The approximate number of Message-IDs nnmail will keep in its cache.
425 If this variable is nil, no checking on duplicate messages will be
427 :group 'nnmail-duplicate
428 :type '(choice (const :tag "disable" nil)
429 (integer :format "%v")))
431 (defcustom nnmail-message-id-cache-file "~/.nnmail-cache"
432 "*The file name of the nnmail Message-ID cache."
433 :group 'nnmail-duplicate
437 (defcustom nnmail-treat-duplicates 'warn
438 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
439 Three values are legal: nil, which means that nnmail is not to keep a
440 Message-ID cache; `warn', which means that nnmail should insert extra
441 headers to warn the user about the duplication (this is the default);
442 and `delete', which means that nnmail will delete duplicated mails.
444 This variable can also be a function. It will be called from a buffer
445 narrowed to the article in question with the Message-ID as a
446 parameter. It should return nil, `warn' or `delete'."
447 :group 'nnmail-duplicate
448 :type '(choice (const :tag "off" nil)
452 (defcustom nnmail-extra-headers nil
453 "*Extra headers to parse."
455 :type '(repeat symbol))
457 ;;; Internal variables.
459 (defvar nnmail-split-history nil
460 "List of group/article elements that say where the previous split put messages.")
462 (defvar nnmail-current-spool nil)
464 (defvar nnmail-pop-password nil
465 "*Password to use when reading mail from a POP server, if required.")
467 (defvar nnmail-split-fancy-syntax-table nil
468 "Syntax table used by `nnmail-split-fancy'.")
469 (unless (syntax-table-p nnmail-split-fancy-syntax-table)
470 (setq nnmail-split-fancy-syntax-table
471 (copy-syntax-table (standard-syntax-table)))
472 ;; support the %-hack
473 (modify-syntax-entry ?\% "." nnmail-split-fancy-syntax-table))
475 (defvar nnmail-prepare-save-mail-hook nil
476 "Hook called before saving mail.")
478 (defvar nnmail-moved-inboxes nil
479 "List of inboxes that have been moved.")
481 (defvar nnmail-internal-password nil)
483 (defvar nnmail-split-tracing nil)
484 (defvar nnmail-split-trace nil)
488 (defconst nnmail-version "nnmail 1.0"
493 (defun nnmail-request-post (&optional server)
494 (mail-send-and-exit nil))
496 (defvar nnmail-file-coding-system 'binary
497 "Coding system used in nnmail.")
499 (defvar nnmail-file-coding-system-1
500 (if (string-match "nt" system-configuration)
501 'raw-text-dos 'binary)
502 "Another coding system used in nnmail.")
504 (defun nnmail-find-file (file)
505 "Insert FILE in server buffer safely."
506 (set-buffer nntp-server-buffer)
507 (delete-region (point-min) (point-max))
508 (let ((format-alist nil)
509 (after-insert-file-functions nil))
511 (let ((auto-mode-alist (nnheader-auto-mode-alist))
512 (pathname-coding-system nnmail-file-coding-system))
513 (insert-file-contents-as-coding-system
514 nnmail-file-coding-system file)
518 (defvar nnmail-pathname-coding-system
520 "*Coding system for pathname.")
522 (defun nnmail-group-pathname (group dir &optional file)
523 "Make pathname for GROUP."
525 (let ((dir (file-name-as-directory (expand-file-name dir))))
526 (setq group (nnheader-translate-file-chars group))
527 ;; If this directory exists, we use it directly.
528 (if (or nnmail-use-long-file-names
529 (file-directory-p (concat dir group)))
530 (concat dir group "/")
531 ;; If not, we translate dots into slashes.
533 (encode-coding-string
534 (nnheader-replace-chars-in-string group ?. ?/)
535 nnmail-pathname-coding-system)
539 ;; Function rewritten from rmail.el.
540 (defun nnmail-move-inbox (inbox)
541 "Move INBOX to `nnmail-crash-box'."
542 (if (not (file-writable-p nnmail-crash-box))
543 (gnus-error 1 "Can't write to crash box %s. Not moving mail"
545 ;; If the crash box exists and is empty, we delete it.
546 (when (and (file-exists-p nnmail-crash-box)
547 (zerop (nnheader-file-size (file-truename nnmail-crash-box))))
548 (delete-file nnmail-crash-box))
549 (let ((tofile (file-truename (expand-file-name nnmail-crash-box)))
550 (popmail (string-match "^po:" inbox))
551 movemail errors result)
553 (setq inbox (file-truename (expand-file-name inbox)))
555 ;; On some systems, /usr/spool/mail/foo is a directory
556 ;; and the actual inbox is /usr/spool/mail/foo/foo.
557 (when (file-directory-p inbox)
558 (setq inbox (expand-file-name (user-login-name) inbox))))
559 (if (member inbox nnmail-moved-inboxes)
560 ;; We don't try to move an already moved inbox.
564 (when (and nnmail-pop-password
565 (not nnmail-internal-password))
566 (setq nnmail-internal-password nnmail-pop-password))
567 (when (and nnmail-pop-password-required
568 (not nnmail-internal-password))
569 (setq nnmail-internal-password
571 (format "Password for %s: "
572 (substring inbox (+ popmail 3))))))
573 (nnheader-message 5 "Getting mail from the post office..."))
574 (when (or (and (file-exists-p tofile)
575 (/= 0 (nnheader-file-size tofile)))
576 (and (file-exists-p inbox)
577 (/= 0 (nnheader-file-size inbox))))
578 (nnheader-message 5 "Getting mail from %s..." inbox)))
579 ;; Set TOFILE if have not already done so, and
580 ;; rename or copy the file INBOX to TOFILE if and as appropriate.
582 ((file-exists-p tofile)
583 ;; The crash box exists already.
586 (not (file-exists-p inbox)))
587 ;; There is no inbox.
590 ;; If getting from mail spool directory, use movemail to move
591 ;; rather than just renaming, so as to interlock with the
595 (setq errors (generate-new-buffer " *nnmail loss*"))
596 (buffer-disable-undo errors)
597 (if (nnheader-functionp nnmail-movemail-program)
600 (funcall nnmail-movemail-program inbox tofile)
605 (insert (prin1-to-string err))
607 (let ((default-directory "/"))
614 nnmail-movemail-program exec-directory)
615 nil errors nil inbox tofile)
616 (when nnmail-internal-password
617 (list nnmail-internal-password))
618 (when nnmail-movemail-args
619 nnmail-movemail-args))))))
620 (push inbox nnmail-moved-inboxes)
621 (if (and (not (buffer-modified-p errors))
623 ;; No output => movemail won
626 (when (file-exists-p tofile)
627 (set-file-modes tofile nnmail-default-file-modes))))
629 ;; There may be a warning about older revisions. We
631 (goto-char (point-min))
632 (if (search-forward "older revision" nil t)
635 (when (file-exists-p tofile)
637 tofile nnmail-default-file-modes))))
638 ;; Probably a real error.
639 ;; We nix out the password in case the error
640 ;; was because of a wrong password being given.
641 (setq nnmail-internal-password nil)
642 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
643 (goto-char (point-max))
644 (skip-chars-backward " \t")
645 (delete-region (point) (point-max))
646 (goto-char (point-min))
647 (when (looking-at "movemail: ")
648 (delete-region (point-min) (match-end 0)))
650 (format "movemail: %s (%d return). Continue? "
651 (buffer-string) result))
652 (error "%s" (buffer-string)))
653 (setq tofile nil)))))))
654 (nnheader-message 5 "Getting mail from %s...done" inbox)
657 (kill-buffer errors))
660 (defun nnmail-get-active ()
661 "Returns an assoc of group names and active ranges.
662 nn*-request-list should have been called before calling this function."
664 ;; Go through all groups from the active list.
666 (set-buffer nntp-server-buffer)
667 (goto-char (point-min))
668 (while (re-search-forward
669 "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
670 ;; We create an alist with `(GROUP (LOW . HIGH))' elements.
671 (push (list (match-string 1)
672 (cons (string-to-int (match-string 3))
673 (string-to-int (match-string 2))))
677 (defvar nnmail-active-file-coding-system 'binary
678 "*Coding system for active file.")
680 (defun nnmail-save-active (group-assoc file-name)
681 "Save GROUP-ASSOC in ACTIVE-FILE."
682 (let ((coding-system-for-write nnmail-active-file-coding-system))
684 (with-temp-file file-name
685 (nnmail-generate-active group-assoc)))))
687 (defun nnmail-generate-active (alist)
688 "Generate an active file from group-alist ALIST."
691 (while (setq group (pop alist))
692 (insert (format "%s %d %d y\n" (car group) (cdadr group)
695 (defun nnmail-get-split-group (file group)
696 "Find out whether this FILE is to be split into GROUP only.
697 If GROUP is non-nil and we are using procmail, return the group name
698 only when the file is the correct procmail file. When GROUP is nil,
699 return nil if FILE is a spool file or the procmail group for which it
700 is a spool. If not using procmail, return GROUP."
701 (if (or (eq nnmail-spool-file 'procmail)
703 (if (string-match (concat "^" (regexp-quote
705 (file-name-as-directory
706 nnmail-procmail-directory)))
708 nnmail-procmail-suffix "$")
709 (expand-file-name file))
710 (let ((procmail-group (substring (expand-file-name file)
714 (if (string-equal group procmail-group)
721 (defun nnmail-process-babyl-mail-format (func artnum-func)
722 (let ((case-fold-search t)
723 start message-id content-length do-search end)
725 (goto-char (point-min))
727 "
\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
728 (goto-char (match-end 0))
729 (delete-region (match-beginning 0) (match-end 0))
733 ;; Skip all the headers in case there are more "From "s...
734 (or (search-forward "\n\n" nil t)
735 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
736 (search-forward "
\1f\f"))
738 ;; Unquote the ">From " line, if any.
739 (goto-char (point-min))
740 (when (looking-at ">From ")
741 (replace-match "X-From-Line: ") )
742 (run-hooks 'nnmail-prepare-incoming-header-hook)
743 (goto-char (point-max))
744 ;; Find the Message-ID header.
746 (if (re-search-backward
747 "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]*>\\)" nil t)
748 (setq message-id (buffer-substring (match-beginning 1)
750 ;; There is no Message-ID here, so we create one.
752 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
754 (insert "Original-")))
756 (insert "Message-ID: " (setq message-id (nnmail-message-id))
758 ;; Look for a Content-Length header.
759 (if (not (save-excursion
760 (and (re-search-backward
761 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
762 (setq content-length (string-to-int
766 ;; We destroy the header, since none of
767 ;; the backends ever use it, and we do not
768 ;; want to confuse other mailers by having
769 ;; a (possibly) faulty header.
770 (progn (insert "X-") t))))
773 (if (or (= (+ (point) content-length) (point-max))
775 (goto-char (+ (point) content-length))
778 (goto-char (+ (point) content-length))
779 (setq do-search nil))
782 ;; Go to the beginning of the next article - or to the end
785 (if (re-search-forward "^
\1f" nil t)
786 (goto-char (match-beginning 0))
787 (goto-char (1- (point-max)))))
788 (delete-char 1) ; delete ^_
791 (narrow-to-region start (point))
792 (goto-char (point-min))
793 (nnmail-check-duplication message-id func artnum-func)
794 (setq end (point-max))))
797 (defsubst nnmail-search-unix-mail-delim ()
798 "Put point at the beginning of the next Unix mbox message."
799 ;; Algorithm used to find the the next article in the
800 ;; brain-dead Unix mbox format:
802 ;; 1) Search for "^From ".
803 ;; 2) If we find it, then see whether the previous
804 ;; line is blank and the next line looks like a header.
805 ;; Then it's possible that this is a mail delim, and we use it.
806 (let ((case-fold-search nil)
809 (if (not (re-search-forward "^From " nil t))
813 (when (and (or (bobp)
816 (eq (char-after) ?\n)))
819 (while (looking-at ">From \\|From ")
821 (looking-at "[^ \n\t:]+[ \n\t]*:")))
822 (setq found 'yes)))))
826 (defun nnmail-search-unix-mail-delim-backward ()
827 "Put point at the beginning of the current Unix mbox message."
828 ;; Algorithm used to find the the next article in the
829 ;; brain-dead Unix mbox format:
831 ;; 1) Search for "^From ".
832 ;; 2) If we find it, then see whether the previous
833 ;; line is blank and the next line looks like a header.
834 ;; Then it's possible that this is a mail delim, and we use it.
835 (let ((case-fold-search nil)
838 (if (not (re-search-backward "^From " nil t))
842 (when (and (or (bobp)
845 (eq (char-after) ?\n)))
848 (while (looking-at ">From \\|From ")
850 (looking-at "[^ \n\t:]+[ \n\t]*:")))
851 (setq found 'yes)))))
855 (defun nnmail-process-unix-mail-format (func artnum-func)
856 (let ((case-fold-search t)
857 start message-id content-length end skip head-end)
858 (goto-char (point-min))
859 (if (not (and (re-search-forward "^From " nil t)
860 (goto-char (match-beginning 0))))
861 ;; Possibly wrong format?
863 (pop-to-buffer (nnheader-find-file-noselect nnmail-current-spool))
864 (error "Error, unknown mail format! (Possibly corrupted.)"))
865 ;; Carry on until the bitter end.
869 ;; Find the end of the head.
872 (if (search-forward "\n\n" nil t)
874 ;; This will never happen, but just to be on the safe side --
875 ;; if there is no head-body delimiter, we search a bit manually.
876 (while (and (looking-at "From \\|[^ \t]+:")
880 ;; Find the Message-ID header.
881 (goto-char (point-min))
882 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
883 (setq message-id (match-string 1))
885 (when (re-search-forward "^Message-ID[ \t]*:" nil t)
887 (insert "Original-")))
888 ;; There is no Message-ID here, so we create one.
890 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
891 ;; Look for a Content-Length header.
892 (goto-char (point-min))
893 (if (not (re-search-forward
894 "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
895 (setq content-length nil)
896 (setq content-length (string-to-int (match-string 1)))
897 ;; We destroy the header, since none of the backends ever
898 ;; use it, and we do not want to confuse other mailers by
899 ;; having a (possibly) faulty header.
902 (run-hooks 'nnmail-prepare-incoming-header-hook)
903 ;; Find the end of this article.
904 (goto-char (point-max))
906 (setq head-end (point))
907 ;; We try the Content-Length value. The idea: skip over the header
908 ;; separator, then check what happens content-length bytes into the
909 ;; message body. This should be either the end ot the buffer, the
910 ;; message separator or a blank line followed by the separator.
911 ;; The blank line should probably be deleted. If neither of the
912 ;; three is met, the content-length header is probably invalid.
915 (setq skip (+ (point) content-length))
917 (cond ((or (= skip (point-max))
918 (= (1+ skip) (point-max)))
919 (setq end (point-max)))
920 ((looking-at "From ")
922 ((looking-at "[ \t]*\n\\(From \\)")
923 (setq end (match-beginning 1)))
927 ;; No Content-Length, so we find the beginning of the next
928 ;; article or the end of the buffer.
930 (or (nnmail-search-unix-mail-delim)
931 (goto-char (point-max))))
932 ;; Allow the backend to save the article.
935 (narrow-to-region start (point))
936 (goto-char (point-min))
937 (nnmail-check-duplication message-id func artnum-func)
938 (setq end (point-max))))
941 (defun nnmail-process-mmdf-mail-format (func artnum-func)
942 (let ((delim "^\^A\^A\^A\^A$")
944 start message-id end)
945 (goto-char (point-min))
946 (if (not (and (re-search-forward delim nil t)
948 ;; Possibly wrong format?
950 (pop-to-buffer (nnheader-find-file-noselect nnmail-current-spool))
951 (error "Error, unknown mail format! (Possibly corrupted.)"))
952 ;; Carry on until the bitter end.
955 ;; Find the end of the head.
958 (if (search-forward "\n\n" nil t)
960 ;; This will never happen, but just to be on the safe side --
961 ;; if there is no head-body delimiter, we search a bit manually.
962 (while (and (looking-at "From \\|[^ \t]+:")
966 ;; Find the Message-ID header.
967 (goto-char (point-min))
968 (if (re-search-forward "^Message-ID[ \t]*:[ \n\t]*\\(<[^>]+>\\)" nil t)
969 (setq message-id (match-string 1))
970 ;; There is no Message-ID here, so we create one.
972 (when (re-search-backward "^Message-ID[ \t]*:" nil t)
974 (insert "Original-")))
976 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
977 (run-hooks 'nnmail-prepare-incoming-header-hook)
978 ;; Find the end of this article.
979 (goto-char (point-max))
981 (if (re-search-forward delim nil t)
983 (goto-char (point-max)))
984 ;; Allow the backend to save the article.
987 (narrow-to-region start (point))
988 (goto-char (point-min))
989 (nnmail-check-duplication message-id func artnum-func)
990 (setq end (point-max))))
994 (defun nnmail-split-incoming (incoming func &optional exit-func
996 "Go through the entire INCOMING file and pick out each individual mail.
997 FUNC will be called with the buffer narrowed to each mail."
998 (let (;; If this is a group-specific split, we bind the split
999 ;; methods to just this group.
1000 (nnmail-split-methods (if (and group
1001 (or (eq nnmail-spool-file 'procmail)
1002 nnmail-use-procmail)
1003 (not nnmail-resplit-incoming))
1004 (list (list group ""))
1005 nnmail-split-methods)))
1007 ;; Insert the incoming file.
1008 (set-buffer (get-buffer-create " *nnmail incoming*"))
1010 (nnheader-insert-file-contents incoming)
1011 (unless (zerop (buffer-size))
1012 (goto-char (point-min))
1013 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
1014 ;; Handle both babyl, MMDF and unix mail formats, since movemail will
1015 ;; use the former when fetching from a mailbox, the latter when
1016 ;; fetching from a file.
1017 (cond ((or (looking-at "\^L")
1018 (looking-at "BABYL OPTIONS:"))
1019 (nnmail-process-babyl-mail-format func artnum-func))
1020 ((looking-at "\^A\^A\^A\^A")
1021 (nnmail-process-mmdf-mail-format func artnum-func))
1023 (nnmail-process-unix-mail-format func artnum-func))))
1025 (funcall exit-func))
1026 (kill-buffer (current-buffer)))))
1028 (defun nnmail-article-group (func &optional trace)
1029 "Look at the headers and return an alist of groups that match.
1030 FUNC will be called with the group name to determine the article number."
1031 (let ((methods nnmail-split-methods)
1032 (obuf (current-buffer))
1034 end group-art method regrepp)
1035 (if (and (sequencep methods)
1036 (= (length methods) 1))
1037 ;; If there is only just one group to put everything in, we
1038 ;; just return a list with just this one method in.
1040 (list (cons (caar methods) (funcall func (caar methods)))))
1041 ;; We do actual comparison.
1045 (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
1046 (set-buffer nntp-server-buffer)
1048 ;; Copy the headers into the work buffer.
1049 (insert-buffer-substring obuf beg end)
1050 ;; Fold continuation lines.
1051 (goto-char (point-min))
1052 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1053 (replace-match " " t t))
1054 ;; Nuke pathologically long headers. Since Gnus applies
1055 ;; pathologically complex regexps to the buffer, lines
1056 ;; that are looong will take longer than the Universe's
1057 ;; existence to process.
1058 (goto-char (point-min))
1061 (if (> (current-column) 1024)
1065 (goto-char (point-min))
1066 (run-hooks 'nnmail-split-hook)
1067 (when (setq nnmail-split-tracing trace)
1068 (setq nnmail-split-trace nil))
1069 (if (and (symbolp nnmail-split-methods)
1070 (fboundp nnmail-split-methods))
1073 ;; `nnmail-split-methods' is a function, so we
1074 ;; just call this function here and use the
1076 (or (funcall nnmail-split-methods)
1080 "Error in `nnmail-split-methods'; using `bogus' mail group")
1083 (setq split (gnus-remove-duplicates split))
1084 ;; The article may be "cross-posted" to `junk'. What
1085 ;; to do? Just remove the `junk' spec. Don't really
1086 ;; see anything else to do...
1088 (while (setq elem (car (memq 'junk split)))
1089 (setq split (delq elem split))))
1093 (lambda (group) (cons group (funcall func group)))
1095 ;; Go through the split methods to find a match.
1097 (or nnmail-crosspost
1099 (goto-char (point-max))
1100 (setq method (pop methods)
1103 (not (equal "" (nth 1 method))))
1106 (if (stringp (nth 1 method))
1109 (string-match "\\\\[0-9&]" (car method)))
1110 (re-search-backward (cadr method) nil t))
1111 ;; Function to say whether this is a match.
1112 (funcall (nth 1 method) (car method))))
1113 ;; Don't enter the article into the same
1115 (not (assoc (car method) group-art)))
1116 (push (cons (if regrepp
1117 (nnmail-expand-newtext (car method))
1119 (funcall func (car method)))
1121 ;; This is the final group, which is used as a
1125 (list (cons (car method)
1126 (funcall func (car method)))))))))
1127 ;; Produce a trace if non-empty.
1128 (when (and trace nnmail-split-trace)
1129 (let ((trace (nreverse nnmail-split-trace))
1130 (restore (current-buffer)))
1131 (nnheader-set-temp-buffer "*Split Trace*")
1134 (insert (car trace) "\n")
1135 (setq trace (cdr trace)))
1136 (goto-char (point-min))
1137 (gnus-configure-windows 'split-trace)
1138 (set-buffer restore)))
1139 ;; See whether the split methods returned `junk'.
1140 (if (equal group-art '(junk))
1142 ;; The article may be "cross-posted" to `junk'. What
1143 ;; to do? Just remove the `junk' spec. Don't really
1144 ;; see anything else to do...
1146 (while (setq elem (car (memq 'junk group-art)))
1147 (setq group-art (delq elem group-art)))
1148 (nreverse group-art)))))))
1150 (defun nnmail-insert-lines ()
1151 "Insert how many lines there are in the body of the mail.
1152 Return the number of characters in the body."
1155 (goto-char (point-min))
1156 (when (search-forward "\n\n" nil t)
1157 (setq chars (- (point-max) (point)))
1158 (setq lines (count-lines (point) (point-max)))
1161 (when (re-search-backward "^Lines: " nil t)
1162 (delete-region (point) (progn (forward-line 1) (point)))))
1164 (insert (format "Lines: %d\n" (max lines 0)))
1167 (defun nnmail-insert-xref (group-alist)
1168 "Insert an Xref line based on the (group . article) alist."
1170 (goto-char (point-min))
1171 (when (search-forward "\n\n" nil t)
1173 (when (re-search-backward "^Xref: " nil t)
1174 (delete-region (match-beginning 0)
1175 (progn (forward-line 1) (point))))
1176 (insert (format "Xref: %s" (system-name)))
1178 (insert (format " %s:%d"
1179 (encode-coding-string
1181 nnmail-pathname-coding-system)
1182 (cdar group-alist)))
1183 (setq group-alist (cdr group-alist)))
1186 ;;; Message washing functions
1188 (defun nnmail-remove-leading-whitespace ()
1189 "Remove excessive whitespace from all headers."
1190 (goto-char (point-min))
1191 (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
1192 (replace-match "\\1" t)))
1194 (defun nnmail-remove-list-identifiers ()
1195 "Remove list identifiers from Subject headers."
1196 (let ((regexp (if (stringp nnmail-list-identifiers) nnmail-list-identifiers
1197 (mapconcat 'identity nnmail-list-identifiers "\\|"))))
1199 (goto-char (point-min))
1200 (when (re-search-forward
1201 (concat "^Subject: +\\(Re: +\\)?\\(" regexp "\\) *")
1203 (delete-region (match-beginning 2) (match-end 0))))))
1205 (defun nnmail-remove-tabs ()
1206 "Translate TAB characters into SPACE characters."
1207 (subst-char-in-region (point-min) (point-max) ?\t ? t))
1209 ;;; Utility functions
1211 (defun nnmail-make-complex-temp-name (prefix)
1212 (let ((newname (make-temp-name prefix))
1214 (while (file-exists-p newname)
1215 (setq newprefix (concat newprefix "x"))
1216 (setq newname (make-temp-name newprefix)))
1219 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
1221 (defun nnmail-split-fancy ()
1222 "Fancy splitting method.
1223 See the documentation for the variable `nnmail-split-fancy' for documentation."
1224 (let ((syntab (syntax-table)))
1227 (set-syntax-table nnmail-split-fancy-syntax-table)
1228 (nnmail-split-it nnmail-split-fancy))
1229 (set-syntax-table syntab))))
1231 (defvar nnmail-split-cache nil)
1232 ;; Alist of split expressions their equivalent regexps.
1234 (defun nnmail-split-it (split)
1235 ;; Return a list of groups matching SPLIT.
1242 ;; A group name. Do the \& and \N subs into the string.
1244 (when nnmail-split-tracing
1245 (push (format "\"%s\"" split) nnmail-split-trace))
1246 (list (nnmail-expand-newtext split)))
1248 ;; Junk the message.
1250 (when nnmail-split-tracing
1251 (push "junk" nnmail-split-trace))
1254 ;; Builtin & operation.
1255 ((eq (car split) '&)
1256 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
1258 ;; Builtin | operation.
1259 ((eq (car split) '|)
1261 (while (and (not done) (cdr split))
1262 (setq split (cdr split)
1263 done (nnmail-split-it (car split))))
1266 ;; Builtin : operation.
1267 ((eq (car split) ':)
1268 (nnmail-split-it (save-excursion (eval (cdr split)))))
1270 ;; Check the cache for the regexp for this split.
1271 ((setq cached-pair (assq split nnmail-split-cache))
1272 (goto-char (point-max))
1273 ;; FIX FIX FIX problem with re-search-backward is that if you have
1274 ;; a split: (from "foo-\\(bar\\|baz\\)@gnus.org "mail.foo.\\1")
1275 ;; and someone mails a message with 'To: foo-bar@gnus.org' and
1276 ;; 'CC: foo-baz@gnus.org', we'll pick 'mail.foo.baz' as the group
1277 ;; if the cc line is a later header, even though the other choice
1278 ;; is probably better. Also, this routine won't do a crosspost
1279 ;; when there are two different matches.
1280 ;; I guess you could just make this more determined, and it could
1281 ;; look for still more matches prior to this one, and recurse
1282 ;; on each of the multiple matches hit. Of course, then you'd
1283 ;; want to make sure that nnmail-article-group or nnmail-split-fancy
1284 ;; removed duplicates, since there might be more of those.
1285 ;; I guess we could also remove duplicates in the & split case, since
1286 ;; that's the only thing that can introduce them.
1287 (when (re-search-backward (cdr cached-pair) nil t)
1288 (when nnmail-split-tracing
1289 (push (cdr cached-pair) nnmail-split-trace))
1290 ;; Someone might want to do a \N sub on this match, so get the
1291 ;; correct match positions.
1292 (goto-char (match-end 0))
1293 (let ((value (nth 1 split)))
1294 (re-search-backward (if (symbolp value)
1295 (cdr (assq value nnmail-split-abbrev-alist))
1298 (nnmail-split-it (nth 2 split))))
1300 ;; Not in cache, compute a regexp for the field/value pair.
1302 (let* ((field (nth 0 split))
1303 (value (nth 1 split))
1304 (regexp (concat "^\\(\\("
1306 (cdr (assq field nnmail-split-abbrev-alist))
1310 (cdr (assq value nnmail-split-abbrev-alist))
1313 (push (cons split regexp) nnmail-split-cache)
1314 ;; Now that it's in the cache, just call nnmail-split-it again
1315 ;; on the same split, which will find it immediately in the cache.
1316 (nnmail-split-it split))))))
1318 (defun nnmail-expand-newtext (newtext)
1319 (let ((len (length newtext))
1321 c expanded beg N did-expand)
1324 (while (and (< pos len)
1325 (not (= (aref newtext pos) ?\\)))
1326 (setq pos (1+ pos)))
1328 (push (substring newtext beg pos) expanded))
1330 ;; We hit a \; expand it.
1333 c (aref newtext pos))
1334 (if (not (or (= c ?\&)
1337 ;; \ followed by some character we don't expand.
1338 (push (char-to-string c) expanded)
1343 (when (match-beginning N)
1344 (push (buffer-substring (match-beginning N) (match-end N))
1346 (setq pos (1+ pos)))
1348 (apply 'concat (nreverse expanded))
1351 ;; Get a list of spool files to read.
1352 (defun nnmail-get-spool-files (&optional group)
1353 (if (null nnmail-spool-file)
1354 ;; No spool file whatsoever.
1357 ;; If procmail is used to get incoming mail, the files
1358 ;; are stored in this directory.
1359 (and (file-exists-p nnmail-procmail-directory)
1360 (or (eq nnmail-spool-file 'procmail)
1361 nnmail-use-procmail)
1363 nnmail-procmail-directory
1364 t (concat (if group (concat "^" (regexp-quote group)) "")
1365 nnmail-procmail-suffix "$"))))
1367 (crash (when (and (file-exists-p nnmail-crash-box)
1368 (> (nnheader-file-size
1369 (file-truename nnmail-crash-box))
1371 (list nnmail-crash-box))))
1372 ;; Remove any directories that inadvertently match the procmail
1373 ;; suffix, which might happen if the suffix is "".
1375 (when (file-directory-p (car p))
1376 (setq procmails (delete (car p) procmails)))
1378 ;; Return the list of spools.
1382 (or (eq nnmail-spool-file 'procmail)
1383 nnmail-use-procmail)
1387 (eq nnmail-spool-file 'procmail))
1389 ((listp nnmail-spool-file)
1395 (if (and (not (string-match "^po:" file))
1396 (file-directory-p file))
1397 (nnheader-directory-regular-files file)
1401 ((stringp nnmail-spool-file)
1402 (if (and (not (string-match "^po:" nnmail-spool-file))
1403 (file-directory-p nnmail-spool-file))
1405 (nnheader-directory-regular-files nnmail-spool-file)
1407 (cons nnmail-spool-file procmails)))
1408 ((eq nnmail-spool-file 'pop)
1409 (cons (format "po:%s" (user-login-name)) procmails))
1413 ;; Activate a backend only if it isn't already activated.
1414 ;; If FORCE, re-read the active file even if the backend is
1415 ;; already activated.
1416 (defun nnmail-activate (backend &optional force)
1417 (nnheader-init-server-buffer)
1418 (let (file timestamp file-time)
1419 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1421 (and (setq file (ignore-errors
1422 (symbol-value (intern (format "%s-active-file"
1424 (setq file-time (nth 5 (file-attributes file)))
1428 (symbol-value (intern
1429 (format "%s-active-timestamp"
1432 (not (consp timestamp))
1433 (equal timestamp '(0 0))
1434 (> (nth 0 file-time) (nth 0 timestamp))
1435 (and (= (nth 0 file-time) (nth 0 timestamp))
1436 (> (nth 1 file-time) (nth 1 timestamp))))))
1438 (or (eq timestamp 'none)
1439 (set (intern (format "%s-active-timestamp" backend))
1441 (funcall (intern (format "%s-request-list" backend)))))
1444 (defun nnmail-message-id ()
1445 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1448 ;;; nnmail duplicate handling
1451 (defvar nnmail-cache-buffer nil)
1453 (defun nnmail-cache-open ()
1454 (if (or (not nnmail-treat-duplicates)
1455 (and nnmail-cache-buffer
1456 (buffer-name nnmail-cache-buffer)))
1457 () ; The buffer is open.
1460 (setq nnmail-cache-buffer
1461 (get-buffer-create " *nnmail message-id cache*")))
1462 (when (file-exists-p nnmail-message-id-cache-file)
1463 (nnheader-insert-file-contents nnmail-message-id-cache-file))
1464 (set-buffer-modified-p nil)
1467 (defun nnmail-cache-close ()
1468 (when (and nnmail-cache-buffer
1469 nnmail-treat-duplicates
1470 (buffer-name nnmail-cache-buffer)
1471 (buffer-modified-p nnmail-cache-buffer))
1473 (set-buffer nnmail-cache-buffer)
1474 ;; Weed out the excess number of Message-IDs.
1475 (goto-char (point-max))
1476 (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1479 (delete-region (point-min) (point))))
1481 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1482 (make-directory (file-name-directory nnmail-message-id-cache-file)
1484 (nnmail-write-region (point-min) (point-max)
1485 nnmail-message-id-cache-file nil 'silent)
1486 (set-buffer-modified-p nil)
1487 (setq nnmail-cache-buffer nil)
1488 (kill-buffer (current-buffer)))))
1490 (defun nnmail-cache-insert (id)
1491 (when nnmail-treat-duplicates
1492 (unless (gnus-buffer-live-p nnmail-cache-buffer)
1493 (nnmail-cache-open))
1495 (set-buffer nnmail-cache-buffer)
1496 (goto-char (point-max))
1499 (defun nnmail-cache-id-exists-p (id)
1500 (when nnmail-treat-duplicates
1502 (set-buffer nnmail-cache-buffer)
1503 (goto-char (point-max))
1504 (search-backward id nil t))))
1506 (defun nnmail-fetch-field (header)
1509 (message-narrow-to-head)
1510 (message-fetch-field header))))
1512 (defun nnmail-check-duplication (message-id func artnum-func)
1513 (run-hooks 'nnmail-prepare-incoming-message-hook)
1514 ;; If this is a duplicate message, then we do not save it.
1515 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1516 (case-fold-search t)
1517 (action (when duplication
1519 ((memq nnmail-treat-duplicates '(warn delete))
1520 nnmail-treat-duplicates)
1521 ((nnheader-functionp nnmail-treat-duplicates)
1522 (funcall nnmail-treat-duplicates message-id))
1524 nnmail-treat-duplicates))))
1526 ;; Let the backend save the article (or not).
1529 (funcall func (setq group-art
1530 (nreverse (nnmail-article-group artnum-func))))
1531 (nnmail-cache-insert message-id))
1532 ((eq action 'delete)
1533 (setq group-art nil))
1535 ;; We insert a warning.
1536 (let ((case-fold-search t))
1537 (goto-char (point-min))
1538 (re-search-forward "^message-id[ \t]*:" nil t)
1541 "Gnus-Warning: This is a duplicate of message " message-id "\n")
1542 (funcall func (setq group-art
1543 (nreverse (nnmail-article-group artnum-func))))))
1545 (funcall func (setq group-art
1546 (nreverse (nnmail-article-group artnum-func))))))
1547 ;; Add the group-art list to the history list.
1549 (push group-art nnmail-split-history)
1550 (delete-region (point-min) (point-max)))))
1554 (defun nnmail-get-value (&rest args)
1555 (let ((sym (intern (apply 'format args))))
1557 (symbol-value sym))))
1559 (defun nnmail-get-new-mail (method exit-func temp
1560 &optional group spool-func)
1561 "Read new incoming mail."
1562 (let* ((spools (nnmail-get-spool-files group))
1564 nnmail-current-spool incoming incomings spool)
1565 (when (and (nnmail-get-value "%s-get-new-mail" method)
1567 ;; We first activate all the groups.
1568 (nnmail-activate method)
1569 ;; Allow the user to hook.
1570 (run-hooks 'nnmail-pre-get-new-mail-hook)
1571 ;; Open the message-id cache.
1573 ;; The we go through all the existing spool files and split the
1576 (setq spool (pop spools))
1577 ;; We read each spool file if either the spool is a POP-mail
1578 ;; spool, or the file exists. We can't check for the
1579 ;; existence of POPped mail.
1580 (when (or (string-match "^po:" spool)
1581 (and (file-exists-p (file-truename spool))
1582 (> (nnheader-file-size (file-truename spool)) 0)))
1583 (nnheader-message 3 "%s: Reading incoming mail..." method)
1584 (when (and (nnmail-move-inbox spool)
1585 (file-exists-p nnmail-crash-box))
1586 (setq nnmail-current-spool spool)
1587 ;; There is new mail. We first find out if all this mail
1588 ;; is supposed to go to some specific group.
1589 (setq group (nnmail-get-split-group spool group-in))
1590 ;; We split the mail
1591 (nnmail-split-incoming
1592 nnmail-crash-box (intern (format "%s-save-mail" method))
1593 spool-func group (intern (format "%s-active-number" method)))
1594 ;; Check whether the inbox is to be moved to the special tmp dir.
1596 (nnmail-make-complex-temp-name
1598 (if nnmail-tmp-directory
1600 (file-name-as-directory nnmail-tmp-directory)
1601 (file-name-nondirectory
1602 (concat (file-name-as-directory temp) "Incoming")))
1603 (concat (file-name-as-directory temp) "Incoming")))))
1604 (unless (file-exists-p (file-name-directory incoming))
1605 (make-directory (file-name-directory incoming) t))
1606 (rename-file nnmail-crash-box incoming t)
1607 (push incoming incomings))))
1608 ;; If we did indeed read any incoming spools, we save all info.
1611 (nnmail-get-value "%s-group-alist" method)
1612 (nnmail-get-value "%s-active-file" method))
1614 (funcall exit-func))
1615 (run-hooks 'nnmail-read-incoming-hook)
1616 (nnheader-message 3 "%s: Reading incoming mail...done" method))
1617 ;; Close the message-id cache.
1618 (nnmail-cache-close)
1619 ;; Allow the user to hook.
1620 (run-hooks 'nnmail-post-get-new-mail-hook)
1621 ;; Delete all the temporary files.
1623 (setq incoming (pop incomings))
1624 (and nnmail-delete-incoming
1625 (file-exists-p incoming)
1626 (file-writable-p incoming)
1627 (delete-file incoming))))))
1629 (defun nnmail-expired-article-p (group time force &optional inhibit)
1630 "Say whether an article that is TIME old in GROUP should be expired."
1633 (let ((days (or (and nnmail-expiry-wait-function
1634 (funcall nnmail-expiry-wait-function group))
1635 nnmail-expiry-wait)))
1636 (cond ((or (eq days 'never)
1639 ;; This isn't an expirable group.
1641 ((eq days 'immediate)
1642 ;; We expire all articles on sight.
1644 ((equal time '(0 0))
1645 ;; This is an ange-ftp group, and we don't have any dates.
1648 (setq days (days-to-time days))
1649 ;; Compare the time with the current time.
1651 (time-less-p days (time-since time))
1654 (defvar nnmail-read-passwd nil)
1655 (defun nnmail-read-passwd (prompt &rest args)
1656 "Read a password using PROMPT.
1657 If ARGS, PROMPT is used as an argument to `format'."
1660 (apply 'format prompt args)
1662 (unless nnmail-read-passwd
1663 (if (functionp 'read-passwd)
1664 (setq nnmail-read-passwd 'read-passwd)
1665 (if (load "passwd" t)
1666 (setq nnmail-read-passwd 'read-passwd)
1667 (unless (fboundp 'ange-ftp-read-passwd)
1668 (autoload 'ange-ftp-read-passwd "ange-ftp"))
1669 (setq nnmail-read-passwd 'ange-ftp-read-passwd))))
1670 (funcall nnmail-read-passwd prompt)))
1672 (defun nnmail-check-syntax ()
1673 "Check (and modify) the syntax of the message in the current buffer."
1675 (message-narrow-to-head)
1676 (let ((case-fold-search t))
1677 (unless (re-search-forward "^Message-ID[ \t]*:" nil t)
1678 (insert "Message-ID: " (nnmail-message-id) "\n")))))
1680 (defun nnmail-write-region (start end filename &optional append visit lockname)
1681 "Do a `write-region', and then set the file modes."
1682 (let ((pathname-coding-system 'binary))
1683 (write-region-as-coding-system
1684 nnmail-file-coding-system start end filename append visit lockname)
1685 (set-file-modes filename nnmail-default-file-modes)))
1688 ;;; Status functions
1691 (defun nnmail-replace-status (name value)
1692 "Make status NAME and VALUE part of the current status line."
1694 (message-narrow-to-head)
1695 (let ((status (nnmail-decode-status)))
1696 (setq status (delq (member name status) status))
1698 (push (cons name value) status))
1699 (message-remove-header "status")
1700 (goto-char (point-max))
1701 (insert "Status: " (nnmail-encode-status status) "\n"))))
1703 (defun nnmail-decode-status ()
1704 "Return a status-value alist from STATUS."
1705 (goto-char (point-min))
1706 (when (re-search-forward "^Status: " nil t)
1707 (let (name value status)
1709 ;; Narrow to the status.
1712 (if (re-search-forward "^[^ \t]" nil t)
1715 ;; Go through all elements and add them to the list.
1716 (goto-char (point-min))
1717 (while (re-search-forward "[^ \t=]+" nil t)
1718 (setq name (match-string 0))
1719 (if (not (eq (char-after) ?=))
1723 (if (not (eq (char-after) ?\"))
1724 (if (not (looking-at "[^ \t]"))
1728 (setq value (match-string 0))
1729 (goto-char (match-end 0)))
1731 (setq value (read (current-buffer)))))
1732 (push (cons name value) status)))
1735 (defun nnmail-encode-status (status)
1736 "Return a status string from STATUS."
1741 (if (string-match "[ \t]" (cdr elem))
1742 (prin1-to-string (cdr elem))
1746 (defun nnmail-split-history ()
1747 "Generate an overview of where the last mail split put articles."
1749 (unless nnmail-split-history
1750 (error "No current split history"))
1751 (with-output-to-temp-buffer "*nnmail split history*"
1752 (let ((history nnmail-split-history)
1754 (while (setq elem (pop history))
1755 (princ (mapconcat (lambda (ga)
1756 (concat (car ga) ":" (int-to-string (cdr ga))))
1761 (defun nnmail-purge-split-history (group)
1762 "Remove all instances of GROUP from `nnmail-split-history'."
1763 (let ((history nnmail-split-history))
1765 (setcar history (gnus-delete-if (lambda (e) (string= (car e) group))
1768 (setq nnmail-split-history (delq nil nnmail-split-history))))
1770 (defun nnmail-new-mail-p (group)
1771 "Say whether GROUP has new mail."
1772 (let ((his nnmail-split-history)
1775 (when (assoc group (pop his))
1781 (autoload 'pop3-movemail "pop3"))
1783 (defun nnmail-pop3-movemail (inbox crashbox)
1784 "Function to move mail from INBOX on a pop3 server to file CRASHBOX."
1785 (let ((pop3-maildrop
1786 (substring inbox (match-end (string-match "^po:" inbox))))
1788 (or nnmail-pop-password
1790 (format "Password for %s: " inbox)))))
1791 (pop3-movemail crashbox)))
1793 (defun nnmail-within-headers-p ()
1794 "Check to see if point is within the headers of a unix mail message.
1795 Doesn't change point."
1796 (let ((pos (point)))
1798 (and (nnmail-search-unix-mail-delim-backward)
1799 (not (search-forward "\n\n" pos t))))))
1801 (run-hooks 'nnmail-load-hook)
1805 ;;; nnmail.el ends here