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