source: tags/0.9.0/event.lisp

Last change on this file was 204, checked in by Erik Huelsmann, 17 years ago

DCC implementation checkpoint: Working DCC CHAT with passive local side.
'passive local' == either remote initiates or local passive initiative.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision URL
File size: 17.2 KB
Line 
1;;;; $Id: event.lisp 204 2007-04-30 07:56:05Z ehuelsmann $
2;;;; $URL: tags/0.9.0/event.lisp $
3
4;;;; See LICENSE for licensing information.
5
6(in-package :irc)
7
8(defgeneric irc-message-event (connection message)
9  (:documentation "Upon receipt of an IRC message from the
10connection's stream, irc-message-event will be called with the
11message."))
12
13(defmethod irc-message-event (connection (message irc-message))
14  (declare (ignore connection))
15  (unless (apply-to-hooks message)
16    (client-log (connection message) message "UNHANDLED-EVENT:")))
17
18(defgeneric dcc-message-event (connection message)
19  (:documentation "Upon receipt of an IRC message from the
20connection's stream, irc-message-event will be called with the
21message."))
22
23(defmethod dcc-message-event (connection (message dcc-message))
24  (declare (ignore connection))
25  (unless (apply-to-hooks message)
26    (client-log (connection message) message "UNHANDLED-EVENT:")))
27
28
29(defgeneric default-hook (message)
30  (:documentation "Minimum action to be executed upon reception
31of the IRC message to keep the connection, channel and user
32objects in sync."))
33
34(defmacro generate-maskmode-hooks (listmsg-class endmsg-class
35                                                 tmp-symbol mode-symbol)
36  `(progn
37     (defmethod default-hook ((message ,listmsg-class))
38       (destructuring-bind
39           (target channel-name mask &optional set-by time-set)
40           (arguments message)
41         (declare (ignore target set-by time-set))
42         ;; note: the structure currently does not allow for logging
43         ;; set-by and time-set: the MODE message handling currently
44         ;; does not allow that.
45         (let ((channel (find-channel (connection message) channel-name)))
46           (when channel
47             (unless (has-mode-p channel ',tmp-symbol)
48               ;; start with a new list, replacing the old value later
49               (add-mode channel ',tmp-symbol
50                         (make-instance 'list-value-mode
51                                        :value-type :non-user)))
52             ;; use package-local symbol to prevent conflicts
53             (set-mode channel ',tmp-symbol mask)))))
54
55     (defmethod default-hook ((message ,endmsg-class))
56       (let ((channel (find-channel (connection message)
57                                    (car (arguments message)))))
58         (when channel
59           (let ((mode (has-mode-p channel ',tmp-symbol)))
60             (when mode
61               ;; replace list
62               (add-mode channel ',mode-symbol mode)
63               (remove-mode channel ',tmp-symbol))))))))
64
65(generate-maskmode-hooks irc-rpl_banlist-message
66                         irc-rpl_endofbanlist-message
67                         banlist-in-progress :ban)
68(generate-maskmode-hooks irc-rpl_exceptlist-message
69                         irc-rpl_endofexceptlist-message
70                         exceptlist-in-progress :except)
71(generate-maskmode-hooks irc-rpl_invitelist-message
72                         irc-rpl_endofinvitelist-message
73                         invitelist-in-progress :invite)
74
75(defmethod default-hook ((message irc-rpl_isupport-message))
76  (destructuring-bind
77      (target &rest capabilities)
78      ;; the last argument contains only an explanitory text
79      (butlast (arguments message))
80    (declare (ignore target))
81    (let* ((connection (connection message))
82           (current-case-mapping (case-map-name connection)))
83      (flet ((split-arg (x)
84                (let ((eq-pos (position #\= x)))
85                  (if eq-pos
86                      (list (substring x 0 eq-pos)
87                            (substring x (1+ eq-pos)))
88                    (list x))))
89             (decode-arg (text)
90                ;; decode \xHH into (char-code HH)
91                ;; btw: how should that work with multibyte utf8?
92                (format nil "~{~A~}"
93                        (do* ((start 0 (+ 4 pos))
94                              (pos (search "\\x" text)
95                                   (search "\\x" text :start2 (1+ pos)))
96                              (points))
97                            ((null pos)
98                             (reverse (push (substring text start) points)))
99                          (push (substring text start pos) points)
100                          (push (code-char (parse-integer text
101                                                          :start (+ 2 pos)
102                                                          :end (+ 4 pos)
103                                                          :junk-allowed nil
104                                                          :radix 16))
105                                points))))
106             (negate-param (param)
107                 (if (eq #\- (char (first param) 0))
108                     (assoc (substring (first param) 1)
109                            *default-isupport-values*
110                            :test #'string=)
111                   param)))
112
113        (setf (server-capabilities connection)
114              (reduce #'(lambda (x y)
115                          (adjoin y x :key #'first :test #'string=))
116                      (append
117                       (remove nil (mapcar #'negate-param
118                                           (mapcar #'(lambda (x)
119                                                       (mapcar #'decode-arg x))
120                                                   (mapcar #'split-arg
121                                                           capabilities))))
122                       (server-capabilities connection))
123                      :initial-value '()))
124        (setf (channel-mode-descriptions connection)
125              (chanmode-descs-from-isupport (server-capabilities connection))
126              (nick-prefixes connection)
127              (nick-prefixes-from-isupport (server-capabilities connection)))
128        (when (not (equal current-case-mapping
129                          (case-map-name connection)))
130          ;; we need to re-normalize nicks and channel names
131          (re-apply-case-mapping connection))))))
132
133(defmethod default-hook ((message irc-rpl_whoisuser-message))
134  (destructuring-bind
135      (target nick username hostname star realname)
136      (arguments message)
137    (declare (ignore target star))
138    (let ((user (find-user (connection message) nick)))
139      (when user
140        (setf (realname user) realname
141              (username user) username
142              (hostname user) hostname)))))
143
144(defmethod default-hook ((message irc-rpl_list-message))
145  (destructuring-bind
146      (channel count topic)
147      (arguments message)
148    (let ((connection (connection message))
149          (user-count (parse-integer count)))
150      (add-channel connection (or (find-channel connection channel)
151                                  (make-channel connection
152                                                :name channel
153                                                :topic topic
154                                                :user-count user-count))))))
155
156(defmethod default-hook ((message irc-rpl_topic-message))
157  (destructuring-bind
158      (target channel &optional topic)
159      (arguments message)
160    (declare (ignore target))
161    (setf (topic (find-channel (connection message) channel)) topic)))
162
163(defmethod default-hook ((message irc-rpl_namreply-message))
164  (let* ((connection (connection message)))
165    (destructuring-bind
166        (nick chan-visibility channel names)
167        (arguments message)
168      (declare (ignore nick))
169      (let ((channel (find-channel connection channel)))
170        (setf (visibility channel)
171              (or (second (assoc chan-visibility
172                                 '(("=" :public) ("*" :private) ("@" :secret))
173                                 :test #'string=))
174                  :unknown))
175        (unless (has-mode-p channel 'namreply-in-progress)
176          (add-mode channel 'namreply-in-progress
177                    (make-instance 'list-value-mode :value-type :user)))
178        (dolist (nickname (tokenize-string names))
179          (let ((user (find-or-make-user connection
180                                         (canonicalize-nickname connection
181                                                                nickname))))
182            (unless (equal user (user connection))
183              (add-user connection user)
184              (add-user channel user)
185              (set-mode channel 'namreply-in-progress user))
186            (let* ((mode-char (getf (nick-prefixes connection)
187                                    (elt nickname 0)))
188                   (mode-name (when mode-char
189                                (mode-name-from-char connection
190                                                     channel mode-char))))
191              (when mode-name
192                (if (has-mode-p channel mode-name)
193                    (set-mode channel mode-name user)
194                  (set-mode-value (add-mode channel mode-name
195                                            (make-mode connection
196                                                       channel mode-name))
197                                  user))))))))))
198
199(defmethod default-hook ((message irc-rpl_endofnames-message))
200  (let* ((channel (find-channel (connection message)
201                                (second (arguments message))))
202         (mode (get-mode channel 'namreply-in-progress))
203         (channel-users))
204    (remove-mode channel 'namreply-in-progress)
205    (maphash #'(lambda (nick user-obj)
206                 (declare (ignore nick))
207                 (pushnew user-obj channel-users)) (users channel))
208    (dolist (user (remove-if #'(lambda (x)
209                                 (member x mode)) channel-users))
210      (remove-user channel user))))
211
212(defmethod default-hook ((message irc-ping-message))
213  (apply #'pong (connection message) (arguments message)))
214
215(defmethod default-hook ((message irc-join-message))
216  (with-slots
217       (connection source host user arguments)
218       message
219    (destructuring-bind
220        (channel)
221        arguments
222      (let ((user (find-or-make-user connection source
223                                     :hostname host
224                                     :username user))
225            (channel (or (find-channel connection channel)
226                         (make-channel connection :name channel))))
227        (when (self-message-p message)
228          (add-channel connection channel))
229        (add-user connection user)
230        (add-user channel user)))))
231
232(defmethod default-hook ((message irc-topic-message))
233  (with-slots
234       (connection arguments)
235       message
236    (destructuring-bind
237        (channel &optional topic)
238        arguments
239      (setf (topic (find-channel connection channel)) topic))))
240
241(defmethod default-hook ((message irc-part-message))
242  (with-slots
243      (connection arguments source)
244      message
245    (destructuring-bind
246        (channel &optional text)
247        arguments
248      (declare (ignore text))
249      (let ((channel (find-channel connection channel))
250            (user (find-user connection source)))
251        (when (and user channel)
252          (if (self-message-p message)
253              (remove-channel user channel)
254            (remove-user channel user)))))))
255
256(defmethod default-hook ((message irc-quit-message))
257  (let* ((connection (connection message))
258         (user (find-user connection (source message))))
259    (unless (null user)
260      (remove-user-everywhere connection user))))
261
262(defmethod default-hook ((message irc-rpl_channelmodeis-message))
263  (with-slots
264      (connection arguments)
265      message
266    (destructuring-bind
267        (target channel &rest mode-arguments)
268        arguments
269    (let* ((channel (find-channel connection channel))
270           (mode-changes
271            (when channel
272              (parse-mode-arguments connection channel mode-arguments
273                                    :server-p (user connection)))))
274      (dolist (change mode-changes)
275        (destructuring-bind
276            (op mode-name value)
277            change
278          (unless (has-mode-p channel mode-name)
279            (add-mode target mode-name
280                      (make-mode connection channel mode-name)))
281          (funcall (if (char= #\+ op) #'set-mode #'unset-mode)
282                   channel mode-name value)))))))
283
284(defmethod default-hook ((message irc-mode-message))
285  (destructuring-bind
286      (target &rest arguments)
287      (arguments message)
288    (let* ((connection (connection message))
289           (target (or (find-channel connection target)
290                       (find-user connection target)))
291           (mode-changes
292            (when target
293              (parse-mode-arguments connection target arguments
294                                     :server-p (user connection)))))
295      (dolist (change mode-changes)
296        (destructuring-bind
297            (op mode-name value)
298            change
299          (unless (has-mode-p target mode-name)
300            (add-mode target mode-name
301                      (make-mode connection target mode-name)))
302          (funcall (if (char= #\+ op) #'set-mode #'unset-mode)
303                   target mode-name value))))))
304
305(defmethod default-hook ((message irc-nick-message))
306  (with-slots
307      (connection source host user arguments)
308      message
309    (destructuring-bind
310        (new-nick)
311        arguments
312      (let* ((user (find-or-make-user connection source
313                                      :hostname host
314                                      :username user)))
315        (change-nickname connection user new-nick)))))
316
317(defmethod default-hook ((message irc-kick-message))
318  (with-slots
319      (connection arguments)
320      message
321    (destructuring-bind
322        (channel nick &optional reason)
323        arguments
324      (declare (ignore reason))
325      (let* ((channel (find-channel connection channel))
326             (user (find-user connection nick)))
327        (when (and user channel)
328          (if (user-eq-me-p connection user)
329              (remove-channel user channel)
330            (remove-user channel user)))))))
331
332;;###TODO: generate these responses in a DCC CHAT context too.
333(macrolet ((define-ctcp-reply-hook ((message-var message-type) &body body)
334               `(defmethod default-hook ((,message-var ,message-type))
335                  (when (ctcp-request-p ,message-var)
336                    ,@body))))
337  (define-ctcp-reply-hook (message ctcp-time-message)
338      (multiple-value-bind
339          (second minute hour date month year day)
340          (get-decoded-time)
341        (send-irc-message
342         (connection message)
343         :notice (source message)
344         (make-ctcp-message
345          (format nil "TIME ~A"
346                  (make-time-message second minute hour date month year day))))))
347  (define-ctcp-reply-hook (message ctcp-source-message)
348      (send-irc-message
349       (connection message)
350       :notice
351       (source message)
352       (make-ctcp-message
353        (format nil "SOURCE ~A:~A:~A"
354                *download-host*
355                *download-directory*
356                *download-file*))))
357  (define-ctcp-reply-hook (message ctcp-finger-message)
358      (let* ((user (user (connection message)))
359             (finger-info (if (not (zerop (length (realname user))))
360                              (realname user)
361                              (nickname user))))
362        (send-irc-message
363         (connection message)
364         :notice (source message)
365         (make-ctcp-message
366          (format nil "FINGER ~A" finger-info)))))
367  (define-ctcp-reply-hook (message ctcp-version-message)
368      (send-irc-message
369       (connection message)
370       :notice (source message)
371       (make-ctcp-message
372        (format nil "VERSION ~A" *ctcp-version*))))
373  (define-ctcp-reply-hook (message ctcp-ping-message)
374      (send-irc-message
375       (connection message)
376       :notice (source message)
377       (make-ctcp-message
378        (format nil "PING ~A" (car (last (arguments message))))))))
379
380(defmethod irc-message-event (connection (message ctcp-dcc-chat-request-message))
381  (declare (ignore connection))
382  (apply-to-hooks message)
383  (client-log (connection message) message))
384;  (when (automatically-accept-dcc-connections (configuration (connection message)))
385;    (let* ((user (find-user (connection message) (source message)))
386;           (args (tokenize-string (trailing-argument message)))
387;           (remote-address (hbo-to-dotted-quad (parse-integer (fourth args))))
388;           (remote-port (parse-integer (fifth args) :junk-allowed t)))
389;      (push (make-dcc-connection :user user
390;                                 :remote-address remote-address
391;                                 :remote-port remote-port)
392;            *dcc-connections*))))
393 
394(defmethod irc-message-event (connection (message ctcp-dcc-send-request-message))
395  (declare (ignore connection))
396  (apply-to-hooks message)
397  (client-log (connection message) message))
398;  (when (automatically-accept-dcc-downloads (configuration (connection message)))
399;    (let* ((user (find-user (connection message) (source message)))
400;           (args (tokenize-string (trailing-argument message)))
401;           (filename (third args))
402;           (remote-address (hbo-to-dotted-quad (parse-integer (fourth args))))
403;           (remote-port (parse-integer (fifth args)))
404;           (filesize (parse-integer (sixth args) :junk-allowed t)))
405;      (let ((dcc-connection (make-dcc-connection :user user
406;                                                 :remote-address remote-address
407;                                                 :remote-port remote-port)))
408;      (with-open-file (stream filename :direction :output
409;                              :if-exists :supersede)
410;        (write-sequence (read-message-loop dcc-connection) stream))))))
411 
Note: See TracBrowser for help on using the repository browser.