Skip to content

Instantly share code, notes, and snippets.

@fergusq
Last active February 8, 2017 16:18
Show Gist options
  • Save fergusq/92c2fe9135e5985ac9265acd15561311 to your computer and use it in GitHub Desktop.
Save fergusq/92c2fe9135e5985ac9265acd15561311 to your computer and use it in GitHub Desktop.
Pöytäkirjapalvelin
\define{\Alku{nimi}{aika}{paikka}{pvm}}{<html>
<head>
<title>\nimi</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
ol {
list-style-type: none;
counter-reset: item;
margin: 0;
padding: 0;
}
ol > li {
display: table;
counter-increment: item;
margin-bottom: 0.6em;
font-size: 100%;
}
ol > li:before {
content: counters(item, ".") ". ";
display: table-cell;
padding-right: 0.6em;
}
li ol > li {
margin: 0;
}
li ol > li:before {
content: counters(item, ".") " ";
}
vapaa {
color: green;
}
varattava {
color: blue;
}
ei {
color: red;
}
erikseen {
color: brown;
}
div.content {
margin-left: auto;
margin-right: auto;
max-width: 800px;
}
div.header-center, footer {
text-align: center;
}
div.header-right {
float: right;
margin-top: 0.5em;
}
div.header-left {
float: left;
margin-top: 0.5em;
}
img.logo {
width: 40%;
}
</style>
<script type="text/javascript">
function showHideHeaders() {
var elements = document.getElementsByClassName("esitys");
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = (elements[i].style.display === "none" ? "table" : "none");
}
}
</script>
</head>
<body>
<div class="content">
<header>
<div class="header-right">
\nimi<br/>
\pvm
</div>
<div class="header-left">
<a href="#" onclick="showHideHeaders();">Näytä/Piilota esitykset</a>
</div>
<div class="header-center"><img class="logo" src="/opkh/mayk.png" /></div>
</header>
<p><b>Aika:</b> \aika</p>
<p><b>Paikka:</b> \paikka</p>
\ifndef{väritön}<p><vapaa>Puheenvuoroja myönnetään</vapaa> &mdash; <erikseen>Itsenäisiä puheenvuoroja myönnetään</erikseen> &mdash;
<varattava>Puheenvuorot varattava etukäteen</varattava> &mdash; <ei>Puheenvuoroja ei myönnetä</ei></p>\endif}
\define{Loppu}{
<footer>
Maunulan yhteiskoulun lukio ja Helsingin matematiikkalukio<br/>
<b><i>Opiskelijakunta</i></b>
</footer>
</div>
</body>
</html>}
\define{Asiat}{<p>Käsiteltävät asiat:</p><ol>}
\define{\Kohta{teksti}}{<li class="esitys"><i>\teksti</i></li>}
\define{\Neutraali{teksti}}{<li><b>\teksti</b></li>}
\define{\Vapaa{teksti}}{<li><vapaa>\teksti</vapaa></li>}
\define{\Varattava{teksti}}{<li><varattava>\teksti</varattava></li>}
\define{\EiMyönnetä{teksti}}{<li><ei>\teksti</ei></li>}
\define{\Erikseen{teksti}}{<li><erikseen>\teksti</erikseen></li>}
\define{\Kohdat{otsikko}}{<li>\otsikko<ol>}
\define{\Neutraalit{teksti}}{<li><b>\teksti</b><ol>}
\define{\Vapaat{otsikko}}{<li><vapaa>\otsikko</vapaa><ol>}
\define{\Varattavat{otsikko}}{<li><varattava>\otsikko</varattava><ol>}
\define{LKohdat}{</ol></li>}
\define{LAsiat}{</ol>}
#!/bin/bash
cd apitmp
cat $1.txt | java -jar pkgen.jar >$1.tex
pdflatex $1.tex 2>/dev/null >/dev/null
pdflatex $1.tex 2>/dev/null >/dev/null
if [ ! "$2" ]; then rm $1.{aux,log,tex}; fi
if [ -e "../tiedostot/$1.pdf" ]; then
now=`date +"%s"`
mv "../tiedostot/$1.pdf" "../tiedostot/$1.pdf.$now.backup"
fi
mv $1.pdf ../tiedostot
{
http := require("http_server")
}
filter cond {
push item for item if cond(item)
}
/* LINKS CONTROLLER */
record PkController : http.Controller {
@http.handle "/opkh/(pk|el|yk|at|it|lk|rk|sm|vi|hp)/[0-9]+"
function handleLink request {
matches := match("/opkh/(pk|el|yk|at|it|lk|rk|sm|vi|hp)/([0-9]+)", request.path)
doc_type := matches[1]
id := matches[2]
file := "tiedostot/"..doc_type..id..".txt"
if fileExists(file) do
content := [{}()|bufferedExec("gpp", "-T", file) | filter { |l|; return #l > 0 and l[:1] != "#" }] & "\n"
request.send "200 OK", content
return
done
file := "tiedostot/"..doc_type..id..".pdf"
if fileExists(file) do
request.sendFile "application/pdf", file
return
done
request.send404
}
@http.handle "/opkh/mayk\\.png"
function handleMaykpng request {
request.sendFile "image/png", "tiedostot/mayk.png"
}
@http.handle "/opkh/novellit.pdf"
function handleNovellitpdf request {
request.sendFile "application/pdf", "tiedostot/novellit.pdf"
}
@http.handle "/opkh/?"
function handleRoot request {
links := []
for doc_type in ["pk", "el", "yk", "at", "it", "lk", "rk", "sm", "vi", "hp"] do
seq 1, 50 | for id do
files := (("tiedostot/"..doc_type..id) ... [".txt", ".pdf"])
link := ""
for file in files do
if fileExists(file) do
link = "<a href=\"/opkh/"..doc_type.."/"..id.."\">"..longName(doc_type).." "..id.."</a>"
break
done
done
if [ link != "" ] do
links += link
done
done
done
/* TODO älä kovakoodaa */
links += "<a href=\"/opkh/novellit.pdf\">Halloween-novellit</a>"
request.send "200 OK", "<html>
<head><title>OPKH:n tiedostot</title></head>
<body>
<h1>OPKH:n tiedostot</h1>
<p>Saatavilla olevat tiedostot:</p>
<ul>"..[push("<li>"..link.."</li>") for link in links]&" ".."</ul>
</body></html>"
}
@http.handle "/opkh/sihteeri/?"
function handleSecretaryAPI request {
if [ request.command = "POST" ] do
apikey := request.form["apikey"]
pknum := request.form["pknum"]
unless [ pknum =~ "[0-9]+" ] do
request.send403
return
done
pktext := request.form["pktext"]
if [ apikey = "INSERT PASSWORD HERE" ] do
pktype := push("yk") if [ request.form["pktype"] = "yk" ] else push("pk")
errprint "[".. timeString() .."] " .. "Secretary API: Authorized request ("..pktype..pknum..")\n"
file := "apitmp/" .. pktype .. pknum .. ".txt"
if fileExists(file) do
errprint "[".. timeString() .."] " .. "Secretary API: Creating backup...\n"
{}|bufferedExec("mv", file, file.."."..currentTime()..".backup") | {}
done
for tag in [push(pktext) | search("\\\\(\\w*|\\\\)")] do
if [ tag != "\\begin" and tag != "\\end" and tag != "\\textbf" and tag != "\\textit" and tag != "\\\\" and tag != "\\euro" and tag != "\\today" and tag != "\\_" and tag != "\\item" ] do
request.send "403 Forbidden", "<html>
<head><meta charset=\"UTF-8\" /><title>Sihteerirajapinta</title></head>
<body>
<h1>Sihteerirajapinta</h1>
<p>403 Forbidden - Teksti sisältää kielletyn komennon: <tt>"..tag.."</tt>.</p>
</body>
</html>"
return
done
done
push(pktext) | writeStrings(file)
errprint "[".. timeString() .."] " .. "Secretary API: Executing makepk.sh...\n"
{} | bufferedExec("./makepk.sh", pktype .. pknum) | {}
request.redirect "/opkh/"..pktype.."/"..pknum
else
errprint "[".. timeString() .."] " .. "Secretary API: Unauthorized request (pk"..pknum..")\n"
request.send "401 Unauthorized", "<html>
<head><meta charset=\"UTF-8\" /><title>Sihteerirajapinta</title></head>
<body>
<h1>Sihteerirajapinta</h1>
<p>401 Unauthorized - Väärä avainkoodi.</p>
</body>
</html>"
done
else
request.send "200 OK", "<html>
<head><meta charset=\"UTF-8\" /><title>Sihteerirajapinta</title></head>
<body>
<h1>Sihteerirajapinta</h1>
<p>Tämän lomakkeen avulla voi lähettää uuden pöytäkirjan palvelimelle.</p>
<form action=\"/opkh/sihteeri/\" method=\"POST\">
<p>Avainkoodi <input type=\"password\" name=\"apikey\" /></p>
<p><select name=\"pktype\">
<option value=\"pk\">Hallituksen kokouksen pöytäkirja</option>
<option value=\"yk\">Yleiskokouksen pöytäkirja</option>
</select> nro <input type=\"text\" name=\"pknum\" /></p>
<textarea name=\"pktext\">Pöytäkirjan teksti</textarea>
<p><input type=\"submit\" value=\"Lähetä\" /></p>
</form>
</body>
</html>"
done
}
function handle request {
request.send403
}
}
function longName(doc_type) {
return "Pöytäkirja nro" if [ doc_type = "pk" ]
return "Esityslista nro" if [ doc_type = "el" ]
return "Automaattityöryhmän muistio nro" if [ doc_type = "at" ]
return "Viestintätyöryhmän muistio nro" if [ doc_type = "it" ]
return "Lukiolaisten tilan työryhmän muistio nro" if [ doc_type = "lk" ]
return "Ruokailutyöryhmän muistio nro" if [ doc_type = "rk" ]
return "Sosiaalisen median työryhmän muistio nro" if [ doc_type = "sm" ]
return "Koulun siisteyden ja viihtyvyyden työryhmän muistio nro" if [ doc_type = "vi" ]
return "Hupitoimikunnan pöytäkirja nro" if [ doc_type = "hp" ]
return "Yleiskokouksen pöytäkirja nro" if [ doc_type = "yk" ]
}
function timeString() {
{} | bufferedExec("date", "+%d.%m.%Y/%H:%M") | head(1)
}
/* MAIN PROCEDURE */
main {
http_server := new http.HttpServer(25565)
http_server.controllers["/"] = http.controller({ |request|
request.redirect "/opkh"
})
ctrl := new PkController
http_server.controllers["/opkh"] = ctrl
date = "[".. timeString() .."]"
print date.." Started."
{} | while true; do
http_server.update
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment