5a0db116263838f5e4b6e168c2b844706869f157
[elisp/riece.git] / lisp / riece-server.el
1 ;;; riece-server.el --- functions to open and close servers
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
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.
14
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.
19
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Code:
26
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)
32 (require 'riece-cache)
33
34 (eval-and-compile
35   (defvar riece-server-keyword-map
36     '((:host)
37       (:service 6667)
38       (:nickname riece-nickname)
39       (:realname riece-realname)
40       (:username riece-username)
41       (:password)
42       (:function riece-default-open-connection-function)
43       (:coding riece-default-coding-system))
44     "Mapping from keywords to default values.
45 All keywords that can be used must be listed here."))
46
47 (defmacro riece-server-keyword-bind (plist &rest body)
48   "Return a `let' form that binds all variables in PLIST.
49 After this is done, BODY will be executed in the scope
50 of the `let' form.
51
52 The variables bound and their default values are described by
53 the `riece-server-keyword-map' variable."
54   `(let ,(mapcar
55           (lambda (keyword)
56             (list (intern (substring (symbol-name (car keyword)) 1))
57                   (if (cadr keyword)
58                       `(or (plist-get ,plist ',(car keyword))
59                            ,(cadr keyword))
60                     `(plist-get ,plist ',(car keyword)))))
61           riece-server-keyword-map)
62      ,@body))
63
64 (put 'riece-server-keyword-bind 'lisp-indent-function 1)
65 (put 'riece-server-keyword-bind 'edebug-form-spec '(form body))
66
67 (defun riece-server-parse-string (string)
68   "Convert a STRING set as `riece-server' and return a property list."
69   (when (or (string-match "^\\[\\([^]]+\\)\\]:?\\([0-9]*\\)" string)
70             (string-match "^\\([^:]+\\):?\\([0-9]*\\)" string))
71     (let ((host (match-string 1 string))
72           (service (match-string 2 string))
73           (password (substring string (match-end 0)))
74           plist)
75       (setq plist (cons `(:host ,host) plist))
76       (unless (equal service "")
77         (setq plist (cons `(:service ,(string-to-number service)) plist)))
78       (unless (equal password "")
79         (setq plist (cons `(:password ,(substring password 1)) plist)))
80       (apply #'nconc plist))))
81
82 (defun riece-server-name-to-server (server-name)
83   (let ((entry (assoc server-name riece-server-alist)))
84     (if entry
85         (unless (listp (cdr entry))
86           (setcdr entry (riece-server-parse-string (cdr entry))))
87       (setq entry (cons server-name (riece-server-parse-string server-name))
88             riece-server-alist (cons entry riece-server-alist)
89             riece-save-variables-are-dirty t))
90     (cdr entry)))
91
92 (defun riece-server-process-name (server-name)
93   (if (equal server-name "")
94       "IRC"
95     (format "IRC<%s>" server-name)))
96
97 (defun riece-server-process (server-name)
98   (cdr (assoc server-name riece-server-process-alist)))
99
100 (defmacro riece-with-server-buffer (server-name &rest body)
101   `(let ((process (riece-server-process ,server-name)))
102      (if process
103          (with-current-buffer (process-buffer process)
104            ,@body)
105        (error "Server closed"))))
106
107 (put 'riece-with-server-buffer 'lisp-indent-function 1)
108 (put 'riece-with-server-buffer 'edebug-form-spec '(form body))
109
110 (defun riece-make-queue ()
111   "Make a queue object."
112   (vector nil nil))
113
114 (defun riece-queue-enqueue (queue object)
115   "Add OBJECT to the end of QUEUE."
116   (if (aref queue 1)
117       (let ((last (list object)))
118         (nconc (aref queue 1) last)
119         (aset queue 1 last))
120     (aset queue 0 (list object))
121     (aset queue 1 (aref queue 0))))
122
123 (defun riece-queue-dequeue (queue)
124   "Remove an object from the beginning of QUEUE."
125   (unless (aref queue 0)
126     (error "Empty queue"))
127   (prog1 (car (aref queue 0))
128     (unless (aset queue 0 (cdr (aref queue 0)))
129       (aset queue 1 nil))))
130
131 (defun riece-queue-empty (queue)
132   "Return t if QUEUE is empty."
133   (null (aref queue 0)))
134
135 ;; stolen (and renamed) from time-date.el.
136 (defun riece-seconds-to-time (seconds)
137   "Convert SECONDS (a floating point number) to a time value."
138   (list (floor seconds 65536)
139         (floor (mod seconds 65536))
140         (floor (* (- seconds (ffloor seconds)) 1000000))))
141
142 ;; stolen (and renamed) from time-date.el.
143 (defun riece-time-less-p (t1 t2)
144   "Say whether time value T1 is less than time value T2."
145   (or (< (car t1) (car t2))
146       (and (= (car t1) (car t2))
147            (< (nth 1 t1) (nth 1 t2)))))
148
149 ;; stolen (and renamed) from time-date.el.
150 (defun riece-time-since (time)
151   "Return the time elapsed since TIME."
152   (let* ((current (current-time))
153          (rest (when (< (nth 1 current) (nth 1 time))
154                  (expt 2 16))))
155     (list (- (+ (car current) (if rest -1 0)) (car time))
156           (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
157
158 (defun riece-flush-send-queue (process)
159   (with-current-buffer (process-buffer process)
160     (let ((length 0)
161           string)
162       (if (riece-time-less-p (riece-seconds-to-time riece-send-delay)
163                              (riece-time-since riece-last-send-time))
164           (setq riece-send-size 0))
165       (while (and (not (riece-queue-empty riece-send-queue))
166                   (<= riece-send-size riece-max-send-size))
167         (setq string (riece-queue-dequeue riece-send-queue)
168               length (length string))
169         (if (> length riece-max-send-size)
170             (message "Long message (%d > %d)" length riece-max-send-size)
171           (setq riece-send-size (+ riece-send-size length))
172           (when (<= riece-send-size riece-max-send-size)
173             (process-send-string process string)
174             (setq riece-last-send-time (current-time)))))
175       (unless (riece-queue-empty riece-send-queue)
176         (riece-run-at-time riece-send-delay nil
177                            (lambda (process)
178                              (if (riece-server-process-opened process)
179                                  (riece-flush-send-queue process)))
180                            process)))))
181
182 (defun riece-process-send-string (process string)
183   (with-current-buffer (process-buffer process)
184     (riece-queue-enqueue riece-send-queue string))
185   (riece-flush-send-queue process))
186
187 (defun riece-current-server-name ()
188   (or riece-overriding-server-name
189                                         ;already in the server buffer
190       (if (local-variable-p 'riece-server-name (current-buffer))
191           riece-server-name
192         (if riece-current-channel
193             (riece-identity-server riece-current-channel)
194           (if (riece-server-opened "")
195               "")))))
196
197 (defun riece-send-string (string &optional identity)
198   (let* ((server-name (if identity
199                           (riece-identity-server identity)
200                         (riece-current-server-name)))
201          (process (riece-server-process server-name)))
202     (unless process
203       (error "%s" (substitute-command-keys
204                    "Type \\[riece-command-open-server] to open server.")))
205     (riece-process-send-string
206      process
207      (with-current-buffer (process-buffer process)
208        (if identity
209            (riece-encode-coding-string-for-identity string identity)
210          (riece-encode-coding-string string))))))
211
212 (defun riece-open-server (server server-name)
213   (let ((protocol (or (plist-get server :protocol)
214                       riece-protocol))
215         function
216         process)
217     (condition-case nil
218         (require (intern (concat "riece-" (symbol-name protocol))))
219       (error))
220     (setq function (intern-soft (concat "riece-"
221                                         (symbol-name protocol)
222                                         "-open-server")))
223     (unless function
224       (error "\"%S\" is not supported" protocol))
225     (condition-case nil
226         (setq process (funcall function server server-name))
227       (error))
228     (when process
229       (with-current-buffer (process-buffer process)
230         (make-local-variable 'riece-protocol)
231         (setq riece-protocol protocol))
232       (setq riece-server-process-alist
233             (cons (cons server-name process)
234                   riece-server-process-alist)))))
235
236 (defun riece-quit-server-process (process &optional message)
237   (let ((function (intern-soft
238                    (concat "riece-"
239                            (with-current-buffer (process-buffer process)
240                              (symbol-name riece-protocol))
241                            "-quit-server-process"))))
242     (if function
243         (funcall function process message))))
244
245 (defun riece-reset-process-buffer (process)
246   (save-excursion
247     (set-buffer (process-buffer process))
248     (if (fboundp 'set-buffer-multibyte)
249         (set-buffer-multibyte nil))
250     (kill-all-local-variables)
251     (make-local-variable 'riece-real-nickname)
252     (make-local-variable 'riece-last-nickname)
253     (make-local-variable 'riece-nick-accepted)
254     (make-local-variable 'riece-real-server-name)
255     (make-local-variable 'riece-real-userhost)
256     (make-local-variable 'riece-user-at-host)
257     (make-local-variable 'riece-user-at-host-type)
258     (make-local-variable 'riece-supported-user-modes)
259     (make-local-variable 'riece-supported-channel-modes)
260     (make-local-variable 'riece-channel-filter)
261     (make-local-variable 'riece-server-name)
262     (make-local-variable 'riece-read-point)
263     (setq riece-read-point (point-min))
264     (make-local-variable 'riece-filter-running)
265     (make-local-variable 'riece-send-queue)
266     (setq riece-send-queue (riece-make-queue))
267     (make-local-variable 'riece-send-size)
268     (setq riece-send-size 0)
269     (make-local-variable 'riece-last-send-time)
270     (setq riece-last-send-time '(0 0 0))
271     (make-local-variable 'riece-user-obarray)
272     (setq riece-user-obarray (make-vector riece-user-obarray-size 0))
273     (make-local-variable 'riece-channel-obarray)
274     (setq riece-channel-obarray (make-vector riece-channel-obarray-size 0))
275     (make-local-variable 'riece-coding-system)
276     (make-local-variable 'riece-channel-cache)
277     (setq riece-channel-cache (riece-make-cache riece-channel-cache-max-size))
278     (make-local-variable 'riece-user-cache)
279     (setq riece-user-cache (riece-make-cache riece-user-cache-max-size))
280     (buffer-disable-undo)
281     (erase-buffer)))
282
283 (defun riece-close-server-process (process)
284   (with-current-buffer (process-buffer process)
285     (run-hooks 'riece-after-close-hook))
286   (kill-buffer (process-buffer process))
287   (setq riece-server-process-alist
288         (delq (rassq process riece-server-process-alist)
289               riece-server-process-alist)))
290
291 (defun riece-server-process-opened (process)
292   (not (null (memq (process-status process) '(open run)))))
293
294 (defun riece-server-opened (&optional server-name)
295   (if server-name
296       (let ((process (riece-server-process server-name)))
297         (and process
298              (riece-server-process-opened process)))
299     (let ((alist riece-server-process-alist))
300       (catch 'found
301         (while alist
302           (if (riece-server-process-opened (cdr (car alist)))
303               (throw 'found t))
304           (setq alist (cdr alist)))))))
305
306 (defun riece-server-properties (server-name)
307   "Return a list of properties associated with SERVER-NAME."
308   (if (equal server-name "")
309       riece-server
310     (let ((entry (assoc server-name riece-server-alist)))
311       (unless entry
312         (error "No such server"))
313       (cdr entry))))
314
315 (provide 'riece-server)
316
317 ;;; riece-server.el ends here