def commandInfoQuery(self, con, iq_obj): """ Send disco#info result for query for command (JEP-0050, example 6.). Return True if the result was sent, False if not """ try: jid = helpers.get_full_jid_from_iq(iq_obj) except helpers.InvalidFormat: log.warn("Invalid JID: %s, ignoring it" % iq_obj.getFrom()) return node = iq_obj.getTagAttr("query", "node") if node not in self.__commands: return False cmd = self.__commands[node] if cmd.isVisibleFor(self.isSameJID(jid)): iq = iq_obj.buildReply("result") q = iq.getTag("query") q.addChild("identity", attrs={"type": "command-node", "category": "automation", "name": cmd.commandname}) q.addChild("feature", attrs={"var": nbxmpp.NS_COMMANDS}) for feature in cmd.commandfeatures: q.addChild("feature", attrs={"var": feature}) self.connection.send(iq) return True return False
def commandItemsQuery(self, con, iq_obj): ''' Send disco#items result for query for command. Return True if the result was sent, False if not. ''' jid = helpers.get_full_jid_from_iq(iq_obj) node = iq_obj.getTagAttr('query', 'node') if node not in self.__commands: return False cmd = self.__commands[node] if cmd.isVisibleFor(self.isSameJID(jid)): iq = iq_obj.buildReply('result') self.connection.send(iq) return True return False
def commandListQuery(self, con, iq_obj): iq = iq_obj.buildReply('result') jid = helpers.get_full_jid_from_iq(iq_obj) q = iq.getTag('query') # buildReply don't copy the node attribute. Re-add it q.setAttr('node', xmpp.NS_COMMANDS) for node, cmd in self.__commands.iteritems(): if cmd.isVisibleFor(self.isSameJID(jid)): q.addChild('item', { # TODO: find the jid 'jid': self.getOurBareJID() + u'/' + self.server_resource, 'node': node, 'name': cmd.commandname}) self.connection.send(iq)
def _JingleCB(self, con, stanza): """ The jingle stanza dispatcher Route jingle stanza to proper JingleSession object, or create one if it is a new session. TODO: Also check if the stanza isn't an error stanza, if so route it adequatelly. """ # get data try: jid = helpers.get_full_jid_from_iq(stanza) except helpers.InvalidFormat: logger.warn('Invalid JID: %s, ignoring it' % stanza.getFrom()) return id_ = stanza.getID() if (jid, id_) in self.__iq_responses.keys(): self.__iq_responses[(jid, id_)].on_stanza(stanza) del self.__iq_responses[(jid, id_)] raise nbxmpp.NodeProcessed jingle = stanza.getTag('jingle') # a jingle element is not necessary in iq-result stanza # don't check for that if jingle: sid = jingle.getAttr('sid') else: sid = None for sesn in self._sessions.values(): if id_ in sesn.iq_ids: sesn.on_stanza(stanza) return # do we need to create a new jingle object if sid not in self._sessions: #TODO: tie-breaking and other things... newjingle = JingleSession(con=self, weinitiate=False, jid=jid, iq_id=id_, sid=sid) self._sessions[sid] = newjingle # we already have such session in dispatcher... self._sessions[sid].collect_iq_id(id_) self._sessions[sid].on_stanza(stanza) # Delete invalid/unneeded sessions if sid in self._sessions and \ self._sessions[sid].state == JingleStates.ended: self.delete_jingle_session(sid) raise nbxmpp.NodeProcessed
def _capsPresenceCB(self, con, presence): ''' Handle incoming presence stanzas... This is a callback for xmpp registered in connection_handlers.py''' # we will put these into proper Contact object and ask # for disco... so that disco will learn how to interpret # these caps jid = helpers.get_full_jid_from_iq(presence) contact = gajim.contacts.get_contact_from_full_jid(self.name, jid) if contact is None: room_jid, nick = gajim.get_room_and_nick_from_fjid(jid) contact = gajim.contacts.get_gc_contact( self.name, room_jid, nick) if contact is None: # TODO: a way to put contact not-in-roster # into Contacts return # get the caps element caps = presence.getTag('c') if not caps: contact.caps_node = None contact.caps_hash = None contact.caps_hash_method = None return hash_method, node, hash = caps['hash'], caps['node'], caps['ver'] if hash_method is None and node and hash: # Old XEP-115 implentation hash_method = 'old' if hash_method is None or node is None or hash is None: # improper caps in stanza, ignoring contact.caps_node = None contact.caps_hash = None contact.hash_method = None return # start disco query... gajim.capscache.preload(self, jid, node, hash_method, hash) # overwriting old data contact.caps_node = node contact.caps_hash_method = hash_method contact.caps_hash = hash
def _capsPresenceCB(self, con, presence): """ Handle incoming presence stanzas... This is a callback for xmpp registered in connection_handlers.py""" # we will put these into proper Contact object and ask # for disco... so that disco will learn how to interpret # these caps jid = helpers.get_full_jid_from_iq(presence) contact = gajim.contacts.get_contact_from_full_jid(self.name, jid) if contact is None: room_jid, nick = gajim.get_room_and_nick_from_fjid(jid) contact = gajim.contacts.get_gc_contact(self.name, room_jid, nick) if contact is None: # TODO: a way to put contact not-in-roster # into Contacts return # get the caps element caps = presence.getTag("c") if not caps: contact.caps_node = None contact.caps_hash = None contact.caps_hash_method = None return hash_method, node, hash = caps["hash"], caps["node"], caps["ver"] if hash_method is None and node and hash: # Old XEP-115 implentation hash_method = "old" if hash_method is None or node is None or hash is None: # improper caps in stanza, ignoring contact.caps_node = None contact.caps_hash = None contact.hash_method = None return # start disco query... gajim.capscache.preload(self, jid, node, hash_method, hash) # overwriting old data contact.caps_node = node contact.caps_hash_method = hash_method contact.caps_hash = hash
def commandListQuery(self, con, iq_obj): iq = iq_obj.buildReply("result") jid = helpers.get_full_jid_from_iq(iq_obj) q = iq.getTag("query") # buildReply don't copy the node attribute. Re-add it q.setAttr("node", nbxmpp.NS_COMMANDS) for node, cmd in self.__commands.iteritems(): if cmd.isVisibleFor(self.isSameJID(jid)): q.addChild( "item", { # TODO: find the jid "jid": self.getOurBareJID() + u"/" + self.server_resource, "node": node, "name": cmd.commandname, }, ) self.connection.send(iq)
def commandInfoQuery(self, con, iq_obj): ''' Send disco#info result for query for command (JEP-0050, example 6.). Return True if the result was sent, False if not. ''' jid = helpers.get_full_jid_from_iq(iq_obj) node = iq_obj.getTagAttr('query', 'node') if node not in self.__commands: return False cmd = self.__commands[node] if cmd.isVisibleFor(self.isSameJID(jid)): iq = iq_obj.buildReply('result') q = iq.getTag('query') q.addChild('identity', attrs = {'type': 'command-node', 'category': 'automation', 'name': cmd.commandname}) q.addChild('feature', attrs = {'var': xmpp.NS_COMMANDS}) for feature in cmd.commandfeatures: q.addChild('feature', attrs = {'var': feature}) self.connection.send(iq) return True return False
def _CommandExecuteCB(self, con, iq_obj): jid = helpers.get_full_jid_from_iq(iq_obj) cmd = iq_obj.getTag('command') if cmd is None: return node = cmd.getAttr('node') if node is None: return sessionid = cmd.getAttr('sessionid') if sessionid is None: # we start a new command session... only if we are visible for the jid # and command exist if node not in self.__commands.keys(): self.connection.send( xmpp.Error(iq_obj, xmpp.NS_STANZAS + ' item-not-found')) raise xmpp.NodeProcessed newcmd = self.__commands[node] if not newcmd.isVisibleFor(self.isSameJID(jid)): return # generate new sessionid sessionid = self.connection.getAnID() # create new instance and run it obj = newcmd(conn = self, jid = jid, sessionid = sessionid) rc = obj.execute(iq_obj) if rc: self.__sessions[(jid, sessionid, node)] = obj raise xmpp.NodeProcessed else: # the command is already running, check for it magictuple = (jid, sessionid, node) if magictuple not in self.__sessions: # we don't have this session... ha! return action = cmd.getAttr('action') obj = self.__sessions[magictuple] try: if action == 'cancel': rc = obj.cancel(iq_obj) elif action == 'prev': rc = obj.prev(iq_obj) elif action == 'next': rc = obj.next(iq_obj) elif action == 'execute' or action is None: rc = obj.execute(iq_obj) elif action == 'complete': rc = obj.complete(iq_obj) else: # action is wrong. stop the session, send error raise AttributeError except AttributeError: # the command probably doesn't handle invoked action... # stop the session, return error del self.__sessions[magictuple] return # delete the session if rc is False if not rc: del self.__sessions[magictuple] raise xmpp.NodeProcessed