-- ************************************************************************ -- -- Bible module -- Copyright 2019 by Sean Conner. All Rights Reserved. -- -- 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 . -- -- Comments, questions and criticisms can be sent to: sean@conman.org -- -- ************************************************************************ -- luacheck: globals handler -- luacheck: ignore 611 local uurl = require "GLV-1.url-util" local math = require "math" local string = require "string" local tonumber = tonumber _ENV = {} -- ************************************************************************ function handler(conf,_,loc,pathinfo,ios) if pathinfo == "" then loc.path = loc.path .. "/" ios:write("31 ",uurl.toa(loc),"\r\n") return 31 end if pathinfo == "/" then ios:write( "20 text/gemini\r\n", "I'm thinking of a number between 1 and 100. Click the\r\n", "link below to guess my number!\r\n", "\r\n", string.format("=> %s/%d Hazzard a guess\r\n",conf.path,math.random(100) + 1000) ) return 20 end if not loc.query then ios:write("10 Guess a number\r\n") return 10 end local guess = uurl.esc_query:match(loc.query) if guess:match "^[%+%-][%d]" then ios:write("10 Guess a number (just digits, no plus or minus sign)\r\n") return 10 end if guess:match "^[%D]" then ios:write( "20 text/gemini\r\n", "Sigh. I think your client is buggy and is assuming\r\n", "that the query string needs to start with the name,\r\n", "which it does not. It just needs to be\r\n", "a number. You might want to try again. Sorry.\r\n", "\r\n", string.format("=> %s/ Try again\r\n",conf.path), string.format("=> / Top level menu\r\n") ) return 20 end if guess:match "%?" then ios:write( "20 text/gemini\r\n", "Sign. I think your client is buggy and has appended a\r\n", "second query string to one that already exists. The query\r\n", 'your client sent, "' .. loc.query .. '" is not proper. It\r\n', "should be just a number. You might want to try again. Sorry.\r\n", "\r\n", string.format("=> %s/ Try again\r\n",conf.path), string.format("=> / Top level menu\r\n") ) return 20 end guess = tonumber(guess) if not guess then ios:write("10 Guess a number\r\n") return 10 end local num = tonumber(pathinfo:sub(2,-1)) if not num then ios:write("10 Guess a number\r\n") return 10 end num = num % 97 if guess < num then ios:write("10 Higher\r\n") return 10 elseif guess > num then ios:write("10 Lower\r\n") return 10 else ios:write( "20 text/gemini\r\n", "Congratulations! You guessed the number!\r\n", "\r\n", string.format("=> %s/%d Try again?\r\n",conf.path,math.random(100) + 1000), "=> / Nah, take me back home\r\n" ) return 20 end end -- ************************************************************************ return _ENV