summaryrefslogtreecommitdiff
path: root/misc/mon_ami/mon_ami_handler.py
blob: 015f5478c05b6a766d03affc5f011f63b99c1f71 (plain)
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
# -*- coding: utf-8 -*-
# MonAMI Asterisk Manger Interface Server
# Asterisk AMI Emulator Handler Process
# (c) AMOOMA GmbH 2012-2013

from threading import Thread
from log import ldebug, linfo, lwarn, lerror, lcritic
from time import sleep
from traceback import format_exc
from collections import deque
from urllib import unquote
from asterisk import AsteriskAMIServer
from socket import SHUT_RDWR
from helper import sval


class MonAMIHandler(Thread):

  def __init__(self, socket, address, event_socket=None):
    Thread.__init__(self)
    self.runthread = True
    self.socket = socket
    self.address = address
    self.event_socket = event_socket
    self.ami = None
    self.deregister_at_server = None
    self.message_pipe = deque()
    self.channels = {}
    self.user_password_authentication = None
    self.account_name = ''


  def stop(self):
    ldebug('thread stop', self)
    self.ami.stop()
    self.runthread = False


  def shutdown(self):
    self.deregister_at_server(self)
    ldebug('closing connection to %s:%d' % self.address)
    try:
      self.socket.shutdown(SHUT_RDWR)
      self.socket.close()
      ldebug('connection closed ', self)
    except:
      ldebug('connection closed by foreign host', self)
  
  def run(self):
    ldebug('starting MonAMI handler thread', self)

    # starting asterisk AMI thread
    self.ami = AsteriskAMIServer(self.socket, self.address, self.message_pipe)
    self.ami.start()
    self.ami.send_greeting()

    # register for events
    self.event_socket.register_client_queue(self.message_pipe, 'CHANNEL_CREATE')
    self.event_socket.register_client_queue(self.message_pipe, 'CHANNEL_DESTROY')
    self.event_socket.register_client_queue(self.message_pipe, 'CHANNEL_STATE')
    self.event_socket.register_client_queue(self.message_pipe, 'CHANNEL_ANSWER')
    self.event_socket.register_client_queue(self.message_pipe, 'CHANNEL_BRIDGE')

    while self.runthread and self.ami.isAlive():
      if self.message_pipe:
        message = self.message_pipe.pop()
        message_type = sval(message, 'type')
        if message_type == 'freeswitch_event':
          self.handle_fs_event(message['body'])
        elif message_type == 'ami_client_message':
          self.handle_ami_client_message(message['body'])
      else:
        sleep(0.1)

    self.event_socket.deregister_client_queue_all(self.message_pipe)

    ldebug('exiting MonAMI handler thread', self)
    self.shutdown()


  def handle_ami_client_message(self, message):

    if 'Action' in message:
      action = message['Action'].lower()
      
      if action == 'login':
        if 'UserName' in message:
          self.account_name = message['UserName']
        if 'Secret' in message and self.user_password_authentication and self.user_password_authentication(self.account_name, message['Secret']):
          self.ami.send_login_ack()
          ldebug('AMI connection authenticated - account: %s' % self.account_name, self)
        else:
          self.ami.send_login_nack()
          linfo('AMI authentication failed - account: %s' % sval(message, 'UserName'), self)
          self.ami.stop()
          self.stop()
      elif action == 'logoff':
        self.ami.send_logout_ack()
        ldebug('AMI logout', self)
        self.ami.stop()
        self.stop()
      elif action == 'ping':
        self.ami.send_pong(sval(message, 'ActionID'))
      elif action == 'status':
        self.ami.send_status_ack(sval(message, 'ActionID'))
      elif action == 'command' and sval(message, 'Command') == 'core show version':
        self.ami.send_asterisk_version(sval(message, 'ActionID'))
      elif action == 'hangup':
        account_name, separator, uuid = str(sval(message, 'Channel')).rpartition('-uuid-')
        if account_name != '':
          self.event_socket.hangup(uuid)
          self.ami.send_hangup_ack()
      elif action == 'originate':
        self.message_originate(message)
      elif action == 'extensionstate':
        self.ami.send_extension_state(sval(message, 'ActionID'), sval(message, 'Exten'), sval(message, 'Context'))
      else:
        ldebug('unknown asterisk message received: %s' % message, self)
        self.ami.send_message_unknown(message['Action'])


  def to_unique_channel_name(self, uuid, channel_name):

    # strip anything left of sip_account_name
    path, separator, contact_part = channel_name.rpartition('/sip:')
    if path == '':
      path, separator, contact_part = channel_name.rpartition('/')

    # if failed return name unchanged
    if path == '':
      return channel_name


    # strip domain part
    account_name = contact_part.partition('@')[0]

    # if failed return name unchanged
    if account_name == '':
      return channel_name

    # create unique channel name
    return 'SIP/%s-uuid-%s' % (account_name, uuid)

  def message_originate(self, message):
    destination_number = str(sval(message, 'Exten'))
    action_id = sval(message, 'ActionID')
    self.ami.send_originate_ack(action_id)
    uuid = self.event_socket.originate(self.account_name, destination_number, action_id)


  def handle_fs_event(self, event):
    event_type = event['Event-Name']
    #ldebug('event type received: %s' % event_type, self)

    event_types = {
      'CHANNEL_CREATE':   self.event_channel_create,
      'CHANNEL_DESTROY':  self.event_channel_destroy,
      'CHANNEL_STATE':    self.event_channel_state,
      'CHANNEL_ANSWER':   self.event_channel_answer,
      'CHANNEL_BRIDGE':   self.event_channel_bridge,
    }

    uuid = event_types[event_type](event)

    if not uuid:
      return False

    channel = sval(self.channels, uuid);

    if not channel:
      return False

    o_uuid = channel['o_uuid']
    o_channel = sval(self.channels, o_uuid);

    if sval(channel, 'origination_action') or sval(o_channel, 'origination_action'):
      if not sval(channel, 'ami_start') and not sval(o_channel, 'ami_start'):
        if sval(channel, 'owned') and sval(channel, 'origination_action'):
          ldebug('sending AMI events for origitate call start (on this channel): %s' % uuid, self)
          self.ami_send_originate_start(channel)
          self.channels[uuid]['ami_start'] = True
        elif sval(o_channel, 'owned') and sval(o_channel, 'origination_action'):
          ldebug('sending AMI events for origitate call start (on other channel): %s' % uuid, self)
          self.ami_send_originate_start(o_channel)
          self.channels[o_uuid]['ami_start'] = True
      elif o_channel:
        if sval(channel, 'owned') and sval(channel, 'origination_action'):
          ldebug('sending AMI events for origitate call progress (on this channel): %s' % uuid, self)
          self.ami_send_originate_outbound(channel)
          self.channels[uuid]['origination_action'] = False
        elif sval(o_channel, 'owned') and sval(o_channel, 'origination_action'):
          ldebug('sending AMI events for origitate call progress (on other channel): %s' % uuid, self)
          self.ami_send_originate_outbound(o_channel)
          self.channels[o_uuid]['origination_action'] = False
    elif o_channel:
      if not sval(channel, 'ami_start') and not sval(o_channel, 'ami_start'):
        if sval(channel, 'owned') and sval(channel, 'direction') == 'inbound':
          ldebug('sending AMI events for outbound call start (on this channel): %s' % uuid, self)
          self.ami_send_outbound_start(channel)
          self.channels[uuid]['ami_start'] = True
        elif sval(o_channel, 'owned') and sval(channel, 'direction') == 'outbound':
          ldebug('sending AMI events for outbound call start (on other channel): %s' % uuid, self)
          self.ami_send_outbound_start(o_channel)
          self.channels[o_uuid]['ami_start'] = True
      
      if not sval(channel, 'ami_start')and not sval(o_channel, 'ami_start'):
        if sval(channel, 'owned') and sval(channel, 'direction') == 'outbound':
          ldebug('sending AMI events for inbound call start (on this channel): %s' % uuid, self)
          self.ami_send_inbound_start(channel)
          self.channels[uuid]['ami_start'] = True
        elif sval(o_channel, 'owned') and sval(channel, 'direction') == 'inbound':
          ldebug('sending AMI events for inbound call start (on other channel): %s' % uuid, self)
          self.ami_send_inbound_start(o_channel)
          self.channels[o_uuid]['ami_start'] = True


  def event_channel_create(self, event):
    uuid = sval(event, 'Unique-ID')
    o_uuid = sval(event, 'Other-Leg-Unique-ID')

    if uuid in self.channels:
      ldebug('channel already listed: %s' % uuid, self)
      return false

    channel_name = self.to_unique_channel_name(uuid, unquote(str(sval(event, 'Channel-Name'))))
    o_channel_name = self.to_unique_channel_name(o_uuid, unquote(str(sval(event, 'Other-Leg-Channel-Name'))))

    if self.account_name in channel_name:
      channel_owned = True
    else:
      channel_owned = False

    if self.account_name in o_channel_name:
      channel_related = True
    else:
      channel_related = False

    if not channel_owned and not channel_related:
      ldebug('channel neither owned nor reladed to account: %s' % uuid, self)
      return False
    
    channel = {
      'uuid':               uuid,
      'name':               channel_name,
      'direction':          sval(event, 'Call-Direction'),
      'channel_state':      sval(event, 'Channel-State'),
      'call_state':         sval(event, 'Channel-Call-State'),
      'answer_state':       sval(event, 'Answer-State'),
      'owned':              channel_owned,
      'related':            channel_related,
      'caller_id_name':     unquote(str(sval(event, 'Caller-Caller-ID-Name'))),
      'caller_id_number':   unquote(str(sval(event, 'Caller-Caller-ID-Number'))),
      'callee_id_name':     unquote(str(sval(event, 'Caller-Callee-ID-Name'))),
      'callee_id_number':   unquote(str(sval(event, 'Caller-Callee-ID-Number'))),
      'destination_number': str(sval(event, 'Caller-Destination-Number')),
      'origination_action': sval(event, 'variable_origination_action'),
      'o_uuid': o_uuid,
      'o_name': o_channel_name,            
    }

    if channel['answer_state'] == 'ringing':
      if channel['direction'] == 'inbound':
        asterisk_channel_state = 4
      else:
        asterisk_channel_state = 5
    else:
      asterisk_channel_state = 0

    if not o_uuid:
      ldebug('one legged call, channel: %s' % uuid, self)
    elif o_uuid not in self.channels:
      o_channel = {
        'uuid':               o_uuid,
        'name':               o_channel_name,
        'direction':          sval(event, 'Other-Leg-Direction'),
        'channel_state':      sval(event, 'Channel-State'),
        'call_state':         sval(event, 'Channel-Call-State'),
        'answer_state':       sval(event, 'Answer-State'),
        'owned':              channel_related,
        'related':            channel_owned,
        'caller_id_name':     unquote(str(sval(event, 'Caller-Caller-ID-Name'))),
        'caller_id_number':   unquote(str(sval(event, 'Caller-Caller-ID-Number'))),
        'callee_id_name':     unquote(str(sval(event, 'Caller-Callee-ID-Name'))),
        'callee_id_number':   unquote(str(sval(event, 'Caller-Callee-ID-Number'))),
        'destination_number': str(sval(event, 'Other-Leg-Destination-Number')),
        'o_uuid': uuid,
        'o_name': channel_name,            
      }

      if o_channel['answer_state'] == 'ringing':
        if o_channel['direction'] == 'inbound':
          asterisk_o_channel_state = 4
        else:
          asterisk_o_channel_state = 5
      else:
        asterisk_o_channel_state = 0

      ldebug('create channel list entry for related channel: %s, name: %s' % (o_uuid, o_channel_name), self)
      self.channels[o_uuid] = o_channel
    else:
      ldebug('updating channel: %s, name: %s, o_uuid: %s, o_name %s' % (o_uuid, o_channel_name, uuid, channel_name), self)
      self.channels[o_uuid]['o_uuid'] = uuid
      self.channels[o_uuid]['o_name'] = channel_name
      o_channel = self.channels[o_uuid]

    if channel_owned:
      ldebug('create channel list entry for own channel: %s, name: %s' % (uuid, channel_name), self)
    elif channel_related:
      ldebug('create channel list entry for related channel: %s, name: %s' % (uuid, channel_name), self)
    
    self.channels[uuid] = channel
      
    return uuid
    

  def event_channel_destroy(self, event):
    uuid = sval(event, 'Unique-ID')
    hangup_cause_code = int(sval(event, 'variable_hangup_cause_q850'))
    channel = sval(self.channels, uuid)

    if channel:
      channel['hangup_cause_code'] = hangup_cause_code
      if sval(channel, 'ami_start'):
        self.ami_send_outbound_end(channel)
      del self.channels[uuid]
      ldebug('channel removed from list: %s, cause %d' % (uuid, hangup_cause_code), self)

    return uuid


  def event_channel_state(self, event): 
    uuid = sval(event, 'Unique-ID')
    channel_state = sval(event, 'Channel-State')
    call_state    = sval(event, 'Channel-Call-State')
    answer_state  = sval(event, 'Answer-State')

    if sval(self.channels, uuid) and False:
      ldebug('updating channel state - channel: %s, channel_state: %s, call_state %s, answer_state: %s' % (uuid, channel_state, call_state, answer_state), self)
      self.channels[uuid]['channel_state'] = channel_state
      self.channels[uuid]['call_state']    = call_state
      self.channels[uuid]['answer_state']  = answer_state

    return uuid


  def event_channel_answer(self, event):
    uuid = sval(event, 'Unique-ID')
    o_uuid = sval(event, 'Other-Leg-Unique-ID')
    channel = sval(self.channels, uuid)
    if not o_uuid:
      o_uuid = sval(channel, 'o_uuid')
    o_channel = sval(self.channels, o_uuid)
    origination_action = sval(channel, 'origination_action')

    if channel:
      channel_state = sval(event, 'Channel-State')
      call_state    = sval(event, 'Channel-Call-State')
      answer_state  = sval(event, 'Answer-State')
      ldebug('channel answered - channel: %s, owned: %s, channel_state: %s, call_state %s, answer_state: %s, other leg: %s' % (uuid, sval(channel, 'owned'), channel_state, call_state, answer_state, o_uuid), self)
      self.ami.send_event_newstate(uuid, sval(channel, 'name'), 6, sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'))

      self.channels[uuid]['channel_state'] = channel_state
      self.channels[uuid]['call_state']    = call_state
      self.channels[uuid]['answer_state']  = answer_state

      if sval(channel, 'origination_action'):
        if sval(channel, 'owned'):
          ldebug('sending AMI originate response - success: %s' % uuid, self)
          self.ami.send_event_originate_response(sval(channel, 'uuid'), sval(channel, 'name'), sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'), '101', sval(channel, 'origination_action'), 4)
      elif not o_uuid:
        ldebug('sending AMI events for outbound call start on one legged call (this channel): %s' % uuid, self)
        self.ami_send_outbound_start(channel)
        self.ami.send_event_bridge(uuid, sval(channel, 'name'), sval(channel, 'caller_id_number'), o_uuid, sval(o_channel, 'name'), sval(o_channel, 'caller_id_number'))

        self.channels[uuid]['ami_start'] = True
      
      return uuid
    
    return False

     
  def event_channel_bridge(self, event):
    uuid = sval(event, 'Unique-ID')
    o_uuid = sval(event, 'Other-Leg-Unique-ID')

    ldebug('bridge channel: %s to %s' % (uuid, o_uuid), self)
    channel = sval(self.channels, uuid)
    o_channel = sval(self.channels, o_uuid)

    if sval(channel, 'owned') or sval(o_channel, 'owned'):
      ldebug('sending AMI bridge response: %s -> %s' % (uuid, o_uuid), self)
      self.ami.send_event_bridge(uuid, sval(channel, 'name'), sval(channel, 'caller_id_number'), o_uuid, sval(o_channel, 'name'), sval(o_channel, 'caller_id_number'))


  def ami_send_outbound_start(self, channel):
    self.ami.send_event_newchannel(sval(channel, 'uuid'), sval(channel, 'name'), 0, sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'), sval(channel, 'destination_number'))
    self.ami.send_event_newstate(sval(channel, 'uuid'), sval(channel, 'name'), 4, sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'))
    self.ami.send_event_newchannel(sval(channel, 'o_uuid'), sval(channel, 'o_name'), 0, '', '', '')
    self.ami.send_event_dial_begin(sval(channel, 'uuid'), sval(channel, 'name'), sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'), sval(channel, 'o_name'), sval(channel, 'o_uuid'), sval(channel, 'destination_number'))
    self.ami.send_event_newcallerid(sval(channel, 'o_uuid'), sval(channel, 'o_name'), sval(channel, 'destination_number'), '', 0)
    self.ami.send_event_newstate(sval(channel, 'o_uuid'), sval(channel, 'o_name'), 5, sval(channel, 'destination_number'), '')


  def ami_send_outbound_end(self, channel):
    self.ami.send_event_hangup(sval(channel, 'o_uuid'), sval(channel, 'o_name'), sval(channel, 'destination_number'), '', sval(channel, 'hangup_cause_code'))
    self.ami.send_event_dial_end(sval(channel, 'uuid'), sval(channel, 'name'))
    self.ami.send_event_hangup(sval(channel, 'uuid'), sval(channel, 'name'), sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'), sval(channel, 'hangup_cause_code'))

    if sval(channel, 'origination_action'):
      self.ami.send_event_originate_response(sval(channel, 'uuid'), sval(channel, 'name'), sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'), sval(channel, 'destination_number'), sval(channel, 'origination_action'), 1)
 

  def ami_send_inbound_start(self, channel):
    self.ami.send_event_newchannel(sval(channel, 'o_uuid'), sval(channel, 'o_name'), 0, sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'), sval(channel, 'callee_id_number'))
    self.ami.send_event_newstate(sval(channel, 'o_uuid'), sval(channel, 'o_name'), 4, sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'))
    self.ami.send_event_newchannel(sval(channel, 'uuid'), sval(channel, 'name'), 0, '', '', '')
    self.ami.send_event_dial_begin(sval(channel, 'o_uuid'), sval(channel, 'o_name'), sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'), sval(channel, 'name'), sval(channel, 'uuid'), sval(channel, 'destination_number'))
    self.ami.send_event_newstate(sval(channel, 'uuid'), sval(channel, 'name'), 5, sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'))
    self.ami.send_event_newcallerid(sval(channel, 'uuid'), sval(channel, 'name'), sval(channel, 'destination_number'), '', 0)


  def ami_send_originate_start(self, channel):
    self.ami.send_event_newchannel(sval(channel, 'uuid'), sval(channel, 'name'), 0, '', '', '')
    self.ami.send_event_newcallerid(sval(channel, 'uuid'), sval(channel, 'name'), sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'), 0)
    self.ami.send_event_newaccountcode(sval(channel, 'uuid'), sval(channel, 'name'))
    self.ami.send_event_newcallerid(sval(channel, 'uuid'), sval(channel, 'name'), sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'), 0)
    self.ami.send_event_newstate(sval(channel, 'uuid'), sval(channel, 'name'), 5, sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'))


  def ami_send_originate_outbound(self, channel):
    self.ami.send_event_newchannel(sval(channel, 'o_uuid'), sval(channel, 'o_name'), 0, '', '', '')
    self.ami.send_event_dial_begin(sval(channel, 'uuid'), sval(channel, 'name'), sval(channel, 'caller_id_number'), sval(channel, 'caller_id_name'), sval(channel, 'o_name'), sval(channel, 'o_uuid'), sval(channel, 'destination_number'))
    self.ami.send_event_newcallerid(sval(channel, 'o_uuid'), sval(channel, 'o_name'), sval(channel, 'destination_number'), '', 0)
    self.ami.send_event_newstate(sval(channel, 'o_uuid'), sval(channel, 'o_name'), 5, sval(channel, 'destination_number'), '')