* gnus-offline.el: Rewrite to work with pGnus v0.74 based T-gnus.
[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.17
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   "Sadness like snow"           ; 1.17
97   )
98 (defconst pop3-fma-version (format "Multiple POP3 account utiliy for Gnus v%s - \"%s\""
99                                        pop3-fma-version-number
100                                        pop3-fma-codename))
101
102 (defcustom pop3-fma-spool-file-alist nil
103   "*Spool file to get mail using pop3 protocol.
104 You should specify this variable like
105  '(
106    (\"po:user1@mailhost1\" type)
107    (\"po:user2@mailhost2\" type)
108   )
109 Type must be pass or apop."
110   :group 'pop3-fma
111   :type 'alist)
112
113 (defcustom pop3-fma-local-spool-file-alist nil
114   "*List of Local spool file to get mail."
115   :group 'pop3-fma
116   :type 'alist)
117
118 (defcustom pop3-fma-movemail-type 'lisp
119   "*Type of movemail program.
120 Lisp means `nnmail-movemail-program' is lisp function.
121  Exe means `nnmail-movemail-program' is external program.
122  Please do not use exe if you do not use Meadow."
123   :group 'pop3-fma
124   :type '(choice (const lisp)
125                  (const exe)))
126
127 (defcustom pop3-fma-movemail-arguments '("-pf")
128   "*Options for movemail."
129   :group 'pop3-fma
130   :type '(repeat (string :tag "Argument")))
131
132 (defcustom pop3-fma-save-password-information nil
133   "*If non nil , save POP Server's password information.
134 ============== Important notice =====================
135 Please take care of your password information.
136 If set to t , your pop3 password is saved in pop3-fma-password in raw text.
137 So , Anybody can see this information by describe-variable.
138 If there is any problem , please set this variable to nil(default).
139 ============== Important notice ====================="
140   :group 'pop3-fma
141   :type 'boolean)
142
143 ;;; Internal variables.
144 (defvar pop3-fma-password nil
145   "*POP3 password , user , mailhost information for Gnus.")
146
147 (defvar pop3-fma-movemail-program
148   (if (eq system-type 'windows-nt)
149       "movemail.exe"
150     "movemail")
151   "*External program name your movemail.")
152
153
154 ;; Temporary variable
155 (defvar hdr nil)
156 (defvar passwd nil)
157 (defvar str nil)
158 (defvar spool nil)
159 (defvar movemail-output-buffer " *movemail-out*")
160 (defvar pop3-fma-commandline-arguments nil)
161
162 ;;; To silence byte compiler
163 (and
164  (fboundp 'eval-when-compile)
165  (eval-when-compile
166    (save-excursion
167      (beginning-of-defun)
168      (eval-region (point-min) (point)))
169    (let (case-fold-search)
170      (mapcar
171       (function
172        (lambda (symbol)
173          (unless (boundp symbol)
174            (make-local-variable symbol)
175            (eval (list 'setq symbol nil)))))
176       '(:group
177         :prefix :type
178         pop3-maildrop
179         pop3-mailhost
180         ))
181      (make-local-variable 'byte-compile-warnings)
182      (setq byte-compile-warnings nil))))
183
184 (defun pop3-fma-init-message-hook ()
185   (add-hook 'message-send-hook 'pop3-fma-message-add-header))
186
187 (eval-after-load "message"
188   '(pop3-fma-init-message-hook))
189
190 (add-hook 'gnus-after-exiting-gnus-hook
191           '(lambda () (setq pop3-fma-password nil)))
192 (add-hook 'gnus-before-startup-hook 'pop3-fma-set-pop3-password)
193
194 ;;
195 ;;
196 ;; Gnus POP3 additional utility...
197 ;;
198 (defun pop3-fma-movemail (inbox crashbox)
199   "Function to move mail from INBOX on a pop3 server to file CRASHBOX."
200   (if (string-match "^po:" inbox)
201       (progn
202         (let ((pop3-maildrop
203                (substring inbox (match-end (string-match "^po:" inbox))
204                           (- (match-end (string-match "^.*@" inbox)) 1)))
205               (pop3-mailhost
206                (substring inbox (match-end (string-match "^.*@" inbox))))
207               (pop3-password
208                (if pop3-fma-save-password-information
209                    (pop3-fma-read-passwd (substring inbox (match-end (string-match "^.*@" inbox))))
210                  (pop3-fma-input-password
211                   (substring inbox (match-end (string-match "^.*@" inbox)))
212                   (substring inbox (match-end (string-match "^po:" inbox))
213                              (- (match-end (string-match "^.*@" inbox)) 1)))))
214               (pop3-authentication-scheme
215                (nth 1 (assoc inbox pop3-fma-spool-file-alist))))
216 ;;            (pop3-fma-movemail-type (pop3-fma-get-movemail-type inbox)))
217           (if (eq pop3-authentication-scheme 'pass)
218               (message "Checking new mail user %s at %s using USER/PASS ..." pop3-maildrop pop3-mailhost)
219             (message "Checking new mail user %s at %s using APOP ..." pop3-maildrop pop3-mailhost))
220           (if (and (eq system-type 'windows-nt)
221                    (eq pop3-fma-movemail-type 'exe))
222               (progn
223                 (setenv "MAILHOST" pop3-mailhost)
224                 (if (and (not (memq pop3-password pop3-fma-commandline-arguments))
225                          (not (memq (concat "po:" pop3-maildrop) pop3-fma-commandline-arguments)))
226                     (progn
227                       (if (eq pop3-authentication-scheme 'apop)
228                           (setq pop3-fma-commandline-arguments
229                                 (append
230                                  pop3-fma-movemail-arguments
231                                  (list
232                                   "-A"
233                                   (concat "po:" pop3-maildrop)
234                                   crashbox
235                                   pop3-password)))
236                         (setq pop3-fma-commandline-arguments
237                               (append
238                                pop3-fma-movemail-arguments
239                                (list
240                                 (concat "po:" pop3-maildrop)
241                                 crashbox
242                                 pop3-password))))))
243                 (if (not (get-buffer movemail-output-buffer))
244                     (get-buffer-create movemail-output-buffer))
245                 (set-buffer movemail-output-buffer)
246                 (erase-buffer)
247                 (apply 'call-process (concat
248                                       exec-directory
249                                       pop3-fma-movemail-program)
250                        nil movemail-output-buffer nil
251                        pop3-fma-commandline-arguments)
252                 (let ((string (buffer-string)))
253                   (if (> (length string) 0)
254                       (progn
255                         (if (y-or-n-p
256                              (concat (substring string 0
257                                                 (- (length string) 1))
258                                                 " continue ??"))
259                             nil
260                           nil)))))
261             (pop3-movemail crashbox))))
262     (message "Checking new mail at %s ... " inbox)
263     (call-process (concat exec-directory pop3-fma-movemail-program)
264                   nil
265                   nil
266                   nil
267                   inbox
268                   crashbox)
269     (message "Checking new mail at %s ... done." inbox)))
270 ;;
271 ;;
272 (defun pop3-fma-read-passwd (mailhost)
273   (setq passwd (nth 2 (assoc mailhost pop3-fma-password)))
274   passwd)
275
276 (defun pop3-fma-input-password (mailhost maildrop)
277   (pop3-fma-read-noecho
278    (format "POP Password for %s at %s: " maildrop mailhost) t))
279
280 (setq pop3-read-passwd 'pop3-fma-read-passwd
281       nnmail-read-passwd 'pop3-fma-read-passwd)
282 ;;
283 ;; Set multiple pop3 server's password
284 (defun pop3-fma-store-password (passwd)
285   (interactive
286    (list (pop3-fma-read-noecho
287           (format "POP Password for %s at %s: " pop3-maildrop pop3-mailhost) t)))
288   (if (not (assoc pop3-mailhost pop3-fma-password))
289       (setq pop3-fma-password
290             (append pop3-fma-password
291                     (list
292                      (list
293                       pop3-mailhost
294                       pop3-maildrop
295                       passwd)))))
296     (setcar (cdr (cdr (assoc pop3-mailhost pop3-fma-password)))
297             passwd)
298     (message "POP password registered.")
299     passwd)
300 ;;
301 ;;;###autoload
302 (defun pop3-fma-set-pop3-password()
303   (interactive)
304   (if pop3-fma-save-password-information
305       (progn
306         (mapcar
307          (lambda (x)
308            (let ((pop3-maildrop
309                   (substring (car x) (match-end (string-match "^po:" (car x)))
310                              (- (match-end (string-match "^.*@" (car x))) 1)))
311                  (pop3-mailhost
312                   (substring (car x) (match-end (string-match "^.*@" (car x))))))
313              (call-interactively 'pop3-fma-store-password)))
314          pop3-fma-spool-file-alist)))
315   (setq nnmail-movemail-program 'pop3-fma-movemail)
316 ;;  (setq nnmail-spool-file pop3-fma-spool-file-alist))
317   (setq nnmail-spool-file (append
318                            pop3-fma-local-spool-file-alist
319                            (mapcar
320                             (lambda (spool)
321                               (car spool))
322                             pop3-fma-spool-file-alist))))
323 ;;
324 (defmacro pop3-fma-read-char-exclusive ()
325   (cond ((featurep 'xemacs)
326          '(let ((table (quote ((backspace . ?\C-h) (delete . ?\C-?)
327                                (left . ?\C-h))))
328                 event key)
329             (while (not
330                     (and
331                      (key-press-event-p (setq event (next-command-event)))
332                      (setq key (or (event-to-character event)
333                                    (cdr (assq (event-key event) table)))))))
334             key))
335         ((fboundp 'read-char-exclusive)
336          '(read-char-exclusive))
337         (t
338          '(read-char))))
339 ;;
340 (defun pop3-fma-read-noecho (prompt &optional stars)
341   "Read a single line of text from user without echoing, and return it.
342 Argument PROMPT ."
343   (let ((ans "")
344         (c 0)
345         (echo-keystrokes 0)
346         (cursor-in-echo-area t)
347         (log-message-max-size 0)
348         message-log-max done msg truncate)
349     (while (not done)
350       (if (or (not stars) (string-equal "" ans))
351           (setq msg prompt)
352         (setq msg (concat prompt (make-string (length ans) ?*)))
353         (setq truncate
354               (1+ (- (length msg) (window-width (minibuffer-window)))))
355         (and (> truncate 0)
356              (setq msg (concat "$" (substring msg (1+ truncate))))))
357       (message msg)
358       (setq c (pop3-fma-read-char-exclusive))
359       (cond ((eq ?\C-g c)
360              (setq quit-flag t
361                    done t))
362             ((memq c '(?\r ?\n ?\e))
363              (setq done t))
364             ((eq ?\C-u c)
365              (setq ans ""))
366             ((and (/= ?\b c) (/= ?\177 c))
367              (setq ans (concat ans (char-to-string c))))
368             ((> (length ans) 0)
369              (setq ans (substring ans 0 -1)))))
370     (if quit-flag
371         (prog1
372             (setq quit-flag nil)
373           (message "Quit")
374           (beep t))
375       (message "")
376       ans)))
377 ;;
378 ;;
379 (defun pop3-fma-message-add-header ()
380   (if (message-mail-p)
381       (pop3-fma-add-custom-header "X-Ya-Pop3:" pop3-fma-version)))
382   
383 ;;
384 ;; Add your custom header.
385 (defun pop3-fma-add-custom-header (header string)
386   (let ((delimline
387          (progn (goto-char (point-min))
388                 (re-search-forward
389                  (concat "^" (regexp-quote mail-header-separator) "\n"))
390                 (point-marker))))
391     (goto-char (point-min))
392     (or (re-search-forward (concat "^" header) delimline t)
393         (progn
394           (goto-char delimline)
395           (forward-line -1)
396           (beginning-of-line)
397           (setq hdr (concat header " "))
398           (setq str (concat hdr string))
399           (setq hdr (concat str "\n"))
400           (insert-string hdr)))))
401 ;;
402 ;;
403 (defun pop3-fma-get-movemail-type (inbox)
404   (if (eq (nth 1 (assoc inbox pop3-fma-spool-file-alist)) 'apop)
405       'lisp
406     pop3-fma-movemail-type))
407 ;;
408 (provide 'pop3-fma)
409 ;;
410 ;; pop3-fma.el ends here.
411
412