Ejemplo n.º 1
0
 def del_member(self, e):
     """Remove members from the conference"""
     self.last_event = e
     self.conf_size = int(getUnquotedHeader(e, 'Conference-Size')) # Adjust the conference size
     self.addAction(e)
     confObj = Session.query(Conference).get(getUnquotedHeader(e, 'Conference-Unique-ID'))
     if confObj is None:
         self.log.debug('Conference does not exist on DB, we must create one.')
         owner = Session.query(ModeratorPin).get(getUnquotedHeader(e, 'Conference-Name'))
         if owner is None:
             self.log.warning('Error because we could not get owner.')
             Session.remove()
             return
         
         self.log.info('Creating a new conference for conference id: %s' % getUnquotedHeader(last_event, 'Conference-Unique-ID'))
         confObj = Conference(getUnquotedHeader(last_event, 'Conference-Unique-ID'), parseEventDate(last_event), unicode(getUnquotedHeader(last_event, 'Conference-Name')), unicode(getUnquotedHeader(last_event, 'Conference-Profile-Name')))
         owner.conferences.append(confObj)
         Session.add(confObj)
         Session.flush()
     if self.conf_size == 0:
         confObj.ended = parseEventDate(e)
         try:
             self.log.debug("Conference %s has ended. Removing session %s." % (self.conf_name, Session))
             Session.commit()
         except Exception, e:
             Session.rollback()
             Session.remove()
             self.log.exception('Could not commit to the DB.')
             return
Ejemplo n.º 2
0
 def stop_recording(e, config):
     """Process the process of recording being stopped"""
     log = logging.getLogger('Conference')
     
     owner = Session.query(ModeratorPin).get(getUnquotedHeader(e, 'Conference-Name'))
     if owner is None:
         log.warning('Error because we could not get owner.')
         Session.remove()
         return
     
     confObj = Session.query(Conference).get(getUnquotedHeader(e, 'Conference-Unique-ID'))
     if confObj is None:
         log.info('Creating a new conference for conference id: %s' % getUnquotedHeader(e, 'Conference-Unique-ID'))
         confObj = Conference(getUnquotedHeader(e, 'Conference-Unique-ID'), parseEventDate(e), unicode(getUnquotedHeader(e, 'Conference-Name')), unicode(getUnquotedHeader(e, 'Conference-Profile-Name')))
         owner.conferences.append(confObj)
         Session.add(confObj)
         Session.flush()
         actionObj = ConferenceAction(unicode(getUnquotedHeader(e, 'Action')), 
                                     unicode(getUnquotedHeader(e, 'Caller-Caller-ID-Name')),
                                     unicode(getUnquotedHeader(e, 'Caller-Caller-ID-Number')),
                                     unicode(getUnquotedHeader(e, 'Conference-Size')),
                                     unicode(getUnquotedHeader(e, 'Member-Type')),
                                     parseEventDate(e),
                                     confObj.id)
         confObj.actions.append(actionObj)
         Session.add(actionObj)
         log.debug('Conference %s has been created. Assigning the pointer.' % getUnquotedHeader(e, 'Conference-Name'))
         
     confObj.recording = os.path.join(config.get('mp3', 'store_to'), getUnquotedHeader(e, 'Path').split('/').pop()[:-3] + 'mp3')
     host = unicode(getUnquotedHeader(e, 'FreeSWITCH-IPv4'))
         
     try:
         try:
             t = Thread(target=postRecordingThread, args=(config, getUnquotedHeader(e, 'Path'), confObj, host, owner))
             t.start()
         except:
             log.exception('Could not run thread and commit to database!')
             Session.remove()
             return
     finally:
         try:
             sendEmail(config, getUnquotedHeader(e, 'Path'), confObj, owner)
             Session.commit()
             Session.remove()
         except Exception, e:
             log.exception('Could not commit to database!')
             Session.rollback()
             Session.remove()
             return
Ejemplo n.º 3
0
def main(config):
            
    configureLogging(config)
    log = logging.getLogger('main')
    
    if config.getboolean('daemon', 'daemon'):
        global PIDFILE
        PIDFILE = config.get('daemon', 'pidfile')
        daemonize()
        
    
    pw = config.get('host', 'password')
    h = config.get('host', 'host')
    p = config.get('host', 'port')
    
    #Setting up the DB connection
    log.info('Connecting to DB on: %s ' % (config.get('database', 'url')))

    if ("sqlite" in config.get('database', 'url')):
        engine = create_engine( config.get('database', 'url'), poolclass=NullPool )
        log.warning('You are using SQLite and we will use NullPool as pooling engine.')
    else:
        engine = create_engine( config.get('database', 'url'), pool_recycle=3600 )


    sm = sessionmaker( autoflush=False, autocommit=False, bind=engine)
    Session = scoped_session( sm )
    
    log.info('Connecting to: %s:%s ' % (h, p))
    con = ESLconnection(h, p, pw)
    con.events('plain', 'CUSTOM conference::maintenance')
    while con.connected():
        e = con.recvEvent()

        if e is None:
            continue
        log.debug('Received event:\n%s' % e.serialize())

        conf_uuid = getUnquotedHeader(e, 'Conference-Unique-ID')
        conf_profile = getUnquotedHeader(e, 'Conference-Profile-Name')
        conf_name = getUnquotedHeader(e, 'Conference-Name')
        conf_size = getUnquotedHeader(e, 'Conference-Size')
        caller_id_name = getUnquotedHeader(e, 'Caller-Caller-ID-Name')
        caller_id_number = getUnquotedHeader(e, 'Caller-Caller-ID-Number')
        member_id = getUnquotedHeader(e, 'Member-ID')
        member_type = getUnquotedHeader(e, 'Member-Type')
        action = getUnquotedHeader(e, 'Action')
        event_date = getUnquotedHeader(e, 'Event-Date-Local')
        if event_date is not None:
            # Check the python version, because strptime does not exist in it.
            if sys.version_info[2] < 5:
                spl = event_date.split('-')
                year = int(spl[0])
                month = int(spl[1])
                spl2 = spl[2].split(' ')
                day = int(spl2[0])
                spl3 = spl2[1].split(':')
                hour = int(spl3[0])
                minutes = int(spl3[1])
                seconds = int(spl3[2])
                event_date = datetime(year=year, month=month, day=day, hour=hour, minute=minutes, second=seconds)
            else:
                event_date = datetime.strptime(event_date, '%Y-%m-%d %H:%M:%S')

        if not conf_name:
            log.warning('There is no conf name on this event.\n%s' % e.serialize())
            continue
            
        try:
            owner = Session.query(ModeratorPin).get(conf_name)
        except Exception, err:
            log.critical(traceback.format_exc())
        if owner is None:
            log.critical('Error because we could not get owner.')
            Session.remove()
            continue
            
        confObj = Session.query(Conference).get(conf_uuid)
        if confObj is None:
            log.debug('Conference does not exist, we must create one.')
        
        if action == 'add-member':
            log.info('Member added...')
            if conf_size == '1':
                confObj = Conference(conf_uuid, event_date, unicode(conf_name), unicode(conf_profile))
                owner.conferences.append(confObj)
                Session.add(confObj)
                log.debug('Action taken on %s. %s' % (conf_name, action))
                actionObj = ConferenceAction(unicode(action), 
                                            unicode(caller_id_name),
                                            unicode(caller_id_number),
                                            unicode(conf_size),
                                            unicode(member_type),
                                            event_date,
                                            confObj.id)
                confObj.actions.append(actionObj)
                Session.add(actionObj)
                Session.commit()
                Session.remove()
                log.debug('Conference %s has been created.' % conf_name)
                continue
                
        elif action == 'del-member':
            if conf_size == '0':
                confObj.ended = event_date
                log.debug('Action taken on %s. %s' % (conf_name, action))
                actionObj = ConferenceAction(unicode(action), 
                                            unicode(caller_id_name),
                                            unicode(caller_id_number),
                                            unicode(conf_size),
                                            unicode(member_type),
                                            event_date,
                                            confObj.id)
                confObj.actions.append(actionObj)
                Session.add(actionObj)

                Session.commit()
                Session.remove()
                log.debug('Conference %s has been destroyed.' % conf_name)
                continue
                

        
        log.debug('Action taken on %s. %s' % (conf_name, action))

        # This action comes after the conference has ended.
        if action == 'stop-recording':
            confObj.recording = os.path.join(config.get('mp3', 'store_to'), getUnquotedHeader(e, 'Path').split('/').pop()[:-3] + 'mp3')
            host = unicode(getUnquotedHeader(e, 'FreeSWITCH-IPv4'))
            Session.commit()
            try:
                owner_obj = Session.query(ModeratorPin).get(confObj.owner)
            except Exception, e:
                log.critical('Could not find owner. Db corruption? %s' % e)
                log.critical('Exception: %s' % traceback.format_exc())
                Session.remove()
                continue
                
            t = Thread(target=postRecordingThread, args=(config, getUnquotedHeader(e, 'Path'), confObj, host, owner_obj))
            log.debug('Starting thread %s' % t)
            t.start()
            log.debug('Thread started...')
            Session.remove()
            continue