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 (setq pointer riece-current-channels)
86 (setq pointer (cdr pointer))))
88 (riece-command-switch-to-channel (car pointer))
89 (error "No such channel!")))))
91 (defun riece-command-previous-channel ()
92 "Select the previous channel."
94 (when (> (length riece-current-channels) 1)
95 (let ((pointer (string-list-member-ignore-case
97 riece-current-channels))
98 (start riece-current-channels)
100 (while (and start (not (eq start pointer)))
101 (setq channel (car start))
102 (setq start (cdr start)))
104 (setq start (copy-sequence riece-current-channels))
105 (setq start (delq nil start))
106 (and (> (length start) 1)
107 (setq channel (nth (1- (length start)) start))))
109 (riece-command-switch-to-channel channel)
110 (error "No such channel!")))))
112 (defun riece-command-select-command-buffer ()
113 "Select the command buffer."
115 (let ((window (get-buffer-window riece-command-buffer)))
117 (select-window window))))
119 (defun riece-command-configure-windows ()
121 (riece-redisplay-buffers t))
123 (defun riece-command-toggle-channel-buffer-mode ()
125 (setq riece-channel-buffer-mode
126 (not riece-channel-buffer-mode)
127 riece-save-variables-are-dirty t)
128 (riece-command-configure-windows))
130 (defun riece-command-toggle-user-list-buffer-mode ()
132 (setq riece-user-list-buffer-mode
133 (not riece-user-list-buffer-mode)
134 riece-save-variables-are-dirty t)
135 (riece-command-configure-windows))
137 (defun riece-command-toggle-channel-list-buffer-mode ()
139 (setq riece-channel-list-buffer-mode
140 (not riece-channel-list-buffer-mode)
141 riece-save-variables-are-dirty t)
142 (riece-command-configure-windows))
144 (defun riece-command-finger (user &optional recurse)
146 (let* ((completion-ignore-case t)
147 (user (completing-read
149 (mapcar #'list (riece-get-users-on-server)))))
150 (list user current-prefix-arg)))
152 (riece-send-string (format "WHOIS %s %s\r\n" user user))
153 (riece-send-string (format "WHOIS %s\r\n" user))))
155 (defun riece-command-topic (topic)
157 (list (read-from-minibuffer
158 "Topic: " (cons (or (riece-channel-get-topic
159 riece-current-channel)
162 (riece-send-string (format "TOPIC %s :%s\r\n"
163 (riece-identity-prefix riece-current-channel)
166 (defun riece-command-invite (user)
168 (let ((completion-ignore-case t))
169 (unless (and riece-current-channel
170 (riece-channel-p riece-current-channel))
171 (error "Not on a channel"))
172 (list (completing-read
174 (mapcar #'list (riece-get-users-on-server))))))
175 (riece-send-string (format "INVITE %s %s\r\n"
176 user (riece-identity-prefix
177 riece-current-channel))))
179 (defun riece-command-kick (user &optional message)
181 (let ((completion-ignore-case t))
182 (unless (and riece-current-channel
183 (riece-channel-p 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-current-channel))
207 (cons (riece-identity-prefix riece-current-channel)
209 (if (or (not (equal pattern ""))
210 (yes-or-no-p "Really want to query NAMES without argument? "))
211 (riece-send-string (format "NAMES %s\r\n" pattern))))
213 (defun riece-command-who (pattern)
215 (let ((completion-ignore-case t))
216 (list (read-from-minibuffer
218 (if (and riece-current-channel
219 (riece-channel-p riece-current-channel))
220 (cons (riece-identity-prefix riece-current-channel)
222 (if (or (not (equal pattern ""))
223 (yes-or-no-p "Really want to query WHO without argument? "))
224 (riece-send-string (format "WHO %s\r\n" pattern))))
226 (defun riece-command-list (pattern)
228 (let ((completion-ignore-case t))
229 (list (read-from-minibuffer
231 (if (and riece-current-channel
232 (riece-channel-p riece-current-channel))
233 (cons (riece-identity-prefix riece-current-channel)
235 (if (or (not (equal pattern ""))
236 (yes-or-no-p "Really want to query LIST without argument? "))
237 (riece-send-string (format "LIST %s\r\n" pattern))))
239 (defun riece-command-change-mode (channel change)
241 (let* ((completion-ignore-case t)
243 (if current-prefix-arg
246 (mapcar #'list riece-current-channels))
247 riece-current-channel))
248 (riece-overriding-server-name (riece-identity-server channel))
249 (riece-temp-minibuffer-message
250 (concat "[Available modes: "
251 (riece-with-server-buffer
252 (if (and (riece-channel-p channel)
253 riece-supported-channel-modes)
254 (apply #'string riece-supported-channel-modes)
255 (if (and (not (riece-channel-p channel))
256 riece-supported-user-modes)
257 (apply #'string riece-supported-user-modes))))
260 (read-from-minibuffer
261 (concat (riece-concat-modes channel "Mode (? for help)") ": ")
262 nil riece-minibuffer-map))))
263 (riece-send-string (format "MODE %s :%s\r\n" channel change)))
265 (defun riece-command-set-operators (users &optional arg)
267 (let ((operators (riece-channel-get-operators riece-current-channel))
268 (completion-ignore-case t)
270 (if current-prefix-arg
271 (setq users (riece-completing-read-multiple
273 (mapcar #'list operators)))
274 (setq users (riece-completing-read-multiple
276 (delq nil (mapcar (lambda (user)
277 (unless (member user operators)
279 (riece-channel-get-users
280 riece-current-channel))))))
281 (list users current-prefix-arg)))
284 (setq group (cons (car users) group)
286 (if (or (= (length group) 3)
289 (format "MODE %s %c%s %s\r\n"
290 (riece-identity-prefix riece-current-channel)
291 (if current-prefix-arg
294 (make-string (length group) ?o)
295 (mapconcat #'identity group " ")))))))
297 (defun riece-command-set-speakers (users &optional arg)
299 (let ((speakers (riece-channel-get-speakers riece-current-channel))
300 (completion-ignore-case t)
302 (if current-prefix-arg
303 (setq users (riece-completing-read-multiple
305 (mapcar #'list speakers)))
306 (setq users (riece-completing-read-multiple
308 (delq nil (mapcar (lambda (user)
309 (unless (member user speakers)
311 (riece-channel-get-users
312 riece-current-channel))))))
313 (list users current-prefix-arg)))
316 (setq group (cons (car users) group)
318 (if (or (= (length group) 3)
321 (format "MODE %s %c%s %s\r\n"
322 (riece-identity-prefix riece-current-channel)
323 (if current-prefix-arg
326 (make-string (length group) ?v)
327 (mapconcat #'identity group " ")))))))
329 (defun riece-command-send-message (message notice)
330 "Send MESSAGE to the current channel."
331 (if (equal message "")
332 (error "No text to send"))
333 (unless riece-current-channel
334 (error (substitute-command-keys
335 "Type \\[riece-command-join] to join a channel")))
339 (format "NOTICE %s :%s\r\n"
340 (riece-identity-prefix riece-current-channel)
342 (riece-own-channel-message message riece-current-channel 'notice))
344 (format "PRIVMSG %s :%s\r\n"
345 (riece-identity-prefix riece-current-channel)
347 (riece-own-channel-message message)))
349 (defun riece-command-enter-message ()
350 "Send the current line to the current channel."
352 (riece-command-send-message (buffer-substring
353 (riece-line-beginning-position)
354 (riece-line-end-position))
356 (let ((next-line-add-newlines t))
359 (defun riece-command-enter-message-as-notice ()
360 "Send the current line to the current channel as NOTICE."
362 (riece-command-send-message (buffer-substring
363 (riece-line-beginning-position)
364 (riece-line-end-position))
366 (let ((next-line-add-newlines t))
369 (defun riece-command-join-channel (target key)
370 (let ((server-name (riece-identity-server target))
373 (setq process (cdr (assoc server-name riece-server-process-alist)))
374 (setq process riece-server-process))
376 (error "%s" (substitute-command-keys
377 "Type \\[riece-command-open-server] to open server.")))
378 (riece-process-send-string process
380 (format "JOIN %s :%s\r\n"
381 (riece-identity-prefix target)
383 (format "JOIN %s\r\n"
384 (riece-identity-prefix target))))))
386 (defun riece-command-join-partner (target)
387 (let ((pointer (riece-identity-member target riece-current-channels)))
389 (riece-command-switch-to-channel (car pointer))
390 (riece-join-channel target)
391 (riece-switch-to-channel target)
392 (riece-redisplay-buffers))))
394 (defun riece-command-join (target &optional key)
396 (let ((completion-ignore-case t)
398 (completing-read "Channel/User: "
399 (mapcar #'list riece-current-channels)))
401 (if (and current-prefix-arg
402 (riece-channel-p target))
404 (riece-read-passwd (format "Key for %s: " target))))
406 (let ((pointer (riece-identity-member target riece-current-channels)))
408 (riece-command-switch-to-channel (car pointer))
409 (if (riece-channel-p target)
410 (riece-command-join-channel target key)
411 (riece-command-join-partner target)))))
413 (defun riece-command-part-channel (target message)
414 (let ((server-name (riece-identity-server target))
417 (setq process (cdr (assoc server-name riece-server-process-alist)))
418 (setq process riece-server-process))
420 (error "%s" (substitute-command-keys
421 "Type \\[riece-command-open-server] to open server.")))
422 (riece-process-send-string process
424 (format "PART %s :%s\r\n"
425 (riece-identity-prefix target)
427 (format "PART %s\r\n"
428 (riece-identity-prefix target))))))
430 (defun riece-command-part (target &optional message)
432 (let ((completion-ignore-case t)
434 (completing-read "Channel/User: "
435 (mapcar #'list riece-current-channels)
436 nil t (cons riece-current-channel 0)))
438 (if (and current-prefix-arg
439 (riece-channel-p target))
440 (setq message (read-string "Message: ")))
441 (list target message)))
442 (if (riece-identity-member target riece-current-channels)
443 (if (riece-channel-p target)
444 (riece-command-part-channel target message)
445 (riece-part-channel target)
446 (riece-redisplay-buffers))
447 (error "You are not talking with %s" target)))
449 (defun riece-command-change-nickname (nickname)
450 "Change your nickname to NICK."
451 (interactive "sEnter your nickname: ")
452 (riece-send-string (format "NICK %s\r\n" nickname)))
454 (defun riece-command-scroll-down (lines)
455 "Scroll LINES down dialogue buffer from command buffer."
457 (let ((other-window-scroll-buffer
458 (if riece-channel-buffer-mode
460 riece-dialogue-buffer)))
461 (when (get-buffer-window other-window-scroll-buffer)
463 (scroll-other-window-down lines)
465 (message "Beginning of buffer"))))))
467 (defun riece-command-scroll-up (lines)
468 "Scroll LINES up 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 lines)
478 (message "End of buffer"))))))
480 (defun riece-command-nick-scroll-down (lines)
481 "Scroll LINES down nick buffer from command buffer."
483 (let ((other-window-scroll-buffer riece-user-list-buffer))
484 (when (get-buffer-window other-window-scroll-buffer)
486 (scroll-other-window-down lines)
488 (message "Beginning of buffer"))))))
490 (defun riece-command-nick-scroll-up (lines)
491 "Scroll LINES up nick buffer from command buffer."
493 (let* ((other-window-scroll-buffer riece-user-list-buffer))
494 (when (get-buffer-window other-window-scroll-buffer)
496 (scroll-other-window lines)
498 (message "End of buffer"))))))
500 (defun riece-command-toggle-away (&optional message)
501 "Mark yourself as being away."
503 (if (and (not (riece-user-get-away (riece-current-nickname)))
504 (or (null riece-away-message)
506 (let ((message (read-string "Away message: ")))
509 (riece-send-string (format "AWAY :%s\r\n" message))
510 (riece-send-string "AWAY\r\n")))
512 (defun riece-command-toggle-freeze (&optional arg)
513 "Prevent automatic scrolling of the dialogue window.
514 If prefix argument ARG is non-nil, toggle frozen status."
516 (with-current-buffer (if (and riece-channel-buffer-mode
517 riece-channel-buffer)
519 riece-dialogue-buffer)
520 (setq riece-freeze (if arg
521 (< 0 (prefix-numeric-value arg))
522 (not riece-freeze))))
523 (riece-update-status-indicators)
524 (force-mode-line-update t))
526 (defun riece-command-toggle-own-freeze (&optional arg)
527 "Prevent automatic scrolling of the dialogue window.
528 The difference from `riece-command-freeze' is that your messages are hidden.
529 If prefix argument ARG is non-nil, toggle frozen status."
531 (with-current-buffer (if (and riece-channel-buffer-mode
532 riece-channel-buffer)
534 riece-dialogue-buffer)
536 (< 0 (prefix-numeric-value arg))
537 (not (eq riece-freeze 'own)))
538 (setq riece-freeze 'own)
539 (setq riece-freeze nil)))
540 (riece-update-status-indicators)
541 (force-mode-line-update t))
543 (defun riece-command-quit (&optional arg)
546 (if (y-or-n-p "Really quit IRC? ")
549 (read-string "Message: ")
550 (or riece-quit-message
551 (riece-extended-version)))))
552 (riece-close-all-server message))))
554 (defun riece-command-raw (command)
555 "Enter raw IRC command, which is sent to the server."
556 (interactive "sIRC command: ")
557 (riece-send-string (concat command "\r\n")))
559 (defun riece-command-end-of-buffer ()
560 "Get end of the dialogue buffer."
563 (setq buffer (if riece-channel-buffer-mode
565 riece-dialogue-buffer))
566 (or (setq window (get-buffer-window buffer))
567 (setq window (get-buffer-window riece-dialogue-buffer)
568 buffer riece-dialogue-buffer))
570 (save-selected-window
571 (select-window window)
572 (goto-char (point-max))))))
574 (defun riece-command-copy-region (start end)
575 "Move current region between START and END to `kill-ring'."
577 (kill-new (buffer-substring-no-properties start end)))
579 (defun riece-command-open-server (server-name)
581 (list (completing-read "Server: " riece-server-alist)))
582 (if (assoc server-name riece-server-process-alist)
583 (error "%s is already opened" server-name)
585 (riece-server-name-to-server server-name)
588 (defun riece-command-close-server (server-name &optional message)
590 (list (completing-read "Server: " riece-server-process-alist)
591 (if current-prefix-arg
592 (read-string "Message: ")
593 (or riece-quit-message
594 (riece-extended-version)))))
595 (riece-close-server server-name message))
597 (defun riece-command-universal-server-name-argument ()
599 (let* ((riece-overriding-server-name
600 (completing-read "Server: "
601 riece-server-process-alist))
603 (key-binding (read-key-sequence
604 (format "Command to execute on \"%s\":"
605 riece-overriding-server-name)))))
607 (call-interactively command)))
609 (provide 'riece-commands)
611 ;;; riece-commands.el ends here