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-funcall-ignore-errors (symbol-name function)
81 function prefix (car targets)
83 (riece-ignore-errors (symbol-name after-hook)
84 (run-hook-with-args-until-success
85 after-hook prefix (car targets) message))))
88 (defun riece-handle-ctcp-version-request (prefix target string)
89 (let* ((target-identity (riece-make-identity target riece-server-name))
90 (buffer (if (riece-channel-p target)
91 (riece-channel-buffer target-identity)))
92 (user (riece-prefix-nickname prefix)))
94 (format "NOTICE %s :\1VERSION %s\1\r\n" user (riece-extended-version)))
95 (riece-insert-change buffer (format "CTCP VERSION from %s\n" user))
97 (if (and riece-channel-buffer-mode
98 (not (eq buffer riece-channel-buffer)))
99 (list riece-dialogue-buffer riece-others-buffer)
100 riece-dialogue-buffer)
102 (riece-concat-server-name
103 (format "CTCP VERSION from %s (%s) to %s"
105 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
106 (riece-format-identity target-identity t)))
109 (defun riece-handle-ctcp-ping-request (prefix target string)
110 (let* ((target-identity (riece-make-identity target riece-server-name))
111 (buffer (if (riece-channel-p target)
112 (riece-channel-buffer target-identity)))
113 (user (riece-prefix-nickname prefix)))
116 (format "NOTICE %s :\1PING %s\1\r\n" user string)
117 (format "NOTICE %s :\1PING\1\r\n" user string)))
118 (riece-insert-change buffer (format "CTCP PING from %s\n" user))
120 (if (and riece-channel-buffer-mode
121 (not (eq buffer riece-channel-buffer)))
122 (list riece-dialogue-buffer riece-others-buffer)
123 riece-dialogue-buffer)
125 (riece-concat-server-name
126 (format "CTCP PING from %s (%s) to %s"
128 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
129 (riece-format-identity target-identity t)))
132 (defun riece-handle-ctcp-clientinfo-request (prefix target string)
133 (let* ((target-identity (riece-make-identity target riece-server-name))
134 (buffer (if (riece-channel-p target)
135 (riece-channel-buffer target-identity)))
136 (user (riece-prefix-nickname prefix)))
138 (format "NOTICE %s :\1CLIENTINFO %s\1\r\n"
143 (let ((case-fold-search t))
144 (if (and (fboundp atom)
146 "riece-handle-ctcp-\\(.+\\)-request"
149 (cons (match-string 1 (symbol-name atom))
151 (mapconcat #'upcase (append messages
152 riece-ctcp-additional-clientinfo)
154 (riece-insert-change buffer (format "CTCP CLIENTINFO from %s\n" user))
156 (if (and riece-channel-buffer-mode
157 (not (eq buffer riece-channel-buffer)))
158 (list riece-dialogue-buffer riece-others-buffer)
159 riece-dialogue-buffer)
161 (riece-concat-server-name
162 (format "CTCP CLIENTINFO from %s (%s) to %s"
164 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
165 (riece-format-identity target-identity t)))
168 (defun riece-handle-ctcp-action-request (prefix target string)
169 (let ((buffer (if (riece-channel-p target)
170 (riece-channel-buffer (riece-make-identity
171 target riece-server-name))))
172 (user (riece-prefix-nickname prefix)))
173 (riece-insert buffer (concat riece-ctcp-action-prefix user " " string
176 (if (and riece-channel-buffer-mode
177 (not (eq buffer riece-channel-buffer)))
178 (list riece-dialogue-buffer riece-others-buffer)
179 riece-dialogue-buffer)
180 (concat (riece-concat-server-name (concat riece-ctcp-action-prefix user
181 " " string)) "\n"))))
183 (defun riece-handle-ctcp-time-request (prefix target string)
184 (let* ((target-identity (riece-make-identity target riece-server-name))
185 (buffer (if (riece-channel-p target)
186 (riece-channel-buffer target-identity)))
187 (user (riece-prefix-nickname prefix))
188 (time (format-time-string "%c")))
190 (format "NOTICE %s :\1TIME %s\1\r\n" user time))
191 (riece-insert-change buffer (format "CTCP TIME from %s\n" user))
193 (if (and riece-channel-buffer-mode
194 (not (eq buffer riece-channel-buffer)))
195 (list riece-dialogue-buffer riece-others-buffer)
196 riece-dialogue-buffer)
198 (riece-concat-server-name
199 (format "CTCP TIME from %s (%s) to %s"
201 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
202 (riece-format-identity target-identity t)))
205 (defun riece-handle-ctcp-response (prefix string)
206 (when (and riece-ctcp-enabled prefix string
207 (riece-prefix-nickname prefix))
208 (let* ((parameters (riece-split-parameters string))
209 (targets (split-string (car parameters) ","))
210 (message (nth 1 parameters)))
211 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
212 (let ((response (downcase (match-string 1 message))))
213 (if (match-beginning 2)
214 (setq message (substring (match-string 2 message) 1)))
216 (intern (concat "riece-ctcp-" response "-response-hook")))
217 (function (intern-soft (concat "riece-handle-ctcp-"
218 response "-response")))
220 (intern (concat "riece-ctcp-after-" response
222 (unless (riece-ignore-errors (symbol-name hook)
223 (run-hook-with-args-until-success
224 hook prefix (car targets) message))
226 (riece-funcall-ignore-errors
227 (symbol-name function)
228 function prefix (car targets) message))
229 (riece-ignore-errors (symbol-name after-hook)
230 (run-hook-with-args-until-success
231 after-hook prefix (car targets) message))))
234 (defun riece-handle-ctcp-version-response (prefix target string)
236 (list riece-dialogue-buffer riece-others-buffer)
238 (riece-concat-server-name
239 (format "CTCP VERSION for %s (%s) = %s"
240 (riece-prefix-nickname prefix)
241 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
245 (defun riece-handle-ctcp-ping-response (prefix target string)
246 (let* ((now (current-time))
247 (elapsed (+ (* 65536 (- (car now) (car riece-ctcp-ping-time)))
248 (- (nth 1 now) (nth 1 riece-ctcp-ping-time)))))
250 (list riece-dialogue-buffer riece-others-buffer)
252 (riece-concat-server-name
253 (format "CTCP PING for %s (%s) = %d sec"
254 (riece-prefix-nickname prefix)
255 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
259 (defun riece-handle-ctcp-clientinfo-response (prefix target string)
261 (list riece-dialogue-buffer riece-others-buffer)
263 (riece-concat-server-name
264 (format "CTCP CLIENTINFO for %s (%s) = %s"
265 (riece-prefix-nickname prefix)
266 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
270 (defun riece-handle-ctcp-time-response (prefix target string)
272 (list riece-dialogue-buffer riece-others-buffer)
274 (riece-concat-server-name
275 (format "CTCP TIME for %s (%s) = %s"
276 (riece-prefix-nickname prefix)
277 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
281 (defun riece-command-ctcp-version (target)
283 (list (riece-completing-read-identity
285 (riece-get-identities-on-server (riece-current-server-name)))))
286 (riece-send-string (format "PRIVMSG %s :\1VERSION\1\r\n"
287 (riece-identity-prefix target))))
289 (defun riece-command-ctcp-ping (target)
291 (list (riece-completing-read-identity
293 (riece-get-identities-on-server (riece-current-server-name)))))
294 (riece-send-string (format "PRIVMSG %s :\1PING\1\r\n"
295 (riece-identity-prefix target)))
296 (setq riece-ctcp-ping-time (current-time)))
298 (defun riece-command-ctcp-clientinfo (target)
300 (list (riece-completing-read-identity
302 (riece-get-identities-on-server (riece-current-server-name)))))
303 (riece-send-string (format "PRIVMSG %s :\1CLIENTINFO\1\r\n"
304 (riece-identity-prefix target))))
306 (defun riece-command-ctcp-action (target action)
308 (list (if current-prefix-arg
309 (riece-completing-read-identity
311 (riece-get-identities-on-server (riece-current-server-name)))
312 riece-current-channel)
315 (setq message (buffer-substring (point)
316 (progn (end-of-line) (point))))
317 (if (equal message "")
318 (read-string "Action: ")
319 (prog1 (read-from-minibuffer "Action: " (cons message 0))
320 (let ((next-line-add-newlines t))
322 (if (equal action "")
324 (riece-send-string (format "PRIVMSG %s :\1ACTION %s\1\r\n"
325 (riece-identity-prefix target)
327 (let ((buffer (riece-channel-buffer target)))
330 (concat riece-ctcp-action-prefix
331 (riece-identity-prefix (riece-current-nickname)) " " action "\n"))
333 (if (and riece-channel-buffer-mode
334 (not (eq buffer riece-channel-buffer)))
335 (list riece-dialogue-buffer riece-others-buffer)
336 riece-dialogue-buffer)
338 (riece-with-server-buffer (riece-identity-server target)
339 (riece-concat-server-name
340 (concat riece-ctcp-action-prefix
341 (riece-identity-prefix (riece-current-nickname)) " " action
342 " (in " (riece-format-identity target t) ")")))
345 (defun riece-command-ctcp-time (target)
347 (list (riece-completing-read-identity
349 (riece-get-identities-on-server (riece-current-server-name)))))
350 (riece-send-string (format "PRIVMSG %s :\1TIME\1\r\n"
351 (riece-identity-prefix target))))
353 (defun riece-ctcp-requires ()
354 (if (memq 'riece-highlight riece-addons)
357 (defun riece-ctcp-insinuate ()
358 (add-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
359 (add-hook 'riece-notice-hook 'riece-handle-ctcp-response)
360 (if (memq 'riece-highlight riece-addons)
361 (setq riece-dialogue-font-lock-keywords
362 (cons (list (concat "^" riece-time-prefix-regexp "\\("
363 (regexp-quote riece-ctcp-action-prefix)
365 1 riece-ctcp-action-face t t)
366 riece-dialogue-font-lock-keywords))))
368 (defun riece-ctcp-enable ()
369 (define-key riece-dialogue-mode-map "\C-cv" 'riece-command-ctcp-version)
370 (define-key riece-dialogue-mode-map "\C-cp" 'riece-command-ctcp-ping)
371 (define-key riece-dialogue-mode-map "\C-ca" 'riece-command-ctcp-action)
372 (define-key riece-dialogue-mode-map "\C-cc" 'riece-command-ctcp-clientinfo)
373 (define-key riece-dialogue-mode-map "\C-ct" 'riece-command-ctcp-time)
374 (setq riece-ctcp-enabled t))
376 (defun riece-ctcp-disable ()
377 (define-key riece-dialogue-mode-map "\C-cv" nil)
378 (define-key riece-dialogue-mode-map "\C-cp" nil)
379 (define-key riece-dialogue-mode-map "\C-ca" nil)
380 (define-key riece-dialogue-mode-map "\C-cc" nil)
381 (define-key riece-dialogue-mode-map "\C-ct" nil)
382 (setq riece-ctcp-enabled nil))
384 (provide 'riece-ctcp)
386 ;;; riece-ctcp.el ends here