Signed-off-by: Martyn Ranyard <m@rtyn.berlin>
This commit is contained in:
Martyn 2020-06-30 17:30:05 +02:00
parent 34403053ac
commit f574863601
2 changed files with 48 additions and 48 deletions

View File

@ -181,6 +181,7 @@ func (bb *KardBot) HandleChat() error {
case "PRIVMSG": case "PRIVMSG":
msg := matches[4] msg := matches[4]
rgb.GPrintf("[%s] %s: %s\n", TimeStamp(), userName, msg) rgb.GPrintf("[%s] %s: %s\n", TimeStamp(), userName, msg)
rgb.GPrintf("[%s] raw line: %s\n", TimeStamp(), line)
// parse commands from user message // parse commands from user message
cmdMatches := CmdRegex.FindStringSubmatch(msg) cmdMatches := CmdRegex.FindStringSubmatch(msg)

View File

@ -7,15 +7,15 @@ import (
"github.com/gorilla/handlers" "github.com/gorilla/handlers"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"encoding/json"
"fmt" "fmt"
"html/template" "html/template"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings" "strings"
"time" "time"
"encoding/json"
"io/ioutil"
) )
//var store = sessions.NewCookieStore(os.Getenv("SESSION_KEY")) //var store = sessions.NewCookieStore(os.Getenv("SESSION_KEY"))
@ -102,7 +102,7 @@ func TemplateHandler(response http.ResponseWriter, request *http.Request) {
// NotFoundHandler(response, request) // NotFoundHandler(response, request)
// return // return
} }
var td = TemplateData{ircBot.Prompts[rand.Intn(len(ircBot.Prompts))], len(ircBot.Prompts), ircBot.ActiveChannels(), 0, ircBot.AppCredentials.ClientID, "https://"+ircBot.Config.ExternalUrl} var td = TemplateData{ircBot.Prompts[rand.Intn(len(ircBot.Prompts))], len(ircBot.Prompts), ircBot.ActiveChannels(), 0, ircBot.AppCredentials.ClientID, "https://" + ircBot.Config.ExternalUrl}
err = tmpl.Execute(response, td) err = tmpl.Execute(response, td)
if err != nil { if err != nil {
http.Error(response, err.Error(), http.StatusInternalServerError) http.Error(response, err.Error(), http.StatusInternalServerError)
@ -184,7 +184,7 @@ func UnauthorizedHandler(response http.ResponseWriter, request *http.Request) {
tmpl.Execute(response, nil) tmpl.Execute(response, nil)
} }
func twitchHTTPClient(call string, bearer string) (string,error) { func twitchHTTPClient(call string, bearer string) (string, error) {
url := "https://api.twitch.tv/helix/" + call url := "https://api.twitch.tv/helix/" + call
var bearerHeader = "Bearer " + bearer var bearerHeader = "Bearer " + bearer
@ -194,7 +194,7 @@ func twitchHTTPClient(call string, bearer string) (string,error) {
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return "",err return "", err
} }
defer resp.Body.Close() defer resp.Body.Close()
@ -205,7 +205,7 @@ func twitchHTTPClient(call string, bearer string) (string,error) {
func TwitchAdminHandler(response http.ResponseWriter, request *http.Request) { func TwitchAdminHandler(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request) vars := mux.Vars(request)
if (vars["code"] != "") { if vars["code"] != "" {
response.Header().Add("Content-type", "text/plain") response.Header().Add("Content-type", "text/plain")
resp, err := http.PostForm( resp, err := http.PostForm(
"https://id.twitch.tv/oauth2/token", "https://id.twitch.tv/oauth2/token",
@ -214,7 +214,7 @@ func TwitchAdminHandler(response http.ResponseWriter, request *http.Request) {
"client_secret": {ircBot.AppCredentials.Password}, "client_secret": {ircBot.AppCredentials.Password},
"code": {vars["code"]}, "code": {vars["code"]},
"grant_type": {"authorization_code"}, "grant_type": {"authorization_code"},
"redirect_uri": {"https://"+ircBot.Config.ExternalUrl+"/twitchadmin"}}) "redirect_uri": {"https://" + ircBot.Config.ExternalUrl + "/twitchadmin"}})
if err != nil { if err != nil {
response.WriteHeader(500) response.WriteHeader(500)
response.Header().Add("Content-type", "text/plain") response.Header().Add("Content-type", "text/plain")
@ -267,20 +267,19 @@ func TwitchAdminHandler(response http.ResponseWriter, request *http.Request) {
user := usersObject.Data[0] user := usersObject.Data[0]
magicCode := ircBot.ReadOrCreateChannelKey(user.Login) magicCode := ircBot.ReadOrCreateChannelKey(user.Login)
url := "https://"+ircBot.Config.ExternalUrl+"/admin/"+user.Login+"/"+magicCode url := "https://" + ircBot.Config.ExternalUrl + "/admin/" + user.Login + "/" + magicCode
http.Redirect(response, request, url, http.StatusFound) http.Redirect(response, request, url, http.StatusFound)
} else { } else {
fmt.Fprintf(response, "I'm not okay jack! %v \n", vars) fmt.Fprintf(response, "I'm not okay jack! %v \n", vars)
for key, val := range(vars) { for key, val := range vars {
fmt.Fprint(response, "%s = %s\n", key, val) fmt.Fprint(response, "%s = %s\n", key, val)
} }
} }
} }
func TwitchBackendHandler(response http.ResponseWriter, request *http.Request){ func TwitchBackendHandler(response http.ResponseWriter, request *http.Request) {
response.Header().Add("Content-type", "text/plain") response.Header().Add("Content-type", "text/plain")
vars := mux.Vars(request) vars := mux.Vars(request)
// fmt.Fprintf(response, "I'm okay jack! %v \n", vars) // fmt.Fprintf(response, "I'm okay jack! %v \n", vars)
@ -288,7 +287,7 @@ func TwitchBackendHandler(response http.ResponseWriter, request *http.Request){
// fmt.Fprint(response, "%s = %s\n", key, val) // fmt.Fprint(response, "%s = %s\n", key, val)
// } // }
if (vars["code"] != "") { if vars["code"] != "" {
// https://id.twitch.tv/oauth2/token // https://id.twitch.tv/oauth2/token
// ?client_id=<your client ID> // ?client_id=<your client ID>
// &client_secret=<your client secret> // &client_secret=<your client secret>
@ -309,7 +308,7 @@ func TwitchBackendHandler(response http.ResponseWriter, request *http.Request){
"client_secret": {ircBot.AppCredentials.Password}, "client_secret": {ircBot.AppCredentials.Password},
"code": {vars["code"]}, "code": {vars["code"]},
"grant_type": {"authorization_code"}, "grant_type": {"authorization_code"},
"redirect_uri": {"https://"+ircBot.Config.ExternalUrl+"/twitchadmin"}}) "redirect_uri": {"https://" + ircBot.Config.ExternalUrl + "/twitchadmin"}})
if err != nil { if err != nil {
response.Header().Add("Content-type", "text/plain") response.Header().Add("Content-type", "text/plain")
fmt.Fprint(response, "ERROR: "+err.Error()) fmt.Fprint(response, "ERROR: "+err.Error())
@ -340,8 +339,8 @@ func HandleHTTP(passedIrcBot *irc.KardBot) {
r.HandleFunc("/admin/{channel}/{key}", AdminHandler) r.HandleFunc("/admin/{channel}/{key}", AdminHandler)
//r.HandleFunc("/twitchadmin", TwitchAdminHandler) //r.HandleFunc("/twitchadmin", TwitchAdminHandler)
//r.HandleFunc("/twitchtobackend", TwitchBackendHandler) //r.HandleFunc("/twitchtobackend", TwitchBackendHandler)
r.Path("/twitchtobackend").Queries("access_token","{access_token}","scope","{scope}","token_type","{token_type}").HandlerFunc(TwitchBackendHandler) r.Path("/twitchtobackend").Queries("access_token", "{access_token}", "scope", "{scope}", "token_type", "{token_type}").HandlerFunc(TwitchBackendHandler)
r.Path("/twitchadmin").Queries("code","{code}","scope","{scope}").HandlerFunc(TwitchAdminHandler) r.Path("/twitchadmin").Queries("code", "{code}", "scope", "{scope}").HandlerFunc(TwitchAdminHandler)
http.Handle("/", r) http.Handle("/", r)
srv := &http.Server{ srv := &http.Server{
Handler: loggedRouter, Handler: loggedRouter,