-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_parse.go
More file actions
211 lines (163 loc) · 4.23 KB
/
message_parse.go
File metadata and controls
211 lines (163 loc) · 4.23 KB
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
package irc
import (
"strings"
)
var (
// ErrEmptyMessage is returned when the parser encounters an empty message.
ErrEmptyMessage = &ParseError{"empty message"}
// ErrInvalidMessage is returned when the parser encounters an invalid message.
// This error is likely to be replaced with a more helpful error (encoding something
// like where the error occurred).
ErrInvalidMessage = &ParseError{"invalid message"}
)
// Parse parses a string into a message. All fields of the message struct
// will be set (default as needed), so there is no need to zero before parsing.
//
// Parse does not check the input for newlines, which are normally invalid.
func (m *Message) Parse(raw string) error {
return parseMessage(raw, m)
}
// ParseMessage parses a string and returns a new Message. A string is
// used as the input, as it was found that they're more performant than
// using a byte slice, even with an extra initial copy.
//
// ParseMessage does not check the input for newlines, which are normally invalid.
func ParseMessage(raw string) (*Message, error) {
m := &Message{}
if err := parseMessage(raw, m); err != nil {
return nil, err
}
return m, nil
}
func parseMessage(raw string, m *Message) error {
if raw == "" {
return ErrEmptyMessage
}
// Easier and no slower to just zero out the message early.
// It may be better to not do this selectively and have an option to
// do something like keeping the tags map around (to be reused).
*m = Message{
Raw: raw,
}
var err error
raw, err = m.parseTags(raw)
if err != nil {
return err
}
raw, err = m.parsePrefix(raw)
if err != nil {
return err
}
raw, err = m.parseCommand(raw)
if err != nil {
return err
}
if raw == "" {
return nil
}
m.parseParamsAndTrailing(raw)
return nil
}
func (m *Message) parseTags(raw string) (string, error) {
// raw is guaranteed to not be empty when this function is called.
if raw[0] != '@' {
return raw, nil
}
raw = raw[1:]
i := strings.IndexByte(raw, ' ')
if i == -1 {
return "", ErrInvalidMessage
}
tags := raw[:i]
raw = raw[i+1:]
// <SPACE> can be many spaces, but the above stopped at the first space,
// so trim off any other spaces.
raw = strings.TrimLeftFunc(raw, isSpace)
if tags == "" {
m.ForcedTags = true
return raw, nil
}
numTags := strings.Count(tags, ";") + 1
m.Tags = make(map[string]string, numTags)
for tags != "" {
var pair string
end := strings.IndexByte(tags, ';')
if end == -1 {
pair = tags
tags = ""
} else {
pair = tags[:end]
tags = tags[end+1:]
}
i := strings.IndexByte(pair, '=')
if i == -1 {
m.Tags[pair] = ""
continue
}
k := pair[:i]
v := pair[i+1:]
m.Tags[k] = tagUnescape(v)
}
return raw, nil
}
func (m *Message) parsePrefix(raw string) (string, error) {
if raw == "" {
return "", ErrInvalidMessage
}
if raw[0] != ':' {
return raw, nil
}
raw = raw[1:]
i := strings.IndexByte(raw, ' ')
if i == -1 {
return "", ErrInvalidMessage
}
prefix := raw[:i]
raw = raw[i+1:]
user := strings.IndexByte(prefix, '!')
host := strings.IndexByte(prefix, '@')
if user > 0 && host > user {
m.Prefix.Name = prefix[:user]
m.Prefix.User = prefix[user+1 : host]
m.Prefix.Host = prefix[host+1:]
} else if user > 0 {
m.Prefix.Name = prefix[:user]
m.Prefix.User = prefix[user+1:]
} else if host > 0 {
m.Prefix.Name = prefix[:host]
m.Prefix.Host = prefix[host+1:]
} else {
m.Prefix.Name = prefix
}
// <SPACE> can be many spaces, but the above stopped at the first space,
// so trim off any other spaces.
raw = strings.TrimLeftFunc(raw, isSpace)
return raw, nil
}
func (m *Message) parseCommand(raw string) (string, error) {
if raw == "" {
return "", ErrInvalidMessage
}
i := strings.IndexByte(raw, ' ')
if i == -1 {
m.Command = raw
return "", nil
}
m.Command = raw[:i]
raw = raw[i+1:]
// No space trimming is needed here, as parseParamsAndTrailing uses
// stringFields to pick apart the params.
return raw, nil
}
func (m *Message) parseParamsAndTrailing(raw string) {
if i := strings.IndexByte(raw, ':'); i != -1 {
m.Trailing = raw[i+1:]
raw = raw[:i]
// Set this up in case the message is reencoded.
m.ForcedTrailing = m.Trailing == ""
}
m.Params = stringFields(raw, ' ')
}
func isSpace(r rune) bool {
return r == ' '
}