コード例 #1
0
ファイル: bot.py プロジェクト: SirAnthony/marvin-xmpp
class Bot:
    '''
Core commands:
!modules
    Lists all loaded modules.
!functions
    Lists all available commands.
!aliases
    Lists all aliases for commands.
!load [modulename]
!reload [modulename]
    Reloads module modulename or reloads pluggins directory.
!help [module name]
    With module name given prints module info.
    Without arguments prints this help.
'''

    version = '0.7.0'

    def __init__(self):
        self.__quit = False
        signal.signal(signal.SIGTERM, sigTermProcess)
        signal.signal(signal.SIGHUP,  sigHupProcess)
        print "Parsing config"
        config = json.loads(open('config.json').read())
        self.admin = config.get('admin')
        self.login = config.get('jid')
        self.password = config.get('password')
        self.nick = config.get('nick')
        self.rooms = config.get('conference')
        self.client = None
        self.iq = True
        self.last = datetime(1, 1, 1)
        self.__repeats = deque((), 10)
        self.manager = Manager()
        self.process()

    def connect(self):
        self.client = None #remove old client
        jid = xmpp.JID(self.login)
        client = xmpp.Client(jid.getDomain(),debug=[])

        print 'Connecting'
        if not client.connect():
            raise ConnectError('Unable to connect.')
        if not client.auth(jid.getNode(), self.password):
            raise AuthException('Unable to authorize.')

        self.client = client #Make new main client

        for room, params in self.rooms.items():
            self._joinPresence(room, params)

        client.RegisterHandler('message', self.messageProcess)
        client.RegisterHandler('presence', self.presenceProcess)
        client.RegisterHandler('iq', self.iqProcess, typ='result', ns=xmpp.NS_TIME)
        client.sendInitPresence()
        print 'Connected'
        return True

    def process(self):
        print "Bot started"
        while not self.__quit:
            try:
                self.checkReconnect()
                if self.client:
                    if self.client.Process(1) == 0:
                        self.connect()
                else:
                    if not self.connect():
                        raise ConnectError('Unknown connection error.')
            except xmpp.protocol.XMLNotWellFormed:
                logging.error('CONNECTION: reconnect (detected not valid XML)')
                self.conn = None
            except KeyboardInterrupt:
                self.exit('EXIT: interrupted by keyboard')
            except SystemExit:
                self.exit('EXIT: interrupted by SIGTERM')
            except ReloadData:
                print 'Reload: SIGHUP'
                self.manager.load_dir()
                self.connect()
            except AuthException:
                self.exit('EXIT: auth problems, check config.')
            except ConnectError, e:
                print str(e)
                time.sleep(300)
            except: