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 (eval-when-compile (require 'riece-inlines))
29 (require 'riece-channel)
30 (require 'riece-complete)
31 (require 'riece-display)
32 (require 'riece-version)
33 (require 'riece-server)
35 (require 'riece-identity)
36 (require 'riece-message)
39 (defun riece-command-switch-to-channel (channel)
41 (list (completing-read "Channel/User: "
42 (mapcar #'list riece-current-channels)
44 (riece-switch-to-channel channel)
45 (riece-command-configure-windows))
47 (defun riece-command-switch-to-channel-by-number (number)
49 (let ((command-name (symbol-name this-command)))
50 (if (string-match "[0-9]+$" command-name)
51 (list (string-to-number (match-string 0 command-name)))
52 (list (string-to-number (read-string "Number: "))))))
53 (let ((channels riece-current-channels)
58 (setq index (1+ index)))
59 (setq channels (cdr channels)))
61 (riece-command-switch-to-channel (car channels))
62 (error "No such number!"))))
67 (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
68 (number-to-string number)))
69 'riece-command-switch-to-channel-by-number)
70 (setq number (1+ number)))))
72 (defun riece-command-next-channel ()
73 "Select the next channel."
75 (let ((pointer (cdr (string-list-member-ignore-case
77 riece-current-channels))))
80 (setq pointer (cdr pointer)))
82 (riece-command-switch-to-channel (car pointer))
83 (error "No such channel!"))))
85 (defun riece-command-previous-channel ()
86 "Select the previous channel."
88 (let ((pointer (string-list-member-ignore-case
90 riece-current-channels))
91 (start riece-current-channels)
93 (while (and start (not (eq start pointer)))
95 (setq channel (car start)))
96 (setq start (cdr start)))
98 (riece-command-switch-to-channel channel)
99 (error "No such channel!"))))
101 (defun riece-command-select-command-buffer ()
102 "Select the command buffer."
104 (let ((window (get-buffer-window riece-command-buffer)))
106 (select-window window))))
108 (defun riece-command-configure-windows ()
110 (riece-redisplay-buffers t))
112 (defun riece-command-toggle-channel-buffer-mode ()
114 (setq riece-channel-buffer-mode
115 (not riece-channel-buffer-mode))
116 (riece-command-configure-windows))
118 (defun riece-command-toggle-user-list-buffer-mode ()
120 (setq riece-user-list-buffer-mode
121 (not riece-user-list-buffer-mode))
122 (riece-command-configure-windows))
124 (defun riece-command-toggle-channel-list-buffer-mode ()
126 (setq riece-channel-list-buffer-mode
127 (not riece-channel-list-buffer-mode))
128 (riece-command-configure-windows))
130 (defun riece-command-finger (user &optional recurse)
132 (let* ((completion-ignore-case t)
133 (user (completing-read
135 (mapcar #'list (riece-get-users-on-server)))))
136 (list user current-prefix-arg)))
138 (riece-send-string (format "WHOIS %s %s\r\n" user user))
139 (riece-send-string (format "WHOIS %s\r\n" user))))
141 (defun riece-command-topic (topic)
143 (list (read-from-minibuffer
144 "Topic: " (cons (or (riece-channel-get-topic
145 riece-current-channel)
148 (riece-send-string (format "TOPIC %s :%s\r\n"
149 (riece-identity-prefix riece-current-channel)
152 (defun riece-command-invite (user)
154 (let ((completion-ignore-case t))
155 (unless (and riece-current-channel
156 (riece-channel-p riece-current-channel))
157 (error "Not on a channel"))
158 (list (completing-read
160 (mapcar #'list (riece-get-users-on-server))))))
161 (riece-send-string (format "INVITE %s %s\r\n"
162 user (riece-identity-prefix
163 riece-current-channel))))
165 (defun riece-command-kick (user &optional message)
167 (let ((completion-ignore-case t))
168 (unless (and riece-current-channel
169 (riece-channel-p riece-current-channel))
170 (error "Not on a channel"))
171 (list (completing-read
173 (mapcar #'list (riece-channel-get-users
174 riece-current-channel)))
175 (if current-prefix-arg
176 (read-string "Message: ")))))
179 (format "KICK %s %s :%s\r\n"
180 (riece-identity-prefix riece-current-channel)
182 (format "KICK %s %s\r\n"
183 (riece-identity-prefix riece-current-channel)
186 (defun riece-command-names (pattern)
188 (let ((completion-ignore-case t))
189 (list (read-from-minibuffer
191 (if (and riece-current-channel
192 (riece-channel-p riece-current-channel))
193 (cons (riece-identity-prefix riece-current-channel)
195 (if (or (not (equal pattern ""))
196 (yes-or-no-p "Really want to query NAMES without argument? "))
197 (riece-send-string (format "NAMES %s\r\n" pattern))))
199 (defun riece-command-who (pattern)
201 (let ((completion-ignore-case t))
202 (list (read-from-minibuffer
204 (if (and riece-current-channel
205 (riece-channel-p riece-current-channel))
206 (cons (riece-identity-prefix riece-current-channel)
208 (if (or (not (equal pattern ""))
209 (yes-or-no-p "Really want to query WHO without argument? "))
210 (riece-send-string (format "WHO %s\r\n" pattern))))
212 (defun riece-command-list (pattern)
214 (let ((completion-ignore-case t))
215 (list (read-from-minibuffer
217 (if (and riece-current-channel
218 (riece-channel-p riece-current-channel))
219 (cons (riece-identity-prefix riece-current-channel)
221 (if (or (not (equal pattern ""))
222 (yes-or-no-p "Really want to query LIST without argument? "))
223 (riece-send-string (format "LIST %s\r\n" pattern))))
225 (defun riece-command-change-mode (channel change)
227 (let* ((completion-ignore-case t)
229 (if current-prefix-arg
232 (mapcar #'list riece-current-channels))
233 riece-current-channel))
234 (riece-overriding-server-name (riece-identity-server channel))
235 (riece-temp-minibuffer-message
236 (concat "[Available modes: "
237 (riece-with-server-buffer
238 (if (and (riece-channel-p channel)
239 riece-supported-channel-modes)
240 (apply #'string riece-supported-channel-modes)
241 (if (and (not (riece-channel-p channel))
242 riece-supported-user-modes)
243 (apply #'string riece-supported-user-modes))))
246 (read-from-minibuffer
247 (concat (riece-concat-modes channel "Mode (? for help)") ": ")
248 nil riece-minibuffer-map))))
249 (riece-send-string (format "MODE %s :%s\r\n" channel change)))
251 (defun riece-command-set-operators (users &optional arg)
253 (let ((operators (riece-channel-get-operators riece-current-channel))
254 (completion-ignore-case t)
256 (if current-prefix-arg
257 (setq users (riece-completing-read-multiple
259 (mapcar #'list operators)))
260 (setq users (riece-completing-read-multiple
262 (delq nil (mapcar (lambda (user)
263 (unless (member user operators)
265 (riece-channel-get-users
266 riece-current-channel))))))
267 (list users current-prefix-arg)))
270 (setq group (cons (car users) group)
272 (if (or (= (length group) 3)
275 (format "MODE %s %c%s %s\r\n"
276 (riece-identity-prefix riece-current-channel)
277 (if current-prefix-arg
280 (make-string (length group) ?o)
281 (mapconcat #'identity group " ")))))))
283 (defun riece-command-set-speakers (users &optional arg)
285 (let ((speakers (riece-channel-get-speakers riece-current-channel))
286 (completion-ignore-case t)
288 (if current-prefix-arg
289 (setq users (riece-completing-read-multiple
291 (mapcar #'list speakers)))
292 (setq users (riece-completing-read-multiple
294 (delq nil (mapcar (lambda (user)
295 (unless (member user speakers)
297 (riece-channel-get-users
298 riece-current-channel))))))
299 (list users current-prefix-arg)))
302 (setq group (cons (car users) group)
304 (if (or (= (length group) 3)
307 (format "MODE %s %c%s %s\r\n"
308 (riece-identity-prefix riece-current-channel)
309 (if current-prefix-arg
312 (make-string (length group) ?v)
313 (mapconcat #'identity group " ")))))))
315 (defun riece-command-send-message (message notice)
316 "Send MESSAGE to the current channel."
317 (if (equal message "")
318 (error "No text to send"))
319 (unless riece-current-channel
320 (error (substitute-command-keys
321 "Type \\[riece-command-join] to join a channel")))
325 (format "NOTICE %s :%s\r\n"
326 (riece-identity-prefix riece-current-channel)
328 (riece-own-channel-message message riece-current-channel 'notice))
330 (format "PRIVMSG %s :%s\r\n"
331 (riece-identity-prefix riece-current-channel)
333 (riece-own-channel-message message)))
335 (defun riece-command-enter-message ()
336 "Send the current line to the current channel."
338 (riece-command-send-message (buffer-substring
339 (riece-line-beginning-position)
340 (riece-line-end-position))
342 (let ((next-line-add-newlines t))
345 (defun riece-command-enter-message-as-notice ()
346 "Send the current line to the current channel as NOTICE."
348 (riece-command-send-message (buffer-substring
349 (riece-line-beginning-position)
350 (riece-line-end-position))
352 (let ((next-line-add-newlines t))
355 (defun riece-command-join-channel (target key)
356 (let ((server-name (riece-identity-server target))
359 (setq process (cdr (assoc server-name riece-server-process-alist)))
360 (setq process riece-server-process))
362 (error "%s" (substitute-command-keys
363 "Type \\[riece-command-open-server] to open server.")))
364 (riece-process-send-string process
366 (format "JOIN %s :%s\r\n"
367 (riece-identity-prefix target)
369 (format "JOIN %s\r\n"
370 (riece-identity-prefix target))))))
372 (defun riece-command-join-partner (target)
373 (let ((pointer (riece-identity-member target riece-current-channels)))
375 (riece-command-switch-to-channel (car pointer))
376 (riece-join-channel target)
377 (riece-switch-to-channel target)
378 (riece-redisplay-buffers))))
380 (defun riece-command-join (target &optional key)
382 (let ((completion-ignore-case t)
384 (completing-read "Channel/User: "
385 (mapcar #'list riece-current-channels)))
387 (if (and current-prefix-arg
388 (riece-channel-p target))
390 (riece-read-passwd (format "Key for %s: " target))))
392 (let ((pointer (riece-identity-member target riece-current-channels)))
394 (riece-command-switch-to-channel (car pointer))
395 (if (riece-channel-p target)
396 (riece-command-join-channel target key)
397 (riece-command-join-partner target)))))
399 (defun riece-command-part-channel (target message)
400 (let ((server-name (riece-identity-server target))
403 (setq process (cdr (assoc server-name riece-server-process-alist)))
404 (setq process riece-server-process))
406 (error "%s" (substitute-command-keys
407 "Type \\[riece-command-open-server] to open server.")))
408 (riece-process-send-string process
410 (format "PART %s :%s\r\n"
411 (riece-identity-prefix target)
413 (format "PART %s\r\n"
414 (riece-identity-prefix target))))))
416 (defun riece-command-part (target &optional message)
418 (let ((completion-ignore-case t)
420 (completing-read "Channel/User: "
421 (mapcar #'list riece-current-channels)
422 nil t (cons riece-current-channel 0)))
424 (if (and current-prefix-arg
425 (riece-channel-p target))
426 (setq message (read-string "Message: ")))
427 (list target message)))
428 (if (riece-identity-member target riece-current-channels)
429 (if (riece-channel-p target)
430 (riece-command-part-channel target message)
431 (riece-part-channel target)
432 (riece-redisplay-buffers))
433 (error "You are not talking with %s" target)))
435 (defun riece-command-change-nickname (nickname)
436 "Change your nickname to NICK."
437 (interactive "sEnter your nickname: ")
438 (riece-send-string (format "NICK %s\r\n" nickname)))
440 (defun riece-command-scroll-down (lines)
441 "Scroll LINES down dialogue buffer from command buffer."
443 (let ((other-window-scroll-buffer
444 (if riece-channel-buffer-mode
446 riece-dialogue-buffer)))
447 (when (get-buffer-window other-window-scroll-buffer)
449 (scroll-other-window-down lines)
451 (message "Beginning of buffer"))))))
453 (defun riece-command-scroll-up (lines)
454 "Scroll LINES up dialogue buffer from command buffer."
456 (let* ((other-window-scroll-buffer
457 (if riece-channel-buffer-mode
459 riece-dialogue-buffer)))
460 (when (get-buffer-window other-window-scroll-buffer)
462 (scroll-other-window lines)
464 (message "End of buffer"))))))
466 (defun riece-command-nick-scroll-down (lines)
467 "Scroll LINES down nick buffer from command buffer."
469 (let ((other-window-scroll-buffer riece-user-list-buffer))
470 (when (get-buffer-window other-window-scroll-buffer)
472 (scroll-other-window-down lines)
474 (message "Beginning of buffer"))))))
476 (defun riece-command-nick-scroll-up (lines)
477 "Scroll LINES up nick buffer from command buffer."
479 (let* ((other-window-scroll-buffer riece-user-list-buffer))
480 (when (get-buffer-window other-window-scroll-buffer)
482 (scroll-other-window lines)
484 (message "End of buffer"))))))
486 (defun riece-command-toggle-away (&optional message)
487 "Mark yourself as being away."
489 (if current-prefix-arg
490 (let ((message (read-string "Away message: ")))
493 (riece-send-string (format "AWAY :%s\r\n" message))
494 (riece-send-string "AWAY\r\n")))
496 (defun riece-command-toggle-freeze (&optional arg)
497 "Prevent automatic scrolling of the dialogue window.
498 If prefix argument ARG is non-nil, toggle frozen status."
500 (riece-freeze (if riece-channel-buffer-mode
502 riece-dialogue-buffer)
503 (if arg (prefix-numeric-value arg))))
505 (defun riece-command-toggle-own-freeze (&optional arg)
506 "Prevent automatic scrolling of the dialogue window.
507 The difference from `riece-command-freeze' is that your messages are hidden.
508 If prefix argument ARG is non-nil, toggle frozen status."
510 (riece-own-freeze (if riece-channel-buffer-mode
512 riece-dialogue-buffer)
513 (if arg (prefix-numeric-value arg))))
515 (defun riece-command-quit (&optional arg)
518 (if (y-or-n-p "Really quit IRC? ")
521 (read-string "Message: ")
522 (or riece-quit-message
523 (riece-extended-version)))))
524 (riece-close-all-server message))))
526 (defun riece-command-raw (command)
527 "Enter raw IRC command, which is sent to the server."
528 (interactive "sIRC command: ")
529 (riece-send-string (concat command "\r\n")))
531 (defun riece-command-end-of-buffer ()
532 "Get end of the dialogue buffer."
535 (setq buffer (if riece-channel-buffer-mode
537 riece-dialogue-buffer))
538 (or (setq window (get-buffer-window buffer))
539 (setq window (get-buffer-window riece-dialogue-buffer)
540 buffer riece-dialogue-buffer))
542 (save-selected-window
543 (select-window window)
544 (goto-char (point-max))))))
546 (defun riece-command-copy-region (start end)
547 "Move current region between START and END to `kill-ring'."
549 (kill-new (buffer-substring-no-properties start end)))
551 (defun riece-command-open-server (server-name)
553 (list (completing-read "Server: " riece-server-alist)))
554 (let ((process (riece-start-server
555 (riece-server-name-to-server server-name)
557 (with-current-buffer (process-buffer process)
558 (setq riece-server-name server-name))
559 (setq riece-server-process-alist
560 (cons (cons server-name process)
561 riece-server-process-alist))))
563 (defun riece-command-close-server (server-name &optional message)
565 (list (completing-read "Server: " riece-server-process-alist)
566 (if current-prefix-arg
567 (read-string "Message: ")
568 (or riece-quit-message
569 (riece-extended-version)))))
570 (riece-close-server server-name message))
572 (defun riece-command-universal-server-name-argument ()
574 (let* ((riece-overriding-server-name
575 (completing-read "Server: "
576 riece-server-process-alist))
578 (key-binding (read-key-sequence
579 (format "Command to execute on \"%s\":"
580 riece-overriding-server-name)))))
582 (call-interactively command)))
584 (provide 'riece-commands)
586 ;;; riece-commands.el ends here