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-display)
30 (require 'riece-version)
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-channels)
41 (riece-switch-to-channel channel)
42 (riece-redisplay-buffers)))
44 (defun riece-command-switch-to-channel-by-number (number)
46 (let ((command-name (symbol-name this-command)))
47 (if (string-match "[0-9]+$" command-name)
48 (list (string-to-number (match-string 0 command-name)))
49 (list (string-to-number (read-string "Number: "))))))
50 (let ((channel (nth (1- number) riece-current-channels)))
52 (riece-command-switch-to-channel channel)
53 (error "No such number!"))))
58 (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
59 (number-to-string number)))
60 'riece-command-switch-to-channel-by-number)
61 (setq number (1+ number)))))
63 (defun riece-command-next-channel ()
64 "Select the next channel."
66 (when (> (length riece-current-channels) 1)
67 (let ((pointer (cdr (riece-identity-member
69 riece-current-channels))))
72 (setq pointer (cdr pointer)))
74 (setq pointer riece-current-channels)
77 (setq pointer (cdr pointer))))
79 (riece-command-switch-to-channel (car pointer))
80 (error "No such channel!")))))
82 (defun riece-command-previous-channel ()
83 "Select the previous channel."
85 (when (> (length riece-current-channels) 1)
86 (let ((pointer (riece-identity-member
88 riece-current-channels))
89 (start riece-current-channels)
91 (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-toggle-channel-buffer-mode ()
116 (setq riece-channel-buffer-mode
117 (not riece-channel-buffer-mode)
118 riece-save-variables-are-dirty t)
119 (riece-command-configure-windows))
121 (defun riece-command-toggle-user-list-buffer-mode ()
123 (setq riece-user-list-buffer-mode
124 (not riece-user-list-buffer-mode)
125 riece-save-variables-are-dirty t)
126 (riece-command-configure-windows))
128 (defun riece-command-toggle-channel-list-buffer-mode ()
130 (setq riece-channel-list-buffer-mode
131 (not riece-channel-list-buffer-mode)
132 riece-save-variables-are-dirty t)
133 (riece-command-configure-windows))
135 (defun riece-command-finger (user &optional recurse)
137 (let* ((completion-ignore-case t)
138 (user (completing-read
140 (mapcar #'list (riece-get-users-on-server)))))
141 (list user current-prefix-arg)))
143 (riece-send-string (format "WHOIS %s %s\r\n" user user))
144 (riece-send-string (format "WHOIS %s\r\n" user))))
146 (defun riece-command-topic (topic)
148 (list (read-from-minibuffer
149 "Topic: " (cons (or (riece-with-identity-buffer riece-current-channel
150 (riece-channel-get-topic
151 (riece-identity-prefix
152 riece-current-channel)))
155 (riece-send-string (format "TOPIC %s :%s\r\n"
156 (riece-identity-prefix riece-current-channel)
159 (defun riece-command-invite (user)
161 (let ((completion-ignore-case t))
162 (unless (and riece-current-channel
163 (riece-channel-p (riece-identity-prefix
164 riece-current-channel)))
165 (error "Not on a channel"))
166 (list (completing-read
168 (mapcar #'list (riece-get-users-on-server))))))
169 (riece-send-string (format "INVITE %s %s\r\n"
170 user (riece-identity-prefix
171 riece-current-channel))))
173 (defun riece-command-kick (user &optional message)
175 (let ((completion-ignore-case t))
176 (unless (and riece-current-channel
177 (riece-channel-p (riece-identity-prefix
178 riece-current-channel)))
179 (error "Not on a channel"))
180 (list (completing-read
182 (mapcar #'list (riece-channel-get-users
183 riece-current-channel)))
184 (if current-prefix-arg
185 (read-string "Message: ")))))
188 (format "KICK %s %s :%s\r\n"
189 (riece-identity-prefix riece-current-channel)
191 (format "KICK %s %s\r\n"
192 (riece-identity-prefix riece-current-channel)
195 (defun riece-command-names (pattern)
197 (let ((completion-ignore-case t))
198 (list (read-from-minibuffer
200 (if (and riece-current-channel
201 (riece-channel-p (riece-identity-prefix
202 riece-current-channel)))
203 (cons (riece-decode-identity riece-current-channel t)
205 (if (or (not (equal pattern ""))
206 (yes-or-no-p "Really want to query NAMES without argument? "))
207 (riece-send-string (format "NAMES %s\r\n" pattern))))
209 (defun riece-command-who (pattern)
211 (let ((completion-ignore-case t))
212 (list (read-from-minibuffer
214 (if (and riece-current-channel
215 (riece-channel-p (riece-identity-prefix
216 riece-current-channel)))
217 (cons (riece-decode-identity riece-current-channel t)
219 (if (or (not (equal pattern ""))
220 (yes-or-no-p "Really want to query WHO without argument? "))
221 (riece-send-string (format "WHO %s\r\n" pattern))))
223 (defun riece-command-list (pattern)
225 (let ((completion-ignore-case t))
226 (list (read-from-minibuffer
228 (if (and riece-current-channel
229 (riece-channel-p (riece-identity-prefix
230 riece-current-channel)))
231 (cons (riece-decode-identity riece-current-channel t)
233 (if (or (not (equal pattern ""))
234 (yes-or-no-p "Really want to query LIST without argument? "))
235 (riece-send-string (format "LIST %s\r\n" pattern))))
237 (defun riece-command-change-mode (channel change)
239 (let* ((completion-ignore-case t)
241 (if current-prefix-arg
242 (riece-completing-read-identity
243 "Channel/User: " riece-current-channels)
244 riece-current-channel))
245 (riece-overriding-server-name (riece-identity-server channel))
246 (riece-temp-minibuffer-message
247 (concat "[Available modes: "
248 (riece-with-server-buffer (riece-identity-server channel)
249 (if (riece-channel-p (riece-identity-prefix channel))
250 (if riece-supported-channel-modes
251 (apply #'string riece-supported-channel-modes))
252 (if riece-supported-user-modes
253 (apply #'string riece-supported-user-modes))))
256 (read-from-minibuffer
257 (concat (riece-concat-channel-modes
258 channel "Mode (? for help)") ": ")
259 nil riece-minibuffer-map))))
260 (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
263 (defun riece-command-set-operators (users &optional arg)
266 (riece-with-identity-buffer riece-current-channel
267 (riece-channel-get-operators
268 (riece-identity-prefix riece-current-channel))))
269 (completion-ignore-case t)
271 (if current-prefix-arg
272 (setq users (riece-completing-read-multiple
274 (mapcar #'list operators)))
275 (setq users (riece-completing-read-multiple
279 (unless (member user operators)
281 (riece-with-identity-buffer
282 riece-current-channel
283 (riece-channel-get-users
284 (riece-identity-prefix
285 riece-current-channel))))))))
286 (list users current-prefix-arg)))
289 (setq group (cons (car users) group)
291 (if (or (= (length group) 3)
294 (format "MODE %s %c%s %s\r\n"
295 (riece-identity-prefix riece-current-channel)
296 (if current-prefix-arg
299 (make-string (length group) ?o)
300 (mapconcat #'identity group " ")))))))
302 (defun riece-command-set-speakers (users &optional arg)
305 (riece-with-identity-buffer riece-current-channel
306 (riece-channel-get-speakers
307 (riece-identity-prefix riece-current-channel))))
308 (completion-ignore-case t)
310 (if current-prefix-arg
311 (setq users (riece-completing-read-multiple
313 (mapcar #'list speakers)))
314 (setq users (riece-completing-read-multiple
318 (unless (member user speakers)
320 (riece-with-identity-buffer
321 riece-current-channel
322 (riece-channel-get-users
323 (riece-identity-prefix
324 riece-current-channel))))))))
325 (list users current-prefix-arg)))
328 (setq group (cons (car users) group)
330 (if (or (= (length group) 3)
333 (format "MODE %s %c%s %s\r\n"
334 (riece-identity-prefix riece-current-channel)
335 (if current-prefix-arg
338 (make-string (length group) ?v)
339 (mapconcat #'identity 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 (unless riece-current-channel
346 (error (substitute-command-keys
347 "Type \\[riece-command-join] to join a channel")))
351 (format "NOTICE %s :%s\r\n"
352 (riece-identity-prefix riece-current-channel)
354 (riece-display-message
355 (riece-make-message (riece-current-nickname) riece-current-channel
358 (format "PRIVMSG %s :%s\r\n"
359 (riece-identity-prefix riece-current-channel)
361 (riece-display-message
362 (riece-make-message (riece-current-nickname) riece-current-channel
365 (defun riece-command-enter-message ()
366 "Send the current line to the current channel."
368 (riece-command-send-message (buffer-substring
369 (riece-line-beginning-position)
370 (riece-line-end-position))
372 (let ((next-line-add-newlines t))
375 (defun riece-command-enter-message-as-notice ()
376 "Send the current line to the current channel as NOTICE."
378 (riece-command-send-message (buffer-substring
379 (riece-line-beginning-position)
380 (riece-line-end-position))
382 (let ((next-line-add-newlines t))
385 (defun riece-command-join-channel (target key)
386 (let ((process (riece-server-process (riece-identity-server target))))
388 (error "%s" (substitute-command-keys
389 "Type \\[riece-command-open-server] to open server.")))
390 (riece-process-send-string process
392 (format "JOIN %s :%s\r\n"
393 (riece-identity-prefix target)
395 (format "JOIN %s\r\n"
396 (riece-identity-prefix target))))))
398 (defun riece-command-join-partner (target)
399 (let ((pointer (riece-identity-member target riece-current-channels)))
401 (riece-command-switch-to-channel (car pointer))
402 (riece-join-channel target)
403 (riece-switch-to-channel target)
404 (riece-redisplay-buffers))))
406 (defun riece-command-join (target &optional key)
408 (let ((completion-ignore-case t)
410 (riece-completing-read-identity
411 "Channel/User: " riece-current-channels))
413 (if (and current-prefix-arg
414 (riece-channel-p target))
416 (riece-read-passwd (format "Key for %s: " target))))
418 (let ((pointer (riece-identity-member target riece-current-channels)))
420 (riece-command-switch-to-channel (car pointer))
421 (if (riece-channel-p (riece-identity-prefix target))
422 (riece-command-join-channel target key)
423 (riece-command-join-partner target)))))
425 (defun riece-command-part-channel (target message)
426 (let ((process (riece-server-process (riece-identity-server target))))
428 (error "%s" (substitute-command-keys
429 "Type \\[riece-command-open-server] to open server.")))
430 (riece-process-send-string process
432 (format "PART %s :%s\r\n"
433 (riece-identity-prefix target)
435 (format "PART %s\r\n"
436 (riece-identity-prefix target))))))
438 (defun riece-command-part (target &optional message)
440 (let ((completion-ignore-case t)
442 (riece-completing-read-identity
443 "Channel/User: " riece-current-channels nil nil
444 (cons (riece-decode-identity riece-current-channel) 0)))
446 (if (and current-prefix-arg
447 (riece-channel-p (riece-identity-prefix target)))
448 (setq message (read-string "Message: ")))
449 (list target message)))
450 (if (riece-identity-member target riece-current-channels)
451 (if (riece-channel-p (riece-identity-prefix target))
452 (riece-command-part-channel target message)
453 (riece-part-channel target)
454 (riece-redisplay-buffers))
455 (error "You are not talking with %s" target)))
457 (defun riece-command-change-nickname (nickname)
458 "Change your nickname to NICK."
459 (interactive "sEnter your nickname: ")
460 (riece-send-string (format "NICK %s\r\n" nickname)))
462 (defun riece-command-scroll-down (lines)
463 "Scroll LINES down dialogue buffer from command buffer."
465 (let ((other-window-scroll-buffer
466 (if riece-channel-buffer-mode
468 riece-dialogue-buffer)))
469 (when (get-buffer-window other-window-scroll-buffer)
471 (scroll-other-window-down lines)
473 (message "Beginning of buffer"))))))
475 (defun riece-command-scroll-up (lines)
476 "Scroll LINES up 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 lines)
486 (message "End of buffer"))))))
488 (defun riece-command-nick-scroll-down (lines)
489 "Scroll LINES down nick buffer from command buffer."
491 (let ((other-window-scroll-buffer riece-user-list-buffer))
492 (when (get-buffer-window other-window-scroll-buffer)
494 (scroll-other-window-down lines)
496 (message "Beginning of buffer"))))))
498 (defun riece-command-nick-scroll-up (lines)
499 "Scroll LINES up nick buffer from command buffer."
501 (let* ((other-window-scroll-buffer riece-user-list-buffer))
502 (when (get-buffer-window other-window-scroll-buffer)
504 (scroll-other-window lines)
506 (message "End of buffer"))))))
508 (defun riece-command-toggle-away (&optional message)
509 "Mark yourself as being away."
511 (if (and (not (riece-user-get-away (riece-current-nickname)))
512 (or (null riece-away-message)
514 (let ((message (read-string "Away message: ")))
517 (riece-send-string (format "AWAY :%s\r\n" message))
518 (riece-send-string "AWAY\r\n")))
520 (defun riece-command-toggle-freeze (&optional arg)
521 "Prevent automatic scrolling of the dialogue window.
522 If prefix argument ARG is non-nil, toggle frozen status."
524 (with-current-buffer (if (and riece-channel-buffer-mode
525 riece-channel-buffer)
527 riece-dialogue-buffer)
528 (setq riece-freeze (if arg
529 (< 0 (prefix-numeric-value arg))
530 (not riece-freeze))))
531 (riece-update-status-indicators)
532 (force-mode-line-update t))
534 (defun riece-command-toggle-own-freeze (&optional arg)
535 "Prevent automatic scrolling of the dialogue window.
536 The difference from `riece-command-freeze' is that your messages are hidden.
537 If prefix argument ARG is non-nil, toggle frozen status."
539 (with-current-buffer (if (and riece-channel-buffer-mode
540 riece-channel-buffer)
542 riece-dialogue-buffer)
544 (< 0 (prefix-numeric-value arg))
545 (not (eq riece-freeze 'own)))
546 (setq riece-freeze 'own)
547 (setq riece-freeze nil)))
548 (riece-update-status-indicators)
549 (force-mode-line-update t))
551 (defun riece-command-quit (&optional arg)
554 (if (y-or-n-p "Really quit IRC? ")
557 (read-string "Message: ")
558 (or riece-quit-message
559 (riece-extended-version))))
560 (process-list riece-process-list))
562 (riece-process-send-string (car process-list)
564 (format "QUIT :%s\r\n" message)
566 (setq process-list (cdr process-list))))))
568 (defun riece-command-raw (command)
569 "Enter raw IRC command, which is sent to the server."
570 (interactive "sIRC command: ")
571 (riece-send-string (concat command "\r\n")))
573 (defun riece-command-end-of-buffer ()
574 "Get end of the dialogue buffer."
577 (setq buffer (if riece-channel-buffer-mode
579 riece-dialogue-buffer))
580 (or (setq window (get-buffer-window buffer))
581 (setq window (get-buffer-window riece-dialogue-buffer)
582 buffer riece-dialogue-buffer))
584 (save-selected-window
585 (select-window window)
586 (goto-char (point-max))))))
588 (defun riece-command-copy-region (start end)
589 "Move current region between START and END to `kill-ring'."
591 (kill-new (buffer-substring-no-properties start end)))
593 (defun riece-command-open-server (server-name)
595 (list (completing-read "Server: " riece-server-alist)))
596 (if (riece-server-process server-name)
597 (error "%s is already opened" server-name))
599 (riece-server-name-to-server server-name)
602 (defun riece-command-close-server (server-name &optional message)
604 (list (completing-read
608 (with-current-buffer (process-buffer process)
609 (list riece-server-name)))
611 (if current-prefix-arg
612 (read-string "Message: ")
613 (or riece-quit-message
614 (riece-extended-version)))))
615 (riece-process-send-string (riece-server-process server-name)
617 (format "QUIT :%s\r\n" message)
620 (defun riece-command-universal-server-name-argument ()
622 (let* ((riece-overriding-server-name
627 (with-current-buffer (process-buffer process)
628 (list riece-server-name)))
629 riece-process-list)))
631 (key-binding (read-key-sequence
632 (format "Command to execute on \"%s\":"
633 riece-overriding-server-name)))))
635 (call-interactively command)))
637 (provide 'riece-commands)
639 ;;; riece-commands.el ends here