* riece-doctor.el: New add-on.
[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   (riece-switch-to-channel channel)
41   (riece-command-configure-windows))
42
43 (defun riece-command-switch-to-channel-by-number (number)
44   (interactive
45    (let ((command-name (symbol-name this-command)))
46      (if (string-match "[0-9]+$" command-name)
47          (list (string-to-number (match-string 0 command-name)))
48        (list (string-to-number (read-string "Number: "))))))
49   (let ((channels riece-current-channels)
50         (index 1))
51     (while (and channels
52                 (< index number))
53       (if (car channels)
54           (setq index (1+ index)))
55       (setq channels (cdr channels)))
56     (if (car channels)
57         (riece-command-switch-to-channel (car channels))
58       (error "No such number!"))))
59         
60 (eval-and-compile
61   (let ((number 1))
62     (while (<= number 20)
63       (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
64                                 (number-to-string number)))
65         'riece-command-switch-to-channel-by-number)
66       (setq number (1+ number)))))
67
68 (defun riece-command-next-channel ()
69   "Select the next channel."
70   (interactive)
71   (when (> (length riece-current-channels) 1)
72     (let ((pointer (cdr (riece-identity-member
73                          riece-current-channel
74                          riece-current-channels))))
75       (while (and pointer
76                   (null (car pointer)))
77         (setq pointer (cdr pointer)))
78       (when (null pointer)
79         (setq pointer riece-current-channels)
80         (while (and pointer
81                     (null (car pointer)))
82           (setq pointer (cdr pointer))))
83       (if (car pointer)
84           (riece-command-switch-to-channel (car pointer))
85         (error "No such channel!")))))
86
87 (defun riece-command-previous-channel ()
88   "Select the previous channel."
89   (interactive)
90   (when (> (length riece-current-channels) 1)
91     (let ((pointer (riece-identity-member
92                     riece-current-channel
93                     riece-current-channels))
94           (start riece-current-channels)
95           channel)
96       (while (and start (not (eq start pointer)))
97         (setq channel (car start))
98         (setq start (cdr start)))
99       (when (null channel)
100         (setq start (copy-sequence riece-current-channels))
101         (setq start (delq nil start))
102         (and (> (length start) 1)
103              (setq channel (nth (1- (length start)) start))))
104       (if channel
105           (riece-command-switch-to-channel channel)
106         (error "No such channel!")))))
107
108 (defun riece-command-select-command-buffer ()
109   "Select the command buffer."
110   (interactive)
111   (let ((window (get-buffer-window riece-command-buffer)))
112     (if window
113         (select-window window))))
114
115 (defun riece-command-configure-windows ()
116   (interactive)
117   (riece-redisplay-buffers t))
118
119 (defun riece-command-toggle-channel-buffer-mode ()
120   (interactive)
121   (setq riece-channel-buffer-mode
122         (not riece-channel-buffer-mode)
123         riece-save-variables-are-dirty t)
124   (riece-command-configure-windows))
125
126 (defun riece-command-toggle-user-list-buffer-mode ()
127   (interactive)
128   (setq riece-user-list-buffer-mode
129         (not riece-user-list-buffer-mode)
130         riece-save-variables-are-dirty t)
131   (riece-command-configure-windows))
132
133 (defun riece-command-toggle-channel-list-buffer-mode ()
134   (interactive)
135   (setq riece-channel-list-buffer-mode
136         (not riece-channel-list-buffer-mode)
137         riece-save-variables-are-dirty t)
138   (riece-command-configure-windows))
139
140 (defun riece-command-finger (user &optional recurse)
141   (interactive
142    (let* ((completion-ignore-case t)
143           (user (completing-read
144                  "User: "
145                  (mapcar #'list (riece-get-users-on-server)))))
146      (list user current-prefix-arg)))
147   (if recurse
148       (riece-send-string (format "WHOIS %s %s\r\n" user user))
149     (riece-send-string (format "WHOIS %s\r\n" user))))
150
151 (defun riece-command-topic (topic)
152   (interactive
153    (list (read-from-minibuffer
154           "Topic: " (cons (or (riece-with-identity-buffer riece-current-channel
155                                 (riece-channel-get-topic
156                                  (riece-identity-prefix
157                                   riece-current-channel)))
158                               "")
159                           0))))
160   (riece-send-string (format "TOPIC %s :%s\r\n"
161                              (riece-identity-prefix riece-current-channel)
162                              topic)))
163
164 (defun riece-command-invite (user)
165   (interactive
166    (let ((completion-ignore-case t))
167      (unless (and riece-current-channel
168                   (riece-channel-p (riece-identity-prefix
169                                     riece-current-channel)))
170        (error "Not on a channel"))
171      (list (completing-read
172             "User: "
173             (mapcar #'list (riece-get-users-on-server))))))
174   (riece-send-string (format "INVITE %s %s\r\n"
175                              user (riece-identity-prefix
176                                    riece-current-channel))))
177
178 (defun riece-command-kick (user &optional message)
179   (interactive
180    (let ((completion-ignore-case t))
181      (unless (and riece-current-channel
182                   (riece-channel-p (riece-identity-prefix
183                                     riece-current-channel)))
184        (error "Not on a channel"))
185      (list (completing-read
186             "User: "
187             (mapcar #'list (riece-channel-get-users
188                             riece-current-channel)))
189            (if current-prefix-arg
190                (read-string "Message: ")))))
191   (riece-send-string
192    (if message
193        (format "KICK %s %s :%s\r\n"
194                (riece-identity-prefix riece-current-channel)
195                user message)
196      (format "KICK %s %s\r\n"
197              (riece-identity-prefix riece-current-channel)
198              user))))
199
200 (defun riece-command-names (pattern)
201   (interactive
202    (let ((completion-ignore-case t))
203      (list (read-from-minibuffer
204             "Pattern: "
205             (if (and riece-current-channel
206                      (riece-channel-p (riece-identity-prefix
207                                        riece-current-channel)))
208                 (cons (riece-decode-identity riece-current-channel t)
209                       0))))))
210   (if (or (not (equal pattern ""))
211           (yes-or-no-p "Really want to query NAMES without argument? "))
212       (riece-send-string (format "NAMES %s\r\n" pattern))))
213
214 (defun riece-command-who (pattern)
215   (interactive
216    (let ((completion-ignore-case t))
217      (list (read-from-minibuffer
218             "Pattern: "
219             (if (and riece-current-channel
220                      (riece-channel-p (riece-identity-prefix
221                                        riece-current-channel)))
222                 (cons (riece-decode-identity riece-current-channel t)
223                       0))))))
224   (if (or (not (equal pattern ""))
225           (yes-or-no-p "Really want to query WHO without argument? "))
226       (riece-send-string (format "WHO %s\r\n" pattern))))
227
228 (defun riece-command-list (pattern)
229   (interactive
230    (let ((completion-ignore-case t))
231      (list (read-from-minibuffer
232             "Pattern: "
233             (if (and riece-current-channel
234                      (riece-channel-p (riece-identity-prefix
235                                        riece-current-channel)))
236                 (cons (riece-decode-identity riece-current-channel t)
237                       0))))))
238   (if (or (not (equal pattern ""))
239           (yes-or-no-p "Really want to query LIST without argument? "))
240       (riece-send-string (format "LIST %s\r\n" pattern))))
241
242 (defun riece-command-change-mode (channel change)
243   (interactive
244    (let* ((completion-ignore-case t)
245           (channel
246            (if current-prefix-arg
247                (riece-completing-read-identity
248                 "Channel/User: " riece-current-channels)
249              riece-current-channel))
250           (riece-overriding-server-name (riece-identity-server channel))
251           (riece-temp-minibuffer-message
252            (concat "[Available modes: "
253                    (riece-with-server-buffer (riece-identity-server channel)
254                      (if (riece-channel-p (riece-identity-prefix channel))
255                          (if riece-supported-channel-modes
256                              (apply #'string riece-supported-channel-modes))
257                        (if riece-supported-user-modes
258                            (apply #'string riece-supported-user-modes))))
259                    "]")))
260      (list channel
261            (read-from-minibuffer
262             (concat (riece-concat-channel-modes
263                      channel "Mode (? for help)") ": ")
264             nil riece-minibuffer-map))))
265   (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
266                              change)))
267
268 (defun riece-command-set-operators (users &optional arg)
269   (interactive
270    (let ((operators
271           (riece-with-identity-buffer riece-current-channel
272             (riece-channel-get-operators
273              (riece-identity-prefix riece-current-channel))))
274          (completion-ignore-case t)
275          users)
276      (if current-prefix-arg
277          (setq users (riece-completing-read-multiple
278                       "Users"
279                       (mapcar #'list operators)))
280        (setq users (riece-completing-read-multiple
281                     "Users"
282                     (delq nil (mapcar
283                                (lambda (user)
284                                  (unless (member user operators)
285                                    (list user)))
286                                (riece-with-identity-buffer
287                                    riece-current-channel
288                                  (riece-channel-get-users
289                                   (riece-identity-prefix
290                                    riece-current-channel))))))))
291      (list users current-prefix-arg)))
292   (let (group)
293     (while users
294       (setq group (cons (car users) group)
295             users (cdr users))
296       (if (or (= (length group) 3)
297               (null users))
298           (riece-send-string
299            (format "MODE %s %c%s %s\r\n"
300                    (riece-identity-prefix riece-current-channel)
301                    (if current-prefix-arg
302                        ?-
303                      ?+)
304                    (make-string (length group) ?o)
305                    (mapconcat #'identity group " ")))))))
306
307 (defun riece-command-set-speakers (users &optional arg)
308   (interactive
309    (let ((speakers
310           (riece-with-identity-buffer riece-current-channel
311             (riece-channel-get-speakers
312              (riece-identity-prefix riece-current-channel))))
313          (completion-ignore-case t)
314          users)
315      (if current-prefix-arg
316          (setq users (riece-completing-read-multiple
317                       "Users"
318                       (mapcar #'list speakers)))
319        (setq users (riece-completing-read-multiple
320                     "Users"
321                     (delq nil (mapcar
322                                (lambda (user)
323                                  (unless (member user speakers)
324                                    (list user)))
325                                (riece-with-identity-buffer
326                                    riece-current-channel
327                                  (riece-channel-get-users
328                                   (riece-identity-prefix
329                                    riece-current-channel))))))))
330      (list users current-prefix-arg)))
331   (let (group)
332     (while users
333       (setq group (cons (car users) group)
334             users (cdr users))
335       (if (or (= (length group) 3)
336               (null users))
337           (riece-send-string
338            (format "MODE %s %c%s %s\r\n"
339                    (riece-identity-prefix riece-current-channel)
340                    (if current-prefix-arg
341                        ?-
342                      ?+)
343                    (make-string (length group) ?v)
344                    (mapconcat #'identity group " ")))))))
345
346 (defun riece-command-send-message (message notice)
347   "Send MESSAGE to the current channel."
348   (if (equal message "")
349       (error "No text to send"))
350   (unless riece-current-channel
351     (error (substitute-command-keys
352             "Type \\[riece-command-join] to join a channel")))
353   (if notice
354       (progn
355         (riece-send-string
356          (format "NOTICE %s :%s\r\n"
357                  (riece-identity-prefix riece-current-channel)
358                  message))
359         (riece-own-channel-message message riece-current-channel 'notice))
360     (riece-send-string
361      (format "PRIVMSG %s :%s\r\n"
362              (riece-identity-prefix riece-current-channel)
363              message))
364     (riece-own-channel-message message)))
365
366 (defun riece-command-enter-message ()
367   "Send the current line to the current channel."
368   (interactive)
369   (riece-command-send-message (buffer-substring
370                                (riece-line-beginning-position)
371                                (riece-line-end-position))
372                               nil)
373   (let ((next-line-add-newlines t))
374     (next-line 1)))
375
376 (defun riece-command-enter-message-as-notice ()
377   "Send the current line to the current channel as NOTICE."
378   (interactive)
379   (riece-command-send-message (buffer-substring
380                                (riece-line-beginning-position)
381                                (riece-line-end-position))
382                               t)
383   (let ((next-line-add-newlines t))
384     (next-line 1)))
385
386 (defun riece-command-join-channel (target key)
387   (let ((process (riece-server-process (riece-identity-server target))))
388     (unless process
389       (error "%s" (substitute-command-keys
390                    "Type \\[riece-command-open-server] to open server.")))
391     (riece-process-send-string process
392                                (if key
393                                    (format "JOIN %s :%s\r\n"
394                                            (riece-identity-prefix target)
395                                            key)
396                                  (format "JOIN %s\r\n"
397                                          (riece-identity-prefix target))))))
398
399 (defun riece-command-join-partner (target)
400   (let ((pointer (riece-identity-member target riece-current-channels)))
401     (if pointer
402         (riece-command-switch-to-channel (car pointer))
403       (riece-join-channel target)
404       (riece-switch-to-channel target)
405       (riece-redisplay-buffers))))
406
407 (defun riece-command-join (target &optional key)
408   (interactive
409    (let ((completion-ignore-case t)
410          (target
411           (riece-completing-read-identity
412            "Channel/User: " riece-current-channels))
413          key)
414      (if (and current-prefix-arg
415               (riece-channel-p target))
416          (setq key
417                (riece-read-passwd (format "Key for %s: " target))))
418      (list target key)))
419   (let ((pointer (riece-identity-member target riece-current-channels)))
420     (if pointer
421         (riece-command-switch-to-channel (car pointer))
422       (if (riece-channel-p (riece-identity-prefix target))
423           (riece-command-join-channel target key)
424         (riece-command-join-partner target)))))
425
426 (defun riece-command-part-channel (target message)
427   (let ((process (riece-server-process (riece-identity-server target))))
428     (unless process
429       (error "%s" (substitute-command-keys
430                    "Type \\[riece-command-open-server] to open server.")))
431     (riece-process-send-string process
432                                (if message
433                                    (format "PART %s :%s\r\n"
434                                            (riece-identity-prefix target)
435                                            message)
436                                  (format "PART %s\r\n"
437                                          (riece-identity-prefix target))))))
438
439 (defun riece-command-part (target &optional message)
440   (interactive
441    (let ((completion-ignore-case t)
442          (target
443           (riece-completing-read-identity
444            "Channel/User: " riece-current-channels))
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                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                 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