-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
executable file
·67 lines (55 loc) · 1.37 KB
/
message.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
package ircbot
import "strings"
//IrcMsg represent a message receive or send to the irc server
type IrcMsg struct {
Raw string
Prefix string
Command string
CmdParams []string
Trailing []string
}
func NewIrcMsg() *IrcMsg {
return &IrcMsg{}
}
func (m *IrcMsg) parseline(line string) {
prefixEnd, trailingStart := -1, len(line)
m.Prefix, m.Command = "", ""
if strings.HasPrefix(line, ":") {
prefixEnd = strings.Index(line, " ")
m.Prefix = line[1:prefixEnd]
}
trailingStart = strings.Index(line, " :")
if trailingStart >= 0 {
m.Trailing = strings.Fields(line[trailingStart+2:])
} else {
trailingStart = len(line) - 1
}
cmdAndParams := strings.Fields(line[(prefixEnd + 1) : trailingStart+1])
if len(cmdAndParams) > 0 {
m.Command = cmdAndParams[0]
}
if len(cmdAndParams) > 1 {
m.CmdParams = cmdAndParams[1:]
}
}
//Channel return the channel that send the IrcMsg
func (m *IrcMsg) Channel() string {
if len(m.CmdParams) <= 0 {
return ""
}
return m.CmdParams[0]
}
//Channel return the nickname of the user who send IrcMsg
func (m *IrcMsg) Nick() string {
if strings.Contains(m.Prefix, "!") {
tmp := strings.SplitAfterN(m.Prefix, "!", 2)[0]
return tmp[:len(tmp)-1]
}
return ""
}
//ParseLine parse a line receive from server and return a new IrcMsg object
func ParseLine(line string) *IrcMsg {
msg := NewIrcMsg()
msg.parseline(line)
return msg
}