* riece-globals.el (riece-join-channel-candidate): New variable.
[riece] / lisp / riece-300.el
1 ;;; riece-300.el --- handlers for 300 replies
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-misc)
28 (require 'riece-commands)
29
30 (require 'riece-filter)                 ;riece-default-handle-message
31
32 (eval-when-compile
33   (autoload 'riece-default-handle-numeric-reply "riece-filter"))
34 (defun riece-handle-default-300-message (prefix number name string)
35   (riece-default-handle-numeric-reply
36    riece-info-prefix prefix number name string))
37
38 (defun riece-handle-302-message (prefix number name string)
39   "RPL_USERHOST \":*1<reply> *( \" \" <reply> )\""
40   (let ((replies (split-string (substring string 1) " ")))
41     (while replies
42       (if (string-match
43            (concat "^\\(" riece-user-regexp
44                    "\\)\\(\\*\\)?=\\([-+]\\)\\([^ ]+\\)")
45            (car replies))
46           (let ((user (match-string 1 (car replies)))
47                 (away (eq (match-string 3 (car replies)) ?-))
48                 (user-at-host (match-string 4 (car replies)))
49                 (operator (not (null (match-beginning 2)))))
50             (riece-user-toggle-away user away)
51             (riece-user-toggle-operator user operator)
52             (riece-insert-info
53              (list riece-dialogue-buffer riece-others-buffer)
54              (concat
55               (riece-concat-server-name
56                (format "%s is (%s) [%s, %s]"
57                        (riece-format-identity
58                         (riece-make-identity user riece-server-name)
59                         t)
60                        (riece-strip-user-at-host user-at-host)
61                        (if operator
62                            "operator"
63                          "not operator")
64                        (if away
65                            "away"
66                          "not away")))
67               "\n"))))
68       (setq replies (cdr replies)))
69   (riece-update-status-indicators)
70   (force-mode-line-update t)))
71
72 (defun riece-handle-303-message (prefix number name string)
73   (riece-insert-info
74    (list riece-dialogue-buffer riece-others-buffer)
75    (concat
76     (riece-concat-server-name
77      (concat "Online: "
78              (mapconcat
79               (lambda (user)
80                 (riece-format-identity
81                  (riece-make-identity user riece-server-name)
82                  t))
83               (split-string (substring string 1) " ")
84               "")))
85     "\n")))
86
87 (defun riece-handle-301-message (prefix number name string)
88   (if (string-match (concat "^\\(" riece-user-regexp "\\) :") string)
89       (let ((user (match-string 1 string))
90             (message (substring string (match-end 0))))
91         (riece-user-toggle-away user t)
92         (riece-insert-info
93          (list riece-dialogue-buffer riece-others-buffer)
94          (concat
95           (riece-concat-server-name
96            (format "%s is away: %s"
97                    (riece-format-identity
98                     (riece-make-identity user riece-server-name)
99                     t)
100                    message))
101           "\n"))))
102   (riece-update-status-indicators)
103   (force-mode-line-update t))
104
105 (defun riece-handle-305-message (prefix number name string)
106   (riece-user-toggle-away riece-real-nickname nil)
107   (riece-update-status-indicators)
108   (force-mode-line-update t))
109
110 (defun riece-handle-306-message (prefix number name string)
111   (riece-user-toggle-away riece-real-nickname t)
112   (riece-update-status-indicators)
113   (force-mode-line-update t))
114
115 (defun riece-handle-311-message (prefix number name string)
116   (if (string-match
117        (concat "^\\(" riece-user-regexp
118                "\\) \\([^ ]+\\) \\([^ ]+\\) \\* :")
119        string)
120       (let ((user (match-string 1 string))
121             (name (substring string (match-end 0)))
122             (user-at-host (concat (match-string 2 string) "@"
123                                   (match-string 3 string))))
124         (riece-insert-info
125          (list riece-dialogue-buffer riece-others-buffer)
126          (concat
127           (riece-concat-server-name
128            (format "%s is %s (%s)"
129                    (riece-format-identity
130                     (riece-make-identity user riece-server-name)
131                     t)
132                    name
133                    user-at-host))
134           "\n")))))
135
136 (defun riece-handle-312-message (prefix number name string)
137   (if (string-match
138        (concat "^\\(" riece-user-regexp "\\) \\([^ ]+\\) :")
139        string)
140       (riece-insert-info
141        (list riece-dialogue-buffer riece-others-buffer)
142        (concat
143         (riece-concat-server-name
144          (format "on via server %s: %s"
145                  riece-real-server-name
146                  (substring string (match-end 0))))
147         "\n"))))
148
149 (defun riece-handle-313-message (prefix number name string)
150   (if (string-match (concat "^" riece-user-regexp) string)
151       (let ((user (match-string 0 string)))
152         (riece-insert-info
153          (list riece-dialogue-buffer riece-others-buffer)
154          (concat
155           (riece-concat-server-name
156            (concat (riece-format-identity
157                     (riece-make-identity user riece-server-name)
158                     t)
159                    " is an IRC operator"))
160           "\n")))))
161
162 (defun riece-handle-317-message (prefix number name string)
163   (if (string-match
164        (concat "^\\(" riece-user-regexp "\\) \\([0-9]+\\) :")
165        string)
166       (let ((user (match-string 1 string))
167             (idle (match-string 2 string)))
168         (riece-insert-info
169          (list riece-dialogue-buffer riece-others-buffer)
170          (concat
171           (riece-concat-server-name
172            (format "%s is %s seconds idle"
173                    (riece-format-identity
174                     (riece-make-identity user riece-server-name)
175                     t)
176                    idle))
177           "\n")))))
178
179 (defun riece-handle-319-message (prefix number name string)
180   (if (string-match (concat "^\\(" riece-user-regexp "\\) :") string)
181       (let ((user (match-string 1 string))
182             (channels
183              (mapconcat
184               (lambda (channel)
185                 (if (string-match
186                      (concat "^\\([@+]?\\)\\(" riece-channel-regexp "\\)")
187                      channel)
188                     (concat
189                      (match-string 1 channel)
190                      (riece-format-identity
191                       (riece-make-identity (match-string 2 channel)
192                                            riece-server-name)
193                       t))))
194               (split-string (substring string (match-end 0)) " ")
195               " ")))
196         (riece-insert-info
197          (list riece-dialogue-buffer riece-others-buffer)
198          (concat
199           (riece-concat-server-name
200            (format "%s: %s"
201                    (riece-format-identity
202                     (riece-make-identity user riece-server-name)
203                     t)
204                    channels))
205           "\n")))))
206
207 (defun riece-handle-351-message (prefix number name string)
208   (if (string-match "\\([^ ]+\\.[^ ]+\\) \\([^ ]+\\) :" string)
209       (riece-insert-info
210        (list riece-dialogue-buffer riece-others-buffer)
211        (concat
212         (riece-concat-server-name
213          (format "%s is running on %s: %s"
214                  (match-string 1 string)
215                  (match-string 2 string)
216                  (substring string (match-end 0))))
217         "\n"))))
218
219 (defun riece-handle-353-message (prefix number name string)
220   "RPL_NAMREPLY \"<channel> :[[@|+]<nick> [[@|+]<nick> [...]]]\"."
221   (if (string-match "^[=\*@] *\\([^ ]+\\) +:" string)
222       (let ((channel (match-string 1 string))
223             users)
224         (setq string (substring string (match-end 0)))
225         (if (string-match " *$" string)
226             (setq string (substring string 0 (match-beginning 0))))
227         (setq users (split-string string))
228         (while users
229           (if (eq (aref (car users) 0) ?@)
230               (progn
231                 (riece-naming-assert-join (substring (car users) 1) channel)
232                 (riece-channel-toggle-operator
233                  channel (substring (car users) 1) t))
234             (if (eq (aref (car users) 0) ?+)
235                 (progn
236                   (riece-naming-assert-join (substring (car users) 1) channel)
237                   (riece-channel-toggle-speaker
238                    channel (substring (car users) 1) t))
239               (riece-naming-assert-join (car users) channel)))
240           (setq users (cdr users)))
241         (riece-redisplay-buffers))))
242
243 (defun riece-handle-322-message (prefix number name string)
244   (if (string-match "^\\([^ ]+\\) \\([0-9]+\\) :" string)
245       (let* ((channel (match-string 1 string))
246              (visible (match-string 2 string))
247              (topic (substring string (match-end 0))))
248         (riece-channel-set-topic (riece-get-channel channel) topic)
249         (let* ((channel-identity (riece-make-identity channel
250                                                       riece-server-name))
251                (buffer (riece-channel-buffer-name channel-identity)))
252           (riece-insert-info buffer (concat visible " users, topic: "
253                                             topic "\n"))
254           (riece-insert-info
255            (if (and riece-channel-buffer-mode
256                     (not (eq buffer riece-channel-buffer)))
257                (list riece-dialogue-buffer riece-others-buffer)
258              riece-dialogue-buffer)
259            (concat
260             (riece-concat-server-name
261              (format "%s: %s users, topic: %s"
262                      (riece-format-identity channel-identity t) visible topic))
263             "\n"))))))
264
265 (defun riece-handle-324-message (prefix number name string)
266   (if (string-match "^\\([^ ]+\\) \\([^ ]+\\) " string)
267       (let* ((channel (match-string 1 string))
268              (mode-string (substring string (match-beginning 2)))
269              (modes (string-to-list (match-string 2 string)))
270              (toggle (car modes)))
271         (setq modes (cdr modes))
272         (while modes
273           (riece-channel-toggle-mode channel (car modes) (eq toggle ?+))
274           (setq modes (cdr modes)))
275         (let* ((channel-identity (riece-make-identity channel
276                                                       riece-server-name))
277                (buffer (riece-channel-buffer-name channel-identity)))
278           (riece-insert-info buffer (concat "Mode: " mode-string "\n"))
279           (riece-insert-info
280            (if (and riece-channel-buffer-mode
281                     (not (eq buffer riece-channel-buffer)))
282                (list riece-dialogue-buffer riece-others-buffer)
283              riece-dialogue-buffer)
284            (concat
285             (riece-concat-server-name
286              (format "Mode for %s: %s"
287                      (riece-format-identity channel-identity t)
288                      mode-string))
289             "\n")))
290         (riece-update-channel-indicator)
291         (force-mode-line-update t))))
292
293 (defun riece-handle-set-topic (prefix number name string remove)
294   (if (string-match "^\\([^ ]+\\) :" string)
295       (let* ((channel (match-string 1 string))
296              (message (substring string (match-end 0)))
297              (channel-identity (riece-make-identity channel riece-server-name))
298              (buffer (riece-channel-buffer-name channel-identity)))
299         (if remove
300             (riece-channel-set-topic (riece-get-channel channel) nil)
301           (riece-channel-set-topic (riece-get-channel channel) message)
302         (riece-insert-info buffer (concat "Topic: " message "\n"))
303         (riece-insert-info
304          (if (and riece-channel-buffer-mode
305                   (not (eq buffer riece-channel-buffer)))
306              (list riece-dialogue-buffer riece-others-buffer)
307            riece-dialogue-buffer)
308          (concat
309           (riece-concat-server-name
310            (format "Topic for %s: %s"
311                    (riece-format-identity channel-identity t)
312                    message))
313           "\n"))
314         (riece-update-channel-indicator)))))
315
316 (defun riece-handle-331-message (prefix number name string)
317   (riece-handle-set-topic prefix number name string t))
318
319 (defun riece-handle-332-message (prefix number name string)
320   (riece-handle-set-topic prefix number name string nil))
321
322 (defun riece-handle-341-message (prefix number name string)
323   (if (string-match "^\\([^ ]+\\) " string)
324       (let* ((channel (match-string 1 string))
325              (user (substring string (match-end 0)))
326              (channel-identity (riece-make-identity channel riece-server-name))
327              (buffer (riece-channel-buffer-name channel-identity)))
328         (riece-insert-info buffer (concat "Inviting " user "\n"))
329         (riece-insert-info
330          (if (and riece-channel-buffer-mode
331                   (not (eq buffer riece-channel-buffer)))
332              (list riece-dialogue-buffer riece-others-buffer)
333            riece-dialogue-buffer)
334          (concat
335           (riece-concat-server-name
336            (format "Inviting %s to %s" user
337                    (riece-format-identity channel-identity t)))
338           "\n")))))
339
340 (defun riece-handle-352-message (prefix number name string)
341   (if (string-match "^\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([HG]\\)\\(\\*\\)?\\([@+]\\)? :\\([0-9]+\\) " string)
342       (let* ((channel (match-string 1 string))
343              (user (match-string 2 string))
344              (host (match-string 3 string))
345              (server (match-string 4 string))
346              (nick (match-string 5 string))
347              (away (equal (match-string 6 string) "G"))
348              (operator (not (null (match-beginning 7))))
349              (flag (match-string 8 string))
350              (hops (match-string 9 string))
351              (name (substring string (match-end 0)))
352              (buffer (riece-channel-buffer-name
353                       (riece-make-identity channel riece-server-name)))
354              (info (format "%10s = %s (%s) [%s, %s, %s hops, on %s]"
355                            (concat
356                             (if (memq flag '(?@ ?+))
357                                 (char-to-string flag)
358                               " ")
359                             (riece-format-identity
360                              (riece-make-identity nick riece-server-name)
361                              t))
362                            name
363                            (riece-strip-user-at-host
364                             (concat user "@" host))
365                            (if operator
366                                "operator"
367                              "not operator")
368                            (if away
369                                "away"
370                              "not away")
371                            hops
372                            server)))
373         (riece-naming-assert-join nick channel)
374         (riece-user-toggle-away user away)
375         (riece-user-toggle-operator user operator)
376         (riece-insert-info buffer (concat info "\n"))
377         (riece-insert-info
378          (if (and riece-channel-buffer-mode
379                   (not (eq buffer riece-channel-buffer)))
380              (list riece-dialogue-buffer riece-others-buffer)
381            riece-dialogue-buffer)
382          (concat
383           (riece-concat-server-name
384            (concat
385             (riece-format-identity
386              (riece-make-identity channel riece-server-name)
387              t)
388             " "
389             info))
390           "\n"))
391         (riece-redisplay-buffers))))
392
393 (defun riece-handle-315-message (prefix number name string))
394 (defun riece-handle-318-message (prefix number name string))
395 (defun riece-handle-323-message (prefix number name string))
396 (defun riece-handle-366-message (prefix number name string))
397
398 (provide 'riece-300)
399
400 ;;; riece-300.el ends here