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