File: riece-ctcp.el

package info (click to toggle)
xemacs21-packages 2009.02.17.dfsg.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 116,928 kB
  • ctags: 88,975
  • sloc: lisp: 1,232,060; ansic: 16,570; java: 13,514; xml: 6,477; sh: 4,611; makefile: 4,036; asm: 3,007; perl: 839; cpp: 500; ruby: 257; csh: 96; haskell: 93; awk: 49; python: 47
file content (415 lines) | stat: -rw-r--r-- 15,117 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
;;; riece-ctcp.el --- CTCP (Client To Client Protocol) support
;; Copyright (C) 1998-2003 Daiki Ueno

;; Author: Daiki Ueno <ueno@unixuser.org>
;; Created: 1998-09-28
;; Keywords: IRC, riece

;; This file is part of Riece.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; NOTE: This is an add-on module for Riece.

;;; Code:

(require 'riece-version)
(require 'riece-misc)
(require 'riece-highlight)
(require 'riece-display)
(require 'riece-debug)
(require 'riece-mcat)

(defface riece-ctcp-action-face
  '((((class color)
      (background dark))
     (:foreground "PaleGreen" :italic t))
    (((class color)
      (background light))
     (:foreground "ForestGreen" :italic t))
    (t
     (:bold t)))
  "Face used for displaying \"*** Action:\" line"
  :group 'riece-highlight-faces)
(defvar riece-ctcp-action-face 'riece-ctcp-action-face)

(defconst riece-ctcp-action-prefix "*** Action: ")

(defvar riece-ctcp-ping-time nil)
(defvar riece-ctcp-additional-clientinfo nil)

(defvar riece-dialogue-mode-map)

(defconst riece-ctcp-description
  "CTCP (Client To Client Protocol) support.")

(defun riece-handle-ctcp-request (prefix string)
  (when (and (get 'riece-ctcp 'riece-addon-enabled) prefix string
	     (riece-prefix-nickname prefix))
    (let* ((parameters (riece-split-parameters string))
	   (targets (split-string (car parameters) ","))
	   (message (nth 1 parameters)))
      (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
	  (let ((request (downcase (match-string 1 message))))
	    (if (match-beginning 2)
		(setq message (substring (match-string 2 message) 1)))
	    (let ((hook
		   (intern (concat "riece-ctcp-" request "-request-hook")))
		  (function
		   (intern-soft (concat "riece-handle-ctcp-" request
					"-request")))
		  (after-hook
		   (intern (concat "riece-ctcp-after-" request
				   "-request-hook"))))
	      (unless (riece-funcall-ignore-errors
		       (symbol-name hook)
		       #'run-hook-with-args-until-success
		       hook prefix (car targets) message)
		(if function
		    (riece-funcall-ignore-errors (symbol-name function)
						  function prefix (car targets)
						  message))
		(riece-funcall-ignore-errors (symbol-name after-hook)
					     #'run-hook-with-args-until-success
					     after-hook prefix (car targets)
					     message)))
	    t)))))

(defun riece-handle-ctcp-version-request (prefix target string)
  (let* ((target-identity (riece-make-identity target riece-server-name))
	 (buffer (if (riece-channel-p target)
		     (riece-channel-buffer target-identity)))
	 (user (riece-prefix-nickname prefix)))
    (riece-send-string
     (format "NOTICE %s :\1VERSION %s\1\r\n" user (riece-extended-version)))
    (riece-insert-change buffer (format "CTCP VERSION from %s\n" user))
    (riece-insert-change
     (if (and riece-channel-buffer-mode
	      (not (eq buffer riece-channel-buffer)))
	 (list riece-dialogue-buffer riece-others-buffer)
       riece-dialogue-buffer)
     (concat
      (riece-concat-server-name
       (format (riece-mcat "CTCP VERSION from %s (%s) to %s")
	       user
	       (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
	       (riece-format-identity target-identity t)))
      "\n"))))

(defun riece-handle-ctcp-ping-request (prefix target string)
  (let* ((target-identity (riece-make-identity target riece-server-name))
	 (buffer (if (riece-channel-p target)
		     (riece-channel-buffer target-identity)))
	 (user (riece-prefix-nickname prefix)))
    (riece-send-string
     (if string
	 (format "NOTICE %s :\1PING %s\1\r\n" user string)
       (format "NOTICE %s :\1PING\1\r\n" user)))
    (riece-insert-change buffer (format "CTCP PING from %s\n" user))
    (riece-insert-change
     (if (and riece-channel-buffer-mode
	      (not (eq buffer riece-channel-buffer)))
	 (list riece-dialogue-buffer riece-others-buffer)
       riece-dialogue-buffer)
     (concat
      (riece-concat-server-name
       (format (riece-mcat "CTCP PING from %s (%s) to %s")
	       user
	       (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
	       (riece-format-identity target-identity t)))
      "\n"))))

(defun riece-handle-ctcp-clientinfo-request (prefix target string)
  (let* ((target-identity (riece-make-identity target riece-server-name))
	 (buffer (if (riece-channel-p target)
		     (riece-channel-buffer target-identity)))
	 (user (riece-prefix-nickname prefix)))
    (riece-send-string
     (format "NOTICE %s :\1CLIENTINFO %s\1\r\n"
	     user
	     (let (messages)
	       (mapatoms
		(lambda (atom)
		  (let ((case-fold-search t))
		    (if (and (fboundp atom)
			     (string-match
			      "riece-handle-ctcp-\\(.+\\)-request"
			      (symbol-name atom)))
			(setq messages
			      (cons (match-string 1 (symbol-name atom))
				    messages))))))
	       (mapconcat #'upcase (append messages
					   riece-ctcp-additional-clientinfo)
			  " "))))
    (riece-insert-change buffer (format "CTCP CLIENTINFO from %s\n" user))
    (riece-insert-change
     (if (and riece-channel-buffer-mode
	      (not (eq buffer riece-channel-buffer)))
	 (list riece-dialogue-buffer riece-others-buffer)
       riece-dialogue-buffer)
     (concat
      (riece-concat-server-name
       (format (riece-mcat "CTCP CLIENTINFO from %s (%s) to %s")
	       user
	       (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
	       (riece-format-identity target-identity t)))
      "\n"))))

(defun riece-handle-ctcp-action-request (prefix target string)
  (let ((buffer (if (riece-channel-p target)
		    (riece-channel-buffer (riece-make-identity
					   target riece-server-name))))
	(user (riece-prefix-nickname prefix)))
    (riece-insert buffer (concat riece-ctcp-action-prefix
				 (riece-format-identity
				  (riece-make-identity user riece-server-name)
				  t)
				 " " string
				 "\n"))
    (riece-insert
     (if (and riece-channel-buffer-mode
	      (not (eq buffer riece-channel-buffer)))
	 (list riece-dialogue-buffer riece-others-buffer)
       riece-dialogue-buffer)
     (concat (riece-concat-server-name
	      (concat riece-ctcp-action-prefix
		      (riece-format-identity
		       (riece-make-identity target riece-server-name)
		       t)
		      ": "
		      (riece-format-identity
		       (riece-make-identity user riece-server-name)
		       t)
		      " " string)) "\n"))))

(defun riece-handle-ctcp-time-request (prefix target string)
  (let* ((target-identity (riece-make-identity target riece-server-name))
	 (buffer (if (riece-channel-p target)
		     (riece-channel-buffer target-identity)))
	 (user (riece-prefix-nickname prefix))
	 (time (format-time-string "%c")))
    (riece-send-string
     (format "NOTICE %s :\1TIME %s\1\r\n" user time))
    (riece-insert-change buffer (format (riece-mcat "CTCP TIME from %s\n")
					user))
    (riece-insert-change
     (if (and riece-channel-buffer-mode
	      (not (eq buffer riece-channel-buffer)))
	 (list riece-dialogue-buffer riece-others-buffer)
       riece-dialogue-buffer)
     (concat
      (riece-concat-server-name
       (format (riece-mcat "CTCP TIME from %s (%s) to %s")
	       user
	       (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
	       (riece-format-identity target-identity t)))
      "\n"))))

(defun riece-handle-ctcp-response (prefix string)
  (when (and (get 'riece-ctcp 'riece-addon-enabled) prefix string
	     (riece-prefix-nickname prefix))
    (let* ((parameters (riece-split-parameters string))
	   (targets (split-string (car parameters) ","))
	   (message (nth 1 parameters)))
      (if (string-match "\1\\([^ ]+\\)\\( .+\\)?\1" message)
	  (let ((response (downcase (match-string 1 message))))
	    (if (match-beginning 2)
		(setq message (substring (match-string 2 message) 1)))
	    (let ((hook
		   (intern (concat "riece-ctcp-" response "-response-hook")))
		  (function (intern-soft (concat "riece-handle-ctcp-"
						 response "-response")))
		  (after-hook
		   (intern (concat "riece-ctcp-after-" response
				   "-response-hook"))))
	      (unless (riece-funcall-ignore-errors
		       (symbol-name hook)
		       #'run-hook-with-args-until-success
		       hook prefix (car targets) message)
		(if function
		    (riece-funcall-ignore-errors
		     (symbol-name function)
		     function prefix (car targets) message))
		(riece-funcall-ignore-errors (symbol-name after-hook)
					     #'run-hook-with-args-until-success
					     after-hook prefix (car targets)
					     message)))
	    t)))))

(defun riece-handle-ctcp-version-response (prefix target string)
  (riece-insert-change
   (list riece-dialogue-buffer riece-others-buffer)
   (concat
    (riece-concat-server-name
     (format (riece-mcat "CTCP VERSION for %s (%s) = %s")
	     (riece-prefix-nickname prefix)
	     (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
	     string))
    "\n")))

(defun riece-handle-ctcp-ping-response (prefix target string)
  (let* ((now (current-time))
	 (elapsed (+ (* 65536 (- (car now) (car riece-ctcp-ping-time)))
		     (- (nth 1 now) (nth 1 riece-ctcp-ping-time)))))
    (riece-insert-change
     (list riece-dialogue-buffer riece-others-buffer)
     (concat
      (riece-concat-server-name
       (format (riece-mcat "CTCP PING for %s (%s) = %d sec")
	       (riece-prefix-nickname prefix)
	       (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
	       elapsed))
      "\n"))))

(defun riece-handle-ctcp-clientinfo-response (prefix target string)
  (riece-insert-change
   (list riece-dialogue-buffer riece-others-buffer)
   (concat
    (riece-concat-server-name
     (format (riece-mcat "CTCP CLIENTINFO for %s (%s) = %s")
	     (riece-prefix-nickname prefix)
	     (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
	     string))
    "\n")))

(defun riece-handle-ctcp-time-response (prefix target string)
  (riece-insert-change
   (list riece-dialogue-buffer riece-others-buffer)
   (concat
    (riece-concat-server-name
     (format (riece-mcat "CTCP TIME for %s (%s) = %s")
	     (riece-prefix-nickname prefix)
	     (riece-strip-user-at-host (riece-prefix-user-at-host prefix))
	     string))
    "\n")))

(defun riece-command-ctcp-version (target)
  (interactive
   (list (riece-completing-read-identity
	  (riece-mcat "Channel/User: ")
	  (riece-get-identities-on-server (riece-current-server-name)))))
  (riece-send-string (format "PRIVMSG %s :\1VERSION\1\r\n"
			     (riece-identity-prefix target))))

(defun riece-command-ctcp-ping (target)
  (interactive
   (list (riece-completing-read-identity
	  (riece-mcat "Channel/User: ")
	  (riece-get-identities-on-server (riece-current-server-name)))))
  (riece-send-string (format "PRIVMSG %s :\1PING\1\r\n"
			     (riece-identity-prefix target)))
  (setq riece-ctcp-ping-time (current-time)))

(defun riece-command-ctcp-clientinfo (target)
  (interactive
   (list (riece-completing-read-identity
	  (riece-mcat "Channel/User: ")
	  (riece-get-identities-on-server (riece-current-server-name)))))
  (riece-send-string (format "PRIVMSG %s :\1CLIENTINFO\1\r\n"
			     (riece-identity-prefix target))))

(defun riece-command-ctcp-action (target action)
  (interactive
   (list (if current-prefix-arg
	     (riece-completing-read-identity
	      (riece-mcat "Channel/User: ")
	      (riece-get-identities-on-server (riece-current-server-name)))
	   riece-current-channel)
	 (let (message)
	   (beginning-of-line)
	   (setq message (buffer-substring (point)
					   (progn (end-of-line) (point))))
	   (if (equal message "")
	       (read-string (riece-mcat "Action: "))
	     (prog1 (read-from-minibuffer (riece-mcat "Action: ")
					  (cons message 0))
	       (let ((next-line-add-newlines t))
		 (next-line 1)))))))
  (if (equal action "")
      (error "No action"))
  (riece-send-string (format "PRIVMSG %s :\1ACTION %s\1\r\n"
			     (riece-identity-prefix target)
			     action))
  (let ((buffer (riece-channel-buffer target)))
    (riece-insert
     buffer
     (concat riece-ctcp-action-prefix
	     (riece-identity-prefix (riece-current-nickname)) " " action "\n"))
    (riece-insert
     (if (and riece-channel-buffer-mode
	      (not (eq buffer riece-channel-buffer)))
	 (list riece-dialogue-buffer riece-others-buffer)
       riece-dialogue-buffer)
     (concat
      (riece-with-server-buffer (riece-identity-server target)
	(riece-concat-server-name
	 (concat riece-ctcp-action-prefix
		 (riece-format-identity target t) ": "
		 (riece-identity-prefix (riece-current-nickname)) " " action)))
      "\n"))))

(defun riece-command-ctcp-time (target)
  (interactive
   (list (riece-completing-read-identity
	  (riece-mcat "Channel/User: ")
	  (riece-get-identities-on-server (riece-current-server-name)))))
  (riece-send-string (format "PRIVMSG %s :\1TIME\1\r\n"
			     (riece-identity-prefix target))))

(defun riece-ctcp-requires ()
  (if (memq 'riece-highlight riece-addons)
      '(riece-highlight)))

(defvar riece-ctcp-dialogue-font-lock-keywords
  (list (concat "^" riece-time-prefix-regexp "\\("
		(regexp-quote riece-ctcp-action-prefix)
		".*\\)$")
	1 riece-ctcp-action-face t t))

(defun riece-ctcp-insinuate ()
  (add-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
  (add-hook 'riece-notice-hook 'riece-handle-ctcp-response)
  (if (memq 'riece-highlight riece-addons)
      (setq riece-dialogue-font-lock-keywords
	    (cons riece-ctcp-dialogue-font-lock-keywords
		  riece-dialogue-font-lock-keywords))))

(defun riece-ctcp-uninstall ()
  (remove-hook 'riece-privmsg-hook 'riece-handle-ctcp-request)
  (remove-hook 'riece-notice-hook 'riece-handle-ctcp-response)
  (setq riece-dialogue-font-lock-keywords
	(delq riece-ctcp-dialogue-font-lock-keywords
	      riece-dialogue-font-lock-keywords)))

(defun riece-ctcp-enable ()
  (define-key riece-dialogue-mode-map "\C-cv" 'riece-command-ctcp-version)
  (define-key riece-dialogue-mode-map "\C-cp" 'riece-command-ctcp-ping)
  (define-key riece-dialogue-mode-map "\C-ca" 'riece-command-ctcp-action)
  (define-key riece-dialogue-mode-map "\C-cc" 'riece-command-ctcp-clientinfo)
  (define-key riece-dialogue-mode-map "\C-ct" 'riece-command-ctcp-time))

(defun riece-ctcp-disable ()
  (define-key riece-dialogue-mode-map "\C-cv" nil)
  (define-key riece-dialogue-mode-map "\C-cp" nil)
  (define-key riece-dialogue-mode-map "\C-ca" nil)
  (define-key riece-dialogue-mode-map "\C-cc" nil)
  (define-key riece-dialogue-mode-map "\C-ct" nil))

(provide 'riece-ctcp)

;;; riece-ctcp.el ends here