This commit was generated by cvs2svn to compensate for changes in r3968,
[elisp/gnus.git-] / lisp / pop3-fma.el
1 ;; pop3-fma.el.el --- POP3 for Multiple Account for Gnus.
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc. , Tatsuya Ichikawa
3 ;;                                                           Yasuo Okabe
4 ;; Author: Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
5 ;;         Yasuo OKABE <okabe@kuis.kyoto-u.ac.jp>
6 ;; Version: 1.16
7 ;; Keywords: mail , gnus , pop3
8 ;;
9 ;; SPECIAL THANKS
10 ;;    Keiichi Suzuki <kei-suzu@mail.wbs.or.jp>
11 ;;    Katsumi Yamaoka <yamaoka@jpl.org>
12 ;;
13 ;; This file is not part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;;; Commentary:
31 ;;
32 ;; Note.
33 ;;
34 ;; This file store pop3 password in variable "pop3-fma-password".
35 ;; Please take care by yourself to treat pop3 password.
36 ;;
37 ;; How to use.
38 ;;
39 ;; add your .emacs following codes.
40 ;;
41 ;;  (require 'pop3-fma)
42 ;;  (setq pop3-fma-spool-file-alist
43 ;;        '(
44 ;;          ("po:username0@mailhost0.your.domain0" pass)
45 ;;          ("po:username1@mailhost1.your.domain1" apop)
46 ;;                         :
47 ;;                         :
48 ;;         ))
49 ;;
50 ;;      pass means normal authentication USER/PASS.
51 ;;      apop means authentication using APOP.
52 ;;
53 ;; When using apop , Please set pop3-fma-movemail-type 'lisp.
54 ;; movemail.exe does not work on APOP protocol.
55 ;;
56 ;; Variables
57 ;;
58 ;;  pop3-fma-spool-file-alist      ... Spool file alist of POP3 protocol
59 ;;  pop3-fma-movemail-type         ... Type of movemail program.
60 ;;                                         'lisp or 'exe
61 ;;                                         'lisp use pop3.el
62 ;;                                         'exe use movemail
63 ;;  pop3-fma-movemail-arguments    ... List of options of movemail program.
64 ;;
65 ;;; Code:
66
67 (require 'cl)
68 (require 'custom)
69
70 (unless (and (condition-case ()
71                  (require 'custom)
72                (file-error nil))
73              (fboundp 'defgroup)
74              (fboundp 'defcustom))
75   (require 'backquote)
76   (defmacro defgroup (&rest args))
77   (defmacro defcustom (symbol value &optional doc &rest args)
78     (` (defvar (, symbol) (, value) (, doc))))
79   )
80
81 (defgroup pop3-fma nil
82   "Multile POP3 account utility for Gnus."
83   :prefix "pop3-fma-"
84   :group 'mail
85   :group 'news)
86
87 (defconst pop3-fma-version-number "1.16")
88 (defconst pop3-fma-codename
89 ;;  "J boy"                     ; 1.00
90 ;;  "Blood line"                ; 1.10
91 ;;  "Star ring"                 ; 1.11
92 ;;  "Goodbye Game"              ; 1.12
93 ;;  "Love is Gamble"            ; 1.13
94 ;;  "Lonely"                    ; 1.14
95   "Feel the wind"               ; 1.16
96   )
97 (defconst pop3-fma-version (format "Multiple POP3 account utiliy for Gnus v%s - \"%s\""
98                                        pop3-fma-version-number
99                                        pop3-fma-codename))
100
101 (defcustom pop3-fma-spool-file-alist nil
102   "*Spool file to get mail using pop3 protocol.
103 You should specify this variable like
104  '(
105    (\"po:user1@mailhost1\" type)
106    (\"po:user2@mailhost2\" type)
107   )
108 Type must be pass or apop."
109   :group 'pop3-fma
110   :type 'alist)
111
112 (defcustom pop3-fma-local-spool-file-alist nil
113   "*List of Local spool file to get mail."
114   :group 'pop3-fma
115   :type 'alist)
116
117 (defcustom pop3-fma-movemail-type 'lisp
118   "*Type of movemail program.
119 Lisp means `nnmail-movemail-program' is lisp function.
120  Exe means `nnmail-movemail-program' is external program.
121  Please do not use exe if you do not use Meadow."
122   :group 'pop3-fma
123   :type '(choice (const lisp)
124                  (const exe)))
125
126 (defcustom pop3-fma-movemail-arguments '("-pf")
127   "*Options for movemail."
128   :group 'pop3-fma
129   :type '(repeat (string :tag "Argument")))
130
131 (defcustom pop3-fma-save-password-information nil
132   "*If non nil , save POP Server's password information.
133 ============== Important notice =====================
134 Please take care of your password information.
135 If set to t , your pop3 password is saved in pop3-fma-password in raw text.
136 So , Anybody can see this information by describe-variable.
137 If there is any problem , please set this variable to nil(default).
138 ============== Important notice ====================="
139   :group 'pop3-fma
140   :type 'boolean)
141
142 ;;; Internal variables.
143 (defvar pop3-fma-password nil
144   "*POP3 password , user , mailhost information for Gnus.")
145
146 (defvar pop3-fma-movemail-program
147   (if (eq system-type 'windows-nt)
148       "movemail.exe"
149     "movemail")
150   "*External program name your movemail.")
151
152
153 ;; Temporary variable
154 (defvar hdr nil)
155 (defvar passwd nil)
156 (defvar str nil)
157 (defvar spool nil)
158 (defvar movemail-output-buffer " *movemail-out*")
159 (defvar pop3-fma-commandline-arguments nil)
160
161 ;;; To silence byte compiler
162 (and
163  (fboundp 'eval-when-compile)
164  (eval-when-compile
165    (save-excursion
166      (beginning-of-defun)
167      (eval-region (point-min) (point)))
168    (let (case-fold-search)
169      (mapcar
170       (function
171        (lambda (symbol)
172          (unless (boundp symbol)
173            (make-local-variable symbol)
174            (eval (list 'setq symbol nil)))))
175       '(:group
176         :prefix :type
177         pop3-maildrop
178         pop3-mailhost
179         ))
180      (make-local-variable 'byte-compile-warnings)
181      (setq byte-compile-warnings nil))))
182
183 (defun pop3-fma-init-message-hook ()
184   (add-hook 'message-send-hook 'pop3-fma-message-add-header))
185
186 (eval-after-load "message"
187   '(pop3-fma-init-message-hook))
188
189 (add-hook 'gnus-after-exiting-gnus-hook
190           '(lambda () (setq pop3-fma-password nil)))
191 (add-hook 'gnus-before-startup-hook 'pop3-fma-set-pop3-password)
192
193 ;;
194 ;;
195 ;; Gnus POP3 additional utility...
196 ;;
197 (defun pop3-fma-movemail (inbox crashbox)
198   "Function to move mail from INBOX on a pop3 server to file CRASHBOX."
199   (if (string-match "^po:" inbox)
200       (progn
201         (let ((pop3-maildrop
202                (substring inbox (match-end (string-match "^po:" inbox))
203                           (- (match-end (string-match "^.*@" inbox)) 1)))
204               (pop3-mailhost
205                (substring inbox (match-end (string-match "^.*@" inbox))))
206               (pop3-password
207                (if pop3-fma-save-password-information
208                    (pop3-fma-read-passwd (substring inbox (match-end (string-match "^.*@" inbox))))
209                  (pop3-fma-input-password
210                   (substring inbox (match-end (string-match "^.*@" inbox)))
211                   (substring inbox (match-end (string-match "^po:" inbox))
212                              (- (match-end (string-match "^.*@" inbox)) 1)))))
213               (pop3-authentication-scheme
214                (nth 1 (assoc inbox pop3-fma-spool-file-alist)))
215               (pop3-fma-movemail-type (pop3-fma-get-movemail-type inbox)))
216           (if (eq pop3-authentication-scheme 'pass)
217               (message "Checking new mail user %s at %s using USER/PASS ..." pop3-maildrop pop3-mailhost)
218             (message "Checking new mail user %s at %s using APOP ..." pop3-maildrop pop3-mailhost))
219           (if (and (eq system-type 'windows-nt)
220                    (eq pop3-fma-movemail-type 'exe))
221               (progn
222                 (setenv "MAILHOST" pop3-mailhost)
223                 (if (and (not (memq pop3-password pop3-fma-commandline-arguments))
224                          (not (memq (concat "po:" pop3-maildrop) pop3-fma-commandline-arguments)))
225                     (progn
226                       (setq pop3-fma-commandline-arguments
227                             (append
228                              pop3-fma-movemail-arguments
229                                     (list
230                                      (concat "po:" pop3-maildrop)
231                                      crashbox
232                                      pop3-password)))))
233                 (if (not (get-buffer movemail-output-buffer))
234                     (get-buffer-create movemail-output-buffer))
235                 (set-buffer movemail-output-buffer)
236                 (erase-buffer)
237                 (apply 'call-process (concat
238                                       exec-directory
239                                       pop3-fma-movemail-program)
240                        nil movemail-output-buffer nil
241                        pop3-fma-commandline-arguments)
242                 (let ((string (buffer-string)))
243                   (if (> (length string) 0)
244                       (progn
245                         (if (y-or-n-p
246                              (concat (substring string 0
247                                                 (- (length string) 1))
248                                                 " continue ??"))
249                             nil
250                           nil)))))
251             (pop3-movemail crashbox))))
252     (message "Checking new mail at %s ... " inbox)
253     (call-process (concat exec-directory pop3-fma-movemail-program)
254                   nil
255                   nil
256                   nil
257                   inbox
258                   crashbox)
259     (message "Checking new mail at %s ... done." inbox)))
260 ;;
261 ;;
262 (defun pop3-fma-read-passwd (mailhost)
263   (setq passwd (nth 2 (assoc mailhost pop3-fma-password)))
264   passwd)
265
266 (defun pop3-fma-input-password (mailhost maildrop)
267   (pop3-fma-read-noecho
268    (format "POP Password for %s at %s: " maildrop mailhost) t))
269
270 (setq pop3-read-passwd 'pop3-fma-read-passwd
271       nnmail-read-passwd 'pop3-fma-read-passwd)
272 ;;
273 ;; Set multiple pop3 server's password
274 (defun pop3-fma-store-password (passwd)
275   (interactive
276    (list (pop3-fma-read-noecho
277           (format "POP Password for %s at %s: " pop3-maildrop pop3-mailhost) t)))
278   (if (not (assoc pop3-mailhost pop3-fma-password))
279       (setq pop3-fma-password
280             (append pop3-fma-password
281                     (list
282                      (list
283                       pop3-mailhost
284                       pop3-maildrop
285                       passwd)))))
286     (setcar (cdr (cdr (assoc pop3-mailhost pop3-fma-password)))
287             passwd)
288     (message "POP password registered.")
289     passwd)
290 ;;
291 ;;;###autoload
292 (defun pop3-fma-set-pop3-password()
293   (interactive)
294   (if pop3-fma-save-password-information
295       (progn
296         (mapcar
297          (lambda (x)
298            (let ((pop3-maildrop
299                   (substring (car x) (match-end (string-match "^po:" (car x)))
300                              (- (match-end (string-match "^.*@" (car x))) 1)))
301                  (pop3-mailhost
302                   (substring (car x) (match-end (string-match "^.*@" (car x))))))
303              (call-interactively 'pop3-fma-store-password)))
304          pop3-fma-spool-file-alist)))
305   (setq nnmail-movemail-program 'pop3-fma-movemail)
306 ;;  (setq nnmail-spool-file pop3-fma-spool-file-alist))
307   (setq nnmail-spool-file (append
308                            pop3-fma-local-spool-file-alist
309                            (mapcar
310                             (lambda (spool)
311                               (car spool))
312                             pop3-fma-spool-file-alist))))
313 ;;
314 (defmacro pop3-fma-read-char-exclusive ()
315   (cond ((featurep 'xemacs)
316          '(let ((table (quote ((backspace . ?\C-h) (delete . ?\C-?)
317                                (left . ?\C-h))))
318                 event key)
319             (while (not
320                     (and
321                      (key-press-event-p (setq event (next-command-event)))
322                      (setq key (or (event-to-character event)
323                                    (cdr (assq (event-key event) table)))))))
324             key))
325         ((fboundp 'read-char-exclusive)
326          '(read-char-exclusive))
327         (t
328          '(read-char))))
329 ;;
330 (defun pop3-fma-read-noecho (prompt &optional stars)
331   "Read a single line of text from user without echoing, and return it.
332 Argument PROMPT ."
333   (let ((ans "")
334         (c 0)
335         (echo-keystrokes 0)
336         (cursor-in-echo-area t)
337         (log-message-max-size 0)
338         message-log-max done msg truncate)
339     (while (not done)
340       (if (or (not stars) (string-equal "" ans))
341           (setq msg prompt)
342         (setq msg (concat prompt (make-string (length ans) ?*)))
343         (setq truncate
344               (1+ (- (length msg) (window-width (minibuffer-window)))))
345         (and (> truncate 0)
346              (setq msg (concat "$" (substring msg (1+ truncate))))))
347       (message msg)
348       (setq c (pop3-fma-read-char-exclusive))
349       (cond ((eq ?\C-g c)
350              (setq quit-flag t
351                    done t))
352             ((memq c '(?\r ?\n ?\e))
353              (setq done t))
354             ((eq ?\C-u c)
355              (setq ans ""))
356             ((and (/= ?\b c) (/= ?\177 c))
357              (setq ans (concat ans (char-to-string c))))
358             ((> (length ans) 0)
359              (setq ans (substring ans 0 -1)))))
360     (if quit-flag
361         (prog1
362             (setq quit-flag nil)
363           (message "Quit")
364           (beep t))
365       (message "")
366       ans)))
367 ;;
368 ;;
369 (defun pop3-fma-message-add-header ()
370   (if (message-mail-p)
371       (pop3-fma-add-custom-header "X-Ya-Pop3:" pop3-fma-version)))
372   
373 ;;
374 ;; Add your custom header.
375 (defun pop3-fma-add-custom-header (header string)
376   (let ((delimline
377          (progn (goto-char (point-min))
378                 (re-search-forward
379                  (concat "^" (regexp-quote mail-header-separator) "\n"))
380                 (point-marker))))
381     (goto-char (point-min))
382     (or (re-search-forward (concat "^" header) delimline t)
383         (progn
384           (goto-char delimline)
385           (forward-line -1)
386           (beginning-of-line)
387           (setq hdr (concat header " "))
388           (setq str (concat hdr string))
389           (setq hdr (concat str "\n"))
390           (insert-string hdr)))))
391 ;;
392 ;;
393 (defun pop3-fma-get-movemail-type (inbox)
394   (if (eq (nth 1 (assoc inbox pop3-fma-spool-file-alist)) 'apop)
395       'lisp
396     pop3-fma-movemail-type))
397 ;;
398 (provide 'pop3-fma)
399 ;;
400 ;; pop3-fma.el ends here.
401
402