* riece.el (riece-buffer-alist): Rename from
[riece] / lisp / riece-commands.el
1 ;;; riece-commands.el --- commands available in command buffer
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'riece-channel)
28 (require 'riece-complete)
29 (require 'riece-layout)
30 (require 'riece-display)
31 (require 'riece-version)
32 (require 'riece-server)
33 (require 'riece-misc)
34 (require 'riece-identity)
35 (require 'riece-message)
36
37 ;;; Channel movement:
38 (defun riece-command-switch-to-channel (channel)
39   (interactive (list (riece-completing-read-identity
40                       "Channel/User: " riece-current-channels nil t)))
41   (unless (equal channel riece-current-channels)
42     (riece-switch-to-channel channel)
43     (riece-redisplay-buffers)))
44
45 (defun riece-command-switch-to-channel-by-number (number)
46   (interactive
47    (let ((command-name (symbol-name this-command)))
48      (if (string-match "[0-9]+$" command-name)
49          (list (string-to-number (match-string 0 command-name)))
50        (list (string-to-number (read-string "Number: "))))))
51   (let ((channel (nth (1- number) riece-current-channels)))
52     (if channel
53         (riece-command-switch-to-channel channel)
54       (error "No such number!"))))
55
56 (eval-and-compile
57   (let ((number 1))
58     (while (<= number 20)
59       (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
60                                 (number-to-string number)))
61         'riece-command-switch-to-channel-by-number)
62       (setq number (1+ number)))))
63
64 (defun riece-command-next-channel ()
65   "Select the next channel."
66   (interactive)
67   (when (> (length riece-current-channels) 1)
68     (let ((pointer (cdr (riece-identity-member
69                          riece-current-channel
70                          riece-current-channels))))
71       (while (and pointer
72                   (null (car pointer)))
73         (setq pointer (cdr pointer)))
74       (when (null pointer)
75         (setq pointer riece-current-channels)
76         (while (and pointer
77                     (null (car pointer)))
78           (setq pointer (cdr pointer))))
79       (if (car pointer)
80           (riece-command-switch-to-channel (car pointer))
81         (error "No such channel!")))))
82
83 (defun riece-command-previous-channel ()
84   "Select the previous channel."
85   (interactive)
86   (when (> (length riece-current-channels) 1)
87     (let ((pointer (riece-identity-member
88                     riece-current-channel
89                     riece-current-channels))
90           (start riece-current-channels)
91           channel)
92       (while (and start (not (eq start pointer)))
93         (if (car start)
94             (setq channel (car start)))
95         (setq start (cdr start)))
96       (when (null channel)
97         (setq start (copy-sequence riece-current-channels))
98         (setq start (delq nil start))
99         (and (> (length start) 1)
100              (setq channel (nth (1- (length start)) start))))
101       (if channel
102           (riece-command-switch-to-channel channel)
103         (error "No such channel!")))))
104
105 (defun riece-command-select-command-buffer ()
106   "Select the command buffer."
107   (interactive)
108   (let ((window (get-buffer-window riece-command-buffer)))
109     (if window
110         (select-window window))))
111
112 (defun riece-command-configure-windows ()
113   (interactive)
114   (riece-redisplay-buffers t))
115
116 (defun riece-command-change-layout (name)
117   "Select a layout-name from all current available layouts and change
118 the layout to the selected layout-name."
119   (interactive (list (completing-read "Layout: " riece-layout-alist)))
120   (setq riece-layout name)
121   (riece-command-configure-windows))
122
123 (defun riece-command-toggle-channel-buffer-mode ()
124   (interactive)
125   (setq riece-channel-buffer-mode
126         (not riece-channel-buffer-mode)
127         riece-save-variables-are-dirty t)
128   (riece-command-configure-windows))
129
130 (defun riece-command-toggle-user-list-buffer-mode ()
131   (interactive)
132   (setq riece-user-list-buffer-mode
133         (not riece-user-list-buffer-mode)
134         riece-save-variables-are-dirty t)
135   (riece-command-configure-windows))
136
137 (defun riece-command-toggle-channel-list-buffer-mode ()
138   (interactive)
139   (setq riece-channel-list-buffer-mode
140         (not riece-channel-list-buffer-mode)
141         riece-save-variables-are-dirty t)
142   (riece-command-configure-windows))
143
144 (defun riece-command-finger (user &optional recurse)
145   (interactive
146    (let* ((completion-ignore-case t)
147           (user (completing-read
148                  "User: "
149                  (mapcar #'list (riece-get-users-on-server)))))
150      (list user current-prefix-arg)))
151   (if recurse
152       (riece-send-string (format "WHOIS %s %s\r\n" user user))
153     (riece-send-string (format "WHOIS %s\r\n" user))))
154
155 (defun riece-command-topic (topic)
156   (interactive
157    (list (read-from-minibuffer
158           "Topic: " (cons (or (riece-with-server-buffer
159                                   (riece-identity-server riece-current-channel)
160                                 (riece-channel-get-topic
161                                  (riece-identity-prefix
162                                   riece-current-channel)))
163                               "")
164                           0))))
165   (riece-send-string (format "TOPIC %s :%s\r\n"
166                              (riece-identity-prefix riece-current-channel)
167                              topic)))
168
169 (defun riece-command-invite (user)
170   (interactive
171    (let ((completion-ignore-case t))
172      (unless (and riece-current-channel
173                   (riece-channel-p (riece-identity-prefix
174                                     riece-current-channel)))
175        (error "Not on a channel"))
176      (list (completing-read
177             "User: "
178             (mapcar #'list (riece-get-users-on-server))))))
179   (riece-send-string (format "INVITE %s %s\r\n"
180                              user (riece-identity-prefix
181                                    riece-current-channel))))
182
183 (defun riece-command-kick (user &optional message)
184   (interactive
185    (let ((completion-ignore-case t))
186      (unless (and riece-current-channel
187                   (riece-channel-p (riece-identity-prefix
188                                     riece-current-channel)))
189        (error "Not on a channel"))
190      (list (completing-read
191             "User: "
192             (mapcar #'list
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-current-channel))
258           (riece-overriding-server-name (riece-identity-server channel))
259           (riece-temp-minibuffer-message
260            (concat "[Available modes: "
261                    (riece-with-server-buffer (riece-identity-server channel)
262                      (if (riece-channel-p (riece-identity-prefix channel))
263                          (if riece-supported-channel-modes
264                              (apply #'string riece-supported-channel-modes))
265                        (if riece-supported-user-modes
266                            (apply #'string riece-supported-user-modes))))
267                    "]")))
268      (list channel
269            (read-from-minibuffer
270             (concat (riece-concat-channel-modes
271                      channel "Mode (? for help)") ": ")
272             nil riece-minibuffer-map))))
273   (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
274                              change)))
275
276 (defun riece-command-set-operators (users &optional arg)
277   (interactive
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    (let ((speakers
320           (riece-with-server-buffer
321               (riece-identity-server riece-current-channel)
322             (riece-channel-get-speakers
323              (riece-identity-prefix riece-current-channel))))
324          (completion-ignore-case t)
325          users)
326      (if current-prefix-arg
327          (setq users (riece-completing-read-multiple
328                       "Users"
329                       (mapcar #'list speakers)))
330        (setq users (riece-completing-read-multiple
331                     "Users"
332                     (delq nil (mapcar
333                                (lambda (user)
334                                  (unless (member user speakers)
335                                    (list user)))
336                                (riece-with-server-buffer
337                                    (riece-identity-server
338                                     riece-current-channel)
339                                  (riece-channel-get-users
340                                   (riece-identity-prefix
341                                    riece-current-channel))))))))
342      (list users current-prefix-arg)))
343   (let (group)
344     (while users
345       (setq group (cons (car users) group)
346             users (cdr users))
347       (if (or (= (length group) 3)
348               (null users))
349           (riece-send-string
350            (format "MODE %s %c%s %s\r\n"
351                    (riece-identity-prefix riece-current-channel)
352                    (if current-prefix-arg
353                        ?-
354                      ?+)
355                    (make-string (length group) ?v)
356                    (mapconcat #'identity group " ")))))))
357
358 (defun riece-command-send-message (message notice)
359   "Send MESSAGE to the current channel."
360   (if (equal message "")
361       (error "No text to send"))
362   (unless riece-current-channel
363     (error (substitute-command-keys
364             "Type \\[riece-command-join] to join a channel")))
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-join-channel (target key)
403   (let ((process (riece-server-process (riece-identity-server target))))
404     (unless process
405       (error "%s" (substitute-command-keys
406                    "Type \\[riece-command-open-server] to open server.")))
407     (riece-process-send-string process
408                                (if key
409                                    (format "JOIN %s :%s\r\n"
410                                            (riece-identity-prefix target)
411                                            key)
412                                  (format "JOIN %s\r\n"
413                                          (riece-identity-prefix target))))))
414
415 (defun riece-command-join-partner (target)
416   (let ((pointer (riece-identity-member target riece-current-channels)))
417     (if pointer
418         (riece-command-switch-to-channel (car pointer))
419       (riece-join-channel target)
420       (riece-switch-to-channel target)
421       (riece-redisplay-buffers))))
422
423 (defun riece-command-join (target &optional key)
424   (interactive
425    (let* ((completion-ignore-case t)
426           (target
427            (if riece-join-channel-candidate
428                (let ((default (riece-format-identity
429                                riece-join-channel-candidate)))
430                  (riece-completing-read-identity
431                   (format "Channel/User (default %s): " default)
432                   riece-current-channels nil nil nil nil default))
433              (riece-completing-read-identity
434               "Channel/User: " riece-current-channels)))
435           key)
436      (if (and current-prefix-arg
437               (riece-channel-p (riece-identity-prefix target)))
438          (setq key
439                (riece-read-passwd (format "Key for %s: "
440                                           (riece-format-identity target)))))
441      (list target key)))
442   (let ((pointer (riece-identity-member target riece-current-channels)))
443     (if pointer
444         (riece-command-switch-to-channel (car pointer))
445       (if (riece-channel-p (riece-identity-prefix target))
446           (riece-command-join-channel target key)
447         (riece-command-join-partner target)))))
448
449 (defun riece-command-part-channel (target message)
450   (let ((process (riece-server-process (riece-identity-server target))))
451     (unless process
452       (error "%s" (substitute-command-keys
453                    "Type \\[riece-command-open-server] to open server.")))
454     (riece-process-send-string process
455                                (if message
456                                    (format "PART %s :%s\r\n"
457                                            (riece-identity-prefix target)
458                                            message)
459                                  (format "PART %s\r\n"
460                                          (riece-identity-prefix target))))))
461
462 (defun riece-command-part (target &optional message)
463   (interactive
464    (let* ((completion-ignore-case t)
465          (target
466           (riece-completing-read-identity
467            (format "Channel/User (default %s): "
468                    (riece-format-identity riece-current-channel))
469            riece-current-channels nil nil nil nil
470            (riece-format-identity riece-current-channel)))
471          message)
472      (if (and current-prefix-arg
473               (riece-channel-p (riece-identity-prefix target)))
474          (setq message (read-string "Message: ")))
475      (list target message)))
476   (if (riece-identity-member target riece-current-channels)
477       (if (riece-channel-p (riece-identity-prefix target))
478           (riece-command-part-channel target message)
479         (riece-part-channel target)
480         (riece-redisplay-buffers))
481     (error "You are not talking with %s" target)))
482
483 (defun riece-command-change-nickname (nickname)
484   "Change your nickname to NICK."
485   (interactive "sEnter your nickname: ")
486   (riece-send-string (format "NICK %s\r\n" nickname)))
487
488 (defun riece-command-scroll-down (lines)
489   "Scroll LINES down 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-down lines)
498         (beginning-of-buffer
499          (message "Beginning of buffer"))))))
500
501 (defun riece-command-scroll-up (lines)
502   "Scroll LINES up dialogue buffer from command buffer."
503   (interactive "P")
504   (let* ((other-window-scroll-buffer
505           (if riece-channel-buffer-mode
506               riece-channel-buffer
507             riece-dialogue-buffer)))
508     (when (get-buffer-window other-window-scroll-buffer)
509       (condition-case nil
510           (scroll-other-window lines)
511         (end-of-buffer
512          (message "End of buffer"))))))
513
514 (defun riece-command-nick-scroll-down (lines)
515   "Scroll LINES down nick buffer from command buffer."
516   (interactive "P")
517   (let ((other-window-scroll-buffer riece-user-list-buffer))
518     (when (get-buffer-window other-window-scroll-buffer)
519       (condition-case nil
520           (scroll-other-window-down lines)
521         (beginning-of-buffer
522          (message "Beginning of buffer"))))))
523
524 (defun riece-command-nick-scroll-up (lines)
525   "Scroll LINES up nick buffer from command buffer."
526   (interactive "P")
527   (let* ((other-window-scroll-buffer riece-user-list-buffer))
528     (when (get-buffer-window other-window-scroll-buffer)
529       (condition-case nil
530           (scroll-other-window lines)
531         (end-of-buffer
532          (message "End of buffer"))))))
533
534 (defun riece-command-toggle-away (&optional message)
535   "Mark yourself as being away."
536   (interactive
537    (if (and (not (riece-with-server-buffer (riece-identity-server
538                                             (riece-current-nickname))
539                    (riece-user-get-away (riece-identity-prefix
540                                          (riece-current-nickname)))))
541             (or (null riece-away-message)
542                 current-prefix-arg))
543        (let ((message (read-string "Away message: ")))
544          (list message))))
545   (if message
546       (riece-send-string (format "AWAY :%s\r\n" message))
547     (riece-send-string "AWAY\r\n")))
548
549 (defun riece-command-toggle-freeze (&optional arg)
550   "Prevent automatic scrolling of the dialogue window.
551 If prefix argument ARG is non-nil, toggle frozen status."
552   (interactive "P")
553   (with-current-buffer (if (and riece-channel-buffer-mode
554                                 riece-channel-buffer)
555                            riece-channel-buffer
556                          riece-dialogue-buffer)
557     (setq riece-freeze (if arg
558                            (< 0 (prefix-numeric-value arg))
559                          (not riece-freeze))))
560   (riece-update-status-indicators)
561   (force-mode-line-update t))
562
563 (defun riece-command-toggle-own-freeze (&optional arg)
564   "Prevent automatic scrolling of the dialogue window.
565 The difference from `riece-command-freeze' is that your messages are hidden.
566 If prefix argument ARG is non-nil, toggle frozen status."
567   (interactive "P")
568   (with-current-buffer (if (and riece-channel-buffer-mode
569                                 riece-channel-buffer)
570                            riece-channel-buffer
571                          riece-dialogue-buffer)
572     (if (if arg
573             (< 0 (prefix-numeric-value arg))
574           (not (eq riece-freeze 'own)))
575         (setq riece-freeze 'own)
576       (setq riece-freeze nil)))
577   (riece-update-status-indicators)
578   (force-mode-line-update t))
579
580 (defun riece-command-quit (&optional arg)
581   "Quit IRC."
582   (interactive "P")
583   (if (y-or-n-p "Really quit IRC? ")
584       (let ((message
585              (if arg
586                  (read-string "Message: ")
587                (or riece-quit-message
588                    (riece-extended-version))))
589             (alist riece-server-process-alist))
590         (while alist
591           (riece-quit-server-process (cdr (car alist)) message)
592           (setq alist (cdr alist))))))
593
594 (defun riece-command-raw (command)
595   "Enter raw IRC command, which is sent to the server."
596   (interactive "sIRC command: ")
597   (riece-send-string (concat command "\r\n")))
598
599 (defun riece-command-end-of-buffer ()
600   "Get end of the dialogue buffer."
601   (interactive)
602   (let (buffer window)
603     (setq buffer (if riece-channel-buffer-mode
604                      riece-channel-buffer
605                    riece-dialogue-buffer))
606     (or (setq window (get-buffer-window buffer))
607         (setq window (get-buffer-window riece-dialogue-buffer)
608               buffer riece-dialogue-buffer))
609     (when window
610       (save-selected-window
611         (select-window window)
612         (goto-char (point-max))))))
613
614 (defun riece-command-copy-region (start end)
615   "Move current region between START and END to `kill-ring'."
616   (interactive "r")
617   (kill-new (buffer-substring-no-properties start end)))
618
619 (defun riece-command-open-server (server-name)
620   (interactive
621    (list (completing-read "Server: " riece-server-alist)))
622   (if (riece-server-process server-name)
623       (error "%s is already opened" server-name))
624   (riece-open-server
625    (riece-server-name-to-server server-name)
626    server-name))
627
628 (defun riece-command-close-server (server-name &optional message)
629   (interactive
630    (list (completing-read "Server: " riece-server-process-alist)
631          (if current-prefix-arg
632              (read-string "Message: ")
633            (or riece-quit-message
634                (riece-extended-version)))))
635   (riece-quit-server-process (riece-server-process server-name) message))
636
637 (defun riece-command-universal-server-name-argument ()
638   (interactive)
639   (let* ((riece-overriding-server-name
640           (completing-read "Server: " riece-server-process-alist)))
641          (command
642           (key-binding (read-key-sequence
643                         (format "Command to execute on \"%s\":"
644                                 riece-overriding-server-name)))))
645     (message "")
646     (call-interactively command)))
647
648 (provide 'riece-commands)
649
650 ;;; riece-commands.el ends here