-
Notifications
You must be signed in to change notification settings - Fork 0
/
ircclient.go
313 lines (264 loc) · 6.53 KB
/
ircclient.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package main
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"net"
"regexp"
"strings"
"sync"
"sync/atomic"
"time"
)
type ircClient struct {
ServerAddress string
WhisperServerAddress string
Nick string
ServerPassword string
PublicMessages chan ircPRIVMSG
Joins chan ircJOIN
Parts chan ircPART
// mainConn handles public messages
conn net.Conn
reader io.Reader
lineReceiver chan string
// whispers have to happen on the "group" cluster. we whisper a player's cards to them.
whisperConn net.Conn
whisperReader io.Reader
whisperLineReceiver chan string
exit chan struct{}
connected int32
}
type ircUserAction struct {
Raw string
Channel string
Nick string
User string
Host string
}
type ircPRIVMSG struct {
ircUserAction
Message string
}
type ircJOIN struct {
ircUserAction
}
type ircPART struct {
ircUserAction
}
func (i *ircClient) Connect() error {
if !atomic.CompareAndSwapInt32(&i.connected, 0, 1) {
return errors.New("already connected")
}
i.lineReceiver = make(chan string)
i.whisperLineReceiver = make(chan string)
i.exit = make(chan struct{})
go i.readLoop()
var err error
i.conn, err = net.DialTimeout("tcp", i.ServerAddress, 2*time.Second)
if err != nil {
return err
}
i.startReader(i.conn, i.lineReceiver)
i.whisperConn, err = net.DialTimeout("tcp", i.WhisperServerAddress, 2*time.Second)
if err != nil {
return err
}
i.startReader(i.whisperConn, i.whisperLineReceiver)
if err = i.Password(i.ServerPassword); err != nil {
return err
}
if err = i.ChangeNick(i.Nick); err != nil {
return err
}
if err = i.CAPREQ("twitch.tv", "membership"); err != nil {
return err
}
return nil
}
func (i *ircClient) readLoop() {
loop:
for {
select {
case line := <-i.lineReceiver:
log.Printf("(M) > %s\n", line)
i.parseLine(line, i.conn)
case line := <-i.whisperLineReceiver:
log.Printf("(W) > %s\n", line)
i.tryParsePING(line, i.whisperConn)
case <-i.exit:
break loop
}
}
}
func (i *ircClient) parseLine(line string, sourceConn net.Conn) {
i.tryParsePRIVMSG(line)
i.tryParsePING(line, sourceConn)
i.tryParseJOIN(line)
i.tryParsePART(line)
}
func (i *ircClient) tryParsePING(line string, sourceConn net.Conn) {
regex := regexp.MustCompile("^PING [:](?P<server>.+)$")
found := regex.FindAllStringSubmatch(line, -1)
if found == nil || len(found) != 1 || len(found[0]) != 2 {
return
}
server := found[0][1]
i.Pong(server, sourceConn)
}
func (i *ircClient) tryParsePRIVMSG(line string) {
if i.PublicMessages == nil {
return
}
regex := regexp.MustCompile(`^[:](?P<nick>.+)[!](?P<user>.+)[@](?P<host>.+) PRIVMSG (?P<channel>[#]\S+) [:](?P<msg>.+)`)
found := regex.FindAllStringSubmatch(line, -1)
if found == nil || len(found) != 1 || len(found[0]) != 6 {
return
}
ircMsg := ircPRIVMSG{
ircUserAction: ircUserAction{
Raw: line,
Nick: found[0][1],
User: found[0][2],
Host: found[0][3],
Channel: found[0][4],
},
Message: found[0][5],
}
i.PublicMessages <- ircMsg
}
func (i *ircClient) tryParseJOIN(line string) {
if i.Joins == nil {
return
}
regex := regexp.MustCompile(`^[:](?P<nick>.+)[!](?P<user>.+)[@](?P<host>.+) JOIN (?P<channel>[#]\S+)`)
found := regex.FindAllStringSubmatch(line, -1)
if found == nil || len(found) != 1 || len(found[0]) != 5 {
return
}
join := ircJOIN{
ircUserAction: ircUserAction{
Raw: line,
Nick: found[0][1],
User: found[0][2],
Host: found[0][3],
Channel: found[0][4],
},
}
i.Joins <- join
}
func (i *ircClient) tryParsePART(line string) {
if i.Parts == nil {
return
}
regex := regexp.MustCompile(`^[:](?P<nick>.+)[!](?P<user>.+)[@](?P<host>.+) PART (?P<channel>[#]\S+)`)
found := regex.FindAllStringSubmatch(line, -1)
if found == nil || len(found) != 1 || len(found[0]) != 5 {
return
}
part := ircPART{
ircUserAction: ircUserAction{
Raw: line,
Nick: found[0][1],
User: found[0][2],
Host: found[0][3],
Channel: found[0][4],
},
}
i.Parts <- part
}
func (i *ircClient) startReader(conn net.Conn, receiver chan string) {
go func() {
r := bufio.NewReader(conn)
for {
line, err := r.ReadString('\n')
if err != nil {
//return err // TODO: send err on error channel, disconnect, retry connect
break
}
line = line[:len(line)-1]
if strings.HasSuffix(line, "\r") {
line = line[:len(line)-1]
}
receiver <- line
}
}()
}
func (i *ircClient) CAPREQ(server, capability string) error {
cmd := fmt.Sprintf("CAP REQ :%s/%s", server, capability)
return i.sendCommand(cmd, i.allServers)
}
func (i *ircClient) ChangeNick(nick string) error {
cmd := fmt.Sprintf("NICK %s", nick)
return i.sendCommand(cmd, i.allServers)
}
func (i *ircClient) Password(password string) error {
cmd := fmt.Sprintf("PASS %s", password)
return i.sendCommand(cmd, i.allServers)
}
func (i *ircClient) Join(channel string) error {
cmd := fmt.Sprintf("JOIN %s", channel)
return i.sendCommand(cmd, i.allServers)
}
func (i *ircClient) Say(channel, message string) error {
cmd := fmt.Sprintf("PRIVMSG %s :%s", channel, message)
return i.sendCommand(cmd, i.mainServer)
}
func (i *ircClient) Whisper(channel, nick, message string) error {
time.Sleep(750 * time.Millisecond) // TODO: make this async
cmd := fmt.Sprintf("PRIVMSG %s :/w %s %s", channel, nick, message)
return i.sendCommand(cmd, i.whisperServer)
}
func (i *ircClient) Pong(server string, conn net.Conn) error {
cmd := fmt.Sprintf("PONG %s", server)
return i.sendCommand(cmd, func() []net.Conn { return []net.Conn{conn} })
}
func (i *ircClient) allServers() []net.Conn {
return []net.Conn{i.conn, i.whisperConn}
}
func (i *ircClient) mainServer() []net.Conn {
return []net.Conn{i.conn}
}
func (i *ircClient) whisperServer() []net.Conn {
return []net.Conn{i.whisperConn}
}
func (i *ircClient) sendCommand(cmd string, conns func() []net.Conn) error {
// TODO: factor the logging out
if strings.HasPrefix(cmd, "PASS") {
log.Println("(C) < PASS **************")
} else {
log.Printf("(C) < %s\n", cmd)
}
cmd += "\n"
byteCmd := []byte(cmd)
var wg sync.WaitGroup
errChan := make(chan error)
for _, conn := range conns() {
wg.Add(1)
go func(c net.Conn) {
_, err := c.Write(byteCmd)
if err != nil {
errChan <- err
}
wg.Done()
// TODO: disconnect on write error; attempt reconnect
}(conn)
}
done := make(chan struct{})
go func() {
wg.Wait()
done <- struct{}{}
}()
loop:
for {
select {
case err := <-errChan:
return err
case <-done:
break loop
}
}
return nil
}