aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--webhooks.go60
1 files changed, 42 insertions, 18 deletions
diff --git a/webhooks.go b/webhooks.go
index 4e0d5b1..d544103 100644
--- a/webhooks.go
+++ b/webhooks.go
@@ -10,8 +10,12 @@ import (
)
const TOKEN_FNAME = "tokens.txt"
-const SOCKET_NAME = "./bees.sock"
+const SOCKET_DIR = "/srv/apiobot/ch/"
+// const SOCKET_DIR = "./socks/"
+var CHANNELS = [...]string{"a","b","g"}
+
var tokens map[string]string
+var msg_chs map[string]chan<- beeMsg
func loadTokens() map[string]string {
out := make(map[string]string)
@@ -45,19 +49,26 @@ func sendSock(msgs <-chan beeMsg, w io.Writer) {
}
}
-func handleHttp(msgch chan<- beeMsg, w http.ResponseWriter, r *http.Request) {
- tok := r.FormValue("token")
- msg := r.FormValue("msg")
- if tok == "" || msg == "" {
+func handleHttp(w http.ResponseWriter, r *http.Request) {
+ tok := r.FormValue("key")
+ msg := r.FormValue("message")
+ irc_ch := r.FormValue("channel")
+
+ if tok == "" {
+ w.WriteHeader(400)
+ fmt.Fprintln(w,"missing key")
+ return
+ }
+ if msg == "" {
w.WriteHeader(400)
- fmt.Fprintln(w,"missing form values")
+ fmt.Fprintln(w,"missing message")
return
}
clientname, ok := tokens[tok]
if !ok {
w.WriteHeader(401)
- fmt.Fprintln(w,"unknown token")
+ fmt.Fprintln(w,"bad key")
return
}
@@ -67,26 +78,39 @@ func handleHttp(msgch chan<- beeMsg, w http.ResponseWriter, r *http.Request) {
return
}
- msgch <- beeMsg{from: clientname, text:msg}
+ if irc_ch == "" {
+ irc_ch = "a"
+ }
+
+ msg_ch, ok := msg_chs[irc_ch]
+ if !ok {
+ w.WriteHeader(400)
+ fmt.Fprintf(w,"invalid channel name, valid channels are %v.",CHANNELS)
+ return
+ }
+
+ msg_ch <- beeMsg{from: clientname, text:msg}
}
func init() {
tokens = loadTokens()
+ msg_chs = make(map[string]chan<- beeMsg)
+ for _,ch := range CHANNELS {
+ sock, err := net.Dial("unixgram", SOCKET_DIR+ch+".sock")
+ if err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+ mc := make(chan beeMsg)
+ msg_chs[ch] = mc
+ go sendSock(mc, sock)
+ }
}
func main() {
- msgs := make(chan beeMsg)
fmt.Println("ok")
- sock, err := net.Dial("unixgram", SOCKET_NAME)
- if err != nil {
- fmt.Println(err)
- }
- go sendSock(msgs, sock)
-
- http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- handleHttp(msgs,w,r)
- })
+ http.HandleFunc("/", handleHttp)
log.Fatal(http.ListenAndServe(":3236", nil))