def test_parse(self): """ Test different forms of JIDs. """ # Basic forms self.assertEqual(jid.parse("user@host/resource"), ("user", "host", "resource")) self.assertEqual(jid.parse("user@host"), ("user", "host", None)) self.assertEqual(jid.parse("host"), (None, "host", None)) self.assertEqual(jid.parse("host/resource"), (None, "host", "resource")) # More interesting forms self.assertEqual(jid.parse("foo/bar@baz"), (None, "foo", "bar@baz")) self.assertEqual(jid.parse("boo@foo/bar@baz"), ("boo", "foo", "bar@baz")) self.assertEqual(jid.parse("boo@foo/bar/baz"), ("boo", "foo", "bar/baz")) self.assertEqual(jid.parse("boo/foo@bar@baz"), (None, "boo", "foo@bar@baz")) self.assertEqual(jid.parse("boo/foo/bar"), (None, "boo", "foo/bar")) self.assertEqual(jid.parse("boo//foo"), (None, "boo", "/foo"))
def test_parse(self): """ Test different forms of JIDs. """ # Basic forms self.assertEquals(jid.parse("user@host/resource"), ("user", "host", "resource")) self.assertEquals(jid.parse("user@host"), ("user", "host", None)) self.assertEquals(jid.parse("host"), (None, "host", None)) self.assertEquals(jid.parse("host/resource"), (None, "host", "resource")) # More interesting forms self.assertEquals(jid.parse("foo/bar@baz"), (None, "foo", "bar@baz")) self.assertEquals(jid.parse("boo@foo/bar@baz"), ("boo", "foo", "bar@baz")) self.assertEquals(jid.parse("boo@foo/bar/baz"), ("boo", "foo", "bar/baz")) self.assertEquals(jid.parse("boo/foo@bar@baz"), (None, "boo", "foo@bar@baz")) self.assertEquals(jid.parse("boo/foo/bar"), (None, "boo", "foo/bar")) self.assertEquals(jid.parse("boo//foo"), (None, "boo", "/foo"))
def on_presence(self, presence): print('received presence: %s' % presence.toXml().encode('utf-8')) user, host, res = parse(presence['from']) jid = '@'.join((user, host)) if presence.hasAttribute('type'): if presence['type'] == 'subscribe': if jid in self.users: print('received subscription') if self.users[jid] is False: iq = IQ(self.xmlstream, 'set') query = domish.Element(('jabber:iq:roster', 'query')) item = domish.Element((None, 'item')) item['jid'] = jid item['name'] = jid item.addElement('group', content='controllers') query.addChild(item) iq.addChild(query) iq.addCallback(self.subscribed, jid) self.xmlstream.send(iq) pres = domish.Element((None, 'presence')) pres['type'] = 'subscribed' pres['to'] = jid self.xmlstream.send(pres) else: presence = domish.Element((None, 'presence')) presence['type'] = 'unsubscribed' presence['to'] = presence['from'] self.xmlstream.send(presence)
def presence_handler(self, elem): content = get_content_dict(elem) from_id = None if elem.hasAttribute('from'): from_id = elem['from'] if 'priority' in content: from_jid = jid.parse(from_id) userhost = jid.JID(from_id).userhost() self.roster[userhost]['resources'][from_jid[2]]['priority'] = content['priority'] if elem.hasAttribute('type') and elem.hasAttribute('from'): presence_type = elem['type'] from_id = elem['from'] if presence_type == 'subscribe': # If we get a subscription request, confirm and set our presence send_presence(presence_type='subscribed', to=from_id, xmlstream=self.stream) send_presence(status='Online', to=from_id, xmlstream=self.stream) logging.debug("%s subscribed to us." % from_id) if from_id not in self.roster: # Reciprocate! send_subscription_request(from_id, xmlstream=self.stream) if presence_type == 'subscribed': get_roster(self.stream) logging.debug("We subscribed to %s." % from_id) if presence_type == 'unsubscribed': get_roster(self.stream) logging.debug("%s unsubscribed from us." % from_id)
def onMessage(self, msg): jidparts = jid.parse(msg['from']) userhost = "%s@%s" % (jidparts[0], jidparts[1]) if self.rooms.has_key(userhost): room_id = self.rooms[userhost][0] body = xpath.queryForString("/message/body", msg) return Message(speaker=jidparts[2], message=body, room_id=room_id, created_at=datetime.datetime.now()).save()
def run(self, application, jidstr, password, muc, server): self.xmppclient = XMPPClient(JID(jidstr), password) if CowrieConfig().getboolean('output_xmpp', 'debug', fallback=False): self.xmppclient.logTraffic = True (user, host, resource) = jid.parse(jidstr) self.muc = XMPPLoggerProtocol(muc, server, user + '-' + resource) self.muc.setHandlerParent(self.xmppclient) self.xmppclient.setServiceParent(application) self.anonymous = True self.xmppclient.startService()
def run(self, application, jidstr, password, muc, server): self.xmppclient = XMPPClient(JID(jidstr), password) if CONFIG.has_option('output_xmpp', 'debug') and \ CONFIG.getboolean('output_xmpp', 'debug') is True: self.xmppclient.logTraffic = True # DEBUG HERE (user, host, resource) = jid.parse(jidstr) self.muc = XMPPLoggerProtocol(muc, server, user + '-' + resource) self.muc.setHandlerParent(self.xmppclient) self.xmppclient.setServiceParent(application) self.anonymous = True self.xmppclient.startService()
def run(self, application, jidstr, password, muc, server): self.xmppclient = XMPPClient(JID(jidstr), password) if CONFIG.has_option('output_xmpp', 'debug') and \ CONFIG.getboolean('output_xmpp', 'debug') is True: self.xmppclient.logTraffic = True # DEBUG HERE (user, host, resource) = jid.parse(jidstr) self.muc = XMPPLoggerProtocol( muc, server, user + '-' + resource) self.muc.setHandlerParent(self.xmppclient) self.xmppclient.setServiceParent(application) self.anonymous = True self.xmppclient.startService()
def onMessage(self, msg): msg.pam = {} msg.pam['from'] = jid.parse(msg['from']) msg.pam['to'] = jid.parse(msg['to']) msg.pam['body'] = str(msg.body) #if msg["type"] == 'chat' and hasattr(msg, "body"): if hasattr(msg, "body"): if msg['from'] in self.current: current = self.current[msg['from']] data = self._listeners[current['owner']].onMessage(msg, current['data']) if data: self.current[msg['from']] = {'owner':name, 'data':data } else: del self.current[msg['from']] else : for (name,listener) in self._listeners.iteritems(): data = listener.onMessage(msg) if data: self.current[msg['from']] = {'owner':name, 'data':data } break
def dispatch(self, stanza): """ Dispatch a stanza to a JID all to all available resources found locally. @raise L{KeyError}: if a destination route is not found """ userid, unused, resource = jid.parse(stanza['to']) util.resetNamespace(stanza, component.NS_COMPONENT_ACCEPT) if resource is not None: self.streams[userid][resource].send(stanza) else: for resource, manager in self.streams[userid].iteritems(): manager.send(stanza)
def run(self, application, jidstr, password, muc, channels, anon=True): self.xmppclient = XMPPClient(jid.JID(jidstr), password) if self.cfg.has_option('database_xmpp', 'debug') and self.cfg.get('database_xmpp', 'debug') in ('1', 'true', 'yes'): self.xmppclient.logTraffic = True # DEBUG HERE self.xmppclient.setServiceParent(application) (user, host, resource) = jid.parse(jidstr) self.muc = XMPPLoggerProtocol(muc, channels.keys(), user + '-' + resource) self.muc.setHandlerParent(self.xmppclient) self.signals = {} for channel in channels: for signal in channels[channel]: self.signals[signal] = channel self.anonymous = True self.xmppclient.startService()
def run(self, application, jidstr, password, muc, channels, anon=True): self.xmppclient = XMPPClient(jid.internJID(jidstr), password) if self.cfg.has_option('database_xmpp', 'debug') and \ self.cfg.get('database_xmpp', 'debug') in ('1', 'true', 'yes'): self.xmppclient.logTraffic = True # DEBUG HERE (user, host, resource) = jid.parse(jidstr) self.muc = XMPPLoggerProtocol( muc, channels.keys(), user + '-' + resource) self.muc.setHandlerParent(self.xmppclient) self.xmppclient.setServiceParent(application) self.signals = {} for channel in channels: for signal in channels[channel]: self.signals[signal] = channel self.anonymous = True self.xmppclient.startService()
def on_presence(self, presence): self.log.debug('received presence: %s' % presence.toXml().encode('utf-8')) if presence.hasAttribute('from'): user, host, res = parse(presence['from']) if presence['from'] in self.active_controllers: if presence.hasAttribute('type'): if presence['type'] == 'unavailable': self.active_controllers.remove(presence['from']) self.log.info('User {_from} disconnected', _from=presence['from']) return elif 'ControlPoint' in res: if presence.hasAttribute('type'): if presence['type'] == 'unavailable': return self.log.info('control point %s added' % presence['from']) if len(self.active_controllers) == 0: self.check_ps_nodes() self.active_controllers.append(presence['from']) del (res) jid = '@'.join((user, host)) if presence.hasAttribute('type'): if presence['type'] == 'subscribe': if jid in self.users: self.log.info('received subscription from %s' % jid) if self.users[jid] is False: iq = IQ(self.xmlstream, 'set') query = domish.Element( ('jabber:iq:roster', 'query')) item = domish.Element((None, 'item')) item['jid'] = jid item['name'] = jid item.addElement('group', content='UPnPCloud') query.addChild(item) iq.addChild(query) iq.addCallback(self.subscribed, jid) iq.send() else: self.log.error('subscription for user %s failed: %s' % (jid, 'Not in user list')) pres = domish.Element((None, 'presence')) pres['type'] = 'unsubscribed' pres['to'] = presence['from'] self.xmlstream.send(pres)
def on_presence(self, resp): print('got presence: %s' % resp.toXml().encode('utf-8')) print('from :%s' % resp['from']) user, host, res = parse(resp['from']) jid = '@'.join((user, host)) if resp.hasAttribute('type'): if resp['type'] == 'subscribed': if jid in self.users: self.users[jid].update({'state': True}) if 'services' in self.users[jid]: self.users[jid]['services'].append(res) else: self.users[jid].update({'services': [res]}) presence = domish.Element((None, 'presence')) presence['type'] = 'subscribe' presence['to'] = resp['from'] self.xmlstream.send(presence) else: presence = domish.Element((None, 'presence')) presence['type'] = 'denying' presence['to'] = resp['from'] self.xmlstream.send(presence) elif resp['type'] == 'unsubscribed': if jid in self.users: print('subscription failed: %s' % resp['from']) return for child in resp.elements(): if child.name == 'ConfigIdCloud': print('Found UPnP Cloud device : %s type is: %s' % ( jid, res)) # if jid == self._jid: # return info = IQ(self.xmlstream, 'get') info['to'] = resp ['from'] query = domish.Element( ('urn:schemas-upnp-org:cloud-1-0', 'query')) query['type'] = 'description' query['name'] = ':'.join(res.split(':')[-2:]) info.addChild(query) info.addCallback(self.on_description) info.send()
def on_iq(self, iq): # print('received iq: %s' % iq.toXml().encode('utf-8')) user, host, res = parse(iq['from']) del (res) if not user: return jid = '@'.join((user, host)) self.log.debug('received request of type {typ} from {user}', typ=iq['type'], user=jid) if jid not in self.users and jid != self.user: self.log.info('rejected User: %s' % jid) return if iq['type'] == 'get': for child in iq.children: if child.name == 'query': if child['type'] == 'description': self.log.debug('description requested') self.dump_description(iq) elif iq['type'] == 'set': if iq.children[0].name == 'Envelope': self.log.debug('received rpc') root = iq.children[0] # print(root.toXml()) for child in root.children: if child.name == 'Header': res = self.services[ child.children[0]['serviceId']]['app'].handle_rpc( root.toXml(), child.children[0]['serviceId']) elif child.name == 'Body': decomposed = child.children[0].uri.split(':') guessed_id = ':'.join( (decomposed[0], decomposed[1], decomposed[2] + 'Id', decomposed[3])) res = self.services[str(guessed_id)]['app'].handle_rpc( root.toXml(), str(guessed_id)) else: self.log.warn('bad iq request: %s' % child.name) continue res.addCallback(self.respond_rpc, iq['from'], iq['id'])
def on_presence(self, resp): self.log.debug('got presence: %s' % resp.toXml().encode('utf-8')) # print('from :%s' % resp['from']) user, host, res = parse(resp['from']) jid = '@'.join((user, host)) if resp.hasAttribute('type'): if resp['type'] == 'subscribed': if jid in self.users: self.users[jid].update({'state': True}) if 'services' in self.users[jid]: self.users[jid]['services'].append(res) else: self.users[jid].update({'services': [res]}) presence = domish.Element((None, 'presence')) presence['type'] = 'subscribe' presence['to'] = resp['from'] self.xmlstream.send(presence) else: presence = domish.Element((None, 'presence')) presence['type'] = 'denying' presence['to'] = resp['from'] self.xmlstream.send(presence) elif resp['type'] == 'unsubscribed': if jid in self.users: self.log.warn('subscription failed: %s' % resp['from']) return for child in resp.elements(): if child.name == 'ConfigIdCloud': self.log.debug('Found UPnP Cloud device : %s type is: %s' % ( jid, res)) info = IQ(self.xmlstream, 'get') # info['to'] = resp['from'] query = domish.Element( ('urn:schemas-upnp-org:cloud-1-0', 'query')) query['type'] = 'description' query['name'] = ':'.join(res.split(':')[-2:]) info.addChild(query) info.addCallback(self.on_description, res) # info.send() info.send(to=resp['from'])
def testInvalid(self): # No host try: jid.parse("user@") assert 0 except jid.InvalidFormat: assert 1 # Double @@ try: jid.parse("user@@host") assert 0 except jid.InvalidFormat: assert 1 # Multiple @ try: jid.parse("user@host@host") assert 0 except jid.InvalidFormat: assert 1
def _jid_parse(jidstring, index): j = jid.parse(jidstring) return j[index]