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)))
50 (unless (run-hook-with-args-until-success
51 (intern (concat "riece-ctcp-" request "-request-hook"))
52 prefix (car targets) message)
53 (let ((function (intern-soft (concat "riece-handle-ctcp-"
58 (funcall function prefix (car targets) message)
61 (message "Error occurred in `%S': %S"
63 (run-hook-with-args-until-success
64 (intern (concat "riece-ctcp-after-" request "-request-hook"))
65 prefix (car targets) message))
68 (defun riece-handle-ctcp-version-request (prefix target string)
69 (let ((buffer (if (riece-channel-p target)
70 (cdr (riece-identity-assoc-no-server
71 (riece-make-identity target)
72 riece-channel-buffer-alist))))
73 (user (riece-prefix-nickname prefix)))
75 (format "NOTICE %s :\1VERSION %s\1\r\n" user (riece-extended-version)))
76 (riece-insert-change buffer (format "CTCP VERSION from %s\n" user))
78 (if (and riece-channel-buffer-mode
79 (not (eq buffer riece-channel-buffer)))
80 (list riece-dialogue-buffer riece-others-buffer)
81 riece-dialogue-buffer)
83 (riece-concat-server-name
84 (format "CTCP VERSION from %s (%s) to %s"
86 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
90 (defun riece-handle-ctcp-ping-request (prefix target string)
91 (let ((buffer (if (riece-channel-p target)
92 (cdr (riece-identity-assoc-no-server
93 (riece-make-identity target)
94 riece-channel-buffer-alist))))
95 (user (riece-prefix-nickname prefix)))
98 (format "NOTICE %s :\1PING %s\1\r\n" user string)
99 (format "NOTICE %s :\1PING\1\r\n" user string)))
100 (riece-insert-change buffer (format "CTCP PING from %s\n" user))
102 (if (and riece-channel-buffer-mode
103 (not (eq buffer riece-channel-buffer)))
104 (list riece-dialogue-buffer riece-others-buffer)
105 riece-dialogue-buffer)
107 (riece-concat-server-name
108 (format "CTCP PING from %s (%s) to %s"
110 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
114 (defun riece-handle-ctcp-response (prefix string)
115 (when (and prefix string
116 (riece-prefix-nickname prefix))
117 (let* ((parameters (riece-split-parameters string))
118 (targets (split-string (car parameters) ","))
119 (message (nth 1 parameters)))
120 (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
121 (let ((response (downcase (match-string 1 message))))
122 (if (match-beginning 2)
123 (setq message (substring (match-string 2 message) 1)))
124 (unless (run-hook-with-args-until-success
125 (intern (concat "riece-ctcp-" response "-response-hook"))
126 prefix (car targets) message)
127 (let ((function (intern-soft (concat "riece-handle-ctcp-"
131 (condition-case error
132 (funcall function prefix (car targets) message)
135 (message "Error occurred in `%S': %S"
137 (run-hook-with-args-until-success
138 (intern (concat "riece-ctcp-after-" response "-response-hook"))
139 prefix (car targets) message))
142 (defun riece-handle-ctcp-version-response (prefix target string)
144 (list riece-dialogue-buffer riece-others-buffer)
146 (riece-concat-server-name
147 (format "CTCP VERSION for %s (%s) = %s"
148 (riece-prefix-nickname prefix)
149 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
153 (defun riece-handle-ctcp-ping-response (prefix target string)
154 (let* ((now (current-time))
155 (elapsed (+ (* 65536 (- (car now) (car riece-ctcp-ping-time)))
156 (- (nth 1 now) (nth 1 riece-ctcp-ping-time)))))
158 (list riece-dialogue-buffer riece-others-buffer)
160 (riece-concat-server-name
161 (format "CTCP PING for %s (%s) = %d sec"
162 (riece-prefix-nickname prefix)
163 (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
167 (defun riece-command-ctcp-version (user)
169 (let ((completion-ignore-case t))
170 (list (completing-read
172 (mapcar #'list (riece-get-users-on-server))))))
173 (riece-send-string (format "PRIVMSG %s :\1VERSION\1\r\n" user)))
175 (defun riece-command-ctcp-ping (user)
177 (let ((completion-ignore-case t))
178 (list (completing-read
180 (mapcar #'list (riece-get-users-on-server))))))
181 (riece-send-string (format "PRIVMSG %s :\1PING\1\r\n" user))
182 (setq riece-ctcp-ping-time (current-time)))
184 (provide 'riece-ctcp)
186 ;;; riece-ctcp.el ends here