This commit is contained in:
rald 2023-08-06 20:03:47 +08:00
parent 2066036a52
commit 2f27b8e8a1
22 changed files with 520 additions and 5 deletions

Binary file not shown.

Binary file not shown.

9
board.cgi Normal file
View File

@ -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

8
board.gmi Normal file
View File

@ -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

103
boggle.cgi Executable file
View File

@ -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")

BIN
boggle.sqlite3 Normal file

Binary file not shown.

0
cgi-example.sqlite3 Normal file
View File

15
check.cgi Executable file
View File

@ -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)

18
choose.cgi Executable file
View File

@ -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")

View File

@ -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,

6
game.txt Normal file
View File

@ -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

62
guestbook/sign.c Normal file
View File

@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#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;
}

BIN
guestbook/sign.cgi Executable file

Binary file not shown.

36
guestbook/view.c Normal file
View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
#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;
}

BIN
guestbook/view.cgi Executable file

Binary file not shown.

12
hw.cgi Executable file
View File

@ -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}!")

16
index.cgi Executable file
View File

@ -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

View File

@ -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()

98
new_game-00.cgi Normal file
View File

@ -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)

105
new_game-01.cgi Normal file
View File

@ -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")

View File

@ -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("```")

24
x.cgi Executable file
View File

@ -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