3fd9accc5a839461006cb9c736ff85f74c7ea217
[elisp/riece.git] / 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
29 (require 'riece-filter)                 ;riece-default-handle-message
30
31 (eval-when-compile
32   (autoload 'riece-default-handle-numeric-reply "riece-filter"))
33 (defun riece-handle-default-300-message (prefix number name string)
34   (riece-default-handle-numeric-reply
35    riece-info-prefix prefix number name string))
36
37 (defun riece-handle-302-message (prefix number name string)
38   "RPL_USERHOST \":*1<reply> *( \" \" <reply> )\""
39   (let ((replies (split-string (substring string 1) " ")))
40     (while replies
41       (if (string-match
42            (concat "^\\(" riece-user-regexp
43                    "\\)\\(\\*\\)?=\\([-+]\\)\\([^ ]+\\)")
44            (car replies))
45           (let ((user (match-string 1 (car replies)))
46                 (operator (not (null (match-beginning 2))))
47                 (away (eq (match-string 3 (car replies)) ?-))
48                 (user-at-host (match-string 4 (car replies)))
49                 status)
50             (if away
51                 (setq status (cons "away" status)))
52             (if operator
53                 (setq status (cons "operator" status)))
54             (riece-user-toggle-away user away)
55             (riece-user-toggle-operator user operator)
56             (riece-insert-info
57              (list riece-dialogue-buffer riece-others-buffer)
58              (concat
59               (riece-concat-server-name
60                (riece-concat-user-status
61                 status
62                 (format "%s is (%s)"
63                         (riece-format-identity
64                          (riece-make-identity user riece-server-name)
65                          t)
66                         (riece-strip-user-at-host user-at-host))))
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]+\\) [^:]*:seconds")
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         (let* ((channel-identity (riece-make-identity channel
242                                                       riece-server-name))
243                (buffer (riece-channel-buffer channel-identity)))
244           (riece-insert-info buffer (concat "Users: " string "\n"))
245           (riece-insert-info
246            (if (and riece-channel-buffer-mode
247                     (not (eq buffer riece-channel-buffer)))
248                (list riece-dialogue-buffer riece-others-buffer)
249              riece-dialogue-buffer)
250            (concat
251             (riece-concat-server-name
252              (format "Users on %s: %s"
253                      (riece-format-identity channel-identity t) string))
254             "\n")))
255         (riece-redisplay-buffers))))
256
257 (defun riece-handle-322-message (prefix number name string)
258   (if (string-match "^\\([^ ]+\\) \\([0-9]+\\) :" string)
259       (let* ((channel (match-string 1 string))
260              (visible (match-string 2 string))
261              (topic (substring string (match-end 0))))
262         (riece-channel-set-topic (riece-get-channel channel) topic)
263         (let* ((channel-identity (riece-make-identity channel
264                                                       riece-server-name))
265                (buffer (riece-channel-buffer channel-identity)))
266           (riece-insert-info buffer (concat visible " users, topic: "
267                                             topic "\n"))
268           (riece-insert-info
269            (if (and riece-channel-buffer-mode
270                     (not (eq buffer riece-channel-buffer)))
271                (list riece-dialogue-buffer riece-others-buffer)
272              riece-dialogue-buffer)
273            (concat
274             (riece-concat-server-name
275              (format "%s: %s users, topic: %s"
276                      (riece-format-identity channel-identity t) visible topic))
277             "\n"))))))
278
279 (defun riece-handle-324-message (prefix number name string)
280   (if (string-match "^\\([^ ]+\\) \\([^ ]+\\) " string)
281       (let* ((channel (match-string 1 string))
282              (mode-string (substring string (match-beginning 2)))
283              (modes (string-to-list (match-string 2 string)))
284              (toggle (car modes)))
285         (setq modes (cdr modes))
286         (while modes
287           (riece-channel-toggle-mode channel (car modes) (eq toggle ?+))
288           (setq modes (cdr modes)))
289         (let* ((channel-identity (riece-make-identity channel
290                                                       riece-server-name))
291                (buffer (riece-channel-buffer channel-identity)))
292           (riece-insert-info buffer (concat "Mode: " mode-string "\n"))
293           (riece-insert-info
294            (if (and riece-channel-buffer-mode
295                     (not (eq buffer riece-channel-buffer)))
296                (list riece-dialogue-buffer riece-others-buffer)
297              riece-dialogue-buffer)
298            (concat
299             (riece-concat-server-name
300              (format "Mode for %s: %s"
301                      (riece-format-identity channel-identity t)
302                      mode-string))
303             "\n")))
304         (riece-update-channel-indicator)
305         (force-mode-line-update t))))
306
307 (defun riece-handle-set-topic (prefix number name string remove)
308   (if (string-match "^\\([^ ]+\\) :" string)
309       (let* ((channel (match-string 1 string))
310              (message (substring string (match-end 0)))
311              (channel-identity (riece-make-identity channel riece-server-name))
312              (buffer (riece-channel-buffer channel-identity)))
313         (if remove
314             (riece-channel-set-topic (riece-get-channel channel) nil)
315           (riece-channel-set-topic (riece-get-channel channel) message)
316         (riece-insert-info buffer (concat "Topic: " message "\n"))
317         (riece-insert-info
318          (if (and riece-channel-buffer-mode
319                   (not (eq buffer riece-channel-buffer)))
320              (list riece-dialogue-buffer riece-others-buffer)
321            riece-dialogue-buffer)
322          (concat
323           (riece-concat-server-name
324            (format "Topic for %s: %s"
325                    (riece-format-identity channel-identity t)
326                    message))
327           "\n"))
328         (riece-update-channel-indicator)))))
329
330 (defun riece-handle-331-message (prefix number name string)
331   (riece-handle-set-topic prefix number name string t))
332
333 (defun riece-handle-332-message (prefix number name string)
334   (riece-handle-set-topic prefix number name string nil))
335
336 (defun riece-handle-341-message (prefix number name string)
337   (if (string-match "^\\([^ ]+\\) " string)
338       (let* ((channel (match-string 1 string))
339              (user (substring string (match-end 0)))
340              (channel-identity (riece-make-identity channel riece-server-name))
341              (buffer (riece-channel-buffer channel-identity)))
342         (riece-insert-info buffer (concat "Inviting " user "\n"))
343         (riece-insert-info
344          (if (and riece-channel-buffer-mode
345                   (not (eq buffer riece-channel-buffer)))
346              (list riece-dialogue-buffer riece-others-buffer)
347            riece-dialogue-buffer)
348          (concat
349           (riece-concat-server-name
350            (format "Inviting %s to %s" user
351                    (riece-format-identity channel-identity t)))
352           "\n")))))
353
354 (defun riece-handle-352-message (prefix number name string)
355   (if (string-match "^\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([HG]\\)\\(\\*\\)?\\([@+]\\)? :\\([0-9]+\\) " string)
356       (let* ((channel (match-string 1 string))
357              (user (match-string 2 string))
358              (host (match-string 3 string))
359              (server (match-string 4 string))
360              (nick (match-string 5 string))
361              (away (equal (match-string 6 string) "G"))
362              (operator (not (null (match-beginning 7))))
363              (flag (match-string 8 string))
364              (hops (match-string 9 string))
365              (name (substring string (match-end 0)))
366              (buffer (riece-channel-buffer (riece-make-identity
367                                             channel riece-server-name)))
368              (info (format "%10s = %s (%s)"
369                            (concat
370                             (if (memq flag '(?@ ?+))
371                                 (char-to-string flag)
372                               " ")
373                             (riece-format-identity
374                              (riece-make-identity nick riece-server-name)
375                              t))
376                            name
377                            (riece-strip-user-at-host
378                             (concat user "@" host))))
379              status)
380         (if operator
381             (setq status (cons "operator" status)))
382         (if away
383             (setq status (cons "away" status)))
384         (unless (equal hops "0")
385           (setq status (cons (concat "on " server)
386                              (cons (concat hops " hops")
387                                    status))))
388         (if status
389             (setq status (nreverse status)))
390         (riece-naming-assert-join nick channel)
391         (riece-user-toggle-away user away)
392         (riece-user-toggle-operator user operator)
393         (riece-insert-info buffer (concat (riece-concat-user-status
394                                            status info)
395                                           "\n"))
396         (riece-insert-info
397          (if (and riece-channel-buffer-mode
398                   (not (eq buffer riece-channel-buffer)))
399              (list riece-dialogue-buffer riece-others-buffer)
400            riece-dialogue-buffer)
401          (concat
402           (riece-concat-server-name
403            (riece-concat-user-status
404             status
405             (concat
406              (riece-format-identity
407               (riece-make-identity channel riece-server-name)
408               t)
409              " "
410              info)))
411           "\n"))
412         (riece-redisplay-buffers))))
413
414 (defun riece-handle-315-message (prefix number name string))
415 (defun riece-handle-318-message (prefix number name string))
416 (defun riece-handle-323-message (prefix number name string))
417 (defun riece-handle-366-message (prefix number name string))
418
419 (provide 'riece-300)
420
421 ;;; riece-300.el ends here