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