1 ;;; riece-ctcp.el --- CTCP add-on
2 ;; Copyright (C) 1998-2003 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: IRC, riece
8 ;; This file is part of Riece.
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)
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.
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.
27 (require 'riece-version)
29 (require 'riece-highlight)
30 (require 'riece-display)
31 (require 'riece-debug)
33 (defface riece-ctcp-action-face
36 (:foreground "PaleGreen" :italic t))
39 (:foreground "ForestGreen" :italic t))
42 "Face used for displaying \"*** Action:\" line"
43 :group 'riece-highlight-faces)
44 (defvar riece-ctcp-action-face 'riece-ctcp-action-face)
46 (defconst riece-ctcp-action-prefix "*** Action: ")
48 (defvar riece-ctcp-ping-time nil)
49 (defvar riece-ctcp-additional-clientinfo nil)
51 (defvar riece-dialogue-mode-map)
53 (defvar riece-ctcp-enabled nil)
55 (defconst riece-ctcp-description
56 "CTCP (Client To Client Protocol) support")
58 (defun riece-handle-ctcp-request (prefix string)
59 (when (and riece-ctcp-enabled prefix string
60 (riece-prefix-nickname prefix))
61 (let* ((parameters (riece-split-parameters string))
62 (targets (split-string (car parameters) ","))
63 (message (nth 1 parameters)))
64 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
65 (let ((request (downcase (match-string 1 message))))
66 (if (match-beginning 2)
67 (setq message (substring (match-string 2 message) 1)))
69 (intern (concat "riece-ctcp-" request "-request-hook")))
71 (intern-soft (concat "riece-handle-ctcp-" request
74 (intern (concat "riece-ctcp-after-" request
76 (unless (riece-funcall-ignore-errors
78 #'run-hook-with-args-until-success
79 hook prefix (car targets) message)
81 (riece-funcall-ignore-errors (symbol-name function)
82 function prefix (car targets)
84 (riece-funcall-ignore-errors (symbol-name after-hook)
85 #'run-hook-with-args-until-success
86 after-hook prefix (car targets)
90 (defun riece-handle-ctcp-version-request (prefix target string)
91 (let* ((target-identity (riece-make-identity target riece-server-name))
92 (buffer (if (riece-channel-p target)
93 (riece-channel-buffer target-identity)))
94 (user (riece-prefix-nickname prefix)))
96 (format "NOTICE %s :\1VERSION %s\1\r\n" user (riece-extended-version)))
97 (riece-insert-change buffer (format "CTCP VERSION from %s\n" user))
99 (if (and riece-channel-buffer-mode
100 (not (eq buffer riece-channel-buffer)))
101 (list riece-dialogue-buffer riece-others-buffer)
102 riece-dialogue-buffer)
104 (riece-concat-server-name
105 (format "CTCP VERSION from %s (%s) to %s"
107 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
108 (riece-format-identity target-identity t)))
111 (defun riece-handle-ctcp-ping-request (prefix target string)
112 (let* ((target-identity (riece-make-identity target riece-server-name))
113 (buffer (if (riece-channel-p target)
114 (riece-channel-buffer target-identity)))
115 (user (riece-prefix-nickname prefix)))
118 (format "NOTICE %s :\1PING %s\1\r\n" user string)
119 (format "NOTICE %s :\1PING\1\r\n" user string)))
120 (riece-insert-change buffer (format "CTCP PING from %s\n" user))
122 (if (and riece-channel-buffer-mode
123 (not (eq buffer riece-channel-buffer)))
124 (list riece-dialogue-buffer riece-others-buffer)
125 riece-dialogue-buffer)
127 (riece-concat-server-name
128 (format "CTCP PING from %s (%s) to %s"
130 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
131 (riece-format-identity target-identity t)))
134 (defun riece-handle-ctcp-clientinfo-request (prefix target string)
135 (let* ((target-identity (riece-make-identity target riece-server-name))
136 (buffer (if (riece-channel-p target)
137 (riece-channel-buffer target-identity)))
138 (user (riece-prefix-nickname prefix)))
140 (format "NOTICE %s :\1CLIENTINFO %s\1\r\n"
145 (let ((case-fold-search t))
146 (if (and (fboundp atom)
148 "riece-handle-ctcp-\\(.+\\)-request"
151 (cons (match-string 1 (symbol-name atom))
153 (mapconcat #'upcase (append messages
154 riece-ctcp-additional-clientinfo)
156 (riece-insert-change buffer (format "CTCP CLIENTINFO from %s\n" user))
158 (if (and riece-channel-buffer-mode
159 (not (eq buffer riece-channel-buffer)))
160 (list riece-dialogue-buffer riece-others-buffer)
161 riece-dialogue-buffer)
163 (riece-concat-server-name
164 (format "CTCP CLIENTINFO from %s (%s) to %s"
166 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
167 (riece-format-identity target-identity t)))
170 (defun riece-handle-ctcp-action-request (prefix target string)
171 (let ((buffer (if (riece-channel-p target)
172 (riece-channel-buffer (riece-make-identity
173 target riece-server-name))))
174 (user (riece-prefix-nickname prefix)))
175 (riece-insert buffer (concat riece-ctcp-action-prefix
176 (riece-format-identity
177 (riece-make-identity user riece-server-name)
182 (if (and riece-channel-buffer-mode
183 (not (eq buffer riece-channel-buffer)))
184 (list riece-dialogue-buffer riece-others-buffer)
185 riece-dialogue-buffer)
186 (concat (riece-concat-server-name
187 (concat riece-ctcp-action-prefix
188 (riece-format-identity
189 (riece-make-identity target riece-server-name)
192 (riece-format-identity
193 (riece-make-identity user riece-server-name)
195 " " string)) "\n"))))
197 (defun riece-handle-ctcp-time-request (prefix target string)
198 (let* ((target-identity (riece-make-identity target riece-server-name))
199 (buffer (if (riece-channel-p target)
200 (riece-channel-buffer target-identity)))
201 (user (riece-prefix-nickname prefix))
202 (time (format-time-string "%c")))
204 (format "NOTICE %s :\1TIME %s\1\r\n" user time))
205 (riece-insert-change buffer (format "CTCP TIME from %s\n" user))
207 (if (and riece-channel-buffer-mode
208 (not (eq buffer riece-channel-buffer)))
209 (list riece-dialogue-buffer riece-others-buffer)
210 riece-dialogue-buffer)
212 (riece-concat-server-name
213 (format "CTCP TIME from %s (%s) to %s"
215 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
216 (riece-format-identity target-identity t)))
219 (defun riece-handle-ctcp-response (prefix string)
220 (when (and riece-ctcp-enabled prefix string
221 (riece-prefix-nickname prefix))
222 (let* ((parameters (riece-split-parameters string))
223 (targets (split-string (car parameters) ","))
224 (message (nth 1 parameters)))
225 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
226 (let ((response (downcase (match-string 1 message))))
227 (if (match-beginning 2)
228 (setq message (substring (match-string 2 message) 1)))
230 (intern (concat "riece-ctcp-" response "-response-hook")))
231 (function (intern-soft (concat "riece-handle-ctcp-"
232 response "-response")))
234 (intern (concat "riece-ctcp-after-" response
236 (unless (riece-funcall-ignore-errors
238 #'run-hook-with-args-until-success
239 hook prefix (car targets) message)
241 (riece-funcall-ignore-errors
242 (symbol-name function)
243 function prefix (car targets) message))
244 (riece-funcall-ignore-errors (symbol-name after-hook)
245 #'run-hook-with-args-until-success
246 after-hook prefix (car targets)
250 (defun riece-handle-ctcp-version-response (prefix target string)
252 (list riece-dialogue-buffer riece-others-buffer)
254 (riece-concat-server-name
255 (format "CTCP VERSION for %s (%s) = %s"
256 (riece-prefix-nickname prefix)
257 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
261 (defun riece-handle-ctcp-ping-response (prefix target string)
262 (let* ((now (current-time))
263 (elapsed (+ (* 65536 (- (car now) (car riece-ctcp-ping-time)))
264 (- (nth 1 now) (nth 1 riece-ctcp-ping-time)))))
266 (list riece-dialogue-buffer riece-others-buffer)
268 (riece-concat-server-name
269 (format "CTCP PING for %s (%s) = %d sec"
270 (riece-prefix-nickname prefix)
271 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
275 (defun riece-handle-ctcp-clientinfo-response (prefix target string)
277 (list riece-dialogue-buffer riece-others-buffer)
279 (riece-concat-server-name
280 (format "CTCP CLIENTINFO for %s (%s) = %s"
281 (riece-prefix-nickname prefix)
282 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
286 (defun riece-handle-ctcp-time-response (prefix target string)
288 (list riece-dialogue-buffer riece-others-buffer)
290 (riece-concat-server-name
291 (format "CTCP TIME for %s (%s) = %s"
292 (riece-prefix-nickname prefix)
293 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
297 (defun riece-command-ctcp-version (target)
299 (list (riece-completing-read-identity
301 (riece-get-identities-on-server (riece-current-server-name)))))
302 (riece-send-string (format "PRIVMSG %s :\1VERSION\1\r\n"
303 (riece-identity-prefix target))))
305 (defun riece-command-ctcp-ping (target)
307 (list (riece-completing-read-identity
309 (riece-get-identities-on-server (riece-current-server-name)))))
310 (riece-send-string (format "PRIVMSG %s :\1PING\1\r\n"
311 (riece-identity-prefix target)))
312 (setq riece-ctcp-ping-time (current-time)))
314 (defun riece-command-ctcp-clientinfo (target)
316 (list (riece-completing-read-identity
318 (riece-get-identities-on-server (riece-current-server-name)))))
319 (riece-send-string (format "PRIVMSG %s :\1CLIENTINFO\1\r\n"
320 (riece-identity-prefix target))))
322 (defun riece-command-ctcp-action (target action)
324 (list (if current-prefix-arg
325 (riece-completing-read-identity
327 (riece-get-identities-on-server (riece-current-server-name)))
328 riece-current-channel)
331 (setq message (buffer-substring (point)
332 (progn (end-of-line) (point))))
333 (if (equal message "")
334 (read-string "Action: ")
335 (prog1 (read-from-minibuffer "Action: " (cons message 0))
336 (let ((next-line-add-newlines t))
338 (if (equal action "")
340 (riece-send-string (format "PRIVMSG %s :\1ACTION %s\1\r\n"
341 (riece-identity-prefix target)
343 (let ((buffer (riece-channel-buffer target)))
346 (concat riece-ctcp-action-prefix
347 (riece-identity-prefix (riece-current-nickname)) " " action "\n"))
349 (if (and riece-channel-buffer-mode
350 (not (eq buffer riece-channel-buffer)))
351 (list riece-dialogue-buffer riece-others-buffer)
352 riece-dialogue-buffer)
354 (riece-with-server-buffer (riece-identity-server target)
355 (riece-concat-server-name
356 (concat riece-ctcp-action-prefix
357 (riece-format-identity target t) ": "
358 (riece-identity-prefix (riece-current-nickname)) " " action)))
361 (defun riece-command-ctcp-time (target)
363 (list (riece-completing-read-identity
365 (riece-get-identities-on-server (riece-current-server-name)))))
366 (riece-send-string (format "PRIVMSG %s :\1TIME\1\r\n"
367 (riece-identity-prefix target))))
369 (defun riece-ctcp-requires ()
370 (if (memq 'riece-highlight riece-addons)
373 (defun riece-ctcp-insinuate ()
374 (add-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
375 (add-hook 'riece-notice-hook 'riece-handle-ctcp-response)
376 (if (memq 'riece-highlight riece-addons)
377 (setq riece-dialogue-font-lock-keywords
378 (cons (list (concat "^" riece-time-prefix-regexp "\\("
379 (regexp-quote riece-ctcp-action-prefix)
381 1 riece-ctcp-action-face t t)
382 riece-dialogue-font-lock-keywords))))
384 (defun riece-ctcp-enable ()
385 (define-key riece-dialogue-mode-map "\C-cv" 'riece-command-ctcp-version)
386 (define-key riece-dialogue-mode-map "\C-cp" 'riece-command-ctcp-ping)
387 (define-key riece-dialogue-mode-map "\C-ca" 'riece-command-ctcp-action)
388 (define-key riece-dialogue-mode-map "\C-cc" 'riece-command-ctcp-clientinfo)
389 (define-key riece-dialogue-mode-map "\C-ct" 'riece-command-ctcp-time)
390 (setq riece-ctcp-enabled t))
392 (defun riece-ctcp-disable ()
393 (define-key riece-dialogue-mode-map "\C-cv" nil)
394 (define-key riece-dialogue-mode-map "\C-cp" nil)
395 (define-key riece-dialogue-mode-map "\C-ca" nil)
396 (define-key riece-dialogue-mode-map "\C-cc" nil)
397 (define-key riece-dialogue-mode-map "\C-ct" nil)
398 (setq riece-ctcp-enabled nil))
400 (provide 'riece-ctcp)
402 ;;; riece-ctcp.el ends here