Change lisp/pop3-fma.el to crypt/encrypt password using base64
[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: 0.20
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"
45 ;;          "po:username1@mailhost1.your.domain1"
46 ;;                         :
47 ;;                         :
48 ;;         ))
49 ;;
50 ;; Variables
51 ;;
52 ;;  pop3-fma-spool-file-alist      ... Spool file alist of POP3 protocol
53 ;;  pop3-fma-movemail-type         ... Type of movemail program.
54 ;;                                         'lisp or 'exe
55 ;;                                         'lisp use pop3.el
56 ;;                                         'exe use movemail
57 ;;  pop3-fma-movemail-arguments    ... List of options of movemail program.
58 ;;
59 ;;; Code:
60
61 (require 'cl)
62 (require 'custom)
63
64 (unless (and (condition-case ()
65                  (require 'custom)
66                (file-error nil))
67              (fboundp 'defgroup)
68              (fboundp 'defcustom))
69   (require 'backquote)
70   (defmacro defgroup (&rest args))
71   (defmacro defcustom (symbol value &optional doc &rest args)
72     (` (defvar (, symbol) (, value) (, doc))))
73   )
74
75 (unless (and (fboundp 'pop3-fma-encode-string)
76              (fboundp 'pop3-fma-decode-string))
77   (require 'mel-b)
78   (fset 'pop3-fma-encode-string 'base64-encode-string)
79   (fset 'pop3-fma-decode-string 'base64-decode-string))
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 "0.20")
88 (defconst pop3-fma-codename
89 ;;  "Feel the wind"             ; 0.10
90 ;;  "My home town"              ; 0.11
91 ;;  "On the road"               ; 0.12
92 ;;  "Rock'n Roll city"          ; 0.13
93   "Money"                       ; 0.20
94 ;;  "Midnight blue train"       ; 0.xx
95 ;;  "Still 19"                  ; 0.xx
96 ;;  "J boy"                     ; 0.xx
97 ;;  "Blood line"                ; 0.xx
98 ;;  "Star ring"                 ; 0.xx
99 ;;  "Goodbye Game"              ; 0.xx
100   )
101 (defconst pop3-fma-version (format "Multiple POP3 account utiliy for Gnus v%s - \"%s\""
102                                        pop3-fma-version-number
103                                        pop3-fma-codename))
104
105 (defcustom pop3-fma-spool-file-alist nil
106   "*Spoolfile to get mail using pop3 protocol.
107 You should specify this variable like
108  '(
109    \"po:user1@mailhost1\"
110    \"po:user2@mailhost2\"
111   )"
112   :group 'pop3-fma
113   :type 'alist)
114
115 (defcustom pop3-fma-movemail-type 'lisp
116   "*Type of movemail program.
117 Lisp means `nnmail-movemail-program' is lisp function.
118  Exe means `nnmail-movemail-program' is external program.
119  Please do not use exe if you do not use Meadow."
120   :group 'pop3-fma
121   :type '(choice (const lisp)
122                  (const exe)))
123
124 (defcustom pop3-fma-movemail-arguments '("-pf")
125   "*Options for movemail."
126   :group 'pop3-fma
127   :type '(repeat (string :tag "Argument")))
128
129 ;;; Internal variables.
130 (defvar pop3-fma-password nil
131   "*POP3 password , user , mailhost information for Gnus.")
132
133 (defvar pop3-fma-movemail-program "movemail.exe"
134   "*External program name your movemail.
135 Please do not set this valiable non-nil if you do not use Meadow.")
136
137 ;; Temporary variable
138 (defvar hdr nil)
139 (defvar passwd nil)
140 (defvar str nil)
141 (defvar pop3-fma-movemail-options pop3-fma-movemail-arguments)
142
143 (eval-after-load "message"
144   '(lambda ()
145      (add-hook 'message-send-hook 'pop3-fma-message-add-header)))
146 (add-hook 'gnus-after-exiting-gnus-hook
147           '(lambda () (setq pop3-fma-password nil)))
148 (add-hook 'gnus-before-startup-hook 'pop3-fma-set-pop3-password)
149
150 ;;
151 ;;
152 ;; Gnus POP3 additional utility...
153 ;;
154 (defun pop3-fma-movemail (inbox crashbox)
155   "Function to move mail from INBOX on a pop3 server to file CRASHBOX."
156   (let ((pop3-maildrop
157          (substring inbox (match-end (string-match "^po:" inbox))
158                     (- (match-end (string-match "^.*@" inbox)) 1)))
159         (pop3-mailhost
160          (substring inbox (match-end (string-match "^.*@" inbox)))))
161     (let ((pop3-password
162            (pop3-fma-read-passwd pop3-mailhost)))
163       (message "Checking new mail user %s at %s..." pop3-maildrop pop3-mailhost)
164       (if (and (eq system-type 'windows-nt)
165                (eq pop3-fma-movemail-type 'exe))
166           (progn
167             (setenv "MAILHOST" pop3-mailhost)
168             (if (and (not (memq pop3-password pop3-fma-movemail-arguments))
169                      (not (memq (concat "po:" pop3-maildrop) pop3-fma-movemail-arguments)))
170                 (progn
171                   (setq pop3-fma-movemail-arguments nil)
172                   (setq pop3-fma-movemail-arguments
173                       (append pop3-fma-movemail-options
174                               (list
175                                (concat "po:" pop3-maildrop)
176                                crashbox
177                                pop3-password)))))
178             (apply 'call-process (concat
179                                   exec-directory
180                                   pop3-fma-movemail-program)
181                    nil nil nil
182                    pop3-fma-movemail-arguments))
183         (pop3-movemail crashbox)))))
184 ;;
185 ;;
186 (defun pop3-fma-read-passwd (mailhost)
187   (setq passwd (nth 2 (assoc mailhost pop3-fma-password)))
188   (pop3-fma-decode-string passwd))
189
190 (setq pop3-read-passwd 'pop3-fma-read-passwd)
191 ;;
192 ;; Set multiple pop3 server's password
193 (defun pop3-fma-store-password (passwd)
194   (interactive
195    (list (pop3-fma-read-noecho
196           (format "POP Password for %s at %s: " pop3-maildrop pop3-mailhost) t)))
197   (if (not (assoc pop3-mailhost pop3-fma-password))
198       (setq pop3-fma-password
199             (append pop3-fma-password
200                     (list
201                      (list
202                       pop3-mailhost
203                       pop3-maildrop
204                       (pop3-fma-encode-string passwd)))))                     
205     (setcar (cdr (cdr (assoc pop3-mailhost pop3-fma-password)))
206             (pop3-fma-encode-string passwd)))
207   (message "POP password registered.")
208   (pop3-fma-encode-string passwd))
209 ;;
210 ;;;###autoload
211 (defun pop3-fma-set-pop3-password()
212   (interactive)
213   (mapcar
214    (lambda (x)
215      (let ((pop3-maildrop
216             (substring x (match-end (string-match "^po:" x))
217                        (- (match-end (string-match "^.*@" x)) 1)))
218            (pop3-mailhost
219             (substring x (match-end (string-match "^.*@" x)))))
220        (call-interactively 'pop3-fma-store-password)))
221    pop3-fma-spool-file-alist)
222   (setq nnmail-movemail-program 'pop3-fma-movemail)
223   (setq nnmail-spool-file pop3-fma-spool-file-alist))
224 ;;
225 (defun pop3-fma-read-noecho (prompt &optional stars)
226   "Read a single line of text from user without echoing, and return it.
227 Argument PROMPT ."
228   (let ((ans "")
229         (c 0)
230         (echo-keystrokes 0)
231         (cursor-in-echo-area t)
232         (log-message-max-size 0)
233         message-log-max done msg truncate)
234     (while (not done)
235       (if (or (not stars) (string-equal "" ans))
236           (setq msg prompt)
237         (setq msg (concat prompt (make-string (length ans) ?*)))
238         (setq truncate
239               (1+ (- (length msg) (window-width (minibuffer-window)))))
240         (and (> truncate 0)
241              (setq msg (concat "$" (substring msg (1+ truncate))))))
242       (message msg)
243       (setq c (read-char-exclusive))
244       (cond ((= c ?\C-g)
245              (setq quit-flag t
246                    done t))
247             ((or (= c ?\r) (= c ?\n) (= c ?\e))
248              (setq done t))
249             ((= c ?\C-u)
250              (setq ans ""))
251             ((and (/= c ?\b) (/= c ?\177))
252              (setq ans (concat ans (char-to-string c))))
253             ((> (length ans) 0)
254              (setq ans (substring ans 0 -1)))))
255     (if quit-flag
256         (prog1
257             (setq quit-flag nil)
258           (message "Quit")
259           (beep t))
260       (message "")
261       ans)))
262 ;;
263 ;;
264 (defun pop3-fma-message-add-header ()
265   (if (message-mail-p)
266       (pop3-fma-add-custom-header "X-Ya-Pop3:" pop3-fma-version)))
267   
268 ;;
269 ;; Add your custom header.
270 ;;
271 (defun pop3-fma-add-custom-header (header string)
272   (let ((delimline
273          (progn (goto-char (point-min))
274                 (re-search-forward
275                  (concat "^" (regexp-quote mail-header-separator) "\n"))
276                 (point-marker))))
277     (goto-char (point-min))
278     (or (re-search-forward (concat "^" header) delimline t)
279         (progn
280           (goto-char delimline)
281           (forward-line -1)
282           (beginning-of-line)
283           (setq hdr (concat header " "))
284           (setq str (concat hdr string))
285           (setq hdr (concat str "\n"))
286           (insert-string hdr)))))
287 ;;
288 (provide 'pop3-fma)
289 ;;
290 ;; pop3-fma.el ends here.