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 (liece-pick-buffer-1 chnl)
167 liece-dialogue-buffer)
168 (and liece-beep (beep t))))
170 (word (nth 0 liece-beep-words-list)))
172 (and (string-match word rest) (beep t))
174 (setq word (nth i liece-beep-words-list))))))
176 ;; Append timestamp if we are being away.
177 (and (string-equal "A" liece-away-indicator)
178 (liece-nick-equal chnl liece-real-nickname)
181 (funcall liece-format-time-function (current-time))
185 (let ((liece-message-target chnl)
186 (liece-message-speaker prefix)
187 (liece-message-type 'privmsg))
188 (liece-display-message temp))
190 (and (liece-nick-equal chnl liece-real-nickname)
191 (not (liece-nick-equal prefix liece-current-chat-partner))
192 (liece-message (_ "A private message has arrived from %s")
195 (defun liece-handle-ping-message (prefix rest)
196 (liece-send "PONG :%s" rest)
197 (liece-command-timestamp-if-interval-expired t)
200 (defun liece-handle-wall-message (prefix rest)
201 (liece-insert-broadcast (append liece-D-buffer liece-O-buffer)
202 (concat (if prefix (concat "from " prefix) "") " "
205 (defun liece-handle-wallops-message (prefix rest)
206 (if liece-show-wallops
207 (liece-insert-wallops (append liece-D-buffer liece-O-buffer)
208 (concat (if prefix prefix "UNKNOWN")
210 (liece-insert-wallops liece-W-buffer
211 (concat (if prefix (concat "from " prefix) "") " "
214 (defun* liece-handle-quit-message (prefix rest)
215 (let ((chnls (liece-nick-get-joined-channels prefix)) text match default)
216 ;; Mark temporary apart, if quitting user is one of our chat partners.
217 (when (liece-nick-member prefix liece-current-chat-partners)
218 (add-to-list 'chnls prefix)
219 (liece-nick-mark-as-apart prefix))
220 (if (liece-handle-check-changes-ignored)
221 (return-from liece-handle-quit-message))
223 (liece-compress-changes
224 (setq text (format (_ " \\(has\\|have\\) left IRC%s")
225 (liece-handle-prepare-comment rest t))
226 match (format "^%s%s.*%s$"
227 (if liece-display-time
228 liece-time-prefix-regexp "")
229 (regexp-quote liece-change-prefix)
231 default (format (_ "%s%s has left IRC%s\n")
232 liece-change-prefix prefix
233 (liece-handle-prepare-comment rest)))
234 (liece-replace (append (liece-pick-buffer chnls)
235 liece-D-buffer liece-O-buffer)
237 (format (_ ", %s have left IRC%s")
238 prefix (liece-handle-prepare-comment rest))))
240 (liece-insert-change (append (liece-pick-buffer chnls)
241 liece-D-buffer liece-O-buffer)
242 (format (_ "%s has left IRC%s\n")
243 (liece-handle-prepare-comment rest)))))
244 (liece-nick-change prefix nil)))
246 (defun* liece-handle-topic-message (prefix rest)
247 (multiple-value-bind (chnl topic) (liece-split-line rest)
248 (setq chnl (liece-channel-virtual chnl)
250 (liece-channel-set-topic topic chnl)
251 (if (liece-handle-check-changes-ignored)
252 (return-from liece-handle-topic-message))
253 (liece-insert-change (liece-pick-buffer chnl)
254 (format (_ "New topic on channel %s set by %s: %s\n")
256 (liece-insert-change (if (liece-nick-equal chnl liece-current-channel)
258 (append liece-D-buffer liece-O-buffer))
259 (format (_ "New topic on channel %s set by %s: %s\n")
261 (liece-set-channel-indicator)))
263 (defun* liece-handle-mode-message (prefix rest)
264 (if (liece-handle-check-changes-ignored)
265 (return-from liece-handle-mode-message))
266 (let ((chnl " ") (str "") mflag mflags marg margs val md chnlp)
267 (or (and (string-match "\\([^ ]*\\) +:?" rest)
268 (setq chnl (match-string 1 rest)
269 str (substring rest (match-end 0))
270 chnlp (liece-channel-p chnl)
271 str (if (= (aref str (1- (length str))) ? )
272 (substring str 0 -1) str)
273 chnl (liece-channel-virtual chnl)))
274 (and (string-match " +:" rest)
275 (setq str (substring rest (match-end 0))))
276 (return-from liece-handle-mode-message))
279 (when (string-match "\\([^ ]*\\) +" str)
280 (setq mflag (match-string 1 str)
281 marg (substring str (match-end 0))
282 mflags (string-to-char-list mflag))
283 (while (string-match "^\\([^ ]*\\) +" marg)
284 (setq margs (cons (match-string 1 marg) margs)
285 marg (substring marg (match-end 0))))
286 (or (string= marg "") (setq margs (cons marg margs)))
287 (while (setq md (pop mflags))
288 (cond ((eq ?- md) (setq val nil))
289 ((eq ?+ md) (setq val t))
290 ((eq ?o md) (liece-channel-set-operator chnl (pop margs) val))
291 ((eq ?v md) (liece-channel-set-voice chnl (pop margs) val))
292 ((eq ?b md) (liece-channel-set-ban chnl (pop margs) val))
293 ((eq ?e md) (liece-channel-set-exception chnl (pop margs) val))
294 ((eq ?I md) (liece-channel-set-invite chnl (pop margs) val))
295 (chnlp (liece-channel-set-mode val md chnl))
296 (t (liece-nick-set-mode val md chnl)))))
298 (liece-set-channel-indicator)
300 (liece-compress-changes
301 (let* ((text (concat (regexp-quote rest) "\n"))
302 (match (format (_ "^%s%sNew mode for %s set by %s: ")
303 (if liece-display-time
304 liece-time-prefix-regexp "")
305 (regexp-quote liece-change-prefix)
306 (regexp-quote chnl) (regexp-quote prefix)))
307 (default (format (_ "%sNew mode for %s set by %s: %s\n")
308 liece-change-prefix chnl prefix str)))
309 (liece-replace (liece-pick-buffer chnl)
310 match default text (concat ", " str "\n"))
311 (liece-replace (if (and liece-current-channel
313 chnl liece-current-channel))
315 (append liece-D-buffer liece-O-buffer))
316 match default text (concat ", " str "\n"))))
318 (liece-insert-change (liece-pick-buffer chnl)
319 (format (_ "New mode for %s set by %s: %s\n")
321 (liece-insert-change (if (and liece-current-channel
323 chnl liece-current-channel))
325 (append liece-D-buffer liece-O-buffer))
326 (format (_ "New mode for %s set by %s: %s\n")
327 chnl prefix str))))))
329 (defun* liece-handle-kick-message (prefix rest)
330 (if (/= 3 (length (setq rest (liece-split-line rest))))
331 (return-from liece-handle-kick-message))
332 (multiple-value-bind (chnl nick message) rest
333 (setq chnl (liece-channel-virtual chnl))
335 (if (liece-nick-equal nick liece-real-nickname)
338 (liece-pick-buffer chnl)
339 (format (_ "You were kicked off channel %s by %s (%s).\n")
340 chnl prefix message))
341 (liece-channel-part chnl))
342 (liece-nick-part nick chnl))
344 (if (liece-handle-check-changes-ignored)
345 (return-from liece-handle-kick-message))
348 (append (liece-pick-buffer chnl)
349 (if (liece-channel-equal chnl liece-current-channel)
351 (append liece-D-buffer liece-O-buffer)))
352 (format "%s has kicked %s out%s%s\n"
354 (if (string= (or liece-current-channel "") chnl)
356 (format " from channel %s" chnl))
359 (format " (%s)" message))))))
361 (defun* liece-handle-invite-message (prefix rest)
362 (or (string-match " +:" rest)
363 (return-from liece-handle-invite-message))
364 (and liece-beep-when-invited liece-beep-on-bells
366 (let ((chnl (liece-channel-virtual (substring rest (match-end 0)))))
367 (liece-insert-info (append liece-D-buffer liece-O-buffer)
368 (format "%s invites you to channel %s\n"
370 (setq liece-default-channel-candidate chnl)))
372 (defun* liece-handle-kill-message (prefix rest)
373 (or (string-match " +:" rest)
374 (return-from liece-handle-kill-message))
375 (let ((path (substring rest (match-end 0))))
376 (liece-insert-info (append liece-D-buffer liece-O-buffer)
377 (format "You were killed by %s. (Path: %s. RIP)\n"
379 (liece-clear-system))
381 (defun* liece-handle-join-message (prefix rest)
382 (let (flag (xnick prefix) (nick prefix) (chnl rest))
384 ((string-match "\007[ov]" chnl)
385 (setq flag (aref (match-string 0 chnl) 1)
386 chnl (substring rest 0 (match-beginning 0))))
387 ((string-match " +$" chnl)
388 (setq chnl (substring chnl 0 (match-beginning 0)))))
389 (setq chnl (liece-channel-virtual chnl))
391 (liece-nick-set-user-at-host nick liece-user-at-host)
393 (if (liece-nick-equal nick liece-real-nickname)
395 (and liece-gather-channel-modes
396 (not (liece-channel-modeless-p (liece-channel-real chnl)))
397 (liece-send "MODE %s " (liece-channel-real chnl)))
398 (liece-channel-join chnl))
399 (liece-nick-join nick chnl))
403 (liece-channel-set-operator chnl xnick t)
404 (setq xnick (concat "@" xnick)))
406 (liece-channel-set-voice chnl xnick t)
407 (setq xnick (concat "+" xnick))))
409 (if (liece-handle-check-changes-ignored)
410 (return-from liece-handle-join-message))
412 (when (and (liece-nick-member nick liece-current-chat-partners)
413 (get (intern nick liece-obarray) 'part))
414 (liece-insert-change (liece-pick-buffer nick)
415 (format (_ "%s has come back as (%s)\n")
416 nick liece-user-at-host))
417 (liece-nick-unmark-as-apart nick))
420 (liece-compress-changes
421 (let* ((text (format (_ " \\(has\\|have\\) joined channel %s")
422 (regexp-quote chnl)))
423 (match (format "^%s%s.*%s$"
424 (if liece-display-time
425 liece-time-prefix-regexp "")
426 (regexp-quote liece-change-prefix)
427 (regexp-quote text)))
428 (default (format (_ "%s%s (%s) has joined channel %s\n")
430 nick liece-user-at-host chnl)))
431 (liece-replace (liece-pick-buffer chnl)
433 (format (_ ", %s (%s) have joined channel %s")
434 nick liece-user-at-host chnl))
435 (liece-replace (if (and liece-current-channel
436 (liece-channel-equal chnl
437 liece-current-channel))
439 (append liece-D-buffer liece-O-buffer))
441 (format (_ ", %s (%s) have joined channel %s")
442 nick liece-user-at-host chnl))))
444 (liece-insert-change (liece-pick-buffer chnl)
445 (format (_ "%s (%s) has joined channel %s\n")
446 nick liece-user-at-host chnl))
447 (liece-insert-change (if (liece-channel-equal chnl
448 liece-current-channel)
450 (append liece-D-buffer liece-O-buffer))
451 (format (_ "%s (%s) has joined channel %s\n")
452 nick liece-user-at-host chnl))))))
454 (defun* liece-handle-part-message (prefix rest)
455 (multiple-value-bind (chnl comment text match default buf) (liece-split-line rest)
456 (setq chnl (liece-channel-virtual chnl)
457 comment (liece-handle-prepare-comment comment))
459 (if (liece-nick-equal prefix liece-real-nickname)
460 (liece-channel-part chnl)
461 (liece-nick-part prefix chnl))
463 (if (liece-handle-check-changes-ignored)
464 (return-from liece-handle-part-message))
466 (setq buf (append liece-D-buffer (liece-pick-buffer chnl)))
467 (unless (and liece-current-channel
468 (liece-channel-equal chnl liece-current-channel))
469 (setq buf (append buf liece-O-buffer)))
471 (liece-compress-changes
472 (setq text (format (_ " \\(has\\|have\\) left channel %s%s")
473 (regexp-quote chnl) (regexp-quote comment))
474 match (format "^%s%s.*%s$"
475 (if liece-display-time
476 liece-time-prefix-regexp "")
477 (regexp-quote liece-change-prefix)
479 default (format (_ "%s%s has left channel %s%s\n")
480 liece-change-prefix prefix chnl comment))
483 (format (_ ", %s have left channel %s%s")
484 prefix chnl comment)))
486 (liece-insert-change buf
487 (format (_ "%s has left channel %s%s\n")
488 prefix chnl comment))))))
490 (defun* liece-handle-silence-message (prefix rest)
491 (let* ((flag (aref rest 0)) (rest (substring rest 1)))
492 (liece-insert-info (append liece-D-buffer liece-O-buffer)
494 (if (eq flag ?-) "unsilenced" "silenced")))))
496 (provide 'liece-handle)
498 ;;; liece-handle.el ends here