1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2 ;; Copyright (C) 1995 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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31 (eval-when-compile (require 'cl))
33 (defvar nnmail-split-methods
35 "*Incoming mail will be split according to this variable.
37 If you'd like, for instance, one mail group for mail from the
38 \"4ad-l\" mailing list, one group for junk mail and one for everything
39 else, you could do something like this:
41 (setq nnmail-split-methods
42 '((\"mail.4ad\" \"From:.*4ad\")
43 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
44 (\"mail.misc\" \"\")))
46 As you can see, this variable is a list of lists, where the first
47 element in each \"rule\" is the name of the group (which, by the way,
48 does not have to be called anything beginning with \"mail\",
49 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
50 nnmail will try to match on the header to find a fit.
52 The second element can also be a function. In that case, it will be
53 called narrowed to the headers with the first element of the rule as
54 the argument. It should return a non-nil value if it thinks that the
55 mail belongs in that group.
57 The last element should always have \"\" as the regexp.
59 This variable can also have a function as its value.")
61 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
62 (defvar nnmail-crosspost t
63 "*If non-nil, do crossposting if several split methods match the mail.
64 If nil, the first match found will be used.")
66 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
67 (defvar nnmail-keep-last-article nil
68 "*If non-nil, nnmail will never delete the last expired article in a
69 directory. You may need to set this variable if other programs are putting
70 new mail into folder numbers that Gnus has marked as expired.")
72 (defvar nnmail-use-long-file-names nil
73 "*If non-nil the mail backends will use long file and directory names.
74 If nil, groups like \"mail.misc\" will end up in directories like
77 (defvar nnmail-expiry-wait 7
78 "*Articles that are older than `nnmail-expiry-wait' days will be expired.")
80 (defvar nnmail-expiry-wait-function nil
81 "*Variable that holds function to specify how old articles should be before they are expired.
82 The function will be called with the name of the group that the
83 expiry is to be performed in, and it should return an integer that
84 says how many days an article can be stored before it is considered
89 (setq nnmail-expiry-wait-function
91 (cond ((string-match \"private\" newsgroup) 31)
92 ((string-match \"junk\" newsgroup) 1)
95 (defvar nnmail-spool-file
97 (concat "/usr/spool/mail/" (user-login-name)))
98 "Where the mail backends will look for incoming mail.
99 This variable is \"/usr/spool/mail/$user\" by default.
100 If this variable is nil, no mail backends will read incoming mail.
101 If this variable is a list, all files mentioned in this list will be
102 used as incoming mailboxes.")
104 (defvar nnmail-crash-box "~/.gnus-crash-box"
105 "*File where Gnus will store mail while processing it.")
107 (defvar nnmail-use-procmail nil
108 "*If non-nil, the mail backends will look in `nnmail-procmail-directory' for spool files.
109 The file(s) in `nnmail-spool-file' will also be read.")
111 (defvar nnmail-procmail-directory "~/incoming/"
112 "*When using procmail (and the like), incoming mail is put in this directory.
113 The Gnus mail backends will read the mail from this directory.")
115 (defvar nnmail-procmail-suffix ".spool"
116 "*Suffix of files created by procmail (and the like).
117 This variable might be a suffix-regexp to match the suffixes of
118 several files - eg. \".spool[0-9]*\".")
120 (defvar nnmail-resplit-incoming nil
121 "*If non-nil, re-split incoming procmail sorted mail.")
123 (defvar nnmail-delete-file-function 'delete-file
124 "Function called to delete files in some mail backends.")
126 (defvar nnmail-movemail-program "movemail"
127 "*A command to be executed to move mail from the inbox.
128 The default is \"movemail\".")
130 (defvar nnmail-read-incoming-hook nil
131 "*Hook that will be run after the incoming mail has been transferred.
132 The incoming mail is moved from `nnmail-spool-file' (which normally is
133 something like \"/usr/spool/mail/$user\") to the user's home
134 directory. This hook is called after the incoming mail box has been
135 emptied, and can be used to call any mail box programs you have
136 running (\"xwatch\", etc.)
140 \(add-hook 'nnmail-read-incoming-hook
142 (start-process \"mailsend\" nil
143 \"/local/bin/mailsend\" \"read\" \"mbox\")))
145 If you have xwatch running, this will alert it that mail has been
148 If you use `display-time', you could use something like this:
150 \(add-hook 'nnmail-read-incoming-hook
152 ;; Update the displayed time, since that will clear out
153 ;; the flag that says you have mail.
154 (if (eq (process-status \"display-time\") 'run)
155 (display-time-filter display-time-process \"\"))))")
157 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
158 (defvar nnmail-prepare-incoming-hook nil
159 "*Hook called before treating incoming mail.
160 The hook is run in a buffer with all the new, incoming mail.")
162 ;; Suggested by Mejia Pablo J <pjm9806@usl.edu>.
163 (defvar nnmail-tmp-directory nil
164 "*If non-nil, use this directory for temporary storage when reading incoming mail.")
166 (defvar nnmail-large-newsgroup 50
167 "*The number of the articles which indicates a large newsgroup.
168 If the number of the articles is greater than the value, verbose
169 messages will be shown to indicate the current status.")
171 (defvar nnmail-split-fancy "mail.misc"
172 "*Incoming mail can be split according to this fancy variable.
173 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
175 The format is this variable is SPLIT, where SPLIT can be one of
178 GROUP: Mail will be stored in GROUP (a string).
180 \(FIELD VALUE SPLIT): If the message field FIELD (a regexp) contains
181 VALUE (a regexp), store the messages as specified by SPLIT.
183 \(| SPLIT...): Process each SPLIT expression until one of them matches.
184 A SPLIT expression is said to match if it will cause the mail
185 message to be stored in one or more groups.
187 \(& SPLIT...): Process each SPLIT expression.
189 FIELD must match a complete field name. VALUE must match a complete
190 word according to the fundamental mode syntax table. You can use .*
191 in the regexps to match partial field names or words.
193 FIELD and VALUE can also be lisp symbols, in that case they are expanded
194 as specified in `nnmail-split-abbrev-alist'.
198 \(setq nnmail-split-methods 'nnmail-split-fancy
200 ;; Messages from the mailer deamon are not crossposted to any of
201 ;; the ordinary groups. Warnings are put in a separate group
203 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
205 ;; Non-error messages are crossposted to all relevant
206 ;; groups, but we don't crosspost between the group for the
207 ;; (ding) list and the group for other (ding) related mail.
208 (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
209 (\"subject\" \"ding\" \"ding.misc\"))
210 ;; Other mailing lists...
211 (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
212 (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
214 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
215 ;; Unmatched mail goes to the catch all group.
218 (defvar nnmail-split-abbrev-alist
219 '((any . "from\\|to\\|cc\\|sender\\|apparently-to")
220 (mail . "mailer-daemon\\|postmaster"))
221 "*Alist of abbreviations allowed in `nnmail-split-fancy'.")
223 (defvar nnmail-delete-incoming nil
224 "*If non-nil, the mail backends will delete incoming files after splitting.")
226 (defvar nnmail-message-id-cache-length 1000
227 "*The approximate number of Message-IDs nnmail will keep in its cache.
228 If this variable is nil, no checking on duplicate messages will be
231 (defvar nnmail-message-id-cache-file "~/.nnmail-cache"
232 "*The file name of the nnmail Message-ID cache.")
234 (defvar nnmail-delete-duplicates nil
235 "*If non-nil, nnmail will delete any duplicate mails it sees.")
239 (defconst nnmail-version "nnmail 1.0"
244 (defun nnmail-request-post (&optional server)
245 (mail-send-and-exit nil))
247 (defun nnmail-find-file (file)
248 "Insert FILE in server buffer safely."
249 (set-buffer nntp-server-buffer)
252 (progn (insert-file-contents file) t)
255 (defun nnmail-group-pathname (group mail-dir)
256 "Make pathname for GROUP."
257 (let ((mail-dir (file-name-as-directory (expand-file-name mail-dir))))
258 ;; If this directory exists, we use it directly.
259 (if (or nnmail-use-long-file-names
260 (file-directory-p (concat mail-dir group)))
261 (concat mail-dir group "/")
262 ;; If not, we translate dots into slashes.
263 (concat mail-dir (nnmail-replace-chars-in-string group ?. ?/) "/"))))
265 (defun nnmail-replace-chars-in-string (string from to)
266 "Replace characters in STRING from FROM to TO."
267 (let ((string (substring string 0)) ;Copy string.
268 (len (length string))
270 ;; Replace all occurrences of FROM with TO.
272 (if (= (aref string idx) from)
273 (aset string idx to))
277 (defun nnmail-days-between (date1 date2)
278 ;; Return the number of days between date1 and date2.
279 (let ((d1 (mapcar (lambda (s) (and s (string-to-int s)) )
280 (timezone-parse-date date1)))
281 (d2 (mapcar (lambda (s) (and s (string-to-int s)) )
282 (timezone-parse-date date2))))
283 (- (timezone-absolute-from-gregorian
284 (nth 1 d1) (nth 2 d1) (car d1))
285 (timezone-absolute-from-gregorian
286 (nth 1 d2) (nth 2 d2) (car d2)))))
288 ;; Function taken from rmail.el.
289 (defun nnmail-move-inbox (inbox)
290 "Move INBOX to `nnmail-crash-box'."
291 (let ((inbox (file-truename
292 (expand-file-name (substitute-in-file-name inbox))))
293 (tofile (file-truename (expand-file-name
294 (substitute-in-file-name nnmail-crash-box))))
295 movemail popmail errors)
296 ;; If getting from mail spool directory,
297 ;; use movemail to move rather than just renaming,
298 ;; so as to interlock with the mailer.
299 (unless (setq popmail (string-match "^po:" (file-name-nondirectory inbox)))
302 (setq inbox (file-name-nondirectory inbox)))
304 ;; On some systems, /usr/spool/mail/foo is a directory
305 ;; and the actual inbox is /usr/spool/mail/foo/foo.
306 (file-directory-p inbox))
307 (setq inbox (expand-file-name (user-login-name) inbox)))
309 (message "Getting mail from post office ...")
310 (when (or (and (file-exists-p tofile)
311 (/= 0 (nth 7 (file-attributes tofile))))
312 (and (file-exists-p inbox)
313 (/= 0 (nth 7 (file-attributes inbox)))))
314 (message "Getting mail from %s..." inbox)))
315 ;; Set TOFILE if have not already done so, and
316 ;; rename or copy the file INBOX to TOFILE if and as appropriate.
317 (cond ((file-exists-p tofile)
318 ;; The crash box exists already.
321 (not (file-exists-p inbox)))
322 ;; There is no inbox.
324 ((and (not movemail) (not popmail))
325 ;; Try copying. If that fails (perhaps no space),
328 (copy-file inbox tofile nil)
330 ;; Third arg is t so we can replace existing file TOFILE.
331 (rename-file inbox tofile t)))
332 ;; Make the real inbox file empty.
333 ;; Leaving it deleted could cause lossage
334 ;; because mailers often won't create the file.
336 (write-region (point) (point) inbox)
341 (setq errors (generate-new-buffer " *nnmail loss*"))
342 (buffer-disable-undo errors)
343 (let ((default-directory "/"))
345 (expand-file-name nnmail-movemail-program exec-directory)
346 nil errors nil inbox tofile))
347 (if (not (buffer-modified-p errors))
348 ;; No output => movemail won
351 (subst-char-in-region (point-min) (point-max) ?\n ?\ )
352 (goto-char (point-max))
353 (skip-chars-backward " \t")
354 (delete-region (point) (point-max))
355 (goto-char (point-min))
356 (if (looking-at "movemail: ")
357 (delete-region (point-min) (match-end 0)))
359 (message (concat "movemail: "
360 (buffer-substring (point-min)
363 (setq tofile nil))))))
366 (kill-buffer errors))
369 (defun nnmail-get-active ()
370 "Returns an assoc of group names and active ranges.
371 nn*-request-list should have been called before calling this function."
373 ;; Go through all groups from the active list.
375 (set-buffer nntp-server-buffer)
376 (goto-char (point-min))
377 (while (re-search-forward
378 "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
379 ;; We create an alist with `(GROUP (LOW . HIGH))' elements.
380 (push (list (match-string 1)
381 (cons (string-to-int (match-string 3))
382 (string-to-int (match-string 2))))
386 (defun nnmail-save-active (group-assoc file-name)
389 (set-buffer (get-buffer-create " *nnmail active*"))
390 (buffer-disable-undo (current-buffer))
393 (setq group (car group-assoc))
394 (insert (format "%s %d %d y\n" (car group) (cdr (car (cdr group)) )
395 (car (car (cdr group)))))
396 (setq group-assoc (cdr group-assoc)))
397 (write-region 1 (point-max) (expand-file-name file-name) nil 'nomesg)
398 (kill-buffer (current-buffer)))))
400 (defun nnmail-get-split-group (file group)
401 (if (or (eq nnmail-spool-file 'procmail)
404 ((string-match (concat "^" (expand-file-name
405 (file-name-as-directory
406 nnmail-procmail-directory))
407 "\\(.*\\)" nnmail-procmail-suffix "$")
408 (expand-file-name file))
409 (substring (expand-file-name file)
410 (match-beginning 1) (match-end 1)))
415 (defun nnmail-process-babyl-mail-format (func)
416 (let (start message-id content-length do-search end)
418 (goto-char (point-min))
419 (re-search-forward "
\f\n0, *unseen,+\n\\*\\*\\* EOOH \\*\\*\\*\n" nil t)
420 (goto-char (match-end 0))
421 (delete-region (match-beginning 0) (match-end 0))
423 ;; Skip all the headers in case there are more "From "s...
424 (or (search-forward "\n\n" nil t)
425 (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
426 (search-forward "
\1f\f"))
427 ;; Find the Message-ID header.
429 (if (re-search-backward "^Message-ID:[ \t]*\\(<[^>]*>\\)" nil t)
430 (setq message-id (buffer-substring (match-beginning 1)
432 ;; There is no Message-ID here, so we create one.
434 (insert "Message-ID: " (setq message-id (nnmail-message-id))
436 ;; Look for a Content-Length header.
437 (if (not (save-excursion
438 (and (re-search-backward
439 "^Content-Length: \\([0-9]+\\)" start t)
440 (setq content-length (string-to-int
444 ;; We destroy the header, since none of
445 ;; the backends ever use it, and we do not
446 ;; want to confuse other mailers by having
447 ;; a (possibly) faulty header.
448 (progn (insert "X-") t))))
450 (if (or (= (+ (point) content-length) (point-max))
452 (goto-char (+ (point) content-length))
455 (goto-char (+ (point) content-length))
456 (setq do-search nil))
458 ;; Go to the beginning of the next article - or to the end
461 (if (re-search-forward "\n
\1f" nil t)
462 (goto-char (+ 1 (match-beginning 0)))
463 (goto-char (- (point-max) 1))))
464 (delete-char 1) ; delete ^_
467 (narrow-to-region start (point))
468 (goto-char (point-min))
469 ;; If this is a duplicate message, then we do not save it.
470 (if (nnmail-cache-id-exists-p message-id)
471 (delete-region (point-min) (point-max))
472 (nnmail-cache-insert message-id)
474 (setq end (point-max))))
477 (defun nnmail-process-unix-mail-format (func)
478 (let ((delim (concat "^" rmail-unix-mail-delimiter))
479 start message-id content-length end skip head-end)
480 (if (not (and (re-search-forward delim nil t)
481 (goto-char (match-beginning 0))))
482 ;; Possibly wrong format?
483 (error "Found no mail!")
484 ;; Carry on until the bitter end.
488 ;; Find the end of the head.
491 (if (search-forward "\n\n" nil t)
493 ;; This will never happen, but just to be on the safe side --
494 ;; if there is no head-body delimiter, we search a bit manually.
495 (while (and (looking-at "From \\|[^ \t]+:")
499 ;; Find the Message-ID header.
500 (goto-char (point-min))
501 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
502 (setq message-id (match-string 1))
503 ;; There is no Message-ID here, so we create one.
505 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
506 ;; Look for a Content-Length header.
507 (goto-char (point-min))
508 (if (not (re-search-forward "^Content-Length: \\([0-9]+\\)" nil t))
509 (setq content-length nil)
510 (setq content-length (string-to-int (match-string 1)))
511 ;; We destroy the header, since none of the backends ever
512 ;; use it, and we do not want to confuse other mailers by
513 ;; having a (possibly) faulty header.
516 ;; Find the end of this article.
517 (goto-char (point-max))
519 (setq head-end (point))
520 ;; We try the Content-Length value.
523 (setq skip (+ (point) content-length))
524 (when (or (= skip (point-max))
525 (and (< skip (point-max))
531 ;; No Content-Length, so we find the beginning of the next
532 ;; article or the end of the buffer.
534 (if (re-search-forward delim nil t)
535 (goto-char (match-beginning 0))
536 (goto-char (point-max))))
537 ;; Allow the backend to save the article.
540 (narrow-to-region start (point))
541 (goto-char (point-min))
542 ;; If this is a duplicate message, then we do not save it.
543 (if (nnmail-cache-id-exists-p message-id)
544 (delete-region (point-min) (point-max))
545 (nnmail-cache-insert message-id)
547 (setq end (point-max))))
550 (defun nnmail-process-mmfd-mail-format (func)
551 (let ((delim "^\^A\^A\^A\^A$")
552 start message-id end)
553 (if (not (and (re-search-forward delim nil t)
555 ;; Possibly wrong format?
556 (error "Found no mail!")
557 ;; Carry on until the bitter end.
560 ;; Find the end of the head.
563 (if (search-forward "\n\n" nil t)
565 ;; This will never happen, but just to be on the safe side --
566 ;; if there is no head-body delimiter, we search a bit manually.
567 (while (and (looking-at "From \\|[^ \t]+:")
571 ;; Find the Message-ID header.
572 (goto-char (point-min))
573 (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
574 (setq message-id (match-string 1))
575 ;; There is no Message-ID here, so we create one.
577 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
578 ;; Find the end of this article.
579 (goto-char (point-max))
581 (if (re-search-forward delim nil t)
583 (goto-char (point-max)))
584 ;; Allow the backend to save the article.
587 (narrow-to-region start (point))
588 (goto-char (point-min))
589 ;; If this is a duplicate message, then we do not save it.
590 (if (nnmail-cache-id-exists-p message-id)
591 (delete-region (point-min) (point-max))
592 (nnmail-cache-insert message-id)
594 (setq end (point-max))))
597 (defun nnmail-split-incoming (incoming func &optional exit-func group)
598 "Go through the entire INCOMING file and pick out each individual mail.
599 FUNC will be called with the buffer narrowed to each mail."
600 (let (;; If this is a group-specific split, we bind the split
601 ;; methods to just this group.
602 (nnmail-split-methods (if (and group
603 (or (eq nnmail-spool-file 'procmail)
605 (not nnmail-resplit-incoming))
606 (list (list group ""))
607 nnmail-split-methods))
608 start end do-search message-id)
610 ;; Open the message-id cache.
612 ;; Insert the incoming file.
613 (set-buffer (get-buffer-create " *nnmail incoming*"))
614 (buffer-disable-undo (current-buffer))
616 (insert-file-contents incoming)
617 (goto-char (point-min))
618 (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
619 ;; Handle both babyl, MMFD and unix mail formats, since movemail will
620 ;; use the former when fetching from a mailbox, the latter when
621 ;; fetches from a file.
622 (cond ((looking-at "\^L")
623 (nnmail-process-babyl-mail-format func))
624 ((looking-at "\^A\^A\^A\^A")
625 (nnmail-process-mmfd-mail-format func))
627 (nnmail-process-unix-mail-format func)))
628 ;; Close the message-id cache.
630 (if exit-func (funcall exit-func))
631 (kill-buffer (current-buffer)))))
633 ;; Mail crossposts syggested by Brian Edmonds <edmonds@cs.ubc.ca>.
634 (defun nnmail-article-group (func)
635 "Look at the headers and return an alist of groups that match.
636 FUNC will be called with the group name to determine the article number."
637 (let ((methods nnmail-split-methods)
638 (obuf (current-buffer))
640 end group-art method)
641 (if (and (sequencep methods) (= (length methods) 1))
642 ;; If there is only just one group to put everything in, we
643 ;; just return a list with just this one method in.
645 (list (cons (car (car methods))
646 (funcall func (car (car methods))))))
647 ;; We do actual comparison.
651 (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
652 (set-buffer nntp-server-buffer)
654 ;; Copy the headers into the work buffer.
655 (insert-buffer-substring obuf beg end)
656 ;; Fold continuation lines.
657 (goto-char (point-min))
658 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
659 (replace-match " " t t))
660 (if (and (symbolp nnmail-split-methods)
661 (fboundp nnmail-split-methods))
662 ;; `nnmail-split-methods' is a function, so we just call
663 ;; this function here and use the result.
666 (lambda (group) (cons group (funcall func group)))
668 (funcall nnmail-split-methods)
671 "Error in `nnmail-split-methods'; using `bogus' mail group")
674 ;; Go throught the split methods to find a match.
675 (while (and methods (or nnmail-crosspost (not group-art)))
676 (goto-char (point-max))
677 (setq method (pop methods))
679 (not (equal "" (nth 1 method))))
682 (if (stringp (nth 1 method))
683 (re-search-backward (car (cdr method)) nil t)
684 ;; Function to say whether this is a match.
685 (funcall (nth 1 method) (car method)))
687 ;; Don't enter the article into the same
689 (not (assoc (car method) group-art)))
690 (push (cons (car method) (funcall func (car method)))
692 ;; This is the final group, which is used as a
696 (list (cons (car method)
697 (funcall func (car method)))))))))
700 (defun nnmail-insert-lines ()
701 "Insert how many lines there are in the body of the mail.
702 Return the number of characters in the body."
705 (goto-char (point-min))
706 (when (search-forward "\n\n" nil t)
707 (setq chars (- (point-max) (point)))
708 (setq lines (- (count-lines (point) (point-max)) 1))
711 (when (re-search-backward "^Lines: " nil t)
712 (delete-region (point) (progn (forward-line 1) (point)))))
713 (insert (format "Lines: %d\n" lines))
716 (defun nnmail-insert-xref (group-alist)
717 "Insert an Xref line based on the (group . article) alist."
719 (goto-char (point-min))
720 (when (search-forward "\n\n" nil t)
722 (if (re-search-backward "^Xref: " nil t)
723 (delete-region (match-beginning 0)
724 (progn (forward-line 1) (point))))
725 (insert (format "Xref: %s" (system-name)))
727 (insert (format " %s:%d" (car (car group-alist))
728 (cdr (car group-alist))))
729 (setq group-alist (cdr group-alist)))
732 ;; Written by byer@mv.us.adobe.com (Scott Byer).
733 (defun nnmail-make-complex-temp-name (prefix)
734 (let ((newname (make-temp-name prefix))
736 (while (file-exists-p newname)
737 (setq newprefix (concat newprefix "x"))
738 (setq newname (make-temp-name newprefix)))
741 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
743 (defun nnmail-split-fancy ()
744 "Fancy splitting method.
745 See the documentation for the variable `nnmail-split-fancy' for documentation."
746 (nnmail-split-it nnmail-split-fancy))
748 (defvar nnmail-split-cache nil)
749 ;; Alist of split expresions their equivalent regexps.
751 (defun nnmail-split-it (split)
752 ;; Return a list of groups matching SPLIT.
753 (cond ((stringp split)
757 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
760 (while (and (not done) (cdr split))
761 (setq split (cdr split)
762 done (nnmail-split-it (car split))))
764 ((assq split nnmail-split-cache)
765 ;; A compiled match expression.
766 (goto-char (point-max))
767 (if (re-search-backward (cdr (assq split nnmail-split-cache)) nil t)
768 (nnmail-split-it (nth 2 split))))
770 ;; An uncompiled match.
771 (let* ((field (nth 0 split))
772 (value (nth 1 split))
773 (regexp (concat "^\\("
776 nnmail-split-abbrev-alist))
781 nnmail-split-abbrev-alist))
784 (setq nnmail-split-cache
785 (cons (cons split regexp) nnmail-split-cache))
786 (goto-char (point-max))
787 (if (re-search-backward regexp nil t)
788 (nnmail-split-it (nth 2 split)))))))
790 ;; Get a list of spool files to read.
791 (defun nnmail-get-spool-files (&optional group)
792 (if (null nnmail-spool-file)
793 ;; No spool file whatsoever.
796 ;; If procmail is used to get incoming mail, the files
797 ;; are stored in this directory.
798 (and (file-exists-p nnmail-procmail-directory)
800 nnmail-procmail-directory
801 t (concat (if group group "")
802 nnmail-procmail-suffix "$") t)))
804 (crash (when (and (file-exists-p nnmail-crash-box)
805 (> (nth 7 (file-attributes nnmail-crash-box)) 0))
806 (list nnmail-crash-box))))
807 ;; Remove any directories that inadvertantly match the procmail
808 ;; suffix, which might happen if the suffix is "".
810 (and (or (file-directory-p (car p))
811 (file-symlink-p (car p)))
812 (setq procmails (delete (car p) procmails)))
814 ;; Return the list of spools.
817 (cond ((listp nnmail-spool-file)
818 (append nnmail-spool-file procmails))
819 ((stringp nnmail-spool-file)
820 (cons nnmail-spool-file procmails))
821 ((eq nnmail-spool-file 'pop)
822 (cons (format "po:%s" (user-login-name)) procmails))
826 ;; Activate a backend only if it isn't already activated.
827 ;; If FORCE, re-read the active file even if the backend is
828 ;; already activated.
829 (defun nnmail-activate (backend &optional force)
830 (let (file timestamp file-time)
831 (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
833 (and (setq file (condition-case ()
834 (symbol-value (intern (format "%s-active-file"
837 (setq file-time (nth 5 (file-attributes file)))
841 (symbol-value (intern
842 (format "%s-active-timestamp"
845 (not (consp timestamp))
846 (equal timestamp '(0 0))
847 (> (nth 0 file-time) (nth 0 timestamp))
848 (and (= (nth 0 file-time) (nth 0 timestamp))
849 (> (nth 1 file-time) (nth 1 timestamp))))))
851 (or (eq timestamp 'none)
852 (set (intern (format "%s-active-timestamp" backend))
854 (funcall (intern (format "%s-request-list" backend)))
855 (set (intern (format "%s-group-alist" backend))
856 (nnmail-get-active))))
859 (defun nnmail-message-id ()
860 (concat "<" (nnmail-unique-id) "@totally-fudged-out-message-id>"))
862 (defvar nnmail-unique-id-char nil)
864 (defun nnmail-number-base36 (num len)
865 (if (if (< len 0) (<= num 0) (= len 0))
867 (concat (nnmail-number-base36 (/ num 36) (1- len))
868 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
871 (defun nnmail-unique-id ()
872 (setq nnmail-unique-id-char
873 (% (1+ (or nnmail-unique-id-char (logand (random t) (1- (lsh 1 20)))))
874 ;; (current-time) returns 16-bit ints,
875 ;; and 2^16*25 just fits into 4 digits i base 36.
877 (let ((tm (if (fboundp 'current-time)
878 (current-time) '(12191 46742 287898))))
880 (nnmail-number-base36 (+ (car tm)
881 (lsh (% nnmail-unique-id-char 25) 16)) 4)
882 (nnmail-number-base36 (+ (nth 1 tm)
883 (lsh (/ nnmail-unique-id-char 25) 16)) 4))))
886 ;;; nnmail duplicate handling
889 (defvar nnmail-cache-buffer nil)
891 (defun nnmail-cache-open ()
892 (if (or (not nnmail-delete-duplicates)
893 (and nnmail-cache-buffer
894 (buffer-name nnmail-cache-buffer)))
895 () ; The buffer is open.
898 (setq nnmail-cache-buffer
899 (get-buffer-create " *nnmail message-id cache*")))
900 (buffer-disable-undo (current-buffer))
901 (and (file-exists-p nnmail-message-id-cache-file)
902 (insert-file-contents nnmail-message-id-cache-file))
905 (defun nnmail-cache-close ()
906 (if (or (not nnmail-cache-buffer)
907 (not nnmail-delete-duplicates)
908 (not (buffer-name nnmail-cache-buffer))
909 (not (buffer-modified-p nnmail-cache-buffer)))
910 () ; The buffer is closed.
912 (set-buffer nnmail-cache-buffer)
913 ;; Weed out the excess number of Message-IDs.
914 (goto-char (point-max))
915 (and (search-backward "\n" nil t nnmail-message-id-cache-length)
918 (delete-region (point-min) (point))))
920 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
921 (make-directory (file-name-directory nnmail-message-id-cache-file)
923 (write-region (point-min) (point-max)
924 nnmail-message-id-cache-file nil 'silent)
925 (set-buffer-modified-p nil))))
927 (defun nnmail-cache-insert (id)
928 (and nnmail-delete-duplicates
930 (set-buffer nnmail-cache-buffer)
931 (goto-char (point-max))
934 (defun nnmail-cache-id-exists-p (id)
935 (and nnmail-delete-duplicates
937 (set-buffer nnmail-cache-buffer)
938 (goto-char (point-max))
939 (search-backward id nil t))))
941 (defun nnmail-get-value (&rest args)
942 (let ((sym (intern (apply 'format args))))
944 (symbol-value sym))))
946 (defun nnmail-get-new-mail (method exit-func temp
947 &optional group spool-func)
948 "Read new incoming mail."
949 (let* ((spools (nnmail-get-spool-files group))
951 incoming incomings spool)
952 (when (and (nnmail-get-value "%s-get-new-mail" method)
954 ;; We first activate all the groups.
955 (nnmail-activate method)
956 ;; The we go through all the existing spool files and split the
959 (setq spool (pop spools))
960 ;; We read each spool file if either the spool is a POP-mail
961 ;; spool, or the file exists. We can't check for the
962 ;; existance of POPped mail.
963 (when (or (string-match "^po:" spool)
964 (and (file-exists-p spool)
965 (> (nth 7 (file-attributes spool)) 0)))
966 (when gnus-verbose-backends
967 (message "%s: Reading incoming mail..." method))
968 (when (and (nnmail-move-inbox spool)
969 (file-exists-p nnmail-crash-box))
970 ;; There is new mail. We first find out if all this mail
971 ;; is supposed to go to some specific group.
972 (setq group (nnmail-get-split-group spool group-in))
974 (nnmail-split-incoming
975 nnmail-crash-box (intern (format "%s-save-mail" method))
977 ;; Check whether the inbox is to be moved to the special tmp dir.
979 (nnmail-make-complex-temp-name
981 (if nnmail-tmp-directory
983 (file-name-as-directory nnmail-tmp-directory)
984 (file-name-nondirectory (concat temp "Incoming")))
985 (concat temp "Incoming")))))
986 (rename-file nnmail-crash-box incoming t)
987 (push incoming incomings))))
988 ;; If we did indeed read any incoming spools, we save all info.
991 (nnmail-get-value "%s-group-alist" method)
992 (nnmail-get-value "%s-active-file" method))
995 (run-hooks 'nnmail-read-incoming-hook)
996 (when gnus-verbose-backends
997 (message "%s: Reading incoming mail...done" method)))
998 ;; Delete all the temporary files.
1000 (setq incoming (pop incomings))
1001 (and nnmail-delete-incoming
1002 (file-exists-p incoming)
1003 (file-writable-p incoming)
1004 (delete-file incoming))))))
1008 ;;; nnmail.el ends here