* riece-commands.el (riece-command-topic): Use
[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    (riece-check-channel-commands-are-usable t)
158    (list (read-from-minibuffer
159           "Topic: " (cons (or (riece-with-server-buffer
160                                   (riece-identity-server riece-current-channel)
161                                 (riece-channel-get-topic
162                                  (riece-identity-prefix
163                                   riece-current-channel)))
164                               "")
165                           0))))
166   (riece-send-string (format "TOPIC %s :%s\r\n"
167                              (riece-identity-prefix riece-current-channel)
168                              topic)))
169
170 (defun riece-command-invite (user)
171   (interactive
172    (riece-check-channel-commands-are-usable t)
173    (let ((completion-ignore-case t))
174      (list (completing-read
175             "User: "
176             (mapcar #'list (riece-get-users-on-server))))))
177   (riece-send-string (format "INVITE %s %s\r\n"
178                              user (riece-identity-prefix
179                                    riece-current-channel))))
180
181 (defun riece-command-kick (user &optional message)
182   (interactive
183    (riece-check-channel-commands-are-usable t)
184    (let ((completion-ignore-case t))
185      (list (completing-read
186             "User: "
187             (mapcar #'list
188                     (riece-with-server-buffer
189                         (riece-identity-server riece-current-channel)
190                       (riece-channel-get-users
191                        (riece-identity-prefix riece-current-channel)))))
192            (if current-prefix-arg
193                (read-string "Message: ")))))
194   (riece-send-string
195    (if message
196        (format "KICK %s %s :%s\r\n"
197                (riece-identity-prefix riece-current-channel)
198                user message)
199      (format "KICK %s %s\r\n"
200              (riece-identity-prefix riece-current-channel)
201              user))))
202
203 (defun riece-command-names (pattern)
204   (interactive
205    (let ((completion-ignore-case t))
206      (list (read-from-minibuffer
207             "Pattern: "
208             (if (and riece-current-channel
209                      (riece-channel-p (riece-identity-prefix
210                                        riece-current-channel)))
211                 (cons (riece-identity-prefix riece-current-channel)
212                       0))))))
213   (if (or (not (equal pattern ""))
214           (yes-or-no-p "Really want to query NAMES without argument? "))
215       (riece-send-string (format "NAMES %s\r\n" pattern))))
216
217 (defun riece-command-who (pattern)
218   (interactive
219    (let ((completion-ignore-case t))
220      (list (read-from-minibuffer
221             "Pattern: "
222             (if (and riece-current-channel
223                      (riece-channel-p (riece-identity-prefix
224                                        riece-current-channel)))
225                 (cons (riece-identity-prefix riece-current-channel)
226                       0))))))
227   (if (or (not (equal pattern ""))
228           (yes-or-no-p "Really want to query WHO without argument? "))
229       (riece-send-string (format "WHO %s\r\n" pattern))))
230
231 (defun riece-command-list (pattern)
232   (interactive
233    (let ((completion-ignore-case t))
234      (list (read-from-minibuffer
235             "Pattern: "
236             (if (and riece-current-channel
237                      (riece-channel-p (riece-identity-prefix
238                                        riece-current-channel)))
239                 (cons (riece-identity-prefix riece-current-channel)
240                       0))))))
241   (if (or (not (equal pattern ""))
242           (yes-or-no-p "Really want to query LIST without argument? "))
243       (riece-send-string (format "LIST %s\r\n" pattern))))
244
245 (defun riece-command-change-mode (channel change)
246   (interactive
247    (let* ((completion-ignore-case t)
248           (channel
249            (if current-prefix-arg
250                (riece-completing-read-identity
251                 "Channel/User: " riece-current-channels)
252              (riece-check-channel-commands-are-usable t)
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    (riece-check-channel-commands-are-usable t)
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    (riece-check-channel-commands-are-usable t)
317    (let ((speakers
318           (riece-with-server-buffer
319               (riece-identity-server riece-current-channel)
320             (riece-channel-get-speakers
321              (riece-identity-prefix riece-current-channel))))
322          (completion-ignore-case t)
323          users)
324      (if current-prefix-arg
325          (setq users (riece-completing-read-multiple
326                       "Users"
327                       (mapcar #'list speakers)))
328        (setq users (riece-completing-read-multiple
329                     "Users"
330                     (delq nil (mapcar
331                                (lambda (user)
332                                  (unless (member user speakers)
333                                    (list user)))
334                                (riece-with-server-buffer
335                                    (riece-identity-server
336                                     riece-current-channel)
337                                  (riece-channel-get-users
338                                   (riece-identity-prefix
339                                    riece-current-channel))))))))
340      (list users current-prefix-arg)))
341   (let (group)
342     (while users
343       (setq group (cons (car users) group)
344             users (cdr users))
345       (if (or (= (length group) 3)
346               (null users))
347           (riece-send-string
348            (format "MODE %s %c%s %s\r\n"
349                    (riece-identity-prefix riece-current-channel)
350                    (if current-prefix-arg
351                        ?-
352                      ?+)
353                    (make-string (length group) ?v)
354                    (mapconcat #'identity group " ")))))))
355
356 (defun riece-command-send-message (message notice)
357   "Send MESSAGE to the current channel."
358   (if (equal message "")
359       (error "No text to send"))
360   (riece-check-channel-commands-are-usable)
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            (if riece-join-channel-candidate
424                (let ((default (riece-format-identity
425                                riece-join-channel-candidate)))
426                  (riece-completing-read-identity
427                   (format "Channel/User (default %s): " default)
428                   riece-current-channels nil nil nil nil default))
429              (riece-completing-read-identity
430               "Channel/User: " riece-current-channels)))
431           key)
432      (if (and current-prefix-arg
433               (riece-channel-p (riece-identity-prefix target)))
434          (setq key
435                (riece-read-passwd (format "Key for %s: "
436                                           (riece-format-identity target)))))
437      (list target key)))
438   (let ((pointer (riece-identity-member target riece-current-channels)))
439     (if pointer
440         (riece-command-switch-to-channel (car pointer))
441       (if (riece-channel-p (riece-identity-prefix target))
442           (riece-command-join-channel target key)
443         (riece-command-join-partner target)))))
444
445 (defun riece-command-part-channel (target message)
446   (let ((process (riece-server-process (riece-identity-server target))))
447     (riece-process-send-string process
448                                (if message
449                                    (format "PART %s :%s\r\n"
450                                            (riece-identity-prefix target)
451                                            message)
452                                  (format "PART %s\r\n"
453                                          (riece-identity-prefix target))))))
454
455 (defun riece-command-part (target &optional message)
456   (interactive
457    (riece-check-channel-commands-are-usable)
458    (let* ((completion-ignore-case t)
459          (target
460           (riece-completing-read-identity
461            (format "Channel/User (default %s): "
462                    (riece-format-identity riece-current-channel))
463            riece-current-channels nil nil nil nil
464            (riece-format-identity riece-current-channel)))
465          message)
466      (if (and current-prefix-arg
467               (riece-channel-p (riece-identity-prefix target)))
468          (setq message (read-string "Message: ")))
469      (list target message)))
470   (if (riece-identity-member target riece-current-channels)
471       (if (riece-channel-p (riece-identity-prefix target))
472           (riece-command-part-channel target message)
473         (riece-part-channel target)
474         (riece-redisplay-buffers))
475     (error "You are not talking with %s" target)))
476
477 (defun riece-command-change-nickname (nickname)
478   "Change your nickname to NICK."
479   (interactive "sEnter your nickname: ")
480   (riece-send-string (format "NICK %s\r\n" nickname)))
481
482 (defun riece-command-scroll-down (lines)
483   "Scroll LINES down dialogue buffer from command buffer."
484   (interactive "P")
485   (let ((other-window-scroll-buffer
486          (if riece-channel-buffer-mode
487              riece-channel-buffer
488            riece-dialogue-buffer)))
489     (when (get-buffer-window other-window-scroll-buffer)
490       (condition-case nil
491           (scroll-other-window-down lines)
492         (beginning-of-buffer
493          (message "Beginning of buffer"))))))
494
495 (defun riece-command-scroll-up (lines)
496   "Scroll LINES up dialogue buffer from command buffer."
497   (interactive "P")
498   (let* ((other-window-scroll-buffer
499           (if riece-channel-buffer-mode
500               riece-channel-buffer
501             riece-dialogue-buffer)))
502     (when (get-buffer-window other-window-scroll-buffer)
503       (condition-case nil
504           (scroll-other-window lines)
505         (end-of-buffer
506          (message "End of buffer"))))))
507
508 (defun riece-command-nick-scroll-down (lines)
509   "Scroll LINES down nick buffer from command buffer."
510   (interactive "P")
511   (let ((other-window-scroll-buffer riece-user-list-buffer))
512     (when (get-buffer-window other-window-scroll-buffer)
513       (condition-case nil
514           (scroll-other-window-down lines)
515         (beginning-of-buffer
516          (message "Beginning of buffer"))))))
517
518 (defun riece-command-nick-scroll-up (lines)
519   "Scroll LINES up nick buffer from command buffer."
520   (interactive "P")
521   (let* ((other-window-scroll-buffer riece-user-list-buffer))
522     (when (get-buffer-window other-window-scroll-buffer)
523       (condition-case nil
524           (scroll-other-window lines)
525         (end-of-buffer
526          (message "End of buffer"))))))
527
528 (defun riece-command-toggle-away (&optional message)
529   "Mark yourself as being away."
530   (interactive
531    (if (and (not (riece-with-server-buffer (riece-identity-server
532                                             (riece-current-nickname))
533                    (riece-user-get-away (riece-identity-prefix
534                                          (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             (alist riece-server-process-alist))
584         (while alist
585           (riece-quit-server-process (cdr (car alist)) message)
586           (setq alist (cdr alist))))))
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 "Server: " riece-server-process-alist)
625          (if current-prefix-arg
626              (read-string "Message: ")
627            (or riece-quit-message
628                (riece-extended-version)))))
629   (riece-quit-server-process (riece-server-process server-name) message))
630
631 (defun riece-command-universal-server-name-argument ()
632   (interactive)
633   (let* ((riece-overriding-server-name
634           (completing-read "Server: " riece-server-process-alist))
635          (command
636           (key-binding (read-key-sequence
637                         (format "Command to execute on \"%s\":"
638                                 riece-overriding-server-name)))))
639     (message "")
640     (call-interactively command)))
641
642 (provide 'riece-commands)
643
644 ;;; riece-commands.el ends here