-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparser.go
209 lines (166 loc) · 4.49 KB
/
parser.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
package irc
import (
"bytes"
"strings"
)
// Identity represents the prefix of a message, generally the user who sent it
type Identity struct {
// Name will contain the nick of who sent the message, the
// server who sent the message, or a blank string
Name string
// User will either contain the user who sent the message or a blank string
User string
// Host will either contain the host of who sent the message or a blank string
Host string
}
// Event represents a line parsed from the server
type Event struct {
// Each event can have an identity
*Identity
// Command is which command is being called.
Command string
// Arguments are all the arguments for the command.
Args []string
}
// ParseIdentity takes an identity string and parses it into an
// identity struct. It will always return an Identity struct and never
// nil.
func ParseIdentity(line string) *Identity {
// Start by creating an Identity with nothing but the host
id := &Identity{
Name: line,
}
uh := strings.SplitN(id.Name, "@", 2)
if len(uh) == 2 {
id.Name, id.Host = uh[0], uh[1]
}
nu := strings.SplitN(id.Name, "!", 2)
if len(nu) == 2 {
id.Name, id.User = nu[0], nu[1]
}
return id
}
// Copy will create a new copy of an Identity
func (i *Identity) Copy() *Identity {
newIdent := &Identity{}
*newIdent = *i
return newIdent
}
// String ensures this is stringable
func (i *Identity) String() string {
buf := &bytes.Buffer{}
buf.WriteString(i.Name)
if i.User != "" {
buf.WriteString("!")
buf.WriteString(i.User)
}
if i.Host != "" {
buf.WriteString("@")
buf.WriteString(i.Host)
}
return buf.String()
}
// ParseEvent takes an event string (usually a whole line) and parses
// it into an Event struct. This will return nil in the case of
// invalid events.
func ParseEvent(line string) *Event {
// Trim the line and make sure we have data
line = strings.TrimSpace(line)
if len(line) == 0 {
return nil
}
c := &Event{Identity: &Identity{}}
if line[0] == ':' {
split := strings.SplitN(line, " ", 2)
if len(split) < 2 {
return nil
}
// Parse the identity, if there was one
c.Identity = ParseIdentity(string(split[0][1:]))
line = split[1]
}
// Split out the trailing then the rest of the args. Because
// we expect there to be at least one result as an arg (the
// command) we don't need to special case the trailing arg and
// can just attempt a split on " :"
split := strings.SplitN(line, " :", 2)
c.Args = strings.FieldsFunc(split[0], func(r rune) bool {
return r == ' '
})
// If there are no args, we need to bail because we need at
// least the command.
if len(c.Args) == 0 {
return nil
}
// If we had a trailing arg, append it to the other args
if len(split) == 2 {
c.Args = append(c.Args, split[1])
}
// Because of how it's parsed, the Command will show up as the
// first arg.
c.Command = c.Args[0]
c.Args = c.Args[1:]
return c
}
// Trailing returns the last argument in the Event or an empty string
// if there are no args
func (e *Event) Trailing() string {
if len(e.Args) < 1 {
return ""
}
return e.Args[len(e.Args)-1]
}
// FromChannel is mostly for PRIVMSG events (and similar derived events)
// It will check if the event came from a channel or a person.
func (e *Event) FromChannel() bool {
if len(e.Args) < 1 || len(e.Args[0]) < 1 {
return false
}
switch e.Args[0][0] {
case '#', '&':
return true
default:
return false
}
}
// Copy will create a new copy of an event
func (e *Event) Copy() *Event {
// Create a new event
newEvent := &Event{}
// Copy stuff from the old event
*newEvent = *e
// Copy the Identity
newEvent.Identity = e.Identity.Copy()
// Copy the Args slice
newEvent.Args = append(make([]string, 0, len(e.Args)), e.Args...)
return newEvent
}
// String ensures this is stringable
func (e *Event) String() string {
buf := &bytes.Buffer{}
// Add the prefix if we have one
if e.Identity.Name != "" {
buf.WriteByte(':')
buf.WriteString(e.Identity.String())
buf.WriteByte(' ')
}
// Add the command since we know we'll always have one
buf.WriteString(e.Command)
if len(e.Args) > 0 {
args := e.Args[:len(e.Args)-1]
trailing := e.Args[len(e.Args)-1]
if len(args) > 0 {
buf.WriteByte(' ')
buf.WriteString(strings.Join(args, " "))
}
// If trailing contains a space or starts with a : we
// need to actually specify that it's trailing.
if strings.ContainsRune(trailing, ' ') || trailing[0] == ':' {
buf.WriteString(" :")
} else {
buf.WriteString(" ")
}
buf.WriteString(trailing)
}
return buf.String()
}