A working version!

This commit is contained in:
Neale Pickett 2014-07-15 01:30:47 +00:00
commit da63df8e4e
2 changed files with 180 additions and 0 deletions

73
chunktail.cgi.go Normal file
View File

@ -0,0 +1,73 @@
package main
import (
"fmt"
"log"
"os"
"bufio"
"strconv"
"net/http"
"net/http/cgi"
"time"
)
type Handler struct {
cgi.Handler
}
func tail(w http.ResponseWriter, pos int) {
f, err := os.Open("/home/neale/bot/neale/log/log")
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = f.Seek(int64(pos), 0)
if err != nil {
log.Fatal(err)
}
bf := bufio.NewScanner(f)
for bf.Scan() {
t := bf.Text()
pos += len(t) + 1 // XXX: this breaks if we ever see \r\n
fmt.Fprintf(w, "data: %s\n", t)
}
fmt.Fprintf(w, "id: %d\n\n", pos)
}
func handleCommand(w http.ResponseWriter, text string) {
fn := fmt.Sprintf("/home/neale/bot/neale/outq/cgi.%d", time.Now().Unix())
f, err := os.Create(fn)
if err != nil {
fmt.Fprintln(w, "NO")
fmt.Fprintln(w, err)
return
}
defer f.Close()
fmt.Fprintf(f, "PRIVMSG #tron :%s\n", text)
fmt.Fprintln(w, "OK")
}
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.FormValue("type") {
case "command":
w.Header().Set("Content-Type", "text/plain")
handleCommand(w, r.Form.Get("text"))
default:
w.Header().Set("Content-Type", "text/event-stream")
id, _ := strconv.Atoi(os.Getenv("HTTP_LAST_EVENT_ID"))
tail(w, id)
}
}
func main() {
h := Handler{}
if err := cgi.Serve(h); err != nil {
log.Fatal(err)
}
}

107
index.html Normal file
View File

@ -0,0 +1,107 @@
<!DOCTYPE html>
<html>
<head>
<title>LOL</title>
<script type="application/javascript">
var msgRe = /([^ ]+) (<[^>]+>) (.*)/;
function addMessagePart(p, className, text) {
var e = document.createElement("span");
e.className = className;
e.appendChild(document.createTextNode(text));
p.appendChild(e);
p.appendChild(document.createTextNode(" "));
}
function addMessage(txt) {
var ts = txt.substr(0, 19);
var msg = txt.substr(20);
var a = document.getElementById("a");
var p = document.createElement("p");
m = msg.match(msgRe);
addMessagePart(p, "timestamp", ts);
if (m) {
addMessagePart(p, "forum", m[1]);
addMessagePart(p, "sender", m[2]);
addMessagePart(p, "text", m[3]);
} else {
addMessagePart(p, "raw", msg);
}
a.appendChild(p);
p.scrollIntoView(false);
}
function newmsg(event) {
msgs = event.data.split("\n");
for (var i = 0; i < msgs.length; i += 1) {
addMessage(msgs[i]);
}
}
function handleCommand(event) {
console.log(event);
window.evt = event;
var oReq = new XMLHttpRequest();
function reqListener() {
}
oReq.onload = reqListener;
oReq.open("POST", "chunktail.cgi?post=1", true);
oReq.send(new FormData(event.target));
event.target.reset();
return false;
}
function init() {
var source = new EventSource("chunktail.cgi");
source.onmessage = newmsg;
document.getElementById("command").onsubmit = handleCommand;
}
window.onload = init;
</script>
<style type="text/css">
#a {
max-height: 20em;
overflow-y: scroll;
overflow-x: hidden;
}
#a p {
margin: 0em 0em 0em 4em;
text-indent: -4em;
}
.timestamp {
color: silver;
}
.forum {
color: darkblue;
}
.sender {
color: green;
}
.raw {
color: purple;
}
#command {
width: 75%;
}
</style>
</head>
<body>
<div id="a"></div>
<form id="command">
<input type="hidden" name="type" value="command">
<input name="text" autofocus>
<input type="Submit" value="Send">
</form>
</body>
</html>