From a49878d3de84f3658dea01d71a5fe6698fe08a0b Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Sun, 6 Aug 2023 00:07:04 -0500 Subject: [PATCH] Init --- kaptchapelo.asd | 3 ++ kaptchapelo.lisp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 kaptchapelo.asd create mode 100644 kaptchapelo.lisp diff --git a/kaptchapelo.asd b/kaptchapelo.asd new file mode 100644 index 0000000..d09306d --- /dev/null +++ b/kaptchapelo.asd @@ -0,0 +1,3 @@ +(defsystem "kaptchapelo" + :depends-on ("clack" "quri" "str" "md5" "yason") + :components ((:file "kaptchapelo"))) diff --git a/kaptchapelo.lisp b/kaptchapelo.lisp new file mode 100644 index 0000000..a03a0df --- /dev/null +++ b/kaptchapelo.lisp @@ -0,0 +1,72 @@ +;;; Copyright 2023, Jaidyn Ann +;;; +;;; This program is free software: you can redistribute it and/or +;;; modify it under the terms of the GNU General Public License as +;;; published by the Free Software Foundation, either version 3 of +;;; the License, or (at your option) any later version. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see . + +(defpackage #:kaptchapelo + (:use #:cl) + (:export :start-server)) + +(in-package #:kaptchapelo) + + +(defun byte-array-to-hex-string (simple-array) + "Given an array of bytes (integers), return an equivalent string in hex." + (string-downcase + (reduce #'str:concat + (loop for byte across simple-array + collect (format nil "~2,'0X" byte))))) + + +(defun new-captcha-json () + "Return the kocaptcha-formed JSON to be returned for a new captcha request." + (yason:with-output-to-string* () + (yason:encode-plist + '("md5" "e" "token" "This_isnt_actually_used_lol" "url" "/bird")))) + + +(defun new-captcha-response () + "Create an HTTP response for use with Clack with a new captcha." + (list 201 '(:content-type "application/json") + (list (new-captcha-json)))) + + +(defun index-response () + '(201 (:content-type "text/plain") ("You’ve installed Kaptĉapelo! Good work, guy!"))) + + +(defun 404-response () + '(404 (:content-type "text/plain") ("No such page."))) + + +(defun server (env) + (let* ((uri (quri:uri (getf env :request-uri))) + (params (quri:uri-query-params uri))) + + (format 't (quri:uri-path uri)) + (cond ((string= (quri:uri-path uri) "/new") + (new-captcha-response)) + ((or (string= (quri:uri-path uri) "/") + (string= (quri:uri-path uri) "/index.html")) + (index-response)) + ('t + (404-response)) + ;; At any other path, give control back over to the user’s server + (or (and clack-app (funcall clack-app env)))))) + + + +(defun start-server () + (clack:clackup + (lambda (env) + (funcall #'server env))))