2a7b20b6ed17ea82a2c25e67b7aa42f8f42d872e
[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.11
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 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.11")
77 (defconst pop3-fma-codename
78 ;  "Feel the wind"              ; 0.10
79   "My home town"                ; 0.11
80 ;  "On the road"                ; 0.xx
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 pop3-fma-movemail-options pop3-fma-movemail-arguments)
132
133 (defun pop3-fma-init-hooks ()
134   (add-hook 'message-send-hook 'pop3-fma-message-add-header))
135
136 (eval-after-load "message"
137   '(pop3-fma-init-hooks))
138 ;;
139 ;;
140 ;; Gnus POP3 additional utility...
141 ;;
142 (defun pop3-fma-movemail (inbox crashbox)
143   "Function to move mail from INBOX on a pop3 server to file CRASHBOX."
144   (let ((pop3-maildrop
145          (substring inbox (match-end (string-match "^po:" inbox))
146                     (- (match-end (string-match "^.*@" inbox)) 1)))
147         (pop3-mailhost
148          (substring inbox (match-end (string-match "^.*@" inbox)))))
149     (let ((pop3-password
150            (pop3-fma-read-passwd pop3-mailhost)))
151       (message "Checking new mail user %s at %s..." pop3-maildrop pop3-mailhost)
152       (if (and (eq system-type 'windows-nt)
153                (eq pop3-fma-movemail-type 'exe))
154           (progn
155             (setenv "MAILHOST" pop3-mailhost)
156             (if (and (not (memq pop3-password pop3-fma-movemail-arguments))
157                      (not (memq (concat "po:" pop3-maildrop) pop3-fma-movemail-arguments)))
158                 (progn
159                   (setq pop3-fma-movemail-arguments nil)
160                   (setq pop3-fma-movemail-arguments
161                       (append pop3-fma-movemail-options
162                               (list
163                                (concat "po:" pop3-maildrop)
164                                crashbox
165                                pop3-password)))))
166             (apply 'call-process (concat
167                                   exec-directory
168                                   pop3-fma-movemail-program)
169                    nil nil nil
170                    pop3-fma-movemail-arguments))
171         (pop3-movemail crashbox)))))
172 ;;
173 ;;
174 (defun pop3-fma-read-passwd (mailhost)
175   (setq passwd (nth 2 (assoc mailhost pop3-fma-password)))
176   passwd)
177
178 (setq pop3-read-passwd 'pop3-fma-read-passwd)
179 ;;
180 ;; Set multiple pop3 server's password
181 (defun pop3-fma-store-password (passwd)
182   (interactive
183    (list (pop3-fma-read-noecho
184           (format "POP Password for %s at %s: " pop3-maildrop pop3-mailhost) t)))
185   (if (not (assoc pop3-mailhost pop3-fma-password))
186       (setq pop3-fma-password
187             (append pop3-fma-password
188                     (list
189                      (list
190                       pop3-mailhost
191                       pop3-maildrop
192                       passwd))))
193     (setcar (cdr (cdr (assoc pop3-mailhost pop3-fma-password)))
194             passwd))
195   (message "POP password registered.")
196   passwd)
197 ;;
198 ;;;###autoload
199 (defun pop3-fma-set-pop3-password()
200   (interactive)
201   (mapcar
202    (lambda (x)
203      (let ((pop3-maildrop
204             (substring x (match-end (string-match "^po:" x))
205                        (- (match-end (string-match "^.*@" x)) 1)))
206            (pop3-mailhost
207             (substring x (match-end (string-match "^.*@" x)))))
208        (call-interactively 'pop3-fma-store-password)))
209    pop3-fma-spool-file-alist)
210   (setq nnmail-movemail-program 'pop3-fma-movemail)
211   (setq nnmail-spool-file pop3-fma-spool-file-alist))
212 ;;
213 (defun pop3-fma-read-noecho (prompt &optional stars)
214   "Read a single line of text from user without echoing, and return it.
215 Argument PROMPT ."
216   (let ((ans "")
217         (c 0)
218         (echo-keystrokes 0)
219         (cursor-in-echo-area t)
220         (log-message-max-size 0)
221         message-log-max done msg truncate)
222     (while (not done)
223       (if (or (not stars) (string-equal "" ans))
224           (setq msg prompt)
225         (setq msg (concat prompt (make-string (length ans) ?*)))
226         (setq truncate
227               (1+ (- (length msg) (window-width (minibuffer-window)))))
228         (and (> truncate 0)
229              (setq msg (concat "$" (substring msg (1+ truncate))))))
230       (message msg)
231       (setq c (read-char-exclusive))
232       (cond ((= c ?\C-g)
233              (setq quit-flag t
234                    done t))
235             ((or (= c ?\r) (= c ?\n) (= c ?\e))
236              (setq done t))
237             ((= c ?\C-u)
238              (setq ans ""))
239             ((and (/= c ?\b) (/= c ?\177))
240              (setq ans (concat ans (char-to-string c))))
241             ((> (length ans) 0)
242              (setq ans (substring ans 0 -1)))))
243     (if quit-flag
244         (prog1
245             (setq quit-flag nil)
246           (message "Quit")
247           (beep t))
248       (message "")
249       ans)))
250 ;;
251 ;;
252 (defun pop3-fma-message-add-header ()
253   (pop3-fma-add-custom-header "X-Ya-Pop3:" pop3-fma-version))
254   
255 ;;
256 ;; Add your custom header.
257 ;;
258 (defun pop3-fma-add-custom-header (header string)
259   (let ((delimline
260          (progn (goto-char (point-min))
261                 (re-search-forward
262                  (concat "^" (regexp-quote mail-header-separator) "\n"))
263                 (point-marker))))
264     (goto-char (point-min))
265     (or (re-search-forward (concat "^" header) delimline t)
266         (progn
267           (goto-char delimline)
268           (forward-line -1)
269           (beginning-of-line)
270           (setq hdr (concat header " "))
271           (setq str (concat hdr string))
272           (setq hdr (concat str "\n"))
273           (insert-string hdr)))))
274 ;;
275 (provide 'pop3-fma)
276 ;;
277 ;; pop3-fma.el ends here.
278
279 (provide 'pop3-fma)
280 ;;; pop3-fma.el ends here