-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam.go
293 lines (250 loc) · 8.25 KB
/
team.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package singularity
import (
"errors"
"net/http"
"golang.org/x/net/websocket"
)
//SlackInstance ...
type SlackInstance struct {
rtmResp RTMResp
//Important Data Stuff
singularity *Singularity
connection *websocket.Conn
//Handlers
Commands *CommandHandler //TODO unexport
handlers *EventAPIHandler
customEventHandler func(input chan ChanMessage, output chan ChanMessage)
customCommandHandler func()
customWebsocketHandler func()
log func(level int, message string, i ...interface{})
//Friendly name
Name string
//Token
token string
input chan ChanMessage //input represents what comes from slack
output chan ChanMessage //output goes to slack
quit chan int
//Configuration
Configuration Configuration //TODO unexport, expose funcs.
transport *http.Transport
}
//Quit ...
func (instance *SlackInstance) Quit() {
instance.singularity.RemoveTeam(instance.Name) //Remove Yo Self.
instance.quit <- 0
//TODO Add something to wait for the go rutines to end?
}
//Please dont call this outside of Singularity::Shutdown
func (instance *SlackInstance) quitShutdown() {
instance.quit <- 0
}
//NewTeam creates a new team, that isn't started. To start a team, you'll need to call the Start function on it.
func (singularity *Singularity) NewTeam(Token string) *SlackInstance {
singularity.Lock()
defer singularity.Unlock()
instance := &SlackInstance{token: Token, singularity: singularity}
//Configuration
instance.Configuration = &defaultConfig{config: make(map[string]interface{})} //TODO move outside. configs should be configured before a team is started.
//defaulthandlers //TODO Move so that
instance.Commands = NewCommandHandler()
instance.Commands.setPrefix(".")
addDefaultCommands(instance)
instance.handlers = NewHandler1()
addDefaultHandlers(instance)
singularity.Teams = append(singularity.Teams, *instance)
return instance
}
//Start starts the slack instance.
func (instance *SlackInstance) Start() error {
helper := HTTPHelper{Client: &http.Client{}, Transport: "https://"} //TODO Don't hard code this.
_, err := helper.post("rtm.start", &instance.rtmResp, "token", instance.token)
if err != nil {
return err
}
//Connect to websocket.
//TODO Support custom proxies?
instance.connection, err = websocket.Dial(instance.rtmResp.URL, "", "http://api.slack.com") //TODO Don't hard code here.
if err != nil {
return err
}
var hello struct {
Hello string `json:"type"`
}
err = websocket.JSON.Receive(instance.connection, &hello)
if err != nil {
return err
}
if hello.Hello != "hello" {
return errors.New("Slack did not respond with the correct message")
}
instance.Name = instance.rtmResp.Team.Name
//Set the logger to the default one if it isn't already set.
if instance.log == nil {
instance.log = instance.singularity.log
}
//Channels for this Instance.
instance.input = make(chan ChanMessage, 5) //TODO Configure Amount
instance.output = make(chan ChanMessage, 5) //TODO Configure Amount
instance.quit = make(chan int)
//Start Go-Routines for handling the things.
if instance.customEventHandler != nil {
go instance.customEventHandler(instance.input, instance.output)
} else {
go instance.handleChans()
}
if instance.customWebsocketHandler != nil {
go instance.customWebsocketHandler()
} else {
go instance.handleWebsocket()
}
return nil
}
//TODO Panic Recovery
func (instance *SlackInstance) handleChans() {
for {
outOfSelect:
select {
case val := <-instance.input: //<-instance.input reads from (what is assumed to be) slack.
event, err := val.GetBytes()
if err != nil {
instance.log(LogError, "Failed to get bytes from instance.input: %v", err) //TODO Better error message
break outOfSelect
}
slackType := preParseString("type", event)
if slackType != "" {
instance.log(LogDebug, "Found Type: %v", slackType)
instance.handlers.execute(slackType, event, instance)
} // TODO handle empty types.
case val := <-instance.output: //<-instance.output sends the thing to slack.
thingToSend, err := val.GetInterface()
if err != nil {
instance.log(LogError, "Failed to get the interface from instance.output: %v", err) //TODO Better error message
break outOfSelect
}
err = websocket.JSON.Send(instance.connection, thingToSend) //SendBodies :D
if err != nil {
instance.log(LogError, "Failed to send bodies through the websocket: %v", err) //TODO Better error message
}
case <-instance.quit:
return
}
}
}
// handleWebsocket()
// This is used for grabbing incomming messages and putting them on the queue
// without having to wait for whatever to finish.
func (instance *SlackInstance) handleWebsocket() {
var i interface{}
for {
func() {
//defer Recover() //TODO implement panic recovery.
err := websocket.JSON.Receive(instance.connection, &i)
if err != nil {
instance.log(LogError, "Failed to read from the websocket connection: %v", err) //TODO Better error message
return
}
instance.input <- ChanMessage{i} //Buffered.
}()
}
}
//GetChannelByID returns a channel matching the supplied id, or nil.
func (instance *SlackInstance) GetChannelByID(id string) *Channel {
instance.rtmResp.Lock()
defer instance.rtmResp.Unlock()
for _, channel := range instance.rtmResp.Channels {
if channel.ID == id {
return &channel
}
}
return nil
}
//GetChannelByName returns a channel matching the supplied name, or nil.
func (instance *SlackInstance) GetChannelByName(name string) *Channel {
instance.rtmResp.Lock()
defer instance.rtmResp.Unlock()
for _, channel := range instance.rtmResp.Channels {
if channel.Name == name {
return &channel
}
}
return nil
}
//GetSelf returns self
func (instance *SlackInstance) GetSelf() *Self {
instance.rtmResp.Lock()
defer instance.rtmResp.Unlock()
return &instance.rtmResp.Self
}
//GetTeam returns the team object stored in the RTMResp.
func (instance *SlackInstance) GetTeam() *Team {
instance.rtmResp.Lock()
defer instance.rtmResp.Unlock()
return &instance.rtmResp.Team
}
//GetUserByID returns a user matching the supplied id, or nil.
func (instance *SlackInstance) GetUserByID(id string) *User {
instance.rtmResp.Lock()
defer instance.rtmResp.Unlock()
for _, user := range instance.rtmResp.Users {
if user.ID == id {
return &user
}
}
return nil
}
//GetUserByName returns a user matching the supplied name, or nil.
func (instance *SlackInstance) GetUserByName(name string) *User {
instance.rtmResp.Lock()
defer instance.rtmResp.Unlock()
for _, user := range instance.rtmResp.Users {
if user.Name == name {
return &user
}
}
return nil
}
// Log uses the stored log function.
func (instance *SlackInstance) Log(level int, message string, attachments ...interface{}) {
instance.log(level, message, attachments...)
}
//RegisterCommand ...
func (instance *SlackInstance) RegisterCommand(command string, commandHandler func(Command)) error {
return instance.Commands.registerCommand(command, commandHandler)
}
//RegisterHandler ...
func (instance *SlackInstance) RegisterHandler(key string, handler interface{}) error {
instance.handlers.Lock()
defer instance.handlers.Unlock()
return instance.handlers.registerHandler(key, handler)
}
//RegisterHandlers will allow you to handle the events.
func (instance *SlackInstance) RegisterHandlers(handlers map[string][]interface{}) error {
instance.handlers.Lock()
defer instance.handlers.Unlock()
for key, handler := range handlers {
for _, function := range handler {
if err := instance.handlers.registerHandler(key, function); err != nil {
return err //No errors allowed.
}
}
}
return nil
}
//SendMessage sends a slack message
func (instance *SlackInstance) SendMessage(m Message) {
m.Type = "message"
m.User = instance.GetSelf().ID
instance.output <- ChanMessage{Body: m}
}
//SetLogger sets the function that the instance should use for logging.
func (instance *SlackInstance) SetLogger(logger func(level int, message string, i ...interface{})) {
instance.log = logger
}
//SetHTTPTransport allows you to set a transport to use when communicate with Slack.
func (instance *SlackInstance) SetHTTPTransport(transport *http.Transport) {
instance.transport = transport
}
//WaitForEnd will wait until the team is killed or stopped.
func (instance *SlackInstance) WaitForEnd() int {
return <-instance.quit
}