2023-09-07 20:12:09 +02:00
|
|
|
package main
|
2023-08-08 02:37:37 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-10-10 17:51:12 +02:00
|
|
|
"strings"
|
2023-10-10 16:49:00 +02:00
|
|
|
|
2023-10-10 17:51:12 +02:00
|
|
|
"github.com/gempir/go-twitch-irc/v4"
|
2023-10-10 16:49:00 +02:00
|
|
|
"github.com/google/uuid"
|
2023-08-08 02:37:37 +02:00
|
|
|
)
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// banphraseResponse is the data we receive back from the banphrase API
|
2023-08-08 02:37:37 +02:00
|
|
|
type banphraseResponse struct {
|
|
|
|
Banned bool `json:"banned"`
|
|
|
|
InputMessage string `json:"input_message"`
|
|
|
|
BanphraseData banphraseData `json:"banphrase_data"`
|
|
|
|
}
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// banphraseData contains details about why a message was banphrased.
|
2023-08-08 02:37:37 +02:00
|
|
|
type banphraseData struct {
|
|
|
|
Id int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Phrase string `json:"phrase"`
|
|
|
|
Length int `json:"length"`
|
|
|
|
Permanent bool `json:"permanent"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
banPhraseUrl = "https://pajlada.pajbot.com/api/v1/banphrases/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckMessage checks a given message against the banphrase api.
|
|
|
|
// returns false, "okay" if a message is allowed
|
|
|
|
// returns true and a string with the reason if it was banned.
|
|
|
|
// More information:
|
|
|
|
// https://gist.github.com/pajlada/57464e519ba8d195a97ddcd0755f9715
|
2023-09-07 20:12:09 +02:00
|
|
|
func (app *application) checkMessage(text string) (bool, string) {
|
2023-08-08 02:37:37 +02:00
|
|
|
// {"message": "AHAHAHAHA LUL"}
|
|
|
|
reqBody, err := json.Marshal(map[string]string{
|
|
|
|
"message": text,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-27 16:44:42 +02:00
|
|
|
app.Log.Error(err)
|
|
|
|
return true, "could not check banphrase api"
|
2023-08-08 02:37:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Post(banPhraseUrl, "application/json", bytes.NewBuffer(reqBody))
|
|
|
|
if err != nil {
|
2023-09-27 16:44:42 +02:00
|
|
|
app.Log.Error(err)
|
|
|
|
return true, "could not check banphrase api"
|
2023-08-08 02:37:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2023-10-03 17:01:12 +02:00
|
|
|
app.Log.Error(err)
|
2023-08-08 02:37:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var responseObject banphraseResponse
|
2023-10-10 18:03:13 +02:00
|
|
|
if err := json.Unmarshal(body, &responseObject); err != nil {
|
|
|
|
app.Log.Error(err)
|
|
|
|
return true, "could not check banphrase api"
|
|
|
|
}
|
2023-08-08 02:37:37 +02:00
|
|
|
|
|
|
|
// Bad Message
|
|
|
|
//
|
|
|
|
// {"phrase": "No gyazo allowed"}
|
|
|
|
reason := responseObject.BanphraseData.Name
|
|
|
|
if responseObject.Banned {
|
|
|
|
return true, fmt.Sprint(reason)
|
|
|
|
} else if !responseObject.Banned {
|
|
|
|
// Good message
|
|
|
|
return false, "okay"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Couldn't contact api so assume it was a bad message
|
|
|
|
return true, "Banphrase API couldn't be reached monkaS"
|
|
|
|
}
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// SendNoContext is used to send twitch replies without the full twitch.PrivateMessage context to be logged.
|
2023-10-10 17:51:12 +02:00
|
|
|
func (app *application) SendNoContext(target, message string) {
|
2023-08-08 02:37:37 +02:00
|
|
|
// Message we are trying to send is empty.
|
|
|
|
if len(message) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-03 16:35:34 +01:00
|
|
|
if target == "forsen" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-10 16:49:00 +02:00
|
|
|
identifier := uuid.NewString()
|
2024-02-29 21:17:39 +01:00
|
|
|
go app.Models.SentMessagesLogs.Insert(
|
|
|
|
target, message, "unavailable", "unavailable", "unavailable", "unavailable", identifier, "unavailable")
|
2023-10-10 17:51:12 +02:00
|
|
|
|
|
|
|
// Since messages starting with `.` or `/` are used for special actions
|
|
|
|
// (ban, whisper, timeout) and so on, we place an emote infront of it so
|
|
|
|
// the actions wouldn't execute. `!` and `$` are common bot prefixes so we
|
|
|
|
// don't allow them either.
|
|
|
|
if message[0] == '.' || message[0] == '/' || message[0] == '!' || message[0] == '$' {
|
|
|
|
message = ":tf: " + message
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the message for bad words before we say it
|
|
|
|
messageBanned, banReason := app.checkMessage(message)
|
|
|
|
if !messageBanned {
|
|
|
|
// In case the message we are trying to send is longer than the
|
|
|
|
// maximum allowed message length on twitch we split the message in two parts.
|
|
|
|
// Twitch has a maximum length for messages of 510 characters so to be safe
|
|
|
|
// we split and check at 500 characters.
|
|
|
|
// https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316
|
|
|
|
// TODO: Make it so it splits at a space instead and not in the middle of a word.
|
|
|
|
if len(message) > 500 {
|
|
|
|
firstMessage := message[0:499]
|
|
|
|
secondMessage := message[499:]
|
|
|
|
|
|
|
|
app.TwitchClient.Say(target, firstMessage)
|
|
|
|
app.TwitchClient.Say(target, secondMessage)
|
|
|
|
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// Message was fine.
|
|
|
|
go app.TwitchClient.Say(target, message)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Bad message, replace message and log it.
|
|
|
|
app.TwitchClient.Say(target, "[BANPHRASED] monkaS")
|
|
|
|
app.Log.Infow("banned message detected",
|
|
|
|
"target channel", target,
|
|
|
|
"message", message,
|
|
|
|
"ban reason", banReason,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// Send is used to send twitch replies and contains the necessary safeguards and logic for that.
|
|
|
|
// Send also logs the twitch.PrivateMessage contents into the database.
|
2023-10-10 17:51:12 +02:00
|
|
|
func (app *application) Send(target, message string, msgContext twitch.PrivateMessage) {
|
|
|
|
// Message we are trying to send is empty.
|
|
|
|
if len(message) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-03 16:35:34 +01:00
|
|
|
if target == "forsen" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-10 17:51:12 +02:00
|
|
|
commandName := strings.ToLower(strings.SplitN(msgContext.Message, " ", 3)[0][2:])
|
|
|
|
identifier := uuid.NewString()
|
2024-02-29 21:17:39 +01:00
|
|
|
go app.Models.SentMessagesLogs.Insert(
|
|
|
|
target, message, commandName, msgContext.User.Name, msgContext.User.ID, msgContext.Message, identifier, msgContext.Raw)
|
2023-10-10 16:49:00 +02:00
|
|
|
|
2023-08-08 02:37:37 +02:00
|
|
|
// Since messages starting with `.` or `/` are used for special actions
|
|
|
|
// (ban, whisper, timeout) and so on, we place an emote infront of it so
|
|
|
|
// the actions wouldn't execute. `!` and `$` are common bot prefixes so we
|
|
|
|
// don't allow them either.
|
|
|
|
if message[0] == '.' || message[0] == '/' || message[0] == '!' || message[0] == '$' {
|
|
|
|
message = ":tf: " + message
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the message for bad words before we say it
|
2023-09-07 20:12:09 +02:00
|
|
|
messageBanned, banReason := app.checkMessage(message)
|
2023-10-05 21:28:19 +02:00
|
|
|
if !messageBanned {
|
2023-08-08 02:37:37 +02:00
|
|
|
// In case the message we are trying to send is longer than the
|
|
|
|
// maximum allowed message length on twitch we split the message in two parts.
|
|
|
|
// Twitch has a maximum length for messages of 510 characters so to be safe
|
|
|
|
// we split and check at 500 characters.
|
|
|
|
// https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316
|
|
|
|
// TODO: Make it so it splits at a space instead and not in the middle of a word.
|
|
|
|
if len(message) > 500 {
|
|
|
|
firstMessage := message[0:499]
|
|
|
|
secondMessage := message[499:]
|
|
|
|
|
2023-09-07 20:12:09 +02:00
|
|
|
app.TwitchClient.Say(target, firstMessage)
|
|
|
|
app.TwitchClient.Say(target, secondMessage)
|
2023-08-08 02:37:37 +02:00
|
|
|
|
2023-10-05 21:28:19 +02:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// Message was fine.
|
2023-10-10 16:49:00 +02:00
|
|
|
go app.TwitchClient.Say(target, message)
|
2023-08-08 02:37:37 +02:00
|
|
|
return
|
|
|
|
}
|
2023-10-05 21:28:19 +02:00
|
|
|
} else {
|
|
|
|
// Bad message, replace message and log it.
|
|
|
|
app.TwitchClient.Say(target, "[BANPHRASED] monkaS")
|
|
|
|
app.Log.Infow("banned message detected",
|
|
|
|
"target channel", target,
|
|
|
|
"message", message,
|
|
|
|
"ban reason", banReason,
|
|
|
|
)
|
|
|
|
|
2023-08-08 02:37:37 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// SendNoBanphrase does not check the banphrase before sending a twitch mesage.
|
2023-10-10 16:49:00 +02:00
|
|
|
func (app *application) SendNoBanphrase(target, message string) {
|
|
|
|
// Message we are trying to send is empty.
|
|
|
|
if len(message) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-03 16:35:34 +01:00
|
|
|
if target == "forsen" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-10 16:49:00 +02:00
|
|
|
identifier := uuid.NewString()
|
2024-02-29 21:17:39 +01:00
|
|
|
go app.Models.SentMessagesLogs.Insert(
|
|
|
|
target, message, "unavailable", "unavailable", "unavailable", "unavailable", identifier, "unavailable")
|
2023-10-10 16:49:00 +02:00
|
|
|
|
|
|
|
// Since messages starting with `.` or `/` are used for special actions
|
|
|
|
// (ban, whisper, timeout) and so on, we place an emote infront of it so
|
|
|
|
// the actions wouldn't execute. `!` and `$` are common bot prefixes so we
|
|
|
|
// don't allow them either.
|
|
|
|
if message[0] == '.' || message[0] == '/' || message[0] == '!' || message[0] == '$' {
|
|
|
|
message = ":tf: " + message
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the message for bad words before we say it
|
|
|
|
// Message was fine.
|
|
|
|
go app.TwitchClient.Say(target, message)
|
|
|
|
}
|
|
|
|
|
2023-08-08 02:37:37 +02:00
|
|
|
// SendNoLimit does not check for the maximum message size.
|
|
|
|
// Used in sending commands from the database since the command has to have
|
|
|
|
// been gotten in there somehow. So it fits. Still checks for banphrases.
|
2023-09-07 20:12:09 +02:00
|
|
|
func (app *application) SendNoLimit(target, message string) {
|
2023-08-08 02:37:37 +02:00
|
|
|
// Message we are trying to send is empty.
|
|
|
|
if len(message) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since messages starting with `.` or `/` are used for special actions
|
|
|
|
// (ban, whisper, timeout) and so on, we place an emote infront of it so
|
|
|
|
// the actions wouldn't execute. `!` and `$` are common bot prefixes so we
|
|
|
|
// don't allow them either.
|
|
|
|
if message[0] == '.' || message[0] == '/' || message[0] == '!' || message[0] == '$' {
|
|
|
|
message = ":tf: " + message
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the message for bad words before we say it
|
2023-09-07 20:12:09 +02:00
|
|
|
messageBanned, banReason := app.checkMessage(message)
|
2023-08-08 02:37:37 +02:00
|
|
|
if messageBanned {
|
|
|
|
// Bad message, replace message and log it.
|
2023-10-10 16:49:00 +02:00
|
|
|
go app.TwitchClient.Say(target, "[BANPHRASED] monkaS")
|
2023-10-03 17:01:12 +02:00
|
|
|
app.Log.Infow("banned message detected",
|
2023-08-08 02:37:37 +02:00
|
|
|
"target channel", target,
|
|
|
|
"message", message,
|
|
|
|
"ban reason", banReason,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// In case the message we are trying to send is longer than the
|
|
|
|
// maximum allowed message length on twitch we split the message in two parts.
|
|
|
|
// Twitch has a maximum length for messages of 510 characters so to be safe
|
|
|
|
// we split and check at 500 characters.
|
|
|
|
// https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316
|
|
|
|
// TODO: Make it so it splits at a space instead and not in the middle of a word.
|
|
|
|
// Message was fine.
|
2023-10-10 16:49:00 +02:00
|
|
|
identifier := uuid.NewString()
|
2024-02-29 21:35:45 +01:00
|
|
|
go app.Models.SentMessagesLogs.Insert(
|
|
|
|
target, message, "unavailable", "unavailable", "unavailable", "unavailable", identifier, "unavailable")
|
2023-10-10 16:49:00 +02:00
|
|
|
go app.TwitchClient.Say(target, message)
|
2023-08-08 02:37:37 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|