-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircmessage.go
115 lines (103 loc) · 2.3 KB
/
ircmessage.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
package main
import (
"strings"
)
type IRCMessageType int
const (
_ = iota
IRCMessagePRIVMSG
IRCMessageJOIN
IRCMessagePART
IRCMessageNOTICE
IRCMessagePING
IRCMessageQUIT
IRCMessageKICK
IRCMessageRAW
IRCMessageERROR
IRCMessageMODE
)
type IRCMessage struct {
Type IRCMessageType
From string // Who/where message was sent from
To string // Who/where message was sent to
Dest string // Calculated reply destination
Data string
KNick string // Nick being kicked
Raw string // Raw code
Full string // Full, un-modified message
}
func IRCMessageParse(in string) (m IRCMessage) {
// [:prefix] <command> [params] [:trailing]
hasPrefix := false
secIndex := 0
in = strings.TrimSuffix(in, "\r\n")
m.Full = in
if strings.HasPrefix(in, ":") {
hasPrefix = true
secIndex++
}
sections := strings.Split(in, ":")
prefixCmdParams := strings.Fields(sections[secIndex]);
secIndex++
pcmIndex := 0
if hasPrefix {
m.From = prefixCmdParams[pcmIndex];
pcmIndex++
}
m.Type = IRCMessageTypeParse(prefixCmdParams[pcmIndex])
pcmIndex++
switch m.Type {
case IRCMessagePRIVMSG:
m.To = prefixCmdParams[pcmIndex]
m.Data = sections[secIndex]
case IRCMessagePART:
m.To = prefixCmdParams[pcmIndex]
m.Data = sections[secIndex]
case IRCMessageNOTICE:
m.To = prefixCmdParams[pcmIndex]
m.Data = sections[secIndex]
case IRCMessageJOIN:
m.To = prefixCmdParams[pcmIndex]
case IRCMessagePING:
m.Data = sections[secIndex]
case IRCMessageQUIT:
m.Data = sections[secIndex]
case IRCMessageKICK:
m.To = prefixCmdParams[pcmIndex]
m.KNick = prefixCmdParams[pcmIndex + 1]
m.Data = sections[secIndex]
case IRCMessageRAW:
m.Raw = prefixCmdParams[pcmIndex - 1]
case IRCMessageERROR:
m.Data = sections[secIndex]
case IRCMessageMODE:
m.To = prefixCmdParams[pcmIndex]
m.Data = sections[secIndex]
}
return
}
func IRCMessageTypeParse(in string) (t IRCMessageType) {
switch in {
case "PRIVMSG":
return IRCMessagePRIVMSG
case "JOIN":
return IRCMessageJOIN
case "PART":
return IRCMessagePART
case "NOTICE":
return IRCMessageNOTICE
case "PING":
return IRCMessagePING
case "QUIT":
return IRCMessageQUIT
case "KICK":
return IRCMessageKICK
case "ERROR":
return IRCMessageERROR
case "MODE":
return IRCMessageMODE
default:
return IRCMessageRAW
}
}
// vim: set noexpandtab ts=8 sw=8 sts=8: