From cd8b6cd616690a150ae53efdd1951b0424270f0f Mon Sep 17 00:00:00 2001 From: not manx Date: Sat, 11 Jul 2020 23:26:40 +0000 Subject: [PATCH] The Game --- loligram.lisp | 23 +++++++++++++---------- lolinet.lisp | 17 ----------------- lolisp.asd | 1 - main.lisp | 23 +++++++++++++++++------ package.lisp | 6 +++++- upload.lisp | 49 ------------------------------------------------- utils.lisp | 23 +++++------------------ 7 files changed, 40 insertions(+), 102 deletions(-) delete mode 100644 lolinet.lisp delete mode 100644 upload.lisp diff --git a/loligram.lisp b/loligram.lisp index 7d391c1..c1d3d27 100644 --- a/loligram.lisp +++ b/loligram.lisp @@ -1,28 +1,31 @@ (in-package #:lolisp) -(defvar *send-loli* (str "https://api.telegram.org/bot" (config-item :loligram-url) "/sendPhoto?chat_id=~a&photo=~a")) -(defvar *no-loli* (str "https://api.telegram.org/bot" (config-item :loligram-url) "/sendMessage?chat_id=~a&text=Go away")) +(defmacro telegram-api-string (api-part) + `(str "https://api.telegram.org/bot" (config-item :loligram-url) ,api-part)) + +(defvar *send-loli* (telegram-api-string "/sendPhoto?chat_id=~a&photo=~a")) +(defvar *no-loli* (telegram-api-string "/sendMessage?chat_id=~a&text=Go away")) (defvar *chats* (make-hash-table)) (defun configure-loligram () - (setf *send-loli* (str "https://api.telegram.org/bot" (config-item :loligram-url) "/sendPhoto?chat_id=~a&photo=~a")) - (setf *no-loli* (str "https://api.telegram.org/bot" (config-item :loligram-url) "/sendMessage?chat_id=~a&text=Go away")) + (setf *send-loli* (telegram-api-string "/sendPhoto?chat_id=~a&photo=~a")) + (setf *no-loli* (telegram-api-string "/sendMessage?chat_id=~a&text=Go away")) (setf *chats* (make-hash-table)) (loop for chat in (config-item :loligram-chats) do (setf (gethash chat *chats*) t))) -(toot:handle (loligram :get (str "/loli/" (config-item :loligram-url)) :content-type toot:@plain) +(toot:handle (loligram :get (str "/loli/" (config-item :loligram-url)) + :accessor 'lolisp + :content-type toot:@plain) () (when (null (tbnl:raw-post-data)) (tbnl:abort-request-handler)) (let* ((json (jsown:parse (babel:octets-to-string (tbnl:raw-post-data)))) - (chat-id (jsown:val (jsown:val (jsown:val json "message") "chat") "id")) - (text (jsown:val (jsown:val json "message") "text"))) + (chat-id (json-value json "message" "chat" "id")) + (text (json-value json "message" "text"))) (cond ((not (gethash chat-id *chats*)) (dex:get (format nil *no-loli* chat-id))) ((string= text "/loli") (dex:get (format nil *send-loli* chat-id (rori:loli-link "s")))))) - "mwee") - - + "mwee") ;; Return mwee diff --git a/lolinet.lisp b/lolinet.lisp deleted file mode 100644 index 5ed2f04..0000000 --- a/lolinet.lisp +++ /dev/null @@ -1,17 +0,0 @@ -(in-package #:lolisp) - -(toot:handle (get-loli :get "/loli/") - (rating) - (let ((loli (rori:loli-get (or rating "s")))) - (cl-who:with-html-output-to-string (page) - (:html - (:head - (:meta :charset "utf-8") - (:meta :name "viewport" :content "width=device-width, initial-scale=1") - (:title "lolisp") - (:link :rel "stylesheet" :href "https://plum.moe/static/css/style.css") - (:link :rel "stylesheet" :href "https://plum.moe/static/css/loli.css")) - (:body - (:img :src (rori:based-loli (cdr (assoc :file-url loli)))) - (:p :id "tags" (format page "~a" (cdr (assoc :tags loli)))) - (:a :id "about" :href "/loli/about.html" "about")))))) diff --git a/lolisp.asd b/lolisp.asd index c97080d..fc8ad18 100644 --- a/lolisp.asd +++ b/lolisp.asd @@ -12,6 +12,5 @@ :Components ((:file "package") (:file "config") (:file "utils") - (:file "lolinet") (:file "loligram") (:file "main"))) diff --git a/main.lisp b/main.lisp index 89c681a..f556b2d 100644 --- a/main.lisp +++ b/main.lisp @@ -10,8 +10,7 @@ :port (config-item :port) :name 'lolisp :message-log-destination *error-output* - :access-log-destination *standard-output* - :document-root (config-item :document-root)))) + :access-log-destination *standard-output*))) (defun start () (handler-case (configure) @@ -23,7 +22,19 @@ (defun stop () (hunchentoot:stop *serb* :soft t)) -(export '(configure - start - stop - *serb*)) +(toot:handle (get-loli :get "/loli/" + :accessor 'lolisp) + (rating) + (let ((loli (rori:loli-get (or rating "s")))) + (cl-who:with-html-output-to-string (page) + (:html + (:head + (:meta :charset "utf-8") + (:meta :name "viewport" :content "width=device-width, initial-scale=1") + (:title "lolisp") + (:link :rel "stylesheet" :href "https://plum.moe/static/css/style.css") + (:link :rel "stylesheet" :href "https://plum.moe/static/css/loli.css")) + (:body + (:img :src (rori:based-loli (cdr (assoc :file-url loli)))) + (:p :id "tags" (format page "~a" (cdr (assoc :tags loli)))) + (:a :id "about" :href "/loli/about.html" "about")))))) diff --git a/package.lisp b/package.lisp index 17057ac..fc7fb15 100644 --- a/package.lisp +++ b/package.lisp @@ -1,2 +1,6 @@ (defpackage #:lolisp - (:use :cl)) + (:use :cl) + (:export #:configure + #:start + #:stop + #:*serb*)) diff --git a/upload.lisp b/upload.lisp deleted file mode 100644 index c8ebb03..0000000 --- a/upload.lisp +++ /dev/null @@ -1,49 +0,0 @@ -(in-package #:lolisp) - -(defconstant png-header #(137 80 78 71 13 10 26 10)) -(defvar hunchenroot (merge-pathnames "flags/" (toot:config-item :document-root))) - -(toot:handle (form :post "/upload" toot:@plain) - () - (let ((thing (do-upload (tbnl:post-parameter "flag")))) - (typecase thing - (string thing) - (t "Upload Success")))) - -(defun do-upload (file) - (let ((tmp-name (first file)) - (mime-type (third file))) - (cond - ((not (string= "image/png" mime-type)) - "Image should be a PNG") - ((not (probe-file tmp-name)) - "What the fuck?") - ((< 15360 (image-size tmp-name)) - "Image too large. Max size is 15KB") - ((not (image-dimensions-p tmp-name)) - "Wrong image dimensions.") - ((not (valid-header-p tmp-name)) - "Invalid PNG header") - (t (upload-file tmp-name (second file)))))) - -(defun upload-file (tmp-name real-name) - (real-move-file tmp-name (merge-pathnames real-name hunchenroot)) - t) - -(defun image-size (img) - (osicat-posix:stat-size (osicat-posix:stat img))) - -(defun image-dimensions-p (img) - "This is silly" - (string= "1611" (inferior-shell:run/ss (str "/usr/bin/identify -format \"%w%h\" " img)))) - -(defun valid-header-p (img) - (with-open-file (s img :element-type '(unsigned-byte 8)) - (loop for header-byte across png-header - for file-byte = (read-byte s) - always (= header-byte file-byte)))) - -(hunchentoot:start (make-instance 'toot:acceptor - :port 4244 - :access-log-destination *standard-output* - :message-log-destination *error-output*)) diff --git a/utils.lisp b/utils.lisp index f728d1a..6732fdd 100644 --- a/utils.lisp +++ b/utils.lisp @@ -3,21 +3,8 @@ (defmacro str (&rest strs) `(concatenate 'string ,@strs)) -(defun copy-stream (from to &key (buffer-size 4096) - &aux (buffer (make-array buffer-size :element-type '(unsigned-byte 8)))) - (loop for bytes-read = (read-sequence buffer from) - while (plusp bytes-read) - do (write-sequence buffer to :end bytes-read))) - -(defun real-copy-file (from to) - (with-open-file (from from :direction :input - :element-type '(unsigned-byte 8)) - (with-open-file (to to :direction :output - :if-exists :supersede - :element-type '(unsigned-byte 8)) - (copy-stream from to)))) - -(defun real-move-file (from to) - (real-copy-file from to) - (delete-file from)) - +(defmacro json-value (json &rest rest) + (let ((this `(jsown:val ,json ,(pop rest)))) + (loop while rest + do (setf this `(jsown:val ,this ,(pop rest)))) + this))