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