Fix -- user list buffer not updating when parts are hidden.
[riece] / lisp / riece-ctcp.el
1 ;;; riece-ctcp.el --- CTCP (Client To Client Protocol) support -*- lexical-binding: t -*-
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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 (require 'riece-mcat)
37 (require 'riece-message)
38
39 (defface riece-ctcp-action-face
40   '((((class color)
41       (background dark))
42      (:foreground "PaleGreen" :italic t))
43     (((class color)
44       (background light))
45      (:foreground "ForestGreen" :italic t))
46     (t
47      (:bold t)))
48   "Face used for displaying \"*** Action:\" line"
49   :group 'riece-highlight-faces)
50 (defvar riece-ctcp-action-face 'riece-ctcp-action-face)
51
52 (defconst riece-ctcp-action-prefix "*** Action: ")
53
54 (defvar riece-ctcp-ping-time nil)
55 (defvar riece-ctcp-additional-clientinfo nil)
56
57 (defvar riece-dialogue-mode-map)
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 (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)))
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 (riece-mcat "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)))
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 (riece-mcat "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 (riece-mcat "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-ctcp-action-format-message (message &optional global)
175   (riece-with-server-buffer (riece-identity-server
176                              (riece-message-speaker message))
177     (concat
178      (if global
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)))
187      "\n")))
188
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
193                                               riece-server-name)
194                          (riece-make-identity target
195                                               riece-server-name)
196                          string
197                          'action
198                          (riece-identity-equal-no-server
199                           user riece-real-nickname)))))
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 (riece-mcat "CTCP TIME from %s\n")
210                                         user))
211     (riece-insert-change
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)
216      (concat
217       (riece-concat-server-name
218        (format (riece-mcat "CTCP TIME from %s (%s) to %s")
219                user
220                (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
221                (riece-format-identity target-identity t)))
222       "\n"))))
223
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)))
234             (let ((hook
235                    (intern (concat "riece-ctcp-" response "-response-hook")))
236                   (function (intern-soft (concat "riece-handle-ctcp-"
237                                                  response "-response")))
238                   (after-hook
239                    (intern (concat "riece-ctcp-after-" response
240                                    "-response-hook"))))
241               (unless (riece-funcall-ignore-errors
242                        (symbol-name hook)
243                        #'run-hook-with-args-until-success
244                        hook prefix (car targets) message)
245                 (if function
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)
252                                              message)))
253             t)))))
254
255 (defun riece-handle-ctcp-version-response (prefix _target string)
256   (riece-insert-change
257    (list riece-dialogue-buffer riece-others-buffer)
258    (concat
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))
263              string))
264     "\n")))
265
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)))))
270     (riece-insert-change
271      (list riece-dialogue-buffer riece-others-buffer)
272      (concat
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))
277                elapsed))
278       "\n"))))
279
280 (defun riece-handle-ctcp-clientinfo-response (prefix _target string)
281   (riece-insert-change
282    (list riece-dialogue-buffer riece-others-buffer)
283    (concat
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))
288              string))
289     "\n")))
290
291 (defun riece-handle-ctcp-time-response (prefix _target string)
292   (riece-insert-change
293    (list riece-dialogue-buffer riece-others-buffer)
294    (concat
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))
299              string))
300     "\n")))
301
302 (defun riece-command-ctcp-version (target)
303   (interactive
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))))
309
310 (defun riece-command-ctcp-ping (target)
311   (interactive
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)))
318
319 (defun riece-command-ctcp-clientinfo (target)
320   (interactive
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))))
326
327 (defun riece-command-ctcp-action (target action)
328   (interactive
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)
334          (let (message)
335            (beginning-of-line)
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: ")
341                                           (cons message 0))
342                (if (> (forward-line) 0)
343                    (insert "\n")))))))
344   (if (equal action "")
345       (error "No action"))
346   (riece-send-string (format "PRIVMSG %s :\1ACTION %s\1\r\n"
347                              (riece-identity-prefix target)
348                              action))
349   (riece-display-message
350    (riece-make-message (riece-current-nickname) target action 'action t)))
351
352 (defun riece-command-ctcp-time (target)
353   (interactive
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))))
359
360 (defun riece-ctcp-requires ()
361   (if (memq 'riece-highlight riece-addons)
362       '(riece-highlight)))
363
364 (defvar riece-ctcp-dialogue-font-lock-keywords
365   (list (concat "^" riece-time-prefix-regexp "\\("
366                 (regexp-quote riece-ctcp-action-prefix)
367                 ".*\\)$")
368         1 riece-ctcp-action-face t t))
369
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))))
381
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)))
388
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))
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
403 (provide 'riece-ctcp)
404
405 ;;; riece-ctcp.el ends here