* riece-log.el (riece-log-get-directory): Support riece-identity
[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: "
256                 (riece-get-identities-on-server (riece-current-server-name)))
257              (riece-check-channel-commands-are-usable t)
258              riece-current-channel))
259           (riece-overriding-server-name (riece-identity-server channel))
260           (riece-temp-minibuffer-message
261            (concat "[Available modes: "
262                    (riece-with-server-buffer (riece-identity-server channel)
263                      (if (riece-channel-p (riece-identity-prefix channel))
264                          (if riece-supported-channel-modes
265                              (apply #'string riece-supported-channel-modes))
266                        (if riece-supported-user-modes
267                            (apply #'string riece-supported-user-modes))))
268                    "]")))
269      (list channel
270            (read-from-minibuffer
271             (concat (riece-concat-channel-modes
272                      channel "Mode (? for help)") ": ")
273             nil riece-minibuffer-map))))
274   (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
275                              change)))
276
277 (defun riece-command-set-operators (users &optional arg)
278   (interactive
279    (progn
280      (riece-check-channel-commands-are-usable t)
281      (let ((completion-ignore-case t))
282        (list (riece-completing-read-multiple
283               "Users"
284               (riece-with-server-buffer
285                   (riece-identity-server riece-current-channel)
286                 (riece-channel-get-users (riece-identity-prefix
287                                          riece-current-channel)))
288               (if current-prefix-arg
289                   (lambda (user)
290                     (memq ?o (cdr user)))
291                 (lambda (user)
292                   (not (memq ?o (cdr user))))))
293              current-prefix-arg))))
294   (let (group)
295     (while users
296       (setq group (cons (car users) group)
297             users (cdr users))
298       (when (or (= (length group) 3)
299                 (null users))
300         (riece-send-string
301          (format "MODE %s %c%s %s\r\n"
302                  (riece-identity-prefix riece-current-channel)
303                  (if current-prefix-arg
304                      ?-
305                    ?+)
306                  (make-string (length group) ?o)
307                  (mapconcat #'identity group " ")))
308         (setq group nil)))))
309
310 (defun riece-command-set-speakers (users &optional arg)
311   (interactive
312    (progn
313      (riece-check-channel-commands-are-usable t)
314      (let ((completion-ignore-case t))
315        (list (riece-completing-read-multiple
316               "Users"
317               (riece-with-server-buffer
318                   (riece-identity-server riece-current-channel)
319                 (riece-channel-get-users (riece-identity-prefix
320                                           riece-current-channel)))
321               (if current-prefix-arg
322                   (lambda (user)
323                     (memq ?v (cdr user)))
324                 (lambda (user)
325                   (not (memq ?v (cdr user))))))
326              current-prefix-arg))))
327   (let (group)
328     (while users
329       (setq group (cons (car users) group)
330             users (cdr users))
331       (when (or (= (length group) 3)
332                 (null users))
333         (riece-send-string
334          (format "MODE %s %c%s %s\r\n"
335                  (riece-identity-prefix riece-current-channel)
336                  (if current-prefix-arg
337                      ?-
338                    ?+)
339                  (make-string (length group) ?v)
340                  (mapconcat #'identity group " ")))
341         (setq group nil)))))
342
343 (defun riece-command-send-message (message notice)
344   "Send MESSAGE to the current channel."
345   (if (equal message "")
346       (error "No text to send"))
347   (riece-check-channel-commands-are-usable)
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-enter-message-to-user (user)
386   "Send the current line to USER."
387   (interactive
388    (let ((completion-ignore-case t))
389      (list (riece-completing-read-identity
390             "User: "
391             (riece-get-users-on-server (riece-current-server-name))))))
392   (let ((text (buffer-substring
393                (riece-line-beginning-position)
394                (riece-line-end-position))))
395     (riece-send-string
396      (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
397     (riece-display-message
398      (riece-make-message (riece-current-nickname) user text nil t)))
399   (let ((next-line-add-newlines t))
400     (next-line 1)))
401
402 (defun riece-command-join-channel (target key)
403   (let ((process (riece-server-process (riece-identity-server target))))
404     (unless process
405       (error "%s" (substitute-command-keys
406                    "Type \\[riece-command-open-server] to open server.")))
407     (riece-process-send-string process
408                                (if key
409                                    (format "JOIN %s :%s\r\n"
410                                            (riece-identity-prefix target)
411                                            key)
412                                  (format "JOIN %s\r\n"
413                                          (riece-identity-prefix target))))))
414
415 (defun riece-command-join-partner (target)
416   (let ((pointer (riece-identity-member target riece-current-channels)))
417     (if pointer
418         (riece-command-switch-to-channel (car pointer))
419       (riece-join-channel target)
420       (riece-switch-to-channel target)
421       (riece-redisplay-buffers))))
422
423 (defun riece-command-join (target &optional key)
424   (interactive
425    (let* ((completion-ignore-case t)
426           (target
427            (if riece-join-channel-candidate
428                (let ((default (riece-format-identity
429                                riece-join-channel-candidate)))
430                  (riece-completing-read-identity
431                   (format "Channel/User (default %s): " default)
432                   (riece-get-identities-on-server (riece-current-server-name))
433                   nil nil nil nil default))
434              (riece-completing-read-identity
435               "Channel/User: "
436               (riece-get-identities-on-server (riece-current-server-name)))))
437           key)
438      (if (and current-prefix-arg
439               (riece-channel-p (riece-identity-prefix target)))
440          (setq key
441                (riece-read-passwd (format "Key for %s: "
442                                           (riece-format-identity target)))))
443      (list target key)))
444   (let ((pointer (riece-identity-member target riece-current-channels)))
445     (if pointer
446         (riece-command-switch-to-channel (car pointer))
447       (if (riece-channel-p (riece-identity-prefix target))
448           (riece-command-join-channel target key)
449         (riece-command-join-partner target)))))
450
451 (defun riece-command-part-channel (target message)
452   (let ((process (riece-server-process (riece-identity-server target))))
453     (riece-process-send-string process
454                                (if message
455                                    (format "PART %s :%s\r\n"
456                                            (riece-identity-prefix target)
457                                            message)
458                                  (format "PART %s\r\n"
459                                          (riece-identity-prefix target))))))
460
461 (defun riece-command-part (target &optional message)
462   (interactive
463    (progn
464      (riece-check-channel-commands-are-usable)
465      (let* ((completion-ignore-case t)
466             (target
467              (riece-completing-read-identity
468               (format "Channel/User (default %s): "
469                       (riece-format-identity riece-current-channel))
470               riece-current-channels nil nil nil nil
471               (riece-format-identity riece-current-channel)))
472             message)
473        (if (and current-prefix-arg
474                 (riece-channel-p (riece-identity-prefix target)))
475            (setq message (read-string "Message: ")))
476        (list target message))))
477   (if (riece-identity-member target riece-current-channels)
478       (if (riece-channel-p (riece-identity-prefix target))
479           (riece-command-part-channel target message)
480         (riece-part-channel target)
481         (riece-redisplay-buffers))
482     (error "You are not talking with %s" target)))
483
484 (defun riece-command-change-nickname (nickname)
485   "Change your nickname to NICK."
486   (interactive "sEnter your nickname: ")
487   (riece-send-string (format "NICK %s\r\n" nickname)))
488
489 (defun riece-command-scroll-down (lines)
490   "Scroll LINES down dialogue buffer from command buffer."
491   (interactive "P")
492   (let ((buffer (if (and riece-channel-buffer-mode
493                          riece-current-channel)
494                     riece-channel-buffer
495                   riece-dialogue-buffer)))
496     (if (get-buffer-window buffer)
497         (condition-case nil
498             (let ((other-window-scroll-buffer buffer))
499               (scroll-other-window-down lines))
500           (beginning-of-buffer
501            (message "Beginning of buffer"))))))
502
503 (defun riece-command-scroll-up (lines)
504   "Scroll LINES up dialogue buffer from command buffer."
505   (interactive "P")
506   (let ((buffer (if (and riece-channel-buffer-mode
507                          riece-current-channel)
508                     riece-channel-buffer
509                   riece-dialogue-buffer)))
510     (if (get-buffer-window buffer)
511         (condition-case nil
512             (let ((other-window-scroll-buffer buffer))
513               (scroll-other-window lines))
514           (end-of-buffer
515            (message "End of buffer"))))))
516
517 (defun riece-command-user-list-scroll-down (lines)
518   "Scroll LINES down user list buffer from command buffer."
519   (interactive "P")
520   (if (get-buffer-window riece-user-list-buffer)
521       (condition-case nil
522           (let ((other-window-scroll-buffer riece-user-list-buffer))
523             (scroll-other-window-down lines))
524         (beginning-of-buffer
525          (message "Beginning of buffer")))))
526
527 (defun riece-command-user-list-scroll-up (lines)
528   "Scroll LINES up user list buffer from command buffer."
529   (interactive "P")
530   (if (get-buffer-window riece-user-list-buffer)
531       (condition-case nil
532           (let ((other-window-scroll-buffer riece-user-list-buffer))
533             (scroll-other-window lines))
534         (end-of-buffer
535          (message "End of buffer")))))
536
537 (defun riece-command-toggle-away (&optional message)
538   "Mark yourself as being away."
539   (interactive
540    (if (and (not (riece-with-server-buffer (riece-identity-server
541                                             (riece-current-nickname))
542                    (riece-user-get-away (riece-identity-prefix
543                                          (riece-current-nickname)))))
544             (or (null riece-away-message)
545                 current-prefix-arg))
546        (let ((message (read-string "Away message: ")))
547          (list message))))
548   (if message
549       (riece-send-string (format "AWAY :%s\r\n" message))
550     (riece-send-string "AWAY\r\n")))
551
552 (defun riece-command-toggle-freeze (&optional arg)
553   "Prevent automatic scrolling of the dialogue window.
554 If prefix argument ARG is non-nil, toggle frozen status."
555   (interactive "P")
556   (with-current-buffer (if (and riece-channel-buffer-mode
557                                 riece-channel-buffer)
558                            riece-channel-buffer
559                          riece-dialogue-buffer)
560     (setq riece-freeze (if arg
561                            (< 0 (prefix-numeric-value arg))
562                          (not riece-freeze))))
563   (riece-update-status-indicators)
564   (force-mode-line-update t))
565
566 (defun riece-command-toggle-own-freeze (&optional arg)
567   "Prevent automatic scrolling of the dialogue window.
568 The difference from `riece-command-freeze' is that your messages are hidden.
569 If prefix argument ARG is non-nil, toggle frozen status."
570   (interactive "P")
571   (with-current-buffer (if (and riece-channel-buffer-mode
572                                 riece-channel-buffer)
573                            riece-channel-buffer
574                          riece-dialogue-buffer)
575     (if (if arg
576             (< 0 (prefix-numeric-value arg))
577           (not (eq riece-freeze 'own)))
578         (setq riece-freeze 'own)
579       (setq riece-freeze nil)))
580   (riece-update-status-indicators)
581   (force-mode-line-update t))
582
583 (eval-when-compile
584   (autoload 'riece-exit "riece"))
585 (defun riece-command-quit (&optional arg)
586   "Quit IRC."
587   (interactive "P")
588   (if (y-or-n-p "Really quit IRC? ")
589       (if riece-server-process-alist
590           (let ((message
591                  (if arg
592                      (read-string "Message: ")
593                    (or riece-quit-message
594                        (riece-extended-version))))
595                 (alist riece-server-process-alist))
596             (while alist
597               (riece-quit-server-process (cdr (car alist)) message)
598               (setq alist (cdr alist))))
599         ;; If no server process is available, exit immediately.
600         (riece-exit))))
601
602 (defun riece-command-raw (command)
603   "Enter raw IRC command, which is sent to the server."
604   (interactive "sIRC command: ")
605   (riece-send-string (concat command "\r\n")))
606
607 (defun riece-command-end-of-buffer ()
608   "Get end of the dialogue buffer."
609   (interactive)
610   (let (buffer window)
611     (setq buffer (if riece-channel-buffer-mode
612                      riece-channel-buffer
613                    riece-dialogue-buffer))
614     (or (setq window (get-buffer-window buffer))
615         (setq window (get-buffer-window riece-dialogue-buffer)
616               buffer riece-dialogue-buffer))
617     (when window
618       (save-selected-window
619         (select-window window)
620         (goto-char (point-max))))))
621
622 (defun riece-command-copy-region (start end)
623   "Move current region between START and END to `kill-ring'."
624   (interactive "r")
625   (kill-new (buffer-substring-no-properties start end)))
626
627 (defun riece-command-open-server (server-name)
628   (interactive
629    (list (completing-read "Server: " riece-server-alist)))
630   (if (riece-server-process server-name)
631       (error "%s is already opened" server-name))
632   (riece-open-server
633    (riece-server-name-to-server server-name)
634    server-name))
635
636 (defun riece-command-close-server (server-name &optional message)
637   (interactive
638    (list (completing-read "Server: " riece-server-process-alist)
639          (if current-prefix-arg
640              (read-string "Message: ")
641            (or riece-quit-message
642                (riece-extended-version)))))
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