aboutsummaryrefslogtreecommitdiffstats
path: root/hbot/internaltriggers.go
blob: 4e7b1ce19f83a6a1e0fa6fe874631048067bf7d8 (plain) (blame)
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
package hbot

import (
	"fmt"
	"strings"
)

// A trigger to respond to the servers ping pong messages.
// If PingPong messages are not responded to, the server assumes the
// client has timed out and will close the connection.
var pingPong = Trigger{
	Condition: func(bot *Bot, m *Message) bool {
		return m.Command == "PING"
	},
	Action: func(bot *Bot, m *Message) {
		bot.Send("PONG :" + m.Trailing())
	},
}

var joinChannels = Trigger{
	Condition: func(bot *Bot, m *Message) bool {
		return m.Command == "001" || m.Command == "372"
	},
	Action: func(bot *Bot, m *Message) {
		bot.joinOnce.Do(func() {
			for _, channel := range bot.config.Channels {
				splitchan := strings.SplitN(channel, ":", 2)
				bot.config.Logger.Verbosef("Joining %q", channel)
				if len(splitchan) == 2 {
					channel = splitchan[0]
					password := splitchan[1]
					bot.Send(fmt.Sprintf("JOIN %s %s", channel, password))
				} else {
					bot.Send(fmt.Sprintf("JOIN %s", channel))
				}
			}
			// Fire Joined
			select {
			case <-bot.joined:
			default:
				close(bot.joined)
			}
		})
	},
}

// Get bot's prefix by catching its own join
var getPrefix = Trigger{
	Condition: func(bot *Bot, m *Message) bool {
		return m.Command == "JOIN" && m.Prefix.Name == bot.Nick()
	},
	Action: func(bot *Bot, m *Message) {
		bot.mu.Lock()
		bot.config.Nick = m.Prefix.Name
		bot.prefix = m.Prefix
		bot.mu.Unlock()
	},
}

// Track nick changes internally so we can adjust the bot's prefix
var setNick = Trigger{
	Condition: func(bot *Bot, m *Message) bool {
		return m.Command == "NICK" && m.Prefix.Name == bot.Nick()
	},
	Action: func(bot *Bot, m *Message) {
		to := m.Param(0)
		bot.mu.Lock()
		bot.config.Nick = to
		bot.prefix.Name = to
		bot.mu.Unlock()
		bot.config.Logger.Verbosef("Nick changed to %q", to)
	},
}

// Throw errors on invalid nick changes
var nickError = Trigger{
	Condition: func(bot *Bot, m *Message) bool {
		return m.Command == "436" || m.Command == "433" ||
			m.Command == "432" || m.Command == "431" || m.Command == "400"
	},
	Action: func(bot *Bot, m *Message) {
		bot.config.Logger.Errorf("Nick change error %q: %q", m.Command, m.Params)
	},
}

var saslFail = Trigger{
	Condition: func(bot *Bot, m *Message) bool {
		return m.Command == "904" || m.Command == "905" ||
			m.Command == "906" || m.Command == "907"
	},
	Action: func(bot *Bot, m *Message) {
		bot.config.Logger.Errorf("SASL error %q: %q", m.Command, m.Params)
	},
}

var saslSuccess = Trigger{
	Condition: func(bot *Bot, m *Message) bool {
		return m.Command == "900" || m.Command == "903"
	},
	Action: func(bot *Bot, m *Message) {
		bot.config.Logger.Verbosef("SASL success: %q", m.Trailing())
	},
}

var errorLogger = Trigger{
	Condition: func(bot *Bot, m *Message) bool {
		return m.Command == "ERROR"
	},
	Action: func(bot *Bot, m *Message) {
		bot.config.Logger.Errorf("Server error: %q", m.Trailing())
	},
}