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-ignore-errors (symbol-name hook)
77 (run-hook-with-args-until-success
78 hook prefix (car targets) message))
80 (riece-ignore-errors (symbol-name function)
81 (funcall function prefix (car targets) message)))
82 (riece-ignore-errors (symbol-name after-hook)
83 (run-hook-with-args-until-success
84 after-hook prefix (car targets) message))))
87 (defun riece-handle-ctcp-version-request (prefix target string)
88 (let* ((target-identity (riece-make-identity target riece-server-name))
89 (buffer (if (riece-channel-p target)
90 (riece-channel-buffer target-identity)))
91 (user (riece-prefix-nickname prefix)))
93 (format "NOTICE %s :\1VERSION %s\1\r\n" user (riece-extended-version)))
94 (riece-insert-change buffer (format "CTCP VERSION from %s\n" user))
96 (if (and riece-channel-buffer-mode
97 (not (eq buffer riece-channel-buffer)))
98 (list riece-dialogue-buffer riece-others-buffer)
99 riece-dialogue-buffer)
101 (riece-concat-server-name
102 (format "CTCP VERSION from %s (%s) to %s"
104 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
105 (riece-format-identity target-identity t)))
108 (defun riece-handle-ctcp-ping-request (prefix target string)
109 (let* ((target-identity (riece-make-identity target riece-server-name))
110 (buffer (if (riece-channel-p target)
111 (riece-channel-buffer target-identity)))
112 (user (riece-prefix-nickname prefix)))
115 (format "NOTICE %s :\1PING %s\1\r\n" user string)
116 (format "NOTICE %s :\1PING\1\r\n" user string)))
117 (riece-insert-change buffer (format "CTCP PING from %s\n" user))
119 (if (and riece-channel-buffer-mode
120 (not (eq buffer riece-channel-buffer)))
121 (list riece-dialogue-buffer riece-others-buffer)
122 riece-dialogue-buffer)
124 (riece-concat-server-name
125 (format "CTCP PING from %s (%s) to %s"
127 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
128 (riece-format-identity target-identity t)))
131 (defun riece-handle-ctcp-clientinfo-request (prefix target string)
132 (let* ((target-identity (riece-make-identity target riece-server-name))
133 (buffer (if (riece-channel-p target)
134 (riece-channel-buffer target-identity)))
135 (user (riece-prefix-nickname prefix)))
137 (format "NOTICE %s :\1CLIENTINFO %s\1\r\n"
142 (let ((case-fold-search t))
143 (if (and (fboundp atom)
145 "riece-handle-ctcp-\\(.+\\)-request"
148 (cons (match-string 1 (symbol-name atom))
150 (mapconcat #'upcase (append messages
151 riece-ctcp-additional-clientinfo)
153 (riece-insert-change buffer (format "CTCP CLIENTINFO from %s\n" user))
155 (if (and riece-channel-buffer-mode
156 (not (eq buffer riece-channel-buffer)))
157 (list riece-dialogue-buffer riece-others-buffer)
158 riece-dialogue-buffer)
160 (riece-concat-server-name
161 (format "CTCP CLIENTINFO from %s (%s) to %s"
163 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
164 (riece-format-identity target-identity t)))
167 (defun riece-handle-ctcp-action-request (prefix target string)
168 (let ((buffer (if (riece-channel-p target)
169 (riece-channel-buffer (riece-make-identity
170 target riece-server-name))))
171 (user (riece-prefix-nickname prefix)))
172 (riece-insert buffer (concat riece-ctcp-action-prefix user " " string
175 (if (and riece-channel-buffer-mode
176 (not (eq buffer riece-channel-buffer)))
177 (list riece-dialogue-buffer riece-others-buffer)
178 riece-dialogue-buffer)
179 (concat (riece-concat-server-name (concat riece-ctcp-action-prefix user
180 " " string)) "\n"))))
182 (defun riece-handle-ctcp-time-request (prefix target string)
183 (let* ((target-identity (riece-make-identity target riece-server-name))
184 (buffer (if (riece-channel-p target)
185 (riece-channel-buffer target-identity)))
186 (user (riece-prefix-nickname prefix))
187 (time (format-time-string "%c")))
189 (format "NOTICE %s :\1TIME %s\1\r\n" user time))
190 (riece-insert-change buffer (format "CTCP TIME from %s\n" user))
192 (if (and riece-channel-buffer-mode
193 (not (eq buffer riece-channel-buffer)))
194 (list riece-dialogue-buffer riece-others-buffer)
195 riece-dialogue-buffer)
197 (riece-concat-server-name
198 (format "CTCP TIME from %s (%s) to %s"
200 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
201 (riece-format-identity target-identity t)))
204 (defun riece-handle-ctcp-response (prefix string)
205 (when (and riece-ctcp-enabled prefix string
206 (riece-prefix-nickname prefix))
207 (let* ((parameters (riece-split-parameters string))
208 (targets (split-string (car parameters) ","))
209 (message (nth 1 parameters)))
210 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
211 (let ((response (downcase (match-string 1 message))))
212 (if (match-beginning 2)
213 (setq message (substring (match-string 2 message) 1)))
215 (intern (concat "riece-ctcp-" response "-response-hook")))
216 (function (intern-soft (concat "riece-handle-ctcp-"
217 response "-response")))
219 (intern (concat "riece-ctcp-after-" response
221 (unless (riece-ignore-errors (symbol-name hook)
222 (run-hook-with-args-until-success
223 hook prefix (car targets) message))
225 (riece-ignore-errors (symbol-name function)
226 (funcall function prefix (car targets) message)))
227 (riece-ignore-errors (symbol-name after-hook)
228 (run-hook-with-args-until-success
229 after-hook prefix (car targets) message))))
232 (defun riece-handle-ctcp-version-response (prefix target string)
234 (list riece-dialogue-buffer riece-others-buffer)
236 (riece-concat-server-name
237 (format "CTCP VERSION for %s (%s) = %s"
238 (riece-prefix-nickname prefix)
239 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
243 (defun riece-handle-ctcp-ping-response (prefix target string)
244 (let* ((now (current-time))
245 (elapsed (+ (* 65536 (- (car now) (car riece-ctcp-ping-time)))
246 (- (nth 1 now) (nth 1 riece-ctcp-ping-time)))))
248 (list riece-dialogue-buffer riece-others-buffer)
250 (riece-concat-server-name
251 (format "CTCP PING for %s (%s) = %d sec"
252 (riece-prefix-nickname prefix)
253 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
257 (defun riece-handle-ctcp-clientinfo-response (prefix target string)
259 (list riece-dialogue-buffer riece-others-buffer)
261 (riece-concat-server-name
262 (format "CTCP CLIENTINFO for %s (%s) = %s"
263 (riece-prefix-nickname prefix)
264 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
268 (defun riece-handle-ctcp-time-response (prefix target string)
270 (list riece-dialogue-buffer riece-others-buffer)
272 (riece-concat-server-name
273 (format "CTCP TIME for %s (%s) = %s"
274 (riece-prefix-nickname prefix)
275 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
279 (defun riece-command-ctcp-version (target)
281 (list (riece-completing-read-identity
283 (riece-get-identities-on-server (riece-current-server-name)))))
284 (riece-send-string (format "PRIVMSG %s :\1VERSION\1\r\n"
285 (riece-identity-prefix target))))
287 (defun riece-command-ctcp-ping (target)
289 (list (riece-completing-read-identity
291 (riece-get-identities-on-server (riece-current-server-name)))))
292 (riece-send-string (format "PRIVMSG %s :\1PING\1\r\n"
293 (riece-identity-prefix target)))
294 (setq riece-ctcp-ping-time (current-time)))
296 (defun riece-command-ctcp-clientinfo (target)
298 (list (riece-completing-read-identity
300 (riece-get-identities-on-server (riece-current-server-name)))))
301 (riece-send-string (format "PRIVMSG %s :\1CLIENTINFO\1\r\n"
302 (riece-identity-prefix target))))
304 (defun riece-command-ctcp-action (target action)
306 (list (if current-prefix-arg
307 (riece-completing-read-identity
309 (riece-get-identities-on-server (riece-current-server-name)))
310 riece-current-channel)
313 (setq message (buffer-substring (point)
314 (progn (end-of-line) (point))))
315 (if (equal message "")
316 (read-string "Action: ")
317 (prog1 (read-from-minibuffer "Action: " (cons message 0))
318 (let ((next-line-add-newlines t))
320 (if (equal action "")
322 (riece-send-string (format "PRIVMSG %s :\1ACTION %s\1\r\n"
323 (riece-identity-prefix target)
325 (let ((buffer (riece-channel-buffer target)))
328 (concat riece-ctcp-action-prefix
329 (riece-identity-prefix (riece-current-nickname)) " " action "\n"))
331 (if (and riece-channel-buffer-mode
332 (not (eq buffer riece-channel-buffer)))
333 (list riece-dialogue-buffer riece-others-buffer)
334 riece-dialogue-buffer)
336 (riece-with-server-buffer (riece-identity-server target)
337 (riece-concat-server-name
338 (concat riece-ctcp-action-prefix
339 (riece-identity-prefix (riece-current-nickname)) " " action
340 " (in " (riece-format-identity target t) ")")))
343 (defun riece-command-ctcp-time (target)
345 (list (riece-completing-read-identity
347 (riece-get-identities-on-server (riece-current-server-name)))))
348 (riece-send-string (format "PRIVMSG %s :\1TIME\1\r\n"
349 (riece-identity-prefix target))))
351 (defun riece-ctcp-requires ()
352 (if (memq 'riece-highlight riece-addons)
355 (defun riece-ctcp-insinuate ()
356 (add-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
357 (add-hook 'riece-notice-hook 'riece-handle-ctcp-response)
358 (if (memq 'riece-highlight riece-addons)
359 (setq riece-dialogue-font-lock-keywords
360 (cons (list (concat "^" riece-time-prefix-regexp "\\("
361 (regexp-quote riece-ctcp-action-prefix)
363 1 riece-ctcp-action-face t t)
364 riece-dialogue-font-lock-keywords))))
366 (defun riece-ctcp-enable ()
367 (define-key riece-dialogue-mode-map "\C-cv" 'riece-command-ctcp-version)
368 (define-key riece-dialogue-mode-map "\C-cp" 'riece-command-ctcp-ping)
369 (define-key riece-dialogue-mode-map "\C-ca" 'riece-command-ctcp-action)
370 (define-key riece-dialogue-mode-map "\C-cc" 'riece-command-ctcp-clientinfo)
371 (define-key riece-dialogue-mode-map "\C-ct" 'riece-command-ctcp-time)
372 (setq riece-ctcp-enabled t))
374 (defun riece-ctcp-disable ()
375 (define-key riece-dialogue-mode-map "\C-cv" nil)
376 (define-key riece-dialogue-mode-map "\C-cp" nil)
377 (define-key riece-dialogue-mode-map "\C-ca" nil)
378 (define-key riece-dialogue-mode-map "\C-cc" nil)
379 (define-key riece-dialogue-mode-map "\C-ct" nil)
380 (setq riece-ctcp-enabled nil))
382 (provide 'riece-ctcp)
384 ;;; riece-ctcp.el ends here