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)
37 (defun riece-command-switch-to-channel (channel)
38 (interactive (list (riece-completing-read-identity
39 "Channel/User: " riece-current-channels nil t)))
40 (unless (equal channel riece-current-channel)
41 (riece-switch-to-channel channel)
42 (riece-redisplay-buffers)))
44 (defun riece-command-switch-to-channel-by-number (number)
46 (let ((command-name (symbol-name this-command)))
47 (if (string-match "[0-9]+$" command-name)
48 (list (string-to-number (match-string 0 command-name)))
49 (list (string-to-number (read-string "Number: "))))))
50 (let ((channel (nth (1- number) riece-current-channels)))
52 (riece-command-switch-to-channel channel)
53 (error "No such number!"))))
58 (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
59 (number-to-string number)))
60 'riece-command-switch-to-channel-by-number)
61 (setq number (1+ number)))))
63 (defun riece-command-next-channel ()
64 "Select the next channel."
66 (when (> (length riece-current-channels) 1)
67 (let ((pointer (cdr (riece-identity-member
69 riece-current-channels))))
72 (setq pointer (cdr pointer)))
74 (setq pointer riece-current-channels)
77 (setq pointer (cdr pointer))))
79 (riece-command-switch-to-channel (car pointer))
80 (error "No such channel!")))))
82 (defun riece-command-previous-channel ()
83 "Select the previous channel."
85 (when (> (length riece-current-channels) 1)
86 (let ((pointer (riece-identity-member
88 riece-current-channels))
89 (start riece-current-channels)
91 (while (and start (not (eq start pointer)))
93 (setq channel (car start)))
94 (setq start (cdr start)))
96 (setq start (copy-sequence riece-current-channels))
97 (setq start (delq nil start))
98 (and (> (length start) 1)
99 (setq channel (nth (1- (length start)) start))))
101 (riece-command-switch-to-channel channel)
102 (error "No such channel!")))))
104 (defun riece-command-select-command-buffer ()
105 "Select the command buffer."
107 (let ((window (get-buffer-window riece-command-buffer)))
109 (select-window window))))
111 (defun riece-command-configure-windows ()
113 (riece-redisplay-buffers t))
115 (defun riece-command-change-layout (name)
116 "Select a layout-name from all current available layouts and change
117 the layout to the selected layout-name."
118 (interactive (list (completing-read "Layout: " riece-layout-alist)))
119 (setq riece-layout name
120 riece-save-variables-are-dirty t)
121 (riece-command-configure-windows))
123 (defun riece-command-toggle-channel-buffer-mode ()
125 (setq riece-channel-buffer-mode
126 (not riece-channel-buffer-mode)
127 riece-save-variables-are-dirty t)
128 (riece-command-configure-windows))
130 (defun riece-command-toggle-user-list-buffer-mode ()
132 (setq riece-user-list-buffer-mode
133 (not riece-user-list-buffer-mode)
134 riece-save-variables-are-dirty t)
135 (riece-command-configure-windows))
137 (defun riece-command-toggle-channel-list-buffer-mode ()
139 (setq riece-channel-list-buffer-mode
140 (not riece-channel-list-buffer-mode)
141 riece-save-variables-are-dirty t)
142 (riece-command-configure-windows))
144 (defun riece-command-finger (user &optional recurse)
146 (let* ((completion-ignore-case t)
147 (user (riece-completing-read-identity
149 (riece-get-users-on-server (riece-current-server-name)))))
150 (list user current-prefix-arg)))
152 (riece-send-string (format "WHOIS %s %s\r\n"
153 (riece-identity-prefix user)
154 (riece-identity-prefix user)))
155 (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))
157 (defun riece-command-topic (topic)
160 (riece-check-channel-commands-are-usable t)
161 (list (read-from-minibuffer
162 "Topic: " (cons (or (riece-with-server-buffer
163 (riece-identity-server
164 riece-current-channel)
165 (riece-channel-get-topic
166 (riece-identity-prefix
167 riece-current-channel)))
170 (riece-send-string (format "TOPIC %s :%s\r\n"
171 (riece-identity-prefix riece-current-channel)
174 (defun riece-command-invite (user)
176 (let ((completion-ignore-case t))
177 (riece-check-channel-commands-are-usable t)
178 (list (riece-completing-read-identity
180 (riece-get-users-on-server (riece-current-server-name))))))
181 (riece-send-string (format "INVITE %s %s\r\n"
182 (riece-identity-prefix user)
183 (riece-identity-prefix riece-current-channel))))
185 (defun riece-command-kick (user &optional message)
187 (let ((completion-ignore-case t))
188 (riece-check-channel-commands-are-usable t)
189 (list (completing-read
191 (riece-with-server-buffer
192 (riece-identity-server riece-current-channel)
193 (riece-channel-get-users (riece-identity-prefix
194 riece-current-channel))))
195 (if current-prefix-arg
196 (read-string "Message: ")))))
199 (format "KICK %s %s :%s\r\n"
200 (riece-identity-prefix riece-current-channel)
202 (format "KICK %s %s\r\n"
203 (riece-identity-prefix riece-current-channel)
206 (defun riece-command-names (pattern)
208 (let ((completion-ignore-case t))
209 (list (read-from-minibuffer
211 (if (and riece-current-channel
212 (riece-channel-p (riece-identity-prefix
213 riece-current-channel)))
214 (cons (riece-identity-prefix riece-current-channel)
216 (if (or (not (equal pattern ""))
217 (yes-or-no-p "Really want to query NAMES without argument? "))
218 (riece-send-string (format "NAMES %s\r\n" pattern))))
220 (defun riece-command-who (pattern)
222 (let ((completion-ignore-case t))
223 (list (read-from-minibuffer
225 (if (and riece-current-channel
226 (riece-channel-p (riece-identity-prefix
227 riece-current-channel)))
228 (cons (riece-identity-prefix riece-current-channel)
230 (if (or (not (equal pattern ""))
231 (yes-or-no-p "Really want to query WHO without argument? "))
232 (riece-send-string (format "WHO %s\r\n" pattern))))
234 (defun riece-command-list (pattern)
236 (let ((completion-ignore-case t))
237 (list (read-from-minibuffer
239 (if (and riece-current-channel
240 (riece-channel-p (riece-identity-prefix
241 riece-current-channel)))
242 (cons (riece-identity-prefix riece-current-channel)
244 (if (or (not (equal pattern ""))
245 (yes-or-no-p "Really want to query LIST without argument? "))
246 (riece-send-string (format "LIST %s\r\n" pattern))))
248 (defun riece-command-change-mode (channel change)
250 (let* ((completion-ignore-case t)
252 (if current-prefix-arg
253 (riece-completing-read-identity
255 (riece-get-identities-on-server (riece-current-server-name)))
256 (riece-check-channel-commands-are-usable t)
257 riece-current-channel))
258 (riece-overriding-server-name (riece-identity-server channel))
259 (riece-temp-minibuffer-message
260 (concat "[Available modes: "
261 (riece-with-server-buffer (riece-identity-server channel)
262 (if (riece-channel-p (riece-identity-prefix channel))
263 (if riece-supported-channel-modes
264 (apply #'string riece-supported-channel-modes))
265 (if riece-supported-user-modes
266 (apply #'string riece-supported-user-modes))))
269 (read-from-minibuffer
270 (concat (riece-concat-channel-modes
271 channel "Mode (? for help)") ": ")
272 nil riece-minibuffer-map))))
273 (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
276 (defun riece-command-set-operators (users &optional arg)
279 (riece-check-channel-commands-are-usable t)
280 (let ((completion-ignore-case t))
281 (list (riece-completing-read-multiple
283 (riece-with-server-buffer
284 (riece-identity-server riece-current-channel)
285 (riece-channel-get-users (riece-identity-prefix
286 riece-current-channel)))
287 (if current-prefix-arg
289 (memq ?o (cdr user)))
291 (not (memq ?o (cdr user))))))
292 current-prefix-arg))))
295 (setq group (cons (car users) group)
297 (when (or (= (length group) 3)
300 (format "MODE %s %c%s %s\r\n"
301 (riece-identity-prefix riece-current-channel)
302 (if current-prefix-arg
305 (make-string (length group) ?o)
306 (mapconcat #'identity group " ")))
309 (defun riece-command-set-speakers (users &optional arg)
312 (riece-check-channel-commands-are-usable t)
313 (let ((completion-ignore-case t))
314 (list (riece-completing-read-multiple
316 (riece-with-server-buffer
317 (riece-identity-server riece-current-channel)
318 (riece-channel-get-users (riece-identity-prefix
319 riece-current-channel)))
320 (if current-prefix-arg
322 (memq ?v (cdr user)))
324 (not (memq ?v (cdr user))))))
325 current-prefix-arg))))
328 (setq group (cons (car users) group)
330 (when (or (= (length group) 3)
333 (format "MODE %s %c%s %s\r\n"
334 (riece-identity-prefix riece-current-channel)
335 (if current-prefix-arg
338 (make-string (length group) ?v)
339 (mapconcat #'identity group " ")))
342 (defun riece-command-send-message (message notice)
343 "Send MESSAGE to the current channel."
344 (if (equal message "")
345 (error "No text to send"))
346 (riece-check-channel-commands-are-usable)
350 (format "NOTICE %s :%s\r\n"
351 (riece-identity-prefix riece-current-channel)
353 (riece-display-message
354 (riece-make-message (riece-current-nickname) riece-current-channel
357 (format "PRIVMSG %s :%s\r\n"
358 (riece-identity-prefix riece-current-channel)
360 (riece-display-message
361 (riece-make-message (riece-current-nickname) riece-current-channel
364 (defun riece-command-enter-message ()
365 "Send the current line to the current channel."
367 (riece-command-send-message (buffer-substring
368 (riece-line-beginning-position)
369 (riece-line-end-position))
371 (let ((next-line-add-newlines t))
374 (defun riece-command-enter-message-as-notice ()
375 "Send the current line to the current channel as NOTICE."
377 (riece-command-send-message (buffer-substring
378 (riece-line-beginning-position)
379 (riece-line-end-position))
381 (let ((next-line-add-newlines t))
384 (defun riece-command-enter-message-to-user (user)
385 "Send the current line to USER."
387 (let ((completion-ignore-case t))
388 (list (riece-completing-read-identity
390 (riece-get-users-on-server (riece-current-server-name))))))
391 (let ((text (buffer-substring
392 (riece-line-beginning-position)
393 (riece-line-end-position))))
395 (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
396 (riece-display-message
397 (riece-make-message (riece-current-nickname) user text nil t)))
398 (let ((next-line-add-newlines t))
401 (defun riece-command-join-channel (target key)
402 (let ((process (riece-server-process (riece-identity-server target))))
404 (error "%s" (substitute-command-keys
405 "Type \\[riece-command-open-server] to open server.")))
406 (riece-process-send-string process
408 (format "JOIN %s :%s\r\n"
409 (riece-identity-prefix target)
411 (format "JOIN %s\r\n"
412 (riece-identity-prefix target))))))
414 (defun riece-command-join-partner (target)
415 (let ((pointer (riece-identity-member target riece-current-channels)))
417 (riece-command-switch-to-channel (car pointer))
418 (riece-join-channel target)
419 (riece-switch-to-channel target)
420 (riece-redisplay-buffers))))
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 (riece-redisplay-buffers))
481 (error "You are not talking with %s" target)))
483 (defun riece-command-change-nickname (nickname)
484 "Change your nickname to NICK."
485 (interactive "sEnter your nickname: ")
486 (riece-send-string (format "NICK %s\r\n" nickname)))
488 (defun riece-command-scroll-down (lines)
489 "Scroll LINES down dialogue buffer from command buffer."
491 (let ((buffer (if (and riece-channel-buffer-mode
492 riece-current-channel)
494 riece-dialogue-buffer)))
495 (if (get-buffer-window buffer)
497 (let ((other-window-scroll-buffer buffer))
498 (scroll-other-window-down lines))
500 (message "Beginning of buffer"))))))
502 (defun riece-command-scroll-up (lines)
503 "Scroll LINES up dialogue buffer from command buffer."
505 (let ((buffer (if (and riece-channel-buffer-mode
506 riece-current-channel)
508 riece-dialogue-buffer)))
509 (if (get-buffer-window buffer)
511 (let ((other-window-scroll-buffer buffer))
512 (scroll-other-window lines))
514 (message "End of buffer"))))))
516 (defun riece-command-user-list-scroll-down (lines)
517 "Scroll LINES down user list buffer from command buffer."
519 (if (get-buffer-window riece-user-list-buffer)
521 (let ((other-window-scroll-buffer riece-user-list-buffer))
522 (scroll-other-window-down lines))
524 (message "Beginning of buffer")))))
526 (defun riece-command-user-list-scroll-up (lines)
527 "Scroll LINES up user list buffer from command buffer."
529 (if (get-buffer-window riece-user-list-buffer)
531 (let ((other-window-scroll-buffer riece-user-list-buffer))
532 (scroll-other-window lines))
534 (message "End of buffer")))))
536 (defun riece-command-toggle-away (&optional message)
537 "Mark yourself as being away."
539 (if (and (not (riece-with-server-buffer (riece-identity-server
540 (riece-current-nickname))
541 (riece-user-get-away (riece-identity-prefix
542 (riece-current-nickname)))))
544 (list (read-from-minibuffer
545 "Away message: " (cons (or riece-away-message "") 0)))))
546 (if (riece-with-server-buffer (riece-identity-server
547 (riece-current-nickname))
548 (riece-user-get-away (riece-identity-prefix
549 (riece-current-nickname))))
550 (riece-send-string "AWAY\r\n")
551 (riece-send-string (format "AWAY :%s\r\n" (or message
552 riece-away-message)))))
554 (defun riece-command-toggle-freeze (&optional arg)
555 "Prevent automatic scrolling of the dialogue window.
556 If prefix argument ARG is non-nil, toggle frozen status."
558 (with-current-buffer (if (and riece-channel-buffer-mode
559 riece-channel-buffer)
561 riece-dialogue-buffer)
562 (setq riece-freeze (if arg
563 (< 0 (prefix-numeric-value arg))
564 (not riece-freeze))))
565 (riece-update-status-indicators)
566 (force-mode-line-update t))
568 (defun riece-command-toggle-own-freeze (&optional arg)
569 "Prevent automatic scrolling of the dialogue window.
570 The difference from `riece-command-freeze' is that your messages are hidden.
571 If prefix argument ARG is non-nil, toggle frozen status."
573 (with-current-buffer (if (and riece-channel-buffer-mode
574 riece-channel-buffer)
576 riece-dialogue-buffer)
578 (< 0 (prefix-numeric-value arg))
579 (not (eq riece-freeze 'own)))
580 (setq riece-freeze 'own)
581 (setq riece-freeze nil)))
582 (riece-update-status-indicators)
583 (force-mode-line-update t))
586 (autoload 'riece-exit "riece"))
587 (defun riece-command-quit (&optional arg)
590 (if (y-or-n-p "Really quit IRC? ")
591 (if riece-server-process-alist
594 (read-string "Message: ")
596 (alist riece-server-process-alist))
598 (riece-quit-server-process (cdr (car alist)) message)
599 (setq alist (cdr alist))))
600 ;; If no server process is available, exit immediately.
603 (defun riece-command-raw (command)
604 "Enter raw IRC command, which is sent to the server."
605 (interactive "sIRC command: ")
606 (riece-send-string (concat command "\r\n")))
608 (defun riece-command-end-of-buffer ()
609 "Get end of the dialogue buffer."
612 (setq buffer (if riece-channel-buffer-mode
614 riece-dialogue-buffer))
615 (or (setq window (get-buffer-window buffer))
616 (setq window (get-buffer-window riece-dialogue-buffer)
617 buffer riece-dialogue-buffer))
619 (save-selected-window
620 (select-window window)
621 (goto-char (point-max))))))
623 (defun riece-command-copy-region (start end)
624 "Move current region between START and END to `kill-ring'."
626 (kill-new (buffer-substring-no-properties start end)))
628 (defun riece-command-open-server (server-name)
630 (list (completing-read "Server: " riece-server-alist)))
631 (if (riece-server-process server-name)
632 (error "%s is already opened" server-name))
634 (riece-server-name-to-server server-name)
637 (defun riece-command-close-server (server-name &optional message)
639 (list (completing-read "Server: " riece-server-process-alist)
640 (if current-prefix-arg
641 (read-string "Message: ")
642 riece-quit-message)))
643 (riece-quit-server-process (riece-server-process server-name) message))
645 (defun riece-command-universal-server-name-argument ()
647 (let* ((riece-overriding-server-name
648 (completing-read "Server: " riece-server-process-alist))
650 (key-binding (read-key-sequence
651 (format "Command to execute on \"%s\":"
652 riece-overriding-server-name)))))
654 (call-interactively command)))
656 (provide 'riece-commands)
658 ;;; riece-commands.el ends here