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 (riece-switch-to-channel channel)
41 (riece-command-configure-windows))
43 (defun riece-command-switch-to-channel-by-number (number)
45 (let ((command-name (symbol-name this-command)))
46 (if (string-match "[0-9]+$" command-name)
47 (list (string-to-number (match-string 0 command-name)))
48 (list (string-to-number (read-string "Number: "))))))
49 (let ((channels riece-current-channels)
54 (setq index (1+ index)))
55 (setq channels (cdr channels)))
57 (riece-command-switch-to-channel (car channels))
58 (error "No such number!"))))
63 (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
64 (number-to-string number)))
65 'riece-command-switch-to-channel-by-number)
66 (setq number (1+ number)))))
68 (defun riece-command-next-channel ()
69 "Select the next channel."
71 (when (> (length riece-current-channels) 1)
72 (let ((pointer (cdr (riece-identity-member-no-server
74 riece-current-channels))))
77 (setq pointer (cdr pointer)))
79 (setq pointer riece-current-channels)
82 (setq pointer (cdr pointer))))
84 (riece-command-switch-to-channel (car pointer))
85 (error "No such channel!")))))
87 (defun riece-command-previous-channel ()
88 "Select the previous channel."
90 (when (> (length riece-current-channels) 1)
91 (let ((pointer (riece-identity-member-no-server
93 riece-current-channels))
94 (start riece-current-channels)
96 (while (and start (not (eq start pointer)))
97 (setq channel (car start))
98 (setq start (cdr start)))
100 (setq start (copy-sequence riece-current-channels))
101 (setq start (delq nil start))
102 (and (> (length start) 1)
103 (setq channel (nth (1- (length start)) start))))
105 (riece-command-switch-to-channel channel)
106 (error "No such channel!")))))
108 (defun riece-command-select-command-buffer ()
109 "Select the command buffer."
111 (let ((window (get-buffer-window riece-command-buffer)))
113 (select-window window))))
115 (defun riece-command-configure-windows ()
117 (riece-redisplay-buffers t))
119 (defun riece-command-toggle-channel-buffer-mode ()
121 (setq riece-channel-buffer-mode
122 (not riece-channel-buffer-mode)
123 riece-save-variables-are-dirty t)
124 (riece-command-configure-windows))
126 (defun riece-command-toggle-user-list-buffer-mode ()
128 (setq riece-user-list-buffer-mode
129 (not riece-user-list-buffer-mode)
130 riece-save-variables-are-dirty t)
131 (riece-command-configure-windows))
133 (defun riece-command-toggle-channel-list-buffer-mode ()
135 (setq riece-channel-list-buffer-mode
136 (not riece-channel-list-buffer-mode)
137 riece-save-variables-are-dirty t)
138 (riece-command-configure-windows))
140 (defun riece-command-finger (user &optional recurse)
142 (let* ((completion-ignore-case t)
143 (user (completing-read
145 (mapcar #'list (riece-get-users-on-server)))))
146 (list user current-prefix-arg)))
148 (riece-send-string (format "WHOIS %s %s\r\n" user user))
149 (riece-send-string (format "WHOIS %s\r\n" user))))
151 (defun riece-command-topic (topic)
153 (list (read-from-minibuffer
154 "Topic: " (cons (or (riece-with-identity-buffer riece-current-channel
155 (riece-channel-get-topic
156 (riece-identity-prefix
157 riece-current-channel)))
160 (riece-send-string (format "TOPIC %s :%s\r\n"
161 (riece-identity-prefix riece-current-channel)
164 (defun riece-command-invite (user)
166 (let ((completion-ignore-case t))
167 (unless (and riece-current-channel
168 (riece-channel-p (riece-identity-prefix
169 riece-current-channel)))
170 (error "Not on a channel"))
171 (list (completing-read
173 (mapcar #'list (riece-get-users-on-server))))))
174 (riece-send-string (format "INVITE %s %s\r\n"
175 user (riece-identity-prefix
176 riece-current-channel))))
178 (defun riece-command-kick (user &optional message)
180 (let ((completion-ignore-case t))
181 (unless (and riece-current-channel
182 (riece-channel-p (riece-identity-prefix
183 riece-current-channel)))
184 (error "Not on a channel"))
185 (list (completing-read
187 (mapcar #'list (riece-channel-get-users
188 riece-current-channel)))
189 (if current-prefix-arg
190 (read-string "Message: ")))))
193 (format "KICK %s %s :%s\r\n"
194 (riece-identity-prefix riece-current-channel)
196 (format "KICK %s %s\r\n"
197 (riece-identity-prefix riece-current-channel)
200 (defun riece-command-names (pattern)
202 (let ((completion-ignore-case t))
203 (list (read-from-minibuffer
205 (if (and riece-current-channel
206 (riece-channel-p (riece-identity-prefix
207 riece-current-channel)))
208 (cons (riece-decode-identity riece-current-channel t)
210 (if (or (not (equal pattern ""))
211 (yes-or-no-p "Really want to query NAMES without argument? "))
212 (riece-send-string (format "NAMES %s\r\n" pattern))))
214 (defun riece-command-who (pattern)
216 (let ((completion-ignore-case t))
217 (list (read-from-minibuffer
219 (if (and riece-current-channel
220 (riece-channel-p (riece-identity-prefix
221 riece-current-channel)))
222 (cons (riece-decode-identity riece-current-channel t)
224 (if (or (not (equal pattern ""))
225 (yes-or-no-p "Really want to query WHO without argument? "))
226 (riece-send-string (format "WHO %s\r\n" pattern))))
228 (defun riece-command-list (pattern)
230 (let ((completion-ignore-case t))
231 (list (read-from-minibuffer
233 (if (and riece-current-channel
234 (riece-channel-p (riece-identity-prefix
235 riece-current-channel)))
236 (cons (riece-decode-identity riece-current-channel t)
238 (if (or (not (equal pattern ""))
239 (yes-or-no-p "Really want to query LIST without argument? "))
240 (riece-send-string (format "LIST %s\r\n" pattern))))
242 (defun riece-command-change-mode (channel change)
244 (let* ((completion-ignore-case t)
246 (if current-prefix-arg
247 (riece-completing-read-identity
248 "Channel/User: " riece-current-channels)
249 riece-current-channel))
250 (riece-overriding-server-name (riece-identity-server channel))
251 (riece-temp-minibuffer-message
252 (concat "[Available modes: "
253 (riece-with-server-buffer (riece-identity-server channel)
254 (if (riece-channel-p (riece-identity-prefix channel))
255 (if riece-supported-channel-modes
256 (apply #'string riece-supported-channel-modes))
257 (if riece-supported-user-modes
258 (apply #'string riece-supported-user-modes))))
261 (read-from-minibuffer
262 (concat (riece-concat-channel-modes
263 channel "Mode (? for help)") ": ")
264 nil riece-minibuffer-map))))
265 (riece-send-string (format "MODE %s :%s\r\n" channel change)))
267 (defun riece-command-set-operators (users &optional arg)
270 (riece-with-identity-buffer riece-current-channel
271 (riece-channel-get-operators
272 (riece-identity-prefix riece-current-channel))))
273 (completion-ignore-case t)
275 (if current-prefix-arg
276 (setq users (riece-completing-read-multiple
278 (mapcar #'list operators)))
279 (setq users (riece-completing-read-multiple
283 (unless (member user operators)
285 (riece-with-identity-buffer
286 riece-current-channel
287 (riece-channel-get-users
288 (riece-identity-prefix
289 riece-current-channel))))))))
290 (list users current-prefix-arg)))
293 (setq group (cons (car users) group)
295 (if (or (= (length group) 3)
298 (format "MODE %s %c%s %s\r\n"
299 (riece-identity-prefix riece-current-channel)
300 (if current-prefix-arg
303 (make-string (length group) ?o)
304 (mapconcat #'identity group " ")))))))
306 (defun riece-command-set-speakers (users &optional arg)
309 (riece-with-identity-buffer riece-current-channel
310 (riece-channel-get-speakers
311 (riece-identity-prefix riece-current-channel))))
312 (completion-ignore-case t)
314 (if current-prefix-arg
315 (setq users (riece-completing-read-multiple
317 (mapcar #'list speakers)))
318 (setq users (riece-completing-read-multiple
322 (unless (member user speakers)
324 (riece-with-identity-buffer
325 riece-current-channel
326 (riece-channel-get-users
327 (riece-identity-prefix
328 riece-current-channel))))))))
329 (list users current-prefix-arg)))
332 (setq group (cons (car users) group)
334 (if (or (= (length group) 3)
337 (format "MODE %s %c%s %s\r\n"
338 (riece-identity-prefix riece-current-channel)
339 (if current-prefix-arg
342 (make-string (length group) ?v)
343 (mapconcat #'identity group " ")))))))
345 (defun riece-command-send-message (message notice)
346 "Send MESSAGE to the current channel."
347 (if (equal message "")
348 (error "No text to send"))
349 (unless riece-current-channel
350 (error (substitute-command-keys
351 "Type \\[riece-command-join] to join a channel")))
355 (format "NOTICE %s :%s\r\n"
356 (riece-identity-prefix riece-current-channel)
358 (riece-own-channel-message message riece-current-channel 'notice))
360 (format "PRIVMSG %s :%s\r\n"
361 (riece-identity-prefix riece-current-channel)
363 (riece-own-channel-message message)))
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))
445 (if (and current-prefix-arg
446 (riece-channel-p (riece-identity-prefix target)))
447 (setq message (read-string "Message: ")))
448 (list target message)))
449 (if (riece-identity-member target riece-current-channels)
450 (if (riece-channel-p (riece-identity-prefix target))
451 (riece-command-part-channel target message)
452 (riece-part-channel target)
453 (riece-redisplay-buffers))
454 (error "You are not talking with %s" target)))
456 (defun riece-command-change-nickname (nickname)
457 "Change your nickname to NICK."
458 (interactive "sEnter your nickname: ")
459 (riece-send-string (format "NICK %s\r\n" nickname)))
461 (defun riece-command-scroll-down (lines)
462 "Scroll LINES down dialogue buffer from command buffer."
464 (let ((other-window-scroll-buffer
465 (if riece-channel-buffer-mode
467 riece-dialogue-buffer)))
468 (when (get-buffer-window other-window-scroll-buffer)
470 (scroll-other-window-down lines)
472 (message "Beginning of buffer"))))))
474 (defun riece-command-scroll-up (lines)
475 "Scroll LINES up dialogue buffer from command buffer."
477 (let* ((other-window-scroll-buffer
478 (if riece-channel-buffer-mode
480 riece-dialogue-buffer)))
481 (when (get-buffer-window other-window-scroll-buffer)
483 (scroll-other-window lines)
485 (message "End of buffer"))))))
487 (defun riece-command-nick-scroll-down (lines)
488 "Scroll LINES down nick buffer from command buffer."
490 (let ((other-window-scroll-buffer riece-user-list-buffer))
491 (when (get-buffer-window other-window-scroll-buffer)
493 (scroll-other-window-down lines)
495 (message "Beginning of buffer"))))))
497 (defun riece-command-nick-scroll-up (lines)
498 "Scroll LINES up nick buffer from command buffer."
500 (let* ((other-window-scroll-buffer riece-user-list-buffer))
501 (when (get-buffer-window other-window-scroll-buffer)
503 (scroll-other-window lines)
505 (message "End of buffer"))))))
507 (defun riece-command-toggle-away (&optional message)
508 "Mark yourself as being away."
510 (if (and (not (riece-user-get-away (riece-current-nickname)))
511 (or (null riece-away-message)
513 (let ((message (read-string "Away message: ")))
516 (riece-send-string (format "AWAY :%s\r\n" message))
517 (riece-send-string "AWAY\r\n")))
519 (defun riece-command-toggle-freeze (&optional arg)
520 "Prevent automatic scrolling of the dialogue window.
521 If prefix argument ARG is non-nil, toggle frozen status."
523 (with-current-buffer (if (and riece-channel-buffer-mode
524 riece-channel-buffer)
526 riece-dialogue-buffer)
527 (setq riece-freeze (if arg
528 (< 0 (prefix-numeric-value arg))
529 (not riece-freeze))))
530 (riece-update-status-indicators)
531 (force-mode-line-update t))
533 (defun riece-command-toggle-own-freeze (&optional arg)
534 "Prevent automatic scrolling of the dialogue window.
535 The difference from `riece-command-freeze' is that your messages are hidden.
536 If prefix argument ARG is non-nil, toggle frozen status."
538 (with-current-buffer (if (and riece-channel-buffer-mode
539 riece-channel-buffer)
541 riece-dialogue-buffer)
543 (< 0 (prefix-numeric-value arg))
544 (not (eq riece-freeze 'own)))
545 (setq riece-freeze 'own)
546 (setq riece-freeze nil)))
547 (riece-update-status-indicators)
548 (force-mode-line-update t))
550 (defun riece-command-quit (&optional arg)
553 (if (y-or-n-p "Really quit IRC? ")
556 (read-string "Message: ")
557 (or riece-quit-message
558 (riece-extended-version))))
559 (process-list riece-process-list))
561 (riece-process-send-string (car process-list)
563 (format "QUIT :%s\r\n" message)
565 (setq process-list (cdr process-list))))))
567 (defun riece-command-raw (command)
568 "Enter raw IRC command, which is sent to the server."
569 (interactive "sIRC command: ")
570 (riece-send-string (concat command "\r\n")))
572 (defun riece-command-end-of-buffer ()
573 "Get end of the dialogue buffer."
576 (setq buffer (if riece-channel-buffer-mode
578 riece-dialogue-buffer))
579 (or (setq window (get-buffer-window buffer))
580 (setq window (get-buffer-window riece-dialogue-buffer)
581 buffer riece-dialogue-buffer))
583 (save-selected-window
584 (select-window window)
585 (goto-char (point-max))))))
587 (defun riece-command-copy-region (start end)
588 "Move current region between START and END to `kill-ring'."
590 (kill-new (buffer-substring-no-properties start end)))
592 (defun riece-command-open-server (server-name)
594 (list (completing-read "Server: " riece-server-alist)))
595 (if (riece-server-process server-name)
596 (error "%s is already opened" server-name))
598 (riece-server-name-to-server server-name)
601 (defun riece-command-close-server (server-name &optional message)
603 (list (completing-read
607 (with-current-buffer (process-buffer process)
610 (if current-prefix-arg
611 (read-string "Message: ")
612 (or riece-quit-message
613 (riece-extended-version)))))
614 (riece-process-send-string (riece-server-process server-name)
616 (format "QUIT :%s\r\n" message)
619 (defun riece-command-universal-server-name-argument ()
621 (let* ((riece-overriding-server-name
626 (with-current-buffer (process-buffer process)
628 riece-process-list)))
630 (key-binding (read-key-sequence
631 (format "Command to execute on \"%s\":"
632 riece-overriding-server-name)))))
634 (call-interactively command)))
636 (provide 'riece-commands)
638 ;;; riece-commands.el ends here