632601036008d1b2f03241a5ab252f7f71b2e76b
[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.10
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 (unless (and (fboundp 'pop3-fma-encode-string)
82              (fboundp 'pop3-fma-decode-string))
83   (require 'mel-b)
84   (fset 'pop3-fma-encode-string 'base64-encode-string)
85   (fset 'pop3-fma-decode-string 'base64-decode-string))
86
87 (defgroup pop3-fma nil
88   "Multile POP3 account utility for Gnus."
89   :prefix "pop3-fma-"
90   :group 'mail
91   :group 'news)
92
93 (defconst pop3-fma-version-number "1.10")
94 (defconst pop3-fma-codename
95 ;;  "Feel the wind"             ; 0.10
96 ;;  "My home town"              ; 0.11
97 ;;  "On the road"               ; 0.12
98 ;;  "Rock'n Roll city"          ; 0.13
99 ;;  "Money"                     ; 0.20
100 ;;  "Still 19"                  ; 0.21
101 ;;  "J boy"                     ; 1.00
102   "Blood line"                  ; 1.10
103 ;;  "Star ring"                 ; 0.xx
104 ;;  "Goodbye Game"              ; 0.xx
105   )
106 (defconst pop3-fma-version (format "Multiple POP3 account utiliy for Gnus v%s - \"%s\""
107                                        pop3-fma-version-number
108                                        pop3-fma-codename))
109
110 (defcustom pop3-fma-spool-file-alist nil
111   "*Spool file to get mail using pop3 protocol.
112 You should specify this variable like
113  '(
114    (\"po:user1@mailhost1\" type)
115    (\"po:user2@mailhost2\" type)
116   )
117 Type must be pass or apop."
118   :group 'pop3-fma
119   :type 'alist)
120
121 (defcustom pop3-fma-local-spool-file-alist nil
122   "*List of Local spool file to get mail."
123   :group 'pop3-fma
124   :type 'alist)
125
126 (defcustom pop3-fma-movemail-type 'lisp
127   "*Type of movemail program.
128 Lisp means `nnmail-movemail-program' is lisp function.
129  Exe means `nnmail-movemail-program' is external program.
130  Please do not use exe if you do not use Meadow."
131   :group 'pop3-fma
132   :type '(choice (const lisp)
133                  (const exe)))
134
135 (defcustom pop3-fma-movemail-arguments '("-pf")
136   "*Options for movemail."
137   :group 'pop3-fma
138   :type '(repeat (string :tag "Argument")))
139
140 ;;; Internal variables.
141 (defvar pop3-fma-password nil
142   "*POP3 password , user , mailhost information for Gnus.")
143
144 (defvar pop3-fma-movemail-program "movemail.exe"
145   "*External program name your movemail.
146 Please do not set this valiable non-nil if you do not use Meadow.")
147
148 ;; Temporary variable
149 (defvar hdr nil)
150 (defvar passwd nil)
151 (defvar str nil)
152 (defvar pop3-fma-movemail-options pop3-fma-movemail-arguments)
153 (defvar spool nil)
154
155 (defun pop3-fma-init-message-hook ()
156   (add-hook 'message-send-hook 'pop3-fma-message-add-header))
157
158 (eval-after-load "message"
159   '(pop3-fma-init-message-hook))
160
161 (add-hook 'gnus-after-exiting-gnus-hook
162           '(lambda () (setq pop3-fma-password nil)))
163 (add-hook 'gnus-before-startup-hook 'pop3-fma-set-pop3-password)
164
165 ;;
166 ;;
167 ;; Gnus POP3 additional utility...
168 ;;
169 (defun pop3-fma-movemail (inbox crashbox)
170   "Function to move mail from INBOX on a pop3 server to file CRASHBOX."
171   (if (string-match "^po:" inbox)
172       (progn
173         (let ((pop3-maildrop
174                (substring inbox (match-end (string-match "^po:" inbox))
175                           (- (match-end (string-match "^.*@" inbox)) 1)))
176               (pop3-mailhost
177                (substring inbox (match-end (string-match "^.*@" inbox))))
178               (pop3-password
179                (pop3-fma-read-passwd (substring inbox (match-end (string-match "^.*@" inbox)))))
180               (pop3-authentication-scheme
181                (nth 1 (assoc inbox pop3-fma-spool-file-alist)))
182               (pop3-fma-movemail-type (pop3-fma-get-movemail-type inbox)))
183           (if (eq pop3-authentication-scheme 'pass)
184               (message "Checking new mail user %s at %s using USER/PASS ..." pop3-maildrop pop3-mailhost)
185             (message "Checking new mail user %s at %s using APOP ..." pop3-maildrop pop3-mailhost))
186           (if (and (eq system-type 'windows-nt)
187                    (eq pop3-fma-movemail-type 'exe))
188               (progn
189                 (setenv "MAILHOST" pop3-mailhost)
190                 (if (and (not (memq pop3-password pop3-fma-movemail-arguments))
191                          (not (memq (concat "po:" pop3-maildrop) pop3-fma-movemail-arguments)))
192                     (progn
193                       (setq pop3-fma-movemail-arguments nil)
194                       (setq pop3-fma-movemail-arguments
195                             (append pop3-fma-movemail-options
196                                     (list
197                                      (concat "po:" pop3-maildrop)
198                                      crashbox
199                                      pop3-password)))))
200                 (apply 'call-process (concat
201                                       exec-directory
202                                       pop3-fma-movemail-program)
203                        nil nil nil
204                        pop3-fma-movemail-arguments))
205             (pop3-movemail crashbox))))
206     (message "Checking new mail at %s ... " inbox)
207     (call-process (concat exec-directory pop3-fma-movemail-program)
208                   nil
209                   nil
210                   nil
211                   inbox
212                   crashbox)
213     (message "Checking new mail at %s ... done." inbox)))
214 ;;
215 ;;
216 (defun pop3-fma-read-passwd (mailhost)
217   (setq passwd (nth 2 (assoc mailhost pop3-fma-password)))
218   (pop3-fma-decode-string passwd))
219
220 (setq pop3-read-passwd 'pop3-fma-read-passwd)
221 ;;
222 ;; Set multiple pop3 server's password
223 (defun pop3-fma-store-password (passwd)
224   (interactive
225    (list (pop3-fma-read-noecho
226           (format "POP Password for %s at %s: " pop3-maildrop pop3-mailhost) t)))
227   (if (not (assoc pop3-mailhost pop3-fma-password))
228       (setq pop3-fma-password
229             (append pop3-fma-password
230                     (list
231                      (list
232                       pop3-mailhost
233                       pop3-maildrop
234                       (pop3-fma-encode-string passwd)))))                     
235     (setcar (cdr (cdr (assoc pop3-mailhost pop3-fma-password)))
236             (pop3-fma-encode-string passwd)))
237   (message "POP password registered.")
238   (pop3-fma-encode-string passwd))
239 ;;
240 ;;;###autoload
241 (defun pop3-fma-set-pop3-password()
242   (interactive)
243   (mapcar
244    (lambda (x)
245      (let ((pop3-maildrop
246             (substring (car x) (match-end (string-match "^po:" (car x)))
247                        (- (match-end (string-match "^.*@" (car x))) 1)))
248            (pop3-mailhost
249             (substring (car x) (match-end (string-match "^.*@" (car x))))))
250        (call-interactively 'pop3-fma-store-password)))
251    pop3-fma-spool-file-alist)
252   (setq nnmail-movemail-program 'pop3-fma-movemail)
253 ;;  (setq nnmail-spool-file pop3-fma-spool-file-alist))
254   (setq nnmail-spool-file (append
255                            pop3-fma-local-spool-file-alist
256                            (mapcar
257                             (lambda (spool)
258                               (car spool))
259                             pop3-fma-spool-file-alist))))
260 ;;
261 (defun pop3-fma-read-noecho (prompt &optional stars)
262   "Read a single line of text from user without echoing, and return it.
263 Argument PROMPT ."
264   (let ((ans "")
265         (c 0)
266         (echo-keystrokes 0)
267         (cursor-in-echo-area t)
268         (log-message-max-size 0)
269         message-log-max done msg truncate)
270     (while (not done)
271       (if (or (not stars) (string-equal "" ans))
272           (setq msg prompt)
273         (setq msg (concat prompt (make-string (length ans) ?*)))
274         (setq truncate
275               (1+ (- (length msg) (window-width (minibuffer-window)))))
276         (and (> truncate 0)
277              (setq msg (concat "$" (substring msg (1+ truncate))))))
278       (message msg)
279       (setq c (read-char-exclusive))
280       (cond ((= c ?\C-g)
281              (setq quit-flag t
282                    done t))
283             ((or (= c ?\r) (= c ?\n) (= c ?\e))
284              (setq done t))
285             ((= c ?\C-u)
286              (setq ans ""))
287             ((and (/= c ?\b) (/= c ?\177))
288              (setq ans (concat ans (char-to-string c))))
289             ((> (length ans) 0)
290              (setq ans (substring ans 0 -1)))))
291     (if quit-flag
292         (prog1
293             (setq quit-flag nil)
294           (message "Quit")
295           (beep t))
296       (message "")
297       ans)))
298 ;;
299 ;;
300 (defun pop3-fma-message-add-header ()
301   (if (message-mail-p)
302       (pop3-fma-add-custom-header "X-Ya-Pop3:" pop3-fma-version)))
303   
304 ;;
305 ;; Add your custom header.
306 (defun pop3-fma-add-custom-header (header string)
307   (let ((delimline
308          (progn (goto-char (point-min))
309                 (re-search-forward
310                  (concat "^" (regexp-quote mail-header-separator) "\n"))
311                 (point-marker))))
312     (goto-char (point-min))
313     (or (re-search-forward (concat "^" header) delimline t)
314         (progn
315           (goto-char delimline)
316           (forward-line -1)
317           (beginning-of-line)
318           (setq hdr (concat header " "))
319           (setq str (concat hdr string))
320           (setq hdr (concat str "\n"))
321           (insert-string hdr)))))
322 ;;
323 ;;
324 (defun pop3-fma-get-movemail-type (inbox)
325   (if (eq (nth 1 (assoc inbox pop3-fma-spool-file-alist)) 'apop)
326       lisp
327     pop3-fma-movemail-type))
328 ;;
329 (provide 'pop3-fma)
330 ;;
331 ;; pop3-fma.el ends here.