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