* riece-doctor.el (riece-doctor-buffer-name): Assume that the 1st
[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       (:password)
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."))
44
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
48 of the `let' form.
49
50 The variables bound and their default values are described by
51 the `riece-server-keyword-map' variable."
52   `(let ,(mapcar
53           (lambda (keyword)
54             (list (intern (substring (symbol-name (car keyword)) 1))
55                   (if (cadr keyword)
56                       `(or (plist-get ,plist ',(car keyword))
57                            ,(cadr keyword))
58                     `(plist-get ,plist ',(car keyword)))))
59           riece-server-keyword-map)
60      ,@body))
61
62 (put 'riece-server-keyword-bind 'lisp-indent-function 1)
63 (put 'riece-server-keyword-bind 'edebug-form-spec '(form body))
64
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)))
72           plist)
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))))
79
80 (defun riece-server-name-to-server (server-name)
81   (let ((entry (assoc server-name riece-server-alist)))
82     (if entry
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))
88     (cdr entry)))
89
90 (defun riece-server-process-name (server-name)
91   (if (equal server-name "")
92       "IRC"
93     (format "IRC<%s>" server-name)))
94
95 (defun riece-server-process (server-name)
96   (cdr (assoc server-name riece-server-process-alist)))
97
98 (defmacro riece-with-server-buffer (server-name &rest body)
99   `(let ((process (riece-server-process ,server-name)))
100      (if process
101          (with-current-buffer (process-buffer process)
102            ,@body)
103        (error "Server closed"))))
104
105 (put 'riece-with-server-buffer 'lisp-indent-function 1)
106 (put 'riece-with-server-buffer 'edebug-form-spec '(form body))
107
108 (defun riece-process-send-string (process string)
109   (with-current-buffer (process-buffer process)
110     (process-send-string process (riece-encode-coding-string string))))
111
112 (defun riece-current-server-name ()
113   (or riece-overriding-server-name
114                                         ;already in the server buffer
115       (if (local-variable-p 'riece-server-name (current-buffer))
116           riece-server-name
117         (if riece-current-channel
118             (riece-identity-server riece-current-channel)
119           (if (riece-server-opened "")
120               "")))))
121
122 (defun riece-send-string (string)
123   (let* ((server-name (riece-current-server-name))
124          (process (riece-server-process server-name)))
125     (unless process
126       (error "%s" (substitute-command-keys
127                    "Type \\[riece-command-open-server] to open server.")))
128     (riece-process-send-string process string)))
129
130 (defun riece-open-server (server server-name)
131   (riece-server-keyword-bind server
132     (let (selective-display
133           (coding-system-for-read 'binary)
134           (coding-system-for-write 'binary)
135           process)
136       (if (equal server-name "")
137           (message "Connecting to IRC server...")
138         (message "Connecting to %s..." server-name))
139       (setq process
140             (funcall function (riece-server-process-name server-name)
141                      (concat " *IRC*" server-name)
142                      host service))
143       (if (equal server-name "")
144           (message "Connecting to IRC server...done")
145         (message "Connecting to %s...done" server-name))
146       (riece-reset-process-buffer process)
147       (with-current-buffer (process-buffer process)
148         (setq riece-server-name server-name))
149       (set-process-sentinel process 'riece-sentinel)
150       (set-process-filter process 'riece-filter)
151       (if (equal server-name "")
152           (message "Logging in to IRC server...")
153         (message "Logging in to %s..." server-name))
154       (if riece-reconnect-with-password ;password incorrect or not set.
155           (unwind-protect
156               (setq password
157                     (condition-case nil
158                         (let (inhibit-quit)
159                           (if (equal server-name "")
160                               (riece-read-passwd "Password: ")
161                             (riece-read-passwd (format "Password for %s: "
162                                                        server-name))))
163                       (quit
164                        (if (equal server-name "")
165                            (message "Password: Quit")
166                          (message (format "Password for %s: Quit"
167                                           server-name)))
168                        'quit)))
169             (setq riece-reconnect-with-password nil)))
170       (if (eq password 'quit)
171           (progn
172             (riece-close-server-process process)
173             ;; If no server process is available, exit.
174             (unless riece-server-process-alist
175               (riece-exit)))
176         (if password
177             (riece-process-send-string process
178                                        (format "PASS %s\r\n" password)))
179         (riece-process-send-string process
180                                    (format "USER %s * * :%s\r\n"
181                                            (user-real-login-name)
182                                            (or username
183                                                "No information given")))
184         (riece-process-send-string process (format "NICK %s\r\n" nickname))
185         (with-current-buffer (process-buffer process)
186           (setq riece-last-nickname riece-real-nickname
187                 riece-nick-accepted 'sent
188                 riece-coding-system coding))
189         (setq riece-server-process-alist
190               (cons (cons server-name process)
191                     riece-server-process-alist))))))
192
193 (defun riece-reset-process-buffer (process)
194   (save-excursion
195     (set-buffer (process-buffer process))
196     (if (fboundp 'set-buffer-multibyte)
197         (set-buffer-multibyte nil))
198     (kill-all-local-variables)
199     (make-local-variable 'riece-real-nickname)
200     (make-local-variable 'riece-last-nickname)
201     (make-local-variable 'riece-nick-accepted)
202     (make-local-variable 'riece-real-server-name)
203     (make-local-variable 'riece-real-userhost)
204     (make-local-variable 'riece-user-at-host)
205     (make-local-variable 'riece-user-at-host-type)
206     (make-local-variable 'riece-supported-user-modes)
207     (make-local-variable 'riece-supported-channel-modes)
208     (make-local-variable 'riece-channel-filter)
209     (make-local-variable 'riece-server-name)
210     (make-local-variable 'riece-read-point)
211     (setq riece-read-point (point-min))
212     (make-local-variable 'riece-obarray)
213     (setq riece-obarray (make-vector riece-obarray-size 0))
214     (make-local-variable 'riece-coding-system)
215     (buffer-disable-undo)
216     (erase-buffer)))
217
218 (defun riece-close-server-process (process)
219   (if riece-debug
220       (delete-process process)
221     (kill-buffer (process-buffer process)))
222   (setq riece-server-process-alist
223         (delq (rassq process riece-server-process-alist)
224               riece-server-process-alist)))
225
226 (defun riece-server-process-opened (process)
227   (not (null (memq (process-status process) '(open run)))))
228
229 (defun riece-server-opened (&optional server-name)
230   (if server-name
231       (let ((process (riece-server-process server-name)))
232         (and process
233              (riece-server-process-opened process)))
234     (let ((alist riece-server-process-alist))
235       (catch 'found
236         (while alist
237           (if (riece-server-process-opened (cdr (car alist)))
238               (throw 'found t))
239           (setq alist (cdr alist)))))))
240
241 (eval-when-compile
242   (autoload 'riece-exit "riece"))
243 (defun riece-quit-server-process (process &optional message)
244   (if riece-quit-timeout
245       (riece-run-at-time riece-quit-timeout nil
246                          (lambda (process)
247                            (when (rassq process riece-server-process-alist)
248                              (riece-close-server-process process)
249                              ;; If no server process is available, exit.
250                              (unless riece-server-process-alist
251                                (riece-exit))))
252                          process))
253   (riece-process-send-string process
254                              (if message
255                                  (format "QUIT :%s\r\n" message)
256                                "QUIT\r\n")))
257
258 (provide 'riece-server)
259
260 ;;; riece-server.el ends here