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-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 (completing-read
149 (mapcar #'list (riece-get-users-on-server)))))
150 (list user current-prefix-arg)))
152 (riece-send-string (format "WHOIS %s %s\r\n" user user))
153 (riece-send-string (format "WHOIS %s\r\n" user))))
155 (defun riece-command-topic (topic)
158 (riece-check-channel-commands-are-usable t)
159 (list (read-from-minibuffer
160 "Topic: " (cons (or (riece-with-server-buffer
161 (riece-identity-server
162 riece-current-channel)
163 (riece-channel-get-topic
164 (riece-identity-prefix
165 riece-current-channel)))
168 (riece-send-string (format "TOPIC %s :%s\r\n"
169 (riece-identity-prefix riece-current-channel)
172 (defun riece-command-invite (user)
174 (let ((completion-ignore-case t))
175 (riece-check-channel-commands-are-usable t)
176 (list (completing-read
178 (mapcar #'list (riece-get-users-on-server))))))
179 (riece-send-string (format "INVITE %s %s\r\n"
180 user (riece-identity-prefix
181 riece-current-channel))))
183 (defun riece-command-kick (user &optional message)
185 (let ((completion-ignore-case t))
186 (riece-check-channel-commands-are-usable t)
187 (list (completing-read
190 (riece-with-server-buffer
191 (riece-identity-server riece-current-channel)
192 (riece-channel-get-users
193 (riece-identity-prefix 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
253 "Channel/User: " riece-current-channels)
254 (riece-check-channel-commands-are-usable t)
255 riece-current-channel))
256 (riece-overriding-server-name (riece-identity-server channel))
257 (riece-temp-minibuffer-message
258 (concat "[Available modes: "
259 (riece-with-server-buffer (riece-identity-server channel)
260 (if (riece-channel-p (riece-identity-prefix channel))
261 (if riece-supported-channel-modes
262 (apply #'string riece-supported-channel-modes))
263 (if riece-supported-user-modes
264 (apply #'string riece-supported-user-modes))))
267 (read-from-minibuffer
268 (concat (riece-concat-channel-modes
269 channel "Mode (? for help)") ": ")
270 nil riece-minibuffer-map))))
271 (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
274 (defun riece-command-set-operators (users &optional arg)
277 (riece-check-channel-commands-are-usable t)
279 (riece-with-server-buffer
280 (riece-identity-server riece-current-channel)
281 (riece-channel-get-operators
282 (riece-identity-prefix riece-current-channel))))
283 (completion-ignore-case t)
285 (if current-prefix-arg
286 (setq users (riece-completing-read-multiple
288 (mapcar #'list operators)))
289 (setq users (riece-completing-read-multiple
293 (unless (member user operators)
295 (riece-with-server-buffer
296 (riece-identity-server
297 riece-current-channel)
298 (riece-channel-get-users
299 (riece-identity-prefix
300 riece-current-channel))))))))
301 (list users current-prefix-arg))))
304 (setq group (cons (car users) group)
306 (if (or (= (length group) 3)
309 (format "MODE %s %c%s %s\r\n"
310 (riece-identity-prefix riece-current-channel)
311 (if current-prefix-arg
314 (make-string (length group) ?o)
315 (mapconcat #'identity group " ")))))))
317 (defun riece-command-set-speakers (users &optional arg)
320 (riece-check-channel-commands-are-usable t)
322 (riece-with-server-buffer
323 (riece-identity-server riece-current-channel)
324 (riece-channel-get-speakers
325 (riece-identity-prefix riece-current-channel))))
326 (completion-ignore-case t)
328 (if current-prefix-arg
329 (setq users (riece-completing-read-multiple
331 (mapcar #'list speakers)))
332 (setq users (riece-completing-read-multiple
336 (unless (member user speakers)
338 (riece-with-server-buffer
339 (riece-identity-server
340 riece-current-channel)
341 (riece-channel-get-users
342 (riece-identity-prefix
343 riece-current-channel))))))))
344 (list users current-prefix-arg))))
347 (setq group (cons (car users) group)
349 (if (or (= (length group) 3)
352 (format "MODE %s %c%s %s\r\n"
353 (riece-identity-prefix riece-current-channel)
354 (if current-prefix-arg
357 (make-string (length group) ?v)
358 (mapconcat #'identity group " ")))))))
360 (defun riece-command-send-message (message notice)
361 "Send MESSAGE to the current channel."
362 (if (equal message "")
363 (error "No text to send"))
364 (riece-check-channel-commands-are-usable)
368 (format "NOTICE %s :%s\r\n"
369 (riece-identity-prefix riece-current-channel)
371 (riece-display-message
372 (riece-make-message (riece-current-nickname) riece-current-channel
375 (format "PRIVMSG %s :%s\r\n"
376 (riece-identity-prefix riece-current-channel)
378 (riece-display-message
379 (riece-make-message (riece-current-nickname) riece-current-channel
382 (defun riece-command-enter-message ()
383 "Send the current line to the current channel."
385 (riece-command-send-message (buffer-substring
386 (riece-line-beginning-position)
387 (riece-line-end-position))
389 (let ((next-line-add-newlines t))
392 (defun riece-command-enter-message-as-notice ()
393 "Send the current line to the current channel as NOTICE."
395 (riece-command-send-message (buffer-substring
396 (riece-line-beginning-position)
397 (riece-line-end-position))
399 (let ((next-line-add-newlines t))
402 (defun riece-command-enter-message-to-user (user)
403 "Send the current line to USER."
405 (let ((completion-ignore-case t))
406 (list (completing-read
408 (mapcar #'list (riece-get-users-on-server))))))
409 (let ((text (buffer-substring
410 (riece-line-beginning-position)
411 (riece-line-end-position))))
413 (format "PRIVMSG %s :%s\r\n" user text))
414 (riece-display-message
415 (riece-make-message (riece-current-nickname)
416 (riece-make-identity user (riece-current-server-name))
418 (let ((next-line-add-newlines t))
421 (defun riece-command-join-channel (target key)
422 (let ((process (riece-server-process (riece-identity-server target))))
424 (error "%s" (substitute-command-keys
425 "Type \\[riece-command-open-server] to open server.")))
426 (riece-process-send-string process
428 (format "JOIN %s :%s\r\n"
429 (riece-identity-prefix target)
431 (format "JOIN %s\r\n"
432 (riece-identity-prefix target))))))
434 (defun riece-command-join-partner (target)
435 (let ((pointer (riece-identity-member target riece-current-channels)))
437 (riece-command-switch-to-channel (car pointer))
438 (riece-join-channel target)
439 (riece-switch-to-channel target)
440 (riece-redisplay-buffers))))
442 (defun riece-command-join (target &optional key)
444 (let* ((completion-ignore-case t)
446 (if riece-join-channel-candidate
447 (let ((default (riece-format-identity
448 riece-join-channel-candidate)))
449 (riece-completing-read-identity
450 (format "Channel/User (default %s): " default)
451 riece-current-channels nil nil nil nil default))
452 (riece-completing-read-identity
453 "Channel/User: " riece-current-channels)))
455 (if (and current-prefix-arg
456 (riece-channel-p (riece-identity-prefix target)))
458 (riece-read-passwd (format "Key for %s: "
459 (riece-format-identity target)))))
461 (let ((pointer (riece-identity-member target riece-current-channels)))
463 (riece-command-switch-to-channel (car pointer))
464 (if (riece-channel-p (riece-identity-prefix target))
465 (riece-command-join-channel target key)
466 (riece-command-join-partner target)))))
468 (defun riece-command-part-channel (target message)
469 (let ((process (riece-server-process (riece-identity-server target))))
470 (riece-process-send-string process
472 (format "PART %s :%s\r\n"
473 (riece-identity-prefix target)
475 (format "PART %s\r\n"
476 (riece-identity-prefix target))))))
478 (defun riece-command-part (target &optional message)
481 (riece-check-channel-commands-are-usable)
482 (let* ((completion-ignore-case t)
484 (riece-completing-read-identity
485 (format "Channel/User (default %s): "
486 (riece-format-identity riece-current-channel))
487 riece-current-channels nil nil nil nil
488 (riece-format-identity riece-current-channel)))
490 (if (and current-prefix-arg
491 (riece-channel-p (riece-identity-prefix target)))
492 (setq message (read-string "Message: ")))
493 (list target message))))
494 (if (riece-identity-member target riece-current-channels)
495 (if (riece-channel-p (riece-identity-prefix target))
496 (riece-command-part-channel target message)
497 (riece-part-channel target)
498 (riece-redisplay-buffers))
499 (error "You are not talking with %s" target)))
501 (defun riece-command-change-nickname (nickname)
502 "Change your nickname to NICK."
503 (interactive "sEnter your nickname: ")
504 (riece-send-string (format "NICK %s\r\n" nickname)))
506 (defun riece-command-scroll-down (lines)
507 "Scroll LINES down dialogue buffer from command buffer."
509 (let ((other-window-scroll-buffer
510 (if riece-channel-buffer-mode
512 riece-dialogue-buffer)))
513 (when (get-buffer-window other-window-scroll-buffer)
515 (scroll-other-window-down lines)
517 (message "Beginning of buffer"))))))
519 (defun riece-command-scroll-up (lines)
520 "Scroll LINES up dialogue buffer from command buffer."
522 (let* ((other-window-scroll-buffer
523 (if riece-channel-buffer-mode
525 riece-dialogue-buffer)))
526 (when (get-buffer-window other-window-scroll-buffer)
528 (scroll-other-window lines)
530 (message "End of buffer"))))))
532 (defun riece-command-nick-scroll-down (lines)
533 "Scroll LINES down nick buffer from command buffer."
535 (let ((other-window-scroll-buffer riece-user-list-buffer))
536 (when (get-buffer-window other-window-scroll-buffer)
538 (scroll-other-window-down lines)
540 (message "Beginning of buffer"))))))
542 (defun riece-command-nick-scroll-up (lines)
543 "Scroll LINES up nick buffer from command buffer."
545 (let* ((other-window-scroll-buffer riece-user-list-buffer))
546 (when (get-buffer-window other-window-scroll-buffer)
548 (scroll-other-window lines)
550 (message "End of buffer"))))))
552 (defun riece-command-toggle-away (&optional message)
553 "Mark yourself as being away."
555 (if (and (not (riece-with-server-buffer (riece-identity-server
556 (riece-current-nickname))
557 (riece-user-get-away (riece-identity-prefix
558 (riece-current-nickname)))))
559 (or (null riece-away-message)
561 (let ((message (read-string "Away message: ")))
564 (riece-send-string (format "AWAY :%s\r\n" message))
565 (riece-send-string "AWAY\r\n")))
567 (defun riece-command-toggle-freeze (&optional arg)
568 "Prevent automatic scrolling of the dialogue window.
569 If prefix argument ARG is non-nil, toggle frozen status."
571 (with-current-buffer (if (and riece-channel-buffer-mode
572 riece-channel-buffer)
574 riece-dialogue-buffer)
575 (setq riece-freeze (if arg
576 (< 0 (prefix-numeric-value arg))
577 (not riece-freeze))))
578 (riece-update-status-indicators)
579 (force-mode-line-update t))
581 (defun riece-command-toggle-own-freeze (&optional arg)
582 "Prevent automatic scrolling of the dialogue window.
583 The difference from `riece-command-freeze' is that your messages are hidden.
584 If prefix argument ARG is non-nil, toggle frozen status."
586 (with-current-buffer (if (and riece-channel-buffer-mode
587 riece-channel-buffer)
589 riece-dialogue-buffer)
591 (< 0 (prefix-numeric-value arg))
592 (not (eq riece-freeze 'own)))
593 (setq riece-freeze 'own)
594 (setq riece-freeze nil)))
595 (riece-update-status-indicators)
596 (force-mode-line-update t))
598 (defun riece-command-quit (&optional arg)
601 (if (y-or-n-p "Really quit IRC? ")
604 (read-string "Message: ")
605 (or riece-quit-message
606 (riece-extended-version))))
607 (alist riece-server-process-alist))
609 (riece-quit-server-process (cdr (car alist)) message)
610 (setq alist (cdr alist))))))
612 (defun riece-command-raw (command)
613 "Enter raw IRC command, which is sent to the server."
614 (interactive "sIRC command: ")
615 (riece-send-string (concat command "\r\n")))
617 (defun riece-command-end-of-buffer ()
618 "Get end of the dialogue buffer."
621 (setq buffer (if riece-channel-buffer-mode
623 riece-dialogue-buffer))
624 (or (setq window (get-buffer-window buffer))
625 (setq window (get-buffer-window riece-dialogue-buffer)
626 buffer riece-dialogue-buffer))
628 (save-selected-window
629 (select-window window)
630 (goto-char (point-max))))))
632 (defun riece-command-copy-region (start end)
633 "Move current region between START and END to `kill-ring'."
635 (kill-new (buffer-substring-no-properties start end)))
637 (defun riece-command-open-server (server-name)
639 (list (completing-read "Server: " riece-server-alist)))
640 (if (riece-server-process server-name)
641 (error "%s is already opened" server-name))
643 (riece-server-name-to-server server-name)
646 (defun riece-command-close-server (server-name &optional message)
648 (list (completing-read "Server: " riece-server-process-alist)
649 (if current-prefix-arg
650 (read-string "Message: ")
651 (or riece-quit-message
652 (riece-extended-version)))))
653 (riece-quit-server-process (riece-server-process server-name) message))
655 (defun riece-command-universal-server-name-argument ()
657 (let* ((riece-overriding-server-name
658 (completing-read "Server: " riece-server-process-alist))
660 (key-binding (read-key-sequence
661 (format "Command to execute on \"%s\":"
662 riece-overriding-server-name)))))
664 (call-interactively command)))
666 (provide 'riece-commands)
668 ;;; riece-commands.el ends here