pop3-fma.el: Bug fix.
[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 ;; Author: Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
4 ;; Version: 0.12
5 ;; Keywords: mail , gnus , pop3
6 ;;
7 ;; SPECIAL THANKS
8 ;;    Keiichi Suzuki <kei-suzu@mail.wbs.or.jp>
9 ;;    Katsumi Yamaoka <yamaoka@jpl.org>
10 ;;
11 ;; This file is not part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29 ;;
30 ;;
31 ;; How to use.
32 ;;
33 ;; add your .emacs following codes.
34 ;;
35 ;;  (autoload 'pop3-fma-set-pop3-password "pop3-fma")
36 ;;  (add-hook 'gnus-load-hook 'pop3-fma-set-pop3-password)
37 ;;  (setq pop3-fma-spool-file-alist
38 ;;        '(
39 ;;          "po:username0@mailhost0.your.domain0"
40 ;;          "po:username1@mailhost1.your.domain1"
41 ;;                         :
42 ;;                         :
43 ;;         ))
44 ;;
45 ;; Variables
46 ;;
47 ;;  pop3-fma-spool-file-alist      ... Spool file alist of POP3 protocol
48 ;;  pop3-fma-movemail-type         ... Type of movemail program.
49 ;;                                         'lisp or 'exe
50 ;;                                         'lisp use pop3.el
51 ;;                                         'exe use movemail
52 ;;  pop3-fma-movemail-arguments    ... List of options of movemail program.
53 ;;
54 ;;; Code:
55
56 (require 'cl)
57 (require 'custom)
58
59 (unless (and (condition-case ()
60                  (require 'custom)
61                (file-error nil))
62              (fboundp 'defgroup)
63              (fboundp 'defcustom))
64   (require 'backquote)
65   (defmacro defgroup (&rest args))
66   (defmacro defcustom (symbol value &optional doc &rest args)
67     (` (defvar (, symbol) (, value) (, doc))))
68   )
69
70 (defgroup pop3-fma nil
71   "Multile POP3 account utility for Gnus."
72   :prefix "pop3-fma-"
73   :group 'mail
74   :group 'news)
75
76 (defconst pop3-fma-version-number "0.12")
77 (defconst pop3-fma-codename
78 ;;;  "Feel the wind"            ; 0.10
79 ;;  "My home town"              ; 0.11
80   "On the road"                 ; 0.12
81 ;;  "Before generation of Love" ; 0.xx
82 ;;  "Lonely Christmas eve"      ; 0.xx
83 ;;  "Rock'n Roll city"          ; 0.xx
84 ;;  "Money"                     ; 0.xx
85 ;;  "Midnight blue train"       ; 0.xx
86 ;;  "Still 19"                  ; 0.xx
87 ;;  "J boy"                     ; 0.xx
88 ;;  "Blood line"                ; 0.xx
89 ;;  "Star ring"                 ; 0.xx
90 ;;  "Goodbye Game"              ; 0.xx
91   )
92 (defconst pop3-fma-version (format "Multiple POP3 account utiliy for Gnus v%s - \"%s\""
93                                        pop3-fma-version-number
94                                        pop3-fma-codename))
95
96 (defcustom pop3-fma-spool-file-alist nil
97   "*Spoolfile to get mail using pop3 protocol.
98 You should specify this variable like
99  '(
100    \"po:user1@mailhost1\"
101    \"po:user2@mailhost2\"
102   )"
103   :group 'pop3-fma
104   :type 'alist)
105
106 (defcustom pop3-fma-movemail-type 'lisp
107   "*Type of movemail program.
108 Lisp means `nnmail-movemail-program' is lisp function.
109  Exe means `nnmail-movemail-program' is external program.
110  Please do not use exe if you do not use Meadow."
111   :group 'pop3-fma
112   :type '(choice (const lisp)
113                  (const exe)))
114
115 (defcustom pop3-fma-movemail-arguments '("-pf")
116   "*Options for movemail."
117   :group 'pop3-fma
118   :type '(repeat (string :tag "Argument")))
119
120 ;;; Internal variables.
121 (defvar pop3-fma-password nil
122   "*POP3 password , user , mailhost information for Gnus.")
123
124 (defvar pop3-fma-movemail-program "movemail.exe"
125   "*External program name your movemail.
126 Please do not set this valiable non-nil if you do not use Meadow.")
127
128 ;; Temporary variable
129 (defvar hdr nil)
130 (defvar passwd nil)
131 (defvar str nil)
132 (defvar pop3-fma-movemail-options pop3-fma-movemail-arguments)
133
134 (defun pop3-fma-init-hooks ()
135   (add-hook 'message-send-hook 'pop3-fma-message-add-header))
136
137 (eval-after-load "message"
138   '(pop3-fma-init-hooks))
139 ;;
140 ;;
141 ;; Gnus POP3 additional utility...
142 ;;
143 (defun pop3-fma-movemail (inbox crashbox)
144   "Function to move mail from INBOX on a pop3 server to file CRASHBOX."
145   (let ((pop3-maildrop
146          (substring inbox (match-end (string-match "^po:" inbox))
147                     (- (match-end (string-match "^.*@" inbox)) 1)))
148         (pop3-mailhost
149          (substring inbox (match-end (string-match "^.*@" inbox)))))
150     (let ((pop3-password
151            (pop3-fma-read-passwd pop3-mailhost)))
152       (message "Checking new mail user %s at %s..." pop3-maildrop pop3-mailhost)
153       (if (and (eq system-type 'windows-nt)
154                (eq pop3-fma-movemail-type 'exe))
155           (progn
156             (setenv "MAILHOST" pop3-mailhost)
157             (if (and (not (memq pop3-password pop3-fma-movemail-arguments))
158                      (not (memq (concat "po:" pop3-maildrop) pop3-fma-movemail-arguments)))
159                 (progn
160                   (setq pop3-fma-movemail-arguments nil)
161                   (setq pop3-fma-movemail-arguments
162                       (append pop3-fma-movemail-options
163                               (list
164                                (concat "po:" pop3-maildrop)
165                                crashbox
166                                pop3-password)))))
167             (apply 'call-process (concat
168                                   exec-directory
169                                   pop3-fma-movemail-program)
170                    nil nil nil
171                    pop3-fma-movemail-arguments))
172         (pop3-movemail crashbox)))))
173 ;;
174 ;;
175 (defun pop3-fma-read-passwd (mailhost)
176   (setq passwd (nth 2 (assoc mailhost pop3-fma-password)))
177   passwd)
178
179 (setq pop3-read-passwd 'pop3-fma-read-passwd)
180 ;;
181 ;; Set multiple pop3 server's password
182 (defun pop3-fma-store-password (passwd)
183   (interactive
184    (list (pop3-fma-read-noecho
185           (format "POP Password for %s at %s: " pop3-maildrop pop3-mailhost) t)))
186   (if (not (assoc pop3-mailhost pop3-fma-password))
187       (setq pop3-fma-password
188             (append pop3-fma-password
189                     (list
190                      (list
191                       pop3-mailhost
192                       pop3-maildrop
193                       passwd))))
194     (setcar (cdr (cdr (assoc pop3-mailhost pop3-fma-password)))
195             passwd))
196   (message "POP password registered.")
197   passwd)
198 ;;
199 ;;;###autoload
200 (defun pop3-fma-set-pop3-password()
201   (interactive)
202   (mapcar
203    (lambda (x)
204      (let ((pop3-maildrop
205             (substring x (match-end (string-match "^po:" x))
206                        (- (match-end (string-match "^.*@" x)) 1)))
207            (pop3-mailhost
208             (substring x (match-end (string-match "^.*@" x)))))
209        (call-interactively 'pop3-fma-store-password)))
210    pop3-fma-spool-file-alist)
211   (setq nnmail-movemail-program 'pop3-fma-movemail)
212   (setq nnmail-spool-file pop3-fma-spool-file-alist))
213 ;;
214 (defun pop3-fma-read-noecho (prompt &optional stars)
215   "Read a single line of text from user without echoing, and return it.
216 Argument PROMPT ."
217   (let ((ans "")
218         (c 0)
219         (echo-keystrokes 0)
220         (cursor-in-echo-area t)
221         (log-message-max-size 0)
222         message-log-max done msg truncate)
223     (while (not done)
224       (if (or (not stars) (string-equal "" ans))
225           (setq msg prompt)
226         (setq msg (concat prompt (make-string (length ans) ?*)))
227         (setq truncate
228               (1+ (- (length msg) (window-width (minibuffer-window)))))
229         (and (> truncate 0)
230              (setq msg (concat "$" (substring msg (1+ truncate))))))
231       (message msg)
232       (setq c (read-char-exclusive))
233       (cond ((= c ?\C-g)
234              (setq quit-flag t
235                    done t))
236             ((or (= c ?\r) (= c ?\n) (= c ?\e))
237              (setq done t))
238             ((= c ?\C-u)
239              (setq ans ""))
240             ((and (/= c ?\b) (/= c ?\177))
241              (setq ans (concat ans (char-to-string c))))
242             ((> (length ans) 0)
243              (setq ans (substring ans 0 -1)))))
244     (if quit-flag
245         (prog1
246             (setq quit-flag nil)
247           (message "Quit")
248           (beep t))
249       (message "")
250       ans)))
251 ;;
252 ;;
253 (defun pop3-fma-message-add-header ()
254   (if (message-mail-p)
255       (pop3-fma-add-custom-header "X-Ya-Pop3:" pop3-fma-version)))
256   
257 ;;
258 ;; Add your custom header.
259 ;;
260 (defun pop3-fma-add-custom-header (header string)
261   (let ((delimline
262          (progn (goto-char (point-min))
263                 (re-search-forward
264                  (concat "^" (regexp-quote mail-header-separator) "\n"))
265                 (point-marker))))
266     (goto-char (point-min))
267     (or (re-search-forward (concat "^" header) delimline t)
268         (progn
269           (goto-char delimline)
270           (forward-line -1)
271           (beginning-of-line)
272           (setq hdr (concat header " "))
273           (setq str (concat hdr string))
274           (setq hdr (concat str "\n"))
275           (insert-string hdr)))))
276 ;;
277 (provide 'pop3-fma)
278 ;;
279 ;; pop3-fma.el ends here.