-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
107 lines (87 loc) · 2.63 KB
/
events.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
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
package irc
import (
"fmt"
"time"
)
// Handle registers a new event handler
func (c *Client) Handle(event string, fn func(m *Message)) {
c.hub.Handle(event, fn)
}
// coreEvents setups event handlers for the most common tasks that everyone most likely wants
func (c *Client) coreEvents() {
// Handle PING PONG
// We also try to reclaim our nick on each PING from the server
c.Handle("PING", func(m *Message) {
// Send PONG
c.Sendf("PONG %s", m.Params)
// Try to reclaim our nick on each PING
c.ReclaimNick()
})
// If the nick that PARTs is our configured nick we'll reclaim it.
c.Handle("QUIT", func(m *Message) {
if m.Name == c.nick {
// Acquire lock
c.infoMu.Lock()
// Send NICK command
c.Nick(c.nick)
// Set current nick to what we just changed it to be
c.currentNick = c.nick
// Release the lock
c.infoMu.Unlock()
}
})
// 401 is returned by the server after a WHOIS request if the nick is not in use
// Let's verify if the WHOIS request was made from a nick reclaim attempt
c.Handle("401", func(m *Message) {
// Our current nick is not the nick that we want
// Let's acquire a lock and change it
if m.Params == fmt.Sprintf("%s %s :No such nick or channel name", c.currentNick, c.nick) ||
m.Params == fmt.Sprintf("%s %s :No such nick", c.currentNick, c.nick) {
// Acquire lock
c.infoMu.Lock()
// Send NICK command
c.Nick(c.nick)
// Set current nick to what we just changed it to be
c.currentNick = c.nick
// Release the lock
c.infoMu.Unlock()
}
})
// Things to do after a successful connect
c.Handle("001", func(m *Message) {
// The post connect messages and modes should occur before
// joining any channels.
for _, pcm := range c.postConnectMessages {
c.Privmsg(pcm.target, pcm.message)
}
for _, m := range c.postConnectModes {
c.Sendf("MODE %s %s", c.currentNick, m)
}
// To make sure all the messages and modes has been
// successfully applied before we join a channel we'll sleep
// for a short while.
time.Sleep(3 * time.Second)
for _, ch := range c.channels {
c.Sendf("JOIN %s", ch)
}
})
// Handle CTCP version requests
c.Handle("PRIVMSG", func(m *Message) {
// Make sure that the CTCP VERSION request is made to our current nick
if m.Params == fmt.Sprintf("%s :\x01VERSION\x01", c.currentNick) {
// Reply
c.Noticef(m.Name, "\x01VERSION %s\x01", c.version)
}
})
// Handle nick in use
c.Handle("433", func(m *Message) {
// Acquire lock
c.infoMu.Lock()
// Update the nick
c.currentNick = fmt.Sprintf("%s_", c.currentNick)
// Send nick to server
c.Nick(c.currentNick)
// Release the lock
c.infoMu.Unlock()
})
}