2016-01-17 18:00:44 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
2013-12-10 23:37:07 +00:00
|
|
|
|
2016-03-24 17:32:40 +00:00
|
|
|
package talker
|
2012-08-17 22:09:29 +00:00
|
|
|
|
|
|
|
import (
|
2012-08-25 04:46:13 +00:00
|
|
|
"fmt"
|
2019-02-20 21:47:41 +00:00
|
|
|
"io/ioutil"
|
2019-02-21 14:48:48 +00:00
|
|
|
"net/http"
|
2019-02-20 21:47:41 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2012-08-25 04:49:48 +00:00
|
|
|
"strings"
|
2016-01-15 14:43:31 +00:00
|
|
|
|
2021-07-28 15:32:59 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2019-03-07 16:35:42 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2016-01-17 18:00:44 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
2016-04-01 14:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
2019-02-20 21:47:41 +00:00
|
|
|
"github.com/velour/catbase/config"
|
2012-08-17 22:09:29 +00:00
|
|
|
)
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
var goatse = []string{
|
2016-03-25 19:10:56 +00:00
|
|
|
"```* g o a t s e x * g o a t s e x * g o a t s e x *",
|
2013-02-05 19:52:11 +00:00
|
|
|
"g g",
|
|
|
|
"o / \\ \\ / \\ o",
|
|
|
|
"a| | \\ | | a",
|
|
|
|
"t| `. | | : t",
|
|
|
|
"s` | | \\| | s",
|
|
|
|
"e \\ | / / \\\\\\ --__ \\\\ : e",
|
|
|
|
"x \\ \\/ _--~~ ~--__| \\ | x",
|
|
|
|
"* \\ \\_-~ ~-_\\ | *",
|
|
|
|
"g \\_ \\ _.--------.______\\| | g",
|
2013-02-05 20:11:03 +00:00
|
|
|
"o \\ \\______// _ ___ _ \\_\\__> \\ | o",
|
|
|
|
"a \\ . C ___) ______ \\_\\____> | / a",
|
|
|
|
"t /\\ | C ____)/ \\ \\_____> |_/ t",
|
|
|
|
"s / /\\| C_____) | \\___> / \\ s",
|
|
|
|
"e | \\ _C_____)\\______/ // _/ / \\ e",
|
|
|
|
"x | \\ |__ \\\\_________// \\__/ | x",
|
2013-02-05 19:52:11 +00:00
|
|
|
"* | \\ \\____) `---- --' | *",
|
|
|
|
"g | \\_ ___\\ /_ _/ | g",
|
|
|
|
"o | / | | \\ | o",
|
|
|
|
"a | | / \\ \\ | a",
|
2013-02-05 20:11:03 +00:00
|
|
|
"t | / / |{nick}| \\ |t",
|
2013-02-05 19:52:11 +00:00
|
|
|
"s | / / \\__/\\___/ | |s",
|
|
|
|
"e | / | | | |e",
|
|
|
|
"x | | | | | |x",
|
2016-03-25 19:10:56 +00:00
|
|
|
"* g o a t s e x * g o a t s e x * g o a t s e x *```",
|
2013-02-05 19:52:11 +00:00
|
|
|
}
|
|
|
|
|
2012-08-17 22:09:29 +00:00
|
|
|
type TalkerPlugin struct {
|
2019-05-27 23:21:53 +00:00
|
|
|
bot bot.Bot
|
2019-02-20 21:47:41 +00:00
|
|
|
config *config.Config
|
2019-01-20 20:21:26 +00:00
|
|
|
sayings []string
|
2012-08-17 22:09:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
func New(b bot.Bot) *TalkerPlugin {
|
|
|
|
tp := &TalkerPlugin{
|
2019-05-27 23:21:53 +00:00
|
|
|
bot: b,
|
2019-02-20 21:47:41 +00:00
|
|
|
config: b.Config(),
|
2012-08-17 22:09:29 +00:00
|
|
|
}
|
2019-02-05 19:41:38 +00:00
|
|
|
b.Register(tp, bot.Message, tp.message)
|
|
|
|
b.Register(tp, bot.Help, tp.help)
|
2019-05-27 23:21:53 +00:00
|
|
|
tp.registerWeb(b.DefaultConnector())
|
2019-02-05 19:41:38 +00:00
|
|
|
return tp
|
2012-08-17 22:09:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *TalkerPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2012-08-17 22:09:29 +00:00
|
|
|
channel := message.Channel
|
|
|
|
body := message.Body
|
2013-03-04 15:43:23 +00:00
|
|
|
lowermessage := strings.ToLower(body)
|
2012-08-17 22:09:29 +00:00
|
|
|
|
2019-02-20 21:47:41 +00:00
|
|
|
if message.Command && strings.HasPrefix(lowermessage, "cowsay") {
|
2019-02-21 14:48:48 +00:00
|
|
|
msg, err := p.cowSay(strings.TrimPrefix(message.Body, "cowsay "))
|
|
|
|
if err != nil {
|
2020-04-29 14:57:00 +00:00
|
|
|
p.bot.Send(c, bot.Message, channel, fmt.Sprintf("Error running cowsay: %s", err))
|
2019-02-21 14:48:48 +00:00
|
|
|
return true
|
|
|
|
}
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Message, channel, msg)
|
2019-02-21 14:48:48 +00:00
|
|
|
return true
|
2019-02-20 21:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if message.Command && strings.HasPrefix(lowermessage, "list cows") {
|
|
|
|
cows := p.allCows()
|
|
|
|
m := fmt.Sprintf("Cows: %s", strings.Join(cows, ", "))
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Message, channel, m)
|
2019-02-20 21:47:41 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:43:31 +00:00
|
|
|
// TODO: This ought to be space split afterwards to remove any punctuation
|
2016-03-30 23:25:02 +00:00
|
|
|
if message.Command && strings.HasPrefix(lowermessage, "say") {
|
2013-03-04 15:43:23 +00:00
|
|
|
msg := strings.TrimSpace(body[3:])
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Message, channel, msg)
|
2013-03-04 15:50:39 +00:00
|
|
|
return true
|
2013-03-04 15:43:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 23:25:02 +00:00
|
|
|
if message.Command && strings.HasPrefix(lowermessage, "goatse") {
|
2013-02-05 20:11:03 +00:00
|
|
|
nick := message.User.Name
|
2016-05-11 16:10:15 +00:00
|
|
|
if parts := strings.Fields(message.Body); len(parts) > 1 {
|
2013-02-05 20:11:03 +00:00
|
|
|
nick = parts[1]
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:10:56 +00:00
|
|
|
output := ""
|
2013-02-05 19:52:11 +00:00
|
|
|
for _, line := range goatse {
|
2013-02-05 20:11:03 +00:00
|
|
|
nick = fmt.Sprintf("%9.9s", nick)
|
|
|
|
line = strings.Replace(line, "{nick}", nick, 1)
|
2016-03-25 19:10:56 +00:00
|
|
|
output += line + "\n"
|
2013-02-05 19:52:11 +00:00
|
|
|
}
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Message, channel, output)
|
2013-02-05 19:52:11 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2012-08-17 22:09:29 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *TalkerPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
|
|
|
p.bot.Send(c, bot.Message, message.Channel, "Hi, this is talker. I like to talk about FredFelps!")
|
2019-02-05 19:41:38 +00:00
|
|
|
return true
|
2013-05-08 00:08:18 +00:00
|
|
|
}
|
2019-02-20 21:47:41 +00:00
|
|
|
|
2019-02-21 14:48:48 +00:00
|
|
|
func (p *TalkerPlugin) cowSay(text string) (string, error) {
|
|
|
|
fields := strings.Split(text, " ")
|
2019-02-20 21:47:41 +00:00
|
|
|
cow := "default"
|
2019-02-21 14:48:48 +00:00
|
|
|
if len(fields) > 1 && p.hasCow(fields[0]) {
|
|
|
|
cow = fields[0]
|
|
|
|
text = strings.Join(fields[1:], " ")
|
2019-02-20 21:47:41 +00:00
|
|
|
}
|
|
|
|
cmd := exec.Command("cowsay", "-f", cow, text)
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
2019-02-21 14:48:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2019-02-20 21:47:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 14:48:48 +00:00
|
|
|
if err = cmd.Start(); err != nil {
|
|
|
|
return "", err
|
2019-02-20 21:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
|
|
|
if err != nil {
|
2019-02-21 14:48:48 +00:00
|
|
|
return "", err
|
2019-02-20 21:47:41 +00:00
|
|
|
}
|
2019-02-21 14:48:48 +00:00
|
|
|
|
|
|
|
return fmt.Sprintf("```%s```", output), nil
|
2019-02-20 21:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TalkerPlugin) hasCow(cow string) bool {
|
|
|
|
cows := p.allCows()
|
|
|
|
for _, c := range cows {
|
|
|
|
if strings.ToLower(cow) == c {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TalkerPlugin) allCows() []string {
|
|
|
|
f, err := os.Open(p.config.Get("talker.cowpath", "/usr/local/share/cows"))
|
|
|
|
if err != nil {
|
|
|
|
return []string{"default"}
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := f.Readdir(0)
|
|
|
|
if err != nil {
|
|
|
|
return []string{"default"}
|
|
|
|
}
|
|
|
|
|
|
|
|
cows := []string{}
|
|
|
|
for _, f := range files {
|
|
|
|
if strings.HasSuffix(f.Name(), ".cow") {
|
|
|
|
cows = append(cows, strings.TrimSuffix(f.Name(), ".cow"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cows
|
|
|
|
}
|
2019-02-21 14:48:48 +00:00
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *TalkerPlugin) registerWeb(c bot.Connector) {
|
2021-07-28 15:32:59 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2019-02-21 14:48:48 +00:00
|
|
|
r.ParseForm()
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msgf("Cowsay:\n%+v", r.PostForm.Get("text"))
|
2019-02-21 15:01:10 +00:00
|
|
|
channel := r.PostForm.Get("channel_id")
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msgf("channel: %s", channel)
|
2019-02-21 14:48:48 +00:00
|
|
|
msg, err := p.cowSay(r.PostForm.Get("text"))
|
|
|
|
if err != nil {
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Message, channel, fmt.Sprintf("Error running cowsay: %s", err))
|
2019-02-21 14:48:48 +00:00
|
|
|
return
|
|
|
|
}
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Message, channel, msg)
|
2019-02-21 15:01:10 +00:00
|
|
|
w.WriteHeader(200)
|
2019-02-21 14:48:48 +00:00
|
|
|
})
|
2021-07-28 15:32:59 +00:00
|
|
|
p.bot.RegisterWeb(r, "/cowsay")
|
2019-02-21 14:48:48 +00:00
|
|
|
}
|