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-response (prefix string)
183 (when (and riece-ctcp-enabled prefix string
184 (riece-prefix-nickname prefix))
185 (let* ((parameters (riece-split-parameters string))
186 (targets (split-string (car parameters) ","))
187 (message (nth 1 parameters)))
188 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
189 (let ((response (downcase (match-string 1 message))))
190 (if (match-beginning 2)
191 (setq message (substring (match-string 2 message) 1)))
193 (intern (concat "riece-ctcp-" response "-response-hook")))
194 (function (intern-soft (concat "riece-handle-ctcp-"
195 response "-response")))
197 (intern (concat "riece-ctcp-after-" response
199 (unless (riece-ignore-errors (symbol-name hook)
200 (run-hook-with-args-until-success
201 hook prefix (car targets) message))
203 (riece-ignore-errors (symbol-name function)
204 (funcall function prefix (car targets) message)))
205 (riece-ignore-errors (symbol-name after-hook)
206 (run-hook-with-args-until-success
207 after-hook prefix (car targets) message))))
210 (defun riece-handle-ctcp-version-response (prefix target string)
212 (list riece-dialogue-buffer riece-others-buffer)
214 (riece-concat-server-name
215 (format "CTCP VERSION for %s (%s) = %s"
216 (riece-prefix-nickname prefix)
217 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
221 (defun riece-handle-ctcp-ping-response (prefix target string)
222 (let* ((now (current-time))
223 (elapsed (+ (* 65536 (- (car now) (car riece-ctcp-ping-time)))
224 (- (nth 1 now) (nth 1 riece-ctcp-ping-time)))))
226 (list riece-dialogue-buffer riece-others-buffer)
228 (riece-concat-server-name
229 (format "CTCP PING for %s (%s) = %d sec"
230 (riece-prefix-nickname prefix)
231 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
235 (defun riece-handle-ctcp-clientinfo-response (prefix target string)
237 (list riece-dialogue-buffer riece-others-buffer)
239 (riece-concat-server-name
240 (format "CTCP CLIENTINFO for %s (%s) = %s"
241 (riece-prefix-nickname prefix)
242 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
246 (defun riece-command-ctcp-version (target)
248 (list (riece-completing-read-identity
250 (riece-get-identities-on-server (riece-current-server-name)))))
251 (riece-send-string (format "PRIVMSG %s :\1VERSION\1\r\n"
252 (riece-identity-prefix target))))
254 (defun riece-command-ctcp-ping (target)
256 (list (riece-completing-read-identity
258 (riece-get-identities-on-server (riece-current-server-name)))))
259 (riece-send-string (format "PRIVMSG %s :\1PING\1\r\n"
260 (riece-identity-prefix target)))
261 (setq riece-ctcp-ping-time (current-time)))
263 (defun riece-command-ctcp-clientinfo (target)
265 (list (riece-completing-read-identity
267 (riece-get-identities-on-server (riece-current-server-name)))))
268 (riece-send-string (format "PRIVMSG %s :\1CLIENTINFO\1\r\n"
269 (riece-identity-prefix target))))
271 (defun riece-command-ctcp-action (target action)
273 (list (if current-prefix-arg
274 (riece-completing-read-identity
276 (riece-get-identities-on-server (riece-current-server-name)))
277 riece-current-channel)
280 (setq message (buffer-substring (point)
281 (progn (end-of-line) (point))))
282 (if (equal message "")
283 (read-string "Action: ")
284 (prog1 (read-from-minibuffer "Action: " (cons message 0))
285 (let ((next-line-add-newlines t))
287 (if (equal action "")
289 (riece-send-string (format "PRIVMSG %s :\1ACTION %s\1\r\n"
290 (riece-identity-prefix target)
292 (let ((buffer (riece-channel-buffer target)))
295 (concat riece-ctcp-action-prefix
296 (riece-identity-prefix (riece-current-nickname)) " " action "\n"))
298 (if (and riece-channel-buffer-mode
299 (not (eq buffer riece-channel-buffer)))
300 (list riece-dialogue-buffer riece-others-buffer)
301 riece-dialogue-buffer)
303 (riece-with-server-buffer (riece-identity-server target)
304 (riece-concat-server-name
305 (concat riece-ctcp-action-prefix
306 (riece-identity-prefix (riece-current-nickname)) " " action
307 " (in " (riece-format-identity target t) ")")))
310 (defun riece-ctcp-requires ()
311 (if (memq 'riece-highlight riece-addons)
314 (defun riece-ctcp-insinuate ()
315 (add-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
316 (add-hook 'riece-notice-hook 'riece-handle-ctcp-response)
317 (if (memq 'riece-highlight riece-addons)
318 (setq riece-dialogue-font-lock-keywords
319 (cons (list (concat "^" riece-time-prefix-regexp "\\("
320 (regexp-quote riece-ctcp-action-prefix)
322 1 riece-ctcp-action-face t t)
323 riece-dialogue-font-lock-keywords))))
325 (defun riece-ctcp-enable ()
326 (define-key riece-dialogue-mode-map "\C-cv" 'riece-command-ctcp-version)
327 (define-key riece-dialogue-mode-map "\C-cp" 'riece-command-ctcp-ping)
328 (define-key riece-dialogue-mode-map "\C-ca" 'riece-command-ctcp-action)
329 (define-key riece-dialogue-mode-map "\C-cc" 'riece-command-ctcp-clientinfo)
330 (setq riece-ctcp-enabled t))
332 (defun riece-ctcp-disable ()
333 (define-key riece-dialogue-mode-map "\C-cv" nil)
334 (define-key riece-dialogue-mode-map "\C-cp" nil)
335 (define-key riece-dialogue-mode-map "\C-ca" nil)
336 (define-key riece-dialogue-mode-map "\C-cc" nil)
337 (setq riece-ctcp-enabled nil))
339 (provide 'riece-ctcp)
341 ;;; riece-ctcp.el ends here