1 | ;;;; $Id: event.lisp 88 2005-03-20 16:55:43Z ehuelsmann $ |
---|
2 | ;;;; $Source$ |
---|
3 | |
---|
4 | ;;;; See LICENSE for licensing information. |
---|
5 | |
---|
6 | (in-package :irc) |
---|
7 | |
---|
8 | (defgeneric irc-message-event (message) |
---|
9 | (:documentation "Upon receipt of an IRC message from the |
---|
10 | connection's stream, irc-message-event will be called with the |
---|
11 | message.")) |
---|
12 | |
---|
13 | (defmethod irc-message-event ((message irc-message)) |
---|
14 | (apply-to-hooks message) |
---|
15 | (client-log (connection message) message "UNHANDLED-EVENT:")) |
---|
16 | |
---|
17 | |
---|
18 | (defgeneric default-hook (message) |
---|
19 | (:documentation "Minimum action to be executed upon reception |
---|
20 | of the IRC message to keep the connection, channel and user |
---|
21 | objects in sync.")) |
---|
22 | |
---|
23 | (defmethod default-hook ((message irc-rpl_isupport-message)) |
---|
24 | (let* ((capabilities (cdr (arguments message))) |
---|
25 | (connection (connection message)) |
---|
26 | (current-case-mapping (case-map-name connection))) |
---|
27 | (setf (server-capabilities connection) |
---|
28 | (let ((new-values (mapcar #'(lambda (x) |
---|
29 | (let ((eq-pos (position #\= x))) |
---|
30 | (if eq-pos |
---|
31 | (list (subseq x 0 eq-pos) |
---|
32 | (subseq x (1+ eq-pos))) |
---|
33 | (list x)))) capabilities))) |
---|
34 | (merge 'list new-values (copy-seq *default-isupport-values*) |
---|
35 | #'string= :key #'first))) |
---|
36 | (setf (channel-mode-descriptions connection) |
---|
37 | (chanmode-descs-from-isupport (server-capabilities connection)) |
---|
38 | (nick-prefixes connection) |
---|
39 | (nick-prefixes-from-isupport (server-capabilities connection))) |
---|
40 | (when (not (equal current-case-mapping |
---|
41 | (case-map-name connection))) |
---|
42 | ;; we need to re-normalize nicks and channel names |
---|
43 | (re-apply-case-mapping connection)))) |
---|
44 | |
---|
45 | (defmethod default-hook ((message irc-rpl_whoisuser-message)) |
---|
46 | (let ((user (find-user (connection message) |
---|
47 | (second (arguments message)))) |
---|
48 | (realname (trailing-argument message)) |
---|
49 | (username (third (arguments message))) |
---|
50 | (hostname (fourth (arguments message)))) |
---|
51 | (when user |
---|
52 | (setf (realname user) realname) |
---|
53 | (setf (username user) username) |
---|
54 | (setf (hostname user) hostname)))) |
---|
55 | |
---|
56 | (defmethod default-hook ((message irc-rpl_list-message)) |
---|
57 | (let ((connection (connection message)) |
---|
58 | (channel (second (arguments message))) |
---|
59 | (user-count (parse-integer (or (third (arguments message)) "0"))) |
---|
60 | (topic (trailing-argument message))) |
---|
61 | (add-channel connection (or (find-channel connection channel) |
---|
62 | (make-channel connection |
---|
63 | :name channel |
---|
64 | :topic topic |
---|
65 | :user-count user-count))))) |
---|
66 | |
---|
67 | (defmethod default-hook ((message irc-rpl_topic-message)) |
---|
68 | (setf (topic (find-channel (connection message) |
---|
69 | (second (arguments message)))) |
---|
70 | (trailing-argument message))) |
---|
71 | |
---|
72 | (defmethod default-hook ((message irc-rpl_namreply-message)) |
---|
73 | (let* ((connection (connection message)) |
---|
74 | (channel (find-channel connection (car (last (arguments message)))))) |
---|
75 | (dolist (nickname (tokenize-string (trailing-argument message))) |
---|
76 | (let ((user (find-or-make-user connection |
---|
77 | (canonicalize-nickname connection |
---|
78 | nickname)))) |
---|
79 | (unless (equal user (user connection)) |
---|
80 | (add-user connection user) |
---|
81 | (add-user channel user)) |
---|
82 | (let* ((mode-char (getf (nick-prefixes connection) |
---|
83 | (elt nickname 0))) |
---|
84 | (mode-name (when mode-char |
---|
85 | (mode-name-from-char connection |
---|
86 | channel mode-char)))) |
---|
87 | (when mode-name |
---|
88 | (if (has-mode-p channel mode-name) |
---|
89 | (set-mode channel mode-name user) |
---|
90 | (set-mode-value (add-mode channel mode-name |
---|
91 | (make-mode connection |
---|
92 | channel mode-name)) |
---|
93 | user)))))))) |
---|
94 | |
---|
95 | (defmethod default-hook ((message irc-ping-message)) |
---|
96 | (pong (connection message) (trailing-argument message))) |
---|
97 | |
---|
98 | (defmethod default-hook ((message irc-join-message)) |
---|
99 | (let* ((connection (connection message)) |
---|
100 | (user (find-or-make-user |
---|
101 | (connection message) |
---|
102 | (source message) |
---|
103 | :hostname (host message) |
---|
104 | :username (user message))) |
---|
105 | (channel (or (find-channel connection (trailing-argument message)) |
---|
106 | (make-channel connection |
---|
107 | :name (trailing-argument message))))) |
---|
108 | (if (self-message-p message) |
---|
109 | (add-channel connection channel) |
---|
110 | (progn |
---|
111 | (add-user connection user) |
---|
112 | (add-user channel user))))) |
---|
113 | |
---|
114 | (defmethod default-hook ((message irc-topic-message)) |
---|
115 | (setf (topic (find-channel (connection message) |
---|
116 | (first (arguments message)))) |
---|
117 | (trailing-argument message))) |
---|
118 | |
---|
119 | (defmethod default-hook ((message irc-part-message)) |
---|
120 | (let* ((connection (connection message)) |
---|
121 | (channel (find-channel connection (first (arguments message)))) |
---|
122 | (user (find-user connection (source message)))) |
---|
123 | (if (self-message-p message) |
---|
124 | (remove-channel user channel) |
---|
125 | (remove-user channel user)))) |
---|
126 | |
---|
127 | (defmethod default-hook ((message irc-quit-message)) |
---|
128 | (let ((connection (connection message))) |
---|
129 | (remove-user-everywhere connection (find-user connection (source message))))) |
---|
130 | |
---|
131 | (defmethod default-hook ((message irc-mode-message)) |
---|
132 | (destructuring-bind |
---|
133 | (target &rest arguments) |
---|
134 | (arguments message) |
---|
135 | (let* ((connection (connection message)) |
---|
136 | (target (or (find-channel connection target) |
---|
137 | (find-user connection target))) |
---|
138 | (mode-changes |
---|
139 | (when target |
---|
140 | (parse-mode-arguments connection target arguments |
---|
141 | :server-p (user connection))))) |
---|
142 | (dolist (change mode-changes) |
---|
143 | (destructuring-bind |
---|
144 | (op mode-name value) |
---|
145 | change |
---|
146 | (unless (has-mode-p target mode-name) |
---|
147 | (add-mode target mode-name |
---|
148 | (make-mode connection target mode-name))) |
---|
149 | (funcall (if (char= #\+ op) #'set-mode #'unset-mode) |
---|
150 | target mode-name value)))))) |
---|
151 | |
---|
152 | (defmethod default-hook ((message irc-nick-message)) |
---|
153 | (let ((con (connection message))) |
---|
154 | (change-nickname con (find-user con (source message)) |
---|
155 | (trailing-argument message)))) |
---|
156 | |
---|
157 | (defmethod default-hook ((message irc-kick-message)) |
---|
158 | (let* ((connection (connection message)) |
---|
159 | (channel (find-channel connection (first (arguments message)))) |
---|
160 | (user (find-user connection (second (arguments message))))) |
---|
161 | (if (self-message-p message) |
---|
162 | (remove-channel user channel) |
---|
163 | (remove-user channel user)))) |
---|
164 | |
---|
165 | (defmethod default-hook ((message ctcp-time-message)) |
---|
166 | (multiple-value-bind (second minute hour date month year day) (get-decoded-time) |
---|
167 | (send-irc-message |
---|
168 | (connection message) |
---|
169 | :notice (make-ctcp-message |
---|
170 | (format nil "TIME ~A" |
---|
171 | (make-time-message second minute hour date month year day))) |
---|
172 | (source message)))) |
---|
173 | |
---|
174 | (defmethod default-hook ((message ctcp-source-message)) |
---|
175 | (send-irc-message |
---|
176 | (connection message) |
---|
177 | :notice (make-ctcp-message |
---|
178 | (format nil "SOURCE ~A:~A:~A" |
---|
179 | *download-host* |
---|
180 | *download-directory* |
---|
181 | *download-file*)) |
---|
182 | (source message))) |
---|
183 | |
---|
184 | (defmethod default-hook ((message ctcp-finger-message)) |
---|
185 | (let* ((user (user (connection message))) |
---|
186 | (finger-info (if (not (zerop (length (realname user)))) |
---|
187 | (realname user) |
---|
188 | (nickname user)))) |
---|
189 | (send-irc-message |
---|
190 | (connection message) |
---|
191 | :notice (make-ctcp-message |
---|
192 | (format nil "FINGER ~A" finger-info)) |
---|
193 | (source message)))) |
---|
194 | |
---|
195 | (defmethod default-hook ((message ctcp-version-message)) |
---|
196 | (send-irc-message |
---|
197 | (connection message) |
---|
198 | :notice (make-ctcp-message |
---|
199 | (format nil "VERSION ~A" *ctcp-version*)) |
---|
200 | (source message))) |
---|
201 | |
---|
202 | (defmethod default-hook ((message ctcp-ping-message)) |
---|
203 | (send-irc-message |
---|
204 | (connection message) |
---|
205 | :notice (make-ctcp-message |
---|
206 | (format nil "PING ~A" (trailing-argument message))) |
---|
207 | (source message))) |
---|
208 | |
---|
209 | (defmethod irc-message-event ((message ctcp-dcc-chat-request-message)) |
---|
210 | (apply-to-hooks message) |
---|
211 | (client-log (connection message) message)) |
---|
212 | ; (when (automatically-accept-dcc-connections (configuration (connection message))) |
---|
213 | ; (let* ((user (find-user (connection message) (source message))) |
---|
214 | ; (args (tokenize-string (trailing-argument message))) |
---|
215 | ; (remote-address (hbo-to-vector-quad (parse-integer (fourth args)))) |
---|
216 | ; (remote-port (parse-integer (fifth args) :junk-allowed t))) |
---|
217 | ; (push (make-dcc-connection :user user |
---|
218 | ; :remote-address remote-address |
---|
219 | ; :remote-port remote-port) |
---|
220 | ; *dcc-connections*)))) |
---|
221 | |
---|
222 | (defmethod irc-message-event ((message ctcp-dcc-send-request-message)) |
---|
223 | (apply-to-hooks message) |
---|
224 | (client-log (connection message) message)) |
---|
225 | ; (when (automatically-accept-dcc-downloads (configuration (connection message))) |
---|
226 | ; (let* ((user (find-user (connection message) (source message))) |
---|
227 | ; (args (tokenize-string (trailing-argument message))) |
---|
228 | ; (filename (third args)) |
---|
229 | ; (remote-address (hbo-to-vector-quad (parse-integer (fourth args)))) |
---|
230 | ; (remote-port (parse-integer (fifth args))) |
---|
231 | ; (filesize (parse-integer (sixth args) :junk-allowed t))) |
---|
232 | ; (let ((dcc-connection (make-dcc-connection :user user |
---|
233 | ; :remote-address remote-address |
---|
234 | ; :remote-port remote-port))) |
---|
235 | ; (with-open-file (stream filename :direction :output |
---|
236 | ; :if-exists :supersede) |
---|
237 | ; (write-sequence (read-message-loop dcc-connection) stream)))))) |
---|
238 | |
---|