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,37 +7,37 @@ 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"))
type twitchauthresponse struct { type twitchauthresponse struct {
Access_token string `json: "access_token"` Access_token string `json: "access_token"`
Expires_in int `json: "expires_in"` Expires_in int `json: "expires_in"`
Refresh_token string `json: "refresh_token"` Refresh_token string `json: "refresh_token"`
Scope []string `json: "scope"` Scope []string `json: "scope"`
Token_type string `json: "token_type"` Token_type string `json: "token_type"`
} }
type twitchUser struct { type twitchUser struct {
Id string `json: "id"` Id string `json: "id"`
Login string `json: "login"` Login string `json: "login"`
Display_name string `json: "display_name"` Display_name string `json: "display_name"`
Type string `json: "type"` Type string `json: "type"`
Broadcaster_type string `json: "affiliate"` Broadcaster_type string `json: "affiliate"`
Description string `json: "description"` Description string `json: "description"`
Profile_image_url string `json: "profile_image_url"` Profile_image_url string `json: "profile_image_url"`
Offline_image_url string `json: "offline_image_url"` Offline_image_url string `json: "offline_image_url"`
View_count int `json: "view_count"` View_count int `json: "view_count"`
} }
type twitchUsersBigResponse struct { type twitchUsersBigResponse struct {
@ -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,37 +184,37 @@ 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
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", bearerHeader) req.Header.Add("Authorization", bearerHeader)
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()
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
return string([]byte(body)), nil return string([]byte(body)), nil
} }
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",
url.Values{ url.Values{
"client_id": {ircBot.AppCredentials.ClientID}, "client_id": {ircBot.AppCredentials.ClientID},
"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,12 +287,12 @@ 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>
// &code=<authorization code received above> // &code=<authorization code received above>
// &grant_type=authorization_code // &grant_type=authorization_code
// &redirect_uri=<your registered redirect URI> // &redirect_uri=<your registered redirect URI>
// ircBot.AppCredentials.ClientID // ircBot.AppCredentials.ClientID
@ -305,11 +304,11 @@ func TwitchBackendHandler(response http.ResponseWriter, request *http.Request){
resp, err := http.PostForm( resp, err := http.PostForm(
"https://id.twitch.tv/oauth2/token", "https://id.twitch.tv/oauth2/token",
url.Values{ url.Values{
"client_id": {ircBot.AppCredentials.ClientID}, "client_id": {ircBot.AppCredentials.ClientID},
"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,