File: event.lisp

package info (click to toggle)
cl-irc 20060514-dfsg-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,168 kB
  • ctags: 567
  • sloc: lisp: 3,922; makefile: 46; sh: 28
file content (376 lines) | stat: -rw-r--r-- 15,946 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
;;;; $Id: event.lisp 154 2006-05-14 14:26:40Z ehuelsmann $
;;;; $Source$

;;;; See LICENSE for licensing information.

(in-package :irc)

(defgeneric irc-message-event (connection message)
  (:documentation "Upon receipt of an IRC message from the
connection's stream, irc-message-event will be called with the
message."))

(defmethod irc-message-event (connection (message irc-message))
  (declare (ignore connection))
  (apply-to-hooks message)
  (client-log (connection message) message "UNHANDLED-EVENT:"))


(defgeneric default-hook (message)
  (:documentation "Minimum action to be executed upon reception
of the IRC message to keep the connection, channel and user
objects in sync."))

(defmacro generate-maskmode-hooks (listmsg-class endmsg-class
                                                 tmp-symbol mode-symbol)
  `(progn
     (defmethod default-hook ((message ,listmsg-class))
       (destructuring-bind
           (target channel-name mask &optional set-by time-set)
           (arguments message)
         (declare (ignore target set-by time-set))
         ;; note: the structure currently does not allow for logging
         ;; set-by and time-set: the MODE message handling currently
         ;; does not allow that.
         (let ((channel (find-channel (connection message) channel-name)))
           (when channel
             (unless (has-mode-p channel ',tmp-symbol)
               ;; start with a new list, replacing the old value later
               (add-mode channel ',tmp-symbol
                         (make-instance 'list-value-mode
                                        :value-type :non-user)))
             ;; use package-local symbol to prevent conflicts
             (set-mode channel ',tmp-symbol mask)))))

     (defmethod default-hook ((message ,endmsg-class))
       (let ((channel (find-channel (connection message)
                                    (car (arguments message)))))
         (when channel
           (let ((mode (has-mode-p channel ',tmp-symbol)))
             (when mode
               ;; replace list
               (add-mode channel ',mode-symbol mode)
               (remove-mode channel ',tmp-symbol))))))))

(generate-maskmode-hooks irc-rpl_banlist-message
                         irc-rpl_endofbanlist-message
                         banlist-in-progress :ban)
(generate-maskmode-hooks irc-rpl_exceptlist-message
                         irc-rpl_endofexceptlist-message
                         exceptlist-in-progress :except)
(generate-maskmode-hooks irc-rpl_invitelist-message
                         irc-rpl_endofinvitelist-message
                         invitelist-in-progress :invite)

(defmethod default-hook ((message irc-rpl_isupport-message))
  (destructuring-bind
      (target &rest capabilities)
      ;; the last argument contains only an explanitory text
      (butlast (arguments message))
    (declare (ignore target))
    (let* ((connection (connection message))
           (current-case-mapping (case-map-name connection)))
      (setf (server-capabilities connection)
            (reduce #'(lambda (x y)
                        ;; O(n^2), but we're talking small lists anyway...
                        ;; maybe I should have chosen a hash interface
                        ;; after all...
                        (if (assoc (first y) x :test #'string=)
                            x
                          (cons y x)))
                    (append
                     (mapcar #'(lambda (x)
                                 (let ((eq-pos (position #\= x)))
                                   (if eq-pos
                                       (list (substring x 0 eq-pos)
                                             (substring x (1+ eq-pos)))
                                     (list x)))) capabilities)
                     (server-capabilities connection))
                    :initial-value '()))
      (setf (channel-mode-descriptions connection)
            (chanmode-descs-from-isupport (server-capabilities connection))
            (nick-prefixes connection)
            (nick-prefixes-from-isupport (server-capabilities connection)))
      (when (not (equal current-case-mapping
                        (case-map-name connection)))
        ;; we need to re-normalize nicks and channel names
        (re-apply-case-mapping connection)))))

(defmethod default-hook ((message irc-rpl_whoisuser-message))
  (destructuring-bind
      (target nick username hostname star realname)
      (arguments message)
    (declare (ignore target star))
    (let ((user (find-user (connection message) nick)))
      (when user
        (setf (realname user) realname
              (username user) username
              (hostname user) hostname)))))

(defmethod default-hook ((message irc-rpl_list-message))
  (destructuring-bind
      (channel count topic)
      (arguments message)
    (let ((connection (connection message))
          (user-count (parse-integer count)))
      (add-channel connection (or (find-channel connection channel)
                                  (make-channel connection
                                                :name channel
                                                :topic topic
                                                :user-count user-count))))))

(defmethod default-hook ((message irc-rpl_topic-message))
  (destructuring-bind
      (target channel &optional topic)
      (arguments message)
    (declare (ignore target))
    (setf (topic (find-channel (connection message) channel)) topic)))

(defmethod default-hook ((message irc-rpl_namreply-message))
  (let* ((connection (connection message)))
    (destructuring-bind
        (nick chan-visibility channel names)
        (arguments message)
      (declare (ignore nick chan-visibility))
      (let ((channel (find-channel connection channel)))
        (setf (visibility channel)
              (or (car (assoc chan-visibility
                              '(("=" :public) ("*" :private) ("@" :secret))
                              :test #'string=))
                  :unknown))
        (unless (has-mode-p channel 'namreply-in-progress)
          (add-mode channel 'namreply-in-progress
                    (make-instance 'list-value-mode :value-type :user)))
        (dolist (nickname (tokenize-string names))
          (let ((user (find-or-make-user connection
                                         (canonicalize-nickname connection
                                                                nickname))))
            (unless (equal user (user connection))
              (add-user connection user)
              (add-user channel user)
              (set-mode channel 'namreply-in-progress user))
            (let* ((mode-char (getf (nick-prefixes connection)
                                    (elt nickname 0)))
                   (mode-name (when mode-char
                                (mode-name-from-char connection
                                                     channel mode-char))))
              (when mode-name
                (if (has-mode-p channel mode-name)
                    (set-mode channel mode-name user)
                  (set-mode-value (add-mode channel mode-name
                                            (make-mode connection
                                                       channel mode-name))
                                  user))))))))))

(defmethod default-hook ((message irc-rpl_endofnames-message))
  (let* ((channel (find-channel (connection message)
                                (second (arguments message))))
         (mode (get-mode channel 'namreply-in-progress))
         (channel-users))
    (remove-mode channel 'namreply-in-progress)
    (maphash #'(lambda (nick user-obj)
                 (declare (ignore nick))
                 (pushnew user-obj channel-users)) (users channel))
    (dolist (user (remove-if #'(lambda (x)
                                 (member x mode)) channel-users))
      (remove-user channel user))))

(defmethod default-hook ((message irc-ping-message))
  (apply #'pong (connection message) (arguments message)))

(defmethod default-hook ((message irc-join-message))
  (with-slots
       (connection source host user arguments)
       message
    (destructuring-bind
        (channel)
        arguments
      (let ((user (find-or-make-user connection source
                                     :hostname host
                                     :username user))
            (channel (or (find-channel connection channel)
                         (make-channel connection :name channel))))
        (when (self-message-p message)
          (add-channel connection channel))
        (add-user connection user)
        (add-user channel user)))))

(defmethod default-hook ((message irc-topic-message))
  (with-slots
       (connection arguments)
       message
    (destructuring-bind
        (channel &optional topic)
        arguments
      (setf (topic (find-channel connection channel)) topic))))

(defmethod default-hook ((message irc-part-message))
  (with-slots
      (connection arguments source)
      message
    (destructuring-bind
        (channel &optional text)
        arguments
      (declare (ignore text))
      (let ((channel (find-channel connection channel))
            (user (find-user connection source)))
        (when (and user channel)
          (if (self-message-p message)
              (remove-channel user channel)
            (remove-user channel user)))))))

(defmethod default-hook ((message irc-quit-message))
  (let* ((connection (connection message))
         (user (find-user connection (source message))))
    (unless (null user)
      (remove-user-everywhere connection user))))

(defmethod default-hook ((message irc-rpl_channelmodeis-message))
  (with-slots
      (connection arguments)
      message
    (destructuring-bind
        (target channel &rest mode-arguments)
        arguments
    (let* ((channel (find-channel connection channel))
           (mode-changes
            (when channel
              (parse-mode-arguments connection channel mode-arguments
                                    :server-p (user connection)))))
      (dolist (change mode-changes)
        (destructuring-bind
            (op mode-name value)
            change
          (unless (has-mode-p channel mode-name)
            (add-mode target mode-name
                      (make-mode connection channel mode-name)))
          (funcall (if (char= #\+ op) #'set-mode #'unset-mode)
                   channel mode-name value)))))))

(defmethod default-hook ((message irc-mode-message))
  (destructuring-bind
      (target &rest arguments)
      (arguments message)
    (let* ((connection (connection message))
           (target (or (find-channel connection target)
                       (find-user connection target)))
           (mode-changes
            (when target
              (parse-mode-arguments connection target arguments
                                     :server-p (user connection)))))
      (dolist (change mode-changes)
        (destructuring-bind
            (op mode-name value)
            change
          (unless (has-mode-p target mode-name)
            (add-mode target mode-name
                      (make-mode connection target mode-name)))
          (funcall (if (char= #\+ op) #'set-mode #'unset-mode)
                   target mode-name value))))))

(defmethod default-hook ((message irc-nick-message))
  (with-slots
      (connection source host user arguments)
      message
    (destructuring-bind
        (new-nick)
        arguments
      (let* ((user (find-or-make-user connection source
                                      :hostname host
                                      :username user)))
        (change-nickname connection user new-nick)))))

(defmethod default-hook ((message irc-kick-message))
  (with-slots
      (connection arguments)
      message
    (destructuring-bind
        (channel nick &optional reason)
        arguments
      (declare (ignore reason))
      (let* ((channel (find-channel connection channel))
             (user (find-user connection nick)))
        (when (and user channel)
          (if (self-message-p message)
              (remove-channel user channel)
            (remove-user channel user)))))))

(macrolet ((define-ctcp-reply-hook ((message-var message-type) &body body)
               `(defmethod default-hook ((,message-var ,message-type))
                  (when (ctcp-request-p ,message-var)
                    ,@body))))
  (define-ctcp-reply-hook (message ctcp-time-message)
      (multiple-value-bind
          (second minute hour date month year day)
          (get-decoded-time)
        (send-irc-message
         (connection message)
         :notice (source message)
         (make-ctcp-message
          (format nil "TIME ~A"
                  (make-time-message second minute hour date month year day))))))
  (define-ctcp-reply-hook (message ctcp-source-message)
      (send-irc-message
       (connection message)
       :notice
       (source message)
       (make-ctcp-message
        (format nil "SOURCE ~A:~A:~A"
                *download-host*
                *download-directory*
                *download-file*))))
  (define-ctcp-reply-hook (message ctcp-finger-message)
      (let* ((user (user (connection message)))
             (finger-info (if (not (zerop (length (realname user))))
                              (realname user)
                              (nickname user))))
        (send-irc-message
         (connection message)
         :notice (source message)
         (make-ctcp-message
          (format nil "FINGER ~A" finger-info)))))
  (define-ctcp-reply-hook (message ctcp-version-message)
      (send-irc-message
       (connection message)
       :notice (source message)
       (make-ctcp-message
        (format nil "VERSION ~A" *ctcp-version*))))
  (define-ctcp-reply-hook (message ctcp-ping-message)
      (send-irc-message
       (connection message)
       :notice (source message)
       (make-ctcp-message
        (format nil "PING ~A" (car (last (arguments message))))))))

(defmethod irc-message-event (connection (message ctcp-dcc-chat-request-message))
  (declare (ignore connection))
  (apply-to-hooks message)
  (client-log (connection message) message))
;  (when (automatically-accept-dcc-connections (configuration (connection message)))
;    (let* ((user (find-user (connection message) (source message)))
;           (args (tokenize-string (trailing-argument message)))
;           (remote-address (hbo-to-dotted-quad (parse-integer (fourth args))))
;           (remote-port (parse-integer (fifth args) :junk-allowed t)))
;      (push (make-dcc-connection :user user
;                                 :remote-address remote-address
;                                 :remote-port remote-port)
;            *dcc-connections*))))
  
(defmethod irc-message-event (connection (message ctcp-dcc-send-request-message))
  (declare (ignore connection))
  (apply-to-hooks message)
  (client-log (connection message) message))
;  (when (automatically-accept-dcc-downloads (configuration (connection message)))
;    (let* ((user (find-user (connection message) (source message)))
;           (args (tokenize-string (trailing-argument message)))
;           (filename (third args))
;           (remote-address (hbo-to-dotted-quad (parse-integer (fourth args))))
;           (remote-port (parse-integer (fifth args)))
;           (filesize (parse-integer (sixth args) :junk-allowed t)))
;      (let ((dcc-connection (make-dcc-connection :user user
;                                                 :remote-address remote-address
;                                                 :remote-port remote-port)))
;      (with-open-file (stream filename :direction :output
;                              :if-exists :supersede)
;        (write-sequence (read-message-loop dcc-connection) stream))))))