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)))
43 (defun riece-command-switch-to-channel-by-number (number)
45 (let ((command-name (symbol-name this-command)))
46 (if (string-match "[0-9]+$" command-name)
47 (list (string-to-number (match-string 0 command-name)))
48 (list (string-to-number (read-string "Number: "))))))
49 (let ((channel (nth (1- number) riece-current-channels)))
51 (riece-command-switch-to-channel channel)
52 (error "No such number!"))))
57 (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
58 (number-to-string number)))
59 'riece-command-switch-to-channel-by-number)
60 (setq number (1+ number)))))
62 (defun riece-command-next-channel ()
63 "Select the next channel."
65 (when (> (length riece-current-channels) 1)
66 (let ((pointer (cdr (riece-identity-member
68 riece-current-channels))))
71 (setq pointer (cdr pointer)))
73 (setq pointer riece-current-channels)
76 (setq pointer (cdr pointer))))
78 (riece-command-switch-to-channel (car pointer))
79 (error "No such channel!")))))
81 (defun riece-command-previous-channel ()
82 "Select the previous channel."
84 (when (> (length riece-current-channels) 1)
85 (let ((pointer (riece-identity-member
87 riece-current-channels))
88 (start riece-current-channels)
90 (while (and start (not (eq start pointer)))
92 (setq channel (car start)))
93 (setq start (cdr start)))
95 (setq start (copy-sequence riece-current-channels))
96 (setq start (delq nil start))
97 (and (> (length start) 1)
98 (setq channel (nth (1- (length start)) start))))
100 (riece-command-switch-to-channel channel)
101 (error "No such channel!")))))
103 (defun riece-command-select-command-buffer ()
104 "Select the command buffer."
106 (let ((window (get-buffer-window riece-command-buffer)))
108 (select-window window))))
110 (defun riece-command-configure-windows ()
112 (riece-redisplay-buffers t))
114 (defun riece-command-change-layout (name)
115 "Select a layout-name from all current available layouts and change
116 the layout to the selected layout-name."
117 (interactive (list (completing-read "Layout: " riece-layout-alist)))
118 (setq riece-layout name
119 riece-save-variables-are-dirty t)
120 (riece-command-configure-windows))
122 (defun riece-command-toggle-channel-buffer-mode ()
124 (setq riece-channel-buffer-mode
125 (not riece-channel-buffer-mode)
126 riece-save-variables-are-dirty t)
127 (riece-command-configure-windows))
129 (defun riece-command-toggle-user-list-buffer-mode ()
131 (setq riece-user-list-buffer-mode
132 (not riece-user-list-buffer-mode)
133 riece-save-variables-are-dirty t)
134 (riece-command-configure-windows))
136 (defun riece-command-toggle-channel-list-buffer-mode ()
138 (setq riece-channel-list-buffer-mode
139 (not riece-channel-list-buffer-mode)
140 riece-save-variables-are-dirty t)
141 (riece-command-configure-windows))
143 (defun riece-command-finger (user &optional recurse)
145 (let* ((completion-ignore-case t)
146 (user (riece-completing-read-identity
148 (riece-get-users-on-server (riece-current-server-name)))))
149 (list user current-prefix-arg)))
151 (riece-send-string (format "WHOIS %s %s\r\n"
152 (riece-identity-prefix user)
153 (riece-identity-prefix user)))
154 (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))
156 (defun riece-command-topic (topic)
159 (riece-check-channel-commands-are-usable t)
160 (list (read-from-minibuffer
161 "Topic: " (cons (or (riece-with-server-buffer
162 (riece-identity-server
163 riece-current-channel)
164 (riece-channel-get-topic
165 (riece-identity-prefix
166 riece-current-channel)))
169 (riece-send-string (format "TOPIC %s :%s\r\n"
170 (riece-identity-prefix riece-current-channel)
173 (defun riece-command-invite (user)
175 (let ((completion-ignore-case t))
176 (riece-check-channel-commands-are-usable t)
177 (list (riece-completing-read-identity
179 (riece-get-users-on-server (riece-current-server-name))))))
180 (riece-send-string (format "INVITE %s %s\r\n"
181 (riece-identity-prefix user)
182 (riece-identity-prefix riece-current-channel))))
184 (defun riece-command-kick (user &optional message)
186 (let ((completion-ignore-case t))
187 (riece-check-channel-commands-are-usable t)
188 (list (completing-read
190 (riece-with-server-buffer
191 (riece-identity-server riece-current-channel)
192 (riece-channel-get-users (riece-identity-prefix
193 riece-current-channel))))
194 (if current-prefix-arg
195 (read-string "Message: ")))))
198 (format "KICK %s %s :%s\r\n"
199 (riece-identity-prefix riece-current-channel)
201 (format "KICK %s %s\r\n"
202 (riece-identity-prefix riece-current-channel)
205 (defun riece-command-names (pattern)
207 (let ((completion-ignore-case t))
208 (list (read-from-minibuffer
210 (if (and riece-current-channel
211 (riece-channel-p (riece-identity-prefix
212 riece-current-channel)))
213 (cons (riece-identity-prefix riece-current-channel)
215 (if (or (not (equal pattern ""))
216 (yes-or-no-p "Really want to query NAMES without argument? "))
217 (riece-send-string (format "NAMES %s\r\n" pattern))))
219 (defun riece-command-who (pattern)
221 (let ((completion-ignore-case t))
222 (list (read-from-minibuffer
224 (if (and riece-current-channel
225 (riece-channel-p (riece-identity-prefix
226 riece-current-channel)))
227 (cons (riece-identity-prefix riece-current-channel)
229 (if (or (not (equal pattern ""))
230 (yes-or-no-p "Really want to query WHO without argument? "))
231 (riece-send-string (format "WHO %s\r\n" pattern))))
233 (defun riece-command-list (pattern)
235 (let ((completion-ignore-case t))
236 (list (read-from-minibuffer
238 (if (and riece-current-channel
239 (riece-channel-p (riece-identity-prefix
240 riece-current-channel)))
241 (cons (riece-identity-prefix riece-current-channel)
243 (if (or (not (equal pattern ""))
244 (yes-or-no-p "Really want to query LIST without argument? "))
245 (riece-send-string (format "LIST %s\r\n" pattern))))
247 (defun riece-command-change-mode (channel change)
249 (let* ((completion-ignore-case t)
251 (if current-prefix-arg
252 (riece-completing-read-identity
254 (riece-get-identities-on-server (riece-current-server-name)))
255 (riece-check-channel-commands-are-usable t)
256 riece-current-channel))
257 (riece-overriding-server-name (riece-identity-server channel))
258 (riece-temp-minibuffer-message
259 (concat "[Available modes: "
260 (riece-with-server-buffer (riece-identity-server channel)
261 (if (riece-channel-p (riece-identity-prefix channel))
262 (if riece-supported-channel-modes
263 (apply #'string riece-supported-channel-modes))
264 (if riece-supported-user-modes
265 (apply #'string riece-supported-user-modes))))
268 (read-from-minibuffer
269 (concat (riece-concat-channel-modes
270 channel "Mode (? for help)") ": ")
271 nil riece-minibuffer-map))))
272 (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
275 (defun riece-command-set-operators (users &optional arg)
278 (riece-check-channel-commands-are-usable t)
279 (let ((completion-ignore-case t))
280 (list (riece-completing-read-multiple
282 (riece-with-server-buffer
283 (riece-identity-server riece-current-channel)
284 (riece-channel-get-users (riece-identity-prefix
285 riece-current-channel)))
286 (if current-prefix-arg
288 (memq ?o (cdr user)))
290 (not (memq ?o (cdr user))))))
291 current-prefix-arg))))
294 (setq group (cons (car users) group)
296 (when (or (= (length group) 3)
299 (format "MODE %s %c%s %s\r\n"
300 (riece-identity-prefix riece-current-channel)
301 (if current-prefix-arg
304 (make-string (length group) ?o)
305 (mapconcat #'identity (nreverse group) " ")))
308 (defun riece-command-set-speakers (users &optional arg)
311 (riece-check-channel-commands-are-usable t)
312 (let ((completion-ignore-case t))
313 (list (riece-completing-read-multiple
315 (riece-with-server-buffer
316 (riece-identity-server riece-current-channel)
317 (riece-channel-get-users (riece-identity-prefix
318 riece-current-channel)))
319 (if current-prefix-arg
321 (memq ?v (cdr user)))
323 (not (memq ?v (cdr user))))))
324 current-prefix-arg))))
327 (setq group (cons (car users) group)
329 (when (or (= (length group) 3)
332 (format "MODE %s %c%s %s\r\n"
333 (riece-identity-prefix riece-current-channel)
334 (if current-prefix-arg
337 (make-string (length group) ?v)
338 (mapconcat #'identity (nreverse group) " ")))
341 (defun riece-command-send-message (message notice)
342 "Send MESSAGE to the current channel."
343 (if (equal message "")
344 (error "No text to send"))
345 (riece-check-channel-commands-are-usable)
349 (format "NOTICE %s :%s\r\n"
350 (riece-identity-prefix riece-current-channel)
352 (riece-display-message
353 (riece-make-message (riece-current-nickname) riece-current-channel
356 (format "PRIVMSG %s :%s\r\n"
357 (riece-identity-prefix riece-current-channel)
359 (riece-display-message
360 (riece-make-message (riece-current-nickname) riece-current-channel
363 (defun riece-command-enter-message ()
364 "Send the current line to the current channel."
366 (riece-command-send-message (buffer-substring
367 (riece-line-beginning-position)
368 (riece-line-end-position))
370 (let ((next-line-add-newlines t))
373 (defun riece-command-enter-message-as-notice ()
374 "Send the current line to the current channel as NOTICE."
376 (riece-command-send-message (buffer-substring
377 (riece-line-beginning-position)
378 (riece-line-end-position))
380 (let ((next-line-add-newlines t))
383 (defun riece-command-enter-message-to-user (user)
384 "Send the current line to USER."
386 (let ((completion-ignore-case t))
387 (list (riece-completing-read-identity
389 (riece-get-users-on-server (riece-current-server-name))))))
390 (let ((text (buffer-substring
391 (riece-line-beginning-position)
392 (riece-line-end-position))))
394 (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
395 (riece-display-message
396 (riece-make-message (riece-current-nickname) user text nil t)))
397 (let ((next-line-add-newlines t))
400 (defun riece-command-join-channel (target key)
401 (let ((process (riece-server-process (riece-identity-server target))))
403 (error "%s" (substitute-command-keys
404 "Type \\[riece-command-open-server] to open server.")))
405 (riece-process-send-string process
407 (format "JOIN %s :%s\r\n"
408 (riece-identity-prefix target)
410 (format "JOIN %s\r\n"
411 (riece-identity-prefix target))))))
413 (defun riece-command-join-partner (target)
414 (let ((pointer (riece-identity-member target riece-current-channels)))
416 (riece-command-switch-to-channel (car pointer))
417 (riece-join-channel target)
418 (riece-switch-to-channel target))))
420 (defun riece-command-join (target &optional key)
422 (let* ((completion-ignore-case t)
424 (if riece-join-channel-candidate
425 (let ((default (riece-format-identity
426 riece-join-channel-candidate)))
427 (riece-completing-read-identity
428 (format "Channel/User (default %s): " default)
429 (riece-get-identities-on-server (riece-current-server-name))
430 nil nil nil nil default))
431 (riece-completing-read-identity
433 (riece-get-identities-on-server (riece-current-server-name)))))
435 (if (and current-prefix-arg
436 (riece-channel-p (riece-identity-prefix target)))
438 (riece-read-passwd (format "Key for %s: "
439 (riece-format-identity target)))))
441 (let ((pointer (riece-identity-member target riece-current-channels)))
443 (riece-command-switch-to-channel (car pointer))
444 (if (riece-channel-p (riece-identity-prefix target))
445 (riece-command-join-channel target key)
446 (riece-command-join-partner target)))))
448 (defun riece-command-part-channel (target message)
449 (let ((process (riece-server-process (riece-identity-server target))))
450 (riece-process-send-string process
452 (format "PART %s :%s\r\n"
453 (riece-identity-prefix target)
455 (format "PART %s\r\n"
456 (riece-identity-prefix target))))))
458 (defun riece-command-part (target &optional message)
461 (riece-check-channel-commands-are-usable)
462 (let* ((completion-ignore-case t)
464 (riece-completing-read-identity
465 (format "Channel/User (default %s): "
466 (riece-format-identity riece-current-channel))
467 riece-current-channels nil nil nil nil
468 (riece-format-identity riece-current-channel)))
470 (if (and current-prefix-arg
471 (riece-channel-p (riece-identity-prefix target)))
472 (setq message (read-string "Message: ")))
473 (list target message))))
474 (if (riece-identity-member target riece-current-channels)
475 (if (riece-channel-p (riece-identity-prefix target))
476 (riece-command-part-channel target message)
477 (riece-part-channel target))
478 (error "You are not talking with %s" target)))
480 (defun riece-command-change-nickname (nickname)
481 "Change your nickname to NICK."
482 (interactive "sEnter your nickname: ")
483 (riece-send-string (format "NICK %s\r\n" nickname)))
485 (defun riece-command-scroll-down (lines)
486 "Scroll LINES down dialogue buffer from command buffer."
488 (let ((buffer (if (and riece-channel-buffer-mode
489 riece-current-channel)
491 riece-dialogue-buffer)))
492 (if (get-buffer-window buffer)
494 (let ((other-window-scroll-buffer buffer))
495 (scroll-other-window-down lines))
497 (message "Beginning of buffer"))))))
499 (defun riece-command-scroll-up (lines)
500 "Scroll LINES up dialogue buffer from command buffer."
502 (let ((buffer (if (and riece-channel-buffer-mode
503 riece-current-channel)
505 riece-dialogue-buffer)))
506 (if (get-buffer-window buffer)
508 (let ((other-window-scroll-buffer buffer))
509 (scroll-other-window lines))
511 (message "End of buffer"))))))
513 (defun riece-command-user-list-scroll-down (lines)
514 "Scroll LINES down user list buffer from command buffer."
516 (if (get-buffer-window riece-user-list-buffer)
518 (let ((other-window-scroll-buffer riece-user-list-buffer))
519 (scroll-other-window-down lines))
521 (message "Beginning of buffer")))))
523 (defun riece-command-user-list-scroll-up (lines)
524 "Scroll LINES up user list buffer from command buffer."
526 (if (get-buffer-window riece-user-list-buffer)
528 (let ((other-window-scroll-buffer riece-user-list-buffer))
529 (scroll-other-window lines))
531 (message "End of buffer")))))
533 (defun riece-command-toggle-away (&optional message)
534 "Mark yourself as being away."
536 (if (and (not (riece-with-server-buffer (riece-identity-server
537 (riece-current-nickname))
538 (riece-user-get-away (riece-identity-prefix
539 (riece-current-nickname)))))
541 (list (read-from-minibuffer
542 "Away message: " (cons (or riece-away-message "") 0)))))
543 (if (riece-with-server-buffer (riece-identity-server
544 (riece-current-nickname))
545 (riece-user-get-away (riece-identity-prefix
546 (riece-current-nickname))))
547 (riece-send-string "AWAY\r\n")
548 (riece-send-string (format "AWAY :%s\r\n" (or message
549 riece-away-message)))))
551 (defun riece-command-toggle-freeze (&optional arg)
552 "Prevent automatic scrolling of the dialogue window.
553 If prefix argument ARG is non-nil, toggle frozen status."
555 (with-current-buffer (if (and riece-channel-buffer-mode
556 riece-channel-buffer)
558 riece-dialogue-buffer)
559 (setq riece-freeze (if arg
560 (< 0 (prefix-numeric-value arg))
562 (riece-emit-signal 'buffer-freeze-changed
563 (current-buffer) riece-freeze)))
565 (defun riece-command-toggle-own-freeze (&optional arg)
566 "Prevent automatic scrolling of the dialogue window.
567 The difference from `riece-command-freeze' is that your messages are hidden.
568 If prefix argument ARG is non-nil, toggle frozen status."
570 (with-current-buffer (if (and riece-channel-buffer-mode
571 riece-channel-buffer)
573 riece-dialogue-buffer)
575 (< 0 (prefix-numeric-value arg))
576 (not (eq riece-freeze 'own)))
577 (setq riece-freeze 'own)
578 (setq riece-freeze nil)))
579 (riece-emit-signal 'buffer-freeze-changed
580 (current-buffer) riece-freeze))
583 (autoload 'riece-exit "riece"))
584 (defun riece-command-quit (&optional arg)
587 (if (y-or-n-p "Really quit IRC? ")
588 (if riece-server-process-alist
591 (read-string "Message: ")
593 (alist riece-server-process-alist))
595 (riece-quit-server-process (cdr (car alist)) message)
596 (setq alist (cdr alist))))
597 ;; If no server process is available, exit immediately.
600 (defun riece-command-raw (command)
601 "Enter raw IRC command, which is sent to the server."
602 (interactive "sIRC command: ")
603 (riece-send-string (concat command "\r\n")))
605 (defun riece-command-end-of-buffer ()
606 "Get end of the dialogue buffer."
609 (setq buffer (if riece-channel-buffer-mode
611 riece-dialogue-buffer))
612 (or (setq window (get-buffer-window buffer))
613 (setq window (get-buffer-window riece-dialogue-buffer)
614 buffer riece-dialogue-buffer))
616 (save-selected-window
617 (select-window window)
618 (goto-char (point-max))))))
620 (defun riece-command-copy-region (start end)
621 "Move current region between START and END to `kill-ring'."
623 (kill-new (buffer-substring-no-properties start end)))
625 (defun riece-command-open-server (server-name)
627 (list (completing-read "Server: " riece-server-alist)))
628 (if (riece-server-process server-name)
629 (error "%s is already opened" server-name))
631 (riece-server-name-to-server server-name)
634 (defun riece-command-close-server (server-name &optional message)
636 (list (completing-read "Server: " riece-server-process-alist)
637 (if current-prefix-arg
638 (read-string "Message: ")
639 riece-quit-message)))
640 (riece-quit-server-process (riece-server-process server-name) message))
642 (defun riece-command-universal-server-name-argument ()
644 (let* ((riece-overriding-server-name
645 (completing-read "Server: " riece-server-process-alist))
647 (key-binding (read-key-sequence
648 (format "Command to execute on \"%s\":"
649 riece-overriding-server-name)))))
651 (call-interactively command)))
653 (provide 'riece-commands)
655 ;;; riece-commands.el ends here