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