* liece-crypt.el: Remove.
[elisp/liece.git] / lisp / liece-handle.el
1 ;;; liece-handle.el --- implementation of IRC message handlers
2 ;; Copyright (C) 1998-2000 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Revised: 1998-11-25
7 ;; Keywords: IRC, liece
8
9 ;; This file is part of Liece.
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26
27 ;;; Commentary:
28 ;; 
29
30 ;;; Code:
31
32 (eval-when-compile
33   (require 'liece-inlines)
34   (require 'liece-misc)
35   (require 'liece-intl))
36
37 (require 'liece-message)
38 (require 'liece-filter)
39
40 (require 'liece-handler)
41
42 (defmacro liece-handle-prepare-comment (rest &optional quote)
43   `(if (zerop (length ,rest))
44        ""
45      (if ,quote
46          (regexp-quote (format " (%s)" ,rest))
47        (format " (%s)" ,rest))))
48
49 (defmacro liece-handle-message-check-empty (msg)
50   `(string= ,msg ""))
51
52 (defmacro liece-handle-message-check-ignored (prefix rest)
53   `(and ,prefix
54         (liece-ignore-this-p ,prefix liece-user-at-host)
55         (liece-message-from-ignored ,prefix ,rest)))
56
57 (defmacro liece-handle-check-changes-ignored ()
58   'liece-ignore-changes)
59
60 (defconst liece-handle-ctcp-message-regexp "\001\\(.*\\)\001")
61
62 (defmacro liece-handle-ctcp-message-p (msg)
63   `(string-match liece-handle-ctcp-message-regexp ,msg))
64
65 (autoload 'liece-ctcp-message "liece-ctcp")
66 (autoload 'liece-ctcp-notice "liece-ctcp")
67
68 \f
69 (liece-handler-define-backend "generic")
70
71 (mapcar
72  (lambda (message)
73    (liece-handler-define-function
74     message '(prefix rest "generic")
75     (intern (format "liece-handle-%s-message" message)))
76    (defvar ,(intern (format "liece-%s-hook" message)) nil)
77    (defvar ,(intern (format "liece-after-%s-hook" message)) nil))
78  '("nick" "notice" "privmsg" "ping" "wall" "wallops" "quit" "topic"
79    "mode" "kick" "invite" "kill" "join" "part" "silence"))
80
81 (defun* liece-handle-nick-message (prefix rest)
82   (let ((chnls (liece-nick-get-joined-channels prefix)))
83     (liece-nick-change prefix rest)
84     (cond
85      ((liece-nick-equal prefix liece-real-nickname)
86       (setq liece-nickname-last liece-real-nickname
87             liece-real-nickname rest))
88      ((liece-nick-member prefix liece-current-chat-partners)
89       (setq liece-current-chat-partners
90             (string-list-modify-ignore-case (list (cons prefix rest))
91                                             liece-current-chat-partners))
92       (setcar (string-assoc-ignore-case prefix liece-nick-buffer-alist)
93               rest)
94       (setcar (string-assoc-ignore-case prefix liece-channel-buffer-alist)
95               rest)
96       (if (liece-nick-equal prefix liece-current-chat-partner)
97           (setq liece-current-chat-partner rest))
98       (add-to-list 'chnls rest)
99       (liece-channel-change)))
100     (if (liece-handle-check-changes-ignored)
101         (return-from liece-handle-nick-message))
102     (liece-insert-change (append (liece-pick-buffer chnls)
103                                   liece-D-buffer liece-O-buffer)
104                           (format (_ "%s is now known as %s\n") prefix rest))))
105
106 (defun* liece-handle-notice-message (prefix rest)
107   (if (liece-handle-message-check-ignored prefix rest)
108       (return-from liece-handle-notice-message))
109   (or liece-ignore-extra-notices
110         prefix
111         (string-match "as being away" rest)
112         (return-from liece-handle-notice-message))
113
114   ;; No prefix. This is a server notice.
115   (when (and (null prefix) (string-match "^[^ ]* +:?" rest))
116     (liece-insert-notice (append liece-D-buffer liece-O-buffer)
117                           (concat (substring rest (match-end 0)) "\n"))
118     (return-from liece-handle-notice-message))
119   (if (run-hook-with-args-until-success 'liece-notice-cleartext-hook
120                                         prefix rest)
121       (return-from liece-handle-notice-message))
122     
123   (multiple-value-bind (chnl temp) (liece-split-line rest)
124     ;; This is a ctcp reply but contains additional messages
125     ;; at the left or/and right side.
126     (if (liece-handle-ctcp-message-p temp)
127         (setq temp (liece-ctcp-notice prefix temp)))
128     (if (liece-handle-message-check-empty temp)
129         (return-from liece-handle-notice-message))
130
131     ;; Normal message via notice.
132     (setq chnl (liece-channel-virtual chnl))
133     (let ((liece-message-target chnl)
134           (liece-message-speaker prefix)
135           (liece-message-type 'notice))
136       (liece-display-message temp))))
137
138 (defun* liece-handle-privmsg-message (prefix rest)
139   (if (liece-handle-message-check-ignored prefix rest)
140       (return-from liece-handle-privmsg-message))
141   (if (run-hook-with-args-until-success 'liece-privmsg-cleartext-hook
142                                         prefix rest)
143       (return-from liece-handle-privmsg-message))
144
145   (multiple-value-bind (chnl temp) (liece-split-line rest)
146     (setq temp (or temp ""))
147     ;; This is a ctcp request but contains additional messages
148     ;; at the left or/and right side.
149     (if (liece-handle-ctcp-message-p temp)
150         (setq temp (liece-ctcp-message prefix chnl temp)))
151     (if (liece-handle-message-check-empty temp)
152         (return-from liece-handle-privmsg-message))
153
154     (setq chnl (liece-channel-virtual chnl))
155       
156     (when liece-beep-on-bells
157       (if (string-match "\007" rest)
158           (liece-beep))
159       (if (liece-nick-equal chnl liece-real-nickname)
160           (and liece-beep-when-privmsg (liece-beep))
161         (with-current-buffer (if liece-channel-buffer-mode
162                                  (liece-pick-buffer-1 chnl)
163                                liece-dialogue-buffer)
164           (if liece-beep
165               (liece-beep))))
166       (dolist (word liece-beep-words-list)
167         (if (string-match word rest)
168             (liece-beep))))
169
170     ;; Append timestamp if we are being away.
171     (if (and (string-equal "A" liece-away-indicator)
172              (liece-nick-equal chnl liece-real-nickname))
173         (setq temp
174               (concat temp " ("
175                       (funcall liece-format-time-function (current-time))
176                       ")")))
177
178     ;; Normal message.
179     (let ((liece-message-target chnl)
180           (liece-message-speaker prefix)
181           (liece-message-type 'privmsg))
182       (liece-display-message temp))
183
184     ;; Append to the unread list.
185     (let ((item (if (eq liece-command-buffer 'chat)
186                     liece-current-chat-partner
187                   liece-current-channel)))
188       (unless (liece-channel-equal chnl item)
189         (if (liece-channel-unread-p chnl)
190             (setq liece-channel-unread-list
191                   (delete chnl liece-channel-unread-list)))
192         (setq liece-channel-unread-list
193               (cons chnl liece-channel-unread-list))))
194
195     (if (and (liece-nick-equal chnl liece-real-nickname)
196              (not (liece-nick-equal prefix liece-current-chat-partner)))
197         (liece-message (_ "A private message has arrived from %s")
198                        prefix))))
199
200 (defun liece-handle-ping-message (prefix rest)
201   (liece-send "PONG :%s" rest)
202   (liece-command-timestamp-if-interval-expired t)
203   (liece-maybe-poll))
204
205 (defun liece-handle-wall-message (prefix rest)
206   (liece-insert-broadcast (append liece-D-buffer liece-O-buffer)
207                            (concat (if prefix (concat "from " prefix) "") " "
208                                    rest "\n")))
209
210 (defun liece-handle-wallops-message (prefix rest)
211   (if liece-show-wallops
212       (liece-insert-wallops (append liece-D-buffer liece-O-buffer)
213                              (concat (if prefix prefix "UNKNOWN")
214                                      ": " rest "\n")))
215   (liece-insert-wallops liece-W-buffer
216                          (concat (if prefix (concat "from " prefix) "") " "
217                                  rest "\n")))
218
219 (defun* liece-handle-quit-message (prefix rest)
220   (let ((chnls (liece-nick-get-joined-channels prefix)) text match default)
221     ;; Mark temporary apart, if quitting user is one of our chat partners.
222     (when (liece-nick-member prefix liece-current-chat-partners)
223       (add-to-list 'chnls prefix)
224       (liece-nick-mark-as-apart prefix))
225     (if (liece-handle-check-changes-ignored)
226         (return-from liece-handle-quit-message))
227     (cond
228      (liece-compress-changes
229       (setq text (format (_ " \\(has\\|have\\) left IRC%s")
230                          (liece-handle-prepare-comment rest t))
231             match (format "^%s%s.*%s$"
232                           (if liece-display-time
233                               liece-time-prefix-regexp "")
234                           (regexp-quote liece-change-prefix)
235                           (regexp-quote text))
236             default (format (_ "%s%s has left IRC%s\n")
237                             liece-change-prefix prefix
238                             (liece-handle-prepare-comment rest)))
239       (liece-replace (append (liece-pick-buffer chnls)
240                               liece-D-buffer liece-O-buffer)
241                       match default text
242                       (format (_ ", %s have left IRC%s")
243                               prefix (liece-handle-prepare-comment rest))))
244      (t
245       (liece-insert-change (append (liece-pick-buffer chnls)
246                                     liece-D-buffer liece-O-buffer)
247                             (format (_ "%s has left IRC%s\n")
248                                     (liece-handle-prepare-comment rest)))))
249     (liece-nick-change prefix nil)))
250
251 (defun* liece-handle-topic-message (prefix rest)
252   (multiple-value-bind (chnl topic) (liece-split-line rest)
253     (setq chnl (liece-channel-virtual chnl)
254           topic (or topic ""))
255     (liece-channel-set-topic topic chnl)
256     (if (liece-handle-check-changes-ignored)
257         (return-from liece-handle-topic-message))
258     (liece-insert-change (liece-pick-buffer chnl)
259                           (format (_ "New topic on channel %s set by %s: %s\n")
260                                   chnl prefix topic))
261     (liece-insert-change (if (liece-nick-equal chnl liece-current-channel)
262                               liece-D-buffer
263                             (append liece-D-buffer liece-O-buffer))
264                           (format (_ "New topic on channel %s set by %s: %s\n")
265                                   chnl prefix topic))
266     (liece-set-channel-indicator)))
267
268 (defun* liece-handle-mode-message (prefix rest)
269   (if (liece-handle-check-changes-ignored)
270       (return-from liece-handle-mode-message))
271   (let ((chnl " ") (str "") mflag mflags marg margs val md chnlp)
272     (or (and (string-match "\\([^ ]*\\) +:?" rest)
273              (setq chnl (match-string 1 rest)
274                    str (substring rest (match-end 0))
275                    chnlp (liece-channel-p chnl)
276                    str (if (= (aref str (1- (length str))) ? )
277                            (substring str 0 -1) str)
278                    chnl (liece-channel-virtual chnl)))
279         (and (string-match " +:" rest)
280              (setq str (substring rest (match-end 0))))
281         (return-from liece-handle-mode-message))
282
283     ;; parse modes
284     (when (string-match "\\([^ ]*\\) +" str)
285       (setq mflag (match-string 1 str)
286             marg (substring str (match-end 0))
287             mflags (string-to-char-list mflag))
288       (while (string-match "^\\([^ ]*\\) +" marg)
289         (setq margs (cons (match-string 1 marg) margs)
290               marg (substring marg (match-end 0))))
291       (or (string= marg "") (setq margs (cons marg margs)))
292       (while (setq md (pop mflags))
293         (cond ((eq ?- md) (setq val nil))
294               ((eq ?+ md) (setq val t))
295               ((eq ?o md) (liece-channel-set-operator chnl (pop margs) val))
296               ((eq ?v md) (liece-channel-set-voice chnl (pop margs) val))
297               ((eq ?b md) (liece-channel-set-ban chnl (pop margs) val))
298               ((eq ?e md) (liece-channel-set-exception chnl (pop margs) val))
299               ((eq ?I md) (liece-channel-set-invite chnl (pop margs) val))
300               (chnlp (liece-channel-set-mode val md chnl))
301               (t (liece-nick-set-mode val md chnl)))))
302     
303     (liece-set-channel-indicator)
304     (cond
305      (liece-compress-changes
306       (let* ((text (concat (regexp-quote rest) "\n"))
307              (match (format (_ "^%s%sNew mode for %s set by %s: ")
308                             (if liece-display-time
309                                 liece-time-prefix-regexp "")
310                             (regexp-quote liece-change-prefix)
311                             (regexp-quote chnl) (regexp-quote prefix)))
312              (default (format (_ "%sNew mode for %s set by %s: %s\n")
313                               liece-change-prefix chnl prefix str)))
314         (liece-replace (liece-pick-buffer chnl)
315                         match default text (concat ", " str "\n"))
316         (liece-replace (if (and liece-current-channel
317                                  (liece-channel-equal
318                                   chnl liece-current-channel))
319                             liece-D-buffer
320                           (append liece-D-buffer liece-O-buffer))
321                         match default text (concat ", " str "\n"))))
322      (t
323       (liece-insert-change (liece-pick-buffer chnl)
324                             (format (_ "New mode for %s set by %s: %s\n")
325                                     chnl prefix str))
326       (liece-insert-change (if (and liece-current-channel
327                                      (liece-channel-equal
328                                       chnl liece-current-channel))
329                                 liece-D-buffer
330                               (append liece-D-buffer liece-O-buffer))
331                             (format (_ "New mode for %s set by %s: %s\n")
332                                     chnl prefix str))))))
333
334 (defun* liece-handle-kick-message (prefix rest)
335   (if (/= 3 (length (setq rest (liece-split-line rest))))
336       (return-from liece-handle-kick-message))
337   (multiple-value-bind (chnl nick message) rest
338     (setq chnl (liece-channel-virtual chnl))
339     
340     (if (liece-nick-equal nick liece-real-nickname)
341         (progn
342           (liece-insert-change
343            (liece-pick-buffer chnl)
344            (format (_ "You were kicked off channel %s by %s (%s).\n")
345                    chnl prefix message))
346           (liece-channel-part chnl))
347       (liece-nick-part nick chnl))
348     
349     (if (liece-handle-check-changes-ignored)
350         (return-from liece-handle-kick-message))
351
352     (liece-insert-change
353      (append (liece-pick-buffer chnl)
354              (if (liece-channel-equal chnl liece-current-channel)
355                  liece-D-buffer
356                (append liece-D-buffer liece-O-buffer)))
357      (format "%s has kicked %s out%s%s\n"
358              prefix nick
359              (if (string= (or liece-current-channel "") chnl)
360                  ""
361                (format " from channel %s" chnl))
362              (if (not message)
363                  ""
364                (format " (%s)" message))))))
365
366 (defun* liece-handle-invite-message (prefix rest)
367   (or (string-match " +:" rest)
368       (return-from liece-handle-invite-message))
369   (and liece-beep-when-invited liece-beep-on-bells
370        (liece-beep))
371   (let ((chnl (liece-channel-virtual (substring rest (match-end 0)))))
372     (liece-insert-info (append liece-D-buffer liece-O-buffer)
373                         (format "%s invites you to channel %s\n"
374                                 prefix chnl))
375     (setq liece-default-channel-candidate chnl)))
376
377 (defun* liece-handle-kill-message (prefix rest)
378   (or (string-match " +:" rest)
379       (return-from liece-handle-kill-message))
380   (let ((path (substring rest (match-end 0))))
381     (liece-insert-info (append liece-D-buffer liece-O-buffer)
382                         (format "You were killed by %s. (Path: %s. RIP)\n"
383                                 prefix path)))
384   (liece-clear-system))
385
386 (defun* liece-handle-join-message (prefix rest)
387   (let (flag (xnick prefix) (nick prefix) (chnl rest))
388     (cond
389      ((string-match "\007[ov]" chnl)
390       (setq flag (aref (match-string 0 chnl) 1)
391             chnl (substring rest 0 (match-beginning 0))))
392      ((string-match " +$" chnl)
393       (setq chnl (substring chnl 0 (match-beginning 0)))))
394     (setq chnl (liece-channel-virtual chnl))
395     
396     (liece-nick-set-user-at-host nick liece-user-at-host)
397     
398     (if (liece-nick-equal nick liece-real-nickname)
399         (progn
400           (and liece-gather-channel-modes
401                (not (liece-channel-modeless-p (liece-channel-real chnl)))
402                (liece-send "MODE %s " (liece-channel-real chnl)))
403           (liece-channel-join chnl))
404       (liece-nick-join nick chnl))
405     
406     (cond
407      ((eq flag ?o)
408       (liece-channel-set-operator chnl xnick t)
409       (setq xnick (concat "@" xnick)))
410      ((eq flag ?v)
411       (liece-channel-set-voice chnl xnick t)
412       (setq xnick (concat "+" xnick))))
413     
414     (if (liece-handle-check-changes-ignored)
415         (return-from liece-handle-join-message))
416     
417     (when (and (liece-nick-member nick liece-current-chat-partners)
418                (get (intern nick liece-obarray) 'part))
419       (liece-insert-change (liece-pick-buffer nick)
420                             (format (_ "%s has come back as (%s)\n")
421                                     nick liece-user-at-host))
422       (liece-nick-unmark-as-apart nick))
423     
424     (cond
425      (liece-compress-changes
426       (let* ((text (format (_ " \\(has\\|have\\) joined channel %s")
427                            (regexp-quote chnl)))
428              (match (format "^%s%s.*%s$"
429                             (if liece-display-time
430                                 liece-time-prefix-regexp "")
431                             (regexp-quote liece-change-prefix)
432                             (regexp-quote text)))
433              (default (format (_ "%s%s (%s) has joined channel %s\n")
434                               liece-change-prefix
435                               nick liece-user-at-host chnl)))
436         (liece-replace (liece-pick-buffer chnl)
437                         match default text
438                         (format (_ ", %s (%s) have joined channel %s")
439                                 nick liece-user-at-host chnl))
440         (liece-replace (if (and liece-current-channel
441                                  (liece-channel-equal chnl
442                                                        liece-current-channel))
443                             liece-D-buffer
444                           (append liece-D-buffer liece-O-buffer))
445                         match default text
446                         (format (_ ", %s (%s) have joined channel %s")
447                                 nick liece-user-at-host chnl))))
448      (t
449       (liece-insert-change (liece-pick-buffer chnl)
450                             (format (_ "%s (%s) has joined channel %s\n")
451                                     nick liece-user-at-host chnl))
452       (liece-insert-change (if (liece-channel-equal chnl
453                                                       liece-current-channel)
454                                 liece-D-buffer
455                               (append liece-D-buffer liece-O-buffer))
456                             (format (_ "%s (%s) has joined channel %s\n")
457                                     nick liece-user-at-host chnl))))))
458
459 (defun* liece-handle-part-message (prefix rest)
460   (multiple-value-bind (chnl comment text match default buf) (liece-split-line rest)
461     (setq chnl (liece-channel-virtual chnl)
462           comment (liece-handle-prepare-comment comment))
463     
464     (if (liece-nick-equal prefix liece-real-nickname)
465         (liece-channel-part chnl)
466       (liece-nick-part prefix chnl))
467     
468     (if (liece-handle-check-changes-ignored)
469         (return-from liece-handle-part-message))
470     
471     (setq buf (append liece-D-buffer (liece-pick-buffer chnl)))
472     (unless (and liece-current-channel
473                  (liece-channel-equal chnl liece-current-channel))
474       (setq buf (append buf liece-O-buffer)))
475     (cond
476      (liece-compress-changes
477       (setq text (format (_ " \\(has\\|have\\) left channel %s%s")
478                          (regexp-quote chnl) (regexp-quote comment))
479             match (format "^%s%s.*%s$"
480                           (if liece-display-time
481                               liece-time-prefix-regexp "")
482                           (regexp-quote liece-change-prefix)
483                           (regexp-quote text))
484             default (format (_ "%s%s has left channel %s%s\n")
485                             liece-change-prefix prefix chnl comment))
486       (liece-replace buf
487                       match default text
488                       (format (_ ", %s have left channel %s%s")
489                               prefix chnl comment)))
490      (t
491       (liece-insert-change buf
492                             (format (_ "%s has left channel %s%s\n")
493                                     prefix chnl comment))))))
494     
495 (defun* liece-handle-silence-message (prefix rest)
496   (let* ((flag (aref rest 0)) (rest (substring rest 1)))
497     (liece-insert-info (append liece-D-buffer liece-O-buffer)
498                         (concat "User " rest
499                                 (if (eq flag ?-) "unsilenced" "silenced")))))
500
501 (provide 'liece-handle)
502
503 ;;; liece-handle.el ends here