1 ;;; riece-server.el --- functions to open and close servers
2 ;; Copyright (C) 1998-2003 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: IRC, riece
8 ;; This file is part of Riece.
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.
27 (require 'riece-options)
28 (require 'riece-globals) ;for server local variables.
29 (require 'riece-coding) ;riece-default-coding-system
30 (require 'riece-identity)
31 (require 'riece-compat)
34 (defvar riece-server-keyword-map
37 (:nickname riece-nickname)
38 (:username riece-username)
40 (:function riece-default-open-connection-function)
41 (:coding riece-default-coding-system))
42 "Mapping from keywords to default values.
43 All keywords that can be used must be listed here."))
45 (defmacro riece-server-keyword-bind (plist &rest body)
46 "Return a `let' form that binds all variables in PLIST.
47 After this is done, BODY will be executed in the scope
50 The variables bound and their default values are described by
51 the `riece-server-keyword-map' variable."
54 (list (intern (substring (symbol-name (car keyword)) 1))
56 `(or (plist-get ,plist ',(car keyword))
58 `(plist-get ,plist ',(car keyword)))))
59 riece-server-keyword-map)
62 (put 'riece-server-keyword-bind 'lisp-indent-function 1)
63 (put 'riece-server-keyword-bind 'edebug-form-spec '(form body))
65 (defun riece-server-parse-string (string)
66 "Convert a STRING set as `riece-server' and return a property list."
67 (when (or (string-match "^\\[\\([^]]+\\)\\]:?\\([0-9]*\\)" string)
68 (string-match "^\\([^:]+\\):?\\([0-9]*\\)" string))
69 (let ((host (match-string 1 string))
70 (service (match-string 2 string))
71 (password (substring string (match-end 0)))
73 (setq plist (cons `(:host ,host) plist))
74 (unless (equal service "")
75 (setq plist (cons `(:service ,(string-to-int service)) plist)))
76 (unless (equal password "")
77 (setq plist (cons `(:password ,(substring password 1)) plist)))
78 (apply #'nconc plist))))
80 (defun riece-server-name-to-server (server-name)
81 (let ((entry (assoc server-name riece-server-alist)))
83 (unless (listp (cdr entry))
84 (setcdr entry (riece-server-parse-string (cdr entry))))
85 (setq entry (cons server-name (riece-server-parse-string server-name))
86 riece-server-alist (cons entry riece-server-alist)
87 riece-save-variables-are-dirty t))
90 (defun riece-server-process-name (server-name)
91 (if (equal server-name "")
93 (format "IRC<%s>" server-name)))
95 (defun riece-server-process (server-name)
96 (cdr (assoc server-name riece-server-process-alist)))
98 (defmacro riece-with-server-buffer (server-name &rest body)
99 `(let ((process (riece-server-process ,server-name)))
101 (with-current-buffer (process-buffer process)
103 (error "Server closed"))))
105 (put 'riece-with-server-buffer 'lisp-indent-function 1)
106 (put 'riece-with-server-buffer 'edebug-form-spec '(form body))
108 (defun riece-flush-send-queue (process reset)
109 (with-current-buffer (process-buffer process)
113 (setq riece-send-size 0))
114 (while (and riece-send-queue
115 (<= riece-send-size riece-max-send-size))
116 (setq string (riece-encode-coding-string (car riece-send-queue))
117 length (length string))
118 (if (> length riece-max-send-size)
119 (message "Long message (%d > %d)" length riece-max-send-size)
120 (setq riece-send-size (+ riece-send-size length))
121 (if (<= riece-send-size riece-max-send-size)
122 (process-send-string process string)))
123 (setq riece-send-queue (cdr riece-send-queue)))
125 (riece-run-at-time riece-send-delay nil
126 #'riece-flush-send-queue process t)))))
128 (defun riece-process-send-string (process string)
129 (with-current-buffer (process-buffer process)
130 (setq riece-send-queue (nconc riece-send-queue (list string))))
131 (riece-flush-send-queue process nil))
133 (defun riece-current-server-name ()
134 (or riece-overriding-server-name
135 ;already in the server buffer
136 (if (local-variable-p 'riece-server-name (current-buffer))
138 (if riece-current-channel
139 (riece-identity-server riece-current-channel)
140 (if (riece-server-opened "")
143 (defun riece-send-string (string)
144 (let* ((server-name (riece-current-server-name))
145 (process (riece-server-process server-name)))
147 (error "%s" (substitute-command-keys
148 "Type \\[riece-command-open-server] to open server.")))
149 (riece-process-send-string process string)))
151 (defun riece-open-server (server server-name)
152 (let ((protocol (or (plist-get server :protocol)
157 (require (intern (concat "riece-" (symbol-name protocol))))
159 (setq function (intern-soft (concat "riece-"
160 (symbol-name protocol)
163 (error "\"%S\" is not supported" protocol))
165 (setq process (funcall function server server-name))
168 (with-current-buffer (process-buffer process)
169 (make-local-variable 'riece-protocol)
170 (setq riece-protocol protocol))
171 (setq riece-server-process-alist
172 (cons (cons server-name process)
173 riece-server-process-alist)))))
175 (defun riece-quit-server-process (process &optional message)
176 (let ((function (intern-soft
178 (with-current-buffer (process-buffer process)
179 (symbol-name riece-protocol))
180 "-quit-server-process"))))
182 (funcall function process message))))
184 (defun riece-reset-process-buffer (process)
186 (set-buffer (process-buffer process))
187 (if (fboundp 'set-buffer-multibyte)
188 (set-buffer-multibyte nil))
189 (kill-all-local-variables)
190 (make-local-variable 'riece-real-nickname)
191 (make-local-variable 'riece-last-nickname)
192 (make-local-variable 'riece-nick-accepted)
193 (make-local-variable 'riece-real-server-name)
194 (make-local-variable 'riece-real-userhost)
195 (make-local-variable 'riece-user-at-host)
196 (make-local-variable 'riece-user-at-host-type)
197 (make-local-variable 'riece-supported-user-modes)
198 (make-local-variable 'riece-supported-channel-modes)
199 (make-local-variable 'riece-channel-filter)
200 (make-local-variable 'riece-server-name)
201 (make-local-variable 'riece-read-point)
202 (setq riece-read-point (point-min))
203 (make-local-variable 'riece-send-queue)
204 (make-local-variable 'riece-send-size)
205 (setq riece-send-size 0)
206 (make-local-variable 'riece-obarray)
207 (setq riece-obarray (make-vector riece-obarray-size 0))
208 (make-local-variable 'riece-coding-system)
209 (buffer-disable-undo)
212 (defun riece-close-server-process (process)
213 (kill-buffer (process-buffer process))
214 (setq riece-server-process-alist
215 (delq (rassq process riece-server-process-alist)
216 riece-server-process-alist)))
218 (defun riece-server-process-opened (process)
219 (not (null (memq (process-status process) '(open run)))))
221 (defun riece-server-opened (&optional server-name)
223 (let ((process (riece-server-process server-name)))
225 (riece-server-process-opened process)))
226 (let ((alist riece-server-process-alist))
229 (if (riece-server-process-opened (cdr (car alist)))
231 (setq alist (cdr alist)))))))
233 (provide 'riece-server)
235 ;;; riece-server.el ends here