def error(self, pyGlooxTag): error = self._getError(pyGlooxTag) if error: g_messengerEvents.onErrorReceived(error) else: g_logOutput.error(_LOG_AREA.PY_WRAPPER, 'Error is not resolved on the client', self.__class__.__name__, pyGlooxTag.getXml()) self._result = TASK_RESULT.CLEAR
def logger(self, key): if key in self.__loggers: return self.__loggers[key] else: g_logOutput.error(CLIENT_LOG_AREA.GENERIC, 'Events logger is not found. Available loggers are', self.__loggers.keys()) return None return None
def __doCallback(self, result = None, error = None): if error: g_logOutput.error(CLIENT_LOG_AREA.GENERIC, 'Error has been received on requesting nicknames', error) if self.__callback: self.__callback(result or {}, error) self.__callback = None return
def unregisterHandler(self, event, handler): if event in GLOOX_EVENT.ALL: handlers = self.__handlers[event] if handler in handlers: handlers.remove(handler) else: g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Event is not found', event)
def __onRequestChannels(self, chatAction, join=False): chatActionDict = dict(chatAction) data = chatActionDict.get('data', []) requestID = chatActionDict.get('requestID', -1) channels = set() isStore = requestID == -1 isMucEnabled = g_settings.server.isUserRoomsEnabled(PROTO_TYPE.XMPP) for channelData in data: received = entities.BWChannelEntity(dict(channelData)) if isMucEnabled and not received.isSystem( ) and not received.isLazy() and not received.isPrivate( ) and not received.isClan(): g_logOutput.error( CLIENT_LOG_AREA.OBSOLETE, 'Game server sends BW channel on which player is joined, but MUC are enabled. BW channel is ignored', received) continue channel = self.channelsStorage.getChannel(received) if channel: channel.update(other=received) if not isStore: self.__channels[received.getID()] = received else: if isStore: if self.channelsStorage.addChannel(received): g_messengerEvents.channels.onChannelInited(received) else: self.__channels[received.getID()] = received channel = received channels.add(channel) if join: self.joinToChannel(channel.getID()) self.onRequestChannelsComplete(requestID, channels)
def __doLogin(self): client = self.client() if not client.isConnecting(): g_logOutput.warning(CLIENT_LOG_AREA.LOGIN, 'Client is not connecting', client.getConnectionAddress(), client.getConnectionState()) yield lambda callback: callback(None) return g_logOutput.debug(CLIENT_LOG_AREA.TOKEN, 'Sends request to SPA') response = yield self.__tokenRequester.request() g_logOutput.debug(CLIENT_LOG_AREA.TOKEN, 'Response is received from SPA', response) if not response: g_logOutput.error(CLIENT_LOG_AREA.TOKEN, 'Received chat token is empty') return if response.isValid(): if response.getDatabaseID() == getPlayerDatabaseID(): g_logOutput.debug(CLIENT_LOG_AREA.LOGIN, 'Login to XMPP sever') client.login(response.getCredential()) else: g_logOutput.error(CLIENT_LOG_AREA.LOGIN, "Player's database ID mismatch", getPlayerDatabaseID()) else: g_logOutput.warning(CLIENT_LOG_AREA.TOKEN, 'Received chat token is not valid', response) self.__handleTokenError()
def __filterActions(self): for jid, action in self.__actions.items()[:]: if action.isRunning(): continue self.__actions.pop(jid) room = action.getRoom() result = action.getResult() if room is None and result != ACTION_RESULT.DO_NOTHING: g_logOutput.error(_LOG.MESSAGE, 'Action is failed', jid) continue if result & ACTION_RESULT.LAZY_JOIN > 0: LOG_DEBUG('LAZY_JOIN') room.setJoined(True) elif result & ACTION_RESULT.LAZY_LEAVE > 0: LOG_DEBUG('LAZY_LEAVE') room.clearHistory() room.setJoined(False) elif result & ACTION_RESULT.ADD_TO_STORAGE > 0: self._addChannel(room, byAction=result & ACTION_RESULT.SHOW_ROOM > 0) elif result & ACTION_RESULT.REMOVE_FROM_STORAGE > 0: self._removeChannel(room) action.clear(full=False) return
def __handleEvent(self, eventName, *args, **kwargs): handlers = self.__handlers[eventName] for handler in handlers: try: handler(*args, **kwargs) except TypeError: g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, ' Handler is invoked with error', handler) LOG_CURRENT_EXCEPTION()
def sync(self, jid, name = '', groups = None, to = _SUB.OFF, from_ = _SUB.OFF, defaultTask = None): if not jid.getDatabaseID(): g_logOutput.error(_LOG_AREA.SYNC, 'JID "{0}" is invalid'.format(jid)) return generator = self._getSyncGenerator(jid, name, groups, to, from_) if not self._handleTasksResult(jid, generator) and defaultTask: task = defaultTask(jid) task.sync(name, groups, to, from_) task.clear()
def sync(self, jid, name='', groups=None, sub=None, clanInfo=None, defaultTask=None): if not jid.getDatabaseID(): g_logOutput.error(_LOG_AREA.SYNC, 'JID "{0}" is invalid'.format(jid)) return generator = self._getSyncGenerator(jid, name, groups, sub, clanInfo) if not self._handleTasksResult(jid, generator) and defaultTask: task = defaultTask(jid) task.sync(name, groups, sub, clanInfo) task.clear()
def fini(self): client = self.__client for handlerName, _ in _GLOOX_EVENTS_LISTENERS: if not hasattr(client, handlerName): g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Handler no is found', handlerName) continue setattr(client, handlerName, None) self.__handlers.clear() g_logOutput.clear() ClientHolder._clearClient() return
def error(self, pyGlooxTag=None): self._error = self._getError( pyGlooxTag) if pyGlooxTag is not None else None if self._shadowPath is None: if self._error: g_messengerEvents.onErrorReceived(self._error) else: g_logOutput.error(_LOG_AREA.PY_WRAPPER, 'Error is not resolved on the client', self.__class__.__name__, pyGlooxTag.getXml()) self._result = TaskResult.CLEAR return
def fini(self): self.__cancelInboundSubsCallback() client = self.__client for handlerName, _ in _GLOOX_EVENTS_LISTENERS: if not hasattr(client, handlerName): g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Handler no is found', handlerName) continue setattr(client, handlerName, None) self.__handlers.clear() g_logOutput.clear() ClientHolder._clearClient()
def __handleMessage(self, _, msgType, body, jid, pyGlooxTag): if msgType not in MESSAGE_TYPE_TO_ATTR: return message = MessageHandler(MESSAGE_TYPE_TO_ATTR[msgType]).handleTag(pyGlooxTag) if not message.accountDBID: g_logOutput.error(CLIENT_LOG_AREA.MESSAGE, 'Can not find sender info', pyGlooxTag.getXml()) return if body: message.body = self.__msgFilters.chainIn(message.accountDBID, body) if not _REQUIRED_USER_TAGS.issubset(self.__receivedTags): self.__pending.insert(0, (msgType, (jid, message))) return if msgType == MESSAGE_TYPE.CHAT or msgType == MESSAGE_TYPE.NORMAL and message.isHistory(): self.__chatSessions.addMessage(jid, message)
def __addTasks(self, actionID, jid, shadowMode, *tasks): if shadowMode: if all([ task.canShadowMode() for task in tasks ]): for task in tasks[:-1]: task.setShadowMode(actionID, False) tasks[-1].setShadowMode(actionID, True) else: g_logOutput.error(_LOG.PY_WRAPPER, "Trying to shadow tasks seq, but some tasks can't be shadowed") g_messengerEvents.shadow.onActionFailed(jid.getDatabaseID(), actionID, None) if self.__tasks.addTasks(jid, *tasks): self.__tasks.runFirstTask(jid) else: return (False, ClientActionError(actionID, CLIENT_ERROR_ID.LOCKED)) return (True, None)
def __handleMessage(self, _, msgType, body, jid, pyGlooxTag): if msgType == MESSAGE_TYPE.CHAT: state, info, sentAt = ChatMessageHandler().handleTag(pyGlooxTag) if not info: g_logOutput.error(CLIENT_LOG_AREA.MESSAGE, 'Can not find sender info', pyGlooxTag.getXml()) return if body: body = self.__msgFilters.chainIn(info['dbID'], body) if _REQUIRED_USER_TAGS.issubset(self.__receivedTags): self.__chatSessions.onMessageReceived(jid, body, state, info, sentAt) else: self.__pending.insert(0, (msgType, (jid, body, state, info, sentAt)))
def __doConnect(self): client = self.client() if not client.isDisconnected(): g_logOutput.warning(CLIENT_LOG_AREA.CONNECTION, 'Client already is connected(ing)', client.getConnectionAddress(), client.getConnectionState()) return jid = self.__connectionsInfo.getPlayerFullJID() if jid: cType, host, port = self.__connectionsInfo.getNextConnection() g_logOutput.debug(CLIENT_LOG_AREA.CONNECTION, 'Connect to XMPP sever', jid, host, port) if cType == CONNECTION_IMPL_TYPE.TCP: client.connect(str(jid), host, port) elif cType == CONNECTION_IMPL_TYPE.BOSH: client.connectBosh(str(jid), host, port, '/bosh/') else: g_logOutput.error(CLIENT_LOG_AREA.CONNECTION, 'This type of connection is not supported', cType) else: g_logOutput.error(CLIENT_LOG_AREA.CONNECTION, 'JID is empty')
def __filterActions(self): for jid, action in self.__actions.items()[:]: if action.isRunning(): continue self.__actions.pop(jid) room = action.getRoom() result = action.getResult() if room is None and result != ACTION_RESULT.DO_NOTHING: g_logOutput.error(_LOG.MESSAGE, 'Action is failed', jid) continue if result & ACTION_RESULT.ADD_TO_STORAGE > 0: self._addChannel(room, byAction=result & ACTION_RESULT.SHOW_ROOM > 0) elif result & ACTION_RESULT.REMOVE_FROM_STORAGE > 0: self._removeChannel(room) action.clear(full=False) return
def __handleMessage(self, _, msgType, body, jid, pyGlooxTag): if msgType not in MESSAGE_TYPE_TO_ATTR: return message = MessageHandler( MESSAGE_TYPE_TO_ATTR[msgType]).handleTag(pyGlooxTag) if not message.accountDBID: g_logOutput.error(CLIENT_LOG_AREA.MESSAGE, 'Can not find sender info', pyGlooxTag.getXml()) return if body: message.body = self.__msgFilters.chainIn(message.accountDBID, body) if not _REQUIRED_USER_TAGS.issubset(self.__receivedTags): self.__pending.insert(0, (msgType, (jid, message))) return if msgType == MESSAGE_TYPE.CHAT or msgType == MESSAGE_TYPE.NORMAL and message.isHistory( ): self.__chatSessions.addMessage(jid, message)
def init(self): client = self.__client ClientHolder._clearClient() for (handlerName, listenerName,) in _GLOOX_EVENTS_LISTENERS: if not hasattr(client, handlerName): g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Handler no is found', handlerName) continue handler = getattr(client, handlerName) if handler: g_logOutput.warning(CLIENT_LOG_AREA.PY_WRAPPER, 'Handler already is set', handlerName) continue listener = getattr(self, listenerName, None) if listener is None or not callable(listener): g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Listener no is found', listenerName) continue setattr(client, handlerName, listener) ClientEventsHandler._setClient(self)
def init(self): client = self.__client ClientHolder._clearClient() for handlerName, listenerName in _GLOOX_EVENTS_LISTENERS: if not hasattr(client, handlerName): g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Handler no is found', handlerName) continue handler = getattr(client, handlerName) if handler: g_logOutput.warning(CLIENT_LOG_AREA.PY_WRAPPER, 'Handler already is set', handlerName) continue listener = getattr(self, listenerName, None) if listener is None or not callable(listener): g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Listener no is found', listenerName) continue setattr(client, handlerName, listener) ClientEventsHandler._setClient(self) return
def registerHandler(self, event, handler): if event in GLOOX_EVENT.ALL: handlers = self.__handlers[event] if handler in handlers: g_logOutput.warning(CLIENT_LOG_AREA.PY_WRAPPER, 'handler already exists', event, handler) elif not hasattr(handler, '__self__') or not isinstance(handler.__self__, ClientEventsHandler): g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Class of handler is not subclass of ClientEventsHandler', handler) return if callable(handler): handlers.add(handler) else: g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Handler is invalid', handler) else: g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Event is not found', event)
def registerHandler(self, event, handler): if event in GLOOX_EVENT.ALL: handlers = self.__handlers[event] if handler in handlers: g_logOutput.warning(CLIENT_LOG_AREA.PY_WRAPPER, 'handler already exists', event, handler) else: if not hasattr(handler, '__self__') or not isinstance(handler.__self__, ClientEventsHandler): g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Class of handler is not subclass of ClientEventsHandler', handler) return if callable(handler): handlers.add(handler) else: g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Handler is invalid', handler) else: g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Event is not found', event)
def setClientPresence(self, presence): if presence not in PRESENCE.RANGE: g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Value of presence is invalid', presence) return self.__client.presence = presence
def setClientPresenceWithIGR(self, presence, igrID = 0, igrRoomID = 0): if presence not in PRESENCE.RANGE: g_logOutput.error(CLIENT_LOG_AREA.PY_WRAPPER, 'Value of presence is invalid', presence) return self.__client.setPresenceWithIGR(presence, str(igrID), str(igrRoomID))