-
+ DC58FDE859B31B9E12DAB60BB7631955BD12F890B04A8F09BBF170512EE4A0C8A37F692CE419C8D56A4AA5017496C699B506C71610C9AD594B3B22312C298C03
trilemabot/trilemabot.lisp
(0 . 0)(1 . 120)
125 ;;;; trilemabot.lisp
126
127 (in-package #:trilemabot)
128
129 ;; These are used to check that we're joining #trilema; make sure that
130 ;; your bot is configured accordingly if you want to use
131 ;; #trilema-specific functionality.
132 (defparameter *trilema-server* ".freenode.net")
133 (defparameter *trilema-channel* "#trilema")
134 (defparameter *trilema-deedbot-nick* "deedbot")
135
136 (defclass trilemabot (ircbot)
137 ((in-chan :accessor trilemabot-in-chan :initform nil)
138 (voiced :accessor trilemabot-voiced :initform nil)
139 (voice-otp-stash :accessor trilemabot-voice-otp-stash
140 :initarg :voice-otp-stash
141 :initform nil)
142 (inbox :accessor trilemabot-inbox
143 :initform nil)))
144
145 (defmethod ircbot-connect :after ((bot trilemabot))
146 (with-slots (connection) bot
147 (add-hook connection 'irc-join-message
148 #'(lambda (message) (trilemabot-join-channel bot message)))
149 (add-hook connection 'irc-part-message
150 #'(lambda (message) (trilemabot-part-channel bot message)))
151 (add-hook connection 'irc-privmsg-message
152 #'(lambda (message) (trilemabot-handle-privmsg bot message)))
153 (add-hook connection 'irc-mode-message
154 #'(lambda (message) (trilemabot-check-mode bot message)))))
155
156 (defmethod ircbot-disconnect :after ((bot trilemabot) &optional (quit-msg ""))
157 (declare (ignore quit-msg))
158 (with-slots (in-chan voiced) bot
159 (setf in-chan nil
160 voiced nil)))
161
162 (defmethod trilemabot-join-channel ((bot trilemabot) message)
163 (destructuring-bind (channel) (arguments message)
164 (when (and (string= (source message) (ircbot-nick bot))
165 (string= channel *trilema-channel*)
166 (search *trilema-server* (ircbot-server bot)))
167 (setf (trilemabot-in-chan bot) t)
168 (trilemabot-voice bot))))
169
170 (defmethod trilemabot-part-channel ((bot trilemabot) message)
171 (let ((channel (car (arguments message))))
172 (when (and (string= (source message) (ircbot-nick bot))
173 (string= channel *trilema-channel*)
174 (search *trilema-server* (ircbot-server bot)))
175 (setf (trilemabot-in-chan bot) nil
176 (trilemabot-voiced bot) nil))))
177
178 (defmethod trilemabot-handle-privmsg ((bot trilemabot) message)
179 (destructuring-bind (target message-text) (arguments message)
180 (when (and (string= (source message) *trilema-deedbot-nick*)
181 (string= target (ircbot-nick bot)))
182 (format *standard-output* "<~a>: ~a~%"
183 (source message) message-text)
184 (push (list :from (source message)
185 :time (received-time message)
186 :message message-text)
187 (trilemabot-inbox bot)))))
188
189 (defmethod trilemabot-check-mode ((bot trilemabot) message)
190 (when (= 3 (length (arguments message))) ; mode change for user in chan
191 (destructuring-bind (channel mode nick) (arguments message)
192 (when (and (string= channel *trilema-channel*)
193 (string= nick (ircbot-nick bot)))
194 (cond
195 ((or (string= mode "+o")
196 (string= mode "+v")) (setf (trilemabot-voiced bot) t))
197 ((or (string= mode "-o")
198 (string= mode "-v")) (setf (trilemabot-voiced bot) nil)))))))
199
200 (defmethod trilemabot-add-voice-otps ((bot trilemabot) &rest otps)
201 (with-slots (voice-otp-stash) bot
202 (setf voice-otp-stash (nconc voice-otp-stash otps))))
203
204 (defmethod trilemabot-voice ((bot trilemabot))
205 (with-slots (voice-otp-stash) bot
206 (if voice-otp-stash
207 (trilemabot-send-otp bot (pop voice-otp-stash))
208 (format *standard-output* "[trilemabot ~a@~a] No OTPs available.~%"
209 (ircbot-nick bot) (ircbot-server bot)))))
210
211 (defmethod trilemabot-send-up ((bot trilemabot) &optional nick)
212 (ircbot-send-message bot *trilema-deedbot-nick*
213 (make-bot-command "!!up"
214 (or nick (ircbot-nick bot)))))
215
216 (defmethod trilemabot-send-otp ((bot trilemabot) otp)
217 (ircbot-send-message bot *trilema-deedbot-nick*
218 (make-bot-command "!!v" otp)))
219
220 (defun make-bot-command (command &rest arguments)
221 "Make raw bot command string consisting of `command' and
222 space-separated `arguments'."
223 (with-output-to-string (out)
224 (write-string command out)
225 (dolist (arg arguments)
226 (write-string " " out)
227 (write-string arg out))))
228
229 (defmethod trilemabot-save-voice-otp-stash ((bot trilemabot) filespec)
230 (with-open-file (out filespec
231 :direction :output
232 :if-exists :supersede
233 :if-does-not-exist :create)
234 (print (trlb:trilemabot-voice-otp-stash bot) out)
235 (finish-output out)))
236
237 (defun make-trilemabot (server port nick password channels &optional voice-otps)
238 (make-instance 'trilemabot
239 :server server
240 :port port
241 :nick nick
242 :password password
243 :channels channels
244 :voice-otp-stash voice-otps))