Simplified.
[riece] / lisp / riece-ctcp.el
1 ;;; riece-ctcp.el --- CTCP add-on
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Code:
26
27 (require 'riece-version)
28 (require 'riece-misc)
29 (require 'riece-highlight)
30 (require 'riece-display)
31 (require 'riece-debug)
32
33 (defface riece-ctcp-action-face
34   '((((class color)
35       (background dark))
36      (:foreground "PaleGreen" :italic t))
37     (((class color)
38       (background light))
39      (:foreground "ForestGreen" :italic t))
40     (t
41      (:bold t)))
42   "Face used for displaying \"*** Action:\" line"
43   :group 'riece-highlight-faces)
44 (defvar riece-ctcp-action-face 'riece-ctcp-action-face)
45
46 (defconst riece-ctcp-action-prefix "*** Action: ")
47
48 (defvar riece-ctcp-ping-time nil)
49 (defvar riece-ctcp-additional-clientinfo nil)
50
51 (defvar riece-dialogue-mode-map)
52
53 (defvar riece-ctcp-enabled nil)
54
55 (defconst riece-ctcp-description
56   "CTCP (Client To Client Protocol) support")
57
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)))
68             (let ((hook
69                    (intern (concat "riece-ctcp-" request "-request-hook")))
70                   (function
71                    (intern-soft (concat "riece-handle-ctcp-" request
72                                         "-request")))
73                   (after-hook
74                    (intern (concat "riece-ctcp-after-" request
75                                    "-request-hook"))))
76               (unless (riece-ignore-errors (symbol-name hook)
77                         (run-hook-with-args-until-success
78                          hook prefix (car targets) message))
79                 (if function
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))))
85             t)))))
86
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)))
92     (riece-send-string
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))
95     (riece-insert-change
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)
100      (concat
101       (riece-concat-server-name
102        (format "CTCP VERSION from %s (%s) to %s"
103                user
104                (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
105                (riece-format-identity target-identity t)))
106       "\n"))))
107
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)))
113     (riece-send-string
114      (if string
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))
118     (riece-insert-change
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)
123      (concat
124       (riece-concat-server-name
125        (format "CTCP PING from %s (%s) to %s"
126                user
127                (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
128                (riece-format-identity target-identity t)))
129       "\n"))))
130
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)))
136     (riece-send-string
137      (format "NOTICE %s :\1CLIENTINFO %s\1\r\n"
138              user
139              (let (messages)
140                (mapatoms
141                 (lambda (atom)
142                   (let ((case-fold-search t))
143                     (if (and (fboundp atom)
144                              (string-match
145                               "riece-handle-ctcp-\\(.+\\)-request"
146                               (symbol-name atom)))
147                         (setq messages
148                               (cons (match-string 1 (symbol-name atom))
149                                     messages))))))
150                (mapconcat #'upcase (append messages
151                                            riece-ctcp-additional-clientinfo)
152                           " "))))
153     (riece-insert-change buffer (format "CTCP CLIENTINFO from %s\n" user))
154     (riece-insert-change
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)
159      (concat
160       (riece-concat-server-name
161        (format "CTCP CLIENTINFO from %s (%s) to %s"
162                user
163                (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
164                (riece-format-identity target-identity t)))
165       "\n"))))
166
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
173                                  "\n"))
174     (riece-insert
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"))))
181
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)))
192             (let ((hook
193                    (intern (concat "riece-ctcp-" response "-response-hook")))
194                   (function (intern-soft (concat "riece-handle-ctcp-"
195                                                  response "-response")))
196                   (after-hook
197                    (intern (concat "riece-ctcp-after-" response
198                                    "-response-hook"))))
199               (unless (riece-ignore-errors (symbol-name hook)
200                         (run-hook-with-args-until-success
201                          hook prefix (car targets) message))
202                 (if function
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))))
208             t)))))
209
210 (defun riece-handle-ctcp-version-response (prefix target string)
211   (riece-insert-change
212    (list riece-dialogue-buffer riece-others-buffer)
213    (concat
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))
218              string))
219     "\n")))
220
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)))))
225     (riece-insert-change
226      (list riece-dialogue-buffer riece-others-buffer)
227      (concat
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))
232                elapsed))
233       "\n"))))
234
235 (defun riece-handle-ctcp-clientinfo-response (prefix target string)
236   (riece-insert-change
237    (list riece-dialogue-buffer riece-others-buffer)
238    (concat
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))
243              string))
244     "\n")))
245
246 (defun riece-command-ctcp-version (target)
247   (interactive
248    (list (riece-completing-read-identity
249           "Channel/User: "
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))))
253
254 (defun riece-command-ctcp-ping (target)
255   (interactive
256    (list (riece-completing-read-identity
257           "Channel/User: "
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)))
262
263 (defun riece-command-ctcp-clientinfo (target)
264   (interactive
265    (list (riece-completing-read-identity
266           "Channel/User: "
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))))
270
271 (defun riece-command-ctcp-action (target action)
272   (interactive
273    (list (if current-prefix-arg
274              (riece-completing-read-identity
275               "Channel/User: "
276               (riece-get-identities-on-server (riece-current-server-name)))
277            riece-current-channel)
278          (let (message)
279            (beginning-of-line)
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))
286                  (next-line 1)))))))
287   (if (equal action "")
288       (error "No action"))
289   (riece-send-string (format "PRIVMSG %s :\1ACTION %s\1\r\n"
290                              (riece-identity-prefix target)
291                              action))
292   (let ((buffer (riece-channel-buffer target)))
293     (riece-insert
294      buffer
295      (concat riece-ctcp-action-prefix
296              (riece-identity-prefix (riece-current-nickname)) " " action "\n"))
297     (riece-insert
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)
302      (concat
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) ")")))
308       "\n"))))
309
310 (defun riece-ctcp-requires ()
311   (if (memq 'riece-highlight riece-addons)
312       '(riece-highlight)))
313
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)
321                                 ".*\\)$")
322                         1 riece-ctcp-action-face t t)
323                   riece-dialogue-font-lock-keywords))))
324
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))
331
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))
338
339 (provide 'riece-ctcp)
340
341 ;;; riece-ctcp.el ends here