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 (riece-check-channel-commands-are-usable t)
158 (list (read-from-minibuffer
159 "Topic: " (cons (or (riece-with-server-buffer
160 (riece-identity-server riece-current-channel)
161 (riece-channel-get-topic
162 (riece-identity-prefix
163 riece-current-channel)))
166 (riece-send-string (format "TOPIC %s :%s\r\n"
167 (riece-identity-prefix riece-current-channel)
170 (defun riece-command-invite (user)
172 (riece-check-channel-commands-are-usable t)
173 (let ((completion-ignore-case t))
174 (list (completing-read
176 (mapcar #'list (riece-get-users-on-server))))))
177 (riece-send-string (format "INVITE %s %s\r\n"
178 user (riece-identity-prefix
179 riece-current-channel))))
181 (defun riece-command-kick (user &optional message)
183 (riece-check-channel-commands-are-usable t)
184 (let ((completion-ignore-case t))
185 (list (completing-read
188 (riece-with-server-buffer
189 (riece-identity-server riece-current-channel)
190 (riece-channel-get-users
191 (riece-identity-prefix riece-current-channel)))))
192 (if current-prefix-arg
193 (read-string "Message: ")))))
196 (format "KICK %s %s :%s\r\n"
197 (riece-identity-prefix riece-current-channel)
199 (format "KICK %s %s\r\n"
200 (riece-identity-prefix riece-current-channel)
203 (defun riece-command-names (pattern)
205 (let ((completion-ignore-case t))
206 (list (read-from-minibuffer
208 (if (and riece-current-channel
209 (riece-channel-p (riece-identity-prefix
210 riece-current-channel)))
211 (cons (riece-identity-prefix riece-current-channel)
213 (if (or (not (equal pattern ""))
214 (yes-or-no-p "Really want to query NAMES without argument? "))
215 (riece-send-string (format "NAMES %s\r\n" pattern))))
217 (defun riece-command-who (pattern)
219 (let ((completion-ignore-case t))
220 (list (read-from-minibuffer
222 (if (and riece-current-channel
223 (riece-channel-p (riece-identity-prefix
224 riece-current-channel)))
225 (cons (riece-identity-prefix riece-current-channel)
227 (if (or (not (equal pattern ""))
228 (yes-or-no-p "Really want to query WHO without argument? "))
229 (riece-send-string (format "WHO %s\r\n" pattern))))
231 (defun riece-command-list (pattern)
233 (let ((completion-ignore-case t))
234 (list (read-from-minibuffer
236 (if (and riece-current-channel
237 (riece-channel-p (riece-identity-prefix
238 riece-current-channel)))
239 (cons (riece-identity-prefix riece-current-channel)
241 (if (or (not (equal pattern ""))
242 (yes-or-no-p "Really want to query LIST without argument? "))
243 (riece-send-string (format "LIST %s\r\n" pattern))))
245 (defun riece-command-change-mode (channel change)
247 (let* ((completion-ignore-case t)
249 (if current-prefix-arg
250 (riece-completing-read-identity
251 "Channel/User: " riece-current-channels)
252 (riece-check-channel-commands-are-usable t)
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)
274 (riece-check-channel-commands-are-usable t)
276 (riece-with-server-buffer
277 (riece-identity-server riece-current-channel)
278 (riece-channel-get-operators
279 (riece-identity-prefix riece-current-channel))))
280 (completion-ignore-case t)
282 (if current-prefix-arg
283 (setq users (riece-completing-read-multiple
285 (mapcar #'list operators)))
286 (setq users (riece-completing-read-multiple
290 (unless (member user operators)
292 (riece-with-server-buffer
293 (riece-identity-server
294 riece-current-channel)
295 (riece-channel-get-users
296 (riece-identity-prefix
297 riece-current-channel))))))))
298 (list users current-prefix-arg)))
301 (setq group (cons (car users) group)
303 (if (or (= (length group) 3)
306 (format "MODE %s %c%s %s\r\n"
307 (riece-identity-prefix riece-current-channel)
308 (if current-prefix-arg
311 (make-string (length group) ?o)
312 (mapconcat #'identity group " ")))))))
314 (defun riece-command-set-speakers (users &optional arg)
316 (riece-check-channel-commands-are-usable t)
318 (riece-with-server-buffer
319 (riece-identity-server riece-current-channel)
320 (riece-channel-get-speakers
321 (riece-identity-prefix riece-current-channel))))
322 (completion-ignore-case t)
324 (if current-prefix-arg
325 (setq users (riece-completing-read-multiple
327 (mapcar #'list speakers)))
328 (setq users (riece-completing-read-multiple
332 (unless (member user speakers)
334 (riece-with-server-buffer
335 (riece-identity-server
336 riece-current-channel)
337 (riece-channel-get-users
338 (riece-identity-prefix
339 riece-current-channel))))))))
340 (list users current-prefix-arg)))
343 (setq group (cons (car users) group)
345 (if (or (= (length group) 3)
348 (format "MODE %s %c%s %s\r\n"
349 (riece-identity-prefix riece-current-channel)
350 (if current-prefix-arg
353 (make-string (length group) ?v)
354 (mapconcat #'identity group " ")))))))
356 (defun riece-command-send-message (message notice)
357 "Send MESSAGE to the current channel."
358 (if (equal message "")
359 (error "No text to send"))
360 (riece-check-channel-commands-are-usable)
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 (if riece-join-channel-candidate
424 (let ((default (riece-format-identity
425 riece-join-channel-candidate)))
426 (riece-completing-read-identity
427 (format "Channel/User (default %s): " default)
428 riece-current-channels nil nil nil nil default))
429 (riece-completing-read-identity
430 "Channel/User: " riece-current-channels)))
432 (if (and current-prefix-arg
433 (riece-channel-p (riece-identity-prefix target)))
435 (riece-read-passwd (format "Key for %s: "
436 (riece-format-identity target)))))
438 (let ((pointer (riece-identity-member target riece-current-channels)))
440 (riece-command-switch-to-channel (car pointer))
441 (if (riece-channel-p (riece-identity-prefix target))
442 (riece-command-join-channel target key)
443 (riece-command-join-partner target)))))
445 (defun riece-command-part-channel (target message)
446 (let ((process (riece-server-process (riece-identity-server target))))
447 (riece-process-send-string process
449 (format "PART %s :%s\r\n"
450 (riece-identity-prefix target)
452 (format "PART %s\r\n"
453 (riece-identity-prefix target))))))
455 (defun riece-command-part (target &optional message)
457 (riece-check-channel-commands-are-usable)
458 (let* ((completion-ignore-case t)
460 (riece-completing-read-identity
461 (format "Channel/User (default %s): "
462 (riece-format-identity riece-current-channel))
463 riece-current-channels nil nil nil nil
464 (riece-format-identity riece-current-channel)))
466 (if (and current-prefix-arg
467 (riece-channel-p (riece-identity-prefix target)))
468 (setq message (read-string "Message: ")))
469 (list target message)))
470 (if (riece-identity-member target riece-current-channels)
471 (if (riece-channel-p (riece-identity-prefix target))
472 (riece-command-part-channel target message)
473 (riece-part-channel target)
474 (riece-redisplay-buffers))
475 (error "You are not talking with %s" target)))
477 (defun riece-command-change-nickname (nickname)
478 "Change your nickname to NICK."
479 (interactive "sEnter your nickname: ")
480 (riece-send-string (format "NICK %s\r\n" nickname)))
482 (defun riece-command-scroll-down (lines)
483 "Scroll LINES down dialogue buffer from command buffer."
485 (let ((other-window-scroll-buffer
486 (if riece-channel-buffer-mode
488 riece-dialogue-buffer)))
489 (when (get-buffer-window other-window-scroll-buffer)
491 (scroll-other-window-down lines)
493 (message "Beginning of buffer"))))))
495 (defun riece-command-scroll-up (lines)
496 "Scroll LINES up dialogue buffer from command buffer."
498 (let* ((other-window-scroll-buffer
499 (if riece-channel-buffer-mode
501 riece-dialogue-buffer)))
502 (when (get-buffer-window other-window-scroll-buffer)
504 (scroll-other-window lines)
506 (message "End of buffer"))))))
508 (defun riece-command-nick-scroll-down (lines)
509 "Scroll LINES down nick buffer from command buffer."
511 (let ((other-window-scroll-buffer riece-user-list-buffer))
512 (when (get-buffer-window other-window-scroll-buffer)
514 (scroll-other-window-down lines)
516 (message "Beginning of buffer"))))))
518 (defun riece-command-nick-scroll-up (lines)
519 "Scroll LINES up nick buffer from command buffer."
521 (let* ((other-window-scroll-buffer riece-user-list-buffer))
522 (when (get-buffer-window other-window-scroll-buffer)
524 (scroll-other-window lines)
526 (message "End of buffer"))))))
528 (defun riece-command-toggle-away (&optional message)
529 "Mark yourself as being away."
531 (if (and (not (riece-with-server-buffer (riece-identity-server
532 (riece-current-nickname))
533 (riece-user-get-away (riece-identity-prefix
534 (riece-current-nickname)))))
535 (or (null riece-away-message)
537 (let ((message (read-string "Away message: ")))
540 (riece-send-string (format "AWAY :%s\r\n" message))
541 (riece-send-string "AWAY\r\n")))
543 (defun riece-command-toggle-freeze (&optional arg)
544 "Prevent automatic scrolling of the dialogue window.
545 If prefix argument ARG is non-nil, toggle frozen status."
547 (with-current-buffer (if (and riece-channel-buffer-mode
548 riece-channel-buffer)
550 riece-dialogue-buffer)
551 (setq riece-freeze (if arg
552 (< 0 (prefix-numeric-value arg))
553 (not riece-freeze))))
554 (riece-update-status-indicators)
555 (force-mode-line-update t))
557 (defun riece-command-toggle-own-freeze (&optional arg)
558 "Prevent automatic scrolling of the dialogue window.
559 The difference from `riece-command-freeze' is that your messages are hidden.
560 If prefix argument ARG is non-nil, toggle frozen status."
562 (with-current-buffer (if (and riece-channel-buffer-mode
563 riece-channel-buffer)
565 riece-dialogue-buffer)
567 (< 0 (prefix-numeric-value arg))
568 (not (eq riece-freeze 'own)))
569 (setq riece-freeze 'own)
570 (setq riece-freeze nil)))
571 (riece-update-status-indicators)
572 (force-mode-line-update t))
574 (defun riece-command-quit (&optional arg)
577 (if (y-or-n-p "Really quit IRC? ")
580 (read-string "Message: ")
581 (or riece-quit-message
582 (riece-extended-version))))
583 (alist riece-server-process-alist))
585 (riece-quit-server-process (cdr (car alist)) message)
586 (setq alist (cdr alist))))))
588 (defun riece-command-raw (command)
589 "Enter raw IRC command, which is sent to the server."
590 (interactive "sIRC command: ")
591 (riece-send-string (concat command "\r\n")))
593 (defun riece-command-end-of-buffer ()
594 "Get end of the dialogue buffer."
597 (setq buffer (if riece-channel-buffer-mode
599 riece-dialogue-buffer))
600 (or (setq window (get-buffer-window buffer))
601 (setq window (get-buffer-window riece-dialogue-buffer)
602 buffer riece-dialogue-buffer))
604 (save-selected-window
605 (select-window window)
606 (goto-char (point-max))))))
608 (defun riece-command-copy-region (start end)
609 "Move current region between START and END to `kill-ring'."
611 (kill-new (buffer-substring-no-properties start end)))
613 (defun riece-command-open-server (server-name)
615 (list (completing-read "Server: " riece-server-alist)))
616 (if (riece-server-process server-name)
617 (error "%s is already opened" server-name))
619 (riece-server-name-to-server server-name)
622 (defun riece-command-close-server (server-name &optional message)
624 (list (completing-read "Server: " riece-server-process-alist)
625 (if current-prefix-arg
626 (read-string "Message: ")
627 (or riece-quit-message
628 (riece-extended-version)))))
629 (riece-quit-server-process (riece-server-process server-name) message))
631 (defun riece-command-universal-server-name-argument ()
633 (let* ((riece-overriding-server-name
634 (completing-read "Server: " riece-server-process-alist))
636 (key-binding (read-key-sequence
637 (format "Command to execute on \"%s\":"
638 riece-overriding-server-name)))))
640 (call-interactively command)))
642 (provide 'riece-commands)
644 ;;; riece-commands.el ends here