2021-10-20 22:13:19 +02:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-10-20 22:59:05 +02:00
|
|
|
// banphraseResponse is the data we receive back from
|
|
|
|
// the banphrase API
|
2021-10-20 22:13:19 +02:00
|
|
|
type banphraseResponse struct {
|
|
|
|
Banned bool `json:"banned"`
|
|
|
|
InputMessage string `json:"input_message"`
|
|
|
|
BanphraseData banphraseData `json:"banphrase_data"`
|
|
|
|
}
|
|
|
|
|
2021-10-20 22:59:05 +02:00
|
|
|
// banphraseData contains details about why a message
|
|
|
|
// was banphrased.
|
2021-10-20 22:13:19 +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"`
|
|
|
|
}
|
|
|
|
|
2021-10-20 23:03:20 +02:00
|
|
|
var (
|
|
|
|
banPhraseUrl = "https://pajlada.pajbot.com/api/v1/banphrases/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckMessage checks a message against a banphrase api..
|
|
|
|
// returns false, "okay" if a message was allowed
|
|
|
|
// returns true, "[banphrased] monkaS" and the reason if not.
|
2021-10-20 22:59:05 +02:00
|
|
|
// More information:
|
|
|
|
// https://gist.github.com/pajlada/57464e519ba8d195a97ddcd0755f9715
|
2021-10-20 22:13:19 +02:00
|
|
|
func CheckMessage(text string) (bool, string) {
|
2021-10-22 22:24:13 +02:00
|
|
|
// log.Info("fn CheckMessage")
|
2021-10-20 22:13:19 +02:00
|
|
|
|
2021-10-20 22:59:05 +02:00
|
|
|
// {"message": "AHAHAHAHA LUL"}
|
2021-10-20 22:13:19 +02:00
|
|
|
reqBody, err := json.Marshal(map[string]string{
|
|
|
|
"message": text,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2021-10-20 23:03:20 +02:00
|
|
|
resp, err := http.Post(banPhraseUrl, "application/json", bytes.NewBuffer(reqBody))
|
2021-10-20 22:13:19 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseObject banphraseResponse
|
|
|
|
json.Unmarshal(body, &responseObject)
|
|
|
|
|
2021-10-20 22:59:05 +02:00
|
|
|
// {"phrase": "No gyazo allowed"}
|
2021-10-25 18:01:27 +02:00
|
|
|
// reason := responseObject.BanphraseData.Name
|
2021-10-20 22:43:06 +02:00
|
|
|
|
2021-10-20 22:59:05 +02:00
|
|
|
// log.Info("Bancheck: ", responseObject.Banned)
|
|
|
|
// log.Info("Reason: ", reason)
|
|
|
|
// log.Info("Bancheck: ", responseObject.Banned)
|
2021-10-20 22:43:06 +02:00
|
|
|
|
2021-10-20 22:59:05 +02:00
|
|
|
// Bad message
|
2021-10-20 22:13:19 +02:00
|
|
|
if responseObject.Banned {
|
2021-10-25 18:01:27 +02:00
|
|
|
return true, "[Banphrased]"
|
2021-10-22 22:29:31 +02:00
|
|
|
} else if !responseObject.Banned {
|
2021-10-20 22:59:05 +02:00
|
|
|
// Good message
|
2021-10-20 22:13:19 +02:00
|
|
|
return false, "okay"
|
|
|
|
}
|
|
|
|
|
2021-10-20 22:59:05 +02:00
|
|
|
// Couldn't contact api so assume it was a bad message
|
|
|
|
return true, "Banphrase API couldn't be reached monkaS"
|
2021-10-20 22:13:19 +02:00
|
|
|
}
|