1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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.
32 (eval-when-compile (require 'cl))
34 (defvar nnmail-split-methods
36 "*Incoming mail will be split according to this variable.
38 If you'd like, for instance, one mail group for mail from the
39 \"4ad-l\" mailing list, one group for junk mail and one for everything
40 else, you could do something like this:
42 (setq nnmail-split-methods
43 '((\"mail.4ad\" \"From:.*4ad\")
44 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
45 (\"mail.misc\" \"\")))
47 As you can see, this variable is a list of lists, where the first
48 element in each \"rule\" is the name of the group (which, by the way,
49 does not have to be called anything beginning with \"mail\",
50 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
51 nnmail will try to match on the header to find a fit.
53 The second element can also be a function. In that case, it will be
54 called narrowed to the headers with the first element of the rule as
55 the argument. It should return a non-nil value if it thinks that the
56 mail belongs in that group.
58 The last element should always have \"\" as the regexp.
60 This variable can also have a function as its value.")
62 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
63 (defvar nnmail-crosspost t
64 "*If non-nil, do crossposting if several split methods match the mail.
65 If nil, the first match found will be used.")
67 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
68 (defvar nnmail-keep-last-article nil
69 "*If non-nil, nnmail will never delete the last expired article in a directory.
70 You may need to set this variable if other programs are putting
71 new mail into folder numbers that Gnus has marked as expired.")
73 (defvar nnmail-use-long-file-names nil
74 "*If non-nil the mail backends will use long file and directory names.
75 If nil, groups like \"mail.misc\" will end up in directories like
78 (defvar nnmail-expiry-wait 7
79 "*Expirable articles that are older than this will be expired.
80 This variable can either be a number (which will be interpreted as a
81 number of days) -- this doesn't have to be an integer. This variable
82 can also be `immediate' and `never'.")
84 (defvar nnmail-expiry-wait-function nil
85 "*Variable that holds function to specify how old articles should be before they are expired.
86 The function will be called with the name of the group that the
87 expiry is to be performed in, and it should return an integer that
88 says how many days an article can be stored before it is considered
89 \"old\". It can also return the values `never' and `immediate'.
93 (setq nnmail-expiry-wait-function
95 (cond ((string-match \"private\" newsgroup) 31)
96 ((string-match \"junk\" newsgroup) 1)
97 ((string-match \"important\" 'never))
100 (defvar nnmail-spool-file
102 (concat "/usr/spool/mail/" (user-login-name)))
103 "Where the mail backends will look for incoming mail.
104 This variable is \"/usr/spool/mail/$user\" by default.
105 If this variable is nil, no mail backends will read incoming mail.
106 If this variable is a list, all files mentioned in this list will be
107 used as incoming mailboxes.")
109 (defvar nnmail-crash-box "~/.gnus-crash-box"
110 "*File where Gnus will store mail while processing it.")
112 (defvar nnmail-use-procmail nil
113 "*If non-nil, the mail backends will look in `nnmail-procmail-directory' for spool files.
114 The file(s) in `nnmail-spool-file' will also be read.")
116 (defvar nnmail-procmail-directory "~/incoming/"
117 "*When using procmail (and the like), incoming mail is put in this directory.
118 The Gnus mail backends will read the mail from this directory.")
120 (defvar nnmail-procmail-suffix "\\.spool"
121 "*Suffix of files created by procmail (and the like).
122 This variable might be a suffix-regexp to match the suffixes of
123 several files - eg. \".spool[0-9]*\".")
125 (defvar nnmail-resplit-incoming nil
126 "*If non-nil, re-split incoming procmail sorted mail.")
128 (defvar nnmail-delete-file-function 'delete-file
129 "Function called to delete files in some mail backends.")
131 (defvar nnmail-crosspost-link-function 'add-name-to-file
132 "Function called to create a copy of a file.
133 This is `add-name-to-file' by default, which means that crossposts
134 will use hard links. If your file system doesn't allow hard
135 links, you could set this variable to `copy-file' instead.")
137 (defvar nnmail-movemail-program "movemail"
138 "*A command to be executed to move mail from the inbox.
139 The default is \"movemail\".")
141 (defvar nnmail-read-incoming-hook nil
142 "*Hook that will be run after the incoming mail has been transferred.
143 The incoming mail is moved from `nnmail-spool-file' (which normally is
144 something like \"/usr/spool/mail/$user\") to the user's home
145 directory. This hook is called after the incoming mail box has been
146 emptied, and can be used to call any mail box programs you have
147 running (\"xwatch\", etc.)
151 \(add-hook 'nnmail-read-incoming-hook
153 (start-process \"mailsend\" nil
154 \"/local/bin/mailsend\" \"read\" \"mbox\")))
156 If you have xwatch running, this will alert it that mail has been
159 If you use `display-time', you could use something like this:
161 \(add-hook 'nnmail-read-incoming-hook
163 ;; Update the displayed time, since that will clear out
164 ;; the flag that says you have mail.
165 (if (eq (process-status \"display-time\") 'run)
166 (display-time-filter display-time-process \"\"))))")
168 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
169 (defvar nnmail-prepare-incoming-hook nil
170 "*Hook called before treating incoming mail.
171 The hook is run in a buffer with all the new, incoming mail.")
173 (defvar nnmail-pre-get-new-mail-hook nil
174 "Hook called just before starting to handle new incoming mail.")
176 (defvar nnmail-post-get-new-mail-hook nil
177 "Hook called just after finishing handling new incoming mail.")
179 ;; Suggested by Mejia Pablo J <pjm9806@usl.edu>.
180 (defvar nnmail-tmp-directory nil
181 "*If non-nil, use this directory for temporary storage when reading incoming mail.")
183 (defvar nnmail-large-newsgroup 50
184 "*The number of the articles which indicates a large newsgroup.
185 If the number of the articles is greater than the value, verbose
186 messages will be shown to indicate the current status.")
188 (defvar nnmail-split-fancy "mail.misc"
189 "*Incoming mail can be split according to this fancy variable.
190 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
192 The format is this variable is SPLIT, where SPLIT can be one of
195 GROUP: Mail will be stored in GROUP (a string).
197 \(FIELD VALUE SPLIT): If the message field FIELD (a regexp) contains
198 VALUE (a regexp), store the messages as specified by SPLIT.
200 \(| SPLIT...): Process each SPLIT expression until one of them matches.
201 A SPLIT expression is said to match if it will cause the mail
202 message to be stored in one or more groups.
204 \(& SPLIT...): Process each SPLIT expression.
206 FIELD must match a complete field name. VALUE must match a complete
207 word according to the fundamental mode syntax table. You can use .*
208 in the regexps to match partial field names or words.
210 FIELD and VALUE can also be lisp symbols, in that case they are expanded
211 as specified in `nnmail-split-abbrev-alist'.
215 \(setq nnmail-split-methods 'nnmail-split-fancy
217 ;; Messages from the mailer deamon are not crossposted to any of
218 ;; the ordinary groups. Warnings are put in a separate group
220 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
222 ;; Non-error messages are crossposted to all relevant
223 ;; groups, but we don't crosspost between the group for the
224 ;; (ding) list and the group for other (ding) related mail.
225 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
226 (\"subject\" \"ding\" \"ding.misc\"))
227 ;; Other mailing lists...
228 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
229 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
231 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
232 ;; Unmatched mail goes to the catch all group.
235 (defvar nnmail-split-abbrev-alist
236 '((any . "from\\|to\\|cc\\|sender\\|apparently-to")
237 (mail . "mailer-daemon\\|postmaster"))
238 "*Alist of abbreviations allowed in `nnmail-split-fancy'.")
240 (defvar nnmail-delete-incoming nil
241 "*If non-nil, the mail backends will delete incoming files after splitting.")
243 (defvar nnmail-message-id-cache-length 1000
244 "*The approximate number of Message-IDs nnmail will keep in its cache.
245 If this variable is nil, no checking on duplicate messages will be
248 (defvar nnmail-message-id-cache-file "~/.nnmail-cache"
249 "*The file name of the nnmail Message-ID cache.")
251 (defvar nnmail-treat-duplicates 'warn
252 "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
253 Three values are legal: nil, which means that nnmail is not to keep a
254 Message-ID cache; `warn', which means that nnmail should insert extra
255 headers to warn the user about the duplication (this is the default);
256 and `delete', which means that nnmail will delete duplicated mails.
258 This variable can also be a function. It will be called from a buffer
259 narrowed to the article in question with the Message-ID as a
260 parameter. It should return nil, `warn' or `delete'.")
262 ;;; Internal variables.
266 (defconst nnmail-version "nnmail 1.0"
271 (defun nnmail-request-post (&optional server)
272 (mail-send-and-exit nil))
274 (defun nnmail-find-file (file)
275 "Insert FILE in server buffer safely."
276 (set-buffer nntp-server-buffer)
279 (progn (insert-file-contents file) t)
282 (defun nnmail-group-pathname (group dir &optional file)
283 "Make pathname for GROUP."
285 (let ((dir (file-name-as-directory (expand-file-name dir))))
286 ;; If this directory exists, we use it directly.
287 (if (or nnmail-use-long-file-names
288 (file-directory-p (concat dir group)))
289 (concat dir group "/")
290 ;; If not, we translate dots into slashes.
291 (concat dir (nnheader-replace-chars-in-string group ?. ?/) "/")))
294 (defun nnmail-date-to-time (date)
295 "Convert DATE into time."
296 (let* ((d1 (timezone-parse-date date))
297 (t1 (timezone-parse-time (aref d1 3))))
300 (and el (string-to-number el)))
302 (aref t1 2) (aref t1 1) (aref t1 0)
303 (aref d1 2) (aref d1 1) (aref d1 0)
306 (defun nnmail-time-less (t1 t2)
307 "Say whether time T1 is less than time T2."
308 (or (< (car t1) (car t2))
309 (and (= (car t1) (car t2))
310 (< (nth 1 t1) (nth 1 t2)))))
312 (defun nnmail-days-to-time (days)
313 "Convert DAYS into time."
314 (let* ((seconds (* 1.0 days 60 60 24))
316 (ms (condition-case nil (round (/ seconds rest))
317 (range-error (expt 2 16)))))
318 (list ms (condition-case nil (round (- seconds (* ms rest)))
319 (range-error (expt 2 16))))))
321 (defun nnmail-time-since (time)
322 "Return the time since TIME, which is either an internal time or a date."
324 ;; Convert date strings to internal time.
325 (setq time (nnmail-date-to-time time)))
326 (let* ((current (current-time))
327 (rest (if (< (nth 1 current) (nth 1 time)) (expt 2 16))))
328 (list (- (+ (car current) (if rest -1 0)) (car time))
329 (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
331 ;; Function taken from rmail.el.
332 (defun nnmail-move-inbox (inbox)
333 "Move INBOX to `nnmail-crash-box'."
334 (let ((inbox (file-truename
335 (expand-file-name (substitute-in-file-name inbox))))
336 (tofile (file-truename (expand-file-name
337 (substitute-in-file-name nnmail-crash-box))))
338 movemail popmail errors)
339 ;; If getting from mail spool directory,
340 ;; use movemail to move rather than just renaming,
341 ;; so as to interlock with the mailer.
342 (unless (setq popmail (string-match "^po:" (file-name-nondirectory inbox)))
345 (setq inbox (file-name-nondirectory inbox)))
347 ;; On some systems, /usr/spool/mail/foo is a directory
348 ;; and the actual inbox is /usr/spool/mail/foo/foo.
349 (file-directory-p inbox))
350 (setq inbox (expand-file-name (user-login-name) inbox)))
352 (message "Getting mail from post office ...")
353 (when (or (and (file-exists-p tofile)
354 (/= 0 (nth 7 (file-attributes tofile))))
355 (and (file-exists-p inbox)
356 (/= 0 (nth 7 (file-attributes inbox)))))
357 (message "Getting mail from %s..." inbox)))
358 ;; Set TOFILE if have not already done so, and
359 ;; rename or copy the file INBOX to TOFILE if and as appropriate.
360 (cond ((file-exists-p tofile)
361 ;; The crash box exists already.
364 (not (file-exists-p inbox)))
365 ;; There is no inbox.
367 ((and (not movemail) (not popmail))
368 ;; Try copying. If that fails (perhaps no space),
371 (copy-file inbox tofile nil)
373 ;; Third arg is t so we can replace existing file TOFILE.
374 (rename-file inbox tofile t)))
375 ;; Make the real inbox file empty.
376 ;; Leaving it deleted could cause lossage
377 ;; because mailers often won't create the file.
379 (write-region (point) (point) inbox)
384 (setq errors (generate-new-buffer " *nnmail loss*"))
385 (buffer-disable-undo errors)
386 (let ((default-directory "/"))
388 (expand-file-name nnmail-movemail-program exec-directory)
389 nil errors nil inbox tofile))
390 (if (not (buffer-modified-p errors))
391 ;; No output => movemail won
394 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
395 (goto-char (point-max))
396 (skip-chars-backward " \t")
397 (delete-region (point) (point-max))
398 (goto-char (point-min))
399 (if (looking-at "movemail: ")
400 (delete-region (point-min) (match-end 0)))
402 (message (concat "movemail: "
403 (buffer-substring (point-min)
406 (setq tofile nil))))))
409 (kill-buffer errors))
412 (defun nnmail-get-active ()
413 "Returns an assoc of group names and active ranges.
414 nn*-request-list should have been called before calling this function."
416 ;; Go through all groups from the active list.
418 (set-buffer nntp-server-buffer)
419 (goto-char (point-min))
420 (while (re-search-forward
421 "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
422 ;; We create an alist with `(GROUP (LOW . HIGH))' elements.
423 (push (list (match-string 1)
424 (cons (string-to-int (match-string 3))
425 (string-to-int (match-string 2))))
429 (defun nnmail-save-active (group-assoc file-name)
430 "Save GROUP-ASSOC in ACTIVE-FILE."
434 (set-buffer (get-buffer-create " *nnmail active*"))
435 (buffer-disable-undo (current-buffer))
438 (setq group (pop group-assoc))
439 (insert (format "%s %d %d y\n" (car group) (cdr (car (cdr group)) )
440 (car (car (cdr group))))))
441 (unless (file-exists-p (file-name-directory file-name))
442 (make-directory (file-name-directory file-name) t))
443 (write-region 1 (point-max) (expand-file-name file-name) nil 'nomesg)
444 (kill-buffer (current-buffer))))))
446 (defun nnmail-get-split-group (file group)
447 (if (or (eq nnmail-spool-file 'procmail)
450 ((string-match (concat "^" (expand-file-name
451 (file-name-as-directory
452 nnmail-procmail-directory))
453 "\\([^/]*\\)" nnmail-procmail-suffix "$")
454 (expand-file-name file))
455 (substring (expand-file-name file)
456 (match-beginning 1) (match-end 1)))
461 (defun nnmail-process-babyl-mail-format (func)
462 (let ((case-fold-search t)
463 start message-id content-length do-search end)
465 (goto-char (point-min))
467 "
\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
468 (goto-char (match-end 0))
469 (delete-region (match-beginning 0) (match-end 0))
471 ;; Skip all the headers in case there are more "From "s...
472 (or (search-forward "\n\n" nil t)
473 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
474 (search-forward "
\1f\f"))
475 ;; Find the Message-ID header.
477 (if (re-search-backward "^Message-ID:[ \t]*\\(<[^>]*>\\)" nil t)
478 (setq message-id (buffer-substring (match-beginning 1)
480 ;; There is no Message-ID here, so we create one.
482 (when (re-search-backward "^Message-ID:" nil t)
484 (insert "Original-")))
486 (insert "Message-ID: " (setq message-id (nnmail-message-id))
488 ;; Look for a Content-Length header.
489 (if (not (save-excursion
490 (and (re-search-backward
491 "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
492 (setq content-length (string-to-int
496 ;; We destroy the header, since none of
497 ;; the backends ever use it, and we do not
498 ;; want to confuse other mailers by having
499 ;; a (possibly) faulty header.
500 (progn (insert "X-") t))))
502 (if (or (= (+ (point) content-length) (point-max))
504 (goto-char (+ (point) content-length))
507 (goto-char (+ (point) content-length))
508 (setq do-search nil))
510 ;; Go to the beginning of the next article - or to the end
513 (if (re-search-forward "\n
\1f" nil t)
514 (goto-char (+ 1 (match-beginning 0)))
515 (goto-char (- (point-max) 1))))
516 (delete-char 1) ; delete ^_
519 (narrow-to-region start (point))
520 (goto-char (point-min))
521 (nnmail-check-duplication message-id func)
522 (setq end (point-max))))
525 (defun nnmail-process-unix-mail-format (func)
526 (let ((case-fold-search t)
527 (delim (concat "^" rmail-unix-mail-delimiter))
528 start message-id content-length end skip head-end)
529 (goto-char (point-min))
530 (if (not (and (re-search-forward delim nil t)
531 (goto-char (match-beginning 0))))
532 ;; Possibly wrong format?
533 (error "Error, unknown mail format! (Possibly corrupted.)")
534 ;; Carry on until the bitter end.
538 ;; Find the end of the head.
541 (if (search-forward "\n\n" nil t)
543 ;; This will never happen, but just to be on the safe side --
544 ;; if there is no head-body delimiter, we search a bit manually.
545 (while (and (looking-at "From \\|[^ \t]+:")
549 ;; Find the Message-ID header.
550 (goto-char (point-min))
551 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
552 (setq message-id (match-string 1))
554 (when (re-search-backward "^Message-ID:" nil t)
556 (insert "Original-")))
557 ;; There is no Message-ID here, so we create one.
559 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
560 ;; Look for a Content-Length header.
561 (goto-char (point-min))
562 (if (not (re-search-forward "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
563 (setq content-length nil)
564 (setq content-length (string-to-int (match-string 1)))
565 ;; We destroy the header, since none of the backends ever
566 ;; use it, and we do not want to confuse other mailers by
567 ;; having a (possibly) faulty header.
570 ;; Find the end of this article.
571 (goto-char (point-max))
573 (setq head-end (point))
574 ;; We try the Content-Length value.
577 (setq skip (+ (point) content-length))
578 (when (or (= skip (point-max))
579 (and (< skip (point-max))
585 ;; No Content-Length, so we find the beginning of the next
586 ;; article or the end of the buffer.
588 (if (re-search-forward delim nil t)
589 (goto-char (match-beginning 0))
590 (goto-char (point-max))))
591 ;; Allow the backend to save the article.
594 (narrow-to-region start (point))
595 (goto-char (point-min))
596 (nnmail-check-duplication message-id func)
597 (setq end (point-max))))
600 (defun nnmail-process-mmdf-mail-format (func)
601 (let ((delim "^\^A\^A\^A\^A$")
603 start message-id end)
604 (goto-char (point-min))
605 (if (not (and (re-search-forward delim nil t)
607 ;; Possibly wrong format?
608 (error "Error, unknown mail format! (Possibly corrupted.)")
609 ;; Carry on until the bitter end.
612 ;; Find the end of the head.
615 (if (search-forward "\n\n" nil t)
617 ;; This will never happen, but just to be on the safe side --
618 ;; if there is no head-body delimiter, we search a bit manually.
619 (while (and (looking-at "From \\|[^ \t]+:")
623 ;; Find the Message-ID header.
624 (goto-char (point-min))
625 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
626 (setq message-id (match-string 1))
627 ;; There is no Message-ID here, so we create one.
629 (when (re-search-backward "^Message-ID:" nil t)
631 (insert "Original-")))
633 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
634 ;; Find the end of this article.
635 (goto-char (point-max))
637 (if (re-search-forward delim nil t)
639 (goto-char (point-max)))
640 ;; Allow the backend to save the article.
643 (narrow-to-region start (point))
644 (goto-char (point-min))
645 (nnmail-check-duplication message-id func)
646 (setq end (point-max))))
650 (defun nnmail-split-incoming (incoming func &optional exit-func group)
651 "Go through the entire INCOMING file and pick out each individual mail.
652 FUNC will be called with the buffer narrowed to each mail."
653 (let (;; If this is a group-specific split, we bind the split
654 ;; methods to just this group.
655 (nnmail-split-methods (if (and group
656 (or (eq nnmail-spool-file 'procmail)
658 (not nnmail-resplit-incoming))
659 (list (list group ""))
660 nnmail-split-methods))
661 start end do-search message-id)
663 ;; Open the message-id cache.
665 ;; Insert the incoming file.
666 (set-buffer (get-buffer-create " *nnmail incoming*"))
667 (buffer-disable-undo (current-buffer))
669 (insert-file-contents incoming)
670 (goto-char (point-min))
671 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
672 ;; Handle both babyl, MMDF and unix mail formats, since movemail will
673 ;; use the former when fetching from a mailbox, the latter when
674 ;; fetches from a file.
675 (cond ((or (looking-at "\^L")
676 (looking-at "BABYL OPTIONS:"))
677 (nnmail-process-babyl-mail-format func))
678 ((looking-at "\^A\^A\^A\^A")
679 (nnmail-process-mmdf-mail-format func))
681 (nnmail-process-unix-mail-format func)))
682 ;; Close the message-id cache.
684 (if exit-func (funcall exit-func))
685 (kill-buffer (current-buffer)))))
687 ;; Mail crossposts suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
688 (defun nnmail-article-group (func)
689 "Look at the headers and return an alist of groups that match.
690 FUNC will be called with the group name to determine the article number."
691 (let ((methods nnmail-split-methods)
692 (obuf (current-buffer))
694 end group-art method)
695 (if (and (sequencep methods) (= (length methods) 1))
696 ;; If there is only just one group to put everything in, we
697 ;; just return a list with just this one method in.
699 (list (cons (car (car methods))
700 (funcall func (car (car methods))))))
701 ;; We do actual comparison.
705 (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
706 (set-buffer nntp-server-buffer)
708 ;; Copy the headers into the work buffer.
709 (insert-buffer-substring obuf beg end)
710 ;; Fold continuation lines.
711 (goto-char (point-min))
712 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
713 (replace-match " " t t))
714 (if (and (symbolp nnmail-split-methods)
715 (fboundp nnmail-split-methods))
716 ;; `nnmail-split-methods' is a function, so we just call
717 ;; this function here and use the result.
720 (lambda (group) (cons group (funcall func group)))
722 (funcall nnmail-split-methods)
725 "Error in `nnmail-split-methods'; using `bogus' mail group")
728 ;; Go through the split methods to find a match.
729 (while (and methods (or nnmail-crosspost (not group-art)))
730 (goto-char (point-max))
731 (setq method (pop methods))
733 (not (equal "" (nth 1 method))))
736 (if (stringp (nth 1 method))
737 (re-search-backward (car (cdr method)) nil t)
738 ;; Function to say whether this is a match.
739 (funcall (nth 1 method) (car method)))
741 ;; Don't enter the article into the same
743 (not (assoc (car method) group-art)))
744 (push (cons (car method) (funcall func (car method)))
746 ;; This is the final group, which is used as a
750 (list (cons (car method)
751 (funcall func (car method)))))))))
754 (defun nnmail-insert-lines ()
755 "Insert how many lines there are in the body of the mail.
756 Return the number of characters in the body."
759 (goto-char (point-min))
760 (when (search-forward "\n\n" nil t)
761 (setq chars (- (point-max) (point)))
762 (setq lines (count-lines (point) (point-max)))
765 (when (re-search-backward "^Lines: " nil t)
766 (delete-region (point) (progn (forward-line 1) (point)))))
767 (insert (format "Lines: %d\n" (max lines 0)))
770 (defun nnmail-insert-xref (group-alist)
771 "Insert an Xref line based on the (group . article) alist."
773 (goto-char (point-min))
774 (when (search-forward "\n\n" nil t)
776 (if (re-search-backward "^Xref: " nil t)
777 (delete-region (match-beginning 0)
778 (progn (forward-line 1) (point))))
779 (insert (format "Xref: %s" (system-name)))
781 (insert (format " %s:%d" (car (car group-alist))
782 (cdr (car group-alist))))
783 (setq group-alist (cdr group-alist)))
786 ;; Written by byer@mv.us.adobe.com (Scott Byer).
787 (defun nnmail-make-complex-temp-name (prefix)
788 (let ((newname (make-temp-name prefix))
790 (while (file-exists-p newname)
791 (setq newprefix (concat newprefix "x"))
792 (setq newname (make-temp-name newprefix)))
795 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
797 (defun nnmail-split-fancy ()
798 "Fancy splitting method.
799 See the documentation for the variable `nnmail-split-fancy' for documentation."
800 (nnmail-split-it nnmail-split-fancy))
802 (defvar nnmail-split-cache nil)
803 ;; Alist of split expressions their equivalent regexps.
805 (defun nnmail-split-it (split)
806 ;; Return a list of groups matching SPLIT.
807 (cond ((stringp split)
811 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
814 (while (and (not done) (cdr split))
815 (setq split (cdr split)
816 done (nnmail-split-it (car split))))
818 ((assq split nnmail-split-cache)
819 ;; A compiled match expression.
820 (goto-char (point-max))
821 (if (re-search-backward (cdr (assq split nnmail-split-cache)) nil t)
822 (nnmail-split-it (nth 2 split))))
824 ;; An uncompiled match.
825 (let* ((field (nth 0 split))
826 (value (nth 1 split))
827 (regexp (concat "^\\("
830 nnmail-split-abbrev-alist))
835 nnmail-split-abbrev-alist))
838 (setq nnmail-split-cache
839 (cons (cons split regexp) nnmail-split-cache))
840 (goto-char (point-max))
841 (if (re-search-backward regexp nil t)
842 (nnmail-split-it (nth 2 split)))))))
844 ;; Get a list of spool files to read.
845 (defun nnmail-get-spool-files (&optional group)
846 (if (null nnmail-spool-file)
847 ;; No spool file whatsoever.
850 ;; If procmail is used to get incoming mail, the files
851 ;; are stored in this directory.
852 (and (file-exists-p nnmail-procmail-directory)
853 (or (eq nnmail-spool-file 'procmail)
856 nnmail-procmail-directory
857 t (concat (if group group "")
858 nnmail-procmail-suffix "$") t)))
860 (crash (when (and (file-exists-p nnmail-crash-box)
861 (> (nth 7 (file-attributes
862 (file-truename nnmail-crash-box))) 0))
863 (list nnmail-crash-box))))
864 ;; Remove any directories that inadvertantly match the procmail
865 ;; suffix, which might happen if the suffix is "".
867 (when (file-directory-p (car p))
868 (setq procmails (delete (car p) procmails)))
870 ;; Return the list of spools.
874 (or (eq nnmail-spool-file 'procmail)
875 nnmail-use-procmail))
877 ((listp nnmail-spool-file)
878 (append nnmail-spool-file procmails))
879 ((stringp nnmail-spool-file)
880 (cons nnmail-spool-file procmails))
881 ((eq nnmail-spool-file 'pop)
882 (cons (format "po:%s" (user-login-name)) procmails))
886 ;; Activate a backend only if it isn't already activated.
887 ;; If FORCE, re-read the active file even if the backend is
888 ;; already activated.
889 (defun nnmail-activate (backend &optional force)
890 (let (file timestamp file-time)
891 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
893 (and (setq file (condition-case ()
894 (symbol-value (intern (format "%s-active-file"
897 (setq file-time (nth 5 (file-attributes file)))
901 (symbol-value (intern
902 (format "%s-active-timestamp"
905 (not (consp timestamp))
906 (equal timestamp '(0 0))
907 (> (nth 0 file-time) (nth 0 timestamp))
908 (and (= (nth 0 file-time) (nth 0 timestamp))
909 (> (nth 1 file-time) (nth 1 timestamp))))))
911 (or (eq timestamp 'none)
912 (set (intern (format "%s-active-timestamp" backend))
914 (funcall (intern (format "%s-request-list" backend)))
915 (set (intern (format "%s-group-alist" backend))
916 (nnmail-get-active))))
919 (defun nnmail-message-id ()
920 (concat "<" (nnmail-unique-id) "@totally-fudged-out-message-id>"))
922 (defvar nnmail-unique-id-char nil)
924 (defun nnmail-number-base36 (num len)
925 (if (if (< len 0) (<= num 0) (= len 0))
927 (concat (nnmail-number-base36 (/ num 36) (1- len))
928 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
931 (defun nnmail-unique-id ()
932 (setq nnmail-unique-id-char
933 (% (1+ (or nnmail-unique-id-char (logand (random t) (1- (lsh 1 20)))))
934 ;; (current-time) returns 16-bit ints,
935 ;; and 2^16*25 just fits into 4 digits i base 36.
937 (let ((tm (if (fboundp 'current-time)
938 (current-time) '(12191 46742 287898))))
940 (nnmail-number-base36 (+ (car tm)
941 (lsh (% nnmail-unique-id-char 25) 16)) 4)
942 (nnmail-number-base36 (+ (nth 1 tm)
943 (lsh (/ nnmail-unique-id-char 25) 16)) 4))))
946 ;;; nnmail duplicate handling
949 (defvar nnmail-cache-buffer nil)
951 (defun nnmail-cache-open ()
952 (if (or (not nnmail-treat-duplicates)
953 (and nnmail-cache-buffer
954 (buffer-name nnmail-cache-buffer)))
955 () ; The buffer is open.
958 (setq nnmail-cache-buffer
959 (get-buffer-create " *nnmail message-id cache*")))
960 (buffer-disable-undo (current-buffer))
961 (and (file-exists-p nnmail-message-id-cache-file)
962 (insert-file-contents nnmail-message-id-cache-file))
965 (defun nnmail-cache-close ()
966 (when (and nnmail-cache-buffer
967 nnmail-treat-duplicates
968 (buffer-name nnmail-cache-buffer)
969 (buffer-modified-p nnmail-cache-buffer))
971 (set-buffer nnmail-cache-buffer)
972 ;; Weed out the excess number of Message-IDs.
973 (goto-char (point-max))
974 (and (search-backward "\n" nil t nnmail-message-id-cache-length)
977 (delete-region (point-min) (point))))
979 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
980 (make-directory (file-name-directory nnmail-message-id-cache-file)
982 (write-region (point-min) (point-max)
983 nnmail-message-id-cache-file nil 'silent)
984 (set-buffer-modified-p nil))))
986 (defun nnmail-cache-insert (id)
987 (when nnmail-treat-duplicates
989 (set-buffer nnmail-cache-buffer)
990 (goto-char (point-max))
993 (defun nnmail-cache-id-exists-p (id)
994 (when nnmail-treat-duplicates
996 (set-buffer nnmail-cache-buffer)
997 (goto-char (point-max))
998 (search-backward id nil t))))
1000 (defun nnmail-check-duplication (message-id func)
1001 ;; If this is a duplicate message, then we do not save it.
1002 (let* ((duplication (nnmail-cache-id-exists-p message-id))
1003 (action (when duplication
1005 ((memq nnmail-treat-duplicates '(warn delete))
1006 nnmail-treat-duplicates)
1007 ((nnheader-functionp nnmail-treat-duplicates)
1008 (funcall nnmail-treat-duplicates message-id))
1010 nnmail-treat-duplicates)))))
1013 (nnmail-cache-insert message-id)
1015 ((eq action 'delete)
1016 (delete-region (point-min) (point-max)))
1018 ;; We insert a warning.
1019 (let ((case-fold-search t)
1020 (newid (nnmail-message-id)))
1021 (goto-char (point-min))
1022 (when (re-search-forward "^message-id:" nil t)
1024 (insert "Original-"))
1026 (insert "Message-ID: " newid "\n")
1027 (insert "Gnus-Warning: This is a duplication of message "
1029 (nnmail-cache-insert newid)
1036 (defun nnmail-get-value (&rest args)
1037 (let ((sym (intern (apply 'format args))))
1039 (symbol-value sym))))
1041 (defun nnmail-get-new-mail (method exit-func temp
1042 &optional group spool-func)
1043 "Read new incoming mail."
1044 (let* ((spools (nnmail-get-spool-files group))
1046 incoming incomings spool)
1047 (when (and (nnmail-get-value "%s-get-new-mail" method)
1049 ;; We first activate all the groups.
1050 (nnmail-activate method)
1051 ;; Allow the user to hook.
1052 (run-hooks 'nnmail-pre-get-new-mail-hook)
1053 ;; The we go through all the existing spool files and split the
1056 (setq spool (pop spools))
1057 ;; We read each spool file if either the spool is a POP-mail
1058 ;; spool, or the file exists. We can't check for the
1059 ;; existance of POPped mail.
1060 (when (or (string-match "^po:" spool)
1061 (and (file-exists-p spool)
1062 (> (nth 7 (file-attributes (file-truename spool))) 0)))
1063 (nnheader-message 3 "%s: Reading incoming mail..." method)
1064 (when (and (nnmail-move-inbox spool)
1065 (file-exists-p nnmail-crash-box))
1066 ;; There is new mail. We first find out if all this mail
1067 ;; is supposed to go to some specific group.
1068 (setq group (nnmail-get-split-group spool group-in))
1069 ;; We split the mail
1070 (nnmail-split-incoming
1071 nnmail-crash-box (intern (format "%s-save-mail" method))
1073 ;; Check whether the inbox is to be moved to the special tmp dir.
1075 (nnmail-make-complex-temp-name
1077 (if nnmail-tmp-directory
1079 (file-name-as-directory nnmail-tmp-directory)
1080 (file-name-nondirectory (concat temp "Incoming")))
1081 (concat temp "Incoming")))))
1082 (rename-file nnmail-crash-box incoming t)
1083 (push incoming incomings))))
1084 ;; If we did indeed read any incoming spools, we save all info.
1087 (nnmail-get-value "%s-group-alist" method)
1088 (nnmail-get-value "%s-active-file" method))
1090 (funcall exit-func))
1091 (run-hooks 'nnmail-read-incoming-hook)
1092 (nnheader-message 3 "%s: Reading incoming mail...done" method))
1093 ;; Allow the user to hook.
1094 (run-hooks 'nnmail-post-get-new-mail-hook)
1095 ;; Delete all the temporary files.
1097 (setq incoming (pop incomings))
1098 (and nnmail-delete-incoming
1099 (file-exists-p incoming)
1100 (file-writable-p incoming)
1101 (delete-file incoming))))))
1103 (defun nnmail-expired-article-p (group time force &optional inhibit)
1104 "Say whether an article that is TIME old in GROUP should be expired."
1107 (let ((days (or (and nnmail-expiry-wait-function
1108 (funcall nnmail-expiry-wait-function group))
1109 nnmail-expiry-wait)))
1110 (cond ((or (eq days 'never)
1113 ;; This isn't an expirable group.
1115 ((eq days 'immediate)
1116 ;; We expire all articles on sight.
1118 ((equal time '(0 0))
1119 ;; This is an ange-ftp group, and we don't have any dates.
1122 (setq days (nnmail-days-to-time days))
1123 ;; Compare the time with the current time.
1124 (nnmail-time-less days (nnmail-time-since time)))))))
1128 ;;; nnmail.el ends here