1199dc39c81572f7831b335d8f92dc4f62e53445
[riece] / lisp / riece-ctcp.el
1 ;;; riece-ctcp.el --- CTCP (Client To Client Protocol) support
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 ;;; Commentary:
26
27 ;; NOTE: This is an add-on module for Riece.
28
29 ;;; Code:
30
31 (require 'riece-version)
32 (require 'riece-misc)
33 (require 'riece-highlight)
34 (require 'riece-display)
35 (require 'riece-debug)
36
37 (defface riece-ctcp-action-face
38   '((((class color)
39       (background dark))
40      (:foreground "PaleGreen" :italic t))
41     (((class color)
42       (background light))
43      (:foreground "ForestGreen" :italic t))
44     (t
45      (:bold t)))
46   "Face used for displaying \"*** Action:\" line"
47   :group 'riece-highlight-faces)
48 (defvar riece-ctcp-action-face 'riece-ctcp-action-face)
49
50 (defconst riece-ctcp-action-prefix "*** Action: ")
51
52 (defvar riece-ctcp-ping-time nil)
53 (defvar riece-ctcp-additional-clientinfo nil)
54
55 (defvar riece-dialogue-mode-map)
56
57 (defvar riece-ctcp-enabled nil)
58
59 (defconst riece-ctcp-description
60   "CTCP (Client To Client Protocol) support.")
61
62 (defun riece-handle-ctcp-request (prefix string)
63   (when (and riece-ctcp-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)))
72             (let ((hook
73                    (intern (concat "riece-ctcp-" request "-request-hook")))
74                   (function
75                    (intern-soft (concat "riece-handle-ctcp-" request
76                                         "-request")))
77                   (after-hook
78                    (intern (concat "riece-ctcp-after-" request
79                                    "-request-hook"))))
80               (unless (riece-funcall-ignore-errors
81                        (symbol-name hook)
82                        #'run-hook-with-args-until-success
83                        hook prefix (car targets) message)
84                 (if function
85                     (riece-funcall-ignore-errors (symbol-name function)
86                                                   function prefix (car targets)
87                                                   message))
88                 (riece-funcall-ignore-errors (symbol-name after-hook)
89                                              #'run-hook-with-args-until-success
90                                              after-hook prefix (car targets)
91                                              message)))
92             t)))))
93
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)))
99     (riece-send-string
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))
102     (riece-insert-change
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)
107      (concat
108       (riece-concat-server-name
109        (format "CTCP VERSION from %s (%s) to %s"
110                user
111                (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
112                (riece-format-identity target-identity t)))
113       "\n"))))
114
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)))
120     (riece-send-string
121      (if string
122          (format "NOTICE %s :\1PING %s\1\r\n" user string)
123        (format "NOTICE %s :\1PING\1\r\n" user string)))
124     (riece-insert-change buffer (format "CTCP PING from %s\n" user))
125     (riece-insert-change
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)
130      (concat
131       (riece-concat-server-name
132        (format "CTCP PING from %s (%s) to %s"
133                user
134                (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
135                (riece-format-identity target-identity t)))
136       "\n"))))
137
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)))
143     (riece-send-string
144      (format "NOTICE %s :\1CLIENTINFO %s\1\r\n"
145              user
146              (let (messages)
147                (mapatoms
148                 (lambda (atom)
149                   (let ((case-fold-search t))
150                     (if (and (fboundp atom)
151                              (string-match
152                               "riece-handle-ctcp-\\(.+\\)-request"
153                               (symbol-name atom)))
154                         (setq messages
155                               (cons (match-string 1 (symbol-name atom))
156                                     messages))))))
157                (mapconcat #'upcase (append messages
158                                            riece-ctcp-additional-clientinfo)
159                           " "))))
160     (riece-insert-change buffer (format "CTCP CLIENTINFO from %s\n" user))
161     (riece-insert-change
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)
166      (concat
167       (riece-concat-server-name
168        (format "CTCP CLIENTINFO from %s (%s) to %s"
169                user
170                (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
171                (riece-format-identity target-identity t)))
172       "\n"))))
173
174 (defun riece-handle-ctcp-action-request (prefix target string)
175   (let ((buffer (if (riece-channel-p target)
176                     (riece-channel-buffer (riece-make-identity
177                                            target riece-server-name))))
178         (user (riece-prefix-nickname prefix)))
179     (riece-insert buffer (concat riece-ctcp-action-prefix
180                                  (riece-format-identity
181                                   (riece-make-identity user riece-server-name)
182                                   t)
183                                  " " string
184                                  "\n"))
185     (riece-insert
186      (if (and riece-channel-buffer-mode
187               (not (eq buffer riece-channel-buffer)))
188          (list riece-dialogue-buffer riece-others-buffer)
189        riece-dialogue-buffer)
190      (concat (riece-concat-server-name
191               (concat riece-ctcp-action-prefix
192                       (riece-format-identity
193                        (riece-make-identity target riece-server-name)
194                        t)
195                       ": "
196                       (riece-format-identity
197                        (riece-make-identity user riece-server-name)
198                        t)
199                       " " string)) "\n"))))
200
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")))
207     (riece-send-string
208      (format "NOTICE %s :\1TIME %s\1\r\n" user time))
209     (riece-insert-change buffer (format "CTCP TIME from %s\n" user))
210     (riece-insert-change
211      (if (and riece-channel-buffer-mode
212               (not (eq buffer riece-channel-buffer)))
213          (list riece-dialogue-buffer riece-others-buffer)
214        riece-dialogue-buffer)
215      (concat
216       (riece-concat-server-name
217        (format "CTCP TIME from %s (%s) to %s"
218                user
219                (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
220                (riece-format-identity target-identity t)))
221       "\n"))))
222
223 (defun riece-handle-ctcp-response (prefix string)
224   (when (and riece-ctcp-enabled prefix string
225              (riece-prefix-nickname prefix))
226     (let* ((parameters (riece-split-parameters string))
227            (targets (split-string (car parameters) ","))
228            (message (nth 1 parameters)))
229       (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
230           (let ((response (downcase (match-string 1 message))))
231             (if (match-beginning 2)
232                 (setq message (substring (match-string 2 message) 1)))
233             (let ((hook
234                    (intern (concat "riece-ctcp-" response "-response-hook")))
235                   (function (intern-soft (concat "riece-handle-ctcp-"
236                                                  response "-response")))
237                   (after-hook
238                    (intern (concat "riece-ctcp-after-" response
239                                    "-response-hook"))))
240               (unless (riece-funcall-ignore-errors
241                        (symbol-name hook)
242                        #'run-hook-with-args-until-success
243                        hook prefix (car targets) message)
244                 (if function
245                     (riece-funcall-ignore-errors
246                      (symbol-name function)
247                      function prefix (car targets) message))
248                 (riece-funcall-ignore-errors (symbol-name after-hook)
249                                              #'run-hook-with-args-until-success
250                                              after-hook prefix (car targets)
251                                              message)))
252             t)))))
253
254 (defun riece-handle-ctcp-version-response (prefix target string)
255   (riece-insert-change
256    (list riece-dialogue-buffer riece-others-buffer)
257    (concat
258     (riece-concat-server-name
259      (format "CTCP VERSION for %s (%s) = %s"
260              (riece-prefix-nickname prefix)
261              (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
262              string))
263     "\n")))
264
265 (defun riece-handle-ctcp-ping-response (prefix target string)
266   (let* ((now (current-time))
267          (elapsed (+ (* 65536 (- (car now) (car riece-ctcp-ping-time)))
268                      (- (nth 1 now) (nth 1 riece-ctcp-ping-time)))))
269     (riece-insert-change
270      (list riece-dialogue-buffer riece-others-buffer)
271      (concat
272       (riece-concat-server-name
273        (format "CTCP PING for %s (%s) = %d sec"
274                (riece-prefix-nickname prefix)
275                (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
276                elapsed))
277       "\n"))))
278
279 (defun riece-handle-ctcp-clientinfo-response (prefix target string)
280   (riece-insert-change
281    (list riece-dialogue-buffer riece-others-buffer)
282    (concat
283     (riece-concat-server-name
284      (format "CTCP CLIENTINFO for %s (%s) = %s"
285              (riece-prefix-nickname prefix)
286              (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
287              string))
288     "\n")))
289
290 (defun riece-handle-ctcp-time-response (prefix target string)
291   (riece-insert-change
292    (list riece-dialogue-buffer riece-others-buffer)
293    (concat
294     (riece-concat-server-name
295      (format "CTCP TIME for %s (%s) = %s"
296              (riece-prefix-nickname prefix)
297              (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
298              string))
299     "\n")))
300
301 (defun riece-command-ctcp-version (target)
302   (interactive
303    (list (riece-completing-read-identity
304           "Channel/User: "
305           (riece-get-identities-on-server (riece-current-server-name)))))
306   (riece-send-string (format "PRIVMSG %s :\1VERSION\1\r\n"
307                              (riece-identity-prefix target))))
308
309 (defun riece-command-ctcp-ping (target)
310   (interactive
311    (list (riece-completing-read-identity
312           "Channel/User: "
313           (riece-get-identities-on-server (riece-current-server-name)))))
314   (riece-send-string (format "PRIVMSG %s :\1PING\1\r\n"
315                              (riece-identity-prefix target)))
316   (setq riece-ctcp-ping-time (current-time)))
317
318 (defun riece-command-ctcp-clientinfo (target)
319   (interactive
320    (list (riece-completing-read-identity
321           "Channel/User: "
322           (riece-get-identities-on-server (riece-current-server-name)))))
323   (riece-send-string (format "PRIVMSG %s :\1CLIENTINFO\1\r\n"
324                              (riece-identity-prefix target))))
325
326 (defun riece-command-ctcp-action (target action)
327   (interactive
328    (list (if current-prefix-arg
329              (riece-completing-read-identity
330               "Channel/User: "
331               (riece-get-identities-on-server (riece-current-server-name)))
332            riece-current-channel)
333          (let (message)
334            (beginning-of-line)
335            (setq message (buffer-substring (point)
336                                            (progn (end-of-line) (point))))
337            (if (equal message "")
338                (read-string "Action: ")
339              (prog1 (read-from-minibuffer "Action: " (cons message 0))
340                (let ((next-line-add-newlines t))
341                  (next-line 1)))))))
342   (if (equal action "")
343       (error "No action"))
344   (riece-send-string (format "PRIVMSG %s :\1ACTION %s\1\r\n"
345                              (riece-identity-prefix target)
346                              action))
347   (let ((buffer (riece-channel-buffer target)))
348     (riece-insert
349      buffer
350      (concat riece-ctcp-action-prefix
351              (riece-identity-prefix (riece-current-nickname)) " " action "\n"))
352     (riece-insert
353      (if (and riece-channel-buffer-mode
354               (not (eq buffer riece-channel-buffer)))
355          (list riece-dialogue-buffer riece-others-buffer)
356        riece-dialogue-buffer)
357      (concat
358       (riece-with-server-buffer (riece-identity-server target)
359         (riece-concat-server-name
360          (concat riece-ctcp-action-prefix
361                  (riece-format-identity target t) ": "
362                  (riece-identity-prefix (riece-current-nickname)) " " action)))
363       "\n"))))
364
365 (defun riece-command-ctcp-time (target)
366   (interactive
367    (list (riece-completing-read-identity
368           "Channel/User: "
369           (riece-get-identities-on-server (riece-current-server-name)))))
370   (riece-send-string (format "PRIVMSG %s :\1TIME\1\r\n"
371                              (riece-identity-prefix target))))
372
373 (defun riece-ctcp-requires ()
374   (if (memq 'riece-highlight riece-addons)
375       '(riece-highlight)))
376
377 (defun riece-ctcp-insinuate ()
378   (add-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
379   (add-hook 'riece-notice-hook 'riece-handle-ctcp-response)
380   (if (memq 'riece-highlight riece-addons)
381       (setq riece-dialogue-font-lock-keywords
382             (cons (list (concat "^" riece-time-prefix-regexp "\\("
383                                 (regexp-quote riece-ctcp-action-prefix)
384                                 ".*\\)$")
385                         1 riece-ctcp-action-face t t)
386                   riece-dialogue-font-lock-keywords))))
387
388 (defun riece-ctcp-enable ()
389   (define-key riece-dialogue-mode-map "\C-cv" 'riece-command-ctcp-version)
390   (define-key riece-dialogue-mode-map "\C-cp" 'riece-command-ctcp-ping)
391   (define-key riece-dialogue-mode-map "\C-ca" 'riece-command-ctcp-action)
392   (define-key riece-dialogue-mode-map "\C-cc" 'riece-command-ctcp-clientinfo)
393   (define-key riece-dialogue-mode-map "\C-ct" 'riece-command-ctcp-time)
394   (setq riece-ctcp-enabled t))
395
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)
402   (setq riece-ctcp-enabled nil))
403
404 (provide 'riece-ctcp)
405
406 ;;; riece-ctcp.el ends here