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 (customize-set-variable 'riece-layout name))
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 (completing-read
148 (mapcar #'list (riece-get-users-on-server)))))
149 (list user current-prefix-arg)))
151 (riece-send-string (format "WHOIS %s %s\r\n" user user))
152 (riece-send-string (format "WHOIS %s\r\n" user))))
154 (defun riece-command-topic (topic)
156 (list (read-from-minibuffer
157 "Topic: " (cons (or (riece-with-server-buffer
158 (riece-identity-server riece-current-channel)
159 (riece-channel-get-topic
160 (riece-identity-prefix
161 riece-current-channel)))
164 (riece-send-string (format "TOPIC %s :%s\r\n"
165 (riece-identity-prefix riece-current-channel)
168 (defun riece-command-invite (user)
170 (let ((completion-ignore-case t))
171 (unless (and riece-current-channel
172 (riece-channel-p (riece-identity-prefix
173 riece-current-channel)))
174 (error "Not on a channel"))
175 (list (completing-read
177 (mapcar #'list (riece-get-users-on-server))))))
178 (riece-send-string (format "INVITE %s %s\r\n"
179 user (riece-identity-prefix
180 riece-current-channel))))
182 (defun riece-command-kick (user &optional message)
184 (let ((completion-ignore-case t))
185 (unless (and riece-current-channel
186 (riece-channel-p (riece-identity-prefix
187 riece-current-channel)))
188 (error "Not on a channel"))
189 (list (completing-read
191 (mapcar #'list (riece-channel-get-users
192 riece-current-channel)))
193 (if current-prefix-arg
194 (read-string "Message: ")))))
197 (format "KICK %s %s :%s\r\n"
198 (riece-identity-prefix riece-current-channel)
200 (format "KICK %s %s\r\n"
201 (riece-identity-prefix riece-current-channel)
204 (defun riece-command-names (pattern)
206 (let ((completion-ignore-case t))
207 (list (read-from-minibuffer
209 (if (and riece-current-channel
210 (riece-channel-p (riece-identity-prefix
211 riece-current-channel)))
212 (cons (riece-format-identity riece-current-channel t)
214 (if (or (not (equal pattern ""))
215 (yes-or-no-p "Really want to query NAMES without argument? "))
216 (riece-send-string (format "NAMES %s\r\n" pattern))))
218 (defun riece-command-who (pattern)
220 (let ((completion-ignore-case t))
221 (list (read-from-minibuffer
223 (if (and riece-current-channel
224 (riece-channel-p (riece-identity-prefix
225 riece-current-channel)))
226 (cons (riece-format-identity riece-current-channel t)
228 (if (or (not (equal pattern ""))
229 (yes-or-no-p "Really want to query WHO without argument? "))
230 (riece-send-string (format "WHO %s\r\n" pattern))))
232 (defun riece-command-list (pattern)
234 (let ((completion-ignore-case t))
235 (list (read-from-minibuffer
237 (if (and riece-current-channel
238 (riece-channel-p (riece-identity-prefix
239 riece-current-channel)))
240 (cons (riece-format-identity riece-current-channel t)
242 (if (or (not (equal pattern ""))
243 (yes-or-no-p "Really want to query LIST without argument? "))
244 (riece-send-string (format "LIST %s\r\n" pattern))))
246 (defun riece-command-change-mode (channel change)
248 (let* ((completion-ignore-case t)
250 (if current-prefix-arg
251 (riece-completing-read-identity
252 "Channel/User: " riece-current-channels)
253 riece-current-channel))
254 (riece-overriding-server-name (riece-identity-server channel))
255 (riece-temp-minibuffer-message
256 (concat "[Available modes: "
257 (riece-with-server-buffer (riece-identity-server channel)
258 (if (riece-channel-p (riece-identity-prefix channel))
259 (if riece-supported-channel-modes
260 (apply #'string riece-supported-channel-modes))
261 (if riece-supported-user-modes
262 (apply #'string riece-supported-user-modes))))
265 (read-from-minibuffer
266 (concat (riece-concat-channel-modes
267 channel "Mode (? for help)") ": ")
268 nil riece-minibuffer-map))))
269 (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
272 (defun riece-command-set-operators (users &optional arg)
275 (riece-with-server-buffer
276 (riece-identity-server riece-current-channel)
277 (riece-channel-get-operators
278 (riece-identity-prefix riece-current-channel))))
279 (completion-ignore-case t)
281 (if current-prefix-arg
282 (setq users (riece-completing-read-multiple
284 (mapcar #'list operators)))
285 (setq users (riece-completing-read-multiple
289 (unless (member user operators)
291 (riece-with-server-buffer
292 (riece-identity-server
293 riece-current-channel)
294 (riece-channel-get-users
295 (riece-identity-prefix
296 riece-current-channel))))))))
297 (list users current-prefix-arg)))
300 (setq group (cons (car users) group)
302 (if (or (= (length group) 3)
305 (format "MODE %s %c%s %s\r\n"
306 (riece-identity-prefix riece-current-channel)
307 (if current-prefix-arg
310 (make-string (length group) ?o)
311 (mapconcat #'identity group " ")))))))
313 (defun riece-command-set-speakers (users &optional arg)
316 (riece-with-server-buffer
317 (riece-identity-server riece-current-channel)
318 (riece-channel-get-speakers
319 (riece-identity-prefix riece-current-channel))))
320 (completion-ignore-case t)
322 (if current-prefix-arg
323 (setq users (riece-completing-read-multiple
325 (mapcar #'list speakers)))
326 (setq users (riece-completing-read-multiple
330 (unless (member user speakers)
332 (riece-with-server-buffer
333 (riece-identity-server
334 riece-current-channel)
335 (riece-channel-get-users
336 (riece-identity-prefix
337 riece-current-channel))))))))
338 (list users current-prefix-arg)))
341 (setq group (cons (car users) group)
343 (if (or (= (length group) 3)
346 (format "MODE %s %c%s %s\r\n"
347 (riece-identity-prefix riece-current-channel)
348 (if current-prefix-arg
351 (make-string (length group) ?v)
352 (mapconcat #'identity group " ")))))))
354 (defun riece-command-send-message (message notice)
355 "Send MESSAGE to the current channel."
356 (if (equal message "")
357 (error "No text to send"))
358 (unless riece-current-channel
359 (error (substitute-command-keys
360 "Type \\[riece-command-join] to join a channel")))
364 (format "NOTICE %s :%s\r\n"
365 (riece-identity-prefix riece-current-channel)
367 (riece-display-message
368 (riece-make-message (riece-current-nickname) riece-current-channel
371 (format "PRIVMSG %s :%s\r\n"
372 (riece-identity-prefix riece-current-channel)
374 (riece-display-message
375 (riece-make-message (riece-current-nickname) riece-current-channel
378 (defun riece-command-enter-message ()
379 "Send the current line to the current channel."
381 (riece-command-send-message (buffer-substring
382 (riece-line-beginning-position)
383 (riece-line-end-position))
385 (let ((next-line-add-newlines t))
388 (defun riece-command-enter-message-as-notice ()
389 "Send the current line to the current channel as NOTICE."
391 (riece-command-send-message (buffer-substring
392 (riece-line-beginning-position)
393 (riece-line-end-position))
395 (let ((next-line-add-newlines t))
398 (defun riece-command-join-channel (target key)
399 (let ((process (riece-server-process (riece-identity-server target))))
401 (error "%s" (substitute-command-keys
402 "Type \\[riece-command-open-server] to open server.")))
403 (riece-process-send-string process
405 (format "JOIN %s :%s\r\n"
406 (riece-identity-prefix target)
408 (format "JOIN %s\r\n"
409 (riece-identity-prefix target))))))
411 (defun riece-command-join-partner (target)
412 (let ((pointer (riece-identity-member target riece-current-channels)))
414 (riece-command-switch-to-channel (car pointer))
415 (riece-join-channel target)
416 (riece-switch-to-channel target)
417 (riece-redisplay-buffers))))
419 (defun riece-command-join (target &optional key)
421 (let* ((completion-ignore-case t)
423 (riece-completing-read-identity
424 "Channel/User: " riece-current-channels))
426 (if (and current-prefix-arg
427 (riece-channel-p target))
429 (riece-read-passwd (format "Key for %s: " target))))
431 (let ((pointer (riece-identity-member target riece-current-channels)))
433 (riece-command-switch-to-channel (car pointer))
434 (if (riece-channel-p (riece-identity-prefix target))
435 (riece-command-join-channel target key)
436 (riece-command-join-partner target)))))
438 (defun riece-command-part-channel (target message)
439 (let ((process (riece-server-process (riece-identity-server target))))
441 (error "%s" (substitute-command-keys
442 "Type \\[riece-command-open-server] to open server.")))
443 (riece-process-send-string process
445 (format "PART %s :%s\r\n"
446 (riece-identity-prefix target)
448 (format "PART %s\r\n"
449 (riece-identity-prefix target))))))
451 (defun riece-command-part (target &optional message)
453 (let* ((completion-ignore-case t)
455 (riece-completing-read-identity
456 "Channel/User: " riece-current-channels nil nil
457 (cons (riece-format-identity riece-current-channel) 0)))
459 (if (and current-prefix-arg
460 (riece-channel-p (riece-identity-prefix target)))
461 (setq message (read-string "Message: ")))
462 (list target message)))
463 (if (riece-identity-member target riece-current-channels)
464 (if (riece-channel-p (riece-identity-prefix target))
465 (riece-command-part-channel target message)
466 (riece-part-channel target)
467 (riece-redisplay-buffers))
468 (error "You are not talking with %s" target)))
470 (defun riece-command-change-nickname (nickname)
471 "Change your nickname to NICK."
472 (interactive "sEnter your nickname: ")
473 (riece-send-string (format "NICK %s\r\n" nickname)))
475 (defun riece-command-scroll-down (lines)
476 "Scroll LINES down dialogue buffer from command buffer."
478 (let ((other-window-scroll-buffer
479 (if riece-channel-buffer-mode
481 riece-dialogue-buffer)))
482 (when (get-buffer-window other-window-scroll-buffer)
484 (scroll-other-window-down lines)
486 (message "Beginning of buffer"))))))
488 (defun riece-command-scroll-up (lines)
489 "Scroll LINES up 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 lines)
499 (message "End of buffer"))))))
501 (defun riece-command-nick-scroll-down (lines)
502 "Scroll LINES down nick buffer from command buffer."
504 (let ((other-window-scroll-buffer riece-user-list-buffer))
505 (when (get-buffer-window other-window-scroll-buffer)
507 (scroll-other-window-down lines)
509 (message "Beginning of buffer"))))))
511 (defun riece-command-nick-scroll-up (lines)
512 "Scroll LINES up nick buffer from command buffer."
514 (let* ((other-window-scroll-buffer riece-user-list-buffer))
515 (when (get-buffer-window other-window-scroll-buffer)
517 (scroll-other-window lines)
519 (message "End of buffer"))))))
521 (defun riece-command-toggle-away (&optional message)
522 "Mark yourself as being away."
524 (if (and (not (riece-user-get-away (riece-current-nickname)))
525 (or (null riece-away-message)
527 (let ((message (read-string "Away message: ")))
530 (riece-send-string (format "AWAY :%s\r\n" message))
531 (riece-send-string "AWAY\r\n")))
533 (defun riece-command-toggle-freeze (&optional arg)
534 "Prevent automatic scrolling of the dialogue window.
535 If prefix argument ARG is non-nil, toggle frozen status."
537 (with-current-buffer (if (and riece-channel-buffer-mode
538 riece-channel-buffer)
540 riece-dialogue-buffer)
541 (setq riece-freeze (if arg
542 (< 0 (prefix-numeric-value arg))
543 (not riece-freeze))))
544 (riece-update-status-indicators)
545 (force-mode-line-update t))
547 (defun riece-command-toggle-own-freeze (&optional arg)
548 "Prevent automatic scrolling of the dialogue window.
549 The difference from `riece-command-freeze' is that your messages are hidden.
550 If prefix argument ARG is non-nil, toggle frozen status."
552 (with-current-buffer (if (and riece-channel-buffer-mode
553 riece-channel-buffer)
555 riece-dialogue-buffer)
557 (< 0 (prefix-numeric-value arg))
558 (not (eq riece-freeze 'own)))
559 (setq riece-freeze 'own)
560 (setq riece-freeze nil)))
561 (riece-update-status-indicators)
562 (force-mode-line-update t))
564 (defun riece-command-quit (&optional arg)
567 (if (y-or-n-p "Really quit IRC? ")
570 (read-string "Message: ")
571 (or riece-quit-message
572 (riece-extended-version))))
573 (process-list riece-process-list))
575 (riece-quit-server-process (car process-list) message)
576 (setq process-list (cdr process-list))))))
578 (defun riece-command-raw (command)
579 "Enter raw IRC command, which is sent to the server."
580 (interactive "sIRC command: ")
581 (riece-send-string (concat command "\r\n")))
583 (defun riece-command-end-of-buffer ()
584 "Get end of the dialogue buffer."
587 (setq buffer (if riece-channel-buffer-mode
589 riece-dialogue-buffer))
590 (or (setq window (get-buffer-window buffer))
591 (setq window (get-buffer-window riece-dialogue-buffer)
592 buffer riece-dialogue-buffer))
594 (save-selected-window
595 (select-window window)
596 (goto-char (point-max))))))
598 (defun riece-command-copy-region (start end)
599 "Move current region between START and END to `kill-ring'."
601 (kill-new (buffer-substring-no-properties start end)))
603 (defun riece-command-open-server (server-name)
605 (list (completing-read "Server: " riece-server-alist)))
606 (if (riece-server-process server-name)
607 (error "%s is already opened" server-name))
609 (riece-server-name-to-server server-name)
612 (defun riece-command-close-server (server-name &optional message)
614 (list (completing-read
618 (with-current-buffer (process-buffer process)
619 (list riece-server-name)))
621 (if current-prefix-arg
622 (read-string "Message: ")
623 (or riece-quit-message
624 (riece-extended-version)))))
625 (riece-quit-server-process (riece-server-process server-name) message))
627 (defun riece-command-universal-server-name-argument ()
629 (let* ((riece-overriding-server-name
634 (with-current-buffer (process-buffer process)
635 (list riece-server-name)))
636 riece-process-list)))
638 (key-binding (read-key-sequence
639 (format "Command to execute on \"%s\":"
640 riece-overriding-server-name)))))
642 (call-interactively command)))
644 (provide 'riece-commands)
646 ;;; riece-commands.el ends here