2018-10-21 13:57:23 +02:00
|
|
|
package wat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-11-08 11:40:08 +01:00
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
2018-10-21 13:57:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type WatBot struct {
|
|
|
|
client *irc.Client
|
2018-11-08 11:40:08 +01:00
|
|
|
conn *tls.Conn
|
2019-01-04 10:55:40 +01:00
|
|
|
c *WatConfig
|
2018-11-08 11:40:08 +01:00
|
|
|
game *WatGame
|
2024-09-29 14:35:25 +02:00
|
|
|
integration *WatIntegration
|
2018-11-08 11:40:08 +01:00
|
|
|
Db *WatDb
|
|
|
|
Nick string
|
2018-10-21 13:57:23 +02:00
|
|
|
}
|
|
|
|
|
2019-01-04 10:55:40 +01:00
|
|
|
type WatConfig struct {
|
2024-09-22 17:42:49 +02:00
|
|
|
BotHosts []string
|
|
|
|
BotGames BotGameConfig
|
2024-09-21 16:06:04 +02:00
|
|
|
AdminHosts []string
|
2019-01-04 10:55:40 +01:00
|
|
|
IgnoredHosts []string
|
2024-09-21 18:40:29 +02:00
|
|
|
AutoJoinChannels []string
|
2024-09-21 16:06:04 +02:00
|
|
|
PermittedChannels []string
|
2018-10-23 01:15:28 +02:00
|
|
|
}
|
|
|
|
|
2019-01-04 10:55:40 +01:00
|
|
|
func NewWatBot(config *irc.ClientConfig, watConfig *WatConfig, serverConn *tls.Conn) *WatBot {
|
|
|
|
wat := WatBot{conn: serverConn, Nick: config.Nick, c: watConfig}
|
2018-10-21 13:57:23 +02:00
|
|
|
wat.Db = NewWatDb()
|
|
|
|
wat.game = NewWatGame(&wat, wat.Db)
|
2024-09-29 14:35:25 +02:00
|
|
|
wat.integration = NewWatIntegration(&wat, wat.Db, &WatIntegrationConfig{BotHosts: watConfig.BotHosts, BotGames: watConfig.BotGames})
|
2018-10-21 13:57:23 +02:00
|
|
|
config.Handler = irc.HandlerFunc(wat.HandleIrcMsg)
|
|
|
|
wat.client = irc.NewClient(wat.conn, *config)
|
|
|
|
return &wat
|
|
|
|
}
|
|
|
|
|
|
|
|
func CleanNick(nick string) string {
|
2018-11-08 11:40:08 +01:00
|
|
|
return string(nick[0]) + "\u200c" + nick[1:]
|
2018-10-21 13:57:23 +02:00
|
|
|
}
|
|
|
|
|
2024-09-21 18:40:29 +02:00
|
|
|
func PrefixChannel(channel string) string {
|
|
|
|
// there could theoretically be other channel prefixes ..
|
|
|
|
if channel[0] != '#' && channel[0] != '!' {
|
|
|
|
channel = "#" + channel
|
|
|
|
}
|
|
|
|
return channel
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:57:23 +02:00
|
|
|
func (w *WatBot) HandleIrcMsg(c *irc.Client, m *irc.Message) {
|
|
|
|
switch cmd := m.Command; cmd {
|
|
|
|
case "PING":
|
|
|
|
w.write("PONG", m.Params[0])
|
|
|
|
case "PRIVMSG":
|
|
|
|
w.Msg(m)
|
2024-09-21 18:40:29 +02:00
|
|
|
case "001":
|
|
|
|
for _, channel := range w.c.AutoJoinChannels {
|
|
|
|
w.write("JOIN", PrefixChannel(channel))
|
|
|
|
}
|
2018-10-21 13:57:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 14:14:08 +02:00
|
|
|
func (w *WatBot) Admin(m *irc.Message) bool {
|
2024-09-21 17:49:39 +02:00
|
|
|
return w.Allowed(m.Prefix.Host, w.c.AdminHosts)
|
2018-10-21 14:14:08 +02:00
|
|
|
}
|
|
|
|
|
2019-01-04 10:55:40 +01:00
|
|
|
func (w *WatBot) Allowed(c string, r []string) bool {
|
|
|
|
for _, allowed := range r {
|
2018-10-23 01:15:28 +02:00
|
|
|
if c == allowed {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:55:40 +01:00
|
|
|
func (w *WatBot) CanRespond(m *irc.Message) bool {
|
|
|
|
if w.Admin(m) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if w.Allowed(m.Prefix.Host, w.c.IgnoredHosts) {
|
|
|
|
return false
|
|
|
|
}
|
2021-03-10 15:03:48 +01:00
|
|
|
// if !strings.Contains(m.Prefix.Host, "") {
|
|
|
|
// return false
|
|
|
|
// }
|
2024-09-21 18:40:29 +02:00
|
|
|
if !w.Allowed(PrefixChannel(m.Params[0]), w.c.PermittedChannels) {
|
2019-01-04 10:55:40 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:57:23 +02:00
|
|
|
func (w *WatBot) Msg(m *irc.Message) {
|
2018-10-23 03:36:02 +02:00
|
|
|
// bail out if you're not yves, if you're not tripsit or if you're not in an allowed channel
|
|
|
|
// but if you're an admin you can do whatever
|
2019-01-04 10:55:40 +01:00
|
|
|
if !w.CanRespond(m) {
|
2018-10-21 14:14:08 +02:00
|
|
|
return
|
2018-10-21 13:57:23 +02:00
|
|
|
}
|
2018-10-21 14:14:08 +02:00
|
|
|
|
2018-10-25 02:04:35 +02:00
|
|
|
// make sure there's actually some text to process
|
2018-10-23 03:36:02 +02:00
|
|
|
if len(m.Params[1]) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-25 02:04:35 +02:00
|
|
|
// fieldsfunc allows you to obtain rune separated fields/args
|
2018-11-08 11:40:08 +01:00
|
|
|
args := strings.FieldsFunc(m.Params[1], func(c rune) bool { return c == ' ' })
|
2018-10-21 14:14:08 +02:00
|
|
|
|
2018-10-23 03:36:02 +02:00
|
|
|
if len(args) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-21 14:14:08 +02:00
|
|
|
if w.Admin(m) {
|
2018-10-25 02:04:35 +02:00
|
|
|
// allow impersonation of the robot from anywhere
|
2018-10-26 13:40:02 +02:00
|
|
|
if (args[0] == "imp" || args[0] == "imps") && len(args) > 2 {
|
|
|
|
if args[0] == "imps" {
|
|
|
|
w.write(args[1], args[2], strings.Join(args[3:], " "))
|
2018-10-25 02:04:35 +02:00
|
|
|
} else {
|
|
|
|
w.write(args[1], args[2:]...)
|
|
|
|
}
|
2018-10-23 03:36:02 +02:00
|
|
|
return
|
2018-10-21 13:57:23 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-21 14:14:08 +02:00
|
|
|
|
2018-10-23 03:36:02 +02:00
|
|
|
// strip offline message prefix from znc for handling offline buffer
|
|
|
|
if args[0][0] == '[' && len(args) > 1 {
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
|
2024-09-22 17:42:49 +02:00
|
|
|
// integration with games in other bots
|
2024-09-29 14:35:25 +02:00
|
|
|
if w.integration.HandleIntegration(m, args) {
|
|
|
|
return
|
2024-09-22 17:42:49 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 03:36:02 +02:00
|
|
|
// check if command char (or something weird) is present
|
|
|
|
if args[0] != "wat" && args[0][0] != '#' {
|
2018-10-21 14:14:08 +02:00
|
|
|
return
|
|
|
|
}
|
2018-10-23 03:36:02 +02:00
|
|
|
|
|
|
|
// clean input
|
2018-10-21 14:14:08 +02:00
|
|
|
if args[0][0] == '#' {
|
|
|
|
args[0] = args[0][1:]
|
|
|
|
}
|
2018-10-23 03:36:02 +02:00
|
|
|
|
2018-10-21 14:14:08 +02:00
|
|
|
user := strings.ToLower(m.Prefix.Name)
|
|
|
|
player := w.Db.User(user, m.Prefix.Host, true)
|
|
|
|
w.game.Msg(m, &player, args)
|
2018-10-21 13:57:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WatBot) Run() {
|
|
|
|
defer w.conn.Close()
|
|
|
|
err := w.client.Run()
|
|
|
|
if err != nil {
|
2018-10-25 02:04:35 +02:00
|
|
|
fmt.Println("Error returned while running client: " + err.Error())
|
2018-10-21 13:57:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WatBot) say(dest, msg string) {
|
|
|
|
if len(msg) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2018-12-04 00:21:11 +01:00
|
|
|
//fmt.Printf("MSG %s: %s\n", dest, msg)
|
2018-10-21 13:57:23 +02:00
|
|
|
w.write("PRIVMSG", dest, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WatBot) reply(s *irc.Message, r string) {
|
|
|
|
sender := s.Params[0]
|
|
|
|
if sender == w.Nick {
|
|
|
|
sender = s.Prefix.Name
|
|
|
|
}
|
|
|
|
w.say(sender, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WatBot) write(cmd string, params ...string) {
|
|
|
|
w.client.WriteMessage(&irc.Message{
|
|
|
|
Command: cmd,
|
2018-11-08 11:40:08 +01:00
|
|
|
Params: params,
|
2018-10-21 13:57:23 +02:00
|
|
|
})
|
|
|
|
}
|