* riece-channel.el: Require 'riece-cache.
[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 (require 'riece-cache)
33
34 (eval-and-compile
35   (defvar riece-server-keyword-map
36     '((:host)
37       (:service 6667)
38       (:nickname riece-nickname)
39       (:username riece-username)
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-number 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-queue-dequeue riece-send-queue)
167               length (length string))
168         (if (> length riece-max-send-size)
169             (message "Long message (%d > %d)" length riece-max-send-size)
170           (setq riece-send-size (+ riece-send-size length))
171           (when (<= riece-send-size riece-max-send-size)
172             (process-send-string process string)
173             (setq riece-last-send-time (current-time)))))
174       (unless (riece-queue-empty riece-send-queue)
175         (riece-run-at-time riece-send-delay nil
176                            (lambda (process)
177                              (if (riece-server-process-opened process)
178                                  (riece-flush-send-queue process)))
179                            process)))))
180
181 (defun riece-process-send-string (process string)
182   (with-current-buffer (process-buffer process)
183     (riece-queue-enqueue riece-send-queue string))
184   (riece-flush-send-queue process))
185
186 (defun riece-current-server-name ()
187   (or riece-overriding-server-name
188                                         ;already in the server buffer
189       (if (local-variable-p 'riece-server-name (current-buffer))
190           riece-server-name
191         (if riece-current-channel
192             (riece-identity-server riece-current-channel)
193           (if (riece-server-opened "")
194               "")))))
195
196 (defun riece-send-string (string &optional identity)
197   (let* ((server-name (if identity
198                           (riece-identity-server identity)
199                         (riece-current-server-name)))
200          (process (riece-server-process server-name))
201          coding-system)
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-send-queue)
265     (setq riece-send-queue (riece-make-queue))
266     (make-local-variable 'riece-send-size)
267     (setq riece-send-size 0)
268     (make-local-variable 'riece-last-send-time)
269     (setq riece-last-send-time '(0 0 0))
270     (make-local-variable 'riece-user-obarray)
271     (setq riece-user-obarray (make-vector riece-user-obarray-size 0))
272     (make-local-variable 'riece-channel-obarray)
273     (setq riece-channel-obarray (make-vector riece-channel-obarray-size 0))
274     (make-local-variable 'riece-coding-system)
275     (make-local-variable 'riece-channel-cache)
276     (setq riece-channel-cache (riece-make-cache riece-channel-cache-max-size))
277     (make-local-variable 'riece-user-cache)
278     (setq riece-user-cache (riece-make-cache riece-user-cache-max-size))
279     (buffer-disable-undo)
280     (erase-buffer)))
281
282 (defun riece-close-server-process (process)
283   (with-current-buffer (process-buffer process)
284     (run-hooks 'riece-after-close-hook))
285   (kill-buffer (process-buffer process))
286   (setq riece-server-process-alist
287         (delq (rassq process riece-server-process-alist)
288               riece-server-process-alist)))
289
290 (defun riece-server-process-opened (process)
291   (not (null (memq (process-status process) '(open run)))))
292
293 (defun riece-server-opened (&optional server-name)
294   (if server-name
295       (let ((process (riece-server-process server-name)))
296         (and process
297              (riece-server-process-opened process)))
298     (let ((alist riece-server-process-alist))
299       (catch 'found
300         (while alist
301           (if (riece-server-process-opened (cdr (car alist)))
302               (throw 'found t))
303           (setq alist (cdr alist)))))))
304
305 (defun riece-server-properties (server-name)
306   "Return a list of properties associated with SERVER-NAME."
307   (if (equal server-name "")
308       riece-server
309     (let ((entry (assoc server-name riece-server-alist)))
310       (unless entry
311         (error "No such server"))
312       (cdr entry))))
313
314 (provide 'riece-server)
315
316 ;;; riece-server.el ends here