def joinToRoom(self, roomJID, password='', name='', initResult=ACTION_RESULT.SHOW_ROOM): if not g_settings.server.XMPP.isMucServiceAllowed( hostname=roomJID.getDomain()): return (False, ClientActionError(CLIENT_ACTION_ID.JOIN_USER_ROOM, CLIENT_ERROR_ID.NOT_SUPPORTED)) else: entry, exists = self._searchChannel(roomJID) if exists is not None: if exists.isJoined(): g_messengerEvents.channels.onPlayerEnterChannelByAction( exists) return (True, None) entry = exists if roomJID not in self.__actions: if password: entry.setPassword(password) action = self.createJoinAction(entry, initResult, name=name) self.__actions[roomJID] = action action.start() else: action = self.__actions[roomJID] if not password or not action.setPassword(password): return (False, ClientActionError(CLIENT_ACTION_ID.JOIN_USER_ROOM, CLIENT_ERROR_ID.LOCKED)) return (True, None)
def createRoom(self, name, password='', initResult=ACTION_RESULT.SHOW_ROOM): roomJID = jid_entity.makeUserRoomJID() if not roomJID: return (False, ClientActionError(CLIENT_ACTION_ID.CREATE_USER_ROOM, CLIENT_ERROR_ID.NOT_SUPPORTED)) else: exists = self.getChannelByName(name) if exists is not None: return (False, ClientChannelError( CHANNEL_ERROR_ID.NAME_ALREADY_EXISTS, name)) if roomJID not in self.__actions: from gui.shared.utils import getPlayerName name = '{0} ({1})'.format(name, getPlayerName()) action = CreateAction(roomJID, name, password, initResult) self.__actions[roomJID] = action action.start() else: return (False, ClientActionError(CLIENT_ACTION_ID.CREATE_USER_ROOM, CLIENT_ERROR_ID.LOCKED)) return (True, None)
def addIgnored(self, dbID, name): error = self._checkCooldown(CLIENT_ACTION_ID.ADD_IGNORED) if error: return (False, error) tasks, itemType = [], XMPP_ITEM_TYPE.EMPTY_ITEM contact = self.usersStorage.getUser(dbID, PROTO_TYPE.XMPP) if contact: if contact.isCurrentPlayer(): return (False, ClientActionError(CLIENT_ACTION_ID.ADD_IGNORED, CLIENT_ERROR_ID.GENERIC)) itemType = contact.getItemType() if itemType in XMPP_ITEM_TYPE.BLOCK_ITEMS: return (False, ClientContactError(CONTACT_ERROR_ID.BLOCK_ITEM_EXISTS, contact.getFullName())) length = self.usersStorage.getCount( ItemsFindCriteria(XMPP_ITEM_TYPE.PERSISTENT_BLOCKING_LIST)) if length >= CONTACT_LIMIT.BLOCK_MAX_COUNT: return (False, ClientIntLimitError(LIMIT_ERROR_ID.MAX_BLOCK_ITEMS, CONTACT_LIMIT.BLOCK_MAX_COUNT)) if contact: jid = contact.getJID() if itemType in XMPP_ITEM_TYPE.SUB_PENDING_ITEMS: tasks.append(sub_tasks.CancelSubscriptionTask(jid)) else: jid = makeContactJID(dbID) tasks.append(block_tasks.AddBlockItemTask(jid, name)) if itemType in XMPP_ITEM_TYPE.ROSTER_ITEMS: groups = contact.getGroups() if groups: tasks.append(roster_tasks.EmptyGroupsTask(jid, groups=groups)) self.__cooldown.process(CLIENT_ACTION_ID.ADD_IGNORED) return self.__addTasks(CLIENT_ACTION_ID.ADD_IGNORED, jid, *tasks)
def find(self, token, **kwargs): """ Process find request :param token: search token (username prefix) :param kwargs: args :return: None """ error = self.__checkCooldown(CLIENT_ACTION_ID.FIND_USERS_BY_PREFIX) if error: self._onSearchFailed(error.getMessage()) return token = token.strip() isCorrect, reason = checkAccountName(token) if not isCorrect: self._onSearchFailed(reason) return client = self.proto.client if client.isConnected(): query = NicknamePrefixSearchQuery( token, limit=self.getSearchResultLimit()) self._lastRequestID = client.sendIQ(query) self.__cooldown.process(CLIENT_ACTION_ID.FIND_USERS_BY_PREFIX) else: error = ClientActionError(CLIENT_ACTION_ID.FIND_USERS_BY_PREFIX, CLIENT_ERROR_ID.NOT_CONNECTED) self._onSearchFailed(error.getMessage())
def createServerActionMessageError(actionID, pyGlooxTag): errorType, condition = ProxyHandler( StanzaErrorExtension()).handleTag(pyGlooxTag) return ServerActionError( actionID, errorType, condition ) if condition != DEF_STANZA_ERROR_CONDITION else ClientActionError( actionID, CLIENT_ERROR_ID.GENERIC)
def find(self, token, **kwargs): client = self.proto.client if client.isConnected(): query = ChannelSearchQuery(token, to=self.__service, count=self.getSearchResultLimit()) self._lastRequestID = client.sendIQ(query) else: error = ClientActionError(CLIENT_ACTION_ID.SEARCH_USER_ROOM, CLIENT_ERROR_ID.NOT_CONNECTED) self._onSearchFailed(error.getMessage())
def addFriend(self, dbID, name, group=None, shadowMode=False): error = self._checkCooldown(CLIENT_ACTION_ID.ADD_FRIEND) if error and not shadowMode: return (False, error) else: if group: if not self.usersStorage.isGroupExists(group): return (False, ClientContactError(CONTACT_ERROR_ID.GROUP_NOT_FOUND, group)) groups = {group} else: groups = None contact = self.usersStorage.getUser(dbID, PROTO_TYPE.XMPP) tasks, itemType = [], XMPP_ITEM_TYPE.EMPTY_ITEM if contact: if contact.isCurrentPlayer(): return (False, ClientActionError(CLIENT_ACTION_ID.ADD_FRIEND, CLIENT_ERROR_ID.GENERIC)) jid = contact.getJID() itemType = contact.getItemType() if itemType in XMPP_ITEM_TYPE.ROSTER_ITEMS: return (False, ClientContactError(CONTACT_ERROR_ID.ROSTER_ITEM_EXISTS, contact.getFullName())) subTo = contact.getSubscription()[0] else: jid = makeContactJID(dbID) subTo = _SUB.OFF result, error = self.__subsRestrictions.canAddFriends() if not result: return (False, error) if itemType == XMPP_ITEM_TYPE.BLOCK_ITEM: tasks.append(block_tasks.RemoveBlockItemTask(jid, name)) tasks.append(roster_tasks.AddRosterItemTask(jid, name, groups)) elif itemType == XMPP_ITEM_TYPE.ROSTER_BLOCK_ITEM: tasks.append(block_tasks.RemoveBlockItemTask(jid, name)) task, exclude = None, set() rosterGroups = contact.getItem().getRosterGroups() for rosterGroup in rosterGroups: if self.usersStorage.isGroupEmpty(rosterGroup): exclude.add(rosterGroup) if groups: if groups != exclude: task = roster_tasks.ChangeRosterItemGroupsTask(jid, name, groups, exclude) elif rosterGroups: task = roster_tasks.ChangeRosterItemGroupsTask(jid, name, set(), exclude) if task: tasks.append(task) elif itemType in XMPP_ITEM_TYPE.SUB_PENDING_ITEMS: tasks.append(sub_tasks.ApproveSubscriptionTask(jid, name)) if groups: tasks.append(roster_tasks.ChangeRosterItemGroupsTask(jid, name, groups)) else: tasks.append(roster_tasks.AddRosterItemTask(jid, name, groups)) if subTo == _SUB.OFF: tasks.append(sub_tasks.AskSubscriptionTask(jid)) if not shadowMode: self.__cooldown.process(CLIENT_ACTION_ID.ADD_FRIEND) return self.__addTasks(CLIENT_ACTION_ID.ADD_FRIEND, jid, shadowMode, *tasks)
def _join(self, info): if info is not None: statuses = info.statuses else: statuses = () if MUC_STATUS.SELF_PRESENCE in statuses and MUC_STATUS.CREATE_ROOM in statuses: self._step = ENTRY_STEP.SET_DATA_FORM self._sendIQ(chat_ext.UserRoomConfigurationFormSet(self._room.getID(), self._room.getName(), self._room.getPassword())) else: g_messengerEvents.onErrorReceived(ClientActionError(CLIENT_ACTION_ID.CREATE_USER_ROOM, CLIENT_ERROR_ID.GENERIC)) self._result = ACTION_RESULT.DO_NOTHING self._sendPresence(chat_ext.MUCLeaveQuery(self._getUserJID())) return
def leaveFromRoom(self, roomJID): _, exists = self._searchChannel(roomJID) if exists is None: return (False, ClientActionError(CLIENT_ACTION_ID.LEAVE_USER_ROOM, CLIENT_ERROR_ID.GENERIC)) elif not exists.isJoined(): self._removeChannel(exists) return (True, None) else: entry = self.__actions.pop(roomJID, None) if entry is not None: entry.clear() action = self.createLeaveAction(exists) self.__actions[roomJID] = action action.start() return
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 renameGroup(self, oldName, newName): error = self._checkCooldown(CLIENT_ACTION_ID.CHANGE_GROUP) if error: return (False, error) elif self.usersStorage.isGroupExists(newName): return (False, ClientContactError(CONTACT_ERROR_ID.GROUP_EXISTS)) elif newName == oldName: return (False, ClientActionError(CLIENT_ACTION_ID.CHANGE_GROUP, CLIENT_ERROR_ID.GENERIC)) elif self.usersStorage.isGroupEmpty(oldName): self.usersStorage.changeEmptyGroup(oldName, newName) g_messengerEvents.users.onEmptyGroupsChanged({newName}, {oldName}) return (True, None) else: task = self.__makeChangeGroupsChain(oldName, newName) self.__cooldown.process(CLIENT_ACTION_ID.CHANGE_GROUP) return self.__addTasks(CLIENT_ACTION_ID.CHANGE_GROUP, task.getJID(), task)
def addTmpIgnored(self, dbID, name): error = self._checkCooldown(CLIENT_ACTION_ID.ADD_IGNORED) if error: return (False, error) tasks, itemType = [], XMPP_ITEM_TYPE.EMPTY_ITEM contact = self.usersStorage.getUser(dbID, PROTO_TYPE.XMPP) if contact: if contact.isCurrentPlayer(): return (False, ClientActionError(CLIENT_ACTION_ID.ADD_IGNORED, CLIENT_ERROR_ID.GENERIC)) itemType = contact.getItemType() if itemType in XMPP_ITEM_TYPE.BLOCK_ITEMS: return (False, ClientContactError(CONTACT_ERROR_ID.BLOCK_ITEM_EXISTS, contact.getFullName())) if contact: jid = contact.getJID() else: jid = makeContactJID(dbID) tasks.append(block_tasks.AddTmpBlockItemTask(jid, name)) self.__cooldown.process(CLIENT_ACTION_ID.ADD_IGNORED) return self.__addTasks(CLIENT_ACTION_ID.ADD_IGNORED, jid, *tasks)
def __addTasks(self, actionID, jid, *tasks): if self.__tasks.addTasks(jid, *tasks): self.__tasks.runFirstTask(jid) else: return (False, ClientActionError(actionID, CLIENT_ERROR_ID.LOCKED)) return (True, None)
def _showClientActionError(actionID, errorID=CLIENT_ERROR_ID.NOT_SUPPORTED): g_messengerEvents.onErrorReceived(ClientActionError(actionID, errorID))