1 ;;; liece-channel.el --- Various facility for channel operation.
2 ;; Copyright (C) 1998-2000 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: IRC, liece
9 ;; This file is part of Liece.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
32 (eval-when-compile (require 'liece-inlines))
33 (eval-when-compile (require 'liece-misc))
35 (eval-when-compile (require 'liece-clfns))
37 (defconst liece-channel-regexp "[+&#!]")
38 (defconst liece-channel-modeless-regexp "[+!]")
40 (defvar liece-default-channel-representation-format "%s+%s")
42 (defconst liece-dcc-channel-representation-format "=%s")
44 (define-widget 'liece-channel-push-button 'push-button
46 :action 'liece-channel-push-button-action)
48 (defun liece-channel-push-button-action (widget &optional event)
49 (let ((chnl (liece-channel-virtual (widget-value widget))))
50 (if (or (liece-channel-member chnl liece-current-channels)
51 (y-or-n-p (format "Do you really join %s? " chnl)))
52 (liece-command-join chnl))))
54 ;;; Reader conventions
55 (defun liece-channel-p (chnl)
56 (string-match (concat "^" liece-channel-regexp) chnl))
58 (defun liece-channel-modeless-p (chnl)
59 (string-match (concat "^" liece-channel-modeless-regexp) chnl))
61 (defun liece-channel-equal (c1 c2)
62 (string-equal-ignore-case c1 c2))
64 (defun liece-channel-member (chnl chnls)
65 "Return non-nil if CHNL is member of CHNLS."
68 (and (stringp item) (liece-channel-equal chnl item)))
71 (defun liece-channel-unread-p (chnl)
72 "Return non-nil if CHNL is unread channel."
75 (and (stringp item) (liece-channel-equal chnl item)))
76 liece-channel-unread-list))
78 (defun liece-channel-get-nicks (&optional chnl)
79 "Return CHNL or current channels's members as list."
80 (get (intern (or chnl liece-current-channel) liece-obarray) 'nick))
82 (defun liece-channel-get-operators (&optional chnl)
83 "Return CHNL or current channels's operators as list."
84 (get (intern (or chnl liece-current-channel) liece-obarray) 'oper))
86 (defun liece-channel-get-voices (&optional chnl)
87 "Return CHNL or current channels's voices as list."
88 (get (intern (or chnl liece-current-channel) liece-obarray) 'voice))
90 (defun liece-channel-get-topic (&optional chnl)
91 "Return CHNL or current channels's topic."
92 (get (intern (or chnl liece-current-channel) liece-obarray) 'topic))
94 (defun liece-channel-get-modes (&optional chnl)
95 "Return CHNL or current channels's mode."
96 (get (intern (or chnl liece-current-channel) liece-obarray) 'mode))
98 (defun liece-channel-get-bans (&optional chnl)
99 "Return CHNL or current channels's ban list."
100 (get (intern (or chnl liece-current-channel) liece-obarray) 'ban))
102 (defun liece-channel-get-invites (&optional chnl)
103 "Return CHNL or current channels's invite list."
104 (get (intern (or chnl liece-current-channel) liece-obarray) 'invite))
106 (defun liece-channel-get-exceptions (&optional chnl)
107 "Return CHNL or current channels's exception list."
108 (get (intern (or chnl liece-current-channel) liece-obarray) 'exception))
110 ;;; Channel status functions
111 (defun liece-channel-remove (channel channels)
112 "Remove CHANNEL from CHANNELS."
115 (and (stringp item) (liece-channel-equal channel item)))
118 (defun liece-channel-delete (channel channels)
119 "Delete CHANNEL from CHANNELS."
122 (and (stringp item) (liece-channel-equal channel item)))
125 (defun liece-channel-set-topic (topic &optional channel)
126 "Set CHANNEL's topic."
127 (put (intern (or channel liece-current-channel) liece-obarray)
130 (defun liece-channel-add-mode (mode &optional channel)
131 "Add MODE to CHANNEL.
132 MODE is a string splitted into characters one by one."
134 (liece-string-to-list
135 (or (liece-channel-get-modes channel)
137 (or (memq mode modes)
139 (put (intern (or channel liece-current-channel) liece-obarray)
140 'mode (mapconcat #'char-to-string modes ""))))
142 (defun liece-channel-remove-mode (mode &optional channel)
143 "Remove MODE from CHANNEL.
144 MODE is a string splitted into characters one by one."
146 (liece-string-to-list
147 (or (liece-channel-get-modes channel)
150 (put (intern (or channel liece-current-channel) liece-obarray)
151 'mode (mapconcat #'char-to-string modes ""))))
153 (defun liece-channel-set-mode (channel mode flag)
154 "Add or remove channel MODE of CHANNEL.
155 MODE is a string splitted into characters one by one.
156 If FLAG is non-nil, given modes are added to the channel.
157 Otherwise they are removed from the channel."
159 (liece-channel-add-mode mode channel)
160 (liece-channel-remove-mode mode channel)))
162 (defun liece-channel-add-ban (pattern &optional channel)
163 "Add ban PATTERN to CHANNEL."
164 (let ((patterns (liece-channel-get-bans channel)))
165 (or (string-list-member-ignore-case pattern patterns)
166 (push pattern patterns))
167 (put (intern (or channel liece-current-channel) liece-obarray)
170 (defun liece-channel-remove-ban (pattern &optional channel)
171 "Remove ban PATTERN from CHANNEL."
174 (lambda (elm) (string-equal pattern elm))
175 (liece-channel-get-bans channel))))
176 (put (intern (or channel liece-current-channel) liece-obarray)
179 (defun liece-channel-set-ban (channel pattern flag)
180 "Add or remove ban PATTERN to CHANNEL.
181 If FLAG is non-nil, given ban patterns are added to the channel.
182 Otherwise they are removed from the channel."
184 (liece-channel-add-ban pattern channel)
185 (liece-channel-remove-ban pattern channel)))
187 (defun liece-channel-add-exception (pattern &optional channel)
188 "Add exception PATTERN to CHANNEL."
189 (let ((patterns (liece-channel-get-exceptions channel)))
190 (or (string-list-member-ignore-case pattern patterns)
191 (push pattern patterns))
192 (put (intern (or channel liece-current-channel) liece-obarray)
193 'exception patterns)))
195 (defun liece-channel-remove-exception (pattern &optional channel)
196 "Remove exception PATTERN from CHANNEL."
199 (lambda (elm) (string-equal pattern elm))
200 (liece-channel-get-exceptions channel))))
201 (put (intern (or channel liece-current-channel) liece-obarray)
202 'exception patterns)))
204 (defun liece-channel-set-exception (channel pattern flag)
205 "Add or remove exception PATTERN to CHANNEL.
206 If FLAG is non-nil, given exception patterns are added to the channel.
207 Otherwise they are removed from the channel."
209 (liece-channel-add-exception pattern channel)
210 (liece-channel-remove-exception pattern channel)))
212 (defun liece-channel-add-invite (pattern &optional channel)
213 "Add invite PATTERN to CHANNEL."
214 (let ((patterns (liece-channel-get-invites channel)))
215 (or (string-list-member-ignore-case pattern patterns)
216 (push pattern patterns))
217 (put (intern (or channel liece-current-channel) liece-obarray)
220 (defun liece-channel-remove-invite (pattern &optional channel)
221 "Remove invite PATTERN from CHANNEL."
224 (lambda (elm) (string-equal pattern elm))
225 (liece-channel-get-invites channel))))
226 (put (intern (or channel liece-current-channel) liece-obarray)
229 (defun liece-channel-set-invite (channel pattern flag)
230 "Add or remove invite PATTERN to CHANNEL.
231 If FLAG is non-nil, given invite patterns are added to the channel.
232 Otherwise they are removed from the channel."
234 (liece-channel-add-invite pattern channel)
235 (liece-channel-remove-invite pattern channel)))
237 (defun liece-channel-virtual (channel)
238 "Convert channel name into internal representation.
239 \(For example if CHANNEL is a string \"#...:*\", it will be converted into
241 (let ((mapping liece-channel-conversion-map) match)
243 (if (string-equal-ignore-case (caar mapping) channel)
244 (setq match (cdar mapping)))
251 (format "^[#+]\\(.*\\):%s$"
252 (regexp-quote liece-channel-conversion-default-mask))
254 (if (eq ?+ (aref channel 0))
255 (concat "-" (match-string 1 channel))
256 (concat "%" (match-string 1 channel))))
257 ;;; ((and (not (equal channel "")) (eq ?! (aref channel 0)))
258 ;;; (concat "!" (substring channel (1+ liece-channel-id-length))))
261 (defun liece-channel-real (channel)
262 "Convert channel name into external representation.
263 \(For example if CHANNEL is a string \"%...\", it will be converted into
265 (let ((mapping liece-channel-conversion-map) match)
267 (if (string-equal-ignore-case (cdar mapping) channel)
268 (setq match (caar mapping)))
272 ((eq ?% (aref channel 0))
273 (concat "#" (substring channel 1) ":"
274 liece-channel-conversion-default-mask))
275 ((eq ?- (aref channel 0))
276 (concat "+" (substring channel 1) ":"
277 liece-channel-conversion-default-mask))
281 (defun liece-command-toggle-channel-buffer-mode ()
282 "Toggle visibility of channel buffer."
284 (if (get-buffer liece-channel-buffer)
285 (setq liece-channel-buffer-mode (not liece-channel-buffer-mode)))
286 (liece-configure-windows))
288 (defun liece-channel-buffer-create (chnl)
289 "Create channel buffer of CHNL."
291 (liece-get-buffer-create (format liece-channel-buffer-format chnl))
292 (let (buffer-read-only)
293 (liece-insert-info (current-buffer)
294 (concat (funcall liece-format-time-function
297 (unless (eq major-mode 'liece-channel-mode)
298 (liece-channel-mode))
299 (set-alist 'liece-channel-buffer-alist chnl (current-buffer))
302 (defun liece-channel-join-internal (item chnls &optional hints)
303 (let (binding inserted)
304 (if (liece-channel-member item hints)
305 (do ((hint hints (cdr hint)) (chnl chnls (cdr chnl)))
306 ((not (or hint chnl)))
307 (if (and (car hint) (liece-channel-equal (car hint) item))
309 (push (car chnl) binding)))
310 (do ((hint hints (cdr hint)) (chnl chnls (cdr chnl)))
311 ((not (or hint chnl)))
312 (if (and (not inserted)
313 (not (or (car hint) (car chnl))))
317 (push (car chnl) binding))))
318 (or (liece-channel-member item binding)
322 (defun liece-channel-join (chnl &optional nosw)
323 "Initialize channel variables of CHNL.
324 If NOSW is non-nil do not switch to newly created channel."
325 (let ((cbuf (cdr (string-assoc-ignore-case chnl liece-channel-buffer-alist)))
326 (nbuf (cdr (string-assoc-ignore-case chnl liece-nick-buffer-alist))))
328 (setq cbuf (liece-channel-buffer-create chnl)))
330 (setq nbuf (liece-nick-buffer-create chnl)))
331 (if (liece-channel-p (liece-channel-real chnl))
332 (setq liece-current-channels
333 (liece-channel-join-internal
334 chnl liece-current-channels liece-default-channel-binding))
335 (setq liece-current-chat-partners
336 (liece-channel-join-internal chnl liece-current-chat-partners
337 liece-default-partner-binding)))
339 (liece-switch-to-channel chnl)
340 (setq liece-channel-buffer cbuf
341 liece-nick-buffer nbuf))
342 (liece-channel-change)))
344 (defmacro liece-channel-switch-to-last (chnls)
345 `(let ((chnl (liece-channel-last ,chnls)))
347 (liece-switch-to-channel chnl))
348 (liece-channel-change)))
350 (defun liece-channel-part-internal (item chnls &optional hints)
354 (if (and chnl (liece-channel-equal item chnl)) nil chnl))
356 (liece-channel-remove item chnls)))
358 (defun liece-channel-part (chnl &optional nosw)
359 "Finalize channel variables of CHNL.
360 If NOSW is non-nil do not switch to newly created channel."
362 ((eq liece-command-buffer-mode 'chat)
363 (setq liece-current-chat-partners
364 (liece-channel-part-internal chnl liece-current-chat-partners
365 liece-default-partner-binding))
367 (liece-channel-switch-to-last liece-current-chat-partners)))
369 (setq liece-current-channels
370 (liece-channel-part-internal chnl liece-current-channels
371 liece-default-channel-binding))
373 (liece-channel-switch-to-last liece-current-channels)))))
375 (defun liece-channel-last (chnls)
376 (car (last (delq nil (copy-sequence chnls)))))
378 (defun liece-channel-change ()
379 (let ((chnls (if (eq liece-command-buffer-mode 'chat)
380 liece-current-chat-partners
381 liece-current-channels))
384 (with-current-buffer liece-channel-list-buffer
385 (let ((n 1) buffer-read-only)
389 (setq chnl (liece-channel-virtual chnl)
390 string (format "%s,%d:%s" string n chnl))
391 (liece-channel-list-add-button n chnl))
393 (if (string-equal string "")
394 (if (eq liece-command-buffer-mode 'chat)
395 (setq liece-channels-indicator "No partner")
396 (setq liece-channels-indicator "No channel"))
397 (setq liece-channels-indicator (substring string 1)))
398 (liece-set-channel-indicator)
399 (setq chnl (if (eq liece-command-buffer-mode 'chat)
400 liece-current-chat-partner
401 liece-current-channel))
404 (run-hook-with-args 'liece-redisplay-buffer-functions chnl)))
405 (liece-redisplay-unread-mark)
406 (liece-configure-windows)))
408 (defsubst liece-channel-set-operator-1 (chnl user val)
409 (let* ((chnl (intern chnl liece-obarray)) (opers (get chnl 'oper)))
411 (or (string-list-member-ignore-case user opers)
412 (put chnl 'oper (cons user opers)))
413 (if (string-list-member-ignore-case user opers)
414 (put chnl 'oper (string-list-remove-ignore-case user opers))))))
416 (defsubst liece-channel-set-voice-1 (chnl user val)
417 (let* ((chnl (intern chnl liece-obarray)) (voices (get chnl 'voice)))
419 (or (string-list-member-ignore-case user voices)
420 (put chnl 'voice (cons user voices)))
421 (if (string-list-member-ignore-case user voices)
422 (put chnl 'voice (string-list-remove-ignore-case user voices))))))
424 (defun liece-channel-set-operator (chnl user val)
425 (let ((nbuf (cdr (string-assoc-ignore-case chnl liece-nick-buffer-alist)))
427 (liece-channel-set-operator-1 chnl user val)
428 (liece-channel-set-voice-1 chnl user val)
429 (setq user (concat (if val "@" " ") user)
430 xuser (concat (if val "[ +]" "@") (regexp-quote xuser)))
431 (with-current-buffer nbuf
432 (let (buffer-read-only)
433 (goto-char (point-min))
434 (liece-nick-replace xuser user nil t)))))
436 (defun liece-channel-set-voice (chnl user val)
437 (let ((nbuf (cdr (string-assoc-ignore-case chnl liece-nick-buffer-alist)))
439 (liece-channel-set-voice-1 chnl user val)
440 (setq user (concat (if val "+" " ") user)
441 xuser (concat (if val " " "\\+") (regexp-quote xuser)))
442 (with-current-buffer nbuf
443 (let (buffer-read-only)
444 (goto-char (point-min))
445 (liece-nick-replace xuser user nil t)))))
447 (defun liece-channel-prepare-partner (join-channel-var)
448 (setq liece-current-chat-partner
449 (or liece-current-chat-partner join-channel-var))
450 (let ((liece-command-buffer-mode 'chat))
451 (liece-channel-join join-channel-var t))
452 (liece-channel-change))
454 (defun liece-channel-buffer-invisible-p (chnl mode)
455 (let ((cbuf (liece-pick-buffer chnl)))
456 (or (liece-frozen (car cbuf))
458 (not (and (eq liece-command-buffer-mode 'chat)
459 liece-current-chat-partner
460 (string-equal-ignore-case
461 chnl liece-current-chat-partner))))
462 (not (and (eq liece-command-buffer-mode 'channel)
463 liece-current-channel
464 (string-equal-ignore-case
465 chnl liece-current-channel))))))
467 (defun liece-channel-prepare-representation (chnl &optional method name)
470 (format liece-dcc-channel-representation-format chnl))
472 (format liece-default-channel-representation-format name chnl))
475 (defun liece-channel-parse-representation (str)
479 (regexp-quote liece-dcc-channel-representation-format)
482 (vector 'dcc nil (match-string 1 str)))
485 (regexp-quote liece-default-channel-representation-format)
486 "\\([^ ]+\\)" "\\([^ ]+\\)")
488 (vector 'irc (match-string 1 str) (match-string 2 str)))
489 (t (vector 'irc nil str))))
491 (defun liece-channel-list-add-button (n chnl)
492 (insert (format "%2d: " n))
493 (if liece-highlight-mode
496 (liece-widget-convert-button
497 'liece-channel-push-button st (point) chnl))
501 (defun liece-channel-add-buttons (start end)
504 (while (re-search-forward
506 (concat "\\(^\\(" liece-time-prefix-regexp "\\)?"
507 "[][=<>(-][][=<>(-]?\\|\\s-+[+@]?\\)"
508 "\\([&#!%][^ :]*\\)"))
510 ;;(re-search-forward "\\s-+\\(\\)\\([-+]\\S-*\\)" end t)
511 (let* ((chnl-start (match-beginning 3))
512 (chnl-end (match-end 3))
513 (chnl (buffer-substring chnl-start chnl-end)))
514 (when liece-highlight-mode
515 (liece-widget-convert-button
516 'liece-channel-push-button chnl-start chnl-end chnl))))))
519 (defun liece-channel-redisplay-buffer (chnl)
521 (cdr (string-assoc-ignore-case
522 chnl liece-channel-buffer-alist)))
523 (window (liece-get-buffer-window liece-channel-buffer)))
524 (when (liece-channel-unread-p chnl)
525 (setq liece-channel-unread-list
526 (delete chnl liece-channel-unread-list))
527 (run-hook-with-args 'liece-channel-read-functions chnl))
529 (with-current-buffer buffer
530 (set-window-buffer window buffer)
531 (unless (liece-frozen buffer)
532 (set-window-point window (point-max)))
533 (setq liece-channel-buffer buffer)))))
536 (defun liece-channel-list-redisplay-buffer (chnl)
537 (let ((window (liece-get-buffer-window liece-channel-list-buffer)))
539 (save-selected-window
540 (select-window window)
541 (goto-char (point-min))
542 (search-forward chnl nil t)
543 (set-window-point window (match-beginning 0))
544 (when liece-highlight-mode
545 (let ((overlay (make-overlay (point)(match-end 0))))
548 (if (overlay-get ovl 'liece-channel)
549 (delete-overlay ovl))))
550 (overlay-put overlay 'face 'underline)
551 (overlay-put overlay 'liece-channel t)))))))
553 (provide 'liece-channel)
555 ;;; liece-channel.el ends here