From 79e8b61ac349327c20fea4002614b55e9a0be5c8 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Wed, 31 May 2023 00:47:16 -0500 Subject: [PATCH] Support message-sending --- spidercat.scm | 33 ++++++++++++++++++++++++++++++++- templates/room-chat.html | 4 ++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/spidercat.scm b/spidercat.scm index aded409..e5929ab 100644 --- a/spidercat.scm +++ b/spidercat.scm @@ -62,6 +62,15 @@ (chatdir:channels irc-dir))))))) +;; “Send” a message to the given chatdir root, simply by creating a file. +;; That was easy! +(define (send-message irc-dir channel message) + (with-output-to-file + (string-append irc-dir "/" channel "/.in/a") + (lambda () + (write-string message)))) + + ;; Returns all of a channel's messages — in alist format, with parsed datetimes. (define (channel-messages irc-dir channel) (map (lambda (msg-alist) @@ -139,6 +148,19 @@ body: (room-chat-html irc-dir channel)))) + +(define (http-send-room-message irc-dir #!optional request path) + (let ([request-data (intarweb:read-urlencoded-request-data request 50000)] + [channel (third path)]) + (if (alist-ref 'message request-data) + (begin + (send-message irc-dir channel (alist-ref 'message request-data)) + ;; We don't want the page to render before the message has been sent! + ;; Then the user might think, “uhh my message not sent¿?” + (sleep 1))) + (http-get-room-dir irc-dir request path))) + + ;; Send response for the / index. (define (http-get-root #!optional irc-dir request path) (spiffy:send-response status: 'ok body: @@ -168,6 +190,11 @@ (("*") . ,http-get-root))) +;; An associative list of POST handlers, to be used by assoc-by-path. +(define http-post-handlers + `(((/ "room" "*") . ,http-send-room-message))) + + ;; Get a pair from an associative list based on the closest match to the ;; given path. Wild-cards acceptable! For example… ;; '(/ "dad" "mom") matches, in order of precedence: @@ -205,7 +232,11 @@ ;; Handle all POST requests. (define (http-post irc-dir request continue) - (continue)) + (let* ([path (uri:uri-path (intarweb:request-uri request))] + [handler (assoc-by-path path http-post-handlers)]) + (if handler + (apply (cdr handler) (list irc-dir request path)) + (continue)))) ;; Creates a handler for all HTTP requests, with the given IRC dir. diff --git a/templates/room-chat.html b/templates/room-chat.html index 56c186c..fded57f 100644 --- a/templates/room-chat.html +++ b/templates/room-chat.html @@ -10,5 +10,9 @@ {{LIST_ITEMS}}
+
+ + +