890e2e8281a392c85ee1e11788792ead889af839
[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-server)
32 (require 'riece-misc)
33 (require 'riece-identity)
34 (require 'riece-message)
35
36 ;;; Channel movement:
37 (defun riece-command-switch-to-channel (channel)
38   (interactive (list (riece-completing-read-identity
39                       "Channel/User: " riece-current-channels nil t)))
40   (unless (equal channel riece-current-channel)
41     (riece-switch-to-channel channel)
42     (riece-redisplay-buffers)))
43
44 (defun riece-command-switch-to-channel-by-number (number)
45   (interactive
46    (let ((command-name (symbol-name this-command)))
47      (if (string-match "[0-9]+$" command-name)
48          (list (string-to-number (match-string 0 command-name)))
49        (list (string-to-number (read-string "Number: "))))))
50   (let ((channel (nth (1- number) riece-current-channels)))
51     (if channel
52         (riece-command-switch-to-channel channel)
53       (error "No such number!"))))
54
55 (eval-and-compile
56   (let ((number 1))
57     (while (<= number 20)
58       (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
59                                 (number-to-string number)))
60         'riece-command-switch-to-channel-by-number)
61       (setq number (1+ number)))))
62
63 (defun riece-command-next-channel ()
64   "Select the next channel."
65   (interactive)
66   (when (> (length riece-current-channels) 1)
67     (let ((pointer (cdr (riece-identity-member
68                          riece-current-channel
69                          riece-current-channels))))
70       (while (and pointer
71                   (null (car pointer)))
72         (setq pointer (cdr pointer)))
73       (when (null pointer)
74         (setq pointer riece-current-channels)
75         (while (and pointer
76                     (null (car pointer)))
77           (setq pointer (cdr pointer))))
78       (if (car pointer)
79           (riece-command-switch-to-channel (car pointer))
80         (error "No such channel!")))))
81
82 (defun riece-command-previous-channel ()
83   "Select the previous channel."
84   (interactive)
85   (when (> (length riece-current-channels) 1)
86     (let ((pointer (riece-identity-member
87                     riece-current-channel
88                     riece-current-channels))
89           (start riece-current-channels)
90           channel)
91       (while (and start (not (eq start pointer)))
92         (if (car start)
93             (setq channel (car start)))
94         (setq start (cdr start)))
95       (when (null channel)
96         (setq start (copy-sequence riece-current-channels))
97         (setq start (delq nil start))
98         (and (> (length start) 1)
99              (setq channel (nth (1- (length start)) start))))
100       (if channel
101           (riece-command-switch-to-channel channel)
102         (error "No such channel!")))))
103
104 (defun riece-command-select-command-buffer ()
105   "Select the command buffer."
106   (interactive)
107   (let ((window (get-buffer-window riece-command-buffer)))
108     (if window
109         (select-window window))))
110
111 (defun riece-command-configure-windows ()
112   (interactive)
113   (riece-redisplay-buffers t))
114
115 (defun riece-command-change-layout (name)
116   "Select a layout-name from all current available layouts and change
117 the layout to the selected layout-name."
118   (interactive (list (completing-read "Layout: " riece-layout-alist)))
119   (setq riece-layout name
120         riece-save-variables-are-dirty t)
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 (riece-completing-read-identity
148                  "User: "
149                  (riece-get-users-on-server (riece-current-server-name)))))
150      (list user current-prefix-arg)))
151   (if recurse
152       (riece-send-string (format "WHOIS %s %s\r\n"
153                                  (riece-identity-prefix user)
154                                  (riece-identity-prefix user)))
155     (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))
156
157 (defun riece-command-topic (topic)
158   (interactive
159    (progn
160      (riece-check-channel-commands-are-usable t)
161      (list (read-from-minibuffer
162             "Topic: " (cons (or (riece-with-server-buffer
163                                     (riece-identity-server
164                                      riece-current-channel)
165                                   (riece-channel-get-topic
166                                    (riece-identity-prefix
167                                     riece-current-channel)))
168                                 "")
169                             0)))))
170   (riece-send-string (format "TOPIC %s :%s\r\n"
171                              (riece-identity-prefix riece-current-channel)
172                              topic)))
173
174 (defun riece-command-invite (user)
175   (interactive
176    (let ((completion-ignore-case t))
177      (riece-check-channel-commands-are-usable t)
178      (list (riece-completing-read-identity
179             "User: "
180             (riece-get-users-on-server (riece-current-server-name))))))
181   (riece-send-string (format "INVITE %s %s\r\n"
182                              (riece-identity-prefix user)
183                              (riece-identity-prefix riece-current-channel))))
184
185 (defun riece-command-kick (user &optional message)
186   (interactive
187    (let ((completion-ignore-case t))
188      (riece-check-channel-commands-are-usable t)
189      (list (completing-read
190             "User: "
191             (riece-with-server-buffer
192                 (riece-identity-server riece-current-channel)
193               (riece-channel-get-users (riece-identity-prefix
194                                         riece-current-channel))))
195            (if current-prefix-arg
196                (read-string "Message: ")))))
197   (riece-send-string
198    (if message
199        (format "KICK %s %s :%s\r\n"
200                (riece-identity-prefix riece-current-channel)
201                user message)
202      (format "KICK %s %s\r\n"
203              (riece-identity-prefix riece-current-channel)
204              user))))
205
206 (defun riece-command-names (pattern)
207   (interactive
208    (let ((completion-ignore-case t))
209      (list (read-from-minibuffer
210             "Pattern: "
211             (if (and riece-current-channel
212                      (riece-channel-p (riece-identity-prefix
213                                        riece-current-channel)))
214                 (cons (riece-identity-prefix riece-current-channel)
215                       0))))))
216   (if (or (not (equal pattern ""))
217           (yes-or-no-p "Really want to query NAMES without argument? "))
218       (riece-send-string (format "NAMES %s\r\n" pattern))))
219
220 (defun riece-command-who (pattern)
221   (interactive
222    (let ((completion-ignore-case t))
223      (list (read-from-minibuffer
224             "Pattern: "
225             (if (and riece-current-channel
226                      (riece-channel-p (riece-identity-prefix
227                                        riece-current-channel)))
228                 (cons (riece-identity-prefix riece-current-channel)
229                       0))))))
230   (if (or (not (equal pattern ""))
231           (yes-or-no-p "Really want to query WHO without argument? "))
232       (riece-send-string (format "WHO %s\r\n" pattern))))
233
234 (defun riece-command-list (pattern)
235   (interactive
236    (let ((completion-ignore-case t))
237      (list (read-from-minibuffer
238             "Pattern: "
239             (if (and riece-current-channel
240                      (riece-channel-p (riece-identity-prefix
241                                        riece-current-channel)))
242                 (cons (riece-identity-prefix riece-current-channel)
243                       0))))))
244   (if (or (not (equal pattern ""))
245           (yes-or-no-p "Really want to query LIST without argument? "))
246       (riece-send-string (format "LIST %s\r\n" pattern))))
247
248 (defun riece-command-change-mode (channel change)
249   (interactive
250    (let* ((completion-ignore-case t)
251           (channel
252            (if current-prefix-arg
253                (riece-completing-read-identity
254                 "Channel/User: "
255                 (riece-get-identities-on-server (riece-current-server-name)))
256              (riece-check-channel-commands-are-usable t)
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    (progn
279      (riece-check-channel-commands-are-usable t)
280      (let ((completion-ignore-case t))
281        (list (riece-completing-read-multiple
282               "Users"
283               (riece-with-server-buffer
284                   (riece-identity-server riece-current-channel)
285                 (riece-channel-get-users (riece-identity-prefix
286                                          riece-current-channel)))
287               (if current-prefix-arg
288                   (lambda (user)
289                     (memq ?o (cdr user)))
290                 (lambda (user)
291                   (not (memq ?o (cdr user))))))
292              current-prefix-arg))))
293   (let (group)
294     (while users
295       (setq group (cons (car users) group)
296             users (cdr users))
297       (when (or (= (length group) 3)
298                 (null users))
299         (riece-send-string
300          (format "MODE %s %c%s %s\r\n"
301                  (riece-identity-prefix riece-current-channel)
302                  (if current-prefix-arg
303                      ?-
304                    ?+)
305                  (make-string (length group) ?o)
306                  (mapconcat #'identity group " ")))
307         (setq group nil)))))
308
309 (defun riece-command-set-speakers (users &optional arg)
310   (interactive
311    (progn
312      (riece-check-channel-commands-are-usable t)
313      (let ((completion-ignore-case t))
314        (list (riece-completing-read-multiple
315               "Users"
316               (riece-with-server-buffer
317                   (riece-identity-server riece-current-channel)
318                 (riece-channel-get-users (riece-identity-prefix
319                                           riece-current-channel)))
320               (if current-prefix-arg
321                   (lambda (user)
322                     (memq ?v (cdr user)))
323                 (lambda (user)
324                   (not (memq ?v (cdr user))))))
325              current-prefix-arg))))
326   (let (group)
327     (while users
328       (setq group (cons (car users) group)
329             users (cdr users))
330       (when (or (= (length group) 3)
331                 (null users))
332         (riece-send-string
333          (format "MODE %s %c%s %s\r\n"
334                  (riece-identity-prefix riece-current-channel)
335                  (if current-prefix-arg
336                      ?-
337                    ?+)
338                  (make-string (length group) ?v)
339                  (mapconcat #'identity group " ")))
340         (setq group nil)))))
341
342 (defun riece-command-send-message (message notice)
343   "Send MESSAGE to the current channel."
344   (if (equal message "")
345       (error "No text to send"))
346   (riece-check-channel-commands-are-usable)
347   (if notice
348       (progn
349         (riece-send-string
350          (format "NOTICE %s :%s\r\n"
351                  (riece-identity-prefix riece-current-channel)
352                  message))
353         (riece-display-message
354          (riece-make-message (riece-current-nickname) riece-current-channel
355                              message 'notice t)))
356     (riece-send-string
357      (format "PRIVMSG %s :%s\r\n"
358              (riece-identity-prefix riece-current-channel)
359              message))
360     (riece-display-message
361      (riece-make-message (riece-current-nickname) riece-current-channel
362                          message nil t))))
363
364 (defun riece-command-enter-message ()
365   "Send the current line to the current channel."
366   (interactive)
367   (riece-command-send-message (buffer-substring
368                                (riece-line-beginning-position)
369                                (riece-line-end-position))
370                               nil)
371   (let ((next-line-add-newlines t))
372     (next-line 1)))
373
374 (defun riece-command-enter-message-as-notice ()
375   "Send the current line to the current channel as NOTICE."
376   (interactive)
377   (riece-command-send-message (buffer-substring
378                                (riece-line-beginning-position)
379                                (riece-line-end-position))
380                               t)
381   (let ((next-line-add-newlines t))
382     (next-line 1)))
383
384 (defun riece-command-enter-message-to-user (user)
385   "Send the current line to USER."
386   (interactive
387    (let ((completion-ignore-case t))
388      (list (riece-completing-read-identity
389             "User: "
390             (riece-get-users-on-server (riece-current-server-name))))))
391   (let ((text (buffer-substring
392                (riece-line-beginning-position)
393                (riece-line-end-position))))
394     (riece-send-string
395      (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
396     (riece-display-message
397      (riece-make-message (riece-current-nickname) user text nil t)))
398   (let ((next-line-add-newlines t))
399     (next-line 1)))
400
401 (defun riece-command-join-channel (target key)
402   (let ((process (riece-server-process (riece-identity-server target))))
403     (unless process
404       (error "%s" (substitute-command-keys
405                    "Type \\[riece-command-open-server] to open server.")))
406     (riece-process-send-string process
407                                (if key
408                                    (format "JOIN %s :%s\r\n"
409                                            (riece-identity-prefix target)
410                                            key)
411                                  (format "JOIN %s\r\n"
412                                          (riece-identity-prefix target))))))
413
414 (defun riece-command-join-partner (target)
415   (let ((pointer (riece-identity-member target riece-current-channels)))
416     (if pointer
417         (riece-command-switch-to-channel (car pointer))
418       (riece-join-channel target)
419       (riece-switch-to-channel target)
420       (riece-redisplay-buffers))))
421
422 (defun riece-command-join (target &optional key)
423   (interactive
424    (let* ((completion-ignore-case t)
425           (target
426            (if riece-join-channel-candidate
427                (let ((default (riece-format-identity
428                                riece-join-channel-candidate)))
429                  (riece-completing-read-identity
430                   (format "Channel/User (default %s): " default)
431                   (riece-get-identities-on-server (riece-current-server-name))
432                   nil nil nil nil default))
433              (riece-completing-read-identity
434               "Channel/User: "
435               (riece-get-identities-on-server (riece-current-server-name)))))
436           key)
437      (if (and current-prefix-arg
438               (riece-channel-p (riece-identity-prefix target)))
439          (setq key
440                (riece-read-passwd (format "Key for %s: "
441                                           (riece-format-identity target)))))
442      (list target key)))
443   (let ((pointer (riece-identity-member target riece-current-channels)))
444     (if pointer
445         (riece-command-switch-to-channel (car pointer))
446       (if (riece-channel-p (riece-identity-prefix target))
447           (riece-command-join-channel target key)
448         (riece-command-join-partner target)))))
449
450 (defun riece-command-part-channel (target message)
451   (let ((process (riece-server-process (riece-identity-server target))))
452     (riece-process-send-string process
453                                (if message
454                                    (format "PART %s :%s\r\n"
455                                            (riece-identity-prefix target)
456                                            message)
457                                  (format "PART %s\r\n"
458                                          (riece-identity-prefix target))))))
459
460 (defun riece-command-part (target &optional message)
461   (interactive
462    (progn
463      (riece-check-channel-commands-are-usable)
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 ((buffer (if (and riece-channel-buffer-mode
492                          riece-current-channel)
493                     riece-channel-buffer
494                   riece-dialogue-buffer)))
495     (if (get-buffer-window buffer)
496         (condition-case nil
497             (let ((other-window-scroll-buffer buffer))
498               (scroll-other-window-down lines))
499           (beginning-of-buffer
500            (message "Beginning of buffer"))))))
501
502 (defun riece-command-scroll-up (lines)
503   "Scroll LINES up dialogue buffer from command buffer."
504   (interactive "P")
505   (let ((buffer (if (and riece-channel-buffer-mode
506                          riece-current-channel)
507                     riece-channel-buffer
508                   riece-dialogue-buffer)))
509     (if (get-buffer-window buffer)
510         (condition-case nil
511             (let ((other-window-scroll-buffer buffer))
512               (scroll-other-window lines))
513           (end-of-buffer
514            (message "End of buffer"))))))
515
516 (defun riece-command-user-list-scroll-down (lines)
517   "Scroll LINES down user list buffer from command buffer."
518   (interactive "P")
519   (if (get-buffer-window riece-user-list-buffer)
520       (condition-case nil
521           (let ((other-window-scroll-buffer riece-user-list-buffer))
522             (scroll-other-window-down lines))
523         (beginning-of-buffer
524          (message "Beginning of buffer")))))
525
526 (defun riece-command-user-list-scroll-up (lines)
527   "Scroll LINES up user list buffer from command buffer."
528   (interactive "P")
529   (if (get-buffer-window riece-user-list-buffer)
530       (condition-case nil
531           (let ((other-window-scroll-buffer riece-user-list-buffer))
532             (scroll-other-window lines))
533         (end-of-buffer
534          (message "End of buffer")))))
535
536 (defun riece-command-toggle-away (&optional message)
537   "Mark yourself as being away."
538   (interactive
539    (if (and (not (riece-with-server-buffer (riece-identity-server
540                                             (riece-current-nickname))
541                    (riece-user-get-away (riece-identity-prefix
542                                          (riece-current-nickname)))))
543             current-prefix-arg)
544        (list (read-from-minibuffer
545               "Away message: " (cons (or riece-away-message "") 0)))))
546   (if (riece-with-server-buffer (riece-identity-server
547                                  (riece-current-nickname))
548         (riece-user-get-away (riece-identity-prefix
549                               (riece-current-nickname))))
550       (riece-send-string "AWAY\r\n")
551     (riece-send-string (format "AWAY :%s\r\n" (or message
552                                                   riece-away-message)))))
553
554 (defun riece-command-toggle-freeze (&optional arg)
555   "Prevent automatic scrolling of the dialogue window.
556 If prefix argument ARG is non-nil, toggle frozen status."
557   (interactive "P")
558   (with-current-buffer (if (and riece-channel-buffer-mode
559                                 riece-channel-buffer)
560                            riece-channel-buffer
561                          riece-dialogue-buffer)
562     (setq riece-freeze (if arg
563                            (< 0 (prefix-numeric-value arg))
564                          (not riece-freeze))))
565   (riece-update-status-indicators)
566   (force-mode-line-update t))
567
568 (defun riece-command-toggle-own-freeze (&optional arg)
569   "Prevent automatic scrolling of the dialogue window.
570 The difference from `riece-command-freeze' is that your messages are hidden.
571 If prefix argument ARG is non-nil, toggle frozen status."
572   (interactive "P")
573   (with-current-buffer (if (and riece-channel-buffer-mode
574                                 riece-channel-buffer)
575                            riece-channel-buffer
576                          riece-dialogue-buffer)
577     (if (if arg
578             (< 0 (prefix-numeric-value arg))
579           (not (eq riece-freeze 'own)))
580         (setq riece-freeze 'own)
581       (setq riece-freeze nil)))
582   (riece-update-status-indicators)
583   (force-mode-line-update t))
584
585 (eval-when-compile
586   (autoload 'riece-exit "riece"))
587 (defun riece-command-quit (&optional arg)
588   "Quit IRC."
589   (interactive "P")
590   (if (y-or-n-p "Really quit IRC? ")
591       (if riece-server-process-alist
592           (let ((message
593                  (if arg
594                      (read-string "Message: ")
595                    riece-quit-message))
596                 (alist riece-server-process-alist))
597             (while alist
598               (riece-quit-server-process (cdr (car alist)) message)
599               (setq alist (cdr alist))))
600         ;; If no server process is available, exit immediately.
601         (riece-exit))))
602
603 (defun riece-command-raw (command)
604   "Enter raw IRC command, which is sent to the server."
605   (interactive "sIRC command: ")
606   (riece-send-string (concat command "\r\n")))
607
608 (defun riece-command-end-of-buffer ()
609   "Get end of the dialogue buffer."
610   (interactive)
611   (let (buffer window)
612     (setq buffer (if riece-channel-buffer-mode
613                      riece-channel-buffer
614                    riece-dialogue-buffer))
615     (or (setq window (get-buffer-window buffer))
616         (setq window (get-buffer-window riece-dialogue-buffer)
617               buffer riece-dialogue-buffer))
618     (when window
619       (save-selected-window
620         (select-window window)
621         (goto-char (point-max))))))
622
623 (defun riece-command-copy-region (start end)
624   "Move current region between START and END to `kill-ring'."
625   (interactive "r")
626   (kill-new (buffer-substring-no-properties start end)))
627
628 (defun riece-command-open-server (server-name)
629   (interactive
630    (list (completing-read "Server: " riece-server-alist)))
631   (if (riece-server-process server-name)
632       (error "%s is already opened" server-name))
633   (riece-open-server
634    (riece-server-name-to-server server-name)
635    server-name))
636
637 (defun riece-command-close-server (server-name &optional message)
638   (interactive
639    (list (completing-read "Server: " riece-server-process-alist)
640          (if current-prefix-arg
641              (read-string "Message: ")
642            riece-quit-message)))
643   (riece-quit-server-process (riece-server-process server-name) message))
644
645 (defun riece-command-universal-server-name-argument ()
646   (interactive)
647   (let* ((riece-overriding-server-name
648           (completing-read "Server: " riece-server-process-alist))
649          (command
650           (key-binding (read-key-sequence
651                         (format "Command to execute on \"%s\":"
652                                 riece-overriding-server-name)))))
653     (message "")
654     (call-interactively command)))
655
656 (provide 'riece-commands)
657
658 ;;; riece-commands.el ends here