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 (when (> (length riece-current-channels) 1)
76 (let ((pointer (cdr (string-list-member-ignore-case
78 riece-current-channels))))
81 (setq pointer (cdr pointer)))
83 (riece-command-switch-to-channel (car pointer))
84 (error "No such channel!")))))
86 (defun riece-command-previous-channel ()
87 "Select the previous channel."
89 (when (> (length riece-current-channels) 1)
90 (let ((pointer (string-list-member-ignore-case
92 riece-current-channels))
93 (start riece-current-channels)
95 (while (and start (not (eq start pointer)))
97 (setq channel (car start)))
98 (setq start (cdr 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-command-configure-windows))
120 (defun riece-command-toggle-user-list-buffer-mode ()
122 (setq riece-user-list-buffer-mode
123 (not riece-user-list-buffer-mode))
124 (riece-command-configure-windows))
126 (defun riece-command-toggle-channel-list-buffer-mode ()
128 (setq riece-channel-list-buffer-mode
129 (not riece-channel-list-buffer-mode))
130 (riece-command-configure-windows))
132 (defun riece-command-finger (user &optional recurse)
134 (let* ((completion-ignore-case t)
135 (user (completing-read
137 (mapcar #'list (riece-get-users-on-server)))))
138 (list user current-prefix-arg)))
140 (riece-send-string (format "WHOIS %s %s\r\n" user user))
141 (riece-send-string (format "WHOIS %s\r\n" user))))
143 (defun riece-command-topic (topic)
145 (list (read-from-minibuffer
146 "Topic: " (cons (or (riece-channel-get-topic
147 riece-current-channel)
150 (riece-send-string (format "TOPIC %s :%s\r\n"
151 (riece-identity-prefix riece-current-channel)
154 (defun riece-command-invite (user)
156 (let ((completion-ignore-case t))
157 (unless (and riece-current-channel
158 (riece-channel-p riece-current-channel))
159 (error "Not on a channel"))
160 (list (completing-read
162 (mapcar #'list (riece-get-users-on-server))))))
163 (riece-send-string (format "INVITE %s %s\r\n"
164 user (riece-identity-prefix
165 riece-current-channel))))
167 (defun riece-command-kick (user &optional message)
169 (let ((completion-ignore-case t))
170 (unless (and riece-current-channel
171 (riece-channel-p riece-current-channel))
172 (error "Not on a channel"))
173 (list (completing-read
175 (mapcar #'list (riece-channel-get-users
176 riece-current-channel)))
177 (if current-prefix-arg
178 (read-string "Message: ")))))
181 (format "KICK %s %s :%s\r\n"
182 (riece-identity-prefix riece-current-channel)
184 (format "KICK %s %s\r\n"
185 (riece-identity-prefix riece-current-channel)
188 (defun riece-command-names (pattern)
190 (let ((completion-ignore-case t))
191 (list (read-from-minibuffer
193 (if (and riece-current-channel
194 (riece-channel-p riece-current-channel))
195 (cons (riece-identity-prefix riece-current-channel)
197 (if (or (not (equal pattern ""))
198 (yes-or-no-p "Really want to query NAMES without argument? "))
199 (riece-send-string (format "NAMES %s\r\n" pattern))))
201 (defun riece-command-who (pattern)
203 (let ((completion-ignore-case t))
204 (list (read-from-minibuffer
206 (if (and riece-current-channel
207 (riece-channel-p riece-current-channel))
208 (cons (riece-identity-prefix riece-current-channel)
210 (if (or (not (equal pattern ""))
211 (yes-or-no-p "Really want to query WHO without argument? "))
212 (riece-send-string (format "WHO %s\r\n" pattern))))
214 (defun riece-command-list (pattern)
216 (let ((completion-ignore-case t))
217 (list (read-from-minibuffer
219 (if (and riece-current-channel
220 (riece-channel-p riece-current-channel))
221 (cons (riece-identity-prefix riece-current-channel)
223 (if (or (not (equal pattern ""))
224 (yes-or-no-p "Really want to query LIST without argument? "))
225 (riece-send-string (format "LIST %s\r\n" pattern))))
227 (defun riece-command-change-mode (channel change)
229 (let* ((completion-ignore-case t)
231 (if current-prefix-arg
234 (mapcar #'list riece-current-channels))
235 riece-current-channel))
236 (riece-overriding-server-name (riece-identity-server channel))
237 (riece-temp-minibuffer-message
238 (concat "[Available modes: "
239 (riece-with-server-buffer
240 (if (and (riece-channel-p channel)
241 riece-supported-channel-modes)
242 (apply #'string riece-supported-channel-modes)
243 (if (and (not (riece-channel-p channel))
244 riece-supported-user-modes)
245 (apply #'string riece-supported-user-modes))))
248 (read-from-minibuffer
249 (concat (riece-concat-modes channel "Mode (? for help)") ": ")
250 nil riece-minibuffer-map))))
251 (riece-send-string (format "MODE %s :%s\r\n" channel change)))
253 (defun riece-command-set-operators (users &optional arg)
255 (let ((operators (riece-channel-get-operators riece-current-channel))
256 (completion-ignore-case t)
258 (if current-prefix-arg
259 (setq users (riece-completing-read-multiple
261 (mapcar #'list operators)))
262 (setq users (riece-completing-read-multiple
264 (delq nil (mapcar (lambda (user)
265 (unless (member user operators)
267 (riece-channel-get-users
268 riece-current-channel))))))
269 (list users current-prefix-arg)))
272 (setq group (cons (car users) group)
274 (if (or (= (length group) 3)
277 (format "MODE %s %c%s %s\r\n"
278 (riece-identity-prefix riece-current-channel)
279 (if current-prefix-arg
282 (make-string (length group) ?o)
283 (mapconcat #'identity group " ")))))))
285 (defun riece-command-set-speakers (users &optional arg)
287 (let ((speakers (riece-channel-get-speakers riece-current-channel))
288 (completion-ignore-case t)
290 (if current-prefix-arg
291 (setq users (riece-completing-read-multiple
293 (mapcar #'list speakers)))
294 (setq users (riece-completing-read-multiple
296 (delq nil (mapcar (lambda (user)
297 (unless (member user speakers)
299 (riece-channel-get-users
300 riece-current-channel))))))
301 (list users current-prefix-arg)))
304 (setq group (cons (car users) group)
306 (if (or (= (length group) 3)
309 (format "MODE %s %c%s %s\r\n"
310 (riece-identity-prefix riece-current-channel)
311 (if current-prefix-arg
314 (make-string (length group) ?v)
315 (mapconcat #'identity group " ")))))))
317 (defun riece-command-send-message (message notice)
318 "Send MESSAGE to the current channel."
319 (if (equal message "")
320 (error "No text to send"))
321 (unless riece-current-channel
322 (error (substitute-command-keys
323 "Type \\[riece-command-join] to join a channel")))
327 (format "NOTICE %s :%s\r\n"
328 (riece-identity-prefix riece-current-channel)
330 (riece-own-channel-message message riece-current-channel 'notice))
332 (format "PRIVMSG %s :%s\r\n"
333 (riece-identity-prefix riece-current-channel)
335 (riece-own-channel-message message)))
337 (defun riece-command-enter-message ()
338 "Send the current line to the current channel."
340 (riece-command-send-message (buffer-substring
341 (riece-line-beginning-position)
342 (riece-line-end-position))
344 (let ((next-line-add-newlines t))
347 (defun riece-command-enter-message-as-notice ()
348 "Send the current line to the current channel as NOTICE."
350 (riece-command-send-message (buffer-substring
351 (riece-line-beginning-position)
352 (riece-line-end-position))
354 (let ((next-line-add-newlines t))
357 (defun riece-command-join-channel (target key)
358 (let ((server-name (riece-identity-server target))
361 (setq process (cdr (assoc server-name riece-server-process-alist)))
362 (setq process riece-server-process))
364 (error "%s" (substitute-command-keys
365 "Type \\[riece-command-open-server] to open server.")))
366 (riece-process-send-string process
368 (format "JOIN %s :%s\r\n"
369 (riece-identity-prefix target)
371 (format "JOIN %s\r\n"
372 (riece-identity-prefix target))))))
374 (defun riece-command-join-partner (target)
375 (let ((pointer (riece-identity-member target riece-current-channels)))
377 (riece-command-switch-to-channel (car pointer))
378 (riece-join-channel target)
379 (riece-switch-to-channel target)
380 (riece-redisplay-buffers))))
382 (defun riece-command-join (target &optional key)
384 (let ((completion-ignore-case t)
386 (completing-read "Channel/User: "
387 (mapcar #'list riece-current-channels)))
389 (if (and current-prefix-arg
390 (riece-channel-p target))
392 (riece-read-passwd (format "Key for %s: " target))))
394 (let ((pointer (riece-identity-member target riece-current-channels)))
396 (riece-command-switch-to-channel (car pointer))
397 (if (riece-channel-p target)
398 (riece-command-join-channel target key)
399 (riece-command-join-partner target)))))
401 (defun riece-command-part-channel (target message)
402 (let ((server-name (riece-identity-server target))
405 (setq process (cdr (assoc server-name riece-server-process-alist)))
406 (setq process riece-server-process))
408 (error "%s" (substitute-command-keys
409 "Type \\[riece-command-open-server] to open server.")))
410 (riece-process-send-string process
412 (format "PART %s :%s\r\n"
413 (riece-identity-prefix target)
415 (format "PART %s\r\n"
416 (riece-identity-prefix target))))))
418 (defun riece-command-part (target &optional message)
420 (let ((completion-ignore-case t)
422 (completing-read "Channel/User: "
423 (mapcar #'list riece-current-channels)
424 nil t (cons riece-current-channel 0)))
426 (if (and current-prefix-arg
427 (riece-channel-p target))
428 (setq message (read-string "Message: ")))
429 (list target message)))
430 (if (riece-identity-member target riece-current-channels)
431 (if (riece-channel-p target)
432 (riece-command-part-channel target message)
433 (riece-part-channel target)
434 (riece-redisplay-buffers))
435 (error "You are not talking with %s" target)))
437 (defun riece-command-change-nickname (nickname)
438 "Change your nickname to NICK."
439 (interactive "sEnter your nickname: ")
440 (riece-send-string (format "NICK %s\r\n" nickname)))
442 (defun riece-command-scroll-down (lines)
443 "Scroll LINES down dialogue buffer from command buffer."
445 (let ((other-window-scroll-buffer
446 (if riece-channel-buffer-mode
448 riece-dialogue-buffer)))
449 (when (get-buffer-window other-window-scroll-buffer)
451 (scroll-other-window-down lines)
453 (message "Beginning of buffer"))))))
455 (defun riece-command-scroll-up (lines)
456 "Scroll LINES up dialogue buffer from command buffer."
458 (let* ((other-window-scroll-buffer
459 (if riece-channel-buffer-mode
461 riece-dialogue-buffer)))
462 (when (get-buffer-window other-window-scroll-buffer)
464 (scroll-other-window lines)
466 (message "End of buffer"))))))
468 (defun riece-command-nick-scroll-down (lines)
469 "Scroll LINES down nick buffer from command buffer."
471 (let ((other-window-scroll-buffer riece-user-list-buffer))
472 (when (get-buffer-window other-window-scroll-buffer)
474 (scroll-other-window-down lines)
476 (message "Beginning of buffer"))))))
478 (defun riece-command-nick-scroll-up (lines)
479 "Scroll LINES up nick buffer from command buffer."
481 (let* ((other-window-scroll-buffer riece-user-list-buffer))
482 (when (get-buffer-window other-window-scroll-buffer)
484 (scroll-other-window lines)
486 (message "End of buffer"))))))
488 (defun riece-command-toggle-away (&optional message)
489 "Mark yourself as being away."
491 (if current-prefix-arg
492 (let ((message (read-string "Away message: ")))
495 (riece-send-string (format "AWAY :%s\r\n" message))
496 (riece-send-string "AWAY\r\n")))
498 (defun riece-command-toggle-freeze (&optional arg)
499 "Prevent automatic scrolling of the dialogue window.
500 If prefix argument ARG is non-nil, toggle frozen status."
502 (riece-freeze (if riece-channel-buffer-mode
504 riece-dialogue-buffer)
505 (if arg (prefix-numeric-value arg))))
507 (defun riece-command-toggle-own-freeze (&optional arg)
508 "Prevent automatic scrolling of the dialogue window.
509 The difference from `riece-command-freeze' is that your messages are hidden.
510 If prefix argument ARG is non-nil, toggle frozen status."
512 (riece-own-freeze (if riece-channel-buffer-mode
514 riece-dialogue-buffer)
515 (if arg (prefix-numeric-value arg))))
517 (defun riece-command-quit (&optional arg)
520 (if (y-or-n-p "Really quit IRC? ")
523 (read-string "Message: ")
524 (or riece-quit-message
525 (riece-extended-version)))))
526 (riece-close-all-server message))))
528 (defun riece-command-raw (command)
529 "Enter raw IRC command, which is sent to the server."
530 (interactive "sIRC command: ")
531 (riece-send-string (concat command "\r\n")))
533 (defun riece-command-end-of-buffer ()
534 "Get end of the dialogue buffer."
537 (setq buffer (if riece-channel-buffer-mode
539 riece-dialogue-buffer))
540 (or (setq window (get-buffer-window buffer))
541 (setq window (get-buffer-window riece-dialogue-buffer)
542 buffer riece-dialogue-buffer))
544 (save-selected-window
545 (select-window window)
546 (goto-char (point-max))))))
548 (defun riece-command-copy-region (start end)
549 "Move current region between START and END to `kill-ring'."
551 (kill-new (buffer-substring-no-properties start end)))
553 (defun riece-command-open-server (server-name)
555 (list (completing-read "Server: " riece-server-alist)))
556 (let ((process (riece-start-server
557 (riece-server-name-to-server server-name)
559 (with-current-buffer (process-buffer process)
560 (setq riece-server-name server-name))
561 (setq riece-server-process-alist
562 (cons (cons server-name process)
563 riece-server-process-alist))))
565 (defun riece-command-close-server (server-name &optional message)
567 (list (completing-read "Server: " riece-server-process-alist)
568 (if current-prefix-arg
569 (read-string "Message: ")
570 (or riece-quit-message
571 (riece-extended-version)))))
572 (riece-close-server server-name message))
574 (defun riece-command-universal-server-name-argument ()
576 (let* ((riece-overriding-server-name
577 (completing-read "Server: "
578 riece-server-process-alist))
580 (key-binding (read-key-sequence
581 (format "Command to execute on \"%s\":"
582 riece-overriding-server-name)))))
584 (call-interactively command)))
586 (provide 'riece-commands)
588 ;;; riece-commands.el ends here