-
Notifications
You must be signed in to change notification settings - Fork 46
/
amigo.go
366 lines (319 loc) · 8.25 KB
/
amigo.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package amigo
import (
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/ivahaev/amigo/uuid"
)
var (
version = "0.1.9"
// TODO: implement function to clear old data in handlers.
agiCommandsHandlers = make(map[string]agiCommand)
agiCommandsMutex = &sync.Mutex{}
errNotConnected = errors.New("Not connected to Asterisk")
)
type handlerFunc func(map[string]string)
type eventHandlerFunc func(string)
// Amigo is a main package struct
type Amigo struct {
settings *Settings
ami *amiAdapter
defaultChannel chan map[string]string
defaultHandler handlerFunc
handlers map[string]handlerFunc
eventHandlers map[string][]eventHandlerFunc
capitalizeProps bool
connectCalled bool
mutex *sync.RWMutex
handlerMutex *sync.RWMutex
}
// Settings represents connection settings for Amigo.
// Default:
// Username = admin,
// Password = amp111,
// Host = 127.0.0.1,
// Port = 5038,
// ActionTimeout = 3s
// DialTimeout = 10s
type Settings struct {
Username string
Password string
Host string
Port string
ActionTimeout time.Duration
DialTimeout time.Duration
ReconnectInterval time.Duration
Keepalive bool
}
type agiCommand struct {
c chan map[string]string
dateTime time.Time
}
// New creates new Amigo struct with credentials provided and returns pointer to it
// Usage: New(username string, secret string, [host string, [port string]])
func New(settings *Settings) *Amigo {
prepareSettings(settings)
var ami *amiAdapter
return &Amigo{
settings: settings,
ami: ami,
handlers: map[string]handlerFunc{},
eventHandlers: map[string][]eventHandlerFunc{},
mutex: &sync.RWMutex{},
handlerMutex: &sync.RWMutex{},
}
}
// CapitalizeProps used to capitalise all prop's names when true provided.
func (a *Amigo) CapitalizeProps(c bool) {
a.capitalizeProps = c
}
// Action used to execute Actions in Asterisk. Returns immediately response from asterisk. Full response will follow.
// Usage amigo.Action(action map[string]string)
func (a *Amigo) Action(action map[string]string) (map[string]string, error) {
if !a.Connected() {
return nil, errNotConnected
}
a.mutex.Lock()
defer a.mutex.Unlock()
result := a.ami.exec(action)
if a.capitalizeProps {
e := map[string]string{}
for k, v := range result {
e[strings.ToUpper(k)] = v
}
return e, nil
}
if strings.ToLower(action["Action"]) == "logoff" {
a.ami.reconnect = false
}
return result, nil
}
// AgiAction used to execute Agi Actions in Asterisk. Returns full response.
// Usage amigo.AgiAction(channel, command string)
func (a *Amigo) AgiAction(channel, command string) (map[string]string, error) {
if !a.Connected() {
return nil, errNotConnected
}
commandID := uuid.NewV4()
action := map[string]string{
"Action": "AGI",
"Channel": channel,
"Command": command,
"CommandID": commandID,
}
ac := agiCommand{make(chan map[string]string), time.Now()}
agiCommandsMutex.Lock()
agiCommandsHandlers[commandID] = ac
agiCommandsMutex.Unlock()
a.mutex.Lock()
result := a.ami.exec(action)
a.mutex.Unlock()
if result["Response"] != "Success" {
return result, errors.New("Fail with command")
}
result = <-ac.c
delete(result, "CommandID")
if a.capitalizeProps {
for k, v := range result {
result[strings.ToUpper(k)] = v
delete(result, k)
}
}
return result, nil
}
// Connect with Asterisk.
// If connect fails, will try to reconnect every second.
func (a *Amigo) Connect() {
var connectCalled bool
a.mutex.RLock()
connectCalled = a.connectCalled
a.mutex.RUnlock()
if connectCalled {
return
}
a.mutex.Lock()
a.connectCalled = true
a.mutex.Unlock()
for {
am, err := newAMIAdapter(a.settings, a.emitEvent)
if err != nil {
go a.emitEvent("error", fmt.Sprintf("AMI Connect error: %s", err.Error()))
} else {
a.mutex.Lock()
a.ami = am
a.mutex.Unlock()
break
}
time.Sleep(time.Second)
}
go func() {
for {
select {
case <-a.ami.stopChan:
return
case e := <-a.ami.eventsChan:
a.handlerMutex.RLock()
if a.defaultChannel != nil {
a.defaultChannel <- e
}
var event = strings.ToUpper(e["Event"])
if len(event) != 0 && (a.handlers[event] != nil || a.defaultHandler != nil) {
if a.capitalizeProps {
ev := map[string]string{}
for k, v := range e {
ev[strings.ToUpper(k)] = v
}
if a.handlers[event] != nil {
a.handlers[event](ev)
}
if a.defaultHandler != nil {
a.defaultHandler(ev)
}
} else {
if a.defaultHandler != nil {
a.defaultHandler(e)
}
if a.handlers[event] != nil {
a.handlers[event](e)
}
}
}
if event == "ASYNCAGI" {
commandID, ok := e["CommandID"]
if !ok {
a.handlerMutex.RUnlock()
continue
}
agiCommandsMutex.Lock()
ac, ok := agiCommandsHandlers[commandID]
if ok {
delete(agiCommandsHandlers, commandID)
agiCommandsMutex.Unlock()
ac.c <- e
} else {
agiCommandsMutex.Unlock()
}
}
a.handlerMutex.RUnlock()
}
}
}()
}
func (a *Amigo) Close() {
if a == nil {
return
}
select {
case <-a.ami.stopChan:
default:
a.ami.reconnect = false
close(a.ami.stopChan)
}
}
// Connected returns true if successfully connected and logged in Asterisk and false otherwise.
func (a *Amigo) Connected() bool {
a.mutex.RLock()
defer a.mutex.RUnlock()
return a.ami != nil && a.ami.online()
}
// On register handler for package events. Now amigo will emit two types of events:
// "connect" fired on connection success and "error" on any error occured.
func (a *Amigo) On(event string, handler func(string)) {
a.handlerMutex.Lock()
defer a.handlerMutex.Unlock()
if _, ok := a.eventHandlers[event]; !ok {
a.eventHandlers[event] = []eventHandlerFunc{}
}
a.eventHandlers[event] = append(a.eventHandlers[event], handler)
}
// RegisterDefaultHandler registers handler function that will called on each event
func (a *Amigo) RegisterDefaultHandler(f handlerFunc) error {
a.handlerMutex.Lock()
defer a.handlerMutex.Unlock()
if a.defaultHandler != nil {
return errors.New("DefaultHandler already registered")
}
a.defaultHandler = f
return nil
}
// RegisterHandler registers handler function for provided event name
func (a *Amigo) RegisterHandler(event string, f handlerFunc) error {
event = strings.ToUpper(event)
a.handlerMutex.Lock()
defer a.handlerMutex.Unlock()
if a.handlers[event] != nil {
return errors.New("Handler already registered")
}
a.handlers[event] = f
return nil
}
// SetEventChannel sets channel for receiving all events
func (a *Amigo) SetEventChannel(c chan map[string]string) {
a.handlerMutex.Lock()
defer a.handlerMutex.Unlock()
a.defaultChannel = c
}
// UnregisterDefaultHandler removes default handler function
func (a *Amigo) UnregisterDefaultHandler(f handlerFunc) error {
a.handlerMutex.Lock()
defer a.handlerMutex.Unlock()
if a.defaultHandler == nil {
return errors.New("DefaultHandler not registered")
}
a.defaultHandler = nil
return nil
}
// EventsChanLength returns the current size of eventsChan
func (a *Amigo) EventsChanLength() int {
return len(a.ami.eventsChan)
}
// UnregisterHandler removes handler function for provided event name
func (a *Amigo) UnregisterHandler(event string, f handlerFunc) error {
event = strings.ToUpper(event)
a.handlerMutex.Lock()
defer a.handlerMutex.Unlock()
if a.handlers[event] == nil {
return errors.New("Handler not registered")
}
a.handlers[event] = nil
return nil
}
func (a *Amigo) emitEvent(name, message string) {
a.handlerMutex.RLock()
defer a.handlerMutex.RUnlock()
if len(a.eventHandlers) == 0 {
return
}
handlers, ok := a.eventHandlers[name]
if !ok {
return
}
for _, h := range handlers {
h(message)
}
}
func prepareSettings(settings *Settings) {
if settings.Username == "" {
settings.Username = "admin"
}
if settings.Password == "" {
settings.Password = "amp111"
}
if settings.Host == "" {
settings.Host = "127.0.0.1"
}
if settings.Port == "" {
settings.Port = "5038"
}
if settings.ActionTimeout == 0 {
settings.ActionTimeout = actionTimeout
}
if settings.DialTimeout == 0 {
settings.DialTimeout = dialTimeout
}
if settings.ReconnectInterval == 0 {
settings.ReconnectInterval = time.Second
}
}