Source

liece / lisp / liece-channel.el

  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
;;; liece-channel.el --- Various facility for channel operation.
;; Copyright (C) 1998-2000 Daiki Ueno

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

;; 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))

(eval-when-compile (require 'liece-clfns))

(defconst liece-channel-regexp "[+&#!]")
(defconst liece-channel-modeless-regexp "[+!]")

(defvar liece-default-channel-representation-format "%s+%s")

(defconst liece-dcc-channel-representation-format "=%s")

(define-widget 'liece-channel-push-button 'push-button
  "A channel button."
  :action 'liece-channel-push-button-action)

(defun liece-channel-push-button-action (widget &optional event)
  (let ((chnl (liece-channel-virtual (widget-value widget))))
    (if (or (liece-channel-member chnl liece-current-channels)
	    (y-or-n-p (format "Do you really join %s? " chnl)))
	(liece-command-join chnl))))

;;; Reader conventions
(defun liece-channel-p (chnl)
  (string-match (concat "^" liece-channel-regexp) chnl))

(defun liece-channel-modeless-p (chnl)
  (string-match (concat "^" liece-channel-modeless-regexp) chnl))

(defun liece-channel-equal (c1 c2)
  (string-equal-ignore-case c1 c2))

(defun liece-channel-member (chnl chnls)
  "Return non-nil if CHNL is member of CHNLS."
  (member-if
   (lambda (item)
     (and (stringp item) (liece-channel-equal chnl item)))
   chnls))

(defun liece-channel-unread-p (chnl)
  "Return non-nil if CHNL is unread channel."
  (member-if
   (lambda (item)
     (and (stringp item) (liece-channel-equal chnl item)))
   liece-channel-unread-list))

(defun liece-channel-get-nicks (&optional chnl)
  "Return CHNL or current channels's members as list."
  (get (intern (or chnl liece-current-channel) liece-obarray) 'nick))

(defun liece-channel-get-operators (&optional chnl)
  "Return CHNL or current channels's operators as list."
  (get (intern (or chnl liece-current-channel) liece-obarray) 'oper))

(defun liece-channel-get-voices (&optional chnl)
  "Return CHNL or current channels's voices as list."
  (get (intern (or chnl liece-current-channel) liece-obarray) 'voice))

(defun liece-channel-get-topic (&optional chnl)
  "Return CHNL or current channels's topic."
  (get (intern (or chnl liece-current-channel) liece-obarray) 'topic))

(defun liece-channel-get-modes (&optional chnl)
  "Return CHNL or current channels's mode."
  (get (intern (or chnl liece-current-channel) liece-obarray) 'mode))

(defun liece-channel-get-bans (&optional chnl)
  "Return CHNL or current channels's ban list."
  (get (intern (or chnl liece-current-channel) liece-obarray) 'ban))

(defun liece-channel-get-invites (&optional chnl)
  "Return CHNL or current channels's invite list."
  (get (intern (or chnl liece-current-channel) liece-obarray) 'invite))

(defun liece-channel-get-exceptions (&optional chnl)
  "Return CHNL or current channels's exception list."
  (get (intern (or chnl liece-current-channel) liece-obarray) 'exception))

;;; Channel status functions
(defun liece-channel-remove (channel channels)
  "Remove CHANNEL from CHANNELS."
  (remove-if
   (lambda (item)
     (and (stringp item) (liece-channel-equal channel item)))
   channels))

(defun liece-channel-delete (channel channels)
  "Delete CHANNEL from CHANNELS."
  (delete-if
   (lambda (item)
     (and (stringp item) (liece-channel-equal channel item)))
   channels))

(defun liece-channel-set-topic (topic &optional channel)
  "Set CHANNEL's topic."
  (put (intern (or channel liece-current-channel) liece-obarray)
       'topic topic))

(defun liece-channel-add-mode (mode &optional channel)
  "Add MODE to CHANNEL.
MODE is a string splitted into characters one by one."
  (let ((modes
	 (liece-string-to-list
	  (or (liece-channel-get-modes channel)
	      ""))))
    (or (memq mode modes)
	(push mode modes))
    (put (intern (or channel liece-current-channel) liece-obarray)
	 'mode (mapconcat #'char-to-string modes ""))))

(defun liece-channel-remove-mode (mode &optional channel)
  "Remove MODE from CHANNEL.
MODE is a string splitted into characters one by one."
  (let ((modes
	 (liece-string-to-list
	  (or (liece-channel-get-modes channel)
	      ""))))
    (delq mode modes)
    (put (intern (or channel liece-current-channel) liece-obarray)
	 'mode (mapconcat #'char-to-string modes ""))))

(defun liece-channel-set-mode (channel mode flag)
  "Add or remove channel MODE of CHANNEL.
MODE is a string splitted into characters one by one.
If FLAG is non-nil, given modes are added to the channel.
Otherwise they are removed from the channel."
  (if flag
      (liece-channel-add-mode mode channel)
    (liece-channel-remove-mode mode channel)))

(defun liece-channel-add-ban (pattern &optional channel)
  "Add ban PATTERN to CHANNEL."
  (let ((patterns (liece-channel-get-bans channel)))
    (or (string-list-member-ignore-case pattern patterns)
	(push pattern patterns))
    (put (intern (or channel liece-current-channel) liece-obarray)
	 'ban patterns)))

(defun liece-channel-remove-ban (pattern &optional channel)
  "Remove ban PATTERN from CHANNEL."
  (let ((patterns
	 (remove-if
	  (lambda (elm) (string-equal pattern elm))
	  (liece-channel-get-bans channel))))
    (put (intern (or channel liece-current-channel) liece-obarray)
	 'ban patterns)))

(defun liece-channel-set-ban (channel pattern flag)
  "Add or remove ban PATTERN to CHANNEL.
If FLAG is non-nil, given ban patterns are added to the channel.
Otherwise they are removed from the channel."
  (if flag
      (liece-channel-add-ban pattern channel)
    (liece-channel-remove-ban pattern channel)))

(defun liece-channel-add-exception (pattern &optional channel)
  "Add exception PATTERN to CHANNEL."
  (let ((patterns (liece-channel-get-exceptions channel)))
    (or (string-list-member-ignore-case pattern patterns)
	(push pattern patterns))
    (put (intern (or channel liece-current-channel) liece-obarray)
	 'exception patterns)))

(defun liece-channel-remove-exception (pattern &optional channel)
  "Remove exception PATTERN from CHANNEL."
  (let ((patterns
	 (remove-if
	  (lambda (elm) (string-equal pattern elm))
	  (liece-channel-get-exceptions channel))))
    (put (intern (or channel liece-current-channel) liece-obarray)
	 'exception patterns)))

(defun liece-channel-set-exception (channel pattern flag)
  "Add or remove exception PATTERN to CHANNEL.
If FLAG is non-nil, given exception patterns are added to the channel.
Otherwise they are removed from the channel."
  (if flag
      (liece-channel-add-exception pattern channel)
     (liece-channel-remove-exception pattern channel)))

(defun liece-channel-add-invite (pattern &optional channel)
  "Add invite PATTERN to CHANNEL."
  (let ((patterns (liece-channel-get-invites channel)))
    (or (string-list-member-ignore-case pattern patterns)
	(push pattern patterns))
    (put (intern (or channel liece-current-channel) liece-obarray)
	 'invite patterns)))

(defun liece-channel-remove-invite (pattern &optional channel)
  "Remove invite PATTERN from CHANNEL."
  (let ((patterns
	 (remove-if
	  (lambda (elm) (string-equal pattern elm))
	  (liece-channel-get-invites channel))))
    (put (intern (or channel liece-current-channel) liece-obarray)
	 'invite patterns)))

(defun liece-channel-set-invite (channel pattern flag)
  "Add or remove invite PATTERN to CHANNEL.
If FLAG is non-nil, given invite patterns are added to the channel.
Otherwise they are removed from the channel."
  (if flag
      (liece-channel-add-invite pattern channel)
     (liece-channel-remove-invite pattern channel)))
  
(defun liece-channel-virtual (channel)
  "Convert channel name into internal representation.
\(For example if CHANNEL is a string \"#...:*\", it will be converted into
\"%...\"\)"
  (let ((mapping liece-channel-conversion-map) match)
    (while mapping
      (if (string-equal-ignore-case (caar mapping) channel)
	  (setq match (cdar mapping)))
      (pop mapping))
    (if match
	match
      (save-match-data
	(cond
	 ((string-match
	   (format "^[#+]\\(.*\\):%s$"
		   (regexp-quote liece-channel-conversion-default-mask))
	   channel)
	  (if (eq ?+ (aref channel 0))
	      (concat "-" (match-string 1 channel))
	    (concat "%" (match-string 1 channel))))
;;;	 ((and (not (equal channel "")) (eq ?! (aref channel 0)))
;;;	  (concat "!" (substring channel (1+ liece-channel-id-length))))
	 (t channel))))))

(defun liece-channel-real (channel)
  "Convert channel name into external representation.
\(For example if CHANNEL is a string \"%...\", it will be converted into
\"#...:*\"\)"
  (let ((mapping liece-channel-conversion-map) match)
    (while mapping
      (if (string-equal-ignore-case (cdar mapping) channel)
	  (setq match (caar mapping)))
      (pop mapping))
    (cond
     (match match)
     ((eq ?% (aref channel 0))
      (concat "#" (substring channel 1) ":"
	      liece-channel-conversion-default-mask))
     ((eq ?- (aref channel 0))
      (concat "+" (substring channel 1) ":"
	      liece-channel-conversion-default-mask))
     (t channel))))

;;;###liece-autoload
(defun liece-command-toggle-channel-buffer-mode ()
  "Toggle visibility of channel buffer."
  (interactive)
  (if (get-buffer liece-channel-buffer)
      (setq liece-channel-buffer-mode (not liece-channel-buffer-mode)))
  (liece-configure-windows))

(defun liece-channel-buffer-create (chnl)
  "Create channel buffer of CHNL."
  (with-current-buffer
       (liece-get-buffer-create (format liece-channel-buffer-format chnl))
     (let (buffer-read-only)
       (liece-insert-info (current-buffer)
			   (concat (funcall liece-format-time-function
					    (current-time))
				   " Created.\n")))
     (unless (eq major-mode 'liece-channel-mode)
       (liece-channel-mode))
     (set-alist 'liece-channel-buffer-alist chnl (current-buffer))
     (current-buffer)))

(defun liece-channel-join-internal (item chnls &optional hints)
  (let (binding inserted)
    (if (liece-channel-member item hints)
	(do ((hint hints (cdr hint)) (chnl chnls (cdr chnl)))
	    ((not (or hint chnl)))
	  (if (and (car hint) (liece-channel-equal (car hint) item))
	      (push item binding)
	    (push (car chnl) binding)))
      (do ((hint hints (cdr hint)) (chnl chnls (cdr chnl)))
	  ((not (or hint chnl)))
	(if (and (not inserted)
		 (not (or (car hint) (car chnl))))
	    (progn
	      (push item binding)
	      (setq inserted t))
	  (push (car chnl) binding))))
    (or (liece-channel-member item binding)
	(push item binding))
    (nreverse binding)))

(defun liece-channel-join (chnl &optional nosw)
  "Initialize channel variables of CHNL.
If NOSW is non-nil do not switch to newly created channel."
  (let ((cbuf (cdr (string-assoc-ignore-case chnl liece-channel-buffer-alist)))
	(nbuf (cdr (string-assoc-ignore-case chnl liece-nick-buffer-alist))))
    (or cbuf
	(setq cbuf (liece-channel-buffer-create chnl)))
    (or nbuf
	(setq nbuf (liece-nick-buffer-create chnl)))
    (if (liece-channel-p (liece-channel-real chnl))
	(setq liece-current-channels
	      (liece-channel-join-internal
	       chnl liece-current-channels liece-default-channel-binding))
      (setq liece-current-chat-partners
	    (liece-channel-join-internal chnl liece-current-chat-partners
					  liece-default-partner-binding)))
    (unless nosw
      (liece-switch-to-channel chnl)
      (setq liece-channel-buffer cbuf
	    liece-nick-buffer nbuf))
    (liece-channel-change)))

(defmacro liece-channel-switch-to-last (chnls)
  `(let ((chnl (liece-channel-last ,chnls)))
     (if chnl
	 (liece-switch-to-channel chnl))
     (liece-channel-change)))

(defun liece-channel-part-internal (item chnls &optional hints)
  (if hints
      (mapcar
       (lambda (chnl)
	 (if (and chnl (liece-channel-equal item chnl)) nil chnl))
       chnls)
    (liece-channel-remove item chnls)))

(defun liece-channel-part (chnl &optional nosw)
  "Finalize channel variables of CHNL.
If NOSW is non-nil do not switch to newly created channel."
  (cond
   ((eq liece-command-buffer-mode 'chat)
    (setq liece-current-chat-partners
	  (liece-channel-part-internal chnl liece-current-chat-partners
					liece-default-partner-binding))
    (unless nosw
      (liece-channel-switch-to-last liece-current-chat-partners)))
   (t
    (setq liece-current-channels
	  (liece-channel-part-internal chnl liece-current-channels
				       liece-default-channel-binding))
    (unless nosw
      (liece-channel-switch-to-last liece-current-channels)))))

(defun liece-channel-last (chnls)
  (car (last (delq nil (copy-sequence chnls)))))

(defun liece-channel-change ()
  (let ((chnls (if (eq liece-command-buffer-mode 'chat)
		   liece-current-chat-partners
		 liece-current-channels))
	(string "")
	chnl)
    (with-current-buffer liece-channel-list-buffer
      (let ((n 1) buffer-read-only)
	(erase-buffer)
	(dolist (chnl chnls)
	  (when chnl
	    (setq chnl (liece-channel-virtual chnl)
		  string (format "%s,%d:%s" string n chnl))
	    (liece-channel-list-add-button n chnl))
	  (incf n))))
    (if (string-equal string "")
	(if (eq liece-command-buffer-mode 'chat)
	    (setq liece-channels-indicator "No partner")
	  (setq liece-channels-indicator "No channel"))
      (setq liece-channels-indicator (substring string 1)))
    (liece-set-channel-indicator)
    (setq chnl (if (eq liece-command-buffer-mode 'chat)
		   liece-current-chat-partner
		 liece-current-channel))
    (when chnl
      (save-excursion
	(run-hook-with-args 'liece-redisplay-buffer-functions chnl)))
    (liece-redisplay-unread-mark)
    (liece-configure-windows)))

(defsubst liece-channel-set-operator-1 (chnl user val)
  (let* ((chnl (intern chnl liece-obarray)) (opers (get chnl 'oper)))
    (if val
	(or (string-list-member-ignore-case user opers)
	    (put chnl 'oper (cons user opers)))
      (if (string-list-member-ignore-case user opers)
	  (put chnl 'oper (string-list-remove-ignore-case user opers))))))

(defsubst liece-channel-set-voice-1 (chnl user val)
  (let* ((chnl (intern chnl liece-obarray)) (voices (get chnl 'voice)))
    (if val
	(or (string-list-member-ignore-case user voices)
	    (put chnl 'voice (cons user voices)))
      (if (string-list-member-ignore-case user voices)
	  (put chnl 'voice (string-list-remove-ignore-case user voices))))))

(defun liece-channel-set-operator (chnl user val)
  (let ((nbuf (cdr (string-assoc-ignore-case chnl liece-nick-buffer-alist)))
	(xuser user))
    (liece-channel-set-operator-1 chnl user val)
    (liece-channel-set-voice-1 chnl user val)
    (setq user (concat (if val "@" " ") user)
	  xuser (concat (if val "[ +]" "@") (regexp-quote xuser)))
    (with-current-buffer nbuf
      (let (buffer-read-only)
	(goto-char (point-min))
	(liece-nick-replace xuser user nil t)))))

(defun liece-channel-set-voice (chnl user val)
  (let ((nbuf (cdr (string-assoc-ignore-case chnl liece-nick-buffer-alist)))
	(xuser user))
    (liece-channel-set-voice-1 chnl user val)
    (setq user (concat (if val "+" " ") user)
	  xuser (concat (if val " " "\\+") (regexp-quote xuser)))
    (with-current-buffer nbuf
      (let (buffer-read-only)
	(goto-char (point-min))
	(liece-nick-replace xuser user nil t)))))

(defun liece-channel-prepare-partner (join-channel-var)
  (setq liece-current-chat-partner
	(or liece-current-chat-partner join-channel-var))
  (let ((liece-command-buffer-mode 'chat))
    (liece-channel-join join-channel-var t))
  (liece-channel-change))

(defun liece-channel-buffer-invisible-p (chnl mode)
  (let ((cbuf (liece-pick-buffer chnl)))
    (or (liece-frozen (car cbuf))
	(and (eq mode 'chat)
	     (not (and (eq liece-command-buffer-mode 'chat)
		       liece-current-chat-partner
		       (string-equal-ignore-case
			chnl liece-current-chat-partner))))
	(not (and (eq liece-command-buffer-mode 'channel)
		  liece-current-channel
		  (string-equal-ignore-case
		   chnl liece-current-channel))))))

(defun liece-channel-prepare-representation (chnl &optional method name)
  (cond
   ((eq method 'dcc)
    (format liece-dcc-channel-representation-format chnl))
   (name
    (format liece-default-channel-representation-format name chnl))
   (t chnl)))

(defun liece-channel-parse-representation (str)
  (cond
   ((string-match
     (format
      (regexp-quote liece-dcc-channel-representation-format)
      "\\([^ ]+\\)")
     str)
    (vector 'dcc nil (match-string 1 str)))
   ((string-match
     (format
      (regexp-quote liece-default-channel-representation-format)
      "\\([^ ]+\\)" "\\([^ ]+\\)")
     str)
    (vector 'irc (match-string 1 str) (match-string 2 str)))
   (t (vector 'irc nil str))))

(defun liece-channel-list-add-button (n chnl)
  (insert (format "%2d: " n))
  (if liece-highlight-mode
      (let ((st (point)))
	(insert chnl)
	(liece-widget-convert-button
	 'liece-channel-push-button st (point) chnl))
    (insert chnl))
  (insert "\n"))

(defun liece-channel-add-buttons (start end)
  (save-excursion
    (goto-char start)
    (while (re-search-forward
	    (eval-when-compile
	      (concat "\\(^\\(" liece-time-prefix-regexp "\\)?"
		      "[][=<>(-][][=<>(-]?\\|\\s-+[+@]?\\)"
		      "\\([&#!%][^ :]*\\)"))
	    end t)
      ;;(re-search-forward "\\s-+\\(\\)\\([-+]\\S-*\\)" end t)
      (let* ((chnl-start (match-beginning 3))
	     (chnl-end (match-end 3))
	     (chnl (buffer-substring chnl-start chnl-end)))
	(when liece-highlight-mode
	  (liece-widget-convert-button
	   'liece-channel-push-button chnl-start chnl-end chnl))))))

;;;###liece-autoload
(defun liece-channel-redisplay-buffer (chnl)
  (let ((buffer
	 (cdr (string-assoc-ignore-case
	       chnl liece-channel-buffer-alist)))
	(window (liece-get-buffer-window liece-channel-buffer)))
    (when (liece-channel-unread-p chnl)
      (setq liece-channel-unread-list
	    (delete chnl liece-channel-unread-list))
      (run-hook-with-args 'liece-channel-read-functions chnl))
    (and buffer window
	 (with-current-buffer buffer
	   (set-window-buffer window buffer)
	   (unless (liece-frozen buffer)
	     (set-window-point window (point-max)))
	   (setq liece-channel-buffer buffer)))))

;;;###liece-autoload
(defun liece-channel-list-redisplay-buffer (chnl)
  (let ((window (liece-get-buffer-window liece-channel-list-buffer)))
    (when window
      (save-selected-window
	(select-window window)
	(goto-char (point-min))
	(search-forward chnl nil t)
	(set-window-point window (match-beginning 0))
	(when liece-highlight-mode
	  (let ((overlay (make-overlay (point)(match-end 0))))
	    (liece-map-overlays
	     (lambda (ovl)
	       (if (overlay-get ovl 'liece-channel)
		   (delete-overlay ovl))))
	    (overlay-put overlay 'face 'underline)
	    (overlay-put overlay 'liece-channel t)))))))

(provide 'liece-channel)

;;; liece-channel.el ends here