/
alea.lisp
270 lines (240 loc) · 10.1 KB
/
alea.lisp
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
;;;; alea.lisp
(in-package #:alea)
;;; "alea" goes here. Hacks and glory await!
;;; sub-thread: define hooks, then read-message-loop on the connection
(defclass response ()
((session
:initarg :session
:initform (error "Must supply session")
:reader session)
(destination
:initarg :destination
:initform (error "Must supply destination")
:reader destination)
(addressee
:initarg :addressee
:initform nil
:reader addressee)
(in-response-to
:initarg :in-response-to
:initform (error "Must supply message this is in response to")
:reader in-response-to)
(text
:initform nil
:accessor text)))
(defgeneric send-response (response))
(defmethod send-response ((response response))
;; assemble full privmsg from text and addressee if any
(with-accessors ((session session)
(destination destination)
(addressee addressee)
(text text)) response
(unless text (error "Must set response text first"))
(let ((responses (if (stringp text) (list text) text)))
(dolist (text responses) t
(let ((connection (connection session))
(full-text (if addressee
(format nil "~a: ~a" addressee text)
text)))
;; then send it
(cl-irc:privmsg connection destination full-text)))
t)))
(defclass place ()
((name
:initarg :name
:initform (error "Must supply name")
:reader name)
(is-public
:initarg :is-public
:initform t
:reader is-public)
(members
:initarg :members
:initform nil
:accessor members)
(context
:initarg :context
:initform (error "Must supply context")
:accessor context)))
(defclass session ()
((session-name
:initarg :session-name
:initform (error "Must supply session-name")
:reader session-name)
(nickname
:initarg :nickname
:initform (error "Must supply nickname")
:reader nickname)
(host
:initarg :host
:initform (error "Must supply host")
:reader host)
(port
:initarg :port
:initform 6667
:reader port)
(init-channels
:initarg :channels
:initform nil
:reader init-channels)
(default-context
:initarg :default-context
:initform 'generic-context
:reader default-context)
(channels
:initform nil
:accessor channels)
(privmsgs
:initform nil
:accessor privmsgs)
(connection
:initarg :connection
:initform nil
:accessor connection
:documentation "cl-irc connection")
(sigil
:documentation "The command character such as !"
:initform #\!
:initarg :sigil
:reader sigil)
(handler
:initform nil)))
(defmethod initialize-instance :after ((session session) &key)
(setf (slot-value session 'handler) (curry #'handle-clirc-message session)))
(defgeneric session-connect (session))
(defmethod session-connect ((session session))
(with-accessors ((host host)
(port port)
(nickname nickname)
(connection connection)) session
(setf connection (cl-irc:connect :nickname nickname :server host :port port))
(setup-hooks session)
session))
(defgeneric handle-clirc-message (session message))
(defmethod handle-clirc-message ((session session) (message cl-irc:irc-err_nicknameinuse-message))
;; Need to generate a new nickname and switch to that.
t)
;; TODO some nickserv integration. it seems to work mainly with NOTICE from NickServ.
(defmethod handle-clirc-message ((session session) (message cl-irc:irc-join-message))
(let ((nickname (cl-irc:source message))
(channel (first (cl-irc:arguments message))))
(format t "~a joined channel ~a~%" nickname channel)
(when (string-equal (bot-nickname session) nickname)
;; bot just joined this channel
(let* ((new-context (make-instance (default-context session)))
;; members will be filled in when it gets its first command
(new-place (make-instance 'place :context new-context :is-public t :name channel)))
(push (cons channel new-place) (channels session)))))
;; return nil so the default hook fires also
nil)
(defmethod handle-clirc-message ((session session) (message cl-irc:irc-rpl_welcome-message))
(format t "~A~%" (describe (connection session)))
(format t "~A~%" (describe message))
(dolist (channel (init-channels session))
(cl-irc:join (connection session) channel)))
(defun bot-nickname (session)
(cl-irc:nickname (cl-irc:user (connection session))))
(defun message-recipient (message)
(first (cl-irc:arguments message)))
(defun remove-first-word (string)
(let* ((first-space (position #\space string))
(next-nonspace (position #\space string :start (+ 1 first-space) :test (complement #'eql))))
(subseq string next-nonspace)))
(defun identify-command (message session)
(let* ((sigil (sigil session))
(potential-command (second (cl-irc:arguments message)))
(first-char (char potential-command 0))
(bot-nickname (bot-nickname session)))
(cond
((string-equal (bot-nickname session) (message-recipient message))
;; in a PM, the entire message is the command
(list :strict t :command potential-command))
((alexandria:starts-with-subseq bot-nickname potential-command :test #'string-equal)
;; if the bot's nickname is there, take it from the first non-whitespace
;; after the first whitespace
(list :strict t :command (remove-first-word potential-command)))
((eql sigil first-char)
(let ((first-nonsigil (position sigil potential-command :test (complement #'eql))))
(when first-nonsigil
(list :strict nil :command (subseq potential-command first-nonsigil)))))
(t nil))))
(defun identify-users (session channel)
;;; TODO: should exclude bot's nickname
(alexandria:hash-table-keys (cl-irc:users (cl-irc:find-channel (connection session) channel))))
(defun identify-relevant-place (message session)
(let* ((bot-nickname (bot-nickname session))
(arguments (cl-irc:arguments message))
(is-privmsg (string-equal (first arguments) bot-nickname))
(destination (if is-privmsg
(cl-irc:source message)
(first arguments))))
(when is-privmsg
(let ((cell (assoc destination (privmsgs session) :test #'string-equal)))
(unless cell
;;; make a privmsg place
(let* ((new-context (make-instance (default-context session)))
(new-place (make-instance 'place :context new-context :is-public nil :name destination :members (list destination)))
(new-cons (cons destination new-place)))
(push new-cons (privmsgs session))))))
(cdr (assoc destination (slot-value session (if is-privmsg 'privmsgs 'channels)) :test #'string-equal))))
(defmethod handle-clirc-message ((session session) (message cl-irc:irc-privmsg-message))
(let
;;; identify the relevant place (creating a privmsg place if needed)
((place (identify-relevant-place message session)))
;;; update the place's members if it's a public place
(when (is-public place)
(let ((members (identify-users session (name place))))
(setf (members place) members)))
;;; identify a command if any
(let ((command (identify-command message session))
(context (context place)))
(if command
;;; parse command. if parse-error and strict command, error message
(let ((response (make-instance 'response
:session session
:in-response-to message
:destination (name place)
:addressee (when (is-public place) (cl-irc:source message))))
(parsed (parse-command context (getf command :command))))
(if (eql (car parsed) :parse-error)
(if (getf command :strict) (setf (text response) "error yo"))
;;; eval command on context and place.
(let ((result (eval-command context place (cl-irc:source message) (car parsed) (cdr parsed))))
(setf (text response) result)))
;;; return the response if any
(if (text response)
(send-response response)
;; if we don't want to handle it, return t
;; that way cl-irc doesn't whine
t))
;; we're doing nothing, just return t
t))))
(defun gather-handler-types ()
"generic-function-methods gets the method metaobjects for the generic function. then method-specializers returns the two class specializers for the method; the second one is the class metaobject for the message, so calling class-name on it gets the symbol we need"
(let ((methods (sb-mop:generic-function-methods #'handle-clirc-message))
(extractor (lambda (method)
(class-name (second (sb-mop:method-specializers method))))))
(mapcar extractor methods)))
(defun setup-hooks (session)
(let ((connection (connection session))
(hook-types (gather-handler-types))
(handler (slot-value session 'handler)))
(dolist (type hook-types)
(cl-irc:remove-hook connection type handler)
(cl-irc:add-hook connection type handler))))
(defun worker (session)
(session-connect session)
(cl-irc:read-message-loop (connection session)))
;;; main thread: make connection, hand off to subthread to process
;;; or nope, just create the thread and tell it to connect
;;; we need to wait to join until we see CL-IRC:IRC-RPL_WELCOME-MESSAGE
(defvar *sessions* (make-hash-table :test 'equal))
(defun connect (name host port nick channel)
(let* ((session (make-instance 'session :session-name name :nickname nick :host host :port port :channels (list channel)))
(stdout *standard-output*)
(threadname (format nil "irc-handler-~S" name)))
(setf (gethash name *sessions*) session)
(sb-thread:make-thread (lambda ()
(let ((*standard-output* stdout))
(worker session))) :name threadname)
session))