-
Notifications
You must be signed in to change notification settings - Fork 0
/
names.go
77 lines (65 loc) · 2.25 KB
/
names.go
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
package main
import (
"strings"
"github.com/osm/irc"
)
// handleNamesReply handles the 353 command (RPL_NAMREPLY) that is issued when
// the bot joins the channel for the first time. It returns a list of all the
// names that currently is in the channel. Big channels might return multiple
// 353 commands, so we'll have to lock the map before we use it to prevent
// race conditions.
func (b *bot) handleNamesReply(m *irc.Message) {
// The raw IRC message will look something like this.
// :irc.example.net 353 foo = #foo :foo +bar @baz
// So we'll remove the foo = #foo part and keep the names.
msg := strings.Join(m.ParamsArray[3:], " ")
// There can be more than one response in big channels, so we'll have
// to acquire a lock before we add anything to the names map.
b.IRC.namesMu.Lock()
defer b.IRC.namesMu.Unlock()
// Names are separated with a space character, so we split on it and
// check whether the name exists in the map before or not.
for _, n := range strings.Split(msg, " ") {
// Strip any protocol and status characters from the given name.
if n[0] == ':' || n[0] == '+' || n[0] == '%' || n[0] == '@' {
n = n[1:]
}
if _, ok := b.IRC.names[n]; !ok {
b.IRC.names[n] = true
}
}
}
// handleNamesAdd adds a non existing name to the names map.
func (b *bot) handleNamesAdd(m *irc.Message) {
b.IRC.namesMu.Lock()
defer b.IRC.namesMu.Unlock()
if _, ok := b.IRC.names[m.Name]; !ok {
b.IRC.names[m.Name] = true
}
}
// handleNamesRemove updates the names map of the bot and removes the name of
// the user that is either PARTing or QUITing.
func (b *bot) handleNamesRemove(m *irc.Message) {
b.IRC.namesMu.Lock()
defer b.IRC.namesMu.Unlock()
if _, ok := b.IRC.names[m.Name]; ok {
delete(b.IRC.names, m.Name)
}
}
// handleNamesChange replaces the name in the names map when someone uses the
// NICK command.
func (b *bot) handleNamesChange(m *irc.Message) {
// Extract the current and new name from the message.
currentName := m.Name
newName := m.Params[1:]
b.IRC.namesMu.Lock()
defer b.IRC.namesMu.Unlock()
// Remove the old name from the map.
if _, ok := b.IRC.names[currentName]; ok {
delete(b.IRC.names, currentName)
}
// ... and add the new name.
if _, ok := b.IRC.names[newName]; !ok {
b.IRC.names[newName] = true
}
}