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)
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.
31 (eval-when-compile (require 'cl
))
39 (defconst elmo-net-folder-name-syntax
'((?
@ [server
".+"])
40 (?
: [port
"^[0-9]+$"])
45 (luna-define-class elmo-net-folder
47 (user auth server port stream-type
))
48 (luna-define-internal-accessors 'elmo-net-folder
))
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
))
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
))
81 (luna-define-class elmo-network-session
() (name
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)
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))
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"
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"
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."
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
))
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
))))
195 (setq pair
(assoc (setq key
(elmo-network-session-cache-key name folder
))
196 elmo-network-session-cache
))
198 (or (not (memq (process-status
199 (elmo-network-session-process-internal
202 (and elmo-network-session-idle-timeout
203 (elmo-network-session-last-accessed-internal
206 (elmo-network-session-last-accessed-internal
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
))
215 (elmo-network-session-set-last-accessed-internal
216 (cdr pair
) (current-time))
217 (cdr pair
)) ; connection cache exists.
220 (elmo-network-open-session
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
))
233 (defun elmo-network-session-buffer-create (session)
234 (let ((buffer-name (elmo-network-session-buffer-name session
))
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
)
242 (defun elmo-network-open-session (class name server port user auth
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."
254 (luna-make-entity class
260 :stream-type stream-type
263 :last-accessed
(current-time)))
264 (retry elmo-network-session-retry-count
)
267 (condition-case error
268 (when (elmo-network-session-set-process-internal
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
)
278 (elmo-authenticate-error
279 (elmo-remove-passwd (elmo-network-session-password-key session
))
280 (message "Authetication is failed")
282 (elmo-network-close-session session
)
283 (unless (if (numberp retry
)
284 (> (setq retry
(1- retry
)) 0)
286 (signal (car error
) (cdr error
))))
288 (elmo-set-plugged nil server port
289 (elmo-network-stream-type-symbol stream-type
)
291 (message "Auto plugged off at %s:%d :%s" server port
(cadr error
))
293 (elmo-network-close-session session
)
294 (signal (car error
) (cdr error
)))
296 (elmo-network-close-session session
)
297 (signal (car error
) (cdr error
)))))
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)))
305 (elmo-network-stream-type-feature stream-type
))
306 (require (elmo-network-stream-type-feature stream-type
)))
308 (let (process-connection-type)
312 (funcall (elmo-network-stream-type-function stream-type
)
313 name buffer server service
)
314 (open-network-stream name buffer server service
)))))
317 (elmo-set-plugged nil server service
318 (elmo-network-stream-type-symbol stream-type
)
320 (message "Auto plugged off at %s:%d" server service
)
322 (signal (car err
) (cdr err
))))
324 (process-kill-without-query process
)
326 (elmo-set-plugged t server service
327 (elmo-network-stream-type-symbol stream-type
)))
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
)
336 (when (eq (nth 1 (car alist
)) symbol
)
337 (setq spec
(car alist
))
339 (setq alist
(cdr alist
)))
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
))))
346 (elmo-net-folder-set-server-internal
348 (or (cdr (assq 'server params
))
349 (plist-get defaults
:server
)))
351 (elmo-net-folder-set-port-internal
353 (or (and port
(string-to-number port
))
354 (plist-get defaults
:port
)))
356 (elmo-net-folder-set-stream-type-internal
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
367 (car (elmo-parse-separated-tokens
368 (substring name
(match-beginning 0))
369 elmo-net-folder-name-syntax
))))
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)
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
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
))
449 ;; Should consider offline append and removal.
450 (luna-define-method elmo-folder-list-messages-unplugged
((folder
452 (if elmo-enable-disconnected-operation
453 (let ((deleting (elmo-dop-list-deleting-messages folder
)))
455 ;; delete deleting messages
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
)
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
)
477 (luna-define-method elmo-folder-delete-messages-internal
((folder
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
487 (elmo-folder-delete-messages-dop folder numbers
))
489 (luna-define-method elmo-folder-msgdb-create
((folder elmo-net-folder
)
491 (if (elmo-folder-plugged-p folder
)
492 (elmo-folder-send folder
'elmo-folder-msgdb-create-plugged
494 (elmo-folder-send folder
'elmo-folder-msgdb-create-unplugged
495 numbers flag-table
)))
497 (luna-define-method elmo-folder-msgdb-create-unplugged
((folder
501 ;; XXXX should be appended to already existing msgdb.
503 (elmo-folder-msgdb-create (elmo-dop-spool-folder folder
)
504 (mapcar 'abs numbers
)
507 (luna-define-method elmo-folder-set-flag
:before
((folder elmo-net-folder
)
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
)
520 (luna-define-method elmo-folder-unset-flag
:before
((folder elmo-net-folder
)
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
)
533 (luna-define-method elmo-folder-set-flag-unplugged
((folder elmo-net-folder
)
535 (elmo-folder-set-flag-dop folder numbers flag
))
537 (luna-define-method elmo-folder-unset-flag-unplugged
((folder elmo-net-folder
)
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
556 (luna-define-generic elmo-message-fetch-unplugged
(folder number strategy
563 (luna-define-method elmo-message-fetch-internal
((folder elmo-net-folder
)
565 &optional section unseen
)
566 (if (elmo-folder-plugged-p folder
)
567 (elmo-message-fetch-plugged folder number
569 (current-buffer) unseen
)
570 (elmo-message-fetch-unplugged folder number
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
578 (elmo-message-fetch-internal
579 (elmo-dop-spool-folder folder
) (abs number
) strategy
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
))
600 (luna-define-method elmo-quit
((folder elmo-net-folder
))
601 (elmo-network-clear-session-cache))
604 (product-provide (provide 'elmo-net
) (require 'elmo-version
))
606 ;;; elmo-net.el ends here