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":
msg := matches[4]
rgb.GPrintf("[%s] %s: %s\n", TimeStamp(), userName, msg)
rgb.GPrintf("[%s] raw line: %s\n", TimeStamp(), line)
// parse commands from user message
cmdMatches := CmdRegex.FindStringSubmatch(msg)

View File

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