diff options
author | ubq323 <ubq323@ubq323.website> | 2022-11-29 15:55:15 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2022-11-29 15:56:36 +0000 |
commit | 60a4a26ce76850f3d374d7206e702fbc6548f58b (patch) | |
tree | bb0574b3c2c8a5ee7e377f4417b764d494a93d82 | |
parent | 4bf0ff599bb2761e4156cd5a26639045ce71d29a (diff) |
update webhooks server thing to support multiple channels
-rw-r--r-- | webhooks.go | 60 |
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)) |