Importing Pterodactyl Gnus v0.73.
[elisp/gnus.git-] / lisp / mail-source.el
1 ;;; mail-source.el --- functions for fetching mail
2 ;; Copyright (C) 1999 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news, mail
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29 (eval-and-compile
30   (autoload 'pop3-movemail "pop3"))
31
32 (defgroup mail-source nil
33   "The mail-fetching library."
34   :group 'gnus)
35
36 (defcustom mail-source-movemail-program "movemail"
37   "*A command to be executed to move mail from the inbox.
38 The default is \"movemail\".
39
40 This can also be a function.  In that case, the function will be
41 called with two parameters -- the name of the INBOX file, and the file
42 to be moved to."
43   :group 'mail-source
44   :type '(choice string
45                  function))
46
47 (defcustom mail-source-movemail-args nil
48   "*Extra arguments to give to `mail-source-movemail-program'  to move mail from the inbox.
49 The default is nil."
50   :group 'mail-source
51   :type '(choice string
52                  (constant nil)))
53
54 (defcustom mail-source-crash-box "~/.emacs-mail-crash-box"
55   "File where mail will be stored while processing it."
56   :group 'mail-source
57   :type 'file)
58
59 (defcustom mail-source-directory "~/Mail/"
60   "Directory where files (if any) will be stored."
61   :group 'mail-source
62   :type 'directory)
63
64 (defcustom mail-source-default-file-modes 384
65   "Set the mode bits of all new mail files to this integer."
66   :group 'mail-source
67   :type 'integer)
68
69 (defcustom mail-source-delete-incoming nil
70   "*If non-nil, delete incoming files after handling."
71   :group 'mail-source
72   :type 'boolean)
73
74 ;;; Internal variables.
75
76 (eval-and-compile
77   (defvar mail-source-keyword-map
78     '((file
79        (:path (or (getenv "MAIL")
80                   (concat "/usr/spool/mail/" (user-login-name)))))
81       (directory
82        (:path)
83        (:suffix ".spool")
84        (:match))
85       (pop
86        (:server (getenv "MAILHOST"))
87        (:port "pop3")
88        (:user (or (user-login-name) (getenv "LOGNAME") (getenv "USER")))
89        (:password))
90       (maildir
91        (:path)))
92     "Mapping from keywords to default values.
93 All keywords that can be used must be listed here."))
94
95 (defvar mail-source-fetcher-alist
96   '((file mail-source-fetch-file)
97     (directory mail-source-fetch-directory)
98     (pop mail-source-fetch-pop)
99     (qmail mail-source-fetch-qmail))
100   "A mapping from source type to fetcher function.")
101
102 (defvar mail-source-password-cache nil)
103
104 ;;; Functions
105
106 (eval-and-compile
107   (defun mail-source-strip-keyword (keyword)
108   "Strip the leading colon off the KEYWORD."
109   (intern (substring (symbol-name keyword) 1))))
110
111 (eval-when-compile
112   (defun mail-source-bind-1 (type)
113     (let* ((defaults (cdr (assq type mail-source-keyword-map)))
114            default bind)
115       (while (setq default (pop defaults))
116         (push (list (mail-source-strip-keyword (car default))
117                     nil)
118               bind))
119       bind)))
120
121 (defmacro mail-source-bind (type source &rest body)
122   "Bind all variables in SOURCE."
123   `(let ,(mail-source-bind-1 type)
124      (mail-source-set-1 source)
125      ,@body))
126
127 (put 'mail-source-bind 'lisp-indent-function 2)
128 (put 'mail-source-bind 'edebug-form-spec '(form form body))
129
130 (defun mail-source-set-1 (source)
131   (let* ((type (pop source))
132          (defaults (cdr (assq type mail-source-keyword-map)))
133          default value keyword)
134     (while (setq default (pop defaults))
135       (set (mail-source-strip-keyword (setq keyword (car default)))
136            (if (setq value (plist-get source keyword))
137                (mail-source-value value)
138              (mail-source-value (cadr default)))))))
139
140 (defun mail-source-value (value)
141   "Return the value of VALUE."
142   (cond
143    ;; String
144    ((stringp value)
145     value)
146    ;; Function
147    ((and (listp value)
148          (functionp (car value)))
149     (eval value))
150    ;; Variable
151    ((and (symbolp value)
152          (boundp value))
153     (symbol-value value))
154    ;; Just return the value.
155    (t
156     value)))
157
158 (defun mail-source-fetch (source callback)
159   "Fetch mail from SOURCE and call CALLBACK zero or more times.
160 CALLBACK will be called with the name of the file where (some of)
161 the mail from SOURCE is put.
162 Return the number of files that were found."
163   (let ((function (cadr (assq (car source) mail-source-fetcher-alist)))
164         (found 0))
165     (unless function
166       (error "%S is an invalid mail source specification" source))
167     ;; If there's anything in the crash box, we do it first.
168     (when (file-exists-p mail-source-crash-box)
169       (message "Processing mail from %s..." mail-source-crash-box)
170       (setq found (mail-source-callback
171                    callback mail-source-crash-box)))
172     (+ found (funcall function source callback))))
173
174 (defun mail-source-make-complex-temp-name (prefix)
175   (let ((newname (make-temp-name prefix))
176         (newprefix prefix))
177     (while (file-exists-p newname)
178       (setq newprefix (concat newprefix "x"))
179       (setq newname (make-temp-name newprefix)))
180     newname))
181
182 (defun mail-source-callback (callback info)
183   "Call CALLBACK on the mail file, and then remove the mail file.
184 Pass INFO on to CALLBACK."
185   (if (or (not (file-exists-p mail-source-crash-box))
186           (zerop (nth 7 (file-attributes mail-source-crash-box))))
187       (progn
188         (delete-file mail-source-crash-box)
189         0)
190     (funcall callback mail-source-crash-box info)
191     (if mail-source-delete-incoming
192         (delete-file mail-source-crash-box)
193       (let ((incoming
194              (mail-source-make-complex-temp-name
195               (expand-file-name
196                "Incoming" mail-source-directory))))
197         (unless (file-exists-p (file-name-directory incoming))
198           (make-directory (file-name-directory incoming) t))
199         (rename-file mail-source-crash-box incoming t)))
200     1))
201
202 (defun mail-source-movemail (from to)
203   "Move FROM to TO using movemail."
204   (if (not (file-writable-p to))
205       (error "Can't write to crash box %s.  Not moving mail" to)
206     (let ((to (file-truename (expand-file-name to)))
207           errors result)
208       (setq to (file-truename to)
209             from (file-truename from))
210       ;; Set TO if have not already done so, and rename or copy
211       ;; the file FROM to TO if and as appropriate.
212       (cond
213        ((file-exists-p to)
214         ;; The crash box exists already.
215         t)
216        ((not (file-exists-p from))
217         ;; There is no inbox.
218         (setq to nil))
219        (t
220         ;; If getting from mail spool directory, use movemail to move
221         ;; rather than just renaming, so as to interlock with the
222         ;; mailer.
223         (unwind-protect
224             (save-excursion
225               (setq errors (generate-new-buffer " *mail source loss*"))
226               (buffer-disable-undo errors)
227               (if (functionp mail-source-movemail-program)
228                   (condition-case err
229                       (progn
230                         (funcall mail-source-movemail-program from to)
231                         (setq result 0))
232                     (error
233                      (save-excursion
234                        (set-buffer errors)
235                        (insert (prin1-to-string err))
236                        (setq result 255))))
237                 (let ((default-directory "/"))
238                   (setq result
239                         (apply
240                          'call-process
241                          (append
242                           (list
243                            (expand-file-name
244                             mail-source-movemail-program exec-directory)
245                            nil errors nil from to)
246                           (when mail-source-movemail-args
247                             mail-source-movemail-args))))))
248               (when (file-exists-p to)
249                 (set-file-modes to mail-source-default-file-modes))
250               (if (and (not (buffer-modified-p errors))
251                        (zerop result))
252                   ;; No output => movemail won.
253                   t
254                 (set-buffer errors)
255                 ;; There may be a warning about older revisions.  We
256                 ;; ignore that.
257                 (goto-char (point-min))
258                 (if (search-forward "older revision" nil t)
259                     t
260                   ;; Probably a real error.
261                   (subst-char-in-region (point-min) (point-max) ?\n ?\  )
262                   (goto-char (point-max))
263                   (skip-chars-backward " \t")
264                   (delete-region (point) (point-max))
265                   (goto-char (point-min))
266                   (when (looking-at "movemail: ")
267                     (delete-region (point-min) (match-end 0)))
268                   (unless (yes-or-no-p
269                            (format "movemail: %s (%d return).  Continue? "
270                                    (buffer-string) result))
271                     (error "%s" (buffer-string)))
272                   (setq to nil)))))))
273       (when (buffer-name errors)
274         (kill-buffer errors))
275       ;; Return whether we moved successfully or not.
276       to)))
277
278 (defvar mail-source-read-passwd nil)
279 (defun mail-source-read-passwd (prompt &rest args)
280   "Read a password using PROMPT.
281 If ARGS, PROMPT is used as an argument to `format'."
282   (let ((prompt
283          (if args
284              (apply 'format prompt args)
285            prompt)))
286     (unless mail-source-read-passwd
287       (if (load "passwd" t)
288           (setq mail-source-read-passwd 'read-passwd)
289         (unless (fboundp 'ange-ftp-read-passwd)
290           (autoload 'ange-ftp-read-passwd "ange-ftp"))
291         (setq mail-source-read-passwd 'ange-ftp-read-passwd)))
292     (funcall mail-source-read-passwd prompt)))
293
294 (defun mail-source-fetch-file (source callback)
295   "Fetcher for single-file sources."
296   (mail-source-bind file source
297     (if (mail-source-movemail path mail-source-crash-box)
298         (mail-source-callback callback path)
299       0)))
300
301 (defun mail-source-fetch-directory (source callback)
302   "Fetcher for directory sources."
303   (mail-source-bind directory source
304     (let ((files (directory-files
305                   path t
306                   (or match (concat (regexp-quote suffix) "$"))))
307           (found 0)
308           file)
309       (while (setq file (pop files))
310         (when (mail-source-movemail file mail-source-crash-box)
311           (incf found (mail-source-callback callback file))))
312       found)))
313
314 (defun mail-source-fetch-pop (source callback)
315   "Fetcher for single-file sources."
316   (mail-source-bind pop source
317     (let ((from (format "%s:%s:%s" server user port)))
318       (setq password
319             (or password
320                 (cdr (assoc from mail-source-password-cache))
321                 (mail-source-read-passwd
322                  (format "Password for %s at %s: " user server))))
323       (unless (assoc from mail-source-password-cache)
324         (push (cons from password) mail-source-password-cache))
325       (let ((pop3-password password)
326             (pop3-maildrop user)
327             (pop3-mailhost server))
328         (if (pop3-movemail mail-source-crash-box)
329             (mail-source-callback callback server)
330           ;; We nix out the password in case the error
331           ;; was because of a wrong password being given.
332           (setq mail-source-password-cache
333                 (delq (assoc from mail-source-password-cache)
334                       mail-source-password-cache))
335           0)))))
336
337 (provide 'mail-source)
338
339 ;;; mail-source.el ends here