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)
32 (defvar riece-dialogue-mode-map)
34 (defun riece-ctcp-insinuate ()
35 (add-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
36 (add-hook 'riece-notice-hook 'riece-handle-ctcp-response)
37 (define-key riece-dialogue-mode-map "\C-cv" 'riece-command-ctcp-version)
38 (define-key riece-dialogue-mode-map "\C-cp" 'riece-command-ctcp-ping))
40 (defun riece-handle-ctcp-request (prefix string)
41 (when (and prefix string
42 (riece-prefix-nickname prefix))
43 (let* ((parameters (riece-split-parameters string))
44 (targets (split-string (car parameters) ","))
45 (message (nth 1 parameters)))
46 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
47 (let ((request (downcase (match-string 1 message))))
48 (if (match-beginning 2)
49 (setq message (substring (match-string 2 message) 1)))
51 (intern (concat "riece-ctcp-" request "-request-hook")))
53 (intern-soft (concat "riece-handle-ctcp-" request
56 (intern (concat "riece-ctcp-after-" request
58 (unless (condition-case error
59 (run-hook-with-args-until-success
60 hook prefix (car targets) message)
63 (message "Error occurred in `%S': %S" hook error))
67 (funcall function prefix (car targets) message)
70 (message "Error occurred in `%S': %S"
73 (run-hook-with-args-until-success
74 after-hook prefix (car targets) message)
77 (message "Error occurred in `%S': %S"
81 (defun riece-handle-ctcp-version-request (prefix target string)
82 (let ((buffer (if (riece-channel-p target)
83 (cdr (riece-identity-assoc-no-server
84 (riece-make-identity target)
85 riece-channel-buffer-alist))))
86 (user (riece-prefix-nickname prefix)))
88 (format "NOTICE %s :\1VERSION %s\1\r\n" user (riece-extended-version)))
89 (riece-insert-change buffer (format "CTCP VERSION from %s\n" user))
91 (if (and riece-channel-buffer-mode
92 (not (eq buffer riece-channel-buffer)))
93 (list riece-dialogue-buffer riece-others-buffer)
94 riece-dialogue-buffer)
96 (riece-concat-server-name
97 (format "CTCP VERSION from %s (%s) to %s"
99 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
103 (defun riece-handle-ctcp-ping-request (prefix target string)
104 (let ((buffer (if (riece-channel-p target)
105 (cdr (riece-identity-assoc-no-server
106 (riece-make-identity target)
107 riece-channel-buffer-alist))))
108 (user (riece-prefix-nickname prefix)))
111 (format "NOTICE %s :\1PING %s\1\r\n" user string)
112 (format "NOTICE %s :\1PING\1\r\n" user string)))
113 (riece-insert-change buffer (format "CTCP PING from %s\n" user))
115 (if (and riece-channel-buffer-mode
116 (not (eq buffer riece-channel-buffer)))
117 (list riece-dialogue-buffer riece-others-buffer)
118 riece-dialogue-buffer)
120 (riece-concat-server-name
121 (format "CTCP PING from %s (%s) to %s"
123 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
127 (defun riece-handle-ctcp-response (prefix string)
128 (when (and prefix string
129 (riece-prefix-nickname prefix))
130 (let* ((parameters (riece-split-parameters string))
131 (targets (split-string (car parameters) ","))
132 (message (nth 1 parameters)))
133 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
134 (let ((response (downcase (match-string 1 message))))
135 (if (match-beginning 2)
136 (setq message (substring (match-string 2 message) 1)))
138 (intern (concat "riece-ctcp-" response "-response-hook")))
139 (function (intern-soft (concat "riece-handle-ctcp-"
140 response "-response")))
142 (intern (concat "riece-ctcp-after-" response
144 (unless (condition-case error
145 (run-hook-with-args-until-success
146 hook prefix (car targets) message)
149 (message "Error occurred in `%S': %S" hook error))
152 (condition-case error
153 (funcall function prefix (car targets) message)
156 (message "Error occurred in `%S': %S"
158 (condition-case error
159 (run-hook-with-args-until-success
160 after-hook prefix (car targets) message)
163 (message "Error occurred in `%S': %S"
164 after-hook error)))))
167 (defun riece-handle-ctcp-version-response (prefix target string)
169 (list riece-dialogue-buffer riece-others-buffer)
171 (riece-concat-server-name
172 (format "CTCP VERSION for %s (%s) = %s"
173 (riece-prefix-nickname prefix)
174 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
178 (defun riece-handle-ctcp-ping-response (prefix target string)
179 (let* ((now (current-time))
180 (elapsed (+ (* 65536 (- (car now) (car riece-ctcp-ping-time)))
181 (- (nth 1 now) (nth 1 riece-ctcp-ping-time)))))
183 (list riece-dialogue-buffer riece-others-buffer)
185 (riece-concat-server-name
186 (format "CTCP PING for %s (%s) = %d sec"
187 (riece-prefix-nickname prefix)
188 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
192 (defun riece-command-ctcp-version (user)
194 (let ((completion-ignore-case t))
195 (list (completing-read
197 (mapcar #'list (riece-get-users-on-server))))))
198 (riece-send-string (format "PRIVMSG %s :\1VERSION\1\r\n" user)))
200 (defun riece-command-ctcp-ping (user)
202 (let ((completion-ignore-case t))
203 (list (completing-read
205 (mapcar #'list (riece-get-users-on-server))))))
206 (riece-send-string (format "PRIVMSG %s :\1PING\1\r\n" user))
207 (setq riece-ctcp-ping-time (current-time)))
209 (provide 'riece-ctcp)
211 ;;; riece-ctcp.el ends here