New feature: toggle visibility of mime buttons.
[more-wl.git] / elmo / elmo-net.el
blob7abbd79fcb6a44861bfafc865485f6cb1eba1699
1 ;;; elmo-net.el --- Network module for ELMO.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
29 ;;; Code:
31 (eval-when-compile (require 'cl))
33 (require 'elmo-util)
34 (require 'elmo-dop)
35 (require 'elmo-vars)
36 (require 'elmo-cache)
37 (require 'elmo)
39 (defconst elmo-net-folder-name-syntax '((?@ [server ".+"])
40 (?: [port "^[0-9]+$"])
41 (?! stream-type)))
43 ;;; ELMO net folder
44 (eval-and-compile
45 (luna-define-class elmo-net-folder
46 (elmo-folder)
47 (user auth server port stream-type))
48 (luna-define-internal-accessors 'elmo-net-folder))
50 ;;; Session
51 (eval-and-compile
52 (autoload 'starttls-negotiate "starttls")
53 (autoload 'sasl-find-mechanism "sasl")
54 (autoload 'sasl-make-client "sasl")
55 (autoload 'sasl-mechanism-name "sasl")
56 (autoload 'sasl-next-step "sasl")
57 (autoload 'sasl-step-data "sasl")
58 (autoload 'sasl-step-set-data "sasl"))
60 (defvar sasl-mechanisms)
62 (defcustom elmo-network-session-idle-timeout nil
63 "Idle timeout of the network cache. Specified in seconds.
64 If elapsed time since last access is larger than this value,
65 cached session is not reused.
66 If nil, network cache is reused."
67 :type '(choice number (const nil))
68 :group 'elmo)
70 (defcustom elmo-network-session-retry-count nil
71 "Retry count for authentication when open network session.
72 If nil, just once. If t, until success."
73 :type '(choice (integer :tag "Times")
74 (const :tag "Just once" nil)
75 (const :tag "Until success" t))
76 :group 'elmo)
78 ;;; Code:
80 (eval-and-compile
81 (luna-define-class elmo-network-session () (name
82 server
83 port
84 user
85 auth
86 stream-type
87 process
88 greeting
89 last-accessed))
90 (luna-define-internal-accessors 'elmo-network-session))
92 (luna-define-generic elmo-network-initialize-session (session)
93 "Initialize SESSION (Called before authentication).")
95 (luna-define-generic elmo-network-initialize-session-buffer (session buffer)
96 "Initialize SESSION's BUFFER.")
98 (luna-define-generic elmo-network-authenticate-session (session)
99 "Authenticate SESSION.")
101 (luna-define-generic elmo-network-setup-session (session)
102 "Setup SESSION. (Called after authentication).")
104 (luna-define-generic elmo-network-close-session (session)
105 "Close SESSION.")
107 (luna-define-method
108 elmo-network-initialize-session-buffer ((session
109 elmo-network-session) buffer)
110 (with-current-buffer buffer
111 (set-buffer-multibyte nil)
112 (buffer-disable-undo (current-buffer))))
114 (luna-define-method elmo-network-close-session ((session elmo-network-session))
115 (when (elmo-network-session-process-internal session)
116 ;;; (memq (process-status (elmo-network-session-process-internal session))
117 ;;; '(open run))
118 (kill-buffer (process-buffer
119 (elmo-network-session-process-internal session)))
120 (delete-process (elmo-network-session-process-internal session))))
122 (defmacro elmo-network-stream-type-spec-string (stream-type)
123 `(nth 0 ,stream-type))
125 (defmacro elmo-network-stream-type-symbol (stream-type)
126 `(nth 1 ,stream-type))
128 (defmacro elmo-network-stream-type-feature (stream-type)
129 `(nth 2 ,stream-type))
131 (defmacro elmo-network-stream-type-function (stream-type)
132 `(nth 3 ,stream-type))
134 (defsubst elmo-network-session-password-key (session)
135 (format "%s:%s/%s@%s:%d"
136 (upcase
137 (nth 1 (split-string (symbol-name
138 (luna-class-name session)) "[4-]")))
139 (elmo-network-session-user-internal session)
140 (elmo-network-session-auth-internal session)
141 (elmo-network-session-server-internal session)
142 (elmo-network-session-port-internal session)))
144 (defvar elmo-network-session-cache nil)
146 (defsubst elmo-network-session-cache-key (name folder)
147 "Returns session cache key for NAME and FOLDER."
148 (format "%s:%s/%s@%s:%d%s"
149 name
150 (elmo-net-folder-user-internal folder)
151 (elmo-net-folder-auth-internal folder)
152 (elmo-net-folder-server-internal folder)
153 (elmo-net-folder-port-internal folder)
155 (elmo-network-stream-type-spec-string
156 (elmo-net-folder-stream-type-internal folder)) "")))
158 (defun elmo-network-clear-session-cache ()
159 "Clear session cache."
160 (interactive)
161 (dolist (pair elmo-network-session-cache)
162 (elmo-network-close-session (cdr pair)))
163 (setq elmo-network-session-cache nil))
165 (defsubst elmo-network-session-buffer-name (session)
166 (format " *%s session for %s@%s:%d%s"
167 (elmo-network-session-name-internal session)
168 (elmo-network-session-user-internal session)
169 (elmo-network-session-server-internal session)
170 (elmo-network-session-port-internal session)
171 (or (elmo-network-stream-type-spec-string
172 (elmo-network-session-stream-type-internal session))
173 "")))
175 (defmacro elmo-network-session-buffer (session)
176 "Get buffer for SESSION."
177 `(process-buffer (elmo-network-session-process-internal ,session)))
179 (defun elmo-network-get-session (class name folder &optional if-exists)
180 "Get network session from session cache or a new network session.
181 CLASS is the class name of the session.
182 NAME is the name of the process.
183 FOLDER is the ELMO folder structure.
184 Returns a `elmo-network-session' instance.
185 If optional argument IF-EXISTS is non-nil, it does not return session
186 if there is no session cache.
187 if making session failed, returns nil."
188 (let (pair session key)
189 (if (not (elmo-plugged-p
190 (elmo-net-folder-server-internal folder)
191 (elmo-net-folder-port-internal folder)
192 (elmo-network-stream-type-symbol
193 (elmo-net-folder-stream-type-internal folder))))
194 (error "Unplugged"))
195 (setq pair (assoc (setq key (elmo-network-session-cache-key name folder))
196 elmo-network-session-cache))
197 (when (and pair
198 (or (not (memq (process-status
199 (elmo-network-session-process-internal
200 (cdr pair)))
201 '(open run)))
202 (and elmo-network-session-idle-timeout
203 (elmo-network-session-last-accessed-internal
204 (cdr pair))
205 (elmo-time-expire
206 (elmo-network-session-last-accessed-internal
207 (cdr pair))
208 elmo-network-session-idle-timeout))))
209 (setq elmo-network-session-cache
210 (delq pair elmo-network-session-cache))
211 (elmo-network-close-session (cdr pair))
212 (setq pair nil))
213 (if pair
214 (progn
215 (elmo-network-session-set-last-accessed-internal
216 (cdr pair) (current-time))
217 (cdr pair)) ; connection cache exists.
218 (unless if-exists
219 (setq session
220 (elmo-network-open-session
221 class
222 name
223 (elmo-net-folder-server-internal folder)
224 (elmo-net-folder-port-internal folder)
225 (elmo-net-folder-user-internal folder)
226 (elmo-net-folder-auth-internal folder)
227 (elmo-net-folder-stream-type-internal folder)))
228 (setq elmo-network-session-cache
229 (cons (cons key session)
230 elmo-network-session-cache))
231 session))))
233 (defun elmo-network-session-buffer-create (session)
234 (let ((buffer-name (elmo-network-session-buffer-name session))
235 buffer)
236 (when (get-buffer buffer-name)
237 (kill-buffer buffer-name))
238 (setq buffer (get-buffer-create buffer-name))
239 (elmo-network-initialize-session-buffer session buffer)
240 buffer))
242 (defun elmo-network-open-session (class name server port user auth
243 stream-type)
244 "Open an authenticated network session.
245 CLASS is the class name of the session.
246 NAME is the name of the process.
247 SERVER is the name of the server server.
248 PORT is the port number of the service.
249 USER is the user-id for the authenticate.
250 AUTH is the authenticate method name (symbol).
251 STREAM-TYPE is the stream type (See also `elmo-network-stream-type-alist').
252 Returns a process object. if making session failed, returns nil."
253 (let ((session
254 (luna-make-entity class
255 :name name
256 :server server
257 :port port
258 :user user
259 :auth auth
260 :stream-type stream-type
261 :process nil
262 :greeting nil
263 :last-accessed (current-time)))
264 (retry elmo-network-session-retry-count)
265 success)
266 (while (not success)
267 (condition-case error
268 (when (elmo-network-session-set-process-internal
269 session
270 (elmo-open-network-stream
271 (elmo-network-session-name-internal session)
272 (elmo-network-session-buffer-create session)
273 server port stream-type))
274 (elmo-network-initialize-session session)
275 (elmo-network-authenticate-session session)
276 (elmo-network-setup-session session)
277 (setq success t))
278 (elmo-authenticate-error
279 (elmo-remove-passwd (elmo-network-session-password-key session))
280 (message "Authetication is failed")
281 (sit-for 1)
282 (elmo-network-close-session session)
283 (unless (if (numberp retry)
284 (> (setq retry (1- retry)) 0)
285 retry)
286 (signal (car error) (cdr error))))
287 (elmo-open-error
288 (elmo-set-plugged nil server port
289 (elmo-network-stream-type-symbol stream-type)
290 (current-time))
291 (message "Auto plugged off at %s:%d :%s" server port (cadr error))
292 (sit-for 1)
293 (elmo-network-close-session session)
294 (signal (car error) (cdr error)))
295 (error
296 (elmo-network-close-session session)
297 (signal (car error) (cdr error)))))
298 session))
300 (defun elmo-open-network-stream (name buffer server service stream-type)
301 (let ((auto-plugged (and elmo-auto-change-plugged
302 (> elmo-auto-change-plugged 0)))
303 process)
304 (if (and stream-type
305 (elmo-network-stream-type-feature stream-type))
306 (require (elmo-network-stream-type-feature stream-type)))
307 (condition-case err
308 (let (process-connection-type)
309 (as-binary-process
310 (setq process
311 (if stream-type
312 (funcall (elmo-network-stream-type-function stream-type)
313 name buffer server service)
314 (open-network-stream name buffer server service)))))
315 (error
316 (when auto-plugged
317 (elmo-set-plugged nil server service
318 (elmo-network-stream-type-symbol stream-type)
319 (current-time))
320 (message "Auto plugged off at %s:%d" server service)
321 (sit-for 1))
322 (signal (car err) (cdr err))))
323 (when process
324 (process-kill-without-query process)
325 (when auto-plugged
326 (elmo-set-plugged t server service
327 (elmo-network-stream-type-symbol stream-type)))
328 process)))
330 (defun elmo-get-network-stream-type (symbol)
331 "Return network stream type corresponding to SYMBOL.
332 Returned value is searched from `elmo-network-stream-type-alist'."
333 (let ((alist elmo-network-stream-type-alist)
334 spec)
335 (while alist
336 (when (eq (nth 1 (car alist)) symbol)
337 (setq spec (car alist))
338 (setq alist nil))
339 (setq alist (cdr alist)))
340 spec))
342 (defun elmo-net-folder-set-parameters (folder params &optional defaults)
343 (let ((port (cdr (assq 'port params)))
344 (stream-type (cdr (assq 'stream-type params))))
345 ;; server
346 (elmo-net-folder-set-server-internal
347 folder
348 (or (cdr (assq 'server params))
349 (plist-get defaults :server)))
350 ;; port
351 (elmo-net-folder-set-port-internal
352 folder
353 (or (and port (string-to-number port))
354 (plist-get defaults :port)))
355 ;; stream-type
356 (elmo-net-folder-set-stream-type-internal
357 folder
358 (or (and stream-type
359 (assoc (concat "!" stream-type) elmo-network-stream-type-alist))
360 (plist-get defaults :stream-type)))))
362 (luna-define-method elmo-folder-initialize ((folder elmo-net-folder) name)
363 ;; user and auth should be set in subclass.
364 (when (string-match "\\(@[^@:/!]+\\)?\\(:[0-9]+\\)?\\(!.*\\)?$" name)
365 (elmo-net-folder-set-parameters
366 folder
367 (car (elmo-parse-separated-tokens
368 (substring name (match-beginning 0))
369 elmo-net-folder-name-syntax))))
370 folder)
372 (luna-define-method elmo-net-port-info ((folder elmo-net-folder))
373 (list (elmo-net-folder-server-internal folder)
374 (elmo-net-folder-port-internal folder)
375 (elmo-network-stream-type-symbol
376 (elmo-net-folder-stream-type-internal folder))))
378 (defun elmo-net-port-label (folder)
379 (concat
380 (symbol-name (elmo-folder-type-internal folder))
381 (if (elmo-net-folder-stream-type-internal folder)
382 (concat "!" (symbol-name
383 (elmo-network-stream-type-symbol
384 (elmo-net-folder-stream-type-internal
385 folder)))))))
387 (luna-define-method elmo-folder-plugged-p ((folder elmo-net-folder))
388 (apply 'elmo-plugged-p
389 (append (elmo-net-port-info folder)
390 (list nil (quote (elmo-net-port-label folder))))))
392 (luna-define-method elmo-folder-set-plugged ((folder elmo-net-folder)
393 plugged &optional add)
394 (apply 'elmo-set-plugged plugged
395 (append (elmo-net-port-info folder)
396 (list nil nil (quote (elmo-net-port-label folder)) add))))
398 (luna-define-method elmo-folder-create ((folder elmo-net-folder))
399 (if (elmo-folder-plugged-p folder)
400 (elmo-folder-send folder 'elmo-folder-create-plugged)
401 (elmo-folder-send folder 'elmo-folder-create-unplugged)))
403 (luna-define-method elmo-folder-create-unplugged ((folder elmo-net-folder))
404 (if elmo-enable-disconnected-operation
405 (elmo-folder-create-dop folder)
406 (error "Unplugged")))
408 (luna-define-method elmo-folder-exists-p ((folder elmo-net-folder))
409 (if (elmo-folder-plugged-p folder)
410 (elmo-folder-send folder 'elmo-folder-exists-p-plugged)
411 ;; If unplugged, guess by msgdb.
412 (file-directory-p (elmo-folder-msgdb-path folder))))
414 (luna-define-method elmo-folder-status ((folder elmo-net-folder))
415 (if (elmo-folder-plugged-p folder)
416 (elmo-folder-send folder 'elmo-folder-status-plugged)
417 (elmo-folder-send folder 'elmo-folder-status-unplugged)))
419 (luna-define-method elmo-folder-status-unplugged
420 ((folder elmo-net-folder))
421 (if elmo-enable-disconnected-operation
422 (elmo-folder-status-dop folder)
423 (error "Unplugged")))
425 (luna-define-method elmo-folder-next-message-number ((folder elmo-net-folder))
426 (if (elmo-folder-plugged-p folder)
427 (elmo-folder-send folder 'elmo-folder-next-message-number-plugged)
428 (elmo-folder-send folder 'elmo-folder-next-message-number-unplugged)))
430 (luna-define-method elmo-folder-next-message-number-unplugged
431 ((folder elmo-net-folder))
432 (if elmo-enable-disconnected-operation
433 (elmo-folder-next-message-number-dop folder)
434 (error "Unplugged")))
436 (luna-define-method elmo-folder-list-messages-internal
437 ((folder elmo-net-folder) &optional nohide)
438 (elmo-net-folder-list-messages-internal folder nohide))
440 (defun elmo-net-folder-list-messages-internal (folder nohide)
441 (if (elmo-folder-plugged-p folder)
442 (elmo-folder-send folder 'elmo-folder-list-messages-plugged nohide)
443 (elmo-folder-send folder 'elmo-folder-list-messages-unplugged)))
445 (luna-define-method elmo-folder-list-messages-plugged
446 ((folder elmo-net-folder))
447 nil)
449 ;; Should consider offline append and removal.
450 (luna-define-method elmo-folder-list-messages-unplugged ((folder
451 elmo-net-folder))
452 (if elmo-enable-disconnected-operation
453 (let ((deleting (elmo-dop-list-deleting-messages folder)))
454 (nconc
455 ;; delete deleting messages
456 (elmo-delete-if
457 (lambda (number) (memq number deleting))
458 ;; current number-list.
459 (elmo-folder-list-messages folder nil 'in-msgdb))
460 ;; append appending messages
461 (mapcar (lambda (x) (* -1 x))
462 (elmo-dop-spool-folder-list-messages folder))))
465 (luna-define-method elmo-folder-list-flagged-internal ((folder elmo-net-folder)
466 flag)
467 (if (and (elmo-folder-plugged-p folder)
468 (elmo-folder-use-flag-p folder))
469 (elmo-folder-send folder 'elmo-folder-list-flagged-plugged flag)
470 ;; Should consider offline append and removal?
473 (luna-define-method elmo-folder-list-flagged-plugged ((folder elmo-net-folder)
474 flag)
477 (luna-define-method elmo-folder-delete-messages-internal ((folder
478 elmo-net-folder)
479 numbers)
480 (if (elmo-folder-plugged-p folder)
481 (elmo-folder-send folder 'elmo-folder-delete-messages-plugged numbers)
482 (elmo-folder-send folder 'elmo-folder-delete-messages-unplugged numbers)))
484 (luna-define-method elmo-folder-delete-messages-unplugged ((folder
485 elmo-net-folder)
486 numbers)
487 (elmo-folder-delete-messages-dop folder numbers))
489 (luna-define-method elmo-folder-msgdb-create ((folder elmo-net-folder)
490 numbers flag-table)
491 (if (elmo-folder-plugged-p folder)
492 (elmo-folder-send folder 'elmo-folder-msgdb-create-plugged
493 numbers flag-table)
494 (elmo-folder-send folder 'elmo-folder-msgdb-create-unplugged
495 numbers flag-table)))
497 (luna-define-method elmo-folder-msgdb-create-unplugged ((folder
498 elmo-net-folder)
499 numbers
500 flag-table)
501 ;; XXXX should be appended to already existing msgdb.
502 (elmo-dop-msgdb
503 (elmo-folder-msgdb-create (elmo-dop-spool-folder folder)
504 (mapcar 'abs numbers)
505 flag-table)))
507 (luna-define-method elmo-folder-set-flag :before ((folder elmo-net-folder)
508 numbers
509 flag
510 &optional is-local)
511 (when (and (not is-local)
512 (elmo-folder-use-flag-p folder))
513 (elmo-folder-send folder
514 (if (elmo-folder-plugged-p folder)
515 'elmo-folder-set-flag-plugged
516 'elmo-folder-set-flag-unplugged)
517 numbers
518 flag)))
520 (luna-define-method elmo-folder-unset-flag :before ((folder elmo-net-folder)
521 numbers
522 flag
523 &optional is-local)
524 (when (and (not is-local)
525 (elmo-folder-use-flag-p folder))
526 (elmo-folder-send folder
527 (if (elmo-folder-plugged-p folder)
528 'elmo-folder-unset-flag-plugged
529 'elmo-folder-unset-flag-unplugged)
530 numbers
531 flag)))
533 (luna-define-method elmo-folder-set-flag-unplugged ((folder elmo-net-folder)
534 numbers flag)
535 (elmo-folder-set-flag-dop folder numbers flag))
537 (luna-define-method elmo-folder-unset-flag-unplugged ((folder elmo-net-folder)
538 numbers flag)
539 (elmo-folder-unset-flag-dop folder numbers flag))
541 (luna-define-method elmo-message-encache :around ((folder elmo-net-folder)
542 number &optional read)
543 (if (elmo-folder-plugged-p folder)
544 (luna-call-next-method)
545 (if elmo-enable-disconnected-operation
546 (elmo-message-encache-dop folder number read)
547 (error "Unplugged"))))
549 (luna-define-generic elmo-message-fetch-plugged (folder number strategy
550 &optional
551 section
552 outbuf
553 unseen)
556 (luna-define-generic elmo-message-fetch-unplugged (folder number strategy
557 &optional
558 section
559 outbuf
560 unseen)
563 (luna-define-method elmo-message-fetch-internal ((folder elmo-net-folder)
564 number strategy
565 &optional section unseen)
566 (if (elmo-folder-plugged-p folder)
567 (elmo-message-fetch-plugged folder number
568 strategy section
569 (current-buffer) unseen)
570 (elmo-message-fetch-unplugged folder number
571 strategy section
572 (current-buffer) unseen)))
574 (luna-define-method elmo-message-fetch-unplugged
575 ((folder elmo-net-folder) number strategy &optional section outbuf unseen)
576 (if (and elmo-enable-disconnected-operation
577 (< number 0))
578 (elmo-message-fetch-internal
579 (elmo-dop-spool-folder folder) (abs number) strategy
580 section unseen)
581 (error "Unplugged")))
583 (luna-define-method elmo-folder-check ((folder elmo-net-folder))
584 (if (elmo-folder-plugged-p folder)
585 (elmo-folder-send folder 'elmo-folder-check-plugged)))
587 (luna-define-method elmo-folder-close :after ((folder elmo-net-folder))
588 (if (elmo-folder-plugged-p folder)
589 (elmo-folder-send folder 'elmo-folder-check-plugged)))
591 (luna-define-method elmo-folder-diff :around ((folder elmo-net-folder))
592 (if (and (elmo-folder-use-flag-p folder)
593 (elmo-folder-plugged-p folder))
594 (elmo-folder-send folder 'elmo-folder-diff-plugged)
595 (luna-call-next-method)))
597 (luna-define-method elmo-folder-local-p ((folder elmo-net-folder))
598 nil)
600 (luna-define-method elmo-quit ((folder elmo-net-folder))
601 (elmo-network-clear-session-cache))
603 (require 'product)
604 (product-provide (provide 'elmo-net) (require 'elmo-version))
606 ;;; elmo-net.el ends here