1 ;;; liece-handle.el --- implementation of IRC message handlers
2 ;; Copyright (C) 1998-2000 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: IRC, liece
9 ;; This file is part of Liece.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
33 (require 'liece-inlines)
35 (require 'liece-intl))
37 (require 'liece-message)
38 (require 'liece-filter)
40 (require 'liece-handler)
42 (defmacro liece-handle-prepare-comment (rest &optional quote)
43 `(if (zerop (length ,rest))
46 (regexp-quote (format " (%s)" ,rest))
47 (format " (%s)" ,rest))))
49 (defmacro liece-handle-message-check-empty (msg)
52 (defmacro liece-handle-message-check-ignored (prefix rest)
54 (liece-ignore-this-p ,prefix liece-user-at-host)
55 (liece-message-from-ignored ,prefix ,rest)))
57 (defmacro liece-handle-check-changes-ignored ()
58 'liece-ignore-changes)
60 (defconst liece-handle-ctcp-message-regexp "\001\\(.*\\)\001")
62 (defmacro liece-handle-ctcp-message-p (msg)
63 `(string-match liece-handle-ctcp-message-regexp ,msg))
65 (autoload 'liece-ctcp-message "liece-ctcp")
66 (autoload 'liece-ctcp-notice "liece-ctcp")
69 (liece-handler-define-backend "generic")
73 (liece-handler-define-function
74 message '(prefix rest "generic")
75 (intern (format "liece-handle-%s-message" message)))
76 (defvar ,(intern (format "liece-%s-hook" message)) nil)
77 (defvar ,(intern (format "liece-after-%s-hook" message)) nil))
78 '("nick" "notice" "privmsg" "ping" "wall" "wallops" "quit" "topic"
79 "mode" "kick" "invite" "kill" "join" "part" "silence"))
81 (defun* liece-handle-nick-message (prefix rest)
82 (let ((chnls (liece-nick-get-joined-channels prefix)))
83 (liece-nick-change prefix rest)
85 ((liece-nick-equal prefix liece-real-nickname)
86 (setq liece-nickname-last liece-real-nickname
87 liece-real-nickname rest))
88 ((liece-nick-member prefix liece-current-chat-partners)
89 (setq liece-current-chat-partners
90 (string-list-modify-ignore-case (list (cons prefix rest))
91 liece-current-chat-partners))
92 (setcar (string-assoc-ignore-case prefix liece-nick-buffer-alist)
94 (setcar (string-assoc-ignore-case prefix liece-channel-buffer-alist)
96 (if (liece-nick-equal prefix liece-current-chat-partner)
97 (setq liece-current-chat-partner rest))
98 (add-to-list 'chnls rest)
99 (liece-channel-change)))
100 (if (liece-handle-check-changes-ignored)
101 (return-from liece-handle-nick-message))
102 (liece-insert-change (append (liece-pick-buffer chnls)
103 liece-D-buffer liece-O-buffer)
104 (format (_ "%s is now known as %s\n") prefix rest))))
106 (defun* liece-handle-notice-message (prefix rest)
107 (if (liece-handle-message-check-ignored prefix rest)
108 (return-from liece-handle-notice-message))
109 (or liece-ignore-extra-notices
111 (string-match "as being away" rest)
112 (return-from liece-handle-notice-message))
114 ;; No prefix. This is a server notice.
115 (when (and (null prefix) (string-match "^[^ ]* +:?" rest))
116 (liece-insert-notice (append liece-D-buffer liece-O-buffer)
117 (concat (substring rest (match-end 0)) "\n"))
118 (return-from liece-handle-notice-message))
120 (with-liece-decryption (rest prefix)
121 (if (run-hook-with-args-until-success 'liece-notice-cleartext-hook
123 (return-from liece-handle-notice-message))
125 (multiple-value-bind (chnl temp) (liece-split-line rest)
126 ;; This is a ctcp reply but contains additional messages
127 ;; at the left or/and right side.
128 (if (liece-handle-ctcp-message-p temp)
129 (setq temp (liece-ctcp-notice prefix temp)))
130 (if (liece-handle-message-check-empty temp)
131 (return-from liece-handle-notice-message))
133 ;; Normal message via notice.
134 (setq chnl (liece-channel-virtual chnl))
135 (let ((liece-message-target chnl)
136 (liece-message-speaker prefix)
137 (liece-message-type 'notice))
138 (liece-display-message temp)))))
140 (defun* liece-handle-privmsg-message (prefix rest)
141 (if (liece-handle-message-check-ignored prefix rest)
142 (return-from liece-handle-privmsg-message))
143 (with-liece-decryption (rest prefix)
144 (if (run-hook-with-args-until-success 'liece-privmsg-cleartext-hook
146 (return-from liece-handle-privmsg-message))
148 (multiple-value-bind (chnl temp) (liece-split-line rest)
149 (setq temp (or temp ""))
150 ;; This is a ctcp request but contains additional messages
151 ;; at the left or/and right side.
152 (if (liece-handle-ctcp-message-p temp)
153 (setq temp (liece-ctcp-message prefix chnl temp)))
154 (if (liece-handle-message-check-empty temp)
155 (return-from liece-handle-privmsg-message))
157 (setq chnl (liece-channel-virtual chnl))
160 (if liece-beep-on-bells
162 (and (string-match "\007" rest) (beep t))
163 (if (liece-nick-equal chnl liece-real-nickname)
164 (and liece-beep-when-privmsg (beep t))
165 (with-current-buffer (if liece-channel-buffer-mode
166 (format liece-channel-buffer-format
168 liece-dialogue-buffer)
169 (and liece-beep (beep t))))
171 (word (nth 0 liece-beep-words-list)))
173 (and (string-match word rest) (beep t))
175 (setq word (nth i liece-beep-words-list))))))
177 ;; Append timestamp if we are being away.
178 (and (string-equal "A" liece-away-indicator)
179 (liece-nick-equal chnl liece-real-nickname)
182 (funcall liece-format-time-function (current-time))
186 (let ((liece-message-target chnl)
187 (liece-message-speaker prefix)
188 (liece-message-type 'privmsg))
189 (liece-display-message temp))
191 (and (liece-nick-equal chnl liece-real-nickname)
192 (not (liece-nick-equal prefix liece-current-chat-partner))
193 (liece-message (_ "A private message has arrived from %s")
196 (defun liece-handle-ping-message (prefix rest)
197 (liece-send "PONG :%s" rest)
198 (liece-command-timestamp-if-interval-expired t)
201 (defun liece-handle-wall-message (prefix rest)
202 (liece-insert-broadcast (append liece-D-buffer liece-O-buffer)
203 (concat (if prefix (concat "from " prefix) "") " "
206 (defun liece-handle-wallops-message (prefix rest)
207 (if liece-show-wallops
208 (liece-insert-wallops (append liece-D-buffer liece-O-buffer)
209 (concat (if prefix prefix "UNKNOWN")
211 (liece-insert-wallops liece-W-buffer
212 (concat (if prefix (concat "from " prefix) "") " "
215 (defun* liece-handle-quit-message (prefix rest)
216 (let ((chnls (liece-nick-get-joined-channels prefix)) text match default)
217 ;; Mark temporary apart, if quitting user is one of our chat partners.
218 (when (liece-nick-member prefix liece-current-chat-partners)
219 (add-to-list 'chnls prefix)
220 (liece-nick-mark-as-apart prefix))
221 (if (liece-handle-check-changes-ignored)
222 (return-from liece-handle-quit-message))
224 (liece-compress-changes
225 (setq text (format (_ " \\(has\\|have\\) left IRC%s")
226 (liece-handle-prepare-comment rest t))
227 match (format "^%s%s.*%s$"
228 (if liece-display-time
229 liece-time-prefix-regexp "")
230 (regexp-quote liece-change-prefix)
232 default (format (_ "%s%s has left IRC%s\n")
233 liece-change-prefix prefix
234 (liece-handle-prepare-comment rest)))
235 (liece-replace (append (liece-pick-buffer chnls)
236 liece-D-buffer liece-O-buffer)
238 (format (_ ", %s have left IRC%s")
239 prefix (liece-handle-prepare-comment rest))))
241 (liece-insert-change (append (liece-pick-buffer chnls)
242 liece-D-buffer liece-O-buffer)
243 (format (_ "%s has left IRC%s\n")
244 (liece-handle-prepare-comment rest)))))
245 (liece-nick-change prefix nil)))
247 (defun* liece-handle-topic-message (prefix rest)
248 (multiple-value-bind (chnl topic) (liece-split-line rest)
249 (setq chnl (liece-channel-virtual chnl)
251 (liece-channel-set-topic topic chnl)
252 (if (liece-handle-check-changes-ignored)
253 (return-from liece-handle-topic-message))
254 (liece-insert-change (liece-pick-buffer chnl)
255 (format (_ "New topic on channel %s set by %s: %s\n")
257 (liece-insert-change (if (liece-nick-equal chnl liece-current-channel)
259 (append liece-D-buffer liece-O-buffer))
260 (format (_ "New topic on channel %s set by %s: %s\n")
262 (liece-set-channel-indicator)))
264 (defun* liece-handle-mode-message (prefix rest)
265 (if (liece-handle-check-changes-ignored)
266 (return-from liece-handle-mode-message))
267 (let ((chnl " ") (str "") mflag mflags marg margs val md chnlp)
268 (or (and (string-match "\\([^ ]*\\) +:?" rest)
269 (setq chnl (match-string 1 rest)
270 str (substring rest (match-end 0))
271 chnlp (liece-channel-p chnl)
272 str (if (= (aref str (1- (length str))) ? )
273 (substring str 0 -1) str)
274 chnl (liece-channel-virtual chnl)))
275 (and (string-match " +:" rest)
276 (setq str (substring rest (match-end 0))))
277 (return-from liece-handle-mode-message))
280 (when (string-match "\\([^ ]*\\) +" str)
281 (setq mflag (match-string 1 str)
282 marg (substring str (match-end 0))
283 mflags (string-to-char-list mflag))
284 (while (string-match "^\\([^ ]*\\) +" marg)
285 (setq margs (cons (match-string 1 marg) margs)
286 marg (substring marg (match-end 0))))
287 (or (string= marg "") (setq margs (cons marg margs)))
288 (while (setq md (pop mflags))
289 (cond ((eq ?- md) (setq val nil))
290 ((eq ?+ md) (setq val t))
291 ((eq ?o md) (liece-channel-set-operator chnl (pop margs) val))
292 ((eq ?v md) (liece-channel-set-voice chnl (pop margs) val))
293 ((eq ?b md) (liece-channel-set-ban chnl (pop margs) val))
294 ((eq ?e md) (liece-channel-set-exception chnl (pop margs) val))
295 ((eq ?I md) (liece-channel-set-invite chnl (pop margs) val))
296 (chnlp (liece-channel-set-mode val md chnl))
297 (t (liece-nick-set-mode val md chnl)))))
299 (liece-set-channel-indicator)
301 (liece-compress-changes
302 (let* ((text (concat (regexp-quote rest) "\n"))
303 (match (format (_ "^%s%sNew mode for %s set by %s: ")
304 (if liece-display-time
305 liece-time-prefix-regexp "")
306 (regexp-quote liece-change-prefix)
307 (regexp-quote chnl) (regexp-quote prefix)))
308 (default (format (_ "%sNew mode for %s set by %s: %s\n")
309 liece-change-prefix chnl prefix str)))
310 (liece-replace (liece-pick-buffer chnl)
311 match default text (concat ", " str "\n"))
312 (liece-replace (if (and liece-current-channel
314 chnl liece-current-channel))
316 (append liece-D-buffer liece-O-buffer))
317 match default text (concat ", " str "\n"))))
319 (liece-insert-change (liece-pick-buffer chnl)
320 (format (_ "New mode for %s set by %s: %s\n")
322 (liece-insert-change (if (and liece-current-channel
324 chnl liece-current-channel))
326 (append liece-D-buffer liece-O-buffer))
327 (format (_ "New mode for %s set by %s: %s\n")
328 chnl prefix str))))))
330 (defun* liece-handle-kick-message (prefix rest)
331 (if (/= 3 (length (setq rest (liece-split-line rest))))
332 (return-from liece-handle-kick-message))
333 (multiple-value-bind (chnl nick message) rest
334 (setq chnl (liece-channel-virtual chnl))
336 (if (liece-nick-equal nick liece-real-nickname)
339 (liece-pick-buffer chnl)
340 (format (_ "You were kicked off channel %s by %s (%s).\n")
341 chnl prefix message))
342 (liece-channel-part chnl))
343 (liece-nick-part nick chnl))
345 (if (liece-handle-check-changes-ignored)
346 (return-from liece-handle-kick-message))
349 (append (liece-pick-buffer chnl)
350 (if (liece-channel-equal chnl liece-current-channel)
352 (append liece-D-buffer liece-O-buffer)))
353 (format "%s has kicked %s out%s%s\n"
355 (if (string= (or liece-current-channel "") chnl)
357 (format " from channel %s" chnl))
360 (format " (%s)" message))))))
362 (defun* liece-handle-invite-message (prefix rest)
363 (or (string-match " +:" rest)
364 (return-from liece-handle-invite-message))
365 (and liece-beep-when-invited liece-beep-on-bells
367 (let ((chnl (liece-channel-virtual (substring rest (match-end 0)))))
368 (liece-insert-info (append liece-D-buffer liece-O-buffer)
369 (format "%s invites you to channel %s\n"
371 (setq liece-default-channel-candidate chnl)))
373 (defun* liece-handle-kill-message (prefix rest)
374 (or (string-match " +:" rest)
375 (return-from liece-handle-kill-message))
376 (let ((path (substring rest (match-end 0))))
377 (liece-insert-info (append liece-D-buffer liece-O-buffer)
378 (format "You were killed by %s. (Path: %s. RIP)\n"
380 (liece-clear-system))
382 (defun* liece-handle-join-message (prefix rest)
383 (let (flag (xnick prefix) (nick prefix) (chnl rest))
385 ((string-match "\007[ov]" chnl)
386 (setq flag (aref (match-string 0 chnl) 1)
387 chnl (substring rest 0 (match-beginning 0))))
388 ((string-match " +$" chnl)
389 (setq chnl (substring chnl 0 (match-beginning 0)))))
390 (setq chnl (liece-channel-virtual chnl))
392 (liece-nick-set-user-at-host nick liece-user-at-host)
394 (if (liece-nick-equal nick liece-real-nickname)
396 (and liece-gather-channel-modes
397 (not (liece-channel-modeless-p (liece-channel-real chnl)))
398 (liece-send "MODE %s " (liece-channel-real chnl)))
399 (liece-channel-join chnl))
400 (liece-nick-join nick chnl))
404 (liece-channel-set-operator chnl xnick t)
405 (setq xnick (concat "@" xnick)))
407 (liece-channel-set-voice chnl xnick t)
408 (setq xnick (concat "+" xnick))))
410 (if (liece-handle-check-changes-ignored)
411 (return-from liece-handle-join-message))
413 (when (and (liece-nick-member nick liece-current-chat-partners)
414 (get (intern nick liece-obarray) 'part))
415 (liece-insert-change (liece-pick-buffer nick)
416 (format (_ "%s has come back as (%s)\n")
417 nick liece-user-at-host))
418 (liece-nick-unmark-as-apart nick))
421 (liece-compress-changes
422 (let* ((text (format (_ " \\(has\\|have\\) joined channel %s")
423 (regexp-quote chnl)))
424 (match (format "^%s%s.*%s$"
425 (if liece-display-time
426 liece-time-prefix-regexp "")
427 (regexp-quote liece-change-prefix)
428 (regexp-quote text)))
429 (default (format (_ "%s%s (%s) has joined channel %s\n")
431 nick liece-user-at-host chnl)))
432 (liece-replace (liece-pick-buffer chnl)
434 (format (_ ", %s (%s) have joined channel %s")
435 nick liece-user-at-host chnl))
436 (liece-replace (if (and liece-current-channel
437 (liece-channel-equal chnl
438 liece-current-channel))
440 (append liece-D-buffer liece-O-buffer))
442 (format (_ ", %s (%s) have joined channel %s")
443 nick liece-user-at-host chnl))))
445 (liece-insert-change (liece-pick-buffer chnl)
446 (format (_ "%s (%s) has joined channel %s\n")
447 nick liece-user-at-host chnl))
448 (liece-insert-change (if (liece-channel-equal chnl
449 liece-current-channel)
451 (append liece-D-buffer liece-O-buffer))
452 (format (_ "%s (%s) has joined channel %s\n")
453 nick liece-user-at-host chnl))))))
455 (defun* liece-handle-part-message (prefix rest)
456 (multiple-value-bind (chnl comment text match default buf) (liece-split-line rest)
457 (setq chnl (liece-channel-virtual chnl)
458 comment (liece-handle-prepare-comment comment))
460 (if (liece-nick-equal prefix liece-real-nickname)
461 (liece-channel-part chnl)
462 (liece-nick-part prefix chnl))
464 (if (liece-handle-check-changes-ignored)
465 (return-from liece-handle-part-message))
467 (setq buf (append liece-D-buffer (liece-pick-buffer chnl)))
468 (unless (and liece-current-channel
469 (liece-channel-equal chnl liece-current-channel))
470 (setq buf (append buf liece-O-buffer)))
472 (liece-compress-changes
473 (setq text (format (_ " \\(has\\|have\\) left channel %s%s")
474 (regexp-quote chnl) (regexp-quote comment))
475 match (format "^%s%s.*%s$"
476 (if liece-display-time
477 liece-time-prefix-regexp "")
478 (regexp-quote liece-change-prefix)
480 default (format (_ "%s%s has left channel %s%s\n")
481 liece-change-prefix prefix chnl comment))
484 (format (_ ", %s have left channel %s%s")
485 prefix chnl comment)))
487 (liece-insert-change buf
488 (format (_ "%s has left channel %s%s\n")
489 prefix chnl comment))))))
491 (defun* liece-handle-silence-message (prefix rest)
492 (let* ((flag (aref rest 0)) (rest (substring rest 1)))
493 (liece-insert-info (append liece-D-buffer liece-O-buffer)
495 (if (eq flag ?-) "unsilenced" "silenced")))))
497 (provide 'liece-handle)
499 ;;; liece-handle.el ends here