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-version)
32 (require 'riece-server)
34 (require 'riece-identity)
35 (require 'riece-message)
38 (defun riece-command-switch-to-channel (channel)
39 (interactive (list (riece-completing-read-identity
40 "Channel/User: " riece-current-channels nil t)))
41 (unless (equal channel riece-current-channels)
42 (riece-switch-to-channel channel)
43 (riece-redisplay-buffers)))
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 (completing-read
150 (mapcar #'list (riece-get-users-on-server)))))
151 (list user current-prefix-arg)))
153 (riece-send-string (format "WHOIS %s %s\r\n" user user))
154 (riece-send-string (format "WHOIS %s\r\n" 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 (completing-read
179 (mapcar #'list (riece-get-users-on-server))))))
180 (riece-send-string (format "INVITE %s %s\r\n"
181 user (riece-identity-prefix
182 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
191 (riece-with-server-buffer
192 (riece-identity-server riece-current-channel)
193 (riece-channel-get-users
194 (riece-identity-prefix 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
254 "Channel/User: " riece-current-channels)
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)
280 (riece-with-server-buffer
281 (riece-identity-server riece-current-channel)
282 (riece-channel-get-operators
283 (riece-identity-prefix riece-current-channel))))
284 (completion-ignore-case t)
286 (if current-prefix-arg
287 (setq users (riece-completing-read-multiple
289 (mapcar #'list operators)))
290 (setq users (riece-completing-read-multiple
294 (unless (member user operators)
296 (riece-with-server-buffer
297 (riece-identity-server
298 riece-current-channel)
299 (riece-channel-get-users
300 (riece-identity-prefix
301 riece-current-channel))))))))
302 (list users current-prefix-arg))))
305 (setq group (cons (car users) group)
307 (if (or (= (length group) 3)
310 (format "MODE %s %c%s %s\r\n"
311 (riece-identity-prefix riece-current-channel)
312 (if current-prefix-arg
315 (make-string (length group) ?o)
316 (mapconcat #'identity group " ")))))))
318 (defun riece-command-set-speakers (users &optional arg)
321 (riece-check-channel-commands-are-usable t)
323 (riece-with-server-buffer
324 (riece-identity-server riece-current-channel)
325 (riece-channel-get-speakers
326 (riece-identity-prefix riece-current-channel))))
327 (completion-ignore-case t)
329 (if current-prefix-arg
330 (setq users (riece-completing-read-multiple
332 (mapcar #'list speakers)))
333 (setq users (riece-completing-read-multiple
337 (unless (member user speakers)
339 (riece-with-server-buffer
340 (riece-identity-server
341 riece-current-channel)
342 (riece-channel-get-users
343 (riece-identity-prefix
344 riece-current-channel))))))))
345 (list users current-prefix-arg))))
348 (setq group (cons (car users) group)
350 (if (or (= (length group) 3)
353 (format "MODE %s %c%s %s\r\n"
354 (riece-identity-prefix riece-current-channel)
355 (if current-prefix-arg
358 (make-string (length group) ?v)
359 (mapconcat #'identity group " ")))))))
361 (defun riece-command-send-message (message notice)
362 "Send MESSAGE to the current channel."
363 (if (equal message "")
364 (error "No text to send"))
365 (riece-check-channel-commands-are-usable)
369 (format "NOTICE %s :%s\r\n"
370 (riece-identity-prefix riece-current-channel)
372 (riece-display-message
373 (riece-make-message (riece-current-nickname) riece-current-channel
376 (format "PRIVMSG %s :%s\r\n"
377 (riece-identity-prefix riece-current-channel)
379 (riece-display-message
380 (riece-make-message (riece-current-nickname) riece-current-channel
383 (defun riece-command-enter-message ()
384 "Send the current line to the current channel."
386 (riece-command-send-message (buffer-substring
387 (riece-line-beginning-position)
388 (riece-line-end-position))
390 (let ((next-line-add-newlines t))
393 (defun riece-command-enter-message-as-notice ()
394 "Send the current line to the current channel as NOTICE."
396 (riece-command-send-message (buffer-substring
397 (riece-line-beginning-position)
398 (riece-line-end-position))
400 (let ((next-line-add-newlines t))
403 (defun riece-command-enter-message-to-user (user)
404 "Send the current line to USER."
406 (let ((completion-ignore-case t))
407 (list (completing-read
409 (mapcar #'list (riece-get-users-on-server))))))
410 (let ((text (buffer-substring
411 (riece-line-beginning-position)
412 (riece-line-end-position))))
414 (format "PRIVMSG %s :%s\r\n" user text))
415 (riece-display-message
416 (riece-make-message (riece-current-nickname)
417 (riece-make-identity user (riece-current-server-name))
419 (let ((next-line-add-newlines t))
422 (defun riece-command-join-channel (target key)
423 (let ((process (riece-server-process (riece-identity-server target))))
425 (error "%s" (substitute-command-keys
426 "Type \\[riece-command-open-server] to open server.")))
427 (riece-process-send-string process
429 (format "JOIN %s :%s\r\n"
430 (riece-identity-prefix target)
432 (format "JOIN %s\r\n"
433 (riece-identity-prefix target))))))
435 (defun riece-command-join-partner (target)
436 (let ((pointer (riece-identity-member target riece-current-channels)))
438 (riece-command-switch-to-channel (car pointer))
439 (riece-join-channel target)
440 (riece-switch-to-channel target)
441 (riece-redisplay-buffers))))
443 (defun riece-command-join (target &optional key)
445 (let* ((completion-ignore-case t)
447 (if riece-join-channel-candidate
448 (let ((default (riece-format-identity
449 riece-join-channel-candidate)))
450 (riece-completing-read-identity
451 (format "Channel/User (default %s): " default)
452 riece-current-channels nil nil nil nil default))
453 (riece-completing-read-identity
454 "Channel/User: " riece-current-channels)))
456 (if (and current-prefix-arg
457 (riece-channel-p (riece-identity-prefix target)))
459 (riece-read-passwd (format "Key for %s: "
460 (riece-format-identity target)))))
462 (let ((pointer (riece-identity-member target riece-current-channels)))
464 (riece-command-switch-to-channel (car pointer))
465 (if (riece-channel-p (riece-identity-prefix target))
466 (riece-command-join-channel target key)
467 (riece-command-join-partner target)))))
469 (defun riece-command-part-channel (target message)
470 (let ((process (riece-server-process (riece-identity-server target))))
471 (riece-process-send-string process
473 (format "PART %s :%s\r\n"
474 (riece-identity-prefix target)
476 (format "PART %s\r\n"
477 (riece-identity-prefix target))))))
479 (defun riece-command-part (target &optional message)
482 (riece-check-channel-commands-are-usable)
483 (let* ((completion-ignore-case t)
485 (riece-completing-read-identity
486 (format "Channel/User (default %s): "
487 (riece-format-identity riece-current-channel))
488 riece-current-channels nil nil nil nil
489 (riece-format-identity riece-current-channel)))
491 (if (and current-prefix-arg
492 (riece-channel-p (riece-identity-prefix target)))
493 (setq message (read-string "Message: ")))
494 (list target message))))
495 (if (riece-identity-member target riece-current-channels)
496 (if (riece-channel-p (riece-identity-prefix target))
497 (riece-command-part-channel target message)
498 (riece-part-channel target)
499 (riece-redisplay-buffers))
500 (error "You are not talking with %s" target)))
502 (defun riece-command-change-nickname (nickname)
503 "Change your nickname to NICK."
504 (interactive "sEnter your nickname: ")
505 (riece-send-string (format "NICK %s\r\n" nickname)))
507 (defun riece-command-scroll-down (lines)
508 "Scroll LINES down dialogue buffer from command buffer."
510 (let ((other-window-scroll-buffer
511 (if riece-channel-buffer-mode
513 riece-dialogue-buffer)))
514 (when (get-buffer-window other-window-scroll-buffer)
516 (scroll-other-window-down lines)
518 (message "Beginning of buffer"))))))
520 (defun riece-command-scroll-up (lines)
521 "Scroll LINES up dialogue buffer from command buffer."
523 (let* ((other-window-scroll-buffer
524 (if riece-channel-buffer-mode
526 riece-dialogue-buffer)))
527 (when (get-buffer-window other-window-scroll-buffer)
529 (scroll-other-window lines)
531 (message "End of buffer"))))))
533 (defun riece-command-nick-scroll-down (lines)
534 "Scroll LINES down nick buffer from command buffer."
536 (let ((other-window-scroll-buffer riece-user-list-buffer))
537 (when (get-buffer-window other-window-scroll-buffer)
539 (scroll-other-window-down lines)
541 (message "Beginning of buffer"))))))
543 (defun riece-command-nick-scroll-up (lines)
544 "Scroll LINES up nick buffer from command buffer."
546 (let* ((other-window-scroll-buffer riece-user-list-buffer))
547 (when (get-buffer-window other-window-scroll-buffer)
549 (scroll-other-window lines)
551 (message "End of buffer"))))))
553 (defun riece-command-toggle-away (&optional message)
554 "Mark yourself as being away."
556 (if (and (not (riece-with-server-buffer (riece-identity-server
557 (riece-current-nickname))
558 (riece-user-get-away (riece-identity-prefix
559 (riece-current-nickname)))))
560 (or (null riece-away-message)
562 (let ((message (read-string "Away message: ")))
565 (riece-send-string (format "AWAY :%s\r\n" message))
566 (riece-send-string "AWAY\r\n")))
568 (defun riece-command-toggle-freeze (&optional arg)
569 "Prevent automatic scrolling of the dialogue window.
570 If prefix argument ARG is non-nil, toggle frozen status."
572 (with-current-buffer (if (and riece-channel-buffer-mode
573 riece-channel-buffer)
575 riece-dialogue-buffer)
576 (setq riece-freeze (if arg
577 (< 0 (prefix-numeric-value arg))
578 (not riece-freeze))))
579 (riece-update-status-indicators)
580 (force-mode-line-update t))
582 (defun riece-command-toggle-own-freeze (&optional arg)
583 "Prevent automatic scrolling of the dialogue window.
584 The difference from `riece-command-freeze' is that your messages are hidden.
585 If prefix argument ARG is non-nil, toggle frozen status."
587 (with-current-buffer (if (and riece-channel-buffer-mode
588 riece-channel-buffer)
590 riece-dialogue-buffer)
592 (< 0 (prefix-numeric-value arg))
593 (not (eq riece-freeze 'own)))
594 (setq riece-freeze 'own)
595 (setq riece-freeze nil)))
596 (riece-update-status-indicators)
597 (force-mode-line-update t))
599 (defun riece-command-quit (&optional arg)
602 (if (y-or-n-p "Really quit IRC? ")
605 (read-string "Message: ")
606 (or riece-quit-message
607 (riece-extended-version))))
608 (alist riece-server-process-alist))
610 (riece-quit-server-process (cdr (car alist)) message)
611 (setq alist (cdr alist))))))
613 (defun riece-command-raw (command)
614 "Enter raw IRC command, which is sent to the server."
615 (interactive "sIRC command: ")
616 (riece-send-string (concat command "\r\n")))
618 (defun riece-command-end-of-buffer ()
619 "Get end of the dialogue buffer."
622 (setq buffer (if riece-channel-buffer-mode
624 riece-dialogue-buffer))
625 (or (setq window (get-buffer-window buffer))
626 (setq window (get-buffer-window riece-dialogue-buffer)
627 buffer riece-dialogue-buffer))
629 (save-selected-window
630 (select-window window)
631 (goto-char (point-max))))))
633 (defun riece-command-copy-region (start end)
634 "Move current region between START and END to `kill-ring'."
636 (kill-new (buffer-substring-no-properties start end)))
638 (defun riece-command-open-server (server-name)
640 (list (completing-read "Server: " riece-server-alist)))
641 (if (riece-server-process server-name)
642 (error "%s is already opened" server-name))
644 (riece-server-name-to-server server-name)
647 (defun riece-command-close-server (server-name &optional message)
649 (list (completing-read "Server: " riece-server-process-alist)
650 (if current-prefix-arg
651 (read-string "Message: ")
652 (or riece-quit-message
653 (riece-extended-version)))))
654 (riece-quit-server-process (riece-server-process server-name) message))
656 (defun riece-command-universal-server-name-argument ()
658 (let* ((riece-overriding-server-name
659 (completing-read "Server: " riece-server-process-alist))
661 (key-binding (read-key-sequence
662 (format "Command to execute on \"%s\":"
663 riece-overriding-server-name)))))
665 (call-interactively command)))
667 (provide 'riece-commands)
669 ;;; riece-commands.el ends here