(riece-command-quit): Clean up if no server process is available.
[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-channel)
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-save-variables-are-dirty t)
122   (riece-command-configure-windows))
123
124 (defun riece-command-toggle-channel-buffer-mode ()
125   (interactive)
126   (setq riece-channel-buffer-mode
127         (not riece-channel-buffer-mode)
128         riece-save-variables-are-dirty t)
129   (riece-command-configure-windows))
130
131 (defun riece-command-toggle-user-list-buffer-mode ()
132   (interactive)
133   (setq riece-user-list-buffer-mode
134         (not riece-user-list-buffer-mode)
135         riece-save-variables-are-dirty t)
136   (riece-command-configure-windows))
137
138 (defun riece-command-toggle-channel-list-buffer-mode ()
139   (interactive)
140   (setq riece-channel-list-buffer-mode
141         (not riece-channel-list-buffer-mode)
142         riece-save-variables-are-dirty t)
143   (riece-command-configure-windows))
144
145 (defun riece-command-finger (user &optional recurse)
146   (interactive
147    (let* ((completion-ignore-case t)
148           (user (riece-completing-read-identity
149                  "User: "
150                  (riece-get-users-on-server (riece-current-server-name)))))
151      (list user current-prefix-arg)))
152   (if recurse
153       (riece-send-string (format "WHOIS %s %s\r\n"
154                                  (riece-identity-prefix user)
155                                  (riece-identity-prefix user)))
156     (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))
157
158 (defun riece-command-topic (topic)
159   (interactive
160    (progn
161      (riece-check-channel-commands-are-usable t)
162      (list (read-from-minibuffer
163             "Topic: " (cons (or (riece-with-server-buffer
164                                     (riece-identity-server
165                                      riece-current-channel)
166                                   (riece-channel-get-topic
167                                    (riece-identity-prefix
168                                     riece-current-channel)))
169                                 "")
170                             0)))))
171   (riece-send-string (format "TOPIC %s :%s\r\n"
172                              (riece-identity-prefix riece-current-channel)
173                              topic)))
174
175 (defun riece-command-invite (user)
176   (interactive
177    (let ((completion-ignore-case t))
178      (riece-check-channel-commands-are-usable t)
179      (list (riece-completing-read-identity
180             "User: "
181             (riece-get-users-on-server (riece-current-server-name))))))
182   (riece-send-string (format "INVITE %s %s\r\n"
183                              (riece-identity-prefix user)
184                              (riece-identity-prefix riece-current-channel))))
185
186 (defun riece-command-kick (user &optional message)
187   (interactive
188    (let ((completion-ignore-case t))
189      (riece-check-channel-commands-are-usable t)
190      (list (completing-read
191             "User: "
192             (riece-with-server-buffer
193                 (riece-identity-server riece-current-channel)
194               (riece-channel-get-users (riece-identity-prefix
195                                         riece-current-channel))))
196            (if current-prefix-arg
197                (read-string "Message: ")))))
198   (riece-send-string
199    (if message
200        (format "KICK %s %s :%s\r\n"
201                (riece-identity-prefix riece-current-channel)
202                user message)
203      (format "KICK %s %s\r\n"
204              (riece-identity-prefix riece-current-channel)
205              user))))
206
207 (defun riece-command-names (pattern)
208   (interactive
209    (let ((completion-ignore-case t))
210      (list (read-from-minibuffer
211             "Pattern: "
212             (if (and riece-current-channel
213                      (riece-channel-p (riece-identity-prefix
214                                        riece-current-channel)))
215                 (cons (riece-identity-prefix riece-current-channel)
216                       0))))))
217   (if (or (not (equal pattern ""))
218           (yes-or-no-p "Really want to query NAMES without argument? "))
219       (riece-send-string (format "NAMES %s\r\n" pattern))))
220
221 (defun riece-command-who (pattern)
222   (interactive
223    (let ((completion-ignore-case t))
224      (list (read-from-minibuffer
225             "Pattern: "
226             (if (and riece-current-channel
227                      (riece-channel-p (riece-identity-prefix
228                                        riece-current-channel)))
229                 (cons (riece-identity-prefix riece-current-channel)
230                       0))))))
231   (if (or (not (equal pattern ""))
232           (yes-or-no-p "Really want to query WHO without argument? "))
233       (riece-send-string (format "WHO %s\r\n" pattern))))
234
235 (defun riece-command-list (pattern)
236   (interactive
237    (let ((completion-ignore-case t))
238      (list (read-from-minibuffer
239             "Pattern: "
240             (if (and riece-current-channel
241                      (riece-channel-p (riece-identity-prefix
242                                        riece-current-channel)))
243                 (cons (riece-identity-prefix riece-current-channel)
244                       0))))))
245   (if (or (not (equal pattern ""))
246           (yes-or-no-p "Really want to query LIST without argument? "))
247       (riece-send-string (format "LIST %s\r\n" pattern))))
248
249 (defun riece-command-change-mode (channel change)
250   (interactive
251    (let* ((completion-ignore-case t)
252           (channel
253            (if current-prefix-arg
254                (riece-completing-read-identity
255                 "Channel/User: " riece-current-channels)
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-current-channels nil nil nil nil default))
432              (riece-completing-read-identity
433               "Channel/User: " riece-current-channels)))
434           key)
435      (if (and current-prefix-arg
436               (riece-channel-p (riece-identity-prefix target)))
437          (setq key
438                (riece-read-passwd (format "Key for %s: "
439                                           (riece-format-identity target)))))
440      (list target key)))
441   (let ((pointer (riece-identity-member target riece-current-channels)))
442     (if pointer
443         (riece-command-switch-to-channel (car pointer))
444       (if (riece-channel-p (riece-identity-prefix target))
445           (riece-command-join-channel target key)
446         (riece-command-join-partner target)))))
447
448 (defun riece-command-part-channel (target message)
449   (let ((process (riece-server-process (riece-identity-server target))))
450     (riece-process-send-string process
451                                (if message
452                                    (format "PART %s :%s\r\n"
453                                            (riece-identity-prefix target)
454                                            message)
455                                  (format "PART %s\r\n"
456                                          (riece-identity-prefix target))))))
457
458 (defun riece-command-part (target &optional message)
459   (interactive
460    (progn
461      (riece-check-channel-commands-are-usable)
462      (let* ((completion-ignore-case t)
463             (target
464              (riece-completing-read-identity
465               (format "Channel/User (default %s): "
466                       (riece-format-identity riece-current-channel))
467               riece-current-channels nil nil nil nil
468               (riece-format-identity riece-current-channel)))
469             message)
470        (if (and current-prefix-arg
471                 (riece-channel-p (riece-identity-prefix target)))
472            (setq message (read-string "Message: ")))
473        (list target message))))
474   (if (riece-identity-member target riece-current-channels)
475       (if (riece-channel-p (riece-identity-prefix target))
476           (riece-command-part-channel target message)
477         (riece-part-channel target)
478         (riece-redisplay-buffers))
479     (error "You are not talking with %s" target)))
480
481 (defun riece-command-change-nickname (nickname)
482   "Change your nickname to NICK."
483   (interactive "sEnter your nickname: ")
484   (riece-send-string (format "NICK %s\r\n" nickname)))
485
486 (defun riece-command-scroll-down (lines)
487   "Scroll LINES down dialogue buffer from command buffer."
488   (interactive "P")
489   (let ((buffer (if (and riece-channel-buffer-mode
490                          riece-current-channel)
491                     riece-channel-buffer
492                   riece-dialogue-buffer)))
493     (if (get-buffer-window buffer)
494         (condition-case nil
495             (let ((other-window-scroll-buffer buffer))
496               (scroll-other-window-down lines))
497           (beginning-of-buffer
498            (message "Beginning of buffer"))))))
499
500 (defun riece-command-scroll-up (lines)
501   "Scroll LINES up dialogue buffer from command buffer."
502   (interactive "P")
503   (let ((buffer (if (and riece-channel-buffer-mode
504                          riece-current-channel)
505                     riece-channel-buffer
506                   riece-dialogue-buffer)))
507     (if (get-buffer-window buffer)
508         (condition-case nil
509             (let ((other-window-scroll-buffer buffer))
510               (scroll-other-window lines))
511           (end-of-buffer
512            (message "End of buffer"))))))
513
514 (defun riece-command-user-list-scroll-down (lines)
515   "Scroll LINES down user list buffer from command buffer."
516   (interactive "P")
517   (if (get-buffer-window riece-user-list-buffer)
518       (condition-case nil
519           (let ((other-window-scroll-buffer riece-user-list-buffer))
520             (scroll-other-window-down lines))
521         (beginning-of-buffer
522          (message "Beginning of buffer")))))
523
524 (defun riece-command-user-list-scroll-up (lines)
525   "Scroll LINES up user list buffer from command buffer."
526   (interactive "P")
527   (if (get-buffer-window riece-user-list-buffer)
528       (condition-case nil
529           (let ((other-window-scroll-buffer riece-user-list-buffer))
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 (eval-when-compile
581   (autoload 'riece-exit "riece"))
582 (defun riece-command-quit (&optional arg)
583   "Quit IRC."
584   (interactive "P")
585   (if (y-or-n-p "Really quit IRC? ")
586       (if riece-server-process-alist
587           (let ((message
588                  (if arg
589                      (read-string "Message: ")
590                    (or riece-quit-message
591                        (riece-extended-version))))
592                 (alist riece-server-process-alist))
593             (while alist
594               (riece-quit-server-process (cdr (car alist)) message)
595               (setq alist (cdr alist))))
596         ;; If no server process is available, exit immediately.
597         (riece-exit))))
598
599 (defun riece-command-raw (command)
600   "Enter raw IRC command, which is sent to the server."
601   (interactive "sIRC command: ")
602   (riece-send-string (concat command "\r\n")))
603
604 (defun riece-command-end-of-buffer ()
605   "Get end of the dialogue buffer."
606   (interactive)
607   (let (buffer window)
608     (setq buffer (if riece-channel-buffer-mode
609                      riece-channel-buffer
610                    riece-dialogue-buffer))
611     (or (setq window (get-buffer-window buffer))
612         (setq window (get-buffer-window riece-dialogue-buffer)
613               buffer riece-dialogue-buffer))
614     (when window
615       (save-selected-window
616         (select-window window)
617         (goto-char (point-max))))))
618
619 (defun riece-command-copy-region (start end)
620   "Move current region between START and END to `kill-ring'."
621   (interactive "r")
622   (kill-new (buffer-substring-no-properties start end)))
623
624 (defun riece-command-open-server (server-name)
625   (interactive
626    (list (completing-read "Server: " riece-server-alist)))
627   (if (riece-server-process server-name)
628       (error "%s is already opened" server-name))
629   (riece-open-server
630    (riece-server-name-to-server server-name)
631    server-name))
632
633 (defun riece-command-close-server (server-name &optional message)
634   (interactive
635    (list (completing-read "Server: " riece-server-process-alist)
636          (if current-prefix-arg
637              (read-string "Message: ")
638            (or riece-quit-message
639                (riece-extended-version)))))
640   (riece-quit-server-process (riece-server-process server-name) message))
641
642 (defun riece-command-universal-server-name-argument ()
643   (interactive)
644   (let* ((riece-overriding-server-name
645           (completing-read "Server: " riece-server-process-alist))
646          (command
647           (key-binding (read-key-sequence
648                         (format "Command to execute on \"%s\":"
649                                 riece-overriding-server-name)))))
650     (message "")
651     (call-interactively command)))
652
653 (provide 'riece-commands)
654
655 ;;; riece-commands.el ends here