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-server-buffer
150 (riece-identity-server riece-current-channel)
151 (riece-channel-get-topic
152 (riece-identity-prefix
153 riece-current-channel)))
156 (riece-send-string (format "TOPIC %s :%s\r\n"
157 (riece-identity-prefix riece-current-channel)
160 (defun riece-command-invite (user)
162 (let ((completion-ignore-case t))
163 (unless (and riece-current-channel
164 (riece-channel-p (riece-identity-prefix
165 riece-current-channel)))
166 (error "Not on a channel"))
167 (list (completing-read
169 (mapcar #'list (riece-get-users-on-server))))))
170 (riece-send-string (format "INVITE %s %s\r\n"
171 user (riece-identity-prefix
172 riece-current-channel))))
174 (defun riece-command-kick (user &optional message)
176 (let ((completion-ignore-case t))
177 (unless (and riece-current-channel
178 (riece-channel-p (riece-identity-prefix
179 riece-current-channel)))
180 (error "Not on a channel"))
181 (list (completing-read
183 (mapcar #'list (riece-channel-get-users
184 riece-current-channel)))
185 (if current-prefix-arg
186 (read-string "Message: ")))))
189 (format "KICK %s %s :%s\r\n"
190 (riece-identity-prefix riece-current-channel)
192 (format "KICK %s %s\r\n"
193 (riece-identity-prefix riece-current-channel)
196 (defun riece-command-names (pattern)
198 (let ((completion-ignore-case t))
199 (list (read-from-minibuffer
201 (if (and riece-current-channel
202 (riece-channel-p (riece-identity-prefix
203 riece-current-channel)))
204 (cons (riece-format-identity riece-current-channel t)
206 (if (or (not (equal pattern ""))
207 (yes-or-no-p "Really want to query NAMES without argument? "))
208 (riece-send-string (format "NAMES %s\r\n" pattern))))
210 (defun riece-command-who (pattern)
212 (let ((completion-ignore-case t))
213 (list (read-from-minibuffer
215 (if (and riece-current-channel
216 (riece-channel-p (riece-identity-prefix
217 riece-current-channel)))
218 (cons (riece-format-identity riece-current-channel t)
220 (if (or (not (equal pattern ""))
221 (yes-or-no-p "Really want to query WHO without argument? "))
222 (riece-send-string (format "WHO %s\r\n" pattern))))
224 (defun riece-command-list (pattern)
226 (let ((completion-ignore-case t))
227 (list (read-from-minibuffer
229 (if (and riece-current-channel
230 (riece-channel-p (riece-identity-prefix
231 riece-current-channel)))
232 (cons (riece-format-identity riece-current-channel t)
234 (if (or (not (equal pattern ""))
235 (yes-or-no-p "Really want to query LIST without argument? "))
236 (riece-send-string (format "LIST %s\r\n" pattern))))
238 (defun riece-command-change-mode (channel change)
240 (let* ((completion-ignore-case t)
242 (if current-prefix-arg
243 (riece-completing-read-identity
244 "Channel/User: " riece-current-channels)
245 riece-current-channel))
246 (riece-overriding-server-name (riece-identity-server channel))
247 (riece-temp-minibuffer-message
248 (concat "[Available modes: "
249 (riece-with-server-buffer (riece-identity-server channel)
250 (if (riece-channel-p (riece-identity-prefix channel))
251 (if riece-supported-channel-modes
252 (apply #'string riece-supported-channel-modes))
253 (if riece-supported-user-modes
254 (apply #'string riece-supported-user-modes))))
257 (read-from-minibuffer
258 (concat (riece-concat-channel-modes
259 channel "Mode (? for help)") ": ")
260 nil riece-minibuffer-map))))
261 (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
264 (defun riece-command-set-operators (users &optional arg)
267 (riece-with-server-buffer
268 (riece-identity-server riece-current-channel)
269 (riece-channel-get-operators
270 (riece-identity-prefix riece-current-channel))))
271 (completion-ignore-case t)
273 (if current-prefix-arg
274 (setq users (riece-completing-read-multiple
276 (mapcar #'list operators)))
277 (setq users (riece-completing-read-multiple
281 (unless (member user operators)
283 (riece-with-server-buffer
284 (riece-identity-server
285 riece-current-channel)
286 (riece-channel-get-users
287 (riece-identity-prefix
288 riece-current-channel))))))))
289 (list users current-prefix-arg)))
292 (setq group (cons (car users) group)
294 (if (or (= (length group) 3)
297 (format "MODE %s %c%s %s\r\n"
298 (riece-identity-prefix riece-current-channel)
299 (if current-prefix-arg
302 (make-string (length group) ?o)
303 (mapconcat #'identity group " ")))))))
305 (defun riece-command-set-speakers (users &optional arg)
308 (riece-with-server-buffer
309 (riece-identity-server 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-server-buffer
325 (riece-identity-server
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-display-message
360 (riece-make-message (riece-current-nickname) riece-current-channel
363 (format "PRIVMSG %s :%s\r\n"
364 (riece-identity-prefix riece-current-channel)
366 (riece-display-message
367 (riece-make-message (riece-current-nickname) riece-current-channel
370 (defun riece-command-enter-message ()
371 "Send the current line to the current channel."
373 (riece-command-send-message (buffer-substring
374 (riece-line-beginning-position)
375 (riece-line-end-position))
377 (let ((next-line-add-newlines t))
380 (defun riece-command-enter-message-as-notice ()
381 "Send the current line to the current channel as NOTICE."
383 (riece-command-send-message (buffer-substring
384 (riece-line-beginning-position)
385 (riece-line-end-position))
387 (let ((next-line-add-newlines t))
390 (defun riece-command-join-channel (target key)
391 (let ((process (riece-server-process (riece-identity-server target))))
393 (error "%s" (substitute-command-keys
394 "Type \\[riece-command-open-server] to open server.")))
395 (riece-process-send-string process
397 (format "JOIN %s :%s\r\n"
398 (riece-identity-prefix target)
400 (format "JOIN %s\r\n"
401 (riece-identity-prefix target))))))
403 (defun riece-command-join-partner (target)
404 (let ((pointer (riece-identity-member target riece-current-channels)))
406 (riece-command-switch-to-channel (car pointer))
407 (riece-join-channel target)
408 (riece-switch-to-channel target)
409 (riece-redisplay-buffers))))
411 (defun riece-command-join (target &optional key)
413 (let* ((completion-ignore-case t)
415 (riece-completing-read-identity
416 "Channel/User: " riece-current-channels))
418 (if (and current-prefix-arg
419 (riece-channel-p target))
421 (riece-read-passwd (format "Key for %s: " target))))
423 (let ((pointer (riece-identity-member target riece-current-channels)))
425 (riece-command-switch-to-channel (car pointer))
426 (if (riece-channel-p (riece-identity-prefix target))
427 (riece-command-join-channel target key)
428 (riece-command-join-partner target)))))
430 (defun riece-command-part-channel (target message)
431 (let ((process (riece-server-process (riece-identity-server target))))
433 (error "%s" (substitute-command-keys
434 "Type \\[riece-command-open-server] to open server.")))
435 (riece-process-send-string process
437 (format "PART %s :%s\r\n"
438 (riece-identity-prefix target)
440 (format "PART %s\r\n"
441 (riece-identity-prefix target))))))
443 (defun riece-command-part (target &optional message)
445 (let* ((completion-ignore-case t)
447 (riece-completing-read-identity
448 "Channel/User: " riece-current-channels nil nil
449 (cons (riece-format-identity riece-current-channel) 0)))
451 (if (and current-prefix-arg
452 (riece-channel-p (riece-identity-prefix target)))
453 (setq message (read-string "Message: ")))
454 (list target message)))
455 (if (riece-identity-member target riece-current-channels)
456 (if (riece-channel-p (riece-identity-prefix target))
457 (riece-command-part-channel target message)
458 (riece-part-channel target)
459 (riece-redisplay-buffers))
460 (error "You are not talking with %s" target)))
462 (defun riece-command-change-nickname (nickname)
463 "Change your nickname to NICK."
464 (interactive "sEnter your nickname: ")
465 (riece-send-string (format "NICK %s\r\n" nickname)))
467 (defun riece-command-scroll-down (lines)
468 "Scroll LINES down dialogue buffer from command buffer."
470 (let ((other-window-scroll-buffer
471 (if riece-channel-buffer-mode
473 riece-dialogue-buffer)))
474 (when (get-buffer-window other-window-scroll-buffer)
476 (scroll-other-window-down lines)
478 (message "Beginning of buffer"))))))
480 (defun riece-command-scroll-up (lines)
481 "Scroll LINES up dialogue buffer from command buffer."
483 (let* ((other-window-scroll-buffer
484 (if riece-channel-buffer-mode
486 riece-dialogue-buffer)))
487 (when (get-buffer-window other-window-scroll-buffer)
489 (scroll-other-window lines)
491 (message "End of buffer"))))))
493 (defun riece-command-nick-scroll-down (lines)
494 "Scroll LINES down nick buffer from command buffer."
496 (let ((other-window-scroll-buffer riece-user-list-buffer))
497 (when (get-buffer-window other-window-scroll-buffer)
499 (scroll-other-window-down lines)
501 (message "Beginning of buffer"))))))
503 (defun riece-command-nick-scroll-up (lines)
504 "Scroll LINES up nick buffer from command buffer."
506 (let* ((other-window-scroll-buffer riece-user-list-buffer))
507 (when (get-buffer-window other-window-scroll-buffer)
509 (scroll-other-window lines)
511 (message "End of buffer"))))))
513 (defun riece-command-toggle-away (&optional message)
514 "Mark yourself as being away."
516 (if (and (not (riece-user-get-away (riece-current-nickname)))
517 (or (null riece-away-message)
519 (let ((message (read-string "Away message: ")))
522 (riece-send-string (format "AWAY :%s\r\n" message))
523 (riece-send-string "AWAY\r\n")))
525 (defun riece-command-toggle-freeze (&optional arg)
526 "Prevent automatic scrolling of the dialogue window.
527 If prefix argument ARG is non-nil, toggle frozen status."
529 (with-current-buffer (if (and riece-channel-buffer-mode
530 riece-channel-buffer)
532 riece-dialogue-buffer)
533 (setq riece-freeze (if arg
534 (< 0 (prefix-numeric-value arg))
535 (not riece-freeze))))
536 (riece-update-status-indicators)
537 (force-mode-line-update t))
539 (defun riece-command-toggle-own-freeze (&optional arg)
540 "Prevent automatic scrolling of the dialogue window.
541 The difference from `riece-command-freeze' is that your messages are hidden.
542 If prefix argument ARG is non-nil, toggle frozen status."
544 (with-current-buffer (if (and riece-channel-buffer-mode
545 riece-channel-buffer)
547 riece-dialogue-buffer)
549 (< 0 (prefix-numeric-value arg))
550 (not (eq riece-freeze 'own)))
551 (setq riece-freeze 'own)
552 (setq riece-freeze nil)))
553 (riece-update-status-indicators)
554 (force-mode-line-update t))
556 (defun riece-command-quit (&optional arg)
559 (if (y-or-n-p "Really quit IRC? ")
562 (read-string "Message: ")
563 (or riece-quit-message
564 (riece-extended-version))))
565 (process-list riece-process-list))
567 (riece-process-send-string (car process-list)
569 (format "QUIT :%s\r\n" message)
571 (setq process-list (cdr process-list))))))
573 (defun riece-command-raw (command)
574 "Enter raw IRC command, which is sent to the server."
575 (interactive "sIRC command: ")
576 (riece-send-string (concat command "\r\n")))
578 (defun riece-command-end-of-buffer ()
579 "Get end of the dialogue buffer."
582 (setq buffer (if riece-channel-buffer-mode
584 riece-dialogue-buffer))
585 (or (setq window (get-buffer-window buffer))
586 (setq window (get-buffer-window riece-dialogue-buffer)
587 buffer riece-dialogue-buffer))
589 (save-selected-window
590 (select-window window)
591 (goto-char (point-max))))))
593 (defun riece-command-copy-region (start end)
594 "Move current region between START and END to `kill-ring'."
596 (kill-new (buffer-substring-no-properties start end)))
598 (defun riece-command-open-server (server-name)
600 (list (completing-read "Server: " riece-server-alist)))
601 (if (riece-server-process server-name)
602 (error "%s is already opened" server-name))
604 (riece-server-name-to-server server-name)
607 (defun riece-command-close-server (server-name &optional message)
609 (list (completing-read
613 (with-current-buffer (process-buffer process)
614 (list riece-server-name)))
616 (if current-prefix-arg
617 (read-string "Message: ")
618 (or riece-quit-message
619 (riece-extended-version)))))
620 (riece-process-send-string (riece-server-process server-name)
622 (format "QUIT :%s\r\n" message)
625 (defun riece-command-universal-server-name-argument ()
627 (let* ((riece-overriding-server-name
632 (with-current-buffer (process-buffer process)
633 (list riece-server-name)))
634 riece-process-list)))
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