-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcallbacks.go
194 lines (176 loc) · 5.14 KB
/
callbacks.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
package lib
import (
"fmt"
"time"
"github.com/renstrom/shortuuid"
)
type MessageCallback struct {
ID string
Pattern string
Respond bool // if true, only respond if the bot is mentioned by name
Chan chan PatternMatch
SlackChan string // if set filter message callbacks to this Slack channel
}
type PatternMatch struct {
Event *Event
Match []string
}
type EventCallback struct {
ID string
Key string
Val string
Chan chan map[string]interface{}
}
type TimerCallback struct {
ID string
Schedule string
State string
Next time.Time
Chan chan time.Time
stop chan bool //send true on this channel to stop the timer
}
type QuestionCallback struct {
ID string
User string
DMChan string
Question string
Answer chan string
asked bool
}
type QuestionQueue struct {
in chan *QuestionCallback
}
func (b *Broker) RegisterCallback(callback interface{}) error {
switch callback.(type) {
case *MessageCallback:
m := callback.(*MessageCallback)
b.callbacks[M].Lock()
defer b.callbacks[M].Unlock()
b.callbacks[M].Index[m.ID] = callback
Logger.Debug("New Callback Registered, id:", m.ID, " *MessageCallback")
case *EventCallback:
e := callback.(*EventCallback)
b.callbacks[E].Lock()
defer b.callbacks[E].Unlock()
b.callbacks[E].Index[e.ID] = callback
Logger.Debug("New Callback Registered, id:", e.ID, " *EventCallback")
case *TimerCallback:
t := callback.(*TimerCallback)
t.Start()
b.callbacks[T].Lock()
defer b.callbacks[T].Unlock()
b.callbacks[T].Index[t.ID] = callback
Logger.Debug("New Callback Registered, id:", t.ID, ":= callback.( *TimerCallback")
case *LinkCallback:
l := callback.(*LinkCallback)
b.callbacks[L].Lock()
defer b.callbacks[L].Unlock()
b.callbacks[L].Index[l.ID] = callback
Logger.Debug("New Callback Registered, id:", l.ID, " *LinkCallback")
case *QuestionCallback:
q := callback.(*QuestionCallback)
b.callbacks[Q].Lock()
defer b.callbacks[Q].Unlock()
b.callbacks[Q].Index[q.ID] = callback
Logger.Debug("New Callback Registered, id:", q.ID, " *QuestionCallback")
default:
err := fmt.Errorf("unknown type in register callback: %T", callback)
Logger.Error(err)
return err
}
return nil
}
func (b *Broker) DeRegisterCallback(callback interface{}) error {
switch callback.(type) {
case *MessageCallback:
m := callback.(*MessageCallback)
b.callbacks[M].Lock()
defer b.callbacks[M].Unlock()
delete(b.callbacks[M].Index, m.ID)
Logger.Debug("De-Registered callback, id: ", m.ID)
case *EventCallback:
e := callback.(*EventCallback)
b.callbacks[E].Lock()
defer b.callbacks[E].Unlock()
delete(b.callbacks[E].Index, e.ID)
Logger.Debug("De-Registered callback, id: ", e.ID)
case *TimerCallback:
t := callback.(*TimerCallback)
t.Stop() // dont leak timers
b.callbacks[T].Lock()
defer b.callbacks[T].Unlock()
delete(b.callbacks[T].Index, t.ID)
Logger.Debug("De-Registered callback, id: ", t.ID)
case *LinkCallback:
l := callback.(*LinkCallback)
l.Delete() //dont leak httproutes
b.callbacks[L].Lock()
defer b.callbacks[L].Unlock()
delete(b.callbacks[L].Index, l.ID)
Logger.Debug("De-Registered callback, id: ", l.ID)
case *QuestionCallback:
q := callback.(*QuestionCallback)
delete(b.callbacks[Q].Index, q.ID)
Logger.Debug("De-Registered callback, id: ", q.ID)
default:
err := fmt.Errorf("unknown type in de-register callback: %T", callback)
Logger.Error(err)
return err
}
return nil
}
func (b *Broker) MessageCallback(pattern string, respond bool, channel ...string) *MessageCallback {
callback := &MessageCallback{
ID: fmt.Sprintf("message:%s", shortuuid.New()),
Pattern: pattern,
Respond: respond,
Chan: make(chan PatternMatch),
}
if channel != nil {
callback.SlackChan = channel[0] // todo: support an array of channels
}
if err := b.RegisterCallback(callback); err != nil {
Logger.Debug("error registering callback ", callback.ID, ":: ", err)
return nil
}
return callback
}
func (b *Broker) EventCallback(key string, val string) *EventCallback {
callback := &EventCallback{
ID: fmt.Sprintf("event:%s", shortuuid.New()),
Key: key,
Val: val,
Chan: make(chan map[string]interface{}),
}
if err := b.RegisterCallback(callback); err != nil {
Logger.Debug("error registering callback ", callback.ID, ":: ", err)
return nil
}
return callback
}
func (b *Broker) TimerCallback(schedule string) *TimerCallback {
callback := &TimerCallback{
ID: fmt.Sprintf("timer:%s", shortuuid.New()),
Schedule: schedule,
Chan: make(chan time.Time),
}
if err := b.RegisterCallback(callback); err != nil {
Logger.Debug("error registering callback ", callback.ID, ":: ", err)
return nil
}
return callback
}
func (b *Broker) QuestionCallback(user string, prompt string) *QuestionCallback {
callback := &QuestionCallback{
ID: fmt.Sprintf("question:%s", shortuuid.New()),
User: user,
Question: prompt,
Answer: make(chan string),
}
if err := b.RegisterCallback(callback); err != nil {
Logger.Debug("error registering callback ", callback.ID, ":: ", err)
return nil
}
return callback
}
// LinkCallback() def is in httpserver.go because it includes net/http (sorry)