-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathami_connector.go
574 lines (471 loc) · 13.2 KB
/
ami_connector.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package main
import (
"errors"
"regexp"
"strings"
"time"
"github.com/ivahaev/amigo"
log "github.com/sirupsen/logrus"
"github.com/serfreeman1337/asterlink/connect"
)
type numForm struct {
R *regexp.Regexp
Expr string
Repl string
}
type AmiConfig struct {
Ami struct {
Host string
Port string
User string
Pass string
}
DP struct {
In []string `yaml:"incoming_context"`
Out []string `yaml:"outgoing_context"`
Ext []string `yaml:"ext_context"`
Dial string `yaml:"dial_context"`
Vote string `yaml:"vote_ivr"`
} `yaml:"dialplan"`
Form struct {
hasCid bool
hasDial bool
hasDid bool
Cid []numForm `yaml:"cid_format"`
Dial []numForm `yaml:"dial_format"`
LimDID []string `yaml:"dids"`
DID map[string]bool
} `yaml:"pbx"`
}
type amiConnector struct {
ami *amigo.Amigo
cfg *AmiConfig
cdr map[string]*connect.Call
rec map[string]string
revote *regexp.Regexp
rechan *regexp.Regexp
connector connect.Connecter
}
func (a *amiConnector) Init() {
a.ami.Connect()
}
func (a *amiConnector) Originate(ext string, dest string, oID string) {
ext = strings.TrimSpace(strings.ReplaceAll(ext, "\n", ""))
dest = strings.TrimSpace(strings.ReplaceAll(dest, "\n", ""))
oID = strings.TrimSpace(strings.ReplaceAll(oID, "\n", ""))
cLog := log.WithFields(log.Fields{"ext": ext, "dest": dest, "oID": oID})
num, ok := a.formatNum(dest, false)
if !ok {
cLog.WithField("num", num).Error("Invalid number to originate call")
return
}
res, err := a.ami.Action(map[string]string{
"Action": "Originate",
"Channel": "Local/" + ext + "@" + a.cfg.DP.Dial,
"Exten": num,
"Context": a.cfg.DP.Dial,
"Priority": "1",
"Variable": "SF_CONNECTOR=" + oID + "|" + dest, // keep track of call record and original caller id
"CallerID": ext,
"Async": "true",
"Codecs": "alaw,ulaw", // TODO: config?
})
if err != nil {
cLog.Error(err)
} else if res["Response"] != "Success" {
cLog.Error(res["Message"])
} else {
cLog.Debug("New originate request")
}
}
func (a *amiConnector) formatNum(cID string, isCid bool) (string, bool) {
var hasForm bool
var form []numForm
if isCid {
hasForm = a.cfg.Form.hasCid
form = a.cfg.Form.Cid
} else {
hasForm = a.cfg.Form.hasDial
form = a.cfg.Form.Dial
}
if !hasForm {
return cID, true
}
for _, f := range form {
if f.R.MatchString(cID) {
return f.R.ReplaceAllString(cID, f.Repl), true
}
}
return "", false
}
func (a *amiConnector) useRec(c *connect.Call, uID string, lID string) {
// Check recording with uniqueid (when recoging is turned on extension).
rec, ok := a.rec[uID]
if !ok { // Then look for recording with linkedid (when recording is turned on queue etc.).
rec, ok = a.rec[lID]
uID = lID
}
if !ok {
return
}
c.Rec = rec
delete(a.rec, uID)
c.Log.WithField("rec", c.Rec).Debug("Set recording file")
}
func (a *amiConnector) isContext(context string, of []string) bool {
for _, v := range of {
if context == v {
return true
}
}
return false
}
func (a *amiConnector) onConnect(message string) {
log.WithField("msg", message).Info("Established connection with AMI")
fexpr := func(r []string) string {
var s string
if len(r) == 1 {
s += r[0]
} else {
s += "("
for _, v := range r {
s += v + "|"
}
s = s[:len(s)-1]
s += ")"
}
return s
}
// posix regexp
filter := "(Event: Newchannel.*[[:cntrl:]]Context: " + fexpr(a.cfg.DP.In) + "[[:cntrl:]]"
filter += "|Event: (DialBegin|DialEnd).*[[:cntrl:]]Context: " + fexpr(append(a.cfg.DP.Ext, a.cfg.DP.Out...)) + "[[:cntrl:]]"
filter += "|Event: Newexten.*[[:cntrl:]]"
if a.cfg.DP.Vote == "" {
filter += "Application: MixMonitor"
} else {
filter += "(Application: MixMonitor|Context: " + a.cfg.DP.Vote + "[[:cntrl:]].*AppData: CDR\\(userfield\\)=\".*rate.*\")"
filter += "|Variable: IVR_CONTEXT.*Value: " + a.cfg.DP.Vote
a.revote = regexp.MustCompile(`CDR\(userfield\)="rate:(\d+)"`)
}
filter += "|Event: (Hangup|BlindTransfer)|Variable: SF_CONNECTOR)"
// let the Asterisk filter events for us
res, err := a.ami.Action(map[string]string{"Action": "Filter", "Operation": "Add", "Filter": filter})
if err != nil {
log.WithField("filter", filter).Fatal(err)
} else if res["Response"] != "Success" {
log.WithField("filter", filter).Fatal(res["Message"])
}
}
func (a *amiConnector) onError(message string) {
log.Error(message)
}
// incoming call
func (a *amiConnector) onNewchannel(e map[string]string) {
if /*_, ok := IncomingContext[e["Context"]]; !ok || */ e["Exten"] == "s" { // TODO: review this 's'!
return
}
if _, ok := a.cdr[e["Linkedid"]]; ok {
log.WithField("lid", e["Linkedid"]).Warn("Already tracked")
return
}
// DID Limit
if a.cfg.Form.hasDid {
if _, ok := a.cfg.Form.DID[e["Exten"]]; !ok {
log.WithFields(log.Fields{"lid": e["Linkedid"], "did": e["Exten"]}).Debug("Skip incoming call for DID")
return
}
}
// skip anonymous calls ?
if e["CallerIDNum"] == "<unknown>" {
log.WithFields(log.Fields{"lid": e["Linkedid"]}).Warn("Anonymous CallerID")
return
}
cID, ok := a.formatNum(e["CallerIDNum"], true)
if !ok {
log.WithFields(log.Fields{"lid": e["Linkedid"], "cid": e["CallerIDNum"]}).Warn("Unknown incoming CallerID")
return
}
// register incoming call
a.cdr[e["Linkedid"]] = &connect.Call{
LID: e["Linkedid"],
Dir: connect.In,
CID: cID,
DID: e["Exten"],
TimeCall: time.Now(),
Ch: e["Channel"],
Log: log.WithField("lid", e["Linkedid"]),
}
a.cdr[e["Linkedid"]].Log.Debug("New incoming call")
go a.connector.Start(a.cdr[e["Linkedid"]])
}
// operator dial
func (a *amiConnector) onDialBegin(e map[string]string) {
c, ok := a.cdr[e["Linkedid"]]
if !ok {
// TODO: review
if !a.isContext(e["Context"], a.cfg.DP.Out) {
return
}
// DID Limit
if a.cfg.Form.hasDid {
if _, ok := a.cfg.Form.DID[e["DestConnectedLineNum"]]; !ok {
log.WithFields(log.Fields{"lid": e["Linkedid"], "did": e["DestConnectedLineNum"]}).Debug("Skip outgoing call for DID")
return
}
}
cID, ok := a.formatNum(e["DestCallerIDNum"], true)
if !ok {
log.WithFields(log.Fields{"lid": e["Linkedid"], "cid": e["DestCallerIDNum"]}).Warn("Unknown outgoing CallerID")
return
}
rext := a.rechan.FindStringSubmatch(e["Channel"])
if len(rext) == 0 {
return
}
// register outbound call
c = &connect.Call{
LID: e["Linkedid"],
Dir: connect.Out,
CID: cID,
DID: e["CallerIDNum"],
TimeCall: time.Now(),
TimeDial: time.Now(),
Ch: e["Channel"],
ChDest: e["DestChannel"],
Log: log.WithField("lid", e["Linkedid"]),
Ext: rext[1],
}
a.cdr[e["Linkedid"]] = c
c.Log.Debug("New outgoing call")
go func() {
a.connector.Start(c)
a.connector.Dial(c, rext[1])
}()
return
} else if c.O { // Originated call
if a.isContext(e["Context"], a.cfg.DP.Ext) {
c.Ch = e["DestChannel"]
c.Ext = e["DestCallerIDNum"]
c.Log.WithField("ext", c.Ext).Debug("From")
a.useRec(c, e["Uniqueid"], e["Linkedid"])
go a.connector.Dial(c, c.Ext)
} else if a.isContext(e["Context"], a.cfg.DP.Out) {
// TODO: do something about originated CallerID
c.ChDest = e["DestChannel"]
c.TimeDial = time.Now()
c.DID = e["DestConnectedLineNum"]
c.Log.WithField("did", c.DID).Debug("Via")
}
return
}
/*if _, ok := ExtContext[e["Context"]]; !ok {
return
}*/
if c.TimeDial.IsZero() {
c.TimeDial = time.Now()
}
c.Log.WithField("ext", e["DestCallerIDNum"]).Debug("Dial")
go a.connector.Dial(c, e["DestCallerIDNum"])
}
// operator answer
func (a *amiConnector) onDialEnd(e map[string]string) {
c, ok := a.cdr[e["Linkedid"]]
if !ok {
return
}
if e["DialStatus"] != "ANSWER" {
return
}
switch c.Dir {
case connect.In:
/*if _, ok := ExtContext[e["Context"]]; !ok {
return
}*/
c.TimeAnswer = time.Now()
c.ChDest = e["DestChannel"]
c.Ext = e["DestCallerIDNum"]
a.useRec(c, e["Uniqueid"], e["Linkedid"])
c.Log.WithField("ext", c.Ext).Debug("Answer")
go a.connector.Answer(c, c.Ext)
case connect.Out:
if c.O && !a.isContext(e["Context"], a.cfg.DP.Out) { // Originated call
return
}
// TODO: the heck am i doing ?
if a.isContext(e["Context"], a.cfg.DP.Ext) && c.ChDest == e["Channel"] {
// BlindTransfer hack ?
rext := a.rechan.FindStringSubmatch(e["DestChannel"])
if len(rext) == 0 {
return
}
c.Ext = rext[1]
c.Log.WithField("ext", c.Ext).Debug("Answer 2")
a.useRec(c, e["Uniqueid"], e["Linkedid"])
go a.connector.Answer(c, c.Ext)
return
}
rext := a.rechan.FindStringSubmatch(e["Channel"])
if len(rext) == 0 {
return
}
c.TimeAnswer = time.Now()
c.Ext = rext[1]
c.Log.WithField("ext", c.Ext).Debug("Dial 2")
a.useRec(c, e["Uniqueid"], e["Linkedid"])
go a.connector.Answer(c, rext[1])
}
}
// call recording and vote
func (a *amiConnector) onNewexten(e map[string]string) {
if e["Application"] == "MixMonitor" { // recording
a.rec[e["Uniqueid"]] = strings.Split(e["AppData"], ",")[0]
log.WithFields(log.Fields{"lid": e["Linkedid"], "uid": e["Uniqueid"], "rec": a.rec[e["Uniqueid"]]}).Debug("MixMonitor")
} else { // vote
c, ok := a.cdr[e["Linkedid"]]
if !ok {
return
}
appData := e["AppData"][:len(e["AppData"])-1] // 3 IQ WHITESPACE TRIM
c.Vote = a.revote.ReplaceAllString(appData, "$1")
c.Log.WithField("vote", c.Vote).Debug("Call voted")
}
}
func (a *amiConnector) onBlindTransfer(e map[string]string) {
// what ?
lID, ok := e["TransfererLinkedid"]
if !ok {
lID, ok = e["Linkedid"]
if !ok {
log.Warn("Unknown BlindTransfer")
log.Warn(e)
return
}
}
c, ok := a.cdr[lID]
if !ok {
return
}
// forget original operator
if c.Dir == connect.In {
c.ChDest = ""
} else if c.Dir == connect.Out {
c.Ch = ""
}
c.Log.WithField("ext", c.Ext).Debug("BlindTransfer")
go a.connector.StopDial(c, c.Ext)
}
// call finish
func (a *amiConnector) onHangup(e map[string]string) {
_, ok := a.rec[e["Uniqueid"]]
if ok {
log.WithFields(log.Fields{"lid": e["Linkedid"], "uid": e["Uniqueid"]}).Debug("Clear recording")
delete(a.rec, e["Uniqueid"])
}
c, ok := a.cdr[e["Linkedid"]]
if !ok {
return
}
if e["Channel"] == c.Ch || e["Channel"] == c.ChDest {
// call marked for vote, keep tracking after operator hangup
if c.Vote == "-" && !c.TimeAnswer.IsZero() && e["Channel"] == c.ChDest {
go a.connector.StopDial(c, c.Ext)
c.ChDest = ""
c.Log.Debug("Wait for vote")
return
}
c.Log.Debug("Call finished")
go a.connector.End(c, e["Cause"])
delete(a.cdr, e["Linkedid"])
} else {
if e["Context"] == a.cfg.DP.Dial && (e["ChannelState"] == "5" || e["ChannelState"] == "0") {
c.Log.WithField("ext", e["CallerIDNum"]).Debug("Dial stop")
go a.connector.StopDial(c, e["CallerIDNum"])
}
}
}
// originated call
func (a *amiConnector) onVarSet(e map[string]string) {
c, ok := a.cdr[e["Linkedid"]]
if !ok {
// VarSet for originated call
if e["Variable"] != "SF_CONNECTOR" || e["Exten"] == "failed" {
return
}
// split tracker variable (see Originate request)
r := strings.Split(e["Value"], "|")
a.cdr[e["Linkedid"]] = &connect.Call{
LID: e["Linkedid"],
CID: r[1],
TimeCall: time.Now(),
TimeDial: time.Now(),
Dir: connect.Out,
Ch: e["Channel"],
O: true,
Log: log.WithField("lid", e["Linkedid"]),
}
log.WithField("oid", r[0]).Debug("New originated call")
a.connector.OrigStart(a.cdr[e["Linkedid"]], r[0])
return
}
if a.cfg.DP.Vote == "" || e["Value"] != a.cfg.DP.Vote {
return
}
// Mark call for vote
c.Vote = "-"
}
func (a *amiConnector) SetConnector(cc connect.Connecter) {
a.connector = cc
}
func NewAmiConnector(cfg *AmiConfig) (a *amiConnector, err error) {
if cfg.Ami.Host == "" || cfg.Ami.User == "" || cfg.Ami.Pass == "" {
err = errors.New("AMI settings are missing from config file")
return
} else if len(cfg.DP.In) == 0 || len(cfg.DP.Out) == 0 || len(cfg.DP.Ext) == 0 || cfg.DP.Dial == "" {
err = errors.New("DialPlan configuration are missing from config file")
return
}
if cfg.Ami.Port == "" {
cfg.Ami.Port = "5038"
}
if len(cfg.Form.Cid) != 0 {
for i, v := range cfg.Form.Cid {
if cfg.Form.Cid[i].R, err = regexp.Compile(v.Expr); err != nil {
return
}
}
cfg.Form.hasCid = true
}
if len(cfg.Form.Dial) != 0 {
for i, v := range cfg.Form.Dial {
if cfg.Form.Dial[i].R, err = regexp.Compile(v.Expr); err != nil {
return
}
}
cfg.Form.hasDial = true
}
if len(cfg.Form.LimDID) != 0 {
cfg.Form.hasDid = true
cfg.Form.DID = make(map[string]bool)
for _, d := range cfg.Form.LimDID {
cfg.Form.DID[d] = true
}
}
settings := &amigo.Settings{Host: cfg.Ami.Host, Username: cfg.Ami.User, Password: cfg.Ami.Pass, Port: cfg.Ami.Port}
a = &amiConnector{
cfg: cfg,
ami: amigo.New(settings),
cdr: make(map[string]*connect.Call),
rec: make(map[string]string),
rechan: regexp.MustCompile(`.*\/(\d+)`),
}
a.ami.On("connect", a.onConnect)
a.ami.On("error", a.onError)
a.ami.RegisterHandler("Newchannel", a.onNewchannel)
a.ami.RegisterHandler("DialBegin", a.onDialBegin)
a.ami.RegisterHandler("DialEnd", a.onDialEnd)
a.ami.RegisterHandler("Newexten", a.onNewexten)
a.ami.RegisterHandler("BlindTransfer", a.onBlindTransfer)
a.ami.RegisterHandler("Hangup", a.onHangup)
a.ami.RegisterHandler("VarSet", a.onVarSet)
return
}