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))
119 (if (run-hook-with-args-until-success 'liece-notice-cleartext-hook
121 (return-from liece-handle-notice-message))
123 (multiple-value-bind (chnl temp) (liece-split-line rest)
124 ;; This is a ctcp reply but contains additional messages
125 ;; at the left or/and right side.
126 (if (liece-handle-ctcp-message-p temp)
127 (setq temp (liece-ctcp-notice prefix temp)))
128 (if (liece-handle-message-check-empty temp)
129 (return-from liece-handle-notice-message))
131 ;; Normal message via notice.
132 (setq chnl (liece-channel-virtual chnl))
133 (let ((liece-message-target chnl)
134 (liece-message-speaker prefix)
135 (liece-message-type 'notice))
136 (liece-display-message temp))))
138 (defun* liece-handle-privmsg-message (prefix rest)
139 (if (liece-handle-message-check-ignored prefix rest)
140 (return-from liece-handle-privmsg-message))
141 (if (run-hook-with-args-until-success 'liece-privmsg-cleartext-hook
143 (return-from liece-handle-privmsg-message))
145 (multiple-value-bind (chnl temp) (liece-split-line rest)
146 (setq temp (or temp ""))
147 ;; This is a ctcp request but contains additional messages
148 ;; at the left or/and right side.
149 (if (liece-handle-ctcp-message-p temp)
150 (setq temp (liece-ctcp-message prefix chnl temp)))
151 (if (liece-handle-message-check-empty temp)
152 (return-from liece-handle-privmsg-message))
154 (setq chnl (liece-channel-virtual chnl))
156 (when liece-beep-on-bells
157 (if (string-match "\007" rest)
159 (if (liece-nick-equal chnl liece-real-nickname)
160 (and liece-beep-when-privmsg (liece-beep))
161 (with-current-buffer (if liece-channel-buffer-mode
162 (liece-pick-buffer-1 chnl)
163 liece-dialogue-buffer)
166 (dolist (word liece-beep-words-list)
167 (if (string-match word rest)
170 ;; Append timestamp if we are being away.
171 (if (and (string-equal "A" liece-away-indicator)
172 (liece-nick-equal chnl liece-real-nickname))
175 (funcall liece-format-time-function (current-time))
179 (let ((liece-message-target chnl)
180 (liece-message-speaker prefix)
181 (liece-message-type 'privmsg))
182 (liece-display-message temp))
184 ;; Append to the unread list.
185 (let ((item (if (eq liece-command-buffer 'chat)
186 liece-current-chat-partner
187 liece-current-channel)))
188 (unless (liece-channel-equal chnl item)
189 (if (liece-channel-unread-p chnl)
190 (setq liece-channel-unread-list
191 (delete chnl liece-channel-unread-list)))
192 (setq liece-channel-unread-list
193 (cons chnl liece-channel-unread-list))
194 (run-hook-with-args 'liece-channel-unread-hook chnl)))
196 (if (and (liece-nick-equal chnl liece-real-nickname)
197 (not (liece-nick-equal prefix liece-current-chat-partner)))
198 (liece-message (_ "A private message has arrived from %s")
201 (defun liece-handle-ping-message (prefix rest)
202 (liece-send "PONG :%s" rest)
203 (liece-command-timestamp-if-interval-expired t)
206 (defun liece-handle-wall-message (prefix rest)
207 (liece-insert-broadcast (append liece-D-buffer liece-O-buffer)
208 (concat (if prefix (concat "from " prefix) "") " "
211 (defun liece-handle-wallops-message (prefix rest)
212 (if liece-show-wallops
213 (liece-insert-wallops (append liece-D-buffer liece-O-buffer)
214 (concat (if prefix prefix "UNKNOWN")
216 (liece-insert-wallops liece-W-buffer
217 (concat (if prefix (concat "from " prefix) "") " "
220 (defun* liece-handle-quit-message (prefix rest)
221 (let ((chnls (liece-nick-get-joined-channels prefix)) text match default)
222 ;; Mark temporary apart, if quitting user is one of our chat partners.
223 (when (liece-nick-member prefix liece-current-chat-partners)
224 (add-to-list 'chnls prefix)
225 (liece-nick-mark-as-apart prefix))
226 (if (liece-handle-check-changes-ignored)
227 (return-from liece-handle-quit-message))
229 (liece-compress-changes
230 (setq text (format (_ " \\(has\\|have\\) left IRC%s")
231 (liece-handle-prepare-comment rest t))
232 match (format "^%s%s.*%s$"
233 (if liece-display-time
234 liece-time-prefix-regexp "")
235 (regexp-quote liece-change-prefix)
237 default (format (_ "%s%s has left IRC%s\n")
238 liece-change-prefix prefix
239 (liece-handle-prepare-comment rest)))
240 (liece-replace (append (liece-pick-buffer chnls)
241 liece-D-buffer liece-O-buffer)
243 (format (_ ", %s have left IRC%s")
244 prefix (liece-handle-prepare-comment rest))))
246 (liece-insert-change (append (liece-pick-buffer chnls)
247 liece-D-buffer liece-O-buffer)
248 (format (_ "%s has left IRC%s\n")
249 (liece-handle-prepare-comment rest)))))
250 (liece-nick-change prefix nil)))
252 (defun* liece-handle-topic-message (prefix rest)
253 (multiple-value-bind (chnl topic) (liece-split-line rest)
254 (setq chnl (liece-channel-virtual chnl)
256 (liece-channel-set-topic topic chnl)
257 (if (liece-handle-check-changes-ignored)
258 (return-from liece-handle-topic-message))
259 (liece-insert-change (liece-pick-buffer chnl)
260 (format (_ "New topic on channel %s set by %s: %s\n")
262 (liece-insert-change (if (liece-nick-equal chnl liece-current-channel)
264 (append liece-D-buffer liece-O-buffer))
265 (format (_ "New topic on channel %s set by %s: %s\n")
267 (liece-set-channel-indicator)))
269 (defun* liece-handle-mode-message (prefix rest)
270 (if (liece-handle-check-changes-ignored)
271 (return-from liece-handle-mode-message))
272 (let ((chnl " ") (str "") mflag mflags marg margs val md chnlp)
273 (or (and (string-match "\\([^ ]*\\) +:?" rest)
274 (setq chnl (match-string 1 rest)
275 str (substring rest (match-end 0))
276 chnlp (liece-channel-p chnl)
277 str (if (= (aref str (1- (length str))) ? )
278 (substring str 0 -1) str)
279 chnl (liece-channel-virtual chnl)))
280 (and (string-match " +:" rest)
281 (setq str (substring rest (match-end 0))))
282 (return-from liece-handle-mode-message))
285 (when (string-match "\\([^ ]*\\) +" str)
286 (setq mflag (match-string 1 str)
287 marg (substring str (match-end 0))
288 mflags (string-to-char-list mflag))
289 (while (string-match "^\\([^ ]*\\) +" marg)
290 (setq margs (cons (match-string 1 marg) margs)
291 marg (substring marg (match-end 0))))
292 (or (string= marg "") (setq margs (cons marg margs)))
293 (while (setq md (pop mflags))
294 (cond ((eq ?- md) (setq val nil))
295 ((eq ?+ md) (setq val t))
296 ((eq ?o md) (liece-channel-set-operator chnl (pop margs) val))
297 ((eq ?v md) (liece-channel-set-voice chnl (pop margs) val))
298 ((eq ?b md) (liece-channel-set-ban chnl (pop margs) val))
299 ((eq ?e md) (liece-channel-set-exception chnl (pop margs) val))
300 ((eq ?I md) (liece-channel-set-invite chnl (pop margs) val))
301 (chnlp (liece-channel-set-mode val md chnl))
302 (t (liece-nick-set-mode val md chnl)))))
304 (liece-set-channel-indicator)
306 (liece-compress-changes
307 (let* ((text (concat (regexp-quote rest) "\n"))
308 (match (format (_ "^%s%sNew mode for %s set by %s: ")
309 (if liece-display-time
310 liece-time-prefix-regexp "")
311 (regexp-quote liece-change-prefix)
312 (regexp-quote chnl) (regexp-quote prefix)))
313 (default (format (_ "%sNew mode for %s set by %s: %s\n")
314 liece-change-prefix chnl prefix str)))
315 (liece-replace (liece-pick-buffer chnl)
316 match default text (concat ", " str "\n"))
317 (liece-replace (if (and liece-current-channel
319 chnl liece-current-channel))
321 (append liece-D-buffer liece-O-buffer))
322 match default text (concat ", " str "\n"))))
324 (liece-insert-change (liece-pick-buffer chnl)
325 (format (_ "New mode for %s set by %s: %s\n")
327 (liece-insert-change (if (and liece-current-channel
329 chnl liece-current-channel))
331 (append liece-D-buffer liece-O-buffer))
332 (format (_ "New mode for %s set by %s: %s\n")
333 chnl prefix str))))))
335 (defun* liece-handle-kick-message (prefix rest)
336 (if (/= 3 (length (setq rest (liece-split-line rest))))
337 (return-from liece-handle-kick-message))
338 (multiple-value-bind (chnl nick message) rest
339 (setq chnl (liece-channel-virtual chnl))
341 (if (liece-nick-equal nick liece-real-nickname)
344 (liece-pick-buffer chnl)
345 (format (_ "You were kicked off channel %s by %s (%s).\n")
346 chnl prefix message))
347 (liece-channel-part chnl))
348 (liece-nick-part nick chnl))
350 (if (liece-handle-check-changes-ignored)
351 (return-from liece-handle-kick-message))
354 (append (liece-pick-buffer chnl)
355 (if (liece-channel-equal chnl liece-current-channel)
357 (append liece-D-buffer liece-O-buffer)))
358 (format "%s has kicked %s out%s%s\n"
360 (if (string= (or liece-current-channel "") chnl)
362 (format " from channel %s" chnl))
365 (format " (%s)" message))))))
367 (defun* liece-handle-invite-message (prefix rest)
368 (or (string-match " +:" rest)
369 (return-from liece-handle-invite-message))
370 (and liece-beep-when-invited liece-beep-on-bells
372 (let ((chnl (liece-channel-virtual (substring rest (match-end 0)))))
373 (liece-insert-info (append liece-D-buffer liece-O-buffer)
374 (format "%s invites you to channel %s\n"
376 (setq liece-default-channel-candidate chnl)))
378 (defun* liece-handle-kill-message (prefix rest)
379 (or (string-match " +:" rest)
380 (return-from liece-handle-kill-message))
381 (let ((path (substring rest (match-end 0))))
382 (liece-insert-info (append liece-D-buffer liece-O-buffer)
383 (format "You were killed by %s. (Path: %s. RIP)\n"
385 (liece-clear-system))
387 (defun* liece-handle-join-message (prefix rest)
388 (let (flag (xnick prefix) (nick prefix) (chnl rest))
390 ((string-match "\007[ov]" chnl)
391 (setq flag (aref (match-string 0 chnl) 1)
392 chnl (substring rest 0 (match-beginning 0))))
393 ((string-match " +$" chnl)
394 (setq chnl (substring chnl 0 (match-beginning 0)))))
395 (setq chnl (liece-channel-virtual chnl))
397 (liece-nick-set-user-at-host nick liece-user-at-host)
399 (if (liece-nick-equal nick liece-real-nickname)
401 (and liece-gather-channel-modes
402 (not (liece-channel-modeless-p (liece-channel-real chnl)))
403 (liece-send "MODE %s " (liece-channel-real chnl)))
404 (liece-channel-join chnl))
405 (liece-nick-join nick chnl))
409 (liece-channel-set-operator chnl xnick t)
410 (setq xnick (concat "@" xnick)))
412 (liece-channel-set-voice chnl xnick t)
413 (setq xnick (concat "+" xnick))))
415 (if (liece-handle-check-changes-ignored)
416 (return-from liece-handle-join-message))
418 (when (and (liece-nick-member nick liece-current-chat-partners)
419 (get (intern nick liece-obarray) 'part))
420 (liece-insert-change (liece-pick-buffer nick)
421 (format (_ "%s has come back as (%s)\n")
422 nick liece-user-at-host))
423 (liece-nick-unmark-as-apart nick))
426 (liece-compress-changes
427 (let* ((text (format (_ " \\(has\\|have\\) joined channel %s")
428 (regexp-quote chnl)))
429 (match (format "^%s%s.*%s$"
430 (if liece-display-time
431 liece-time-prefix-regexp "")
432 (regexp-quote liece-change-prefix)
433 (regexp-quote text)))
434 (default (format (_ "%s%s (%s) has joined channel %s\n")
436 nick liece-user-at-host chnl)))
437 (liece-replace (liece-pick-buffer chnl)
439 (format (_ ", %s (%s) have joined channel %s")
440 nick liece-user-at-host chnl))
441 (liece-replace (if (and liece-current-channel
442 (liece-channel-equal chnl
443 liece-current-channel))
445 (append liece-D-buffer liece-O-buffer))
447 (format (_ ", %s (%s) have joined channel %s")
448 nick liece-user-at-host chnl))))
450 (liece-insert-change (liece-pick-buffer chnl)
451 (format (_ "%s (%s) has joined channel %s\n")
452 nick liece-user-at-host chnl))
453 (liece-insert-change (if (liece-channel-equal chnl
454 liece-current-channel)
456 (append liece-D-buffer liece-O-buffer))
457 (format (_ "%s (%s) has joined channel %s\n")
458 nick liece-user-at-host chnl))))))
460 (defun* liece-handle-part-message (prefix rest)
461 (multiple-value-bind (chnl comment text match default buf) (liece-split-line rest)
462 (setq chnl (liece-channel-virtual chnl)
463 comment (liece-handle-prepare-comment comment))
465 (if (liece-nick-equal prefix liece-real-nickname)
466 (liece-channel-part chnl)
467 (liece-nick-part prefix chnl))
469 (if (liece-handle-check-changes-ignored)
470 (return-from liece-handle-part-message))
472 (setq buf (append liece-D-buffer (liece-pick-buffer chnl)))
473 (unless (and liece-current-channel
474 (liece-channel-equal chnl liece-current-channel))
475 (setq buf (append buf liece-O-buffer)))
477 (liece-compress-changes
478 (setq text (format (_ " \\(has\\|have\\) left channel %s%s")
479 (regexp-quote chnl) (regexp-quote comment))
480 match (format "^%s%s.*%s$"
481 (if liece-display-time
482 liece-time-prefix-regexp "")
483 (regexp-quote liece-change-prefix)
485 default (format (_ "%s%s has left channel %s%s\n")
486 liece-change-prefix prefix chnl comment))
489 (format (_ ", %s have left channel %s%s")
490 prefix chnl comment)))
492 (liece-insert-change buf
493 (format (_ "%s has left channel %s%s\n")
494 prefix chnl comment))))))
496 (defun* liece-handle-silence-message (prefix rest)
497 (let* ((flag (aref rest 0)) (rest (substring rest 1)))
498 (liece-insert-info (append liece-D-buffer liece-O-buffer)
500 (if (eq flag ?-) "unsilenced" "silenced")))))
502 (provide 'liece-handle)
504 ;;; liece-handle.el ends here