-
Notifications
You must be signed in to change notification settings - Fork 59
/
irc.go
150 lines (117 loc) · 2.81 KB
/
irc.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
package twitch
import (
"fmt"
"regexp"
"strings"
)
// Maximum supported length of an irc message
const maxMessageLength = 510
type ircMessage struct {
Raw string
Tags map[string]string
Source ircMessageSource
Command string
Params []string
}
type ircMessageSource struct {
Nickname string
Username string
Host string
}
func parseIRCMessage(line string) (*ircMessage, error) {
message := ircMessage{
Raw: line,
Tags: make(map[string]string),
Params: []string{},
}
split := strings.Split(line, " ")
index := 0
if strings.HasPrefix(split[index], "@") {
message.Tags = parseIRCTags(split[index])
index++
}
if index >= len(split) {
return &message, fmt.Errorf("parseIRCMessage: partial message")
}
if strings.HasPrefix(split[index], ":") {
message.Source = *parseIRCMessageSource(split[index])
index++
}
if index >= len(split) {
return &message, fmt.Errorf("parseIRCMessage: no command")
}
message.Command = split[index]
index++
if index >= len(split) {
return &message, nil
}
var params []string
for i, v := range split[index:] {
if strings.HasPrefix(v, ":") {
v = strings.Join(split[index+i:], " ")
v = strings.TrimPrefix(v, ":")
params = append(params, v)
break
}
params = append(params, v)
}
message.Params = params
return &message, nil
}
func parseIRCTags(rawTags string) map[string]string {
tags := make(map[string]string)
rawTags = strings.TrimPrefix(rawTags, "@")
for _, tag := range strings.Split(rawTags, ";") {
pair := strings.SplitN(tag, "=", 2)
key := pair[0]
var value string
if len(pair) == 2 {
value = parseIRCTagValue(pair[1])
}
tags[key] = value
}
return tags
}
var tagEscapeCharacters = []struct {
from string
to string
}{
{`\s`, ` `},
{`\n`, ``},
{`\r`, ``},
{`\:`, `;`},
{`\\`, `\`},
}
func parseIRCTagValue(rawValue string) string {
for _, escape := range tagEscapeCharacters {
rawValue = strings.ReplaceAll(rawValue, escape.from, escape.to)
}
rawValue = strings.TrimSuffix(rawValue, "\\")
// Some Twitch values can end with a trailing \s
// Example: "system-msg=An\sanonymous\suser\sgifted\sa\sTier\s1\ssub\sto\sTenureCalculator!\s"
rawValue = strings.TrimSpace(rawValue)
return rawValue
}
func parseIRCMessageSource(rawSource string) *ircMessageSource {
var source ircMessageSource
rawSource = strings.TrimPrefix(rawSource, ":")
regex := regexp.MustCompile(`!|@`)
split := regex.Split(rawSource, -1)
if len(split) == 0 {
return &source
}
switch len(split) {
case 1:
source.Host = split[0]
case 2:
// Getting 2 items extremely rare, but does happen sometimes.
// https://github.com/gempir/go-twitch-irc/issues/109
source.Nickname = split[0]
source.Host = split[1]
default:
source.Nickname = split[0]
source.Username = split[1]
source.Host = split[2]
}
return &source
}