summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Okay <cbeok@protonmail.com>2020-04-12 20:50:19 -0500
committerColin Okay <cbeok@protonmail.com>2020-04-12 20:50:19 -0500
commitc3895cdbd1b5550a5ce32033dc4dfa87d19af983 (patch)
tree652571d7f2af41e450ec2c04c0e1d56f7d80c992
parentad672f189d510123bd04a3169523fac597a416f4 (diff)
re-arranged file
-rw-r--r--posterbot.lisp13
1 files changed, 7 insertions, 6 deletions
diff --git a/posterbot.lisp b/posterbot.lisp
index 6abe0cf..4e830c9 100644
--- a/posterbot.lisp
+++ b/posterbot.lisp
@@ -4,6 +4,10 @@
(defclass posterbot (client auto-joiner) ())
+;; THE MAIN METHOD FOR RESPONDING TO USER TEXT EVENTS
+(defmethod handle-event :after ((*posterbot* posterbot) (event text-message-event))
+ (mapc #'handle-link-candiate (ppcre:split " " (msg-body event))))
+
(defvar *posterbot* nil
"Dynamic variable holding the bot instance. Bound by HANDLE-EVENT.")
@@ -20,10 +24,12 @@ the downloaded file."
(loop :for bytes = (read-sequence buffer file-stream)
:while (plusp bytes) :do (write-sequence buffer out)))))
+
(defun filename-from-link (link)
"Extracts the filename of a link. I.e. everything after the last / character."
(first (last (ppcre:split "/" link))))
+
(defun make-mime-type (filename)
"Given a string FILENAME, returns a string representing a sensible guess for a mimetype."
(format nil "image/~a"
@@ -32,6 +38,7 @@ the downloaded file."
((equal type "svg") "svg+xml")
(t type)))))
+
(defun handle-link-candiate (word)
"Checks if WORD is an HTTP URI pointing to an image resource. If it
is, downloads the image and posts it to the current room."
@@ -49,12 +56,6 @@ is, downloads the image and posts it to the current room."
:info (list :|mimetype| (make-mime-type word)))
(send-text-message *posterbot* *room-id* "I have failed you :("))))))
-;; look for links to images, one word at a time, downloading and
-;; posting any images found to the room at the current *ROOM-ID*
-(defmethod handle-event :after ((*posterbot* posterbot) (event text-message-event))
- (mapc #'handle-link-candiate (ppcre:split " " (msg-body event))))
-
-
(defun start-posterbot ()
"A start function to pass in as the :toplevel to SAVE-LISP-AND-DIE"
(let* ((config (if (uiop:file-exists-p "posterbot.config")