1 ;;; riece-ctcp.el --- CTCP (Client To Client Protocol) support -*- lexical-binding: t -*-
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; NOTE: This is an add-on module for Riece.
31 (require 'riece-version)
33 (require 'riece-highlight)
34 (require 'riece-display)
35 (require 'riece-debug)
37 (require 'riece-message)
39 (defface riece-ctcp-action-face
42 (:foreground "PaleGreen" :italic t))
45 (:foreground "ForestGreen" :italic t))
48 "Face used for displaying \"*** Action:\" line"
49 :group 'riece-highlight-faces)
50 (defvar riece-ctcp-action-face 'riece-ctcp-action-face)
52 (defconst riece-ctcp-action-prefix "*** Action: ")
54 (defvar riece-ctcp-ping-time nil)
55 (defvar riece-ctcp-additional-clientinfo nil)
57 (defvar riece-dialogue-mode-map)
59 (defconst riece-ctcp-description
60 "CTCP (Client To Client Protocol) support.")
62 (defun riece-handle-ctcp-request (prefix string)
63 (when (and (get 'riece-ctcp 'riece-addon-enabled) prefix string
64 (riece-prefix-nickname prefix))
65 (let* ((parameters (riece-split-parameters string))
66 (targets (split-string (car parameters) ","))
67 (message (nth 1 parameters)))
68 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
69 (let ((request (downcase (match-string 1 message))))
70 (if (match-beginning 2)
71 (setq message (substring (match-string 2 message) 1)))
73 (intern (concat "riece-ctcp-" request "-request-hook")))
75 (intern-soft (concat "riece-handle-ctcp-" request
78 (intern (concat "riece-ctcp-after-" request
80 (unless (riece-funcall-ignore-errors
82 #'run-hook-with-args-until-success
83 hook prefix (car targets) message)
85 (riece-funcall-ignore-errors (symbol-name function)
86 function prefix (car targets)
88 (riece-funcall-ignore-errors (symbol-name after-hook)
89 #'run-hook-with-args-until-success
90 after-hook prefix (car targets)
94 (defun riece-handle-ctcp-version-request (prefix target _string)
95 (let* ((target-identity (riece-make-identity target riece-server-name))
96 (buffer (if (riece-channel-p target)
97 (riece-channel-buffer target-identity)))
98 (user (riece-prefix-nickname prefix)))
100 (format "NOTICE %s :\1VERSION %s\1\r\n" user (riece-extended-version)))
101 (riece-insert-change buffer (format "CTCP VERSION from %s\n" user))
103 (if (and riece-channel-buffer-mode
104 (not (eq buffer riece-channel-buffer)))
105 (list riece-dialogue-buffer riece-others-buffer)
106 riece-dialogue-buffer)
108 (riece-concat-server-name
109 (format (riece-mcat "CTCP VERSION from %s (%s) to %s")
111 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
112 (riece-format-identity target-identity t)))
115 (defun riece-handle-ctcp-ping-request (prefix target string)
116 (let* ((target-identity (riece-make-identity target riece-server-name))
117 (buffer (if (riece-channel-p target)
118 (riece-channel-buffer target-identity)))
119 (user (riece-prefix-nickname prefix)))
122 (format "NOTICE %s :\1PING %s\1\r\n" user string)
123 (format "NOTICE %s :\1PING\1\r\n" user)))
124 (riece-insert-change buffer (format "CTCP PING from %s\n" user))
126 (if (and riece-channel-buffer-mode
127 (not (eq buffer riece-channel-buffer)))
128 (list riece-dialogue-buffer riece-others-buffer)
129 riece-dialogue-buffer)
131 (riece-concat-server-name
132 (format (riece-mcat "CTCP PING from %s (%s) to %s")
134 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
135 (riece-format-identity target-identity t)))
138 (defun riece-handle-ctcp-clientinfo-request (prefix target _string)
139 (let* ((target-identity (riece-make-identity target riece-server-name))
140 (buffer (if (riece-channel-p target)
141 (riece-channel-buffer target-identity)))
142 (user (riece-prefix-nickname prefix)))
144 (format "NOTICE %s :\1CLIENTINFO %s\1\r\n"
149 (let ((case-fold-search t))
150 (if (and (fboundp atom)
152 "riece-handle-ctcp-\\(.+\\)-request"
155 (cons (match-string 1 (symbol-name atom))
157 (mapconcat #'upcase (append messages
158 riece-ctcp-additional-clientinfo)
160 (riece-insert-change buffer (format "CTCP CLIENTINFO from %s\n" user))
162 (if (and riece-channel-buffer-mode
163 (not (eq buffer riece-channel-buffer)))
164 (list riece-dialogue-buffer riece-others-buffer)
165 riece-dialogue-buffer)
167 (riece-concat-server-name
168 (format (riece-mcat "CTCP CLIENTINFO from %s (%s) to %s")
170 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
171 (riece-format-identity target-identity t)))
174 (defun riece-ctcp-action-format-message (message &optional global)
175 (riece-with-server-buffer (riece-identity-server
176 (riece-message-speaker message))
179 (riece-concat-server-name
180 (concat riece-ctcp-action-prefix
181 (riece-format-identity (riece-message-target message) t) ": "
182 (riece-identity-prefix (riece-message-speaker message)) " "
183 (riece-message-text message)))
184 (concat riece-ctcp-action-prefix
185 (riece-identity-prefix (riece-message-speaker message)) " "
186 (riece-message-text message)))
189 (defun riece-handle-ctcp-action-request (prefix target string)
190 (let ((user (riece-prefix-nickname prefix)))
191 (riece-display-message
192 (riece-make-message (riece-make-identity user
194 (riece-make-identity target
198 (riece-identity-equal-no-server
199 user riece-real-nickname)))))
201 (defun riece-handle-ctcp-time-request (prefix target _string)
202 (let* ((target-identity (riece-make-identity target riece-server-name))
203 (buffer (if (riece-channel-p target)
204 (riece-channel-buffer target-identity)))
205 (user (riece-prefix-nickname prefix))
206 (time (format-time-string "%c")))
208 (format "NOTICE %s :\1TIME %s\1\r\n" user time))
209 (riece-insert-change buffer (format (riece-mcat "CTCP TIME from %s\n")
212 (if (and riece-channel-buffer-mode
213 (not (eq buffer riece-channel-buffer)))
214 (list riece-dialogue-buffer riece-others-buffer)
215 riece-dialogue-buffer)
217 (riece-concat-server-name
218 (format (riece-mcat "CTCP TIME from %s (%s) to %s")
220 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
221 (riece-format-identity target-identity t)))
224 (defun riece-handle-ctcp-response (prefix string)
225 (when (and (get 'riece-ctcp 'riece-addon-enabled) prefix string
226 (riece-prefix-nickname prefix))
227 (let* ((parameters (riece-split-parameters string))
228 (targets (split-string (car parameters) ","))
229 (message (nth 1 parameters)))
230 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
231 (let ((response (downcase (match-string 1 message))))
232 (if (match-beginning 2)
233 (setq message (substring (match-string 2 message) 1)))
235 (intern (concat "riece-ctcp-" response "-response-hook")))
236 (function (intern-soft (concat "riece-handle-ctcp-"
237 response "-response")))
239 (intern (concat "riece-ctcp-after-" response
241 (unless (riece-funcall-ignore-errors
243 #'run-hook-with-args-until-success
244 hook prefix (car targets) message)
246 (riece-funcall-ignore-errors
247 (symbol-name function)
248 function prefix (car targets) message))
249 (riece-funcall-ignore-errors (symbol-name after-hook)
250 #'run-hook-with-args-until-success
251 after-hook prefix (car targets)
255 (defun riece-handle-ctcp-version-response (prefix _target string)
257 (list riece-dialogue-buffer riece-others-buffer)
259 (riece-concat-server-name
260 (format (riece-mcat "CTCP VERSION for %s (%s) = %s")
261 (riece-prefix-nickname prefix)
262 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
266 (defun riece-handle-ctcp-ping-response (prefix _target _string)
267 (let* ((now (current-time))
268 (elapsed (+ (* 65536 (- (car now) (car riece-ctcp-ping-time)))
269 (- (nth 1 now) (nth 1 riece-ctcp-ping-time)))))
271 (list riece-dialogue-buffer riece-others-buffer)
273 (riece-concat-server-name
274 (format (riece-mcat "CTCP PING for %s (%s) = %d sec")
275 (riece-prefix-nickname prefix)
276 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
280 (defun riece-handle-ctcp-clientinfo-response (prefix _target string)
282 (list riece-dialogue-buffer riece-others-buffer)
284 (riece-concat-server-name
285 (format (riece-mcat "CTCP CLIENTINFO for %s (%s) = %s")
286 (riece-prefix-nickname prefix)
287 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
291 (defun riece-handle-ctcp-time-response (prefix _target string)
293 (list riece-dialogue-buffer riece-others-buffer)
295 (riece-concat-server-name
296 (format (riece-mcat "CTCP TIME for %s (%s) = %s")
297 (riece-prefix-nickname prefix)
298 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
302 (defun riece-command-ctcp-version (target)
304 (list (riece-completing-read-identity
305 (riece-mcat "Channel/User: ")
306 (riece-get-identities-on-server (riece-current-server-name)))))
307 (riece-send-string (format "PRIVMSG %s :\1VERSION\1\r\n"
308 (riece-identity-prefix target))))
310 (defun riece-command-ctcp-ping (target)
312 (list (riece-completing-read-identity
313 (riece-mcat "Channel/User: ")
314 (riece-get-identities-on-server (riece-current-server-name)))))
315 (riece-send-string (format "PRIVMSG %s :\1PING\1\r\n"
316 (riece-identity-prefix target)))
317 (setq riece-ctcp-ping-time (current-time)))
319 (defun riece-command-ctcp-clientinfo (target)
321 (list (riece-completing-read-identity
322 (riece-mcat "Channel/User: ")
323 (riece-get-identities-on-server (riece-current-server-name)))))
324 (riece-send-string (format "PRIVMSG %s :\1CLIENTINFO\1\r\n"
325 (riece-identity-prefix target))))
327 (defun riece-command-ctcp-action (target action)
329 (list (if current-prefix-arg
330 (riece-completing-read-identity
331 (riece-mcat "Channel/User: ")
332 (riece-get-identities-on-server (riece-current-server-name)))
333 riece-current-channel)
336 (setq message (buffer-substring (point)
337 (progn (end-of-line) (point))))
338 (if (equal message "")
339 (read-string (riece-mcat "Action: "))
340 (prog1 (read-from-minibuffer (riece-mcat "Action: ")
342 (if (> (forward-line) 0)
344 (if (equal action "")
346 (riece-send-string (format "PRIVMSG %s :\1ACTION %s\1\r\n"
347 (riece-identity-prefix target)
349 (riece-display-message
350 (riece-make-message (riece-current-nickname) target action 'action t)))
352 (defun riece-command-ctcp-time (target)
354 (list (riece-completing-read-identity
355 (riece-mcat "Channel/User: ")
356 (riece-get-identities-on-server (riece-current-server-name)))))
357 (riece-send-string (format "PRIVMSG %s :\1TIME\1\r\n"
358 (riece-identity-prefix target))))
360 (defun riece-ctcp-requires ()
361 (if (memq 'riece-highlight riece-addons)
364 (defvar riece-ctcp-dialogue-font-lock-keywords
365 (list (concat "^" riece-time-prefix-regexp "\\("
366 (regexp-quote riece-ctcp-action-prefix)
368 1 riece-ctcp-action-face t t))
370 (defun riece-ctcp-insinuate ()
371 (add-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
372 (add-hook 'riece-notice-hook 'riece-handle-ctcp-response)
373 (if (memq 'riece-highlight riece-addons)
374 (setq riece-dialogue-font-lock-keywords
375 (cons riece-ctcp-dialogue-font-lock-keywords
376 riece-dialogue-font-lock-keywords)))
377 (unless (assq 'action riece-message-format-function-alist)
378 (setq riece-message-format-function-alist
379 (cons (cons 'action #'riece-ctcp-action-format-message)
380 riece-message-format-function-alist))))
382 (defun riece-ctcp-uninstall ()
383 (remove-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
384 (remove-hook 'riece-notice-hook 'riece-handle-ctcp-response)
385 (setq riece-dialogue-font-lock-keywords
386 (delq riece-ctcp-dialogue-font-lock-keywords
387 riece-dialogue-font-lock-keywords)))
389 (defun riece-ctcp-enable ()
390 (define-key riece-dialogue-mode-map "\C-cv" 'riece-command-ctcp-version)
391 (define-key riece-dialogue-mode-map "\C-cp" 'riece-command-ctcp-ping)
392 (define-key riece-dialogue-mode-map "\C-ca" 'riece-command-ctcp-action)
393 (define-key riece-dialogue-mode-map "\C-cc" 'riece-command-ctcp-clientinfo)
394 (define-key riece-dialogue-mode-map "\C-ct" 'riece-command-ctcp-time))
396 (defun riece-ctcp-disable ()
397 (define-key riece-dialogue-mode-map "\C-cv" nil)
398 (define-key riece-dialogue-mode-map "\C-cp" nil)
399 (define-key riece-dialogue-mode-map "\C-ca" nil)
400 (define-key riece-dialogue-mode-map "\C-cc" nil)
401 (define-key riece-dialogue-mode-map "\C-ct" nil))
403 (provide 'riece-ctcp)
405 ;;; riece-ctcp.el ends here