Back on track with authentication wotzit

This commit is contained in:
Neale Pickett 2014-07-24 03:41:55 +00:00
parent fa18efdad9
commit 8e9ce19272
3 changed files with 31 additions and 26 deletions

View File

@ -5,17 +5,14 @@
<script type="application/javascript" src="irc.js"> <script type="application/javascript" src="irc.js">
</script> </script>
<style type="text/css"> <style type="text/css">
#a, #kiboze { #a {
max-height: 20em; max-height: 20em;
overflow: scroll; overflow-y: scroll;
overflow-x: hidden;
} }
#a p, #kiboze p { #a p {
margin: 0em 0em 0em 10em; margin: 0em 0em 0em 4em;
text-indent: -10em; text-indent: -4em;
}
#kiboze {
background-color: #ddd;
max-height: 6em;
} }
.timestamp { .timestamp {
color: silver; color: silver;
@ -30,22 +27,18 @@
color: purple; color: purple;
} }
input[name~=text] { #command {
width: 60%; width: 75%;
}
input[name~=target] {
width: 8em;
} }
</style> </style>
</head> </head>
<body> <body>
<div id="a"></div> <div id="a"></div>
<form id="command"> <form id="command">
<input type="hidden" name="auth" value="" id="authtok">
<input type="hidden" name="type" value="command"> <input type="hidden" name="type" value="command">
<input name="target" value="#tron">
<input name="text" autofocus> <input name="text" autofocus>
<input type="Submit" value="Send"> <input type="Submit" value="Send">
</form> </form>
<div id="kiboze"></div>
</body> </body>
</html> </html>

View File

@ -2,10 +2,12 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"bufio" "bufio"
"strconv" "strconv"
"strings"
"net/http" "net/http"
"net/http/cgi" "net/http/cgi"
"time" "time"
@ -15,6 +17,7 @@ type Handler struct {
cgi.Handler cgi.Handler
} }
var authtok string
func tail(w http.ResponseWriter, pos int) { func tail(w http.ResponseWriter, pos int) {
f, err := os.Open("/home/neale/public_html/irc/log") f, err := os.Open("/home/neale/public_html/irc/log")
@ -53,6 +56,11 @@ func handleCommand(w http.ResponseWriter, text string) {
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.FormValue("auth") != authtok {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "NO")
return
}
switch r.FormValue("type") { switch r.FormValue("type") {
case "command": case "command":
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
@ -65,6 +73,12 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func main() { func main() {
authtokbytes, err := ioutil.ReadFile("authtok")
if err != nil {
log.Fatal("Cannot read authtok")
}
authtok = strings.TrimSpace(string(authtokbytes))
h := Handler{} h := Handler{}
if err := cgi.Serve(h); err != nil { if err := cgi.Serve(h); err != nil {
log.Fatal(err) log.Fatal(err)

18
irc.js
View File

@ -1,5 +1,5 @@
var msgRe = /([^ ]+) (<[^>]+>) (.*)/; var msgRe = /([^ ]+) (<[^>]+>) (.*)/;
var kibozeRe = "neal"; var authtok;
function addMessagePart(p, className, text) { function addMessagePart(p, className, text) {
var e = document.createElement("span"); var e = document.createElement("span");
@ -32,18 +32,13 @@ function addMessage(txt) {
return; return;
break; break;
case "PRIVMSG": case "PRIVMSG":
addMessagePart(p, "forum", forum);
addMessagePart(p, "sender", sender); addMessagePart(p, "sender", sender);
addMessagePart(p, "forum", forum);
addMessagePart(p, "text", msg); addMessagePart(p, "text", msg);
if (-1 != msg.search(kibozeRe)) {
var k = document.getElementById("kiboze");
var p2 = p.cloneNode(true);
k.insertBefore(p2, k.firstChild);
}
break; break;
default: default:
addMessagePart(p, "forum", forum);
addMessagePart(p, "sender", sender); addMessagePart(p, "sender", sender);
addMessagePart(p, "forum", forum);
addMessagePart(p, "raw", command + " " + args + " " + msg); addMessagePart(p, "raw", command + " " + args + " " + msg);
break; break;
} }
@ -65,7 +60,7 @@ function handleCommand(event) {
function reqListener() { function reqListener() {
} }
oReq.onload = reqListener; oReq.onload = reqListener;
oReq.open("POST", "chunktail.cgi?post=1", true); oReq.open("POST", "irc.cgi", true);
oReq.send(new FormData(event.target)); oReq.send(new FormData(event.target));
event.target.reset(); event.target.reset();
@ -74,7 +69,10 @@ function handleCommand(event) {
} }
function init() { function init() {
var source = new EventSource("chunktail.cgi"); var authtok = prompt("Auth token", "");
document.getElementById("authtok").value = authtok;
var source = new EventSource("irc.cgi?auth=" + authtok);
source.onmessage = newmsg; source.onmessage = newmsg;
document.getElementById("command").onsubmit = handleCommand; document.getElementById("command").onsubmit = handleCommand;