Exemplo n.º 1
0
class JabberRelay(JabberBot, Logger):
    def __init__(self, username, password, host, port):
        Logger.__init__(self, "server")
        JabberBot.__init__(self, username, password)
        self.client = AsyncClient()
        self.client.connect(host, port)

        self.players = {} # identifier => JabberPlayer object
        self._jid2player = {} # jid => JabberPlayer object

    def shutdown(self):
        self.error("Quit")
        self.client.quit()

    def _player_create(self, jid, name):
        identifier = self.client.command(u'player_create', name)

        player = JabberPlayer(self, identifier, name, jid)
        self._jid2player[player.jid] = player
        self.players[player.identifier] = player
        return player

    def _getPlayer(self, jid):
        try:
            return self._jid2player[jid]
        except KeyError:
            name = jid
            return self._player_create(jid, name)

    def getJID(self, msg):
        jid_obj = msg.getFrom()
        jid = unicode(jid_obj)
        # strip resource
        return jid.split('/', 1)[0]

    def getPlayer(self, msg):
        jid = self.getJID(msg)
        return self._getPlayer(jid)

    @botcmd
    def player_get(self, msg, name):
        """Get about yourself"""
        player = self.getPlayer(msg)
        return player.export()

    @botcmd
    def player_list(self, msg, args):
        """List of players"""
        player_list = self._command(u'player_list')
        return unicode(player_list)

    def _command(self, command, *arguments):
        return self.client.command(command, *arguments)

    def command(self, command, *arguments):
        try:
            response = self._command(command, *arguments)
        except UnicodeException, err:
            return u"Error: %s" % unicode(err)
        else:
Exemplo n.º 2
0
class createClient(object):
    def __init__(self):
        self.client = AsyncClient()

    def __enter__(self):
        self.client.connect(DEFAULT_HOST, DEFAULT_PORT)
        return self.client

    def __exit__(self, type, value, traceback):
        try:
            if type is None:
                # (no error)
                events = getAsyncResponses(self.client)
                assert len(events) == 0
            else:
                # error
                clearAsync(self.client)
        finally:
            self.client.quit()
Exemplo n.º 3
0
 def quit(self):
     print("Exit client.")
     AsyncClient.quit(self)