*** empty log message ***
[gnus] / lisp / nnmail.el
index 7b766af..d629cd3 100644 (file)
 (require 'nnheader)
 (require 'rmail)
 (require 'timezone)
+(require 'sendmail)
 
 (defvar nnmail-split-methods
   '(("mail.misc" ""))
-  "Incoming mail will be split according to this variable.
+  "*Incoming mail will be split according to this variable.
 
 If you'd like, for instance, one mail group for mail from the
 \"4ad-l\" mailing list, one group for junk mail and one for everything
@@ -56,20 +57,20 @@ The last element should always have \"\" as the regexp.")
 
 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
 (defvar nnmail-crosspost t
-  "If non-nil, do crossposting if several split methods match the mail.
+  "*If non-nil, do crossposting if several split methods match the mail.
 If nil, the first match found will be used.")
 
 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
 (defvar nnmail-keep-last-article nil
-  "If non-nil, nnmail will never delete the last expired article in a
+  "*If non-nil, nnmail will never delete the last expired article in a
 directory.  You may need to set this variable if other programs are putting
 new mail into folder numbers that Gnus has marked as expired.")
 
 (defvar nnmail-expiry-wait 7
-  "Articles that are older than `nnmail-expiry-wait' days will be expired.")
+  "*Articles that are older than `nnmail-expiry-wait' days will be expired.")
 
 (defvar nnmail-expiry-wait-function nil
-  "Variable that holds funtion to specify how old articles should be before they are expired.
+  "*Variable that holds function to specify how old articles should be before they are expired.
   The function will be called with the name of the group that the
 expiry is to be performed in, and it should return an integer that
 says how many days an article can be stored before it is considered
@@ -88,10 +89,28 @@ Eg.:
       (concat "/usr/spool/mail/" (user-login-name)))
   "Where the mail backends will look for incoming mail.
 This variable is \"/usr/spool/mail/$user\" by default.
-If this variable is nil, no mail backends will read incoming mail.")
+If this variable is nil, no mail backends will read incoming mail.
+If this variable is a list, all files mentioned in this list will be
+used as incoming mailboxes.")
+
+(defvar nnmail-use-procmail nil
+  "*If non-nil, the mail backends will look in `nnmail-procmail-directory' for spool files.
+The file(s) in `nnmail-spool-file' will also be read.")
+
+(defvar nnmail-procmail-directory "~/incoming/"
+  "*When using procmail (and the like), incoming mail is put in this directory.
+The Gnus mail backends will read the mail from this directory.")
+
+(defvar nnmail-procmail-suffix ".spool"
+  "*Suffix of files created by procmail (and the like).
+This variable might be a suffix-regexp to match the suffixes of
+several files - eg. \".spool[0-9]*\".")
+
+(defvar nnmail-resplit-incoming nil
+  "*If non-nil, re-split incoming procmail sorted mail.")
 
 (defvar nnmail-read-incoming-hook nil
-  "Hook that will be run after the incoming mail has been transferred.
+  "*Hook that will be run after the incoming mail has been transferred.
 The incoming mail is moved from `nnmail-spool-file' (which normally is
 something like \"/usr/spool/mail/$user\") to the user's home
 directory. This hook is called after the incoming mail box has been
@@ -107,18 +126,74 @@ Eg.
 
 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
 (defvar nnmail-prepare-incoming-hook nil
-  "Hook called before treating incoming mail.
+  "*Hook called before treating incoming mail.
 The hook is run in a buffer with all the new, incoming mail.")
 
 ;; Suggested by Mejia Pablo J <pjm9806@usl.edu>.
 (defvar nnmail-tmp-directory nil
-  "If non-nil, use this directory for temporary storage when reading incoming mail.")
+  "*If non-nil, use this directory for temporary storage when reading incoming mail.")
 
 (defvar nnmail-large-newsgroup 50
-  "The number of the articles which indicates a large newsgroup.
+  "*The number of the articles which indicates a large newsgroup.
 If the number of the articles is greater than the value, verbose
 messages will be shown to indicate the current status.")
 
+(defvar nnmail-split-fancy "mail.misc"
+  "*Incoming mail can be split according to this fancy variable.
+To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
+
+The format is this variable is SPLIT, where SPLIT can be one of
+the following:
+
+GROUP: Mail will be stored in GROUP (a string).
+
+\(FIELD VALUE SPLIT): If the message field FIELD (a regexp) contains
+  VALUE (a regexp), store the messages as specified by SPLIT.
+
+\(| SPLIT...): Process each SPLIT expression until one of them matches.
+  A SPLIT expression is said to match if it will cause the mail
+  message to be stored in one or more groups.  
+
+\(& SPLIT...): Process each SPLIT expression.
+
+FIELD must match a complete field name.  VALUE must match a complete
+word according to the fundamental mode syntax table.  You can use .*
+in the regexps to match partial field names or words.
+
+FIELD and VALUE can also be lisp symbols, in that case they are expanded
+as specified in `nnmail-split-abbrev-alist'.
+
+Example:
+
+\(setq nnmail-split-methods 'nnmail-split-fancy
+      nnmail-split-fancy
+      ;; Messages from the mailer deamon are not crossposted to any of
+      ;; the ordinary groups.  Warnings are put in a separate group
+      ;; from real errors.
+      '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
+                         \"mail.misc\"))
+         ;; Non-error messages are crossposted to all relevant
+         ;; groups, but we don't crosspost between the group for the
+         ;; (ding) list and the group for other (ding) related mail.
+         (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
+               (\"subject\" \"ding\" \"ding.misc\"))
+            ;; Other mailing lists...
+            (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
+            (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
+            ;; People...
+            (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
+         ;; Unmatched mail goes to the catch all group.
+         \"misc.misc\"))")
+
+(defvar nnmail-split-abbrev-alist
+  '((any . "from\\|to\\|cc\\|sender\\|apparently-to")
+    (mail . "mailer-daemon\\|postmaster"))
+  "*Alist of abbreviations allowed in `nnmail-split-fancy'.")
+
+(defvar nnmail-delete-incoming nil
+  "*If non-nil, the mail backends will delete incoming files after splitting.
+This is nil by default for reasons of security.")
+
 \f
 
 (defconst nnmail-version "nnml 0.0"
@@ -126,6 +201,9 @@ messages will be shown to indicate the current status.")
 
 \f
 
+(defun nnmail-request-post (&optional server)
+  (mail-send-and-exit nil))
+
 (defun nnmail-request-post-buffer (post group subject header article-buffer
                                        info follow-to respect-poster)
   (let ((method-address (cdr (assq 'to-address (nth 5 info))))
@@ -250,18 +328,18 @@ messages will be shown to indicate the current status.")
 (defun nnmail-move-inbox (inbox tofile)
   (let ((inbox (file-truename
                (expand-file-name (substitute-in-file-name inbox))))
-       (tofile (nnmail-make-complex-temp-name (expand-file-name tofile)))
        movemail popmail errors)
     ;; Check whether the inbox is to be moved to the special tmp dir. 
     (if nnmail-tmp-directory
        (setq tofile (concat (file-name-as-directory nnmail-tmp-directory)
                             (file-name-nondirectory tofile))))
+    ;; Make the filename unique.
+    (setq tofile (nnmail-make-complex-temp-name (expand-file-name tofile)))
     ;; If getting from mail spool directory,
     ;; use movemail to move rather than just renaming,
     ;; so as to interlock with the mailer.
-    (setq movemail (string= (file-name-directory inbox)
-                           (file-truename rmail-spool-directory))
-         popmail (string-match "^po:" (file-name-nondirectory inbox)))
+    (or (setq popmail (string-match "^po:" (file-name-nondirectory inbox)))
+       (setq movemail t))
     (if popmail (setq inbox (file-name-nondirectory inbox)))
     (if movemail
        ;; On some systems, /usr/spool/mail/foo is a directory
@@ -306,8 +384,7 @@ messages will be shown to indicate the current status.")
                     ;; No output => movemail won
                     nil
                   (set-buffer errors)
-                  (subst-char-in-region (point-min) (point-max)
-                                        ?\n ?\  )
+                  (subst-char-in-region (point-min) (point-max) ?\n ?\  )
                   (goto-char (point-max))
                   (skip-chars-backward " \t")
                   (delete-region (point) (point-max))
@@ -371,11 +448,33 @@ nn*-request-list should have been called before calling this function."
       (write-region 1 (point-max) (expand-file-name file-name) nil 'nomesg)
       (kill-buffer (current-buffer)))))
 
-(defun nnmail-split-incoming (incoming func &optional dont-kill)
+(defun nnmail-get-split-group (file group)
+  (if (or (eq nnmail-spool-file 'procmail)
+         nnmail-use-procmail)
+      (cond (group group)
+           ((string-match (concat "^" (expand-file-name
+                                       nnmail-procmail-directory)
+                                  "\\(.*\\)" nnmail-procmail-suffix "$")
+                          (expand-file-name file))
+            (substring (expand-file-name file)
+                       (match-beginning 1) (match-end 1)))
+           (t
+            group))
+    group))
+
+(defun nnmail-split-incoming (incoming func &optional dont-kill group)
   "Go through the entire INCOMING file and pick out each individual mail.
 FUNC will be called with the buffer narrowed to each mail."
   (let ((delim (concat "^" rmail-unix-mail-delimiter))
-       start end)
+       ;; If this is a group-specific split, we bind the split
+       ;; methods to just this group.
+       (nnmail-split-methods (if (and group
+                                      (or (eq nnmail-spool-file 'procmail)
+                                          nnmail-use-procmail)
+                                      (not nnmail-resplit-incoming))
+                                 (list (list group ""))
+                               nnmail-split-methods))
+       start end content-length do-search)
     (save-excursion
       (set-buffer (get-buffer-create " *nnmail incoming*"))
       (buffer-disable-undo (current-buffer))
@@ -392,11 +491,35 @@ FUNC will be called with the buffer narrowed to each mail."
            ;; Skip all the headers in case there are more "From "s...
            (if (not (search-forward "\n\n" nil t))
                (forward-line 1))
+           ;; Look for a Content-Length header.
+           (if (not (save-excursion
+                      (and (re-search-backward 
+                            "^Content-Length: \\([0-9]+\\)" start t)
+                           (setq content-length (string-to-int
+                                                 (buffer-substring 
+                                                  (match-beginning 1)
+                                                  (match-end 1))))
+                           ;; We destroy the header, since none of
+                           ;; the backends ever use it, and we do not
+                           ;; want to confuse other mailers by having
+                           ;; a (possibly) faulty header.
+                           (progn (insert "X-") t))))
+               (setq do-search t)
+             (if (save-excursion
+                   (condition-case nil
+                       (forward-char content-length)
+                     (end-of-buffer nil))
+                   (looking-at delim))
+                 (progn
+                   (forward-char content-length)
+                   (setq do-search nil))
+               (setq do-search t)))
            ;; Go to the beginning of the next article - or to the end
            ;; of the buffer.  
-           (if (re-search-forward delim nil t)
-               (goto-char (match-beginning 0))
-             (goto-char (point-max)))
+           (if do-search
+               (if (re-search-forward delim nil t)
+                   (goto-char (match-beginning 0))
+                 (goto-char (point-max))))
            (save-excursion
              (save-restriction
                (narrow-to-region start (point))
@@ -415,52 +538,60 @@ FUNC will be called with the group name to determine the article number."
   (let ((methods nnmail-split-methods)
        (obuf (current-buffer))
        (beg (point-min))
-       end found group-art)
-    (save-excursion
-      ;; Find headers.
-      (goto-char beg)
-      (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
-      (set-buffer (get-buffer-create " *nnmail work*"))
-      (buffer-disable-undo (current-buffer))
-      (erase-buffer)
-      ;; Copy the headers into the work buffer.
-      (insert-buffer-substring obuf beg end)
-      ;; Fold continuation lines.
-      (goto-char (point-min))
-      (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
-       (replace-match " " t t))
-      (if (and (symbolp nnmail-split-methods)
-              (fboundp nnmail-split-methods))
-         (setq group-art
-               (mapcar
-                (lambda (group) (cons group (funcall func group)))
-                (funcall nnmail-split-methods)))
-       ;; Go throught the split methods to find a match.
-       (while (and methods (or nnmail-crosspost (not group-art)))
-         (goto-char (point-max))
-         (if (or (cdr methods)
-                 (not (equal "" (nth 1 (car methods)))))
-             (if (and (condition-case () 
-                          (if (stringp (nth 1 (car methods)))
-                              (re-search-backward
-                               (car (cdr (car methods))) nil t)
-                            ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
-                            (funcall (nth 1 (car methods)) 
-                                     (car (car methods))))
-                        (error nil))
-                      ;; Don't enter the article into the same group twice.
-                      (not (assoc (car (car methods)) group-art)))
-                 (setq group-art
-                       (cons (cons (car (car methods))
-                                   (funcall func (car (car methods)))) 
-                             group-art)))
-           (or group-art
-               (setq group-art 
-                     (list (cons (car (car methods)) 
-                                 (funcall func (car (car methods))))))))
-         (setq methods (cdr methods))))
-      (kill-buffer (current-buffer))
-      group-art)))
+       end group-art)
+    (if (and (sequencep methods) (= (length methods) 1))
+       ;; If there is only just one group to put everything in, we
+       ;; just return a list with just this one method in.
+       (setq group-art
+             (list (cons (car (car methods))
+                         (funcall func (car (car methods))))))
+      ;; We do actual comparison.
+      (save-excursion
+       ;; Find headers.
+       (goto-char beg)
+       (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
+       (set-buffer (get-buffer-create " *nnmail work*"))
+       (buffer-disable-undo (current-buffer))
+       (erase-buffer)
+       ;; Copy the headers into the work buffer.
+       (insert-buffer-substring obuf beg end)
+       ;; Fold continuation lines.
+       (goto-char (point-min))
+       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
+         (replace-match " " t t))
+       (if (and (symbolp nnmail-split-methods)
+                (fboundp nnmail-split-methods))
+           (setq group-art
+                 (mapcar
+                  (lambda (group) (cons group (funcall func group)))
+                  (funcall nnmail-split-methods)))
+         ;; Go throught the split methods to find a match.
+         (while (and methods (or nnmail-crosspost (not group-art)))
+           (goto-char (point-max))
+           (if (or (cdr methods)
+                   (not (equal "" (nth 1 (car methods)))))
+               (if (and (condition-case () 
+                            (if (stringp (nth 1 (car methods)))
+                                (re-search-backward
+                                 (car (cdr (car methods))) nil t)
+                              ;; Suggested by Brian Edmonds 
+                              ;; <edmonds@cs.ubc.ca>.
+                              (funcall (nth 1 (car methods)) 
+                                       (car (car methods))))
+                          (error nil))
+                        ;; Don't enter the article into the same group twice.
+                        (not (assoc (car (car methods)) group-art)))
+                   (setq group-art
+                         (cons (cons (car (car methods))
+                                     (funcall func (car (car methods)))) 
+                               group-art)))
+             (or group-art
+                 (setq group-art 
+                       (list (cons (car (car methods)) 
+                                   (funcall func (car (car methods))))))))
+           (setq methods (cdr methods))))
+       (kill-buffer (current-buffer))
+       group-art))))
 
 (defun nnmail-insert-lines ()
   "Insert how many lines and chars there are in the body of the mail."
@@ -504,6 +635,89 @@ FUNC will be called with the group name to determine the article number."
       (setq newname (make-temp-name newprefix)))
     newname))
 
+;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
+
+(defun nnmail-split-fancy ()
+  "Fancy splitting method.
+See the documentation for the variable `nnmail-split-fancy' for documentation."
+  (nnmail-split-it nnmail-split-fancy))
+
+(defvar nnmail-split-cache nil)
+;; Alist of split expresions their equivalent regexps.
+
+(defun nnmail-split-it (split)
+  ;; Return a list of groups matching SPLIT.
+  (cond ((stringp split)
+        ;; A group.
+        (list split))
+       ((eq (car split) '&)
+        (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
+       ((eq (car split) '|)
+        (let (done)
+          (while (and (not done) (cdr split))
+            (setq split (cdr split)
+                  done (nnmail-split-it (car split))))
+          done))       ((assq split nnmail-split-cache)
+        ;; A compiled match expression.
+        (goto-char (point-max))
+        (if (re-search-backward (cdr (assq split nnmail-split-cache)) nil t)
+            (nnmail-split-it (nth 2 split))))
+       (t
+        ;; An uncompiled match.
+        (let* ((field (nth 0 split))
+               (value (nth 1 split))
+               (regexp (concat "^\\(" 
+                                (if (symbolp field)
+                                    (cdr (assq field 
+                                               nnmail-split-abbrev-alist))
+                                  field)
+                                "\\):.*\\<\\("
+                                (if (symbolp value)
+                                    (cdr (assq value
+                                               nnmail-split-abbrev-alist))
+                                  value)
+                                "\\>\\)")))
+          (setq nnmail-split-cache 
+                (cons (cons split regexp) nnmail-split-cache))
+          (goto-char (point-max))
+          (if (re-search-backward regexp nil t)
+              (nnmail-split-it (nth 2 split)))))))
+
+;; Get a list of spool files to read.
+(defun nnmail-get-spool-files (&optional group)
+  (if (null nnmail-spool-file)
+      ;; No spool file whatsoever.
+      nil)
+  (let* ((procmails 
+         ;; If procmail is used to get incoming mail, the files
+         ;; are stored in this directory.
+         (and (file-exists-p nnmail-procmail-directory)
+              (directory-files 
+               nnmail-procmail-directory 
+               t (concat (if group group "")
+                         nnmail-procmail-suffix "$") t)))
+        (p procmails))
+    ;; Remove any directories that inadvertantly match the procmail
+    ;; suffix, which might happen if the suffix is "".
+    (while p
+      (and (or (file-directory-p (car p))
+              (file-symlink-p (car p)))
+          (setq procmails (delete (car p) procmails)))
+      (setq p (cdr p)))
+    (cond ((listp nnmail-spool-file)
+          (append nnmail-spool-file procmails))
+         ((stringp nnmail-spool-file)
+          (cons nnmail-spool-file procmails))
+         (t
+          procmails))))
+
+(defun nnmail-activate (backend)
+  (if (not (symbol-value (intern (format "%s-group-alist" backend))))
+      (save-excursion
+       (funcall (intern (format "%s-request-list" backend)))
+       (set (intern (format "%s-group-alist" backend)) (nnmail-get-active)))))
+
+
 (provide 'nnmail)
 
-;;; nnml.el ends here
+;;; nnmail.el ends here