Skip to content

Instantly share code, notes, and snippets.

@OrangeCrush
Forked from 21isgonnabeagoodyear/chat.go
Last active August 29, 2015 14:09
Show Gist options
  • Save OrangeCrush/339197c174ec8eabb82d to your computer and use it in GitHub Desktop.
Save OrangeCrush/339197c174ec8eabb82d to your computer and use it in GitHub Desktop.
package chat
import "net/http"
import "sync"
import "html/template"
import "time"
import "strings"
type chatpost struct{
text string
when time.Time
ip string
}
var chatlock sync.Mutex
//var chatstrs [20]string
var chatstrs [20]chatpost
var nicks map[string] string = make(map[string]string)
var numchats int
func startswith(source string, lookfor string) string{
if len(source) <= len(lookfor){return ""}
if source[:len(lookfor)] != lookfor{return ""}
return source[len(lookfor):]
}
func ChatFunc(
w http.ResponseWriter,
r *http.Request) {
chatlock.Lock()
if r.Method == "POST"{
st := r.PostFormValue("chat")
chatstrs[numchats].ip = strings.Split(r.RemoteAddr, ":")[0]
chatstrs[numchats].text = st
chatstrs[numchats].when = time.Now()
if rem := startswith(chatstrs[numchats].text, "my name is "); rem != ""{nicks[chatstrs[numchats].ip] = rem}
if rem := startswith(chatstrs[numchats].text, "jag heter "); rem != ""{nicks[chatstrs[numchats].ip] = rem}
numchats ++
numchats %= 20
}
w.Write([]byte("<html><body>"))
w.Write([]byte("<noscript>NOTE:automatic updating will not work without javascript</noscript>"))
w.Write([]byte("<script type=\"text/javascript\">setInterval(function(){var r = new XMLHttpRequest();r.onload=function(){document.getElementById(\"responses\").innerHTML = this.responseText.slice(this.responseText.indexOf(\"d\"+\"iv\")+19,this.responseText.lastIndexOf(\"d\"+\"iv\")-2);};r.open(\"get\", \"chat\", true);r.send()}, 3000)</script>"))
w.Write([]byte("<div id=\"responses\">"))
// w.Write([]byte("<scrypt type=\"text/javascript\">document.write(\"this page requires no javascript\");</script>"))
for i := (numchats+1)%20;i!= numchats;i=(i+1)%20{
if strings.Split(r.RemoteAddr, ":")[0] == chatstrs[i].ip{w.Write([]byte("<i>"))}
if nick, ok := nicks[chatstrs[i].ip]; ok{w.Write([]byte(template.HTMLEscapeString(nick)))}else{w.Write([]byte(chatstrs[i].ip))}
w.Write([]byte(": "))
if startswith(chatstrs[i].text, "spoiler:") != ""{w.Write([]byte("<span style=\"background:black\">"))}
if startswith(chatstrs[i].text, "http://") != "" || startswith(chatstrs[i].text, "https://") != ""{w.Write([]byte("<a target=\"blank\" href=\""));w.Write([]byte(template.HTMLEscapeString(chatstrs[i].text)));w.Write([]byte("\">"))}
w.Write([]byte(template.HTMLEscapeString(chatstrs[i].text)))
if startswith(chatstrs[i].text, "spoiler:") != ""{w.Write([]byte("</span>"))}
if startswith(chatstrs[i].text, "http://") != "" || startswith(chatstrs[i].text, "https://") != ""{w.Write([]byte("</a>"))}
w.Write([]byte("("))
//w.Write([]byte(strings.Split(time.Since(chatstrs[i].when).String(), ".")[0]+"s"))
w.Write([]byte(time.Since(chatstrs[i].when).String()))
w.Write([]byte(" ago)"))
w.Write([]byte("<br/>"))
if strings.Split(r.RemoteAddr, ":")[0] == chatstrs[i].ip{w.Write([]byte("</i>"))}
}//TODO:remove old things from nicks
w.Write([]byte("</div>"))
w.Write([]byte("<form action=\"chat\" method=\"post\">"))
w.Write([]byte("<input type=\"text\" name=\"chat\" autofocus=\"autofocus\"/>"))
w.Write([]byte("<input type=\"submit\" name=\"chat\"/>"))
w.Write([]byte(""))
w.Write([]byte(""))
w.Write([]byte("</form></body></html>"))
chatlock.Unlock()//que
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment