1 ;;; elmo-net.el -- Network module for ELMO.
3 ;; Copyright 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.
34 (luna-define-class elmo-network-session () (name
42 (luna-define-internal-accessors 'elmo-network-session))
44 (luna-define-generic elmo-network-initialize-session (session)
45 "Initialize SESSION (Called before authentication).")
47 (luna-define-generic elmo-network-initialize-session-buffer (session buffer)
48 "Initialize SESSION's BUFFER.")
50 (luna-define-generic elmo-network-authenticate-session (session)
51 "Authenticate SESSION.")
53 (luna-define-generic elmo-network-setup-session (session)
54 "Setup SESSION. (Called after authentication).")
56 (luna-define-generic elmo-network-close-session (session)
60 elmo-network-initialize-session-buffer ((session
61 elmo-network-session) buffer)
62 (with-current-buffer buffer
63 (elmo-set-buffer-multibyte nil)
64 (buffer-disable-undo (current-buffer))))
66 (luna-define-method elmo-network-close-session ((session elmo-network-session))
67 (when (elmo-network-session-process-internal session)
68 ; (memq (process-status (elmo-network-session-process-internal session))
70 (kill-buffer (process-buffer
71 (elmo-network-session-process-internal session)))
72 (delete-process (elmo-network-session-process-internal session))))
74 (defmacro elmo-network-stream-type-spec-string (stream-type)
75 (` (nth 0 (, stream-type))))
77 (defmacro elmo-network-stream-type-symbol (stream-type)
78 (` (nth 1 (, stream-type))))
80 (defmacro elmo-network-stream-type-feature (stream-type)
81 (` (nth 2 (, stream-type))))
83 (defmacro elmo-network-stream-type-function (stream-type)
84 (` (nth 3 (, stream-type))))
86 (defsubst elmo-network-session-password-key (session)
87 (format "%s:%s/%s@%s:%d"
88 (elmo-network-session-name-internal session)
89 (elmo-network-session-user-internal session)
90 (symbol-name (or (elmo-network-session-auth-internal session)
92 (elmo-network-session-host-internal session)
93 (elmo-network-session-port-internal session)))
95 (defvar elmo-network-session-cache nil)
97 (defsubst elmo-network-session-cache-key (name host port user auth stream-type)
98 "Returns session cache key."
99 (format "%s:%s/%s@%s:%d%s"
100 name user auth host port (or stream-type "")))
102 (defun elmo-network-clear-session-cache ()
103 "Clear session cache."
105 (mapcar (lambda (pair)
106 (elmo-network-close-session (cdr pair)))
107 elmo-network-session-cache)
108 (setq elmo-network-session-cache nil))
110 (defmacro elmo-network-session-buffer (session)
111 "Get buffer for SESSION."
112 (` (process-buffer (elmo-network-session-process-internal
115 (defun elmo-network-get-session (class name host port user auth stream-type
117 "Get network session from session cache or a new network session.
118 CLASS is the class name of the session.
119 NAME is the name of the process.
120 HOST is the name of the server host.
121 PORT is the port number of the service.
122 USER is the user-id for the authenticate.
123 AUTH is the authenticate method name (symbol).
124 STREAM-TYPE is the stream type (See also `elmo-network-stream-type-alist').
125 Returns a `elmo-network-session' instance.
126 If optional argument IF-EXISTS is non-nil, it does not return session
127 if there is no session cache.
128 if making session failed, returns nil."
129 (let (pair session key)
130 (if (not (elmo-plugged-p host port))
132 (setq pair (assoc (setq key (elmo-network-session-cache-key
133 name host port user auth stream-type))
134 elmo-network-session-cache))
136 (memq (process-status
137 (elmo-network-session-process-internal
140 (setq elmo-network-session-cache
141 (delq pair elmo-network-session-cache))
142 (elmo-network-close-session (cdr pair))
145 (cdr pair) ; connection cache exists.
148 (elmo-network-open-session class name
149 host port user auth stream-type))
150 (setq elmo-network-session-cache
151 (cons (cons key session)
152 elmo-network-session-cache))
155 (defun elmo-network-open-session (class name host port user auth
157 "Open an authenticated network session.
158 CLASS is the class name of the session.
159 NAME is the name of the process.
160 HOST is the name of the server host.
161 PORT is the port number of the service.
162 USER is the user-id for the authenticate.
163 AUTH is the authenticate method name (symbol).
164 STREAM-TYPE is the stream type (See also `elmo-network-stream-type-alist').
165 Returns a process object. if making session failed, returns nil."
167 (luna-make-entity class
173 :stream-type stream-type
176 (buffer (format " *%s session for %s@%s:%d%s"
181 (or (elmo-network-stream-type-spec-string stream-type)
184 (condition-case error
186 (if (get-buffer buffer) (kill-buffer buffer))
187 (setq buffer (get-buffer-create buffer))
188 (elmo-network-initialize-session-buffer session buffer)
189 (elmo-network-session-set-process-internal
191 (setq process (elmo-open-network-stream
192 (elmo-network-session-name-internal session)
193 buffer host port stream-type)))
195 (elmo-network-initialize-session session)
196 (elmo-network-authenticate-session session)
197 (elmo-network-setup-session session)))
199 (when (eq (car error) 'elmo-authenticate-error)
200 (elmo-remove-passwd (elmo-network-session-password-key session)))
201 (elmo-network-close-session session)
202 (signal (car error)(cdr error))))
205 (defun elmo-open-network-stream (name buffer host service stream-type)
206 (let ((auto-plugged (and elmo-auto-change-plugged
207 (> elmo-auto-change-plugged 0)))
210 (elmo-network-stream-type-feature stream-type))
211 (require (elmo-network-stream-type-feature stream-type)))
213 (let (process-connection-type)
217 (funcall (elmo-network-stream-type-function stream-type)
218 name buffer host service)
219 (open-network-stream name buffer host service)))))
222 (elmo-set-plugged nil host service (current-time))
223 (message "Auto plugged off at %s:%d" host service)
225 (signal (car err) (cdr err))))
227 (process-kill-without-query process)
229 (elmo-set-plugged t host service))
234 ;;; elmo-net.el ends here