-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparser.go
137 lines (106 loc) · 2.78 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
package irc
import "strings"
// An identity represents the prefix of a message, generally the user who sent it
type Identity struct {
// This is what the Identity was parsed from
Raw string
// The nick will either contain the nick of who sent the message or a blank string
Nick string
// The nick will either contain the user who sent the message or a blank string
User string
// The nick will either contain the host of who sent the message or a blank string
Host string
}
type Event struct {
// This is where the Command was parsed from.
Raw string
// The Identity is also the prefix of the message.
Identity *Identity
// The prefix is essentially a copy of the Raw identity.
Prefix string
// Command is which command is being called.
Command string
// Arguments are all the arguments for the command.
Args []string
}
// https://github.com/kylelemons/blightbot/blob/master/bot/parser.go#L34
func ParseIdentity(line string) *Identity {
// Start by creating an Identity with nothing but the host
id := &Identity{
Raw: line,
Host: line,
}
uh := strings.SplitN(id.Host, "@", 2)
if len(uh) != 2 {
return id
}
id.User, id.Host = uh[0], uh[1]
nu := strings.SplitN(id.User, "!", 2)
if len(nu) != 2 {
return id
}
id.Nick, id.User = nu[0], nu[1]
return id
}
// https://github.com/kylelemons/blightbot/blob/master/bot/parser.go#L55
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{
Raw: line,
}
if line[0] == ':' {
split := strings.SplitN(line, " ", 2)
if len(split) <= 1 {
return nil
}
c.Prefix = string(split[0][1:])
line = split[1]
}
// Split the string on the : separator
split := strings.SplitN(line, ":", 2)
// Grab the args
args := strings.Split(strings.TrimSpace(split[0]), " ")
// Grab the commena
c.Command = strings.ToUpper(args[0])
// Store the args
c.Args = args[1:]
if len(split) > 1 {
c.Args = append(c.Args, string(split[1]))
}
c.Identity = ParseIdentity(c.Prefix)
return c
}
// This 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]
}
// This 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
}
}
func (e *Event) Copy() *Event {
// Create a new event
newEvent := &Event{}
// Copy stuff from the old event
*newEvent = *e
// Copy the Args slice
newEvent.Args = append(make([]string, 0, len(e.Args)), e.Args...)
return newEvent
}