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)
157 (list (read-from-minibuffer
158 "Topic: " (cons (or (riece-with-server-buffer
159 (riece-identity-server riece-current-channel)
160 (riece-channel-get-topic
161 (riece-identity-prefix
162 riece-current-channel)))
165 (riece-send-string (format "TOPIC %s :%s\r\n"
166 (riece-identity-prefix riece-current-channel)
169 (defun riece-command-invite (user)
171 (let ((completion-ignore-case t))
172 (unless (and riece-current-channel
173 (riece-channel-p (riece-identity-prefix
174 riece-current-channel)))
175 (error "Not on a channel"))
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 (unless (and riece-current-channel
187 (riece-channel-p (riece-identity-prefix
188 riece-current-channel)))
189 (error "Not on a channel"))
190 (list (completing-read
193 (riece-with-server-buffer
194 (riece-identity-server riece-current-channel)
195 (riece-channel-get-users
196 (riece-identity-prefix riece-current-channel)))))
197 (if current-prefix-arg
198 (read-string "Message: ")))))
201 (format "KICK %s %s :%s\r\n"
202 (riece-identity-prefix riece-current-channel)
204 (format "KICK %s %s\r\n"
205 (riece-identity-prefix riece-current-channel)
208 (defun riece-command-names (pattern)
210 (let ((completion-ignore-case t))
211 (list (read-from-minibuffer
213 (if (and riece-current-channel
214 (riece-channel-p (riece-identity-prefix
215 riece-current-channel)))
216 (cons (riece-format-identity riece-current-channel t)
218 (if (or (not (equal pattern ""))
219 (yes-or-no-p "Really want to query NAMES without argument? "))
220 (riece-send-string (format "NAMES %s\r\n" pattern))))
222 (defun riece-command-who (pattern)
224 (let ((completion-ignore-case t))
225 (list (read-from-minibuffer
227 (if (and riece-current-channel
228 (riece-channel-p (riece-identity-prefix
229 riece-current-channel)))
230 (cons (riece-format-identity riece-current-channel t)
232 (if (or (not (equal pattern ""))
233 (yes-or-no-p "Really want to query WHO without argument? "))
234 (riece-send-string (format "WHO %s\r\n" pattern))))
236 (defun riece-command-list (pattern)
238 (let ((completion-ignore-case t))
239 (list (read-from-minibuffer
241 (if (and riece-current-channel
242 (riece-channel-p (riece-identity-prefix
243 riece-current-channel)))
244 (cons (riece-identity-prefix riece-current-channel)
246 (if (or (not (equal pattern ""))
247 (yes-or-no-p "Really want to query LIST without argument? "))
248 (riece-send-string (format "LIST %s\r\n" pattern))))
250 (defun riece-command-change-mode (channel change)
252 (let* ((completion-ignore-case t)
254 (if current-prefix-arg
255 (riece-completing-read-identity
256 "Channel/User: " riece-current-channels)
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-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-with-server-buffer
321 (riece-identity-server riece-current-channel)
322 (riece-channel-get-speakers
323 (riece-identity-prefix riece-current-channel))))
324 (completion-ignore-case t)
326 (if current-prefix-arg
327 (setq users (riece-completing-read-multiple
329 (mapcar #'list speakers)))
330 (setq users (riece-completing-read-multiple
334 (unless (member user speakers)
336 (riece-with-server-buffer
337 (riece-identity-server
338 riece-current-channel)
339 (riece-channel-get-users
340 (riece-identity-prefix
341 riece-current-channel))))))))
342 (list users current-prefix-arg)))
345 (setq group (cons (car users) group)
347 (if (or (= (length group) 3)
350 (format "MODE %s %c%s %s\r\n"
351 (riece-identity-prefix riece-current-channel)
352 (if current-prefix-arg
355 (make-string (length group) ?v)
356 (mapconcat #'identity group " ")))))))
358 (defun riece-command-send-message (message notice)
359 "Send MESSAGE to the current channel."
360 (if (equal message "")
361 (error "No text to send"))
362 (unless riece-current-channel
363 (error (substitute-command-keys
364 "Type \\[riece-command-join] to join a channel")))
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-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)
421 (riece-redisplay-buffers))))
423 (defun riece-command-join (target &optional key)
425 (let* ((completion-ignore-case t)
427 (if riece-join-channel-candidate
428 (let ((default (riece-format-identity
429 riece-join-channel-candidate)))
430 (riece-completing-read-identity
431 (format "Channel/User (default %s): " default)
432 riece-current-channels nil nil nil nil default))
433 (riece-completing-read-identity
434 "Channel/User: " riece-current-channels)))
436 (if (and current-prefix-arg
437 (riece-channel-p (riece-identity-prefix target)))
439 (riece-read-passwd (format "Key for %s: "
440 (riece-format-identity target)))))
442 (let ((pointer (riece-identity-member target riece-current-channels)))
444 (riece-command-switch-to-channel (car pointer))
445 (if (riece-channel-p (riece-identity-prefix target))
446 (riece-command-join-channel target key)
447 (riece-command-join-partner target)))))
449 (defun riece-command-part-channel (target message)
450 (let ((process (riece-server-process (riece-identity-server target))))
452 (error "%s" (substitute-command-keys
453 "Type \\[riece-command-open-server] to open server.")))
454 (riece-process-send-string process
456 (format "PART %s :%s\r\n"
457 (riece-identity-prefix target)
459 (format "PART %s\r\n"
460 (riece-identity-prefix target))))))
462 (defun riece-command-part (target &optional message)
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 ((other-window-scroll-buffer
492 (if riece-channel-buffer-mode
494 riece-dialogue-buffer)))
495 (when (get-buffer-window other-window-scroll-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* ((other-window-scroll-buffer
505 (if riece-channel-buffer-mode
507 riece-dialogue-buffer)))
508 (when (get-buffer-window other-window-scroll-buffer)
510 (scroll-other-window lines)
512 (message "End of buffer"))))))
514 (defun riece-command-nick-scroll-down (lines)
515 "Scroll LINES down nick buffer from command buffer."
517 (let ((other-window-scroll-buffer riece-user-list-buffer))
518 (when (get-buffer-window other-window-scroll-buffer)
520 (scroll-other-window-down lines)
522 (message "Beginning of buffer"))))))
524 (defun riece-command-nick-scroll-up (lines)
525 "Scroll LINES up nick buffer from command buffer."
527 (let* ((other-window-scroll-buffer riece-user-list-buffer))
528 (when (get-buffer-window other-window-scroll-buffer)
530 (scroll-other-window lines)
532 (message "End of buffer"))))))
534 (defun riece-command-toggle-away (&optional message)
535 "Mark yourself as being away."
537 (if (and (not (riece-user-get-away (riece-current-nickname)))
538 (or (null riece-away-message)
540 (let ((message (read-string "Away message: ")))
543 (riece-send-string (format "AWAY :%s\r\n" message))
544 (riece-send-string "AWAY\r\n")))
546 (defun riece-command-toggle-freeze (&optional arg)
547 "Prevent automatic scrolling of the dialogue window.
548 If prefix argument ARG is non-nil, toggle frozen status."
550 (with-current-buffer (if (and riece-channel-buffer-mode
551 riece-channel-buffer)
553 riece-dialogue-buffer)
554 (setq riece-freeze (if arg
555 (< 0 (prefix-numeric-value arg))
556 (not riece-freeze))))
557 (riece-update-status-indicators)
558 (force-mode-line-update t))
560 (defun riece-command-toggle-own-freeze (&optional arg)
561 "Prevent automatic scrolling of the dialogue window.
562 The difference from `riece-command-freeze' is that your messages are hidden.
563 If prefix argument ARG is non-nil, toggle frozen status."
565 (with-current-buffer (if (and riece-channel-buffer-mode
566 riece-channel-buffer)
568 riece-dialogue-buffer)
570 (< 0 (prefix-numeric-value arg))
571 (not (eq riece-freeze 'own)))
572 (setq riece-freeze 'own)
573 (setq riece-freeze nil)))
574 (riece-update-status-indicators)
575 (force-mode-line-update t))
577 (defun riece-command-quit (&optional arg)
580 (if (y-or-n-p "Really quit IRC? ")
583 (read-string "Message: ")
584 (or riece-quit-message
585 (riece-extended-version))))
586 (process-list riece-process-list))
588 (riece-quit-server-process (car process-list) message)
589 (setq process-list (cdr process-list))))))
591 (defun riece-command-raw (command)
592 "Enter raw IRC command, which is sent to the server."
593 (interactive "sIRC command: ")
594 (riece-send-string (concat command "\r\n")))
596 (defun riece-command-end-of-buffer ()
597 "Get end of the dialogue buffer."
600 (setq buffer (if riece-channel-buffer-mode
602 riece-dialogue-buffer))
603 (or (setq window (get-buffer-window buffer))
604 (setq window (get-buffer-window riece-dialogue-buffer)
605 buffer riece-dialogue-buffer))
607 (save-selected-window
608 (select-window window)
609 (goto-char (point-max))))))
611 (defun riece-command-copy-region (start end)
612 "Move current region between START and END to `kill-ring'."
614 (kill-new (buffer-substring-no-properties start end)))
616 (defun riece-command-open-server (server-name)
618 (list (completing-read "Server: " riece-server-alist)))
619 (if (riece-server-process server-name)
620 (error "%s is already opened" server-name))
622 (riece-server-name-to-server server-name)
625 (defun riece-command-close-server (server-name &optional message)
627 (list (completing-read
631 (with-current-buffer (process-buffer process)
632 (list riece-server-name)))
634 (if current-prefix-arg
635 (read-string "Message: ")
636 (or riece-quit-message
637 (riece-extended-version)))))
638 (riece-quit-server-process (riece-server-process server-name) message))
640 (defun riece-command-universal-server-name-argument ()
642 (let* ((riece-overriding-server-name
647 (with-current-buffer (process-buffer process)
648 (list riece-server-name)))
649 riece-process-list)))
651 (key-binding (read-key-sequence
652 (format "Command to execute on \"%s\":"
653 riece-overriding-server-name)))))
655 (call-interactively command)))
657 (provide 'riece-commands)
659 ;;; riece-commands.el ends here