1 ;;; riece-commands.el --- commands available in command buffer
2 ;; Copyright (C) 1998-2003 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: IRC, riece
8 ;; This file is part of Riece.
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)
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.
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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 (require 'riece-channel)
28 (require 'riece-complete)
29 (require 'riece-layout)
30 (require 'riece-display)
31 (require 'riece-server)
33 (require 'riece-identity)
34 (require 'riece-message)
36 (autoload 'derived-mode-class "derived")
39 (defun riece-command-switch-to-channel (channel)
40 (interactive (list (riece-completing-read-identity
41 "Channel/User: " riece-current-channels nil t)))
42 (unless (equal channel riece-current-channel)
43 (riece-switch-to-channel channel)))
45 (defun riece-command-switch-to-channel-by-number (number)
47 (let ((command-name (symbol-name this-command)))
48 (if (string-match "[0-9]+$" command-name)
49 (list (string-to-number (match-string 0 command-name)))
50 (list (string-to-number (read-string "Number: "))))))
51 (let ((channel (nth (1- number) riece-current-channels)))
53 (riece-command-switch-to-channel channel)
54 (error "No such number!"))))
59 (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
60 (number-to-string number)))
61 'riece-command-switch-to-channel-by-number)
62 (setq number (1+ number)))))
64 (defun riece-command-next-channel ()
65 "Select the next channel."
67 (when (> (length riece-current-channels) 1)
68 (let ((pointer (cdr (riece-identity-member
70 riece-current-channels))))
73 (setq pointer (cdr pointer)))
75 (setq pointer riece-current-channels)
78 (setq pointer (cdr pointer))))
80 (riece-command-switch-to-channel (car pointer))
81 (error "No such channel!")))))
83 (defun riece-command-previous-channel ()
84 "Select the previous channel."
86 (when (> (length riece-current-channels) 1)
87 (let ((pointer (riece-identity-member
89 riece-current-channels))
90 (start riece-current-channels)
92 (while (and start (not (eq start pointer)))
94 (setq channel (car start)))
95 (setq start (cdr start)))
97 (setq start (copy-sequence riece-current-channels))
98 (setq start (delq nil start))
99 (and (> (length start) 1)
100 (setq channel (nth (1- (length start)) start))))
102 (riece-command-switch-to-channel channel)
103 (error "No such channel!")))))
105 (defun riece-command-select-command-buffer ()
106 "Select the command buffer."
108 (let ((window (get-buffer-window riece-command-buffer)))
110 (select-window window))))
112 (defun riece-command-configure-windows ()
114 (riece-redisplay-buffers t))
116 (defun riece-command-change-layout (name)
117 "Select a layout-name from all current available layouts and change
118 the layout to the selected layout-name."
119 (interactive (list (completing-read "Layout: " riece-layout-alist)))
120 (setq riece-layout name
121 riece-save-variables-are-dirty t)
122 (riece-command-configure-windows))
124 (defun riece-command-toggle-channel-buffer-mode ()
126 (setq riece-channel-buffer-mode
127 (not riece-channel-buffer-mode)
128 riece-save-variables-are-dirty t)
129 (riece-command-configure-windows))
131 (defun riece-command-toggle-user-list-buffer-mode ()
133 (setq riece-user-list-buffer-mode
134 (not riece-user-list-buffer-mode)
135 riece-save-variables-are-dirty t)
136 (riece-command-configure-windows))
138 (defun riece-command-toggle-channel-list-buffer-mode ()
140 (setq riece-channel-list-buffer-mode
141 (not riece-channel-list-buffer-mode)
142 riece-save-variables-are-dirty t)
143 (riece-command-configure-windows))
145 (defun riece-command-finger (user &optional recurse)
147 (let* ((completion-ignore-case t)
148 (user (riece-completing-read-identity
150 (riece-get-users-on-server (riece-current-server-name)))))
151 (list user current-prefix-arg)))
153 (riece-send-string (format "WHOIS %s %s\r\n"
154 (riece-identity-prefix user)
155 (riece-identity-prefix user)))
156 (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))
158 (defun riece-command-topic (topic)
161 (riece-check-channel-commands-are-usable t)
162 (list (read-from-minibuffer
163 "Topic: " (cons (or (riece-with-server-buffer
164 (riece-identity-server
165 riece-current-channel)
166 (riece-channel-get-topic
167 (riece-identity-prefix
168 riece-current-channel)))
171 (riece-send-string (format "TOPIC %s :%s\r\n"
172 (riece-identity-prefix riece-current-channel)
175 (defun riece-command-invite (user)
177 (let ((completion-ignore-case t))
178 (riece-check-channel-commands-are-usable t)
179 (list (riece-completing-read-identity
181 (riece-get-users-on-server (riece-current-server-name))))))
182 (riece-send-string (format "INVITE %s %s\r\n"
183 (riece-identity-prefix user)
184 (riece-identity-prefix riece-current-channel))))
186 (defun riece-command-kick (user &optional message)
188 (let ((completion-ignore-case t))
189 (riece-check-channel-commands-are-usable t)
190 (list (completing-read
192 (riece-with-server-buffer
193 (riece-identity-server riece-current-channel)
194 (riece-channel-get-users (riece-identity-prefix
195 riece-current-channel))))
196 (if current-prefix-arg
197 (read-string "Message: ")))))
200 (format "KICK %s %s :%s\r\n"
201 (riece-identity-prefix riece-current-channel)
203 (format "KICK %s %s\r\n"
204 (riece-identity-prefix riece-current-channel)
207 (defun riece-command-names (pattern)
209 (let ((completion-ignore-case t))
210 (list (read-from-minibuffer
212 (if (and riece-current-channel
213 (riece-channel-p (riece-identity-prefix
214 riece-current-channel)))
215 (cons (riece-identity-prefix riece-current-channel)
217 (if (or (not (equal pattern ""))
218 (yes-or-no-p "Really want to query NAMES without argument? "))
219 (riece-send-string (format "NAMES %s\r\n" pattern))))
221 (defun riece-command-who (pattern)
223 (let ((completion-ignore-case t))
224 (list (read-from-minibuffer
226 (if (and riece-current-channel
227 (riece-channel-p (riece-identity-prefix
228 riece-current-channel)))
229 (cons (riece-identity-prefix riece-current-channel)
231 (if (or (not (equal pattern ""))
232 (yes-or-no-p "Really want to query WHO without argument? "))
233 (riece-send-string (format "WHO %s\r\n" pattern))))
235 (defun riece-command-list (pattern)
237 (let ((completion-ignore-case t))
238 (list (read-from-minibuffer
240 (if (and riece-current-channel
241 (riece-channel-p (riece-identity-prefix
242 riece-current-channel)))
243 (cons (riece-identity-prefix riece-current-channel)
245 (if (or (not (equal pattern ""))
246 (yes-or-no-p "Really want to query LIST without argument? "))
247 (riece-send-string (format "LIST %s\r\n" pattern))))
249 (defun riece-command-change-mode (channel change)
251 (let* ((completion-ignore-case t)
253 (if current-prefix-arg
254 (riece-completing-read-identity
256 (riece-get-identities-on-server (riece-current-server-name)))
257 (riece-check-channel-commands-are-usable t)
258 riece-current-channel))
259 (riece-overriding-server-name (riece-identity-server channel))
260 (riece-temp-minibuffer-message
261 (concat "[Available modes: "
262 (riece-with-server-buffer (riece-identity-server channel)
263 (if (riece-channel-p (riece-identity-prefix channel))
264 (if riece-supported-channel-modes
265 (apply #'string riece-supported-channel-modes))
266 (if riece-supported-user-modes
267 (apply #'string riece-supported-user-modes))))
270 (read-from-minibuffer
271 (concat (riece-concat-channel-modes
272 channel "Mode (? for help)") ": ")
273 nil riece-minibuffer-map))))
274 (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
277 (defun riece-command-set-operators (users &optional arg)
280 (riece-check-channel-commands-are-usable t)
281 (let ((completion-ignore-case t))
282 (list (riece-completing-read-multiple
284 (riece-with-server-buffer
285 (riece-identity-server riece-current-channel)
286 (riece-channel-get-users (riece-identity-prefix
287 riece-current-channel)))
288 (if current-prefix-arg
290 (memq ?o (cdr user)))
292 (not (memq ?o (cdr user))))))
293 current-prefix-arg))))
296 (setq group (cons (car users) group)
298 (when (or (= (length group) 3)
301 (format "MODE %s %c%s %s\r\n"
302 (riece-identity-prefix riece-current-channel)
303 (if current-prefix-arg
306 (make-string (length group) ?o)
307 (mapconcat #'identity (nreverse group) " ")))
310 (defun riece-command-set-speakers (users &optional arg)
313 (riece-check-channel-commands-are-usable t)
314 (let ((completion-ignore-case t))
315 (list (riece-completing-read-multiple
317 (riece-with-server-buffer
318 (riece-identity-server riece-current-channel)
319 (riece-channel-get-users (riece-identity-prefix
320 riece-current-channel)))
321 (if current-prefix-arg
323 (memq ?v (cdr user)))
325 (not (memq ?v (cdr user))))))
326 current-prefix-arg))))
329 (setq group (cons (car users) group)
331 (when (or (= (length group) 3)
334 (format "MODE %s %c%s %s\r\n"
335 (riece-identity-prefix riece-current-channel)
336 (if current-prefix-arg
339 (make-string (length group) ?v)
340 (mapconcat #'identity (nreverse group) " ")))
343 (defun riece-command-send-message (message notice)
344 "Send MESSAGE to the current channel."
345 (if (equal message "")
346 (error "No text to send"))
347 (riece-check-channel-commands-are-usable)
351 (format "NOTICE %s :%s\r\n"
352 (riece-identity-prefix riece-current-channel)
354 (riece-display-message
355 (riece-make-message (riece-current-nickname) riece-current-channel
358 (format "PRIVMSG %s :%s\r\n"
359 (riece-identity-prefix riece-current-channel)
361 (riece-display-message
362 (riece-make-message (riece-current-nickname) riece-current-channel
365 (defun riece-command-enter-message ()
366 "Send the current line to the current channel."
368 (riece-command-send-message (buffer-substring
369 (riece-line-beginning-position)
370 (riece-line-end-position))
372 (let ((next-line-add-newlines t))
375 (defun riece-command-enter-message-as-notice ()
376 "Send the current line to the current channel as NOTICE."
378 (riece-command-send-message (buffer-substring
379 (riece-line-beginning-position)
380 (riece-line-end-position))
382 (let ((next-line-add-newlines t))
385 (defun riece-command-enter-message-to-user (user)
386 "Send the current line to USER."
388 (let ((completion-ignore-case t))
389 (list (riece-completing-read-identity
391 (riece-get-users-on-server (riece-current-server-name))))))
392 (let ((text (buffer-substring
393 (riece-line-beginning-position)
394 (riece-line-end-position))))
396 (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
397 (riece-display-message
398 (riece-make-message (riece-current-nickname) user text nil t)))
399 (let ((next-line-add-newlines t))
402 (defun riece-command-join-channel (target key)
403 (let ((process (riece-server-process (riece-identity-server target))))
405 (error "%s" (substitute-command-keys
406 "Type \\[riece-command-open-server] to open server.")))
407 (riece-process-send-string process
409 (format "JOIN %s :%s\r\n"
410 (riece-identity-prefix target)
412 (format "JOIN %s\r\n"
413 (riece-identity-prefix target))))))
415 (defun riece-command-join-partner (target)
416 (let ((pointer (riece-identity-member target riece-current-channels)))
418 (riece-command-switch-to-channel (car pointer))
419 (riece-join-channel target)
420 (riece-switch-to-channel target))))
422 (defun riece-command-join (target &optional key)
424 (let* ((completion-ignore-case t)
426 (if riece-join-channel-candidate
427 (let ((default (riece-format-identity
428 riece-join-channel-candidate)))
429 (riece-completing-read-identity
430 (format "Channel/User (default %s): " default)
431 (riece-get-identities-on-server (riece-current-server-name))
432 nil nil nil nil default))
433 (riece-completing-read-identity
435 (riece-get-identities-on-server (riece-current-server-name)))))
437 (if (and current-prefix-arg
438 (riece-channel-p (riece-identity-prefix target)))
440 (riece-read-passwd (format "Key for %s: "
441 (riece-format-identity target)))))
443 (let ((pointer (riece-identity-member target riece-current-channels)))
445 (riece-command-switch-to-channel (car pointer))
446 (if (riece-channel-p (riece-identity-prefix target))
447 (riece-command-join-channel target key)
448 (riece-command-join-partner target)))))
450 (defun riece-command-part-channel (target message)
451 (let ((process (riece-server-process (riece-identity-server target))))
452 (riece-process-send-string process
454 (format "PART %s :%s\r\n"
455 (riece-identity-prefix target)
457 (format "PART %s\r\n"
458 (riece-identity-prefix target))))))
460 (defun riece-command-part (target &optional message)
463 (riece-check-channel-commands-are-usable)
464 (let* ((completion-ignore-case t)
466 (riece-completing-read-identity
467 (format "Channel/User (default %s): "
468 (riece-format-identity riece-current-channel))
469 riece-current-channels nil nil nil nil
470 (riece-format-identity riece-current-channel)))
472 (if (and current-prefix-arg
473 (riece-channel-p (riece-identity-prefix target)))
474 (setq message (read-string "Message: ")))
475 (list target message))))
476 (if (riece-identity-member target riece-current-channels)
477 (if (riece-channel-p (riece-identity-prefix target))
478 (riece-command-part-channel target message)
479 (riece-part-channel target))
480 (error "You are not talking with %s" target)))
482 (defun riece-command-change-nickname (nickname)
483 "Change your nickname to NICK."
484 (interactive "sEnter your nickname: ")
485 (riece-send-string (format "NICK %s\r\n" nickname)))
487 (defun riece-command-scroll-down (lines)
488 "Scroll LINES down dialogue buffer from command buffer."
490 (let ((buffer (if (and riece-channel-buffer-mode
491 riece-current-channel)
493 riece-dialogue-buffer)))
494 (if (get-buffer-window buffer)
496 (let ((other-window-scroll-buffer buffer))
497 (scroll-other-window-down lines))
499 (message "Beginning of buffer"))))))
501 (defun riece-command-scroll-up (lines)
502 "Scroll LINES up dialogue buffer from command buffer."
504 (let ((buffer (if (and riece-channel-buffer-mode
505 riece-current-channel)
507 riece-dialogue-buffer)))
508 (if (get-buffer-window buffer)
510 (let ((other-window-scroll-buffer buffer))
511 (scroll-other-window lines))
513 (message "End of buffer"))))))
515 (defun riece-command-user-list-scroll-down (lines)
516 "Scroll LINES down user list buffer from command buffer."
518 (if (get-buffer-window riece-user-list-buffer)
520 (let ((other-window-scroll-buffer riece-user-list-buffer))
521 (scroll-other-window-down lines))
523 (message "Beginning of buffer")))))
525 (defun riece-command-user-list-scroll-up (lines)
526 "Scroll LINES up user list buffer from command buffer."
528 (if (get-buffer-window riece-user-list-buffer)
530 (let ((other-window-scroll-buffer riece-user-list-buffer))
531 (scroll-other-window lines))
533 (message "End of buffer")))))
535 (defun riece-command-toggle-away (&optional message)
536 "Mark yourself as being away."
538 (if (and (not (riece-with-server-buffer (riece-identity-server
539 (riece-current-nickname))
540 (riece-user-get-away (riece-identity-prefix
541 (riece-current-nickname)))))
543 (list (read-from-minibuffer
544 "Away message: " (cons (or riece-away-message "") 0)))))
545 (if (riece-with-server-buffer (riece-identity-server
546 (riece-current-nickname))
547 (riece-user-get-away (riece-identity-prefix
548 (riece-current-nickname))))
549 (riece-send-string "AWAY\r\n")
550 (riece-send-string (format "AWAY :%s\r\n" (or message
551 riece-away-message)))))
553 (defun riece-command-toggle-freeze (&optional arg)
554 "Prevent automatic scrolling of the dialogue window.
555 If prefix argument ARG is non-nil, toggle frozen status."
557 (with-current-buffer (if (eq (derived-mode-class major-mode)
558 'riece-dialogue-mode)
560 (if (and riece-channel-buffer-mode
561 riece-channel-buffer)
563 riece-dialogue-buffer))
564 (setq riece-freeze (if arg
565 (< 0 (prefix-numeric-value arg))
567 (riece-emit-signal 'buffer-freeze-changed
568 (current-buffer) riece-freeze)))
570 (defun riece-command-toggle-own-freeze (&optional arg)
571 "Prevent automatic scrolling of the dialogue window.
572 The difference from `riece-command-freeze' is that your messages are hidden.
573 If prefix argument ARG is non-nil, toggle frozen status."
575 (with-current-buffer (if (eq (derived-mode-class major-mode)
576 'riece-dialogue-mode)
578 (if (and riece-channel-buffer-mode
579 riece-channel-buffer)
581 riece-dialogue-buffer))
583 (< 0 (prefix-numeric-value arg))
584 (not (eq riece-freeze 'own)))
585 (setq riece-freeze 'own)
586 (setq riece-freeze nil))
587 (riece-emit-signal 'buffer-freeze-changed
588 (current-buffer) riece-freeze)))
591 (autoload 'riece-exit "riece"))
592 (defun riece-command-quit (&optional arg)
595 (if (y-or-n-p "Really quit IRC? ")
596 (if riece-server-process-alist
599 (read-string "Message: ")
601 (alist riece-server-process-alist))
603 (riece-quit-server-process (cdr (car alist)) message)
604 (setq alist (cdr alist))))
605 ;; If no server process is available, exit immediately.
608 (defun riece-command-raw (command)
609 "Enter raw IRC command, which is sent to the server."
610 (interactive "sIRC command: ")
611 (riece-send-string (concat command "\r\n")))
613 (defun riece-command-end-of-buffer ()
614 "Get end of the dialogue buffer."
617 (setq buffer (if riece-channel-buffer-mode
619 riece-dialogue-buffer))
620 (or (setq window (get-buffer-window buffer))
621 (setq window (get-buffer-window riece-dialogue-buffer)
622 buffer riece-dialogue-buffer))
624 (save-selected-window
625 (select-window window)
626 (goto-char (point-max))))))
628 (defun riece-command-copy-region (start end)
629 "Move current region between START and END to `kill-ring'."
631 (kill-new (buffer-substring-no-properties start end)))
633 (defun riece-command-complete-user ()
634 "Complete a user name in the current buffer."
636 (let* ((completion-ignore-case t)
637 (table (mapcar (lambda (user)
638 (list (riece-format-identity user t)))
639 (riece-get-users-on-server
640 (riece-current-server-name))))
641 (current (current-word))
642 (completion (try-completion current table))
643 (all (all-completions current table)))
644 (if (eq completion t)
646 (if (null completion)
647 (message "Can't find completion for \"%s\"" current)
648 (if (equal current completion)
649 (with-output-to-temp-buffer "*Help*"
650 (display-completion-list all))
651 (delete-region (point) (- (point) (length current)))
652 (insert completion))))))
654 (defun riece-command-open-server (server-name)
656 (list (completing-read "Server: " riece-server-alist)))
657 (if (riece-server-process server-name)
658 (error "%s is already opened" server-name))
660 (riece-server-name-to-server server-name)
663 (defun riece-command-close-server (server-name &optional message)
665 (list (completing-read "Server: " riece-server-process-alist)
666 (if current-prefix-arg
667 (read-string "Message: ")
668 riece-quit-message)))
669 (riece-quit-server-process (riece-server-process server-name) message))
671 (defun riece-command-universal-server-name-argument ()
673 (let* ((riece-overriding-server-name
674 (completing-read "Server: " riece-server-process-alist))
676 (key-binding (read-key-sequence
677 (format "Command to execute on \"%s\":"
678 riece-overriding-server-name)))))
680 (call-interactively command)))
682 (provide 'riece-commands)
684 ;;; riece-commands.el ends here