-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.go
144 lines (124 loc) · 3.31 KB
/
chat.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
package twitchgo
import (
"bufio"
"errors"
"fmt"
"log"
"net"
"strconv"
"strings"
)
type Chat struct {
Conn net.Conn
Channel string
Connected bool
Joined bool
Twitch *Twitch
}
type Message struct {
Sender string
Text string
Subscriber bool
SubLength int
Mod bool
UserID string
Channel string
}
var chatCallback func(*Message)
func (t *Twitch) ChatConnect(channel string, handler func(*Message)) error {
CHAT_HOST := "irc.chat.twitch.tv:6667"
// Build the chat struct
chat := new(Chat)
chat.Channel = channel
chat.Twitch = t
// Connect to the server
conn, err := net.Dial("tcp", CHAT_HOST)
if err != nil {
log.Fatalf("Could not connect to chat server: %s", err)
}
chat.Conn = conn
// Authenticate
user, err := t.GetLoggedInUser()
if err != nil {
return errors.New(fmt.Sprintf("Error connecting to chat: %v", err))
}
chat.sendMsg("CAP REQ :twitch.tv/membership twitch.tv/tags twitch.tv/commands")
chat.sendMsg("PASS oauth:" + t.config.Token.AccessToken)
chat.sendMsg("NICK " + user.Login)
chatCallback = handler
go chat.readThread(conn)
return nil
}
func (c *Chat) sendMsg(message string) {
_, err := c.Conn.Write([]byte(message + "\r\n"))
if err != nil {
log.Fatalf("Error sending message: %s", err)
}
}
func (c *Chat) readThread(conn net.Conn) {
user, _ := c.Twitch.GetLoggedInUser()
reader := bufio.NewReader(conn)
for {
lineB, _, err := reader.ReadLine()
if err != nil {
log.Fatalf("Error reading from server: %s", err)
}
line := string(lineB)
if strings.Contains(line, ":tmi.twitch.tv 001 "+strings.ToLower(user.Login)+" :Welcome, GLHF!") {
// We've authenticated to the server
c.Connected = true
c.joinChannel()
} else if strings.Contains(line, " 366 "+strings.ToLower(user.Login)+" #"+strings.ToLower(c.Channel)) {
// We've joined the desired channel
c.Joined = true
} else if strings.Contains(line, "PING") {
// Respond to Keepalive message
c.sendPong(line)
} else if strings.Contains(line, ".tmi.twitch.tv PRIVMSG #"+strings.ToLower(c.Channel)+" :") {
// Read a message in the streams chat
m := c.parseMessage(line)
chatCallback(m)
}
}
}
func (c *Chat) joinChannel() {
c.sendMsg("JOIN #" + c.Channel)
}
func (c *Chat) sendPong(line string) {
rsp := strings.Replace(line, "PING", "PONG", 1)
c.sendMsg(rsp)
}
func (c *Chat) parseMessage(line string) *Message {
m := new(Message)
m.Channel = c.Channel
// Parse the advanced tags to pull the user, sub, and mod info
tags := strings.Split(strings.Split(line, "!")[0][1:], ";")
for _, tag := range tags {
tagSplit := strings.Split(tag, "=")
key := tagSplit[0]
val := tagSplit[1]
if key == "display-name" {
m.Sender = val
} else if key == "subscriber" {
m.Subscriber = val == "1"
} else if key == "badge-info" {
// Sub length only exists if they are a sub, ensure it exists
if len(val) > 0 {
if strings.Split(val, "/")[0] == "subscriber" {
subLength, err := strconv.Atoi(strings.Split(val, "/")[1])
if err != nil {
log.Fatalf("Could not determine sub length: %s\n", err)
}
m.SubLength = subLength
}
}
} else if key == "mod" {
m.Mod = val == "1"
} else if key == "user-id" {
m.UserID = val
}
}
// Get the message
m.Text = strings.Split(line, "tmi.twitch.tv PRIVMSG #"+c.Channel+" :")[1]
return m
}