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)
32 (defface riece-ctcp-action-face
35 (:foreground "PaleGreen" :italic t))
38 (:foreground "ForestGreen" :italic t))
41 "Face used for displaying \"*** Action:\" line"
42 :group 'riece-highlight-faces)
43 (defvar riece-ctcp-action-face 'riece-ctcp-action-face)
45 (defconst riece-ctcp-action-prefix "*** Action: ")
47 (defvar riece-ctcp-ping-time nil)
48 (defvar riece-ctcp-additional-clientinfo nil)
50 (defvar riece-dialogue-mode-map)
52 (defvar riece-ctcp-enabled nil)
54 (defun riece-handle-ctcp-request (prefix string)
55 (when (and riece-ctcp-enabled prefix string
56 (riece-prefix-nickname prefix))
57 (let* ((parameters (riece-split-parameters string))
58 (targets (split-string (car parameters) ","))
59 (message (nth 1 parameters)))
60 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
61 (let ((request (downcase (match-string 1 message))))
62 (if (match-beginning 2)
63 (setq message (substring (match-string 2 message) 1)))
65 (intern (concat "riece-ctcp-" request "-request-hook")))
67 (intern-soft (concat "riece-handle-ctcp-" request
70 (intern (concat "riece-ctcp-after-" request
72 (unless (condition-case error
73 (run-hook-with-args-until-success
74 hook prefix (car targets) message)
77 (message "Error in `%S': %S" hook error))
81 (funcall function prefix (car targets) message)
84 (message "Error in `%S': %S"
87 (run-hook-with-args-until-success
88 after-hook prefix (car targets) message)
91 (message "Error in `%S': %S"
95 (defun riece-handle-ctcp-version-request (prefix target string)
96 (let* ((target-identity (riece-make-identity target riece-server-name))
97 (buffer (if (riece-channel-p target)
98 (riece-channel-buffer target-identity)))
99 (user (riece-prefix-nickname prefix)))
101 (format "NOTICE %s :\1VERSION %s\1\r\n" user (riece-extended-version)))
102 (riece-insert-change buffer (format "CTCP VERSION from %s\n" user))
104 (if (and riece-channel-buffer-mode
105 (not (eq buffer riece-channel-buffer)))
106 (list riece-dialogue-buffer riece-others-buffer)
107 riece-dialogue-buffer)
109 (riece-concat-server-name
110 (format "CTCP VERSION from %s (%s) to %s"
112 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
113 (riece-format-identity target-identity t)))
116 (defun riece-handle-ctcp-ping-request (prefix target string)
117 (let* ((target-identity (riece-make-identity target riece-server-name))
118 (buffer (if (riece-channel-p target)
119 (riece-channel-buffer target-identity)))
120 (user (riece-prefix-nickname prefix)))
123 (format "NOTICE %s :\1PING %s\1\r\n" user string)
124 (format "NOTICE %s :\1PING\1\r\n" user string)))
125 (riece-insert-change buffer (format "CTCP PING from %s\n" user))
127 (if (and riece-channel-buffer-mode
128 (not (eq buffer riece-channel-buffer)))
129 (list riece-dialogue-buffer riece-others-buffer)
130 riece-dialogue-buffer)
132 (riece-concat-server-name
133 (format "CTCP PING from %s (%s) to %s"
135 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
136 (riece-format-identity target-identity t)))
139 (defun riece-handle-ctcp-clientinfo-request (prefix target string)
140 (let* ((target-identity (riece-make-identity target riece-server-name))
141 (buffer (if (riece-channel-p target)
142 (riece-channel-buffer target-identity)))
143 (user (riece-prefix-nickname prefix)))
145 (format "NOTICE %s :\1CLIENTINFO %s\1\r\n"
150 (let ((case-fold-search t))
151 (if (and (fboundp atom)
153 "riece-handle-ctcp-\\(.+\\)-request"
156 (cons (match-string 1 (symbol-name atom))
158 (mapconcat #'upcase (append messages
159 riece-ctcp-additional-clientinfo)
161 (riece-insert-change buffer (format "CTCP CLIENTINFO from %s\n" user))
163 (if (and riece-channel-buffer-mode
164 (not (eq buffer riece-channel-buffer)))
165 (list riece-dialogue-buffer riece-others-buffer)
166 riece-dialogue-buffer)
168 (riece-concat-server-name
169 (format "CTCP CLIENTINFO from %s (%s) to %s"
171 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
172 (riece-format-identity target-identity t)))
175 (defun riece-handle-ctcp-action-request (prefix target string)
176 (let ((buffer (if (riece-channel-p target)
177 (riece-channel-buffer (riece-make-identity
178 target riece-server-name))))
179 (user (riece-prefix-nickname prefix)))
180 (riece-insert buffer (concat riece-ctcp-action-prefix user " " string
183 (if (and riece-channel-buffer-mode
184 (not (eq buffer riece-channel-buffer)))
185 (list riece-dialogue-buffer riece-others-buffer)
186 riece-dialogue-buffer)
187 (concat (riece-concat-server-name (concat riece-ctcp-action-prefix user
188 " " string)) "\n"))))
190 (defun riece-handle-ctcp-response (prefix string)
191 (when (and riece-ctcp-enabled prefix string
192 (riece-prefix-nickname prefix))
193 (let* ((parameters (riece-split-parameters string))
194 (targets (split-string (car parameters) ","))
195 (message (nth 1 parameters)))
196 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
197 (let ((response (downcase (match-string 1 message))))
198 (if (match-beginning 2)
199 (setq message (substring (match-string 2 message) 1)))
201 (intern (concat "riece-ctcp-" response "-response-hook")))
202 (function (intern-soft (concat "riece-handle-ctcp-"
203 response "-response")))
205 (intern (concat "riece-ctcp-after-" response
207 (unless (condition-case error
208 (run-hook-with-args-until-success
209 hook prefix (car targets) message)
212 (message "Error in `%S': %S" hook error))
215 (condition-case error
216 (funcall function prefix (car targets) message)
219 (message "Error in `%S': %S"
221 (condition-case error
222 (run-hook-with-args-until-success
223 after-hook prefix (car targets) message)
226 (message "Error in `%S': %S"
227 after-hook error)))))
230 (defun riece-handle-ctcp-version-response (prefix target string)
232 (list riece-dialogue-buffer riece-others-buffer)
234 (riece-concat-server-name
235 (format "CTCP VERSION for %s (%s) = %s"
236 (riece-prefix-nickname prefix)
237 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
241 (defun riece-handle-ctcp-ping-response (prefix target string)
242 (let* ((now (current-time))
243 (elapsed (+ (* 65536 (- (car now) (car riece-ctcp-ping-time)))
244 (- (nth 1 now) (nth 1 riece-ctcp-ping-time)))))
246 (list riece-dialogue-buffer riece-others-buffer)
248 (riece-concat-server-name
249 (format "CTCP PING for %s (%s) = %d sec"
250 (riece-prefix-nickname prefix)
251 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
255 (defun riece-handle-ctcp-clientinfo-response (prefix target string)
257 (list riece-dialogue-buffer riece-others-buffer)
259 (riece-concat-server-name
260 (format "CTCP CLIENTINFO for %s (%s) = %s"
261 (riece-prefix-nickname prefix)
262 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
266 (defun riece-command-ctcp-version (target)
268 (list (riece-completing-read-identity
270 (riece-get-identities-on-server (riece-current-server-name)))))
271 (riece-send-string (format "PRIVMSG %s :\1VERSION\1\r\n"
272 (riece-identity-prefix target))))
274 (defun riece-command-ctcp-ping (target)
276 (list (riece-completing-read-identity
278 (riece-get-identities-on-server (riece-current-server-name)))))
279 (riece-send-string (format "PRIVMSG %s :\1PING\1\r\n"
280 (riece-identity-prefix target)))
281 (setq riece-ctcp-ping-time (current-time)))
283 (defun riece-command-ctcp-clientinfo (target)
285 (list (riece-completing-read-identity
287 (riece-get-identities-on-server (riece-current-server-name)))))
288 (riece-send-string (format "PRIVMSG %s :\1CLIENTINFO\1\r\n"
289 (riece-identity-prefix target))))
291 (defun riece-command-ctcp-action (target action)
293 (list (if current-prefix-arg
294 (riece-completing-read-identity
296 (riece-get-identities-on-server (riece-current-server-name)))
297 riece-current-channel)
300 (setq message (buffer-substring (point)
301 (progn (end-of-line) (point))))
302 (if (equal message "")
303 (read-string "Action: ")
304 (prog1 (read-from-minibuffer "Action: " (cons message 0))
305 (let ((next-line-add-newlines t))
307 (if (equal action "")
309 (riece-send-string (format "PRIVMSG %s :\1ACTION %s\1\r\n"
310 (riece-identity-prefix target)
312 (let ((buffer (riece-channel-buffer target)))
315 (concat riece-ctcp-action-prefix
316 (riece-identity-prefix (riece-current-nickname)) " " action "\n"))
318 (if (and riece-channel-buffer-mode
319 (not (eq buffer riece-channel-buffer)))
320 (list riece-dialogue-buffer riece-others-buffer)
321 riece-dialogue-buffer)
323 (riece-with-server-buffer (riece-identity-server target)
324 (riece-concat-server-name
325 (concat riece-ctcp-action-prefix
326 (riece-identity-prefix (riece-current-nickname)) " " action
327 " (in " (riece-format-identity target t) ")")))
330 (defun riece-ctcp-requires ()
331 (if (memq 'riece-highlight riece-addons)
334 (defun riece-ctcp-insinuate ()
335 (add-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
336 (add-hook 'riece-notice-hook 'riece-handle-ctcp-response)
337 (if (memq 'riece-highlight riece-addons)
338 (setq riece-dialogue-font-lock-keywords
339 (cons (list (concat "^" riece-time-prefix-regexp "\\("
340 (regexp-quote riece-ctcp-action-prefix)
342 1 riece-ctcp-action-face t t)
343 riece-dialogue-font-lock-keywords))))
345 (defun riece-ctcp-enable ()
346 (define-key riece-dialogue-mode-map "\C-cv" 'riece-command-ctcp-version)
347 (define-key riece-dialogue-mode-map "\C-cp" 'riece-command-ctcp-ping)
348 (define-key riece-dialogue-mode-map "\C-ca" 'riece-command-ctcp-action)
349 (define-key riece-dialogue-mode-map "\C-cc" 'riece-command-ctcp-clientinfo)
350 (setq riece-ctcp-enable t))
352 (defun riece-ctcp-disable ()
353 (define-key riece-dialogue-mode-map "\C-cv" nil)
354 (define-key riece-dialogue-mode-map "\C-cp" nil)
355 (define-key riece-dialogue-mode-map "\C-ca" nil)
356 (define-key riece-dialogue-mode-map "\C-cc" nil)
357 (setq riece-ctcp-enabled nil))
359 (provide 'riece-ctcp)
361 ;;; riece-ctcp.el ends here