bf1f52f2549ee8b417594c0a96aeb13084736955
[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             (start 0)
224             user users)
225         (setq string (substring string (match-end 0)))
226         (while (string-match
227                 (concat "\\([@+]\\)?\\(" riece-user-regexp "\\) *")
228                 string start)
229           (put-text-property (match-beginning 2) (match-end 2)
230                              'riece-identity
231                              (riece-make-identity (match-string 2 string)
232                                                   riece-server-name)
233                              string)
234           (setq start (match-end 0)
235                 user (if (match-beginning 1)
236                          (if (eq (aref string (match-beginning 1)) ?@)
237                              (list (match-string 2 string) ?o)
238                            (if (eq (aref string (match-beginning 1)) ?+)
239                                (list (match-string 2 string) ?v)))
240                        (list (match-string 2 string)))
241                 users (cons user users)))
242         (riece-naming-assert-names (nreverse users) channel)
243         (let* ((channel-identity (riece-make-identity channel
244                                                       riece-server-name))
245                (buffer (riece-channel-buffer channel-identity)))
246           (riece-insert-info buffer (concat "Users: " string "\n"))
247           (riece-insert-info
248            (if (and riece-channel-buffer-mode
249                     (not (eq buffer riece-channel-buffer)))
250                (list riece-dialogue-buffer riece-others-buffer)
251              riece-dialogue-buffer)
252            (concat
253             (riece-concat-server-name
254              (format "Users on %s: %s"
255                      (riece-format-identity channel-identity t) string))
256             "\n"))))))
257
258 (defun riece-handle-322-message (prefix number name string)
259   (if (string-match "^\\([^ ]+\\) \\([0-9]+\\) :" string)
260       (let* ((channel (match-string 1 string))
261              (visible (match-string 2 string))
262              (topic (substring string (match-end 0))))
263         (riece-channel-set-topic (riece-get-channel channel) topic)
264         (let* ((channel-identity (riece-make-identity channel
265                                                       riece-server-name))
266                (buffer (riece-channel-buffer channel-identity)))
267           (riece-insert-info buffer (concat visible " users, topic: "
268                                             topic "\n"))
269           (riece-insert-info
270            (if (and riece-channel-buffer-mode
271                     (not (eq buffer riece-channel-buffer)))
272                (list riece-dialogue-buffer riece-others-buffer)
273              riece-dialogue-buffer)
274            (concat
275             (riece-concat-server-name
276              (format "%s: %s users, topic: %s"
277                      (riece-format-identity channel-identity t) visible topic))
278             "\n"))))))
279
280 (defun riece-handle-324-message (prefix number name string)
281   (if (string-match "^\\([^ ]+\\) \\([^ ]+\\) " string)
282       (let* ((channel (match-string 1 string))
283              (mode-string (substring string (match-beginning 2)))
284              (modes (string-to-list (match-string 2 string)))
285              (toggle (car modes)))
286         (setq modes (cdr modes))
287         (while modes
288           (riece-channel-toggle-mode channel (car modes) (eq toggle ?+))
289           (setq modes (cdr modes)))
290         (let* ((channel-identity (riece-make-identity channel
291                                                       riece-server-name))
292                (buffer (riece-channel-buffer channel-identity)))
293           (riece-insert-info buffer (concat "Mode: " mode-string "\n"))
294           (riece-insert-info
295            (if (and riece-channel-buffer-mode
296                     (not (eq buffer riece-channel-buffer)))
297                (list riece-dialogue-buffer riece-others-buffer)
298              riece-dialogue-buffer)
299            (concat
300             (riece-concat-server-name
301              (format "Mode for %s: %s"
302                      (riece-format-identity channel-identity t)
303                      mode-string))
304             "\n")))
305         (riece-update-channel-indicator)
306         (force-mode-line-update t))))
307
308 (defun riece-handle-set-topic (prefix number name string remove)
309   (if (string-match "^\\([^ ]+\\) :" string)
310       (let* ((channel (match-string 1 string))
311              (message (substring string (match-end 0)))
312              (channel-identity (riece-make-identity channel riece-server-name))
313              (buffer (riece-channel-buffer channel-identity)))
314         (if remove
315             (riece-channel-set-topic (riece-get-channel channel) nil)
316           (riece-channel-set-topic (riece-get-channel channel) message)
317         (riece-insert-info buffer (concat "Topic: " message "\n"))
318         (riece-insert-info
319          (if (and riece-channel-buffer-mode
320                   (not (eq buffer riece-channel-buffer)))
321              (list riece-dialogue-buffer riece-others-buffer)
322            riece-dialogue-buffer)
323          (concat
324           (riece-concat-server-name
325            (format "Topic for %s: %s"
326                    (riece-format-identity channel-identity t)
327                    message))
328           "\n"))
329         (riece-update-channel-indicator)))))
330
331 (defun riece-handle-331-message (prefix number name string)
332   (riece-handle-set-topic prefix number name string t))
333
334 (defun riece-handle-332-message (prefix number name string)
335   (riece-handle-set-topic prefix number name string nil))
336
337 (defun riece-handle-341-message (prefix number name string)
338   (if (string-match "^\\([^ ]+\\) " string)
339       (let* ((channel (match-string 1 string))
340              (user (substring string (match-end 0)))
341              (channel-identity (riece-make-identity channel riece-server-name))
342              (buffer (riece-channel-buffer channel-identity)))
343         (riece-insert-info buffer (concat "Inviting " user "\n"))
344         (riece-insert-info
345          (if (and riece-channel-buffer-mode
346                   (not (eq buffer riece-channel-buffer)))
347              (list riece-dialogue-buffer riece-others-buffer)
348            riece-dialogue-buffer)
349          (concat
350           (riece-concat-server-name
351            (format "Inviting %s to %s" user
352                    (riece-format-identity channel-identity t)))
353           "\n")))))
354
355 (defun riece-handle-352-message (prefix number name string)
356   (if (string-match "^\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\([HG]\\)\\(\\*\\)?\\([@+]\\)? :\\([0-9]+\\) " string)
357       (let* ((channel (match-string 1 string))
358              (user (match-string 2 string))
359              (host (match-string 3 string))
360              (server (match-string 4 string))
361              (nick (match-string 5 string))
362              (away (equal (match-string 6 string) "G"))
363              (operator (not (null (match-beginning 7))))
364              (flag (match-string 8 string))
365              (hops (match-string 9 string))
366              (name (substring string (match-end 0)))
367              (buffer (riece-channel-buffer (riece-make-identity
368                                             channel riece-server-name)))
369              (info (format "%10s = %s (%s)"
370                            (concat
371                             (if (memq flag '(?@ ?+))
372                                 (char-to-string flag)
373                               " ")
374                             (riece-format-identity
375                              (riece-make-identity nick riece-server-name)
376                              t))
377                            name
378                            (riece-strip-user-at-host
379                             (concat user "@" host))))
380              status)
381         (if operator
382             (setq status (cons "operator" status)))
383         (if away
384             (setq status (cons "away" status)))
385         (unless (equal hops "0")
386           (setq status (cons (concat "on " server)
387                              (cons (concat hops " hops")
388                                    status))))
389         (if status
390             (setq status (nreverse status)))
391         (riece-naming-assert-join nick channel)
392         (riece-user-toggle-away user away)
393         (riece-user-toggle-operator user operator)
394         (riece-insert-info buffer (concat (riece-concat-user-status
395                                            status info)
396                                           "\n"))
397         (riece-insert-info
398          (if (and riece-channel-buffer-mode
399                   (not (eq buffer riece-channel-buffer)))
400              (list riece-dialogue-buffer riece-others-buffer)
401            riece-dialogue-buffer)
402          (concat
403           (riece-concat-server-name
404            (riece-concat-user-status
405             status
406             (concat
407              (riece-format-identity
408               (riece-make-identity channel riece-server-name)
409               t)
410              " "
411              info)))
412           "\n")))))
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