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)
30 (defvar riece-ctcp-ping-time nil)
31 (defvar riece-ctcp-additional-clientinfo nil)
33 (defvar riece-dialogue-mode-map)
35 (defun riece-ctcp-insinuate ()
36 (add-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
37 (add-hook 'riece-notice-hook 'riece-handle-ctcp-response)
38 (define-key riece-dialogue-mode-map "\C-cv" 'riece-command-ctcp-version)
39 (define-key riece-dialogue-mode-map "\C-cp" 'riece-command-ctcp-ping)
40 (define-key riece-dialogue-mode-map "\C-ca" 'riece-command-ctcp-action)
41 (define-key riece-dialogue-mode-map "\C-cc" 'riece-command-ctcp-clientinfo))
43 (defun riece-handle-ctcp-request (prefix string)
44 (when (and prefix string
45 (riece-prefix-nickname prefix))
46 (let* ((parameters (riece-split-parameters string))
47 (targets (split-string (car parameters) ","))
48 (message (nth 1 parameters)))
49 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
50 (let ((request (downcase (match-string 1 message))))
51 (if (match-beginning 2)
52 (setq message (substring (match-string 2 message) 1)))
54 (intern (concat "riece-ctcp-" request "-request-hook")))
56 (intern-soft (concat "riece-handle-ctcp-" request
59 (intern (concat "riece-ctcp-after-" request
61 (unless (condition-case error
62 (run-hook-with-args-until-success
63 hook prefix (car targets) message)
66 (message "Error occurred in `%S': %S" hook error))
70 (funcall function prefix (car targets) message)
73 (message "Error occurred in `%S': %S"
76 (run-hook-with-args-until-success
77 after-hook prefix (car targets) message)
80 (message "Error occurred in `%S': %S"
84 (defun riece-handle-ctcp-version-request (prefix target string)
85 (let ((buffer (if (riece-channel-p target)
86 (cdr (riece-identity-assoc
87 (riece-make-identity target)
88 riece-channel-buffer-alist))))
89 (user (riece-prefix-nickname prefix)))
91 (format "NOTICE %s :\1VERSION %s\1\r\n" user (riece-extended-version)))
92 (riece-insert-change buffer (format "CTCP VERSION from %s\n" user))
94 (if (and riece-channel-buffer-mode
95 (not (eq buffer riece-channel-buffer)))
96 (list riece-dialogue-buffer riece-others-buffer)
97 riece-dialogue-buffer)
99 (riece-concat-server-name
100 (format "CTCP VERSION from %s (%s) to %s"
102 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
106 (defun riece-handle-ctcp-ping-request (prefix target string)
107 (let ((buffer (if (riece-channel-p target)
108 (cdr (riece-identity-assoc
109 (riece-make-identity target)
110 riece-channel-buffer-alist))))
111 (user (riece-prefix-nickname prefix)))
114 (format "NOTICE %s :\1PING %s\1\r\n" user string)
115 (format "NOTICE %s :\1PING\1\r\n" user string)))
116 (riece-insert-change buffer (format "CTCP PING from %s\n" user))
118 (if (and riece-channel-buffer-mode
119 (not (eq buffer riece-channel-buffer)))
120 (list riece-dialogue-buffer riece-others-buffer)
121 riece-dialogue-buffer)
123 (riece-concat-server-name
124 (format "CTCP PING from %s (%s) to %s"
126 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
130 (defun riece-handle-ctcp-clientinfo-request (prefix target string)
131 (let ((buffer (if (riece-channel-p target)
132 (cdr (riece-identity-assoc
133 (riece-make-identity target)
134 riece-channel-buffer-alist))))
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))
167 (defun riece-handle-ctcp-action-request (prefix target string)
168 (let ((buffer (if (riece-channel-p target)
169 (cdr (riece-identity-assoc
170 (riece-make-identity target)
171 riece-channel-buffer-alist))))
172 (user (riece-prefix-nickname prefix)))
173 (riece-insert-change buffer (concat user " " string "\n"))
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 user " " string)) "\n"))))
181 (defun riece-handle-ctcp-response (prefix string)
182 (when (and prefix string
183 (riece-prefix-nickname prefix))
184 (let* ((parameters (riece-split-parameters string))
185 (targets (split-string (car parameters) ","))
186 (message (nth 1 parameters)))
187 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
188 (let ((response (downcase (match-string 1 message))))
189 (if (match-beginning 2)
190 (setq message (substring (match-string 2 message) 1)))
192 (intern (concat "riece-ctcp-" response "-response-hook")))
193 (function (intern-soft (concat "riece-handle-ctcp-"
194 response "-response")))
196 (intern (concat "riece-ctcp-after-" response
198 (unless (condition-case error
199 (run-hook-with-args-until-success
200 hook prefix (car targets) message)
203 (message "Error occurred in `%S': %S" hook error))
206 (condition-case error
207 (funcall function prefix (car targets) message)
210 (message "Error occurred in `%S': %S"
212 (condition-case error
213 (run-hook-with-args-until-success
214 after-hook prefix (car targets) message)
217 (message "Error occurred in `%S': %S"
218 after-hook error)))))
221 (defun riece-handle-ctcp-version-response (prefix target string)
223 (list riece-dialogue-buffer riece-others-buffer)
225 (riece-concat-server-name
226 (format "CTCP VERSION for %s (%s) = %s"
227 (riece-prefix-nickname prefix)
228 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
232 (defun riece-handle-ctcp-ping-response (prefix target string)
233 (let* ((now (current-time))
234 (elapsed (+ (* 65536 (- (car now) (car riece-ctcp-ping-time)))
235 (- (nth 1 now) (nth 1 riece-ctcp-ping-time)))))
237 (list riece-dialogue-buffer riece-others-buffer)
239 (riece-concat-server-name
240 (format "CTCP PING for %s (%s) = %d sec"
241 (riece-prefix-nickname prefix)
242 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
246 (defun riece-handle-ctcp-clientinfo-response (prefix target string)
248 (list riece-dialogue-buffer riece-others-buffer)
250 (riece-concat-server-name
251 (format "CTCP CLIENTINFO for %s (%s) = %s"
252 (riece-prefix-nickname prefix)
253 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
257 (defun riece-command-ctcp-version (user)
259 (let ((completion-ignore-case t))
260 (list (completing-read
262 (mapcar #'list (riece-get-users-on-server))))))
263 (riece-send-string (format "PRIVMSG %s :\1VERSION\1\r\n" user)))
265 (defun riece-command-ctcp-ping (user)
267 (let ((completion-ignore-case t))
268 (list (completing-read
270 (mapcar #'list (riece-get-users-on-server))))))
271 (riece-send-string (format "PRIVMSG %s :\1PING\1\r\n" user))
272 (setq riece-ctcp-ping-time (current-time)))
274 (defun riece-command-ctcp-clientinfo (user)
276 (let ((completion-ignore-case t))
277 (list (completing-read
279 (mapcar #'list (riece-get-users-on-server))))))
280 (riece-send-string (format "PRIVMSG %s :\1CLIENTINFO\1\r\n" user)))
282 (defun riece-command-ctcp-action (channel action)
284 (list (if current-prefix-arg
287 (mapcar #'list riece-current-channels))
288 riece-current-channel)
289 (read-string "Action: ")))
290 (if (equal action "")
292 (riece-send-string (format "PRIVMSG %s :\1ACTION %s\1\r\n"
293 (riece-identity-prefix channel)
295 (let ((buffer (cdr (riece-identity-assoc
296 (riece-make-identity channel)
297 riece-channel-buffer-alist))))
300 (concat (riece-identity-prefix (riece-current-nickname)) " " action "\n"))
302 (if (and riece-channel-buffer-mode
303 (not (eq buffer riece-channel-buffer)))
304 (list riece-dialogue-buffer riece-others-buffer)
305 riece-dialogue-buffer)
307 (riece-concat-server-name
308 (concat (riece-identity-prefix (riece-current-nickname)) " " action))
311 (provide 'riece-ctcp)
313 ;;; riece-ctcp.el ends here