summaryrefslogtreecommitdiffstats
path: root/wat/bot.go
blob: b36113787a413e87c91f59bb8c02c12f98f9552a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package wat

import (
	"crypto/tls"
	"fmt"
	"strconv"
	"strings"

	"github.com/go-irc/irc"
)

type WatBot struct {
	client *irc.Client
	conn   *tls.Conn
	c      *WatConfig
	game   *WatGame
	Db     *WatDb
	Nick   string
}

type BotGame struct {
	Games []string
	Name  string
}

type BotGameConfig map[string][]string

type WatConfig struct {
	BotHosts          []string
	BotGames          BotGameConfig
	AdminHosts        []string
	IgnoredHosts      []string
	AutoJoinChannels  []string
	PermittedChannels []string
}

func NewWatBot(config *irc.ClientConfig, watConfig *WatConfig, serverConn *tls.Conn) *WatBot {
	wat := WatBot{conn: serverConn, Nick: config.Nick, c: watConfig}
	wat.Db = NewWatDb()
	wat.game = NewWatGame(&wat, wat.Db)
	config.Handler = irc.HandlerFunc(wat.HandleIrcMsg)
	wat.client = irc.NewClient(wat.conn, *config)
	return &wat
}

func CleanNick(nick string) string {
	return string(nick[0]) + "\u200c" + nick[1:]
}

func PrefixChannel(channel string) string {
	// there could theoretically be other channel prefixes ..
	if channel[0] != '#' && channel[0] != '!' {
		channel = "#" + channel
	}
	return channel
}

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)
	case "001":
		for _, channel := range w.c.AutoJoinChannels {
			w.write("JOIN", PrefixChannel(channel))
		}
	}
}

func (w *WatBot) Admin(m *irc.Message) bool {
	return w.Allowed(m.Prefix.Host, w.c.AdminHosts)
}

func (w *WatBot) Bot(m *irc.Message) (bool, []string) {
	isBot := w.Allowed(m.Prefix.Host, w.c.BotHosts)
	var games []string
	if isBot {
		for b, g := range w.c.BotGames {
			if b == m.Prefix.Name {
				games = g
				break
			}
		}
	}
	return isBot, games
}

func (w *WatBot) Allowed(c string, r []string) bool {
	for _, allowed := range r {
		if c == allowed {
			return true
		}
	}
	return false
}

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
	}
	// if !strings.Contains(m.Prefix.Host, "") {
	// 	return false
	// }
	if !w.Allowed(PrefixChannel(m.Params[0]), w.c.PermittedChannels) {
		return false
	}
	return true
}

func (w *WatBot) Msg(m *irc.Message) {
	// 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
	if !w.CanRespond(m) {
		return
	}

	// make sure there's actually some text to process
	if len(m.Params[1]) == 0 {
		return
	}

	// fieldsfunc allows you to obtain rune separated fields/args
	args := strings.FieldsFunc(m.Params[1], func(c rune) bool { return c == ' ' })

	if len(args) == 0 {
		return
	}

	if w.Admin(m) {
		// allow impersonation of the robot from anywhere
		if (args[0] == "imp" || args[0] == "imps") && len(args) > 2 {
			if args[0] == "imps" {
				w.write(args[1], args[2], strings.Join(args[3:], " "))
			} else {
				w.write(args[1], args[2:]...)
			}
			return
		}
	}

	// strip offline message prefix from znc for handling offline buffer
	if args[0][0] == '[' && len(args) > 1 {
		args = args[1:]
	}

	// integration with games in other bots
	isBot, games := w.Bot(m)
	if isBot {
		// Jeopardy
		// parses a message "Top finishers: (nick1: 1300) (nick2: 1200)" from an authorized Jeopardy game bot
		if args[0] == "Top" && args[1] == "finishers:" && w.Allowed("jeopardy", games) {
			// hey, I avoided regex!
			finisherPrizes := strings.Split(strings.Replace(strings.Replace(strings.Replace(strings.Replace(strings.Join(args[2:], " "), ") (", ";", -1), ": ", ":", -1), "(", "", 1), ")", "", 1), ";")
			fmt.Printf("Processing Jeopardy: %s\n", finisherPrizes)
			for _, pair := range finisherPrizes {
				nameCoinPair := strings.Split(pair, ":")
				coins, err := strconv.ParseUint(nameCoinPair[1], 10, 64)
				if err != nil {
					fmt.Printf("Invalid coins, cannot process pair for cashout: %s.\n", nameCoinPair)
					continue
				}
				name := nameCoinPair[0]
				// Jeopardy prizes are quite a lot of $$$, make it a bit more sane
				coins = coins / 40
				// name   = we assume the Jeopardy player name to match a Watbot player name
				// host   = we could use some WHO logic to find the host, but assuming nickname lookup to be sufficient here
				// create = based on the above, maybe rather not create Watbot players based on only a nick?
				//          but it expects someone to have played with Watbot before to be eligible for Jeopardy cashout ..
				player := w.Db.User(name, "", false)
				if player.Nick == "" {
					fmt.Printf("Player %s does not exist in Watbot, skipping cashout.\n", name)
					continue
				} else {
					w.reply(m, fmt.Sprintf("smartass %s, gave u %d :)", player.Nick, coins))
					player.Coins += coins
					w.Db.Update(player)
				}
			}

			return
		}
	}

	// check if command char (or something weird) is present
	if args[0] != "wat" && args[0][0] != '#' {
		return
	}

	// clean input
	if args[0][0] == '#' {
		args[0] = args[0][1:]
	}

	user := strings.ToLower(m.Prefix.Name)
	player := w.Db.User(user, m.Prefix.Host, true)
	w.game.Msg(m, &player, args)
}

func (w *WatBot) Run() {
	defer w.conn.Close()
	err := w.client.Run()
	if err != nil {
		fmt.Println("Error returned while running client: " + err.Error())
	}
}

func (w *WatBot) say(dest, msg string) {
	if len(msg) == 0 {
		return
	}
	//fmt.Printf("MSG %s: %s\n", dest, msg)
	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,
		Params:  params,
	})
}