214ac1c2a7e1c8e676bd8a9eee52da4238d49bc3
[riece] / lisp / riece-commands.el
1 ;;; riece-commands.el --- commands available in command buffer
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Code:
26
27 (require 'riece-channel)
28 (require 'riece-complete)
29 (require 'riece-layout)
30 (require 'riece-display)
31 (require 'riece-server)
32 (require 'riece-misc)
33 (require 'riece-identity)
34 (require 'riece-message)
35
36 ;;; Channel movement:
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-channel)
41     (riece-switch-to-channel channel)))
42
43 (defun riece-command-switch-to-channel-by-number (number)
44   (interactive
45    (let ((command-name (symbol-name this-command)))
46      (if (string-match "[0-9]+$" command-name)
47          (list (string-to-number (match-string 0 command-name)))
48        (list (string-to-number (read-string "Number: "))))))
49   (let ((channel (nth (1- number) riece-current-channels)))
50     (if channel
51         (riece-command-switch-to-channel channel)
52       (error "No such number!"))))
53
54 (eval-and-compile
55   (let ((number 1))
56     (while (<= number 20)
57       (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
58                                 (number-to-string number)))
59         'riece-command-switch-to-channel-by-number)
60       (setq number (1+ number)))))
61
62 (defun riece-command-next-channel ()
63   "Select the next channel."
64   (interactive)
65   (when (> (length riece-current-channels) 1)
66     (let ((pointer (cdr (riece-identity-member
67                          riece-current-channel
68                          riece-current-channels))))
69       (while (and pointer
70                   (null (car pointer)))
71         (setq pointer (cdr pointer)))
72       (when (null pointer)
73         (setq pointer riece-current-channels)
74         (while (and pointer
75                     (null (car pointer)))
76           (setq pointer (cdr pointer))))
77       (if (car pointer)
78           (riece-command-switch-to-channel (car pointer))
79         (error "No such channel!")))))
80
81 (defun riece-command-previous-channel ()
82   "Select the previous channel."
83   (interactive)
84   (when (> (length riece-current-channels) 1)
85     (let ((pointer (riece-identity-member
86                     riece-current-channel
87                     riece-current-channels))
88           (start riece-current-channels)
89           channel)
90       (while (and start (not (eq start pointer)))
91         (if (car start)
92             (setq channel (car start)))
93         (setq start (cdr start)))
94       (when (null channel)
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))))
99       (if channel
100           (riece-command-switch-to-channel channel)
101         (error "No such channel!")))))
102
103 (defun riece-command-select-command-buffer ()
104   "Select the command buffer."
105   (interactive)
106   (let ((window (get-buffer-window riece-command-buffer)))
107     (if window
108         (select-window window))))
109
110 (defun riece-command-configure-windows ()
111   (interactive)
112   (riece-redisplay-buffers t))
113
114 (defun riece-command-change-layout (name)
115   "Select a layout-name from all current available layouts and change
116 the layout to the selected layout-name."
117   (interactive (list (completing-read "Layout: " riece-layout-alist)))
118   (setq riece-layout name
119         riece-save-variables-are-dirty t)
120   (riece-command-configure-windows))
121
122 (defun riece-command-toggle-channel-buffer-mode ()
123   (interactive)
124   (setq riece-channel-buffer-mode
125         (not riece-channel-buffer-mode)
126         riece-save-variables-are-dirty t)
127   (riece-command-configure-windows))
128
129 (defun riece-command-toggle-user-list-buffer-mode ()
130   (interactive)
131   (setq riece-user-list-buffer-mode
132         (not riece-user-list-buffer-mode)
133         riece-save-variables-are-dirty t)
134   (riece-command-configure-windows))
135
136 (defun riece-command-toggle-channel-list-buffer-mode ()
137   (interactive)
138   (setq riece-channel-list-buffer-mode
139         (not riece-channel-list-buffer-mode)
140         riece-save-variables-are-dirty t)
141   (riece-command-configure-windows))
142
143 (defun riece-command-finger (user &optional recurse)
144   (interactive
145    (let* ((completion-ignore-case t)
146           (user (riece-completing-read-identity
147                  "User: "
148                  (riece-get-users-on-server (riece-current-server-name)))))
149      (list user current-prefix-arg)))
150   (if recurse
151       (riece-send-string (format "WHOIS %s %s\r\n"
152                                  (riece-identity-prefix user)
153                                  (riece-identity-prefix user)))
154     (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))
155
156 (defun riece-command-topic (topic)
157   (interactive
158    (progn
159      (riece-check-channel-commands-are-usable t)
160      (list (read-from-minibuffer
161             "Topic: " (cons (or (riece-with-server-buffer
162                                     (riece-identity-server
163                                      riece-current-channel)
164                                   (riece-channel-get-topic
165                                    (riece-identity-prefix
166                                     riece-current-channel)))
167                                 "")
168                             0)))))
169   (riece-send-string (format "TOPIC %s :%s\r\n"
170                              (riece-identity-prefix riece-current-channel)
171                              topic)))
172
173 (defun riece-command-invite (user)
174   (interactive
175    (let ((completion-ignore-case t))
176      (riece-check-channel-commands-are-usable t)
177      (list (riece-completing-read-identity
178             "User: "
179             (riece-get-users-on-server (riece-current-server-name))))))
180   (riece-send-string (format "INVITE %s %s\r\n"
181                              (riece-identity-prefix user)
182                              (riece-identity-prefix riece-current-channel))))
183
184 (defun riece-command-kick (user &optional message)
185   (interactive
186    (let ((completion-ignore-case t))
187      (riece-check-channel-commands-are-usable t)
188      (list (completing-read
189             "User: "
190             (riece-with-server-buffer
191                 (riece-identity-server riece-current-channel)
192               (riece-channel-get-users (riece-identity-prefix
193                                         riece-current-channel))))
194            (if current-prefix-arg
195                (read-string "Message: ")))))
196   (riece-send-string
197    (if message
198        (format "KICK %s %s :%s\r\n"
199                (riece-identity-prefix riece-current-channel)
200                user message)
201      (format "KICK %s %s\r\n"
202              (riece-identity-prefix riece-current-channel)
203              user))))
204
205 (defun riece-command-names (pattern)
206   (interactive
207    (let ((completion-ignore-case t))
208      (list (read-from-minibuffer
209             "Pattern: "
210             (if (and riece-current-channel
211                      (riece-channel-p (riece-identity-prefix
212                                        riece-current-channel)))
213                 (cons (riece-identity-prefix riece-current-channel)
214                       0))))))
215   (if (or (not (equal pattern ""))
216           (yes-or-no-p "Really want to query NAMES without argument? "))
217       (riece-send-string (format "NAMES %s\r\n" pattern))))
218
219 (defun riece-command-who (pattern)
220   (interactive
221    (let ((completion-ignore-case t))
222      (list (read-from-minibuffer
223             "Pattern: "
224             (if (and riece-current-channel
225                      (riece-channel-p (riece-identity-prefix
226                                        riece-current-channel)))
227                 (cons (riece-identity-prefix riece-current-channel)
228                       0))))))
229   (if (or (not (equal pattern ""))
230           (yes-or-no-p "Really want to query WHO without argument? "))
231       (riece-send-string (format "WHO %s\r\n" pattern))))
232
233 (defun riece-command-list (pattern)
234   (interactive
235    (let ((completion-ignore-case t))
236      (list (read-from-minibuffer
237             "Pattern: "
238             (if (and riece-current-channel
239                      (riece-channel-p (riece-identity-prefix
240                                        riece-current-channel)))
241                 (cons (riece-identity-prefix riece-current-channel)
242                       0))))))
243   (if (or (not (equal pattern ""))
244           (yes-or-no-p "Really want to query LIST without argument? "))
245       (riece-send-string (format "LIST %s\r\n" pattern))))
246
247 (defun riece-command-change-mode (channel change)
248   (interactive
249    (let* ((completion-ignore-case t)
250           (channel
251            (if current-prefix-arg
252                (riece-completing-read-identity
253                 "Channel/User: "
254                 (riece-get-identities-on-server (riece-current-server-name)))
255              (riece-check-channel-commands-are-usable t)
256              riece-current-channel))
257           (riece-overriding-server-name (riece-identity-server channel))
258           (riece-temp-minibuffer-message
259            (concat "[Available modes: "
260                    (riece-with-server-buffer (riece-identity-server channel)
261                      (if (riece-channel-p (riece-identity-prefix channel))
262                          (if riece-supported-channel-modes
263                              (apply #'string riece-supported-channel-modes))
264                        (if riece-supported-user-modes
265                            (apply #'string riece-supported-user-modes))))
266                    "]")))
267      (list channel
268            (read-from-minibuffer
269             (concat (riece-concat-channel-modes
270                      channel "Mode (? for help)") ": ")
271             nil riece-minibuffer-map))))
272   (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
273                              change)))
274
275 (defun riece-command-set-operators (users &optional arg)
276   (interactive
277    (progn
278      (riece-check-channel-commands-are-usable t)
279      (let ((completion-ignore-case t))
280        (list (riece-completing-read-multiple
281               "Users"
282               (riece-with-server-buffer
283                   (riece-identity-server riece-current-channel)
284                 (riece-channel-get-users (riece-identity-prefix
285                                          riece-current-channel)))
286               (if current-prefix-arg
287                   (lambda (user)
288                     (memq ?o (cdr user)))
289                 (lambda (user)
290                   (not (memq ?o (cdr user))))))
291              current-prefix-arg))))
292   (let (group)
293     (while users
294       (setq group (cons (car users) group)
295             users (cdr users))
296       (when (or (= (length group) 3)
297                 (null users))
298         (riece-send-string
299          (format "MODE %s %c%s %s\r\n"
300                  (riece-identity-prefix riece-current-channel)
301                  (if current-prefix-arg
302                      ?-
303                    ?+)
304                  (make-string (length group) ?o)
305                  (mapconcat #'identity (nreverse group) " ")))
306         (setq group nil)))))
307
308 (defun riece-command-set-speakers (users &optional arg)
309   (interactive
310    (progn
311      (riece-check-channel-commands-are-usable t)
312      (let ((completion-ignore-case t))
313        (list (riece-completing-read-multiple
314               "Users"
315               (riece-with-server-buffer
316                   (riece-identity-server riece-current-channel)
317                 (riece-channel-get-users (riece-identity-prefix
318                                           riece-current-channel)))
319               (if current-prefix-arg
320                   (lambda (user)
321                     (memq ?v (cdr user)))
322                 (lambda (user)
323                   (not (memq ?v (cdr user))))))
324              current-prefix-arg))))
325   (let (group)
326     (while users
327       (setq group (cons (car users) group)
328             users (cdr users))
329       (when (or (= (length group) 3)
330                 (null users))
331         (riece-send-string
332          (format "MODE %s %c%s %s\r\n"
333                  (riece-identity-prefix riece-current-channel)
334                  (if current-prefix-arg
335                      ?-
336                    ?+)
337                  (make-string (length group) ?v)
338                  (mapconcat #'identity (nreverse group) " ")))
339         (setq group nil)))))
340
341 (defun riece-command-send-message (message notice)
342   "Send MESSAGE to the current channel."
343   (if (equal message "")
344       (error "No text to send"))
345   (riece-check-channel-commands-are-usable)
346   (if notice
347       (progn
348         (riece-send-string
349          (format "NOTICE %s :%s\r\n"
350                  (riece-identity-prefix riece-current-channel)
351                  message))
352         (riece-display-message
353          (riece-make-message (riece-current-nickname) riece-current-channel
354                              message 'notice t)))
355     (riece-send-string
356      (format "PRIVMSG %s :%s\r\n"
357              (riece-identity-prefix riece-current-channel)
358              message))
359     (riece-display-message
360      (riece-make-message (riece-current-nickname) riece-current-channel
361                          message nil t))))
362
363 (defun riece-command-enter-message ()
364   "Send the current line to the current channel."
365   (interactive)
366   (riece-command-send-message (buffer-substring
367                                (riece-line-beginning-position)
368                                (riece-line-end-position))
369                               nil)
370   (let ((next-line-add-newlines t))
371     (next-line 1)))
372
373 (defun riece-command-enter-message-as-notice ()
374   "Send the current line to the current channel as NOTICE."
375   (interactive)
376   (riece-command-send-message (buffer-substring
377                                (riece-line-beginning-position)
378                                (riece-line-end-position))
379                               t)
380   (let ((next-line-add-newlines t))
381     (next-line 1)))
382
383 (defun riece-command-enter-message-to-user (user)
384   "Send the current line to USER."
385   (interactive
386    (let ((completion-ignore-case t))
387      (list (riece-completing-read-identity
388             "User: "
389             (riece-get-users-on-server (riece-current-server-name))))))
390   (let ((text (buffer-substring
391                (riece-line-beginning-position)
392                (riece-line-end-position))))
393     (riece-send-string
394      (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
395     (riece-display-message
396      (riece-make-message (riece-current-nickname) user text nil t)))
397   (let ((next-line-add-newlines t))
398     (next-line 1)))
399
400 (defun riece-command-join-channel (target key)
401   (let ((process (riece-server-process (riece-identity-server target))))
402     (unless process
403       (error "%s" (substitute-command-keys
404                    "Type \\[riece-command-open-server] to open server.")))
405     (riece-process-send-string process
406                                (if key
407                                    (format "JOIN %s :%s\r\n"
408                                            (riece-identity-prefix target)
409                                            key)
410                                  (format "JOIN %s\r\n"
411                                          (riece-identity-prefix target))))))
412
413 (defun riece-command-join-partner (target)
414   (let ((pointer (riece-identity-member target riece-current-channels)))
415     (if pointer
416         (riece-command-switch-to-channel (car pointer))
417       (riece-join-channel target)
418       (riece-switch-to-channel target))))
419
420 (defun riece-command-join (target &optional key)
421   (interactive
422    (let* ((completion-ignore-case t)
423           (target
424            (if riece-join-channel-candidate
425                (let ((default (riece-format-identity
426                                riece-join-channel-candidate)))
427                  (riece-completing-read-identity
428                   (format "Channel/User (default %s): " default)
429                   (riece-get-identities-on-server (riece-current-server-name))
430                   nil nil nil nil default))
431              (riece-completing-read-identity
432               "Channel/User: "
433               (riece-get-identities-on-server (riece-current-server-name)))))
434           key)
435      (if (and current-prefix-arg
436               (riece-channel-p (riece-identity-prefix target)))
437          (setq key
438                (riece-read-passwd (format "Key for %s: "
439                                           (riece-format-identity target)))))
440      (list target key)))
441   (let ((pointer (riece-identity-member target riece-current-channels)))
442     (if pointer
443         (riece-command-switch-to-channel (car pointer))
444       (if (riece-channel-p (riece-identity-prefix target))
445           (riece-command-join-channel target key)
446         (riece-command-join-partner target)))))
447
448 (defun riece-command-part-channel (target message)
449   (let ((process (riece-server-process (riece-identity-server target))))
450     (riece-process-send-string process
451                                (if message
452                                    (format "PART %s :%s\r\n"
453                                            (riece-identity-prefix target)
454                                            message)
455                                  (format "PART %s\r\n"
456                                          (riece-identity-prefix target))))))
457
458 (defun riece-command-part (target &optional message)
459   (interactive
460    (progn
461      (riece-check-channel-commands-are-usable)
462      (let* ((completion-ignore-case t)
463             (target
464              (riece-completing-read-identity
465               (format "Channel/User (default %s): "
466                       (riece-format-identity riece-current-channel))
467               riece-current-channels nil nil nil nil
468               (riece-format-identity riece-current-channel)))
469             message)
470        (if (and current-prefix-arg
471                 (riece-channel-p (riece-identity-prefix target)))
472            (setq message (read-string "Message: ")))
473        (list target message))))
474   (if (riece-identity-member target riece-current-channels)
475       (if (riece-channel-p (riece-identity-prefix target))
476           (riece-command-part-channel target message)
477         (riece-part-channel target))
478     (error "You are not talking with %s" target)))
479
480 (defun riece-command-change-nickname (nickname)
481   "Change your nickname to NICK."
482   (interactive "sEnter your nickname: ")
483   (riece-send-string (format "NICK %s\r\n" nickname)))
484
485 (defun riece-command-scroll-down (lines)
486   "Scroll LINES down dialogue buffer from command buffer."
487   (interactive "P")
488   (let ((buffer (if (and riece-channel-buffer-mode
489                          riece-current-channel)
490                     riece-channel-buffer
491                   riece-dialogue-buffer)))
492     (if (get-buffer-window buffer)
493         (condition-case nil
494             (let ((other-window-scroll-buffer buffer))
495               (scroll-other-window-down lines))
496           (beginning-of-buffer
497            (message "Beginning of buffer"))))))
498
499 (defun riece-command-scroll-up (lines)
500   "Scroll LINES up dialogue buffer from command buffer."
501   (interactive "P")
502   (let ((buffer (if (and riece-channel-buffer-mode
503                          riece-current-channel)
504                     riece-channel-buffer
505                   riece-dialogue-buffer)))
506     (if (get-buffer-window buffer)
507         (condition-case nil
508             (let ((other-window-scroll-buffer buffer))
509               (scroll-other-window lines))
510           (end-of-buffer
511            (message "End of buffer"))))))
512
513 (defun riece-command-user-list-scroll-down (lines)
514   "Scroll LINES down user list buffer from command buffer."
515   (interactive "P")
516   (if (get-buffer-window riece-user-list-buffer)
517       (condition-case nil
518           (let ((other-window-scroll-buffer riece-user-list-buffer))
519             (scroll-other-window-down lines))
520         (beginning-of-buffer
521          (message "Beginning of buffer")))))
522
523 (defun riece-command-user-list-scroll-up (lines)
524   "Scroll LINES up user list buffer from command buffer."
525   (interactive "P")
526   (if (get-buffer-window riece-user-list-buffer)
527       (condition-case nil
528           (let ((other-window-scroll-buffer riece-user-list-buffer))
529             (scroll-other-window lines))
530         (end-of-buffer
531          (message "End of buffer")))))
532
533 (defun riece-command-toggle-away (&optional message)
534   "Mark yourself as being away."
535   (interactive
536    (if (and (not (riece-with-server-buffer (riece-identity-server
537                                             (riece-current-nickname))
538                    (riece-user-get-away (riece-identity-prefix
539                                          (riece-current-nickname)))))
540             current-prefix-arg)
541        (list (read-from-minibuffer
542               "Away message: " (cons (or riece-away-message "") 0)))))
543   (if (riece-with-server-buffer (riece-identity-server
544                                  (riece-current-nickname))
545         (riece-user-get-away (riece-identity-prefix
546                               (riece-current-nickname))))
547       (riece-send-string "AWAY\r\n")
548     (riece-send-string (format "AWAY :%s\r\n" (or message
549                                                   riece-away-message)))))
550
551 (defun riece-command-toggle-freeze (&optional arg)
552   "Prevent automatic scrolling of the dialogue window.
553 If prefix argument ARG is non-nil, toggle frozen status."
554   (interactive "P")
555   (with-current-buffer (if (and riece-channel-buffer-mode
556                                 riece-channel-buffer)
557                            riece-channel-buffer
558                          riece-dialogue-buffer)
559     (setq riece-freeze (if arg
560                            (< 0 (prefix-numeric-value arg))
561                          (not riece-freeze)))
562     (riece-emit-signal 'buffer-freeze-changed
563                        (current-buffer) riece-freeze)))
564
565 (defun riece-command-toggle-own-freeze (&optional arg)
566   "Prevent automatic scrolling of the dialogue window.
567 The difference from `riece-command-freeze' is that your messages are hidden.
568 If prefix argument ARG is non-nil, toggle frozen status."
569   (interactive "P")
570   (with-current-buffer (if (and riece-channel-buffer-mode
571                                 riece-channel-buffer)
572                            riece-channel-buffer
573                          riece-dialogue-buffer)
574     (if (if arg
575             (< 0 (prefix-numeric-value arg))
576           (not (eq riece-freeze 'own)))
577         (setq riece-freeze 'own)
578       (setq riece-freeze nil)))
579   (riece-emit-signal 'buffer-freeze-changed
580                      (current-buffer) riece-freeze))
581
582 (eval-when-compile
583   (autoload 'riece-exit "riece"))
584 (defun riece-command-quit (&optional arg)
585   "Quit IRC."
586   (interactive "P")
587   (if (y-or-n-p "Really quit IRC? ")
588       (if riece-server-process-alist
589           (let ((message
590                  (if arg
591                      (read-string "Message: ")
592                    riece-quit-message))
593                 (alist riece-server-process-alist))
594             (while alist
595               (riece-quit-server-process (cdr (car alist)) message)
596               (setq alist (cdr alist))))
597         ;; If no server process is available, exit immediately.
598         (riece-exit))))
599
600 (defun riece-command-raw (command)
601   "Enter raw IRC command, which is sent to the server."
602   (interactive "sIRC command: ")
603   (riece-send-string (concat command "\r\n")))
604
605 (defun riece-command-end-of-buffer ()
606   "Get end of the dialogue buffer."
607   (interactive)
608   (let (buffer window)
609     (setq buffer (if riece-channel-buffer-mode
610                      riece-channel-buffer
611                    riece-dialogue-buffer))
612     (or (setq window (get-buffer-window buffer))
613         (setq window (get-buffer-window riece-dialogue-buffer)
614               buffer riece-dialogue-buffer))
615     (when window
616       (save-selected-window
617         (select-window window)
618         (goto-char (point-max))))))
619
620 (defun riece-command-copy-region (start end)
621   "Move current region between START and END to `kill-ring'."
622   (interactive "r")
623   (kill-new (buffer-substring-no-properties start end)))
624
625 (defun riece-command-open-server (server-name)
626   (interactive
627    (list (completing-read "Server: " riece-server-alist)))
628   (if (riece-server-process server-name)
629       (error "%s is already opened" server-name))
630   (riece-open-server
631    (riece-server-name-to-server server-name)
632    server-name))
633
634 (defun riece-command-close-server (server-name &optional message)
635   (interactive
636    (list (completing-read "Server: " riece-server-process-alist)
637          (if current-prefix-arg
638              (read-string "Message: ")
639            riece-quit-message)))
640   (riece-quit-server-process (riece-server-process server-name) message))
641
642 (defun riece-command-universal-server-name-argument ()
643   (interactive)
644   (let* ((riece-overriding-server-name
645           (completing-read "Server: " riece-server-process-alist))
646          (command
647           (key-binding (read-key-sequence
648                         (format "Command to execute on \"%s\":"
649                                 riece-overriding-server-name)))))
650     (message "")
651     (call-interactively command)))
652
653 (provide 'riece-commands)
654
655 ;;; riece-commands.el ends here