Exemplo n.º 1
0
 def _obtener_agentes_activos_ami(self):
     manager = Manager()
     ami_manager_user = settings.ASTERISK['AMI_USERNAME']
     ami_manager_pass = settings.ASTERISK['AMI_PASSWORD']
     ami_manager_host = str(
         settings.OML_ASTERISK_HOSTNAME.replace('root@', ''))
     agentes_activos = []
     try:
         manager.connect(ami_manager_host)
         manager.login(ami_manager_user, ami_manager_pass)
         agentes_activos_raw = self._parsear_datos_agentes_pasada_1(
             self._ami_obtener_agentes(manager))
         agentes_activos = self._parsear_datos_agentes_pasada_2(
             agentes_activos_raw)
     except ManagerSocketException as e:
         logger.exception("Error connecting to the manager: {0}".format(
             e.message))
     except ManagerAuthException as e:
         logger.exception("Error logging in to the manager: {0}".format(
             e.message))
     except ManagerException as e:
         logger.exception("Error {0}".format(e.message))
     finally:
         manager.close()
         return agentes_activos
Exemplo n.º 2
0
class Digivoice():
    def __init__(self, grupo_portas_gsm='g1', intervalo=10):
        self.grupo_portas_gsm = grupo_portas_gsm
        self.manager = Manager()
        self.manager_connect = False
        self.intervalo = intervalo

    def debug(self, info):
        if DEBUG:
            logging.debug(info)

    def enviar_sms(self, lista, mysql_conn):
        ids = []

        if len(lista) != 0:
            try:
                self.manager.connect(ami_login['host'])
                self.manager.login(ami_login['user'], ami_login['secret'])
                print('Manager conectado:', self.manager.connected())

                if self.manager.connected():
                    for linha in lista:
                        telefone = linha['destino']
                        mensagem = linha['mensagem']
                        comando = 'dgv send sms {} {} "{}"'.format(
                            self.grupo_portas_gsm, telefone, mensagem)
                        self.debug('Comando: {}'.format(comando))
                        resultado = self.manager.command(comando)
                        ids.append(linha['id'])
                        self.debug(resultado.data)
                        query = "UPDATE mensagens SET enviado = 1, data_envio = '{}' WHERE id = {}".format(
                            datetime.today().strftime('%Y-%m-%d %H:%M:%S'),
                            linha['id'])
                        resultado = mysql_conn.query(query)
                        mysql_conn.commit()
                        sleep(self.intervalo)

                self.manager.logoff()

            except:
                self.debug('Falha no processo')

        else:
            self.debug('Não existem arquivos para envio SMS')

        return ids
    def _obtener_llamadas_en_espera_raw(self):
        manager = Manager()
        ami_manager_user = settings.ASTERISK['AMI_USERNAME']
        ami_manager_pass = settings.ASTERISK['AMI_PASSWORD']
        ami_manager_host = str(settings.OML_ASTERISK_HOSTNAME.replace('root@', ''))
        queue_status_raw = {}
        try:
            manager.connect(ami_manager_host)
            manager.login(ami_manager_user, ami_manager_pass)
            queue_status_raw = manager.send_action({"Action": "QueueStatus"}).data

        except ManagerSocketException as e:
            logger.exception("Error connecting to the manager: {0}".format(e.message))
        except ManagerAuthException as e:
            logger.exception("Error logging in to the manager: {0}".format(e.message))
        except ManagerException as e:
            logger.exception("Error {0}".format(e.message))
        finally:
            manager.close()
            return queue_status_raw
Exemplo n.º 4
0
 def _ami_manager(self, action, content):
     error = False
     data_returned = ''
     manager = Manager()
     ami_manager_user = settings.ASTERISK['AMI_USERNAME']
     ami_manager_pass = settings.ASTERISK['AMI_PASSWORD']
     ami_manager_host = str(settings.ASTERISK_HOSTNAME)
     try:
         manager.connect(ami_manager_host)
         manager.login(ami_manager_user, ami_manager_pass)
         data_returned = self._ami_action(manager, action, content)
         manager.close()
     except ManagerSocketException as e:
         logger.exception("Error connecting to the manager: {0}".format(e))
         error = True
     except ManagerAuthException as e:
         logger.exception("Error logging in to the manager: {0}".format(e))
         error = True
     except ManagerException as e:
         logger.exception("Error {0}".format(e))
         error = True
     return data_returned, error
Exemplo n.º 5
0
def voice_call_generator(router, phone_number):
    print "*********************************************"
    print "voice call generator is executed"
    print "*********************************************"
    
    asterisk_mgr = Manager()
    asterisk_mgr_username = "******"
    asterisk_mgr_password = "******"
    asterisk_mgr.connect(host = '127.0.0.1', port= 5038)
    asterisk_mgr.login(asterisk_mgr_username, asterisk_mgr_password)

    channel = "SIP/alem/%s" % phone_number
    extension = "906"
    context = "outgoing"
    priority = "1"
    other_parameters = {"max_retries":"3"}

    if asterisk_mgr.connected():
        response = asterisk_mgr.originate(channel, extension, context, priority, variables = other_parameters)
        print "********** Response is  %s " % response
    else:
        print "Connection status = %s " % asterisk_mgr.connected()
Exemplo n.º 6
0
class Test_Manager(unittest.TestCase):
    """ Test the asterisk management interface.
    """

    default_events = AsteriskEmu.default_events

    def close(self):
        if self.manager:
            self.manager.close()
            self.manager = None
        self.astemu.close()

    def setUp(self):
        self.manager = None
        self.childpid = None
        self.events = []
        self.evcount = 0
        self.queue = Queue()

    def tearDown(self):
        self.close()

    def handler(self, event, manager):
        self.events.append(event)
        self.queue.put(self.evcount)
        self.evcount += 1

    def run_manager(self, chatscript):
        self.astemu = AsteriskEmu(chatscript)
        self.port = self.astemu.port
        self.manager = Manager()
        self.manager.connect('localhost', port=self.port)
        self.manager.register_event('*', self.handler)

    def compare_result(self, r_event, event):
        for k, v in event.iteritems():
            if k == 'CONTENT':
                self.assertEqual(r_event.data, v)
            elif isinstance(v, str):
                self.assertEqual(r_event[k], v)
            else:
                self.assertEqual(r_event[k], v[-1])
                self.assertEqual(sorted(r_event.multiheaders[k]),
                                 sorted(list(v)))

    def test_login(self):
        self.run_manager({})
        r = self.manager.login('account', 'geheim')
        self.compare_result(r, self.default_events['Login'][0])
        self.close()
        self.assertEqual(self.events, [])

    def test_command(self):
        d = dict
        events = dict \
            ( Command =
                ( Event
                    ( Response  = ('Follows',)
                    , Privilege = ('Command',)
                    , CONTENT   =
                      """Channel              Location             State   Application(Data)
lcr/556              s@attendoparse:9     Up Read(dtmf,,30,noanswer,,2)    
1 active channel
1 active call
372 calls processed
--END COMMAND--\r
"""
                    )
                ,
                )
            )
        self.run_manager(events)
        r = self.manager.command('core show channels')
        self.assertEqual(self.events, [])
        self.compare_result(r, events['Command'][0])

    def test_redirect(self):
        d = dict
        events = dict \
            ( Redirect =
                ( Event
                    ( Response  = ('Success',)
                    , Message   = ('Redirect successful',)
                    )
                ,
                )
            )
        self.run_manager(events)
        r = self.manager.redirect \
            ('lcr/556', 'generic', 'Bye', context='attendo')
        self.assertEqual(self.events, [])
        self.compare_result(r, events['Redirect'][0])

    def test_originate(self):
        d = dict
        events = dict \
            ( Originate =
                ( Event
                    ( Response  = ('Success',)
                    , Message   = ('Originate successfully queued',)
                    )
                , Event
                    ( Event            = ('Newchannel',)
                    , Privilege        = ('call,all',)
                    , Channel          = ('lcr/557',)
                    , ChannelState     = ('1',)
                    , ChannelStateDesc = ('Rsrvd',)
                    , CallerIDNum      = ('',)
                    , CallerIDName     = ('',)
                    , AccountCode      = ('',)
                    , Exten            = ('',)
                    , Context          = ('',)
                    , Uniqueid         = ('1332366541.558',)
                    )
                , Event
                    ( Event            = ('NewAccountCode',)
                    , Privilege        = ('call,all',)
                    , Channel          = ('lcr/557',)
                    , Uniqueid         = ('1332366541.558',)
                    , AccountCode      = ('4019946397',)
                    , OldAccountCode   = ('',)
                    )
                , Event
                    ({ 'Event'           : ('NewCallerid',)
                     , 'Privilege'       : ('call,all',)
                     , 'Channel'         : ('lcr/557',)
                     , 'CallerIDNum'     : ('',)
                     , 'CallerIDName'    : ('',)
                     , 'Uniqueid'        : ('1332366541.558',)
                     , 'CID-CallingPres' :
                        ('0 (Presentation Allowed, Not Screened)',)
                    })
                , Event
                    ( Event            = ('Newchannel',)
                    , Privilege        = ('call,all',)
                    , Channel          = ('lcr/558',)
                    , ChannelState     = ('1',)
                    , ChannelStateDesc = ('Rsrvd',)
                    , CallerIDNum      = ('',)
                    , CallerIDName     = ('',)
                    , AccountCode      = ('',)
                    , Exten            = ('',)
                    , Context          = ('',)
                    , Uniqueid         = ('1332366541.559',)
                    )
                , Event
                    ( Event            = ('Newstate',)
                    , Privilege        = ('call,all',)
                    , Channel          = ('lcr/558',)
                    , ChannelState     = ('4',)
                    , ChannelStateDesc = ('Ring',)
                    , CallerIDNum      = ('0000000000',)
                    , CallerIDName     = ('',)
                    , Uniqueid         = ('1332366541.559',)
                    )
                , Event
                    ( Event            = ('Newstate',)
                    , Privilege        = ('call,all',)
                    , Channel          = ('lcr/558',)
                    , ChannelState     = ('7',)
                    , ChannelStateDesc = ('Busy',)
                    , CallerIDNum      = ('0000000000',)
                    , CallerIDName     = ('',)
                    , Uniqueid         = ('1332366541.559',)
                    )
                , Event
                    ({ 'Event'         : ('Hangup',)
                     , 'Privilege'     : ('call,all',)
                     , 'Channel'       : ('lcr/558',)
                     , 'Uniqueid'      : ('1332366541.559',)
                     , 'CallerIDNum'   : ('0000000000',)
                     , 'CallerIDName'  : ('<unknown>',)
                     , 'Cause'         : ('16',)
                     , 'Cause-txt'     : ('Normal Clearing',)
                    })
                , Event
                    ({ 'Event'         : ('Hangup',)
                     , 'Privilege'     : ('call,all',)
                     , 'Channel'       : ('lcr/557',)
                     , 'Uniqueid'      : ('1332366541.558',)
                     , 'CallerIDNum'   : ('<unknown>',)
                     , 'CallerIDName'  : ('<unknown>',)
                     , 'Cause'         : ('17',)
                     , 'Cause-txt'     : ('User busy',)
                    })
                , Event
                    ( Event            = ('OriginateResponse',)
                    , Privilege        = ('call,all',)
                    , Response         = ('Failure',)
                    , Channel          = ('LCR/Ext1/0000000000',)
                    , Context          = ('linecheck',)
                    , Exten            = ('1',)
                    , Reason           = ('1',)
                    , Uniqueid         = ('<null>',)
                    , CallerIDNum      = ('<unknown>',)
                    , CallerIDName     = ('<unknown>',)
                    )
                )
            )
        self.run_manager(events)
        r = self.manager.originate \
            ('LCR/Ext1/0000000000', '1'
            , context   = 'linecheck'
            , priority  = '1'
            , account   = '4019946397'
            , variables = {'CALL_DELAY' : '1', 'SOUND' : 'abandon-all-hope'}
            )
        self.compare_result(r, events['Originate'][0])
        for k in events['Originate'][1:]:
            n = self.queue.get()
            self.compare_result(self.events[n], events['Originate'][n + 1])

    def test_misc_events(self):
        d = dict
        # Events from SF bug 3470641
        # http://sourceforge.net/tracker/
        # ?func=detail&aid=3470641&group_id=76162&atid=546272
        # But we fail to reproduce the bug.
        events = dict \
            ( Login =
                ( self.default_events['Login'][0]
                , Event
                    ({ 'AppData'    : '0?begin2'
                     , 'Extension'  : 'zap2dahdi'
                     , 'Uniqueid'   : '1325950970.698'
                     , 'Priority'   : '9'
                     , 'Application': 'GotoIf'
                     , 'Context'    : 'macro-dial-one'
                     , 'Privilege'  : 'dialplan,all'
                     , 'Event'      : 'Newexten'
                     , 'Channel'    : 'Local/102@from-queue-a8ca;2'
                    })
                , Event
                    ({ 'Value'     : '2'
                     , 'Variable'  : 'MACRO_DEPTH'
                     , 'Uniqueid'  : '1325950970.698'
                     , 'Privilege' : 'dialplan,all'
                     , 'Event'     : 'VarSet'
                     , 'Channel'   : 'Local/102@from-queue-a8ca;2'
                    })
                , Event
                    ({'Privilege': 'dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 9\r\n'
                      'Application: GotoIf\r\n'
                      'AppData: 0?begin2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 10\r\n'
                      'Application: Set\r\n'
                      'AppData: THISDIAL=SIP/102\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: THISDIAL\r\n'
                      'Value: SIP/102\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 11\r\n'
                      'Application: Return\r\n'
                      'AppData: \r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: GOSUB_RETVAL\r\n'
                      'Value: \r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: dstring\r\n'
                      'Priority: 9\r\n'
                      'Application: Set\r\n'
                      'AppData: DSTRING=SIP/102&\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: DSTRING\r\n'
                      'Value: SIP/102&\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: dstring\r\n'
                      'Priority: 10\r\n'
                      'Application: Set\r\n'
                      'AppData: ITER=2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 6\r\n'
                      'Application: ExecIf\r\n'
                      'AppData: 0?Set(THISPART2=DAHDI/101)\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: ITER\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 7\r\n'
                      'Application: Set\r\n'
                      'AppData: NEWDIAL=SIP/101&\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: NEWDIAL\r\n'
                      'Value: SIP/101&\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 8\r\n'
                      'Application: Set\r\n'
                      'AppData: ITER2=2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: ITER2\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 9\r\n'
                      'Application: GotoIf\r\n'
                      'AppData: 0?begin2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 10\r\n'
                      'Application: Set\r\n'
                      'AppData: THISDIAL=SIP/101\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: THISDIAL\r\n'
                      'Value: SIP/101\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2'
                    , 'Variable': 'MACRO_DEPTH'
                    , 'Event': 'VarSet'
                    , 'Value': '2'
                    , 'Uniqueid': '1325950970.696'
                   })
                )
            )
        self.run_manager(events)
        r = self.manager.login('account', 'geheim')
        self.compare_result(r, events['Login'][0])
        evnames = []
        for s in events['Login'][3]['Privilege'].split('\r\n'):
            if s.startswith('Event:'):
                evnames.append(s.split(':')[1].strip())
        for k in xrange(30):
            n = self.queue.get()
            e = self.events[n]
            if n < 2:
                self.compare_result(e, events['Login'][n + 1])
            elif n == 2:
                self.assertEqual(e['Event'], 'VarSet')
            else:
                self.assertEqual(e['Event'], evnames[n - 3])
        self.assertEqual(len(self.events), 30)

    def test_agent_event(self):
        d = dict
        # Events from SF bug 3470641
        # http://sourceforge.net/tracker/
        # ?func=detail&aid=3470641&group_id=76162&atid=546272
        # But we fail to reproduce the bug.
        events = dict \
            ( Login =
                ( self.default_events['Login'][0]
                , Event
                    ( Event              = ('AgentCalled',)
                    , Privilege          = ('agent,all',)
                    , Queue              = ('test',)
                    , AgentCalled        = ('SIP/s394000',)
                    , AgentName          = ('910567',)
                    , ChannelCalling     = ('SIP/multifon-00000006',)
                    , DestinationChannel = ('SIP/s394000-00000007',)
                    , CallerIDNum        = ('394000',)
                    , CallerIDName       = ('Agent',)
                    , Context            = ('from-multifon',)
                    , Extension          = ('7930456789',)
                    , Priority           = ('3',)
                    , Uniqueid           = ('1302010429.6',)
                    , Variable           = ('data1=456789', 'data2=test')
                    )
                )
            )
        self.run_manager(events)
        r = self.manager.login('account', 'geheim')
        self.compare_result(r, events['Login'][0])
        for k in events['Login'][1:]:
            n = self.queue.get()
            self.compare_result(self.events[n], events['Login'][n + 1])
Exemplo n.º 7
0
 def run_manager(self, chatscript):
     self.astemu = AsteriskEmu(chatscript)
     self.port = self.astemu.port
     self.manager = Manager()
     self.manager.connect('localhost', port=self.port)
     self.manager.register_event('*', self.handler)
Exemplo n.º 8
0
 def run_manager(self, chatscript):
     self.setup_child(chatscript)
     self.manager = Manager()
     self.manager.connect('localhost', port=self.port)
     self.manager.register_event('*', self.handler)
Exemplo n.º 9
0
 def __init__(self):
     self.manager = Manager()
     self.disconnected = False
Exemplo n.º 10
0
class AMIManagerConnector(object):
    """Establece la conexión AMI utilizando la librería pyst2, para manipular asterisk
    """
    def __init__(self):
        self.manager = Manager()
        self.disconnected = False

    def connect(self):
        error = False
        ami_manager_user = settings.ASTERISK['AMI_USERNAME']
        ami_manager_pass = settings.ASTERISK['AMI_PASSWORD']
        ami_manager_host = str(settings.ASTERISK_HOSTNAME)
        try:
            self.manager.connect(ami_manager_host)
            self.manager.login(ami_manager_user, ami_manager_pass)
        except ManagerSocketException as e:
            logger.exception("Error connecting to the manager: {0}".format(e))
            error = True
        except ManagerAuthException as e:
            logger.exception("Error logging in to the manager: {0}".format(e))
            error = True
        except ManagerException as e:
            logger.exception("Error {0}".format(e))
            error = True
        return error

    def disconnect(self):
        # Atención: El Manager solo permite una sola conexión
        self.manager.close()
        self.disconnected = True

    # TODO: Refactorizar esta clase. Nombres mas descriptivos.
    #       Permitir ejecutar varios comandos con la misma sesion.
    def _ami_manager(self, action, content):
        if self.disconnected:
            raise OmlError(
                message='La conexión del Asterisk Manager ya ha sido cerrada')
        if not self.manager.connected():
            raise OmlError(message='El Asterisk Manager no ha sido conectado')

        error = False
        data_returned = ''
        try:
            data_returned = self._ami_action(action, content)
        except ManagerSocketException as e:
            logger.exception("Error connecting to the manager: {0}".format(e))
            error = True
        except ManagerAuthException as e:
            logger.exception("Error logging in to the manager: {0}".format(e))
            error = True
        except ManagerException as e:
            logger.exception("Error {0}".format(e))
            error = True
        return data_returned, error

    def _ami_action(self, action, content):
        if action == 'command':
            data_returned = self.manager.command(content).data
        elif action == 'QueueAdd':
            event_queuelog = 'ADDMEMBER'
            for i in range(len(content[2])):
                dict = {
                    'Action': action,
                    'Queue': content[2][i],
                    'Interface': content[4],
                    'Penalty': content[3][i],
                    'Paused': 0,
                    'MemberName': content[1]
                }
                data_returned = self.manager.send_action(dict)
        elif action == 'QueueRemove':
            event_queuelog = 'REMOVEMEMBER'
            for i in range(len(content[2])):
                dict = {
                    'Action': action,
                    'Queue': content[2][i],
                    'Interface': content[4],
                }
                data_returned = self.manager.send_action(dict)
        elif action == 'QueuePause':
            if content[6] == 'true':
                event_queuelog = 'PAUSEALL'
            elif content[6] == 'false':
                event_queuelog = 'UNPAUSEALL'
            dict = {
                'Action': action,
                'Interface': content[4],
                'Paused': content[6],
            }
            data_returned = self.manager.send_action(dict)
        elif action == 'dbput':
            family = content[0]
            key = content[1]
            value = content[2]
            data_returned = self.manager.dbput(family, key, value)
        elif action == 'originate':
            channel = content[0]
            exten = content[1]
            data_returned = self.manager.originate(channel,
                                                   exten,
                                                   context=content[2],
                                                   priority=1,
                                                   timeout='25000',
                                                   variables=content[3])
        if action == 'QueueAdd' or action == 'QueueRemove' or action == 'QueuePause':
            dict = {
                'Action': 'QueueLog',
                'Queue': 'ALL',
                'Event': event_queuelog,
                'Uniqueid': 'MANAGER',
                'Interface': content[0]
            }
            if action == 'QueuePause':
                dict['Message'] = content[5]
            data_returned = self.manager.send_action(dict)
        return data_returned
Exemplo n.º 11
0
 def __init__(self, grupo_portas_gsm='g1', intervalo=10):
     self.grupo_portas_gsm = grupo_portas_gsm
     self.manager = Manager()
     self.manager_connect = False
     self.intervalo = intervalo
Exemplo n.º 12
0
class Test_Manager(unittest.TestCase):
    """ Test the asterisk management interface.
    """

    default_events = dict \
        ( Login =
            ( Event
                ( Response = ('Success',)
                , Message  = ('Authentication accepted',)
                )
            ,
            )
        , Logoff =
            ( Event
                ( Response = ('Goodbye',)
                , Message  = ('Thanks for all the fish.',)
                )
            ,
            )
        )

    def asterisk_emu(self, sock, chatscript):
        """ Emulate asterisk management interface on a socket.
            Chatscript is a dict of command names to event list mapping.
            The event list contains events to send when the given
            command is recognized.
        """
        while True:
            conn, addr = sock.accept()
            f = conn.makefile('r')
            conn.close()
            f.write('Asterisk Call Manager/1.1\r\n')
            f.flush()
            cmd = lastid = ''
            try:
                for l in f:
                    if l.startswith ('ActionID:'):
                        lastid = l.split(':', 1)[1].strip()
                    elif l.startswith ('Action:'):
                        cmd = l.split(':', 1)[1].strip()
                    elif not l.strip():
                        for d in chatscript, self.default_events:
                            if cmd in d:
                                for event in d[cmd]:
                                    f.write(event.as_string(id = lastid))
                                    f.flush()
                                    if cmd == 'Logoff':
                                        f.close()
                                break
            except:
                pass
            sleep(10000) # wait for being killed

    def setup_child(self, chatscript):
        s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
        s.bind (('localhost', 0))
        s.listen(1)
        pid = fork()
        if not pid:
            # won't return
            self.asterisk_emu(s, chatscript)
        self.childpid = pid
        host, self.port = s.getsockname()
        s.close()

    def close(self):
        if self.manager:
            self.manager.close()
            self.manager = None

    def setUp(self):
        self.manager  = None
        self.childpid = None
        self.events   = []
        self.evcount  = 0
        self.queue    = Queue()

    def tearDown(self):
        self.close()
        if self.childpid:
            kill(self.childpid, SIGTERM)
            waitpid(self.childpid, 0)
            self.childpid = None

    def handler(self, event, manager):
        self.events.append(event)
        self.queue.put(self.evcount)
        self.evcount += 1

    def run_manager(self, chatscript):
        self.setup_child(chatscript)
        self.manager = Manager()
        self.manager.connect('localhost', port = self.port)
        self.manager.register_event ('*', self.handler)

    def compare_result(self, r_event, event):
        for k, v in event.iteritems():
            if k == 'CONTENT':
                self.assertEqual(r_event.data, v)
            elif isinstance(v, str):
                self.assertEqual(r_event[k], v)
            else:
                self.assertEqual(r_event[k], v[-1])
                self.assertEqual(sorted(r_event.multiheaders[k]),
                    sorted(list(v)))

    def test_login(self):
        self.run_manager({})
        r = self.manager.login('account', 'geheim')
        self.compare_result(r, self.default_events['Login'][0])
        self.close()
        self.assertEqual(self.events, [])

    def test_command(self):
        d = dict
        events = dict \
            ( Command =
                ( Event
                    ( Response  = ('Follows',)
                    , Privilege = ('Command',)
                    , CONTENT   = 
"""Channel              Location             State   Application(Data)
lcr/556              s@attendoparse:9     Up Read(dtmf,,30,noanswer,,2)    
1 active channel
1 active call
372 calls processed
--END COMMAND--\r
"""
                    )
                ,
                )
            )
        self.run_manager(events)
        r = self.manager.command ('core show channels')
        self.assertEqual(self.events, [])
        self.compare_result(r, events['Command'][0])

    def test_redirect(self):
        d = dict
        events = dict \
            ( Redirect =
                ( Event
                    ( Response  = ('Success',)
                    , Message   = ('Redirect successful',)
                    )
                ,
                )
            )
        self.run_manager(events)
        r = self.manager.redirect \
            ('lcr/556', 'generic', 'Bye', context='attendo')
        self.assertEqual(self.events, [])
        self.compare_result(r, events['Redirect'][0])

    def test_originate(self):
        d = dict
        events = dict \
            ( Originate =
                ( Event
                    ( Response  = ('Success',)
                    , Message   = ('Originate successfully queued',)
                    )
                , Event
                    ( Event            = ('Newchannel',)
                    , Privilege        = ('call,all',)
                    , Channel          = ('lcr/557',)
                    , ChannelState     = ('1',)
                    , ChannelStateDesc = ('Rsrvd',)
                    , CallerIDNum      = ('',)
                    , CallerIDName     = ('',)
                    , AccountCode      = ('',)
                    , Exten            = ('',)
                    , Context          = ('',)
                    , Uniqueid         = ('1332366541.558',)
                    )
                , Event
                    ( Event            = ('NewAccountCode',)
                    , Privilege        = ('call,all',)
                    , Channel          = ('lcr/557',)
                    , Uniqueid         = ('1332366541.558',)
                    , AccountCode      = ('4019946397',)
                    , OldAccountCode   = ('',)
                    )
                , Event
                    ({ 'Event'           : ('NewCallerid',)
                     , 'Privilege'       : ('call,all',)
                     , 'Channel'         : ('lcr/557',)
                     , 'CallerIDNum'     : ('',)
                     , 'CallerIDName'    : ('',)
                     , 'Uniqueid'        : ('1332366541.558',)
                     , 'CID-CallingPres' :
                        ('0 (Presentation Allowed, Not Screened)',)
                    })
                , Event
                    ( Event            = ('Newchannel',)
                    , Privilege        = ('call,all',)
                    , Channel          = ('lcr/558',)
                    , ChannelState     = ('1',)
                    , ChannelStateDesc = ('Rsrvd',)
                    , CallerIDNum      = ('',)
                    , CallerIDName     = ('',)
                    , AccountCode      = ('',)
                    , Exten            = ('',)
                    , Context          = ('',)
                    , Uniqueid         = ('1332366541.559',)
                    )
                , Event
                    ( Event            = ('Newstate',)
                    , Privilege        = ('call,all',)
                    , Channel          = ('lcr/558',)
                    , ChannelState     = ('4',)
                    , ChannelStateDesc = ('Ring',)
                    , CallerIDNum      = ('0000000000',)
                    , CallerIDName     = ('',)
                    , Uniqueid         = ('1332366541.559',)
                    )
                , Event
                    ( Event            = ('Newstate',)
                    , Privilege        = ('call,all',)
                    , Channel          = ('lcr/558',)
                    , ChannelState     = ('7',)
                    , ChannelStateDesc = ('Busy',)
                    , CallerIDNum      = ('0000000000',)
                    , CallerIDName     = ('',)
                    , Uniqueid         = ('1332366541.559',)
                    )
                , Event
                    ({ 'Event'         : ('Hangup',)
                     , 'Privilege'     : ('call,all',)
                     , 'Channel'       : ('lcr/558',)
                     , 'Uniqueid'      : ('1332366541.559',)
                     , 'CallerIDNum'   : ('0000000000',)
                     , 'CallerIDName'  : ('<unknown>',)
                     , 'Cause'         : ('16',)
                     , 'Cause-txt'     : ('Normal Clearing',)
                    })
                , Event
                    ({ 'Event'         : ('Hangup',)
                     , 'Privilege'     : ('call,all',)
                     , 'Channel'       : ('lcr/557',)
                     , 'Uniqueid'      : ('1332366541.558',)
                     , 'CallerIDNum'   : ('<unknown>',)
                     , 'CallerIDName'  : ('<unknown>',)
                     , 'Cause'         : ('17',)
                     , 'Cause-txt'     : ('User busy',)
                    })
                , Event
                    ( Event            = ('OriginateResponse',)
                    , Privilege        = ('call,all',)
                    , Response         = ('Failure',)
                    , Channel          = ('LCR/Ext1/0000000000',)
                    , Context          = ('linecheck',)
                    , Exten            = ('1',)
                    , Reason           = ('1',)
                    , Uniqueid         = ('<null>',)
                    , CallerIDNum      = ('<unknown>',)
                    , CallerIDName     = ('<unknown>',)
                    )
                )
            )
        self.run_manager(events)
        r = self.manager.originate \
            ('LCR/Ext1/0000000000', '1'
            , context   = 'linecheck'
            , priority  = '1'
            , account   = '4019946397'
            , variables = {'CALL_DELAY' : '1', 'SOUND' : 'abandon-all-hope'}
            )
        self.compare_result(r, events['Originate'][0])
        for k in events['Originate'][1:]:
            n = self.queue.get()
            self.compare_result(self.events[n], events['Originate'][n+1])

    def test_misc_events(self):
        d = dict
        # Events from SF bug 3470641 
        # http://sourceforge.net/tracker/
        # ?func=detail&aid=3470641&group_id=76162&atid=546272
        # But we fail to reproduce the bug.
        events = dict \
            ( Login =
                ( self.default_events['Login'][0]
                , Event
                    ({ 'AppData'    : '0?begin2'
                     , 'Extension'  : 'zap2dahdi'
                     , 'Uniqueid'   : '1325950970.698'
                     , 'Priority'   : '9'
                     , 'Application': 'GotoIf'
                     , 'Context'    : 'macro-dial-one'
                     , 'Privilege'  : 'dialplan,all'
                     , 'Event'      : 'Newexten'
                     , 'Channel'    : 'Local/102@from-queue-a8ca;2'
                    })
                , Event
                    ({ 'Value'     : '2'
                     , 'Variable'  : 'MACRO_DEPTH'
                     , 'Uniqueid'  : '1325950970.698'
                     , 'Privilege' : 'dialplan,all'
                     , 'Event'     : 'VarSet'
                     , 'Channel'   : 'Local/102@from-queue-a8ca;2'
                    })
                , Event
                    ({'Privilege': 'dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 9\r\n'
                      'Application: GotoIf\r\n'
                      'AppData: 0?begin2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 10\r\n'
                      'Application: Set\r\n'
                      'AppData: THISDIAL=SIP/102\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: THISDIAL\r\n'
                      'Value: SIP/102\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 11\r\n'
                      'Application: Return\r\n'
                      'AppData: \r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: GOSUB_RETVAL\r\n'
                      'Value: \r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: dstring\r\n'
                      'Priority: 9\r\n'
                      'Application: Set\r\n'
                      'AppData: DSTRING=SIP/102&\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: DSTRING\r\n'
                      'Value: SIP/102&\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: dstring\r\n'
                      'Priority: 10\r\n'
                      'Application: Set\r\n'
                      'AppData: ITER=2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 6\r\n'
                      'Application: ExecIf\r\n'
                      'AppData: 0?Set(THISPART2=DAHDI/101)\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: ITER\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/102@from-queue-a8ca;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.698\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 7\r\n'
                      'Application: Set\r\n'
                      'AppData: NEWDIAL=SIP/101&\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: NEWDIAL\r\n'
                      'Value: SIP/101&\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 8\r\n'
                      'Application: Set\r\n'
                      'AppData: ITER2=2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: ITER2\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 9\r\n'
                      'Application: GotoIf\r\n'
                      'AppData: 0?begin2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: MACRO_DEPTH\r\n'
                      'Value: 2\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: Newexten\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Context: macro-dial-one\r\n'
                      'Extension: zap2dahdi\r\n'
                      'Priority: 10\r\n'
                      'Application: Set\r\n'
                      'AppData: THISDIAL=SIP/101\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2\r\n'
                      'Variable: THISDIAL\r\n'
                      'Value: SIP/101\r\n'
                      'Uniqueid: 1325950970.696\r\n'
                      '\r\n'
                      'Event: VarSet\r\n'
                      'Privilege: dialplan,all\r\n'
                      'Channel: Local/101@from-queue-4406;2'
                    , 'Variable': 'MACRO_DEPTH'
                    , 'Event': 'VarSet'
                    , 'Value': '2'
                    , 'Uniqueid': '1325950970.696'
                   })
                )
            )
        self.run_manager(events)
        r = self.manager.login('account', 'geheim')
        self.compare_result(r, events['Login'][0])
        evnames = []
        for s in events['Login'][3]['Privilege'].split('\r\n'):
            if s.startswith('Event:'):
                evnames.append(s.split(':')[1].strip())
        for k in xrange(30):
            n = self.queue.get()
            e = self.events[n]
            if n < 2:
                self.compare_result(e, events['Login'][n+1])
            elif n == 2:
                self.assertEqual(e['Event'], 'VarSet')
            else:
                self.assertEqual(e['Event'], evnames[n-3])
        self.assertEqual(len(self.events), 30)

    def test_agent_event(self):
        d = dict
        # Events from SF bug 3470641 
        # http://sourceforge.net/tracker/
        # ?func=detail&aid=3470641&group_id=76162&atid=546272
        # But we fail to reproduce the bug.
        events = dict \
            ( Login =
                ( self.default_events['Login'][0]
                , Event
                    ( Event              = ('AgentCalled',)
                    , Privilege          = ('agent,all',)
                    , Queue              = ('test',)
                    , AgentCalled        = ('SIP/s394000',)
                    , AgentName          = ('910567',)
                    , ChannelCalling     = ('SIP/multifon-00000006',)
                    , DestinationChannel = ('SIP/s394000-00000007',)
                    , CallerIDNum        = ('394000',)
                    , CallerIDName       = ('Agent',)
                    , Context            = ('from-multifon',)
                    , Extension          = ('7930456789',)
                    , Priority           = ('3',)
                    , Uniqueid           = ('1302010429.6',)
                    , Variable           = ('data1=456789', 'data2=test')
                    )
                )
            )
        self.run_manager(events)
        r = self.manager.login('account', 'geheim')
        self.compare_result(r, events['Login'][0])
        for k in events['Login'][1:]:
            n = self.queue.get()
            self.compare_result(self.events[n], events['Login'][n+1])
Exemplo n.º 13
0
 def run_manager(self, chatscript):
     self.setup_child(chatscript)
     self.manager = Manager()
     self.manager.connect('localhost', port = self.port)
     self.manager.register_event ('*', self.handler)
Exemplo n.º 14
0
 def run_manager(self, chatscript):
     self.astemu = AsteriskEmu (chatscript)
     self.port = self.astemu.port
     self.manager = Manager()
     self.manager.connect('localhost', port = self.port)
     self.manager.register_event ('*', self.handler)