Source

liece / lisp / liece-ctcp.el

Full commit
  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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
;;; liece-ctcp.el --- CTCP handlers and commands.
;; Copyright (C) 1998-2000 Daiki Ueno

;; Author: Daiki Ueno <ueno@unixuser.org>
;; Created: 1998-09-28
;; Revised: 1998-11-25
;; Keywords: IRC, liece, CTCP

;; This file is part of Liece.

;; 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., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.


;;; Commentary:
;; 

;;; Code:

(eval-when-compile (require 'liece-inlines))
(eval-when-compile (require 'liece-misc))

(require 'liece-handler)

;; (require 'pccl)

;; (if-broken ccl-usable
;;     (require 'liece-q-el)
;;   (require 'liece-q-ccl))
(require 'liece-q-el)

(require 'liece-x-face)

(autoload 'liece-ctcp-dcc-message "liece-dcc")

(eval-and-compile
  (defconst liece-ctcp-supported-symbols
    '(version userinfo clientinfo ping time x-face comment help)))

(defun liece-ctcp-make-menu-command-wrapper (symbol)
  (fset (intern (format "liece-menu-callback-ctcp-%s" symbol))
	`(lambda ()
	   (interactive)
	   (dolist (nick liece-nick-region-nicks)
	     (funcall (symbol-function
		       (intern (format "liece-command-ctcp-%s" ',symbol)))
		      nick)))))

(dolist (symbol liece-ctcp-supported-symbols)
  (liece-ctcp-make-menu-command-wrapper symbol))
	     
(defvar liece-ctcp-message
  (eval-when-compile
    (concat liece-client-prefix "%s(%s) = %s"))
  "Message in which info of other clients is displayed.")

(defvar liece-ctcp-buffer (append liece-D-buffer liece-O-buffer))

(defvar liece-ctcp-ping-time '(0 0 0))

(defvar liece-ctcp-last-command nil
  "The last command executed.")

(defvar liece-ctcp-last-nick nil
  "The last nick being queried.")

(defconst liece-ctcp-error-message "Unrecognized command: '%s'"
  "Error message given to anyone asking wrong CLIENT data.")

(defun liece-ctcp-last-nick-maybe-change (prefix rest)
  (if (equal prefix liece-ctcp-last-nick)
      (setq liece-ctcp-last-nick rest))
  nil)

(defun liece-ctcp-last-nick-maybe-reset (prefix rest)
  (if (equal prefix liece-ctcp-last-nick)
      (setq liece-ctcp-last-nick nil)))

(add-hook 'liece-nick-hook 'liece-ctcp-last-nick-maybe-change t)
(add-hook 'liece-quit-hook 'liece-ctcp-last-nick-maybe-reset)
  
(defcustom liece-ctcp-file-save-directory liece-directory
  "Directory to save received files."
  :type 'directory
  :group 'liece-ctcp)
  
(liece-handler-define-backend "ctcp-message")

(defmacro liece-register-ctcp-message-handler (name)
  `(liece-handler-define-function
    ,name '(from chnl data "ctcp-message")
    ',(intern (format "liece-ctcp-%s-message" name))))

(liece-register-ctcp-message-handler "version")
(liece-register-ctcp-message-handler "userinfo")
(liece-register-ctcp-message-handler "clientinfo")
(liece-register-ctcp-message-handler "ping")
(liece-register-ctcp-message-handler "time")
(liece-register-ctcp-message-handler "file")
(liece-register-ctcp-message-handler "x-face")
(liece-register-ctcp-message-handler "comment")
(liece-register-ctcp-message-handler "help")
(liece-register-ctcp-message-handler "action")
(liece-register-ctcp-message-handler "dcc")
(liece-register-ctcp-message-handler "errmsg")

(defun* liece-ctcp-message (from chnl rest)
  (or (string-match "^\\([^\001]*\\)\001\\([^\001]*\\)\001" rest)
      (return-from liece-ctcp-message))
  (let (hook after-hook data message)
    (setq data (match-string 2 rest)
	  rest (concat
		(match-string 1 rest)
		(substring rest (match-end 0))))
    (if (string-match "^\\([^ ]*\\) *:?" data)
	(setq message (downcase (match-string 1 data))
	      data (substring data (match-end 0)))
      (setq message "errmsg"
	    data (_ "Couldn't figure out what was said.")))
    (setq hook
	  (intern-soft
	   (concat "liece-ctcp-" message "-hook"))
	  after-hook
	  (intern-soft
	   (concat "liece-after-ctcp-" message "-hook")))
    (if (run-hook-with-args-until-success hook from chnl data)
	(return-from liece-ctcp-message rest))
    (let ((func
	   (liece-handler-find-function
	    message '(from chnl data) "ctcp-message")))
      (if func
	  (funcall func from chnl data)
	(liece-ctcp-messages message from chnl data))
      (run-hook-with-args after-hook from chnl data))
    rest))

(defun liece-ctcp-messages (message from chnl rest)
  (liece-send "NOTICE %s :\001ERRMSG %s :%s\001"
	       from (upcase message)
	       (format liece-ctcp-error-message
		       (upcase message)))
  (setq chnl (liece-channel-virtual chnl))
  (liece-ctcp-insert (upcase message) from chnl rest))

(defun liece-ctcp-action-message (from chnl rest)
  "CTCP ACTION handler."
  (let ((liece-message-target (liece-channel-virtual chnl))
	(liece-message-speaker from)
	(liece-message-type 'action))
    (liece-display-message rest)))

(defun liece-ctcp-insert (message from &optional chnl rest)
  (if (or (null chnl)
	  (liece-nick-equal chnl liece-real-nickname))
      (liece-message "%s query from %s." message from)
    (liece-message "%s query from %s (%s)." message from chnl)
    (liece-insert-client
     (liece-pick-buffer chnl)
     (format "%s query from %s%s\n"
	     message from (if rest (concat ":" rest) "")))))

(defun liece-ctcp-version-message (from chnl rest)
  "CTCP VERSION handler."
  (liece-send "NOTICE %s :\001VERSION %s :\001"
	      from (liece-version))
  (setq chnl (liece-channel-virtual chnl))
  (liece-ctcp-insert "VERSION" from chnl rest))

(defun liece-ctcp-userinfo-message (from chnl rest)
  "CTCP USERINFO handler."
  (liece-send "NOTICE %s :\001USERINFO %s\001"
	      from liece-ctcp-userinfo)
  (setq chnl (liece-channel-virtual chnl))
  (liece-ctcp-insert "USERINFO" from chnl))

(defun liece-ctcp-clientinfo-message (from chnl rest)
  "CTCP CLIENTINFO handler."
  (liece-send "NOTICE %s :\001CLIENTINFO %s\001"
	      from
	      (eval-when-compile
		(mapconcat 
		 (lambda (symbol) (upcase (symbol-name symbol)))
		 liece-ctcp-supported-symbols " ")))
  (setq chnl (liece-channel-virtual chnl))
  (liece-ctcp-insert "CLIENTINFO" from chnl))

(defvar liece-ctcp-help-message
  "This is a help message for CTCP requests.
\"VERSION\" gives version of this client.
\"USERINFO\" gives user supplied information if any.
\"CLIENTINFO\" gives commands this client knows.
\"PING\" returns the arguments it receives.
\"TIME\" tells you the time on the user's host.
\"FILE\" send a small file via IRC messages.
\"X-FACE\" gives you user supplied X-Face.
\"COMMENT\" returns string sent by other person.
\"HELP\" gives this help message"
  "Help message for CTCP requests.")
  
(defun liece-ctcp-help-message (from chnl rest)
  "CTCP HELP handler."
  (liece-send
   "NOTICE %s :\001HELP %s\001"
   from (liece-quote-encode-string liece-ctcp-help-message))
  (setq chnl (liece-channel-virtual chnl))
  (liece-ctcp-insert "HELP" from chnl))

(defun liece-ctcp-comment-message (from chnl rest)
  "CTCP COMMENT handler."
  (setq chnl (liece-channel-virtual chnl))
  (liece-ctcp-insert "COMMENT" from chnl))

(defun liece-ctcp-ping-message (from chnl rest)
  "CTCP PING handler."
  (liece-send "NOTICE %s :\001PING %s\001" from rest)
  (setq chnl (liece-channel-virtual chnl))
  (liece-ctcp-insert "PING" from chnl))

(defun liece-ctcp-time-message (from chnl rest)
  "CTCP TIME handler."
  (liece-send "NOTICE %s :\001TIME %s\001"
	      from (funcall liece-format-time-function
			    (current-time)))
  (setq chnl (liece-channel-virtual chnl))
  (liece-ctcp-insert "TIME" from chnl))

(defun liece-ctcp-x-face-message (from chnl rest)
  "CTCP X-FACE handler."
  (liece-send "NOTICE %s :\001X-FACE %s\001"
	      from liece-ctcp-x-face)
  (setq chnl (liece-channel-virtual chnl))
  (liece-ctcp-insert "X-FACE" from chnl))

(liece-handler-define-backend "ctcp-notice")

(defmacro liece-register-ctcp-notice-handler (name)
  `(liece-handler-define-function
    ,name '(prefix rest "ctcp-notice")
    ',(intern (format "liece-ctcp-%s-notice" name))))

(liece-register-ctcp-notice-handler "version")
(liece-register-ctcp-notice-handler "userinfo")
(liece-register-ctcp-notice-handler "clientinfo")
(liece-register-ctcp-notice-handler "ping")
(liece-register-ctcp-notice-handler "time")
(liece-register-ctcp-notice-handler "file")
(liece-register-ctcp-notice-handler "x-face")
(liece-register-ctcp-notice-handler "comment")
(liece-register-ctcp-notice-handler "help")
(liece-register-ctcp-notice-handler "dcc")
(liece-register-ctcp-notice-handler "errmsg")

(defun* liece-ctcp-notice (prefix rest)
  (or (string-match "^\\([^\001]*\\)\001\\([^\001]*\\)\001" rest)
      (return-from liece-ctcp-notice))
  (let (hook after-hook data message)
    (setq data (match-string 2 rest)
	  rest (concat
		(match-string 1 rest)
		(substring rest (match-end 0))))
    (if (string-match "^\\([^ ]*\\) *:?" data)
	(setq message (downcase (match-string 1 data))
	      data (substring data (match-end 0)))
      (setq message "errmsg"
	    data (_ "Couldn't figure out what was said.")))
    (setq hook
	  (intern-soft
	   (concat "liece-ctcp-" message "-notice-hook"))
	  after-hook
	  (intern-soft
	   (concat "liece-after-ctcp-" message "-notice-hook")))
    (if (run-hook-with-args-until-success hook prefix data)
	(return-from liece-ctcp-notice rest))
    (let ((func
	   (liece-handler-find-function
	    message '(prefix data) "ctcp-notice")))
      (if func
	  (funcall func prefix data)
	(liece-ctcp-notices message prefix data)))
    (run-hook-with-args after-hook prefix data)
    rest))

(defun liece-ctcp-notices (message prefix rest)
  (liece-message
   (_ "Unknown ctcp notice \":%s %s %s\"")
   prefix (upcase message) rest))

(liece-handler-define-backend "ctcp-file")

(defmacro liece-register-file-handler (name)
  `(liece-handler-define-function
    ,name '(prefix name data "ctcp-file")
    ',(intern (format "liece-file-%s" name))))

(liece-register-file-handler "start")
(liece-register-file-handler "cont")
(liece-register-file-handler "end")

(defun* liece-ctcp-file-notice (prefix rest)
  (when liece-file-accept
    (multiple-value-bind (message name data)
	(liece-split-line rest)
      (setq message (downcase message))
      (let ((hook
	     (intern-soft
	      (concat "liece-file-" message "-hook")))
	    (after-hook
	     (intern-soft
	      (concat "liece-after-file-" message "-hook")))
	    func)
	(if (run-hook-with-args-until-success hook prefix name)
	    (return-from liece-ctcp-file-notice))
	(setq func (liece-handler-find-function
		    message '(prefix name data) 'ctcp-file))
	(if func
	    (funcall func prefix name data)
	  (liece-file-notices message prefix name data))
	(run-hook-with-args after-hook prefix name)))))

(defun liece-file-notices (message prefix name data)
  (liece-message
   (_ "Unknown FILE message \":%s %s %s %s\"")
   prefix (upcase message) name data))

(defun liece-file-start (prefix name data)
  "CTCP FILE start handler."
  (save-excursion
    (set-buffer
     (liece-get-buffer-create
      (format " *ctcp-file:%s*" name)))
    (buffer-disable-undo)
    (set-buffer-multibyte nil)
    (erase-buffer)
    (insert data)))

(defun liece-file-cont (prefix name data)
  "CTCP FILE cont handler."
  (save-excursion
    (set-buffer
     (liece-get-buffer-create
      (format " *ctcp-file:%s*" name)))
    (goto-char (point-max))
    (insert data)))

(defun liece-file-end (prefix name data)
  "CTCP FILE cont handler."
  (save-excursion
    (set-buffer
     (liece-get-buffer-create
      (format " *ctcp-file:%s*" name)))
    (goto-char (point-max))
    (insert data)
    (liece-quote-decode-region (point-min)(point-max))
    (goto-char (point-min))
    (when (or (null liece-file-confirm-save)
	      (y-or-n-p "Save file? "))
      (or (file-directory-p liece-ctcp-file-save-directory)
	  (make-directory liece-ctcp-file-save-directory))
      (write-region-as-binary
       (point-min)(point-max)
       (expand-file-name
	(file-name-nondirectory
	 (concat name "-" prefix))
	liece-ctcp-file-save-directory))
      (kill-buffer (current-buffer)))))

(defun liece-ctcp-version-insert (buffer prefix name
					 &optional version environment)
  (or (listp buffer)
      (setq buffer (list buffer)))
  (liece-insert buffer
		(concat (format liece-ctcp-message
				"VERSION" prefix "")
			name "\n"))
  (when version
    (liece-insert buffer
		  (concat (format liece-ctcp-message
				  "VERSION" prefix "")
			  "\t" version
			  (if environment
			      (concat " " environment))
			  "\n"))))

(defun liece-ctcp-version-notice (prefix rest)
  "CTCP VERSION reply handler."
  (if (null rest)
      (liece-message (_ "Empty CLIENT version notice from \"%s\".") prefix)
    (cond
     ((string-match "^\\([^:]*\\):\\([^:]+\\):?\\([^:]*\\)" rest)
      (liece-ctcp-version-insert liece-ctcp-buffer
				 prefix (match-string 1 rest)
				 (match-string 2 rest)
				 (match-string 3 rest)))
     ((string-match "^\\([^:]*\\):\\(.*\\)" rest)
      (liece-ctcp-version-insert liece-ctcp-buffer
				 prefix (match-string 1 rest)))
     (t
      (liece-ctcp-version-insert liece-ctcp-buffer prefix rest)))))

(defun liece-ctcp-clientinfo-notice (prefix rest)
  "CTCP CLIENTINFO reply handler."
  (liece-insert liece-ctcp-buffer
		 (format (concat liece-ctcp-message "\n")
			 "CLIENTINFO" prefix rest)))

(defun liece-ctcp-userinfo-notice (prefix rest)
  "CTCP USERINFO reply handler."
  (liece-insert liece-ctcp-buffer
		 (format (concat liece-ctcp-message "\n")
			 "USERINFO" prefix rest)))

(defun liece-ctcp-help-notice (prefix rest)
  "CTCP HELP reply handler."
  (liece-insert liece-ctcp-buffer
		 (format (concat liece-ctcp-message "\n")
			 "HELP" prefix rest)))

(defun liece-ctcp-x-face-notice (prefix rest)
  "CTCP X-FACE reply handler."
  (let ((buffer liece-ctcp-buffer))
    (liece-insert buffer
		   (format liece-ctcp-message
			   "X-FACE" prefix ""))
    (if (and liece-use-x-face
	     (string-match "[^ \t]" rest))
	(liece-x-face-insert
	 buffer (replace-in-string rest "[ \t\r\n]+" "") prefix)
      (liece-insert buffer rest))
    (let (liece-display-time)
      (liece-insert buffer "\n"))))

(defun liece-ctcp-errmsg-notice (prefix rest)
  "CTCP ERRMSG reply handler."
  (liece-insert liece-ctcp-buffer
		 (format (concat liece-ctcp-message "\n")
			 "ERRMSG" prefix rest)))

(defun liece-ctcp-comment-notice (from rest)
  "CTCP COMMENT reply handler."
  (liece-insert liece-ctcp-buffer
		 (format (concat liece-ctcp-message "\n")
			 "COMMENT" from rest))
  (liece-message "COMMENT query from %s." from))

(defmacro liece-ctcp-prepare-ping-seconds (timenow)
  `(format (_ "%d sec")
	   (+ (* 65536 (- (car ,timenow) (car liece-ctcp-ping-time)))
	      (- (cadr ,timenow) (cadr liece-ctcp-ping-time)))))

(defun liece-ctcp-ping-notice (from rest)
  "CTCP PING reply handler."
  (let ((timenow (current-time)))
    (liece-insert liece-ctcp-buffer
		   (format (concat liece-ctcp-message "\n")
			   "PING" from
			   (liece-ctcp-prepare-ping-seconds timenow)))))

(defun liece-ctcp-time-notice (from rest)
  "CTCP TIME reply handler."
  (liece-insert liece-ctcp-buffer
		(format (concat liece-ctcp-message "\n")
			"TIME" from rest)))

(defmacro liece-complete-client ()
  '(let ((completion-ignore-case t) (nick liece-ctcp-last-nick))
     (liece-minibuffer-completing-read
      (_ "Whose client: ") liece-nick-alist nil nil nil nil
      (if nick (liece-channel-virtual nick)))))

(defun liece-minibuffer-complete-client-query ()
  (let* ((alist
	  (eval-when-compile
	    (list-to-alist
	     (mapcar
	      (lambda (symbol) (downcase (symbol-name symbol)))
	      liece-ctcp-supported-symbols))))
	 (candidate (liece-minibuffer-prepare-candidate))
	 (completion (try-completion candidate alist))
	 (all (all-completions candidate alist)))
    (liece-minibuffer-finalize-completion completion candidate all)))

(defmacro liece-complete-query ()
  '(let ((completion-ignore-case t)
	 (liece-minibuffer-complete-function
	  (function liece-minibuffer-complete-client-query)))
     (read-from-minibuffer
      (_ "Which query: ") liece-ctcp-last-command
      liece-minibuffer-map)))

(defun liece-ctcp-make-command-wrapper (symbol)
  (fset (intern (format "liece-command-ctcp-%s" symbol))
	`(lambda (client)
	   (interactive (list (liece-complete-client)))
	   (setq client (liece-channel-real client)
		 liece-ctcp-last-nick client
		 ,@(if (eq symbol 'ping)
		       '(liece-ctcp-ping-time
			 (current-time))))
	   (liece-send "PRIVMSG %s :\001%s\001"
		       client (upcase (symbol-name ',symbol))))))

(dolist (symbol liece-ctcp-supported-symbols)
  (liece-ctcp-make-command-wrapper symbol))

(defun liece-command-ctcp-action (&optional arg)
  "Send CTCP action."
  (interactive
   (if current-prefix-arg
       (list current-prefix-arg)))
  (let ((completion-ignore-case t)
	(liece-message-type 'action)
	message)
    (if arg
	(setq liece-privmsg-partner 
	      (liece-channel-virtual
	       (liece-minibuffer-completing-read 
		(_ "To whom: ")
		(append liece-nick-alist liece-channel-alist)
		nil nil nil nil liece-privmsg-partner))))
    (beginning-of-line)
    (setq message (buffer-substring (point)(progn (end-of-line)(point))))
    (if (string= message "")
	(setq message (read-string "Action: "))
      (liece-next-line 1))
    (liece-send "PRIVMSG %s :\001ACTION %s\001"
		 (if arg
		     liece-privmsg-partner
		   (liece-channel-real liece-current-channel))
		 message)
    (if arg
	(liece-own-private-message message)
      (liece-own-channel-message message))))

(define-obsolete-function-alias 'liece-command-send-action
  'liece-command-ctcp-action)

(defun liece-command-ctcp-generic (nick command)
  "Ask about someones client clientinfo."
  (interactive (list (liece-complete-client) (liece-complete-query)))
  (setq nick (liece-channel-real nick)
	liece-ctcp-last-nick nick
	liece-ctcp-last-command command)
  (if (string-equal-ignore-case liece-ctcp-last-command "ping")
      (setq liece-ctcp-ping-time (current-time)))
  (liece-send "PRIVMSG %s :\001%s\001" nick command))

(defun liece-command-ctcp-userinfo-from-minibuffer (info)
  "Ask about someones client clientinfo."
  (interactive
   (list (read-from-minibuffer "New userinfo: " liece-ctcp-userinfo)))
  (setq liece-ctcp-userinfo info))

(defun liece-command-ctcp-x-face-from-xbm-file (file)
  (interactive "fXBM File: ")
  (let (data)
    (and (file-exists-p file) (file-readable-p file)
	 (setq data (liece-x-face-encode file))
	 (setq liece-ctcp-x-face
	       (replace-in-string (cadr (nth 3 data)) "[ \t\n]" "")))))

(defun liece-command-send-file (file to)
  "Send a file to given  user."
  (interactive "fFile name: \nsTo whom: ")
  (save-excursion
    (set-buffer (liece-get-buffer-create (format " *ctcp-file:%s*" file)))
    (buffer-disable-undo)
    (set-buffer-multibyte nil)
    (erase-buffer)
    (insert-file-contents-as-binary file)
    (liece-quote-encode-region (point-min)(point-max))
    (goto-char (point-min))
    (let ((bound (min (point-max) (+ 80 (point))))
	  (liece-mime-charset-for-write 'binary))
      (liece-send
       "NOTICE %s :\001FILE START %s :%s\001"
       to file (buffer-substring (point) bound))
      (goto-char bound)
      (while (not (eobp))
	(if (= 1 (mod (point) 800))
	    (sit-for 1))
	(setq bound (min (point-max) (+ 80 (point))))
	(liece-send "NOTICE %s :\001FILE CONT %s :%s\001"
		    to file (buffer-substring (point) bound))
	(goto-char bound)))
    (liece-send "NOTICE %s :\001FILE END %s : \001" to file)
    (kill-buffer (current-buffer))))

(provide 'liece-ctcp)

;;; liece-ctcp.el ends here