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
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
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" (riece-identity-prefix channel)
268 (defun riece-command-set-operators (users &optional arg)
271 (riece-with-identity-buffer riece-current-channel
272 (riece-channel-get-operators
273 (riece-identity-prefix riece-current-channel))))
274 (completion-ignore-case t)
276 (if current-prefix-arg
277 (setq users (riece-completing-read-multiple
279 (mapcar #'list operators)))
280 (setq users (riece-completing-read-multiple
284 (unless (member user operators)
286 (riece-with-identity-buffer
287 riece-current-channel
288 (riece-channel-get-users
289 (riece-identity-prefix
290 riece-current-channel))))))))
291 (list users current-prefix-arg)))
294 (setq group (cons (car users) group)
296 (if (or (= (length group) 3)
299 (format "MODE %s %c%s %s\r\n"
300 (riece-identity-prefix riece-current-channel)
301 (if current-prefix-arg
304 (make-string (length group) ?o)
305 (mapconcat #'identity group " ")))))))
307 (defun riece-command-set-speakers (users &optional arg)
310 (riece-with-identity-buffer riece-current-channel
311 (riece-channel-get-speakers
312 (riece-identity-prefix riece-current-channel))))
313 (completion-ignore-case t)
315 (if current-prefix-arg
316 (setq users (riece-completing-read-multiple
318 (mapcar #'list speakers)))
319 (setq users (riece-completing-read-multiple
323 (unless (member user speakers)
325 (riece-with-identity-buffer
326 riece-current-channel
327 (riece-channel-get-users
328 (riece-identity-prefix
329 riece-current-channel))))))))
330 (list users current-prefix-arg)))
333 (setq group (cons (car users) group)
335 (if (or (= (length group) 3)
338 (format "MODE %s %c%s %s\r\n"
339 (riece-identity-prefix riece-current-channel)
340 (if current-prefix-arg
343 (make-string (length group) ?v)
344 (mapconcat #'identity group " ")))))))
346 (defun riece-command-send-message (message notice)
347 "Send MESSAGE to the current channel."
348 (if (equal message "")
349 (error "No text to send"))
350 (unless riece-current-channel
351 (error (substitute-command-keys
352 "Type \\[riece-command-join] to join a channel")))
356 (format "NOTICE %s :%s\r\n"
357 (riece-identity-prefix riece-current-channel)
359 (riece-own-channel-message message riece-current-channel 'notice))
361 (format "PRIVMSG %s :%s\r\n"
362 (riece-identity-prefix riece-current-channel)
364 (riece-own-channel-message message)))
366 (defun riece-command-enter-message ()
367 "Send the current line to the current channel."
369 (riece-command-send-message (buffer-substring
370 (riece-line-beginning-position)
371 (riece-line-end-position))
373 (let ((next-line-add-newlines t))
376 (defun riece-command-enter-message-as-notice ()
377 "Send the current line to the current channel as NOTICE."
379 (riece-command-send-message (buffer-substring
380 (riece-line-beginning-position)
381 (riece-line-end-position))
383 (let ((next-line-add-newlines t))
386 (defun riece-command-join-channel (target key)
387 (let ((process (riece-server-process (riece-identity-server target))))
389 (error "%s" (substitute-command-keys
390 "Type \\[riece-command-open-server] to open server.")))
391 (riece-process-send-string process
393 (format "JOIN %s :%s\r\n"
394 (riece-identity-prefix target)
396 (format "JOIN %s\r\n"
397 (riece-identity-prefix target))))))
399 (defun riece-command-join-partner (target)
400 (let ((pointer (riece-identity-member target riece-current-channels)))
402 (riece-command-switch-to-channel (car pointer))
403 (riece-join-channel target)
404 (riece-switch-to-channel target)
405 (riece-redisplay-buffers))))
407 (defun riece-command-join (target &optional key)
409 (let ((completion-ignore-case t)
411 (riece-completing-read-identity
412 "Channel/User: " riece-current-channels))
414 (if (and current-prefix-arg
415 (riece-channel-p target))
417 (riece-read-passwd (format "Key for %s: " target))))
419 (let ((pointer (riece-identity-member target riece-current-channels)))
421 (riece-command-switch-to-channel (car pointer))
422 (if (riece-channel-p (riece-identity-prefix target))
423 (riece-command-join-channel target key)
424 (riece-command-join-partner target)))))
426 (defun riece-command-part-channel (target message)
427 (let ((process (riece-server-process (riece-identity-server target))))
429 (error "%s" (substitute-command-keys
430 "Type \\[riece-command-open-server] to open server.")))
431 (riece-process-send-string process
433 (format "PART %s :%s\r\n"
434 (riece-identity-prefix target)
436 (format "PART %s\r\n"
437 (riece-identity-prefix target))))))
439 (defun riece-command-part (target &optional message)
441 (let ((completion-ignore-case t)
443 (riece-completing-read-identity
444 "Channel/User: " riece-current-channels))
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)
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)
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