-
Notifications
You must be signed in to change notification settings - Fork 1
/
gwitch.go
144 lines (109 loc) · 3.68 KB
/
gwitch.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 gwitch
import (
"bufio"
"fmt"
"net"
"net/textproto"
"strings"
"unicode/utf8"
)
//TwitchChat containing Username, OAuth, Channel, Conn, and RawData necessary for connection
type TwitchChat struct {
Username string
OAuth string
Channel string
Conn net.Conn
RawData string
}
//Data containing Method, Username, and Message
type Data struct {
Method string
Username string
Message string
}
//New Gwitch instance
func New(user string, oauth string, channel string, conn net.Conn) (UI *TwitchChat) {
UI = &TwitchChat{user, oauth, channel, conn, ""}
return
}
//Connect to the Twitch IRC servers
func (chat *TwitchChat) Connect() (err error) {
chat.Conn, err = net.Dial("tcp", "irc.twitch.tv:6667")
//SendRawData standard user info
chat.SendRawData("PASS " + chat.OAuth)
chat.SendRawData("NICK " + chat.Username)
chat.SendRawData("JOIN " + chat.Channel)
//SendRawData to request the ability to have IRCV3 and membership updates(JOIN & PART)
chat.SendRawData("CAP REQ :twitch.tv/membership")
chat.SendRawData("CAP REQ :twitch.tv/commands")
return
}
//Close the connection
func (chat *TwitchChat) Close() (err error) {
err = chat.Conn.Close()
return
}
//SendRawData send raw data to the Twitch server
func (chat *TwitchChat) SendRawData(data string) (err error) {
_, err = fmt.Fprintf(chat.Conn, "%s\r\n", data)
return
}
//SendMessage sends message to chat using SendRawData
func (chat *TwitchChat) SendMessage(data string) (err error) {
message := fmt.Sprintf(":%s!%s@%s.tmi.twitch.tv PRIVMSG %s :%s", chat.Username, chat.Username, chat.Username, chat.Channel, data)
err = chat.SendRawData(message)
return
}
//RawRead reads raw data from conn from TwitchChat
func (chat *TwitchChat) RawRead() (data string) {
reader := bufio.NewReader(chat.Conn)
tp := textproto.NewReader(reader)
for {
line, err := tp.ReadLine()
if err != nil {
return ""
}
if strings.HasPrefix(line, "PING") {
chat.SendRawData("PONG " + line[5:])
} else {
data = line
return
}
}
}
//ReadData directly from RawData, interprets if is join, part, message, op, and de-op
func (chat *TwitchChat) ReadData() (data *Data) { //Here comes some spaghetti, let's hope someone or myself will make this non-trashy
var formattedstring string
for {
chat.RawData = chat.RawRead()
if chat.RawData == "" {
return &Data{"ERROR", "", ""}
}
formattedstring = fmt.Sprintf(".tmi.twitch.tv PRIVMSG %s :", chat.Channel)
switch {
case strings.Contains(chat.RawData, formattedstring):
tempstring1 := strings.Split(chat.RawData, formattedstring)
tempstring2 := strings.Split(tempstring1[0], "@")
if tempstring2[1] != chat.Username { //A quick fix to ignore messages created by self
data = &Data{"MESSAGE", strings.ToLower(tempstring2[1]), tempstring1[1]}
return
}
case strings.Contains(chat.RawData, ".tmi.twitch.tv JOIN "+chat.Channel):
tempstring1 := strings.Split(chat.RawData, ".tmi.twitch.tv JOIN "+chat.Channel)
tempstring2 := strings.Split(tempstring1[0], "@")
data = &Data{"JOIN", strings.ToLower(tempstring2[1]), ""}
return
case strings.Contains(chat.RawData, ".tmi.twitch.tv PART "+chat.Channel):
tempstring1 := strings.Split(chat.RawData, ".tmi.twitch.tv PART "+chat.Channel)
tempstring2 := strings.Split(tempstring1[0], "@")
data = &Data{"PART", strings.ToLower(tempstring2[1]), ""}
return
case strings.HasPrefix(chat.RawData, ":jtv MODE "+chat.Channel+" +o "):
data = &Data{"GAINOP", chat.RawData[14+utf8.RuneCountInString(chat.Channel):], ""}
return
case strings.HasPrefix(chat.RawData, ":jtv MODE "+chat.Channel+" -o "):
data = &Data{"LOSEOP", chat.RawData[14+utf8.RuneCountInString(chat.Channel):], ""}
return
}
}
}