From c3895cdbd1b5550a5ce32033dc4dfa87d19af983 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Sun, 12 Apr 2020 20:50:19 -0500 Subject: re-arranged file --- posterbot.lisp | 13 +++++++------ 1 file 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") -- cgit v1.2.3