-
Notifications
You must be signed in to change notification settings - Fork 4
/
conn.go
224 lines (203 loc) · 4.45 KB
/
conn.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
package main
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"math/rand"
"net"
"net/textproto"
"strings"
"sync"
"time"
)
type connType uint32
const (
connWhisperConn = iota
connReadConn
connSendConn
connDelete
)
type connection struct {
sync.Mutex
conn net.Conn
active bool
anon bool
joins []string
msgCount int
lastUse time.Time
alive bool
conntype connType
bot *bot
name string
}
func newConnection(t connType) *connection {
return &connection{
joins: make([]string, 0),
conntype: t,
lastUse: time.Now(),
name: randomHash(),
}
}
func (conn *connection) login(pass string, nick string) {
conn.anon = pass == ""
if !conn.anon {
conn.send("PASS " + pass)
conn.send("NICK " + nick)
return
}
conn.send("NICK justinfan123")
}
func (conn *connection) close() {
if conn.conn != nil {
conn.conn.Close()
}
for _, channel := range conn.joins {
conn.part(channel)
}
conn.alive = false
}
func (conn *connection) part(channel string) {
channel = strings.ToLower(channel)
for i, ch := range conn.joins {
if ch == channel {
conn.joins = append(conn.joins[:i], conn.joins[i+1:]...)
}
}
}
func (conn *connection) restore() {
defer func() {
if r := recover(); r != nil {
Log.Error("cannot restore connection")
}
}()
if conn.conntype == connReadConn {
var i int
var channels []string
for index, co := range conn.bot.readconns {
if conn == co {
i = index
channels = co.joins
break
}
}
Log.Error("readconn died, lost joins:", channels)
conn.bot.Lock()
conn.bot.readconns = append(conn.bot.readconns[:i], conn.bot.readconns[i+1:]...)
conn.bot.Unlock()
for _, channel := range channels {
conns := conn.bot.channels[channel]
for i, co := range conns {
if conn == co {
conn.bot.Lock()
conn.bot.channels[channel] = append(conns[:i], conns[i+1:]...)
conn.bot.Unlock()
conn.part(channel)
}
}
conn.bot.join <- channel
}
} else if conn.conntype == connSendConn {
Log.Error("sendconn died")
var i int
for index, co := range conn.bot.sendconns {
if conn == co {
i = index
break
}
}
conn.bot.Lock()
conn.bot.sendconns = append(conn.bot.sendconns[:i], conn.bot.sendconns[i+1:]...)
conn.bot.Unlock()
} else if conn.conntype == connWhisperConn {
Log.Error("whisperconn died, reconnecting")
conn.close()
conn.bot.newConn(connWhisperConn)
}
conn.conntype = connDelete
}
func (conn *connection) connect(client *Client, pass string, nick string) {
dialer := &net.Dialer{
KeepAlive: time.Second * 10,
}
conn.bot = client.bot
c, err := tls.DialWithDialer(dialer, "tcp", *addr, &tls.Config{})
if err != nil {
Log.Error("unable to connect to irc server", err)
time.Sleep(2 * time.Second)
conn.restore()
return
}
conn.conn = c
conn.send("CAP REQ :twitch.tv/tags twitch.tv/commands")
conn.login(pass, nick)
defer func() {
if r := recover(); r != nil {
Log.Error("error connecting")
}
conn.restore()
}()
tp := textproto.NewReader(bufio.NewReader(conn.conn))
for {
line, err := tp.ReadLine()
if err != nil {
Log.Errorf("[READERROR:%s] %s", conn.name, err.Error())
conn.restore()
return
}
Log.Debugf("[TWITCH:%s] %s", conn.name, line)
if conn.conntype == connDelete {
conn.restore()
}
if strings.HasPrefix(line, "PING") {
conn.send(strings.Replace(line, "PING", "PONG", 1))
} else if strings.HasPrefix(line, "PONG") {
Log.Debug("PONG")
} else {
if isWhisper(line) && conn.conntype != connWhisperConn {
// throw away message
} else {
client.toClient <- line
}
}
conn.active = true
}
}
func isWhisper(line string) bool {
if !strings.Contains(line, ".tmi.twitch.tv WHISPER ") {
return false
}
spl := strings.SplitN(line, " :", 3)
if strings.Contains(spl[1], ".tmi.twitch.tv WHISPER ") {
return true
}
return false
}
func (conn *connection) send(msg string) error {
if conn.conn == nil {
Log.Error("conn is nil", conn, conn.conn)
return errors.New("connection is nil")
}
_, err := fmt.Fprint(conn.conn, msg+"\r\n")
if err != nil {
Log.Error("error sending message")
return err
}
Log.Debugf("[OUTGOING:%s] %s", conn.name, msg)
return nil
}
func (conn *connection) reduceMsgCount() {
conn.msgCount--
}
func (conn *connection) countMsg() {
conn.msgCount++
time.AfterFunc(30*time.Second, conn.reduceMsgCount)
}
func randomHash() string {
n := 5
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
panic(err)
}
return fmt.Sprintf("%X", b)
}