diff --git a/__pycache__/db.cpython-39.pyc b/__pycache__/db.cpython-39.pyc new file mode 100644 index 0000000..3f77dd5 Binary files /dev/null and b/__pycache__/db.cpython-39.pyc differ diff --git a/__pycache__/helpers.cpython-39.pyc b/__pycache__/helpers.cpython-39.pyc new file mode 100644 index 0000000..dbea235 Binary files /dev/null and b/__pycache__/helpers.cpython-39.pyc differ diff --git a/board.cgi b/board.cgi new file mode 100644 index 0000000..e37b92e --- /dev/null +++ b/board.cgi @@ -0,0 +1,9 @@ +20 text/gemini +L N I E +O B T D +W A V I +G D S N + +LOBATED BLOATED BONITAS NITINOL LOBATE BONITA BOATED WONTED AVIDIN ISATIN LOADS NITID INTIS BLOAT BIDET BATED BAWDS BASIN TABID DITAS DIVAS WOADS ABIDE ASIDE GATED DATED DAVIT SABIN SATIN SATED SAVIN SITED SNIDE LOAD NITE NIDE NIDI INTI ETAS EDIT OBIT BLOW BINT BITE BIDE BOAT BOAS BATE BAWD BADS TIED TIDE TAVS TADS TINS DINT DIET DITE DITA DIVA DINS WONT WOAD WADS AWOL AVID VITA VIDE VISA GAOL GATE GADS DATE SATI SATE SITE SIDE SNIT LOB LOW NIB NIT NOB NOW ETA OBI OAT BIN BIT BID BOW BOA BAT BAG BAD BAS TIN TIE TED TAO TAB TAW TAV TAG TAD TAS TIS DIN DIE DIB DIT DIS WON WAB WAT WAG WAD WAS ABO ATE ADS VAT VAW VAS VIS INS GAB GAT GAD GAS DAB DAW DAG SAB SAT SAW SAG SAD SIT SIN + +=> guess.cgi Guess a word \ No newline at end of file diff --git a/board.gmi b/board.gmi new file mode 100644 index 0000000..d2d9030 --- /dev/null +++ b/board.gmi @@ -0,0 +1,8 @@ +``` +I E U S +A F G M +E Y E E +G B I E + +``` +=> guess.cgi Guess a word \ No newline at end of file diff --git a/boggle.cgi b/boggle.cgi new file mode 100755 index 0000000..6726420 --- /dev/null +++ b/boggle.cgi @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 + +import random + +from helpers import get_query_string, get_client_cert,show_header_ok + + + +class Trie: + def __init__(self,letter): + self.letter=letter + self.mark=False + self.next=[None]*26 + @staticmethod + def add(root,word): + curr=root + for i in word: + j=i.upper() + k=ord(j)-65 + if curr.next[k]==None: + curr.next[k]=Trie(j) + curr=curr.next[k] + curr.mark=True + + + +f=open('cgi-bin/dice.txt','r') +dice=f.read().split('\n') +f.close() +dice=list(filter(None,dice)) + + + +random.shuffle(dice) +b=[] +for j in range(4): + b.append([]) + for i in range(4): + die=dice[j*4+i] + b[j].append(die[random.randrange(0,len(die))].upper()) + + + + +def r(s): + return s and len(s)>=3 and len(s)<=16 +f=open('cgi-bin/enable.txt','r') +words=f.read().split('\n') +f.close() +words=list(filter(r,words)) +trie=Trie(None) +for word in words: + Trie.add(trie,word.upper()) + + + +g=[] +for j in range(4): + g.append([]) + for i in range(4): + g[j].append(False) +found=[] +l=['' for i in range(16)] +def dfs(x,y,d,trie): + if x<0 or x>=4 or y<0 or y>=4: + return + if g[y][x]: + return + k=ord(b[y][x].upper())-65 + t=trie.next[k] + if not t: + return + l[d]=b[y][x].upper() + if t.mark: + w=''.join(l[0:d+1]) + if w not in found: + found.append(w) + g[y][x]=True + for j in [-1,0,1]: + for i in [-1,0,1]: + if i!=0 or j!=0: + dfs(x+i,y+j,d+1,t) + g[y][x]=False +for j in range(4): + for i in range(4): + dfs(i,j,0,trie) + +found.sort(key=len,reverse=True) + + + +show_header_ok() +print("```") +for j in range(4): + for i in range(4): + print(b[j][i],end=' ') + print() +print() +print("```") +# for z in found: print(z,end=' ') +print() +print("=> guess.cgi Guess a word") + diff --git a/boggle.sqlite3 b/boggle.sqlite3 new file mode 100644 index 0000000..16d333d Binary files /dev/null and b/boggle.sqlite3 differ diff --git a/cgi-example.sqlite3 b/cgi-example.sqlite3 new file mode 100644 index 0000000..e69de29 diff --git a/check.cgi b/check.cgi new file mode 100755 index 0000000..a8228ef --- /dev/null +++ b/check.cgi @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +import os +import random + +from helpers import get_query_string, get_client_cert,show_header_ok,show_query_string_required + +QUERY_STRING=os.getenv("QUERY_STRING") +if not QUERY_STRING: + print("30 guess.cgi",end='\r\n') + +show_header_ok() + +print(QUERY_STRING) + diff --git a/choose.cgi b/choose.cgi new file mode 100755 index 0000000..3e46fbf --- /dev/null +++ b/choose.cgi @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +import random + +from helpers import get_client_cert,show_redirect +from db import * +from urllib import parse + +TLS_CLIENT_HASH = get_client_cert(False) +user_id = check_hash(TLS_CLIENT_HASH) +user_name = get_name(user_id) or "[Not Set]" + +url = os.getenv("GEMINI_URL") +game_id=parse.parse_qs(parse.urlparse(url).query)['game_id'][0] + +set_current_game(user_id,game_id) + +show_redirect("game.cgi") diff --git a/create_schema.sql b/create_schema.sql index 49ed9f6..4bc3dcc 100644 --- a/create_schema.sql +++ b/create_schema.sql @@ -7,15 +7,15 @@ CREATE TABLE IF NOT EXISTS "certs" ( ); CREATE TABLE IF NOT EXISTS "users" ( "id" INTEGER NOT NULL UNIQUE, - "name" TEXT, - "add_key_code" TEXT, + "name" TEXT DEFAULT "noname", + "add_key_code" TEXT, "game_id" INTEGER, "score" INTEGER DEFAULT 0, PRIMARY KEY("id" AUTOINCREMENT), FOREIGN KEY("game_id") REFERENCES "games"("id") ); CREATE TABLE IF NOT EXISTS "games" ( - "id" INTEGER NOT NULL UNIQUE, + "id" INTEGER NOT NULL UNIQUE, "board" TEXT, "words" TEXT, "graph" TEXT, diff --git a/game.txt b/game.txt new file mode 100644 index 0000000..71c821e --- /dev/null +++ b/game.txt @@ -0,0 +1,6 @@ +O E H I +P U A D +B T R B +S U O L + +HARLOTS BURBOTS BUSTARD OUTPOUR HEPTAD HEARTS HIATUS HARLOT PURDAH AUROUS ABRUPT ABORTS BURBOT TABOUR SUBURB HEATS HEARD HEART HURTS HARTS POUTS PEATS PEART PEARL PURDA ABORT ABOUT DAUBS DAUTS DARTS DRUPE DRUBS DRATS TURBO TAUPE TABOR TORAH TORUS RUBUS ROUTS ROUST BRAID BRATS BORTS BOUTS BLOTS STUPE STAID STOUR SUTRA SURAH LOTAH LOTUS LOBAR LOUTS OPTS OUTS EURO EATS EARL HEAD HEAT HEAR HUBS HUTS HURT HURL HAUT HATS HARD HART HARL POUT POUR PEAT PEAR PUBS PUTS PURL URUS AUTO ARTS DAUB DAUT DATO DART DARB DRUB DRAT DRAB BUTS BURA BURD BURL BUST TUBS TURD TARO TRUE TRAD TORA TOUR RUBS RUTS RAID RATS RATO RUST ROTA ROTS ROUT BATS BARD BRUT BRAE BRAD BRAT BOTA BOTS BORA BORT BOUT BLOT STUB STAR STAB STOB SURA SURD ORAD ORTS OUST LOTA LOTS LORD LOUT LOUR OPE OPT OUT OUR EAU EAT EAR HEP HID HUE HUP HUB HUT HAE HAD HAT PEH PEA PUB PUT PUR UPO UTA UTS URD URB AID ART ARB ABO DAH DAB BUT BUR BUS TUP TUB TAE TAU TAD TAR TAB TOR RUE RUB RUT RAH RAD RAT ROT ROB BAH BAD BAT BAR BRA BRO BOT SUB ORA ORT ORB LOT LOB \ No newline at end of file diff --git a/guestbook/sign.c b/guestbook/sign.c new file mode 100644 index 0000000..bba3f3c --- /dev/null +++ b/guestbook/sign.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include + +#define GUESTBOOK_FILE "/home/fria/gemini/guestbook.gmi" + +int ishex(int x) { + return (x >= '0' && x <= '9') || + (x >= 'a' && x <= 'f') || + (x >= 'A' && x <= 'F'); +} + +int urldecode(const char *s, char *dec) +{ + char *o; + const char *end = s + strlen(s); + int c; + + for (o = dec; s <= end; o++) { + c = *s++; + if (c == '+') c = ' '; + else if (c == '%' && ( !ishex(*s++) || + !ishex(*s++) || + !sscanf(s - 2, "%2x", &c))) + return -1; + + if (dec) *o = c; + } + + return o - dec; +} + +int main() { + char *input=getenv("QUERY_STRING"); + FILE *fp=NULL; + time_t t = time(NULL); + struct tm tm = *localtime(&t); + char *output=NULL; + + if(!(input && *input)) { + printf("10 Enter message?\r\n"); + exit(0); + } + + output=malloc(sizeof(*output)*strlen(input)+1); + if(output==NULL) exit(1); + if(urldecode(input,output)==-1) exit(1); + + if((fp=fopen(GUESTBOOK_FILE,"a"))==NULL) exit(1); + + fprintf(fp,"### %d-%02d-%02d %02d:%02d:%02d\n%s\n",tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,output); + fclose(fp); + + free(output); + output=NULL; + + printf("30 /cgi-bin/guestbook/view.cgi\r\n"); + + return 0; +} diff --git a/guestbook/sign.cgi b/guestbook/sign.cgi new file mode 100755 index 0000000..4b5d43f Binary files /dev/null and b/guestbook/sign.cgi differ diff --git a/guestbook/view.c b/guestbook/view.c new file mode 100644 index 0000000..0011f2d --- /dev/null +++ b/guestbook/view.c @@ -0,0 +1,36 @@ +#include +#include + +#define GUESTBOOK_FILE "/home/fria/gemini/guestbook.gmi" + +int main() { + FILE *fp=NULL; + char *line=NULL; + size_t llen=0; + ssize_t rlen=0; + + printf("20 text/gemini\r\n"); + + printf("# Pantasya\n"); + + printf("=> /pantasya/index.gmi Pantasya Online Computer School\n\n"); + + printf("## Guestbook\n\n"); + + if((fp=fopen(GUESTBOOK_FILE,"r"))) { + while((rlen=getline(&line,&llen,fp))!=-1) { + puts(line); + free(line); + line=NULL; + llen=0; + rlen=0; + } + free(line); + line=NULL; + llen=0; + rlen=0; + fclose(fp); + } + + return 0; +} diff --git a/guestbook/view.cgi b/guestbook/view.cgi new file mode 100755 index 0000000..be23296 Binary files /dev/null and b/guestbook/view.cgi differ diff --git a/hw.cgi b/hw.cgi new file mode 100755 index 0000000..5984597 --- /dev/null +++ b/hw.cgi @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +from urllib.parse import unquote +from os import environ +from sys import exit + +if "QUERY_STRING" not in environ or len(unquote(environ["QUERY_STRING"]))==0: + print("10 Please enter your name",end="\r\n") + exit() +name = unquote(environ["QUERY_STRING"]) + +print("20 text/gemini",end="\r\n") +print(f"Hello {name}!") diff --git a/index.cgi b/index.cgi new file mode 100755 index 0000000..f1edff0 --- /dev/null +++ b/index.cgi @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 + +from helpers import show_header_ok +show_header_ok() + +with open('cgi-bin/README.md') as f: + contents = f.read() + print(contents) + +print("## Project Source:") +print("=> https://tildegit.org/tomasino/gemspace/src/branch/master/cgi-example CGI Example Source on tildegit.org") +print("") +print("## Get Started") +print("=> login.cgi Login with your client certificate to begin") + +#vim:fenc=utf-8:ts=4:sw=4:sta:noet:sts=4:fdm=marker:ai diff --git a/list.cgi b/list.cgi index ba1281c..e0372f0 100755 --- a/list.cgi +++ b/list.cgi @@ -14,6 +14,8 @@ print("# List") print("=> game.cgi back to game") +print("## Game",game_id) + conn=create_connection() with conn: cur = conn.cursor() @@ -21,4 +23,5 @@ with conn: rows = cur.fetchall() for row in rows: print("## "+row[0]) - print(row[1]) + print(' '.join(row[1].split(","))) + print() diff --git a/new_game-00.cgi b/new_game-00.cgi new file mode 100644 index 0000000..ca40f8c --- /dev/null +++ b/new_game-00.cgi @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 + +import random + +from helpers import get_query_string, get_client_cert,show_header_ok + + + +class Trie: + def __init__(self,letter): + self.letter=letter + self.mark=False + self.next=[None]*26 + @staticmethod + def add(root,word): + curr=root + for i in word: + j=i.upper() + k=ord(j)-65 + if curr.next[k]==None: + curr.next[k]=Trie(j) + curr=curr.next[k] + curr.mark=True + + + +f=open('cgi-bin/dice.txt','r') +dice=f.read().split('\n') +f.close() +dice=list(filter(None,dice)) + + + +random.shuffle(dice) +b=[] +for j in range(4): + b.append([]) + for i in range(4): + die=dice[j*4+i] + b[j].append(die[random.randrange(0,len(die))].upper()) + + + + +def r(s): + return s and len(s)>=3 and len(s)<=16 +f=open('cgi-bin/enable.txt','r') +words=f.read().split('\n') +f.close() +words=list(filter(r,words)) +trie=Trie(None) +for word in words: + Trie.add(trie,word.upper()) + + + +g=[] +for j in range(4): + g.append([]) + for i in range(4): + g[j].append(False) +found=[] +l=['' for i in range(16)] +def dfs(x,y,d,trie): + if x<0 or x>=4 or y<0 or y>=4: + return + if g[y][x]: + return + k=ord(b[y][x].upper())-65 + t=trie.next[k] + if not t: + return + l[d]=b[y][x].upper() + if t.mark: + w=''.join(l[0:d+1]) + if w not in found: + found.append(w) + g[y][x]=True + for j in [-1,0,1]: + for i in [-1,0,1]: + if i!=0 or j!=0: + dfs(x+i,y+j,d+1,t) + g[y][x]=False +for j in range(4): + for i in range(4): + dfs(i,j,0,trie) + +found.sort(key=len,reverse=True) + +board=[] +for j in range(4): + for i in range(4): + board.append(b[j][i]) +bstr=','.join(board) +fstr=','.join(found) + +print(bstr) +print(fstr) diff --git a/new_game-01.cgi b/new_game-01.cgi new file mode 100644 index 0000000..29d2e4b --- /dev/null +++ b/new_game-01.cgi @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 + +import random + +from helpers import get_query_string, get_client_cert,show_header_ok + + + +class Trie: + def __init__(self,letter): + self.letter=letter + self.mark=False + self.next=[None]*26 + @staticmethod + def add(root,word): + curr=root + for i in word: + j=i.upper() + k=ord(j)-65 + if curr.next[k]==None: + curr.next[k]=Trie(j) + curr=curr.next[k] + curr.mark=True + + + +f=open('cgi-bin/dice.txt','r') +dice=f.read().split('\n') +f.close() +dice=list(filter(None,dice)) + + + +random.shuffle(dice) +b=[] +for j in range(4): + b.append([]) + for i in range(4): + die=dice[j*4+i] + b[j].append(die[random.randrange(0,len(die))].upper()) + + + + +def r(s): + return s and len(s)>=3 and len(s)<=16 +f=open('cgi-bin/enable.txt','r') +words=f.read().split('\n') +f.close() +words=list(filter(r,words)) +trie=Trie(None) +for word in words: + Trie.add(trie,word.upper()) + + + +g=[] +for j in range(4): + g.append([]) + for i in range(4): + g[j].append(False) +found=[] +l=['' for i in range(16)] +def dfs(x,y,d,trie): + if x<0 or x>=4 or y<0 or y>=4: + return + if g[y][x]: + return + k=ord(b[y][x].upper())-65 + t=trie.next[k] + if not t: + return + l[d]=b[y][x].upper() + if t.mark: + w=''.join(l[0:d+1]) + if w not in found: + found.append(w) + g[y][x]=True + for j in [-1,0,1]: + for i in [-1,0,1]: + if i!=0 or j!=0: + dfs(x+i,y+j,d+1,t) + g[y][x]=False +for j in range(4): + for i in range(4): + dfs(i,j,0,trie) + +found.sort(key=len,reverse=True) + +f=open("cgi-bin/board.gmi","w") +f.write("```\n") +for j in range(4): + for i in range(4): + f.write(b[j][i]+' ') + f.write("\n") +f.write("\n") +f.write("```\n") +#for z in found: f.write(z+' ') +# f.write("\n\n") +f.write("=> guess.cgi Guess a word") +f.close() + +show_header_ok() +print("=> board.gmi View Board") + diff --git a/rank.cgi b/rank.cgi index 079b387..cf3e1e0 100755 --- a/rank.cgi +++ b/rank.cgi @@ -21,7 +21,7 @@ print("=> new_game.cgi new game") conn=create_connection() with conn: cur = conn.cursor() - cur.execute("SELECT name,score FROM users ORDER BY score DESC") + cur.execute("SELECT name,score FROM users WHERE score>0 ORDER BY score DESC") rows = cur.fetchall() print("# Ranking") print("```") diff --git a/x.cgi b/x.cgi new file mode 100755 index 0000000..fbe4e1f --- /dev/null +++ b/x.cgi @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +# modules are all in the parent directory +import sys +# sys.path.append('..') + +# import helpers from modules +from helpers import get_query_string, get_client_cert +from datetime import datetime +import pytz + + + +user_name = get_query_string("What is your name?") + +print("Hello",user_name) + + + +tz_PH = pytz.timezone('Asia/Manila') +datetime_PH = datetime.now(tz_PH) +print("PH time:", datetime_PH.strftime("%H:%M:%S")) + +#vim:fenc=utf-8:ts=4:sw=4:sta:noet:sts=4:fdm=marker:ai