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-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-channel-get-topic
150 riece-current-channel)
153 (riece-send-string (format "TOPIC %s :%s\r\n"
154 (riece-identity-prefix riece-current-channel)
157 (defun riece-command-invite (user)
159 (let ((completion-ignore-case t))
160 (unless (and riece-current-channel
161 (riece-channel-p riece-current-channel))
162 (error "Not on a channel"))
163 (list (completing-read
165 (mapcar #'list (riece-get-users-on-server))))))
166 (riece-send-string (format "INVITE %s %s\r\n"
167 user (riece-identity-prefix
168 riece-current-channel))))
170 (defun riece-command-kick (user &optional message)
172 (let ((completion-ignore-case t))
173 (unless (and riece-current-channel
174 (riece-channel-p riece-current-channel))
175 (error "Not on a channel"))
176 (list (completing-read
178 (mapcar #'list (riece-channel-get-users
179 riece-current-channel)))
180 (if current-prefix-arg
181 (read-string "Message: ")))))
184 (format "KICK %s %s :%s\r\n"
185 (riece-identity-prefix riece-current-channel)
187 (format "KICK %s %s\r\n"
188 (riece-identity-prefix riece-current-channel)
191 (defun riece-command-names (pattern)
193 (let ((completion-ignore-case t))
194 (list (read-from-minibuffer
196 (if (and riece-current-channel
197 (riece-channel-p riece-current-channel))
198 (cons (riece-identity-prefix riece-current-channel)
200 (if (or (not (equal pattern ""))
201 (yes-or-no-p "Really want to query NAMES without argument? "))
202 (riece-send-string (format "NAMES %s\r\n" pattern))))
204 (defun riece-command-who (pattern)
206 (let ((completion-ignore-case t))
207 (list (read-from-minibuffer
209 (if (and riece-current-channel
210 (riece-channel-p riece-current-channel))
211 (cons (riece-identity-prefix riece-current-channel)
213 (if (or (not (equal pattern ""))
214 (yes-or-no-p "Really want to query WHO without argument? "))
215 (riece-send-string (format "WHO %s\r\n" pattern))))
217 (defun riece-command-list (pattern)
219 (let ((completion-ignore-case t))
220 (list (read-from-minibuffer
222 (if (and riece-current-channel
223 (riece-channel-p riece-current-channel))
224 (cons (riece-identity-prefix riece-current-channel)
226 (if (or (not (equal pattern ""))
227 (yes-or-no-p "Really want to query LIST without argument? "))
228 (riece-send-string (format "LIST %s\r\n" pattern))))
230 (defun riece-command-change-mode (channel change)
232 (let* ((completion-ignore-case t)
234 (if current-prefix-arg
237 (mapcar #'list riece-current-channels))
238 riece-current-channel))
239 (riece-overriding-server-name (riece-identity-server channel))
240 (riece-temp-minibuffer-message
241 (concat "[Available modes: "
242 (riece-with-server-buffer
243 (if (and (riece-channel-p channel)
244 riece-supported-channel-modes)
245 (apply #'string riece-supported-channel-modes)
246 (if (and (not (riece-channel-p channel))
247 riece-supported-user-modes)
248 (apply #'string riece-supported-user-modes))))
251 (read-from-minibuffer
252 (concat (riece-concat-modes channel "Mode (? for help)") ": ")
253 nil riece-minibuffer-map))))
254 (riece-send-string (format "MODE %s :%s\r\n" channel change)))
256 (defun riece-command-set-operators (users &optional arg)
258 (let ((operators (riece-channel-get-operators riece-current-channel))
259 (completion-ignore-case t)
261 (if current-prefix-arg
262 (setq users (riece-completing-read-multiple
264 (mapcar #'list operators)))
265 (setq users (riece-completing-read-multiple
267 (delq nil (mapcar (lambda (user)
268 (unless (member user operators)
270 (riece-channel-get-users
271 riece-current-channel))))))
272 (list users current-prefix-arg)))
275 (setq group (cons (car users) group)
277 (if (or (= (length group) 3)
280 (format "MODE %s %c%s %s\r\n"
281 (riece-identity-prefix riece-current-channel)
282 (if current-prefix-arg
285 (make-string (length group) ?o)
286 (mapconcat #'identity group " ")))))))
288 (defun riece-command-set-speakers (users &optional arg)
290 (let ((speakers (riece-channel-get-speakers riece-current-channel))
291 (completion-ignore-case t)
293 (if current-prefix-arg
294 (setq users (riece-completing-read-multiple
296 (mapcar #'list speakers)))
297 (setq users (riece-completing-read-multiple
299 (delq nil (mapcar (lambda (user)
300 (unless (member user speakers)
302 (riece-channel-get-users
303 riece-current-channel))))))
304 (list users current-prefix-arg)))
307 (setq group (cons (car users) group)
309 (if (or (= (length group) 3)
312 (format "MODE %s %c%s %s\r\n"
313 (riece-identity-prefix riece-current-channel)
314 (if current-prefix-arg
317 (make-string (length group) ?v)
318 (mapconcat #'identity group " ")))))))
320 (defun riece-command-send-message (message notice)
321 "Send MESSAGE to the current channel."
322 (if (equal message "")
323 (error "No text to send"))
324 (unless riece-current-channel
325 (error (substitute-command-keys
326 "Type \\[riece-command-join] to join a channel")))
330 (format "NOTICE %s :%s\r\n"
331 (riece-identity-prefix riece-current-channel)
333 (riece-own-channel-message message riece-current-channel 'notice))
335 (format "PRIVMSG %s :%s\r\n"
336 (riece-identity-prefix riece-current-channel)
338 (riece-own-channel-message message)))
340 (defun riece-command-enter-message ()
341 "Send the current line to the current channel."
343 (riece-command-send-message (buffer-substring
344 (riece-line-beginning-position)
345 (riece-line-end-position))
347 (let ((next-line-add-newlines t))
350 (defun riece-command-enter-message-as-notice ()
351 "Send the current line to the current channel as NOTICE."
353 (riece-command-send-message (buffer-substring
354 (riece-line-beginning-position)
355 (riece-line-end-position))
357 (let ((next-line-add-newlines t))
360 (defun riece-command-join-channel (target key)
361 (let ((server-name (riece-identity-server target))
364 (setq process (cdr (assoc server-name riece-server-process-alist)))
365 (setq process riece-server-process))
367 (error "%s" (substitute-command-keys
368 "Type \\[riece-command-open-server] to open server.")))
369 (riece-process-send-string process
371 (format "JOIN %s :%s\r\n"
372 (riece-identity-prefix target)
374 (format "JOIN %s\r\n"
375 (riece-identity-prefix target))))))
377 (defun riece-command-join-partner (target)
378 (let ((pointer (riece-identity-member target riece-current-channels)))
380 (riece-command-switch-to-channel (car pointer))
381 (riece-join-channel target)
382 (riece-switch-to-channel target)
383 (riece-redisplay-buffers))))
385 (defun riece-command-join (target &optional key)
387 (let ((completion-ignore-case t)
389 (completing-read "Channel/User: "
390 (mapcar #'list riece-current-channels)))
392 (if (and current-prefix-arg
393 (riece-channel-p target))
395 (riece-read-passwd (format "Key for %s: " target))))
397 (let ((pointer (riece-identity-member target riece-current-channels)))
399 (riece-command-switch-to-channel (car pointer))
400 (if (riece-channel-p target)
401 (riece-command-join-channel target key)
402 (riece-command-join-partner target)))))
404 (defun riece-command-part-channel (target message)
405 (let ((server-name (riece-identity-server target))
408 (setq process (cdr (assoc server-name riece-server-process-alist)))
409 (setq process riece-server-process))
411 (error "%s" (substitute-command-keys
412 "Type \\[riece-command-open-server] to open server.")))
413 (riece-process-send-string process
415 (format "PART %s :%s\r\n"
416 (riece-identity-prefix target)
418 (format "PART %s\r\n"
419 (riece-identity-prefix target))))))
421 (defun riece-command-part (target &optional message)
423 (let ((completion-ignore-case t)
425 (completing-read "Channel/User: "
426 (mapcar #'list riece-current-channels)
427 nil t (cons riece-current-channel 0)))
429 (if (and current-prefix-arg
430 (riece-channel-p target))
431 (setq message (read-string "Message: ")))
432 (list target message)))
433 (if (riece-identity-member target riece-current-channels)
434 (if (riece-channel-p target)
435 (riece-command-part-channel target message)
436 (riece-part-channel target)
437 (riece-redisplay-buffers))
438 (error "You are not talking with %s" target)))
440 (defun riece-command-change-nickname (nickname)
441 "Change your nickname to NICK."
442 (interactive "sEnter your nickname: ")
443 (riece-send-string (format "NICK %s\r\n" nickname)))
445 (defun riece-command-scroll-down (lines)
446 "Scroll LINES down dialogue buffer from command buffer."
448 (let ((other-window-scroll-buffer
449 (if riece-channel-buffer-mode
451 riece-dialogue-buffer)))
452 (when (get-buffer-window other-window-scroll-buffer)
454 (scroll-other-window-down lines)
456 (message "Beginning of buffer"))))))
458 (defun riece-command-scroll-up (lines)
459 "Scroll LINES up dialogue buffer from command buffer."
461 (let* ((other-window-scroll-buffer
462 (if riece-channel-buffer-mode
464 riece-dialogue-buffer)))
465 (when (get-buffer-window other-window-scroll-buffer)
467 (scroll-other-window lines)
469 (message "End of buffer"))))))
471 (defun riece-command-nick-scroll-down (lines)
472 "Scroll LINES down nick buffer from command buffer."
474 (let ((other-window-scroll-buffer riece-user-list-buffer))
475 (when (get-buffer-window other-window-scroll-buffer)
477 (scroll-other-window-down lines)
479 (message "Beginning of buffer"))))))
481 (defun riece-command-nick-scroll-up (lines)
482 "Scroll LINES up nick buffer from command buffer."
484 (let* ((other-window-scroll-buffer riece-user-list-buffer))
485 (when (get-buffer-window other-window-scroll-buffer)
487 (scroll-other-window lines)
489 (message "End of buffer"))))))
491 (defun riece-command-toggle-away (&optional message)
492 "Mark yourself as being away."
494 (if current-prefix-arg
495 (let ((message (read-string "Away message: ")))
498 (riece-send-string (format "AWAY :%s\r\n" message))
499 (riece-send-string "AWAY\r\n")))
501 (defun riece-command-toggle-freeze (&optional arg)
502 "Prevent automatic scrolling of the dialogue window.
503 If prefix argument ARG is non-nil, toggle frozen status."
505 (riece-freeze (if riece-channel-buffer-mode
507 riece-dialogue-buffer)
508 (if arg (prefix-numeric-value arg))))
510 (defun riece-command-toggle-own-freeze (&optional arg)
511 "Prevent automatic scrolling of the dialogue window.
512 The difference from `riece-command-freeze' is that your messages are hidden.
513 If prefix argument ARG is non-nil, toggle frozen status."
515 (riece-own-freeze (if riece-channel-buffer-mode
517 riece-dialogue-buffer)
518 (if arg (prefix-numeric-value arg))))
520 (defun riece-command-quit (&optional arg)
523 (if (y-or-n-p "Really quit IRC? ")
526 (read-string "Message: ")
527 (or riece-quit-message
528 (riece-extended-version)))))
529 (riece-close-all-server message))))
531 (defun riece-command-raw (command)
532 "Enter raw IRC command, which is sent to the server."
533 (interactive "sIRC command: ")
534 (riece-send-string (concat command "\r\n")))
536 (defun riece-command-end-of-buffer ()
537 "Get end of the dialogue buffer."
540 (setq buffer (if riece-channel-buffer-mode
542 riece-dialogue-buffer))
543 (or (setq window (get-buffer-window buffer))
544 (setq window (get-buffer-window riece-dialogue-buffer)
545 buffer riece-dialogue-buffer))
547 (save-selected-window
548 (select-window window)
549 (goto-char (point-max))))))
551 (defun riece-command-copy-region (start end)
552 "Move current region between START and END to `kill-ring'."
554 (kill-new (buffer-substring-no-properties start end)))
556 (defun riece-command-open-server (server-name)
558 (list (completing-read "Server: " riece-server-alist)))
560 (riece-server-name-to-server server-name)
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