-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
157 lines (136 loc) · 4.06 KB
/
event.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
package dazeus
import (
"errors"
)
type eventType string
const (
// EventConnect is a connect event
EventConnect eventType = "CONNECT"
// EventDisconnect is a disconnect event
EventDisconnect eventType = "DISCONNECT"
// EventJoin is a join event
EventJoin eventType = "JOIN"
// EventPart is a part event
EventPart eventType = "PART"
// EventQuit is a quit event
EventQuit eventType = "QUIT"
// EventNick is a nick event
EventNick eventType = "NICK"
// EventMode is a mode event
EventMode eventType = "MODE"
// EventTopic is a topic event
EventTopic eventType = "TOPIC"
// EventInvite is an invite event
EventInvite eventType = "INVITE"
// EventKick is a kick event
EventKick eventType = "KICK"
// EventPrivMsg is a privmsg event
EventPrivMsg eventType = "PRIVMSG"
// EventNotice is a notice event
EventNotice eventType = "NOTICE"
// EventCtcp is a ctcp event
EventCtcp eventType = "CTCP"
// EventCtcpReply is a CTCP reply event
EventCtcpReply eventType = "CTCP_REP"
// EventAction is an action event
EventAction eventType = "ACTION"
// EventNumeric is a numeric event
EventNumeric eventType = "NUMERIC"
// EventUnknown is an unknown event
EventUnknown eventType = "UNKNOWN"
// EventWhois is a whois event
EventWhois eventType = "WHOIS"
// EventNames is a names event
EventNames eventType = "NAMES"
// EventPrivMsgMe is a privmsg from the bot itself
EventPrivMsgMe eventType = "PRIVMSG_ME"
// EventCtcpMe is CTCP message from the bot itself
EventCtcpMe eventType = "CTCP_ME"
// EventActionMe is an action event from the bot itself
EventActionMe eventType = "ACTION_ME"
// EventPong is a pong event
EventPong eventType = "PONG"
// EventCommand indicates any command event
EventCommand eventType = "COMMAND"
)
// Event represents an event message
type Event struct {
Event eventType
Params []string
DaZeus *DaZeus
Network string
Channel string
Sender string
Command string
}
// Reply allows an event handler to respond to the event with a message
func (event *Event) Reply(message string, highlight bool) error {
return event.DaZeus.Reply(event.Network, event.Channel, event.Sender, message, highlight)
}
// ReplyAction allows an event handler to respond to the event with a ctcp action
func (event *Event) ReplyAction(message string) error {
return event.DaZeus.ReplyAction(event.Network, event.Channel, event.Sender, message)
}
// ReplyNotice allows an event handler to respond to the event with a notice
func (event *Event) ReplyNotice(message string, highlight bool) error {
return event.DaZeus.ReplyNotice(event.Network, event.Channel, event.Sender, message, highlight)
}
// ReplyCtcpReply allows an event handler to respond to the event with a ctcp reply
func (event *Event) ReplyCtcpReply(message string) error {
return event.DaZeus.ReplyCtcpReply(event.Network, event.Channel, event.Sender, message)
}
func handleEvent(dazeus *DaZeus, message Message) error {
evt, err := makeEvent(dazeus, message)
if err != nil {
return err
}
for _, l := range dazeus.listeners {
if l.event == evt.Event && (l.event != EventCommand || l.command == evt.Command) {
dazeus.logger.Print("Calling matching event handler")
l.handler(evt)
}
}
return nil
}
func makeEvent(dazeus *DaZeus, message Message) (Event, error) {
var event Event
messageEventType, ok := message["event"].(string)
if !ok {
return event, errors.New("Could not find event type in message")
}
params, err := makeStringArray(message["params"])
if err != nil {
return event, err
}
var network, channel, sender string
network = params[0]
if len(params) < 2 {
sender = ""
channel = ""
params = params[1:]
} else if len(params) < 3 {
sender = params[1]
channel = ""
params = params[2:]
} else {
sender = params[1]
channel = params[2]
params = params[3:]
}
command := ""
if messageEventType == "COMMAND" {
command = params[0]
params = params[1:]
}
evtType := eventType(messageEventType)
event = Event{
Event: evtType,
Params: params,
DaZeus: dazeus,
Network: network,
Channel: channel,
Sender: sender,
Command: command,
}
return event, nil
}