def on_invite(self, element): """ Handler that responds to channel invites from other users. This will acknowledge the request by joining the room indicated in the xml payload. :param element: A <message/> element, instance of `twisted.words.xish.domish.Element` """ channel = '' password = '' # NOTE: check for either http://xmpp.org/extensions/xep-0045.html#invite # or direct invites http://xmpp.org/extensions/xep-0249.html if xpath.matches('/message/x/invite', element): from_jid = jid.JID(element['from']) to_jid = jid.JID(element['to']) if from_jid.host == self.conference_host: channel = from_jid.userhost() else: channel = to_jid.userhost() # This is a hack around a unicode bug in twisted queryForString strings = xpath.queryForStringList('/message/x/password', element) if strings: password = strings[0] elif xpath.matches('/message/x[@xmlns="jabber:x:conference"]', element): # Direct invite x = xpath.queryForNodes('/message/x', element)[0] channel = x['jid'] password = x.attributes.get('password', '') else: # Probably not an invite, but the overly greedy xpath matched it. Ignore. return self.join(channel, password=password)
def testStaticMethods(self): self.assertEquals(xpath.matches("/foo/bar", self.e), True) self.assertEquals(xpath.queryForNodes("/foo/bar", self.e), [self.bar1, self.bar2, self.bar4]) self.assertEquals(xpath.queryForString("/foo", self.e), "somecontent") self.assertEquals(xpath.queryForStringList("/foo", self.e), ["somecontent", "somemorecontent"])
def test_oneToOneChat(self): """Converting a one to one chat to a multi-user chat. """ archive = [] thread = "e0ffe42b28561960c6b12b944a092794b9683a38" # create messages msg = domish.Element((None, 'message')) msg['to'] = '*****@*****.**' msg['type'] = 'chat' msg.addElement('body', None, 'test') msg.addElement('thread', None, thread) archive.append(msg) msg = domish.Element((None, 'message')) msg['to'] = '*****@*****.**' msg['type'] = 'chat' msg.addElement('body', None, 'yo') msg.addElement('thread', None, thread) archive.append(msg) self.protocol.history(self.room_jid, archive) while len(self.stub.output)>0: m = self.stub.output.pop() # check for delay element self.failUnless(m.name=='message', 'Wrong stanza') self.failUnless(xpath.matches("/message/delay", m), 'Invalid history stanza')
def test_kick(self): """Kick an entity from a room. """ kicked = JID('[email protected]/TroubleMakger') def cb(kicked): self.failUnless(kicked, 'Did not kick user') d = self.protocol.kick(self.room_jid, kicked, self.user_jid, reason='Spam') d.addCallback(cb) iq = self.stub.output[-1] self.failUnless( xpath.matches( "/iq[@type='set' and @to='%s']/query/item[@affiliation='none']" % (self.room_jid.userhost(), ), iq), 'Wrong kick stanza') response = toResponse(iq, 'result') self.stub.send(response) return d
def test_oneToOneChat(self): """Converting a one to one chat to a multi-user chat. """ archive = [] thread = "e0ffe42b28561960c6b12b944a092794b9683a38" # create messages msg = domish.Element((None, 'message')) msg['to'] = '*****@*****.**' msg['type'] = 'chat' msg.addElement('body', None, 'test') msg.addElement('thread', None, thread) archive.append(msg) msg = domish.Element((None, 'message')) msg['to'] = '*****@*****.**' msg['type'] = 'chat' msg.addElement('body', None, 'yo') msg.addElement('thread', None, thread) archive.append(msg) self.protocol.history(self.room_jid, archive) while len(self.stub.output) > 0: m = self.stub.output.pop() # check for delay element self.failUnless(m.name == 'message', 'Wrong stanza') self.failUnless(xpath.matches("/message/delay", m), 'Invalid history stanza')
def test_ban(self): """Ban an entity in a room. """ banned = JID('[email protected]/TroubleMakger') def cb(banned): self.failUnless(banned, 'Did not ban user') d = self.protocol.ban(self.room_jid, banned, self.user_jid, reason='Spam') d.addCallback(cb) iq = self.stub.output[-1] self.failUnless( xpath.matches( "/iq[@type='set' and @to='%s']/query/item[@affiliation='outcast']" % (self.room_jid.userhost(), ), iq), 'Wrong ban stanza') response = toResponse(iq, 'result') self.stub.send(response) return d
def test_voice(self): """ Client requesting voice for a room. """ self.protocol.voice(self.room_jid.userhost()) m = self.stub.output[-1] self.failUnless(xpath.matches("/message/x[@type='submit']/field/value[text()='%s']" % (muc.NS_MUC_REQUEST,), m), 'Invalid voice message stanza')
def test_password(self): """Sending a password via presence to a password protected room. """ self.protocol.password(self.room_jid, 'secret') prs = self.stub.output[-1] self.failUnless(xpath.matches("/presence[@to='%s']/x/password[text()='secret']" % (self.room_jid.full(),), prs), 'Wrong presence stanza')
def test_staticMethods(self): """ Test basic operation of the static methods. """ self.assertEquals(xpath.matches("/foo/bar", self.e), True) self.assertEquals( xpath.queryForNodes("/foo/bar", self.e), [self.bar1, self.bar2, self.bar4, self.bar5, self.bar6, self.bar7] ) self.assertEquals(xpath.queryForString("/foo", self.e), "somecontent") self.assertEquals(xpath.queryForStringList("/foo", self.e), ["somecontent", "somemorecontent"])
def test_invite(self): """Invite a user to a room """ other_jid = '*****@*****.**' self.protocol.invite(other_jid, 'This is a test') msg = self.stub.output[-1] self.failUnless(xpath.matches("/message[@to='%s']/x/invite/reason" % (other_jid,), msg), 'Wrong message type')
def test_privateMessage(self): """Send private messages to muc entities. """ other_nick = self.room_jid.userhost()+'/OtherNick' self.protocol.chat(other_nick, 'This is a test') msg = self.stub.output[-1] self.failUnless(xpath.matches("/message[@type='chat' and @to='%s']/body" % (other_nick,), msg), 'Wrong message type')
def test_voice(self): """ Client requesting voice for a room. """ self.protocol.voice(self.room_jid.userhost()) m = self.stub.output[-1] self.failUnless( xpath.matches( "/message/x[@type='submit']/field/value[text()='%s']" % (muc.NS_MUC_REQUEST, ), m), 'Invalid voice message stanza')
def test_staticMethods(self): """ Test basic operation of the static methods. """ self.assertEqual(xpath.matches("/foo/bar", self.e), True) self.assertEqual( xpath.queryForNodes("/foo/bar", self.e), [self.bar1, self.bar2, self.bar4, self.bar5, self.bar6, self.bar7]) self.assertEqual(xpath.queryForString("/foo", self.e), "somecontent") self.assertEqual(xpath.queryForStringList("/foo", self.e), ["somecontent", "somemorecontent"])
def test_invite(self): """Invite a user to a room """ other_jid = '*****@*****.**' self.protocol.invite(other_jid, 'This is a test') msg = self.stub.output[-1] self.failUnless( xpath.matches("/message[@to='%s']/x/invite/reason" % (other_jid, ), msg), 'Wrong message type')
def test_password(self): """Sending a password via presence to a password protected room. """ self.protocol.password(self.room_jid, 'secret') prs = self.stub.output[-1] self.failUnless( xpath.matches( "/presence[@to='%s']/x/password[text()='secret']" % (self.room_jid.full(), ), prs), 'Wrong presence stanza')
def test_privateMessage(self): """Send private messages to muc entities. """ other_nick = self.room_jid.userhost() + '/OtherNick' self.protocol.chat(other_nick, 'This is a test') msg = self.stub.output[-1] self.failUnless( xpath.matches( "/message[@type='chat' and @to='%s']/body" % (other_nick, ), msg), 'Wrong message type')
def parse_message(self, message): """ Parses the message body from a <message/> element, ignoring any delayed messages. If a message is indeed a delayed message, an empty string is returned :param message: A <message/> element, instance of `twisted.words.xish.domish.Element` :returns: The contents of the message, empty string if the message is delayed """ if xpath.matches('/message/delay', message): return u'' # This is a hack around a unicode bug in twisted queryForString strings = xpath.queryForStringList('/message/body', message) if strings: return strings[0] return u''
def test_roomDestroy(self): """ Destroy a room. """ def cb(destroyed): self.failUnless(destroyed==True, 'Room not destroyed.') d = self.protocol.destroy(self.room_jid) d.addCallback(cb) iq = self.stub.output[-1] self.failUnless(xpath.matches("/iq/query[@xmlns='%s']/destroy"% (muc.NS_MUC_OWNER,), iq), 'Bad configure request') response = toResponse(iq, 'result') self.stub.send(response) return d
def test_roomDestroy(self): """ Destroy a room. """ def cb(destroyed): self.failUnless(destroyed == True, 'Room not destroyed.') d = self.protocol.destroy(self.room_jid) d.addCallback(cb) iq = self.stub.output[-1] self.failUnless( xpath.matches( "/iq/query[@xmlns='%s']/destroy" % (muc.NS_MUC_OWNER, ), iq), 'Bad configure request') response = toResponse(iq, 'result') self.stub.send(response) return d
def test_register(self): """Client registering with a room. http://xmpp.org/extensions/xep-0045.html#register """ def cb(iq): # check for a result self.failUnless(iq['type']=='result', 'We did not get a result') d = self.protocol.register(self.room_jid) d.addCallback(cb) iq = self.stub.output[-1] self.failUnless(xpath.matches("/iq/query[@xmlns='%s']" % (muc.NS_REQUEST), iq), 'Invalid iq register request') response = toResponse(iq, 'result') self.stub.send(response) return d
def test_register(self): """Client registering with a room. http://xmpp.org/extensions/xep-0045.html#register """ def cb(iq): # check for a result self.failUnless(iq['type'] == 'result', 'We did not get a result') d = self.protocol.register(self.room_jid) d.addCallback(cb) iq = self.stub.output[-1] self.failUnless( xpath.matches("/iq/query[@xmlns='%s']" % (muc.NS_REQUEST), iq), 'Invalid iq register request') response = toResponse(iq, 'result') self.stub.send(response) return d
def test_kick(self): """Kick an entity from a room. """ kicked = JID('[email protected]/TroubleMakger') def cb(kicked): self.failUnless(kicked, 'Did not kick user') d = self.protocol.kick(self.room_jid, kicked, self.user_jid, reason='Spam') d.addCallback(cb) iq = self.stub.output[-1] self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='none']" % (self.room_jid.userhost(),), iq), 'Wrong kick stanza') response = toResponse(iq, 'result') self.stub.send(response) return d
def test_ban(self): """Ban an entity in a room. """ banned = JID('[email protected]/TroubleMakger') def cb(banned): self.failUnless(banned, 'Did not ban user') d = self.protocol.ban(self.room_jid, banned, self.user_jid, reason='Spam') d.addCallback(cb) iq = self.stub.output[-1] self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='outcast']" % (self.room_jid.userhost(),), iq), 'Wrong ban stanza') response = toResponse(iq, 'result') self.stub.send(response) return d
def test_grantVoice(self): """Test granting voice to a user. """ give_voice = JID('[email protected]/TroubleMakger') def cb(give_voice): self.failUnless(give_voice, 'Did not give voice user') d = self.protocol.grantVoice(self.user_jid, self.room_jid, give_voice) d.addCallback(cb) iq = self.stub.output[-1] self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@role='participant']" % (self.room_jid.userhost(),), iq), 'Wrong voice stanza') response = toResponse(iq, 'result') self.stub.send(response) return d
def onMessage(self, msg): """ Called when a message stanza was received. """ if xpath.matches("/message/x[@xmlns='jabber:x:delay']", msg) == 1: print "skip" return print "process" # if msg.x and msg.x.uri == "jabber:x:delay": # print "DELAYED "+str(msg.x) + msg.x.ur # if msg.x and msg.x.hasAttribute("xmlns") and msg.x['xmlns'] == "jabber:x:delay": # return if msg["type"] == 'groupchat' and hasattr(msg, "body"): print (msg.body) content = str(msg.body) if(content[:5].lower() == "call "): number = content[5:] print "GOT A NUMBER!! "+ number # if number != self.number: # self.number = number data = { 'Caller' : CALLER_ID, 'Called' : number, 'Url' : 'http://dmt.im/twilio/twilio.xml', # 'SendDigits' : '965376#1' } account = txilio.Account(ACCOUNT_SID, AUTH_TOKEN) d = account.request('Calls', 'POST', data) self.notify("CALLING "+number) def _done(res): print str(res) request.write(str(res)) request.finish() d.addBoth(_done) return NOT_DONE_YET
def test_roomConfigure(self): """ Default configure and changing the room name. """ def cb(iq): self.failUnless(iq['type']=='result', 'Not a result') fields = [] fields.append(data_form.Field(label='Natural-Language Room Name', var='muc#roomconfig_roomname', value=self.test_room)) d = self.protocol.configure(self.room_jid.userhost(), fields) d.addCallback(cb) iq = self.stub.output[-1] self.failUnless(xpath.matches("/iq/query[@xmlns='%s']/x"% (muc.NS_MUC_OWNER,), iq), 'Bad configure request') response = toResponse(iq, 'result') self.stub.send(response) return d
def test_roomConfigure(self): """ Default configure and changing the room name. """ def cb(iq): self.failUnless(iq['type'] == 'result', 'Not a result') fields = [] fields.append( data_form.Field(label='Natural-Language Room Name', var='muc#roomconfig_roomname', value=self.test_room)) d = self.protocol.configure(self.room_jid.userhost(), fields) d.addCallback(cb) iq = self.stub.output[-1] self.failUnless( xpath.matches("/iq/query[@xmlns='%s']/x" % (muc.NS_MUC_OWNER, ), iq), 'Bad configure request') response = toResponse(iq, 'result') self.stub.send(response) return d
def test_grantVoice(self): """Test granting voice to a user. """ give_voice = JID('[email protected]/TroubleMakger') def cb(give_voice): self.failUnless(give_voice, 'Did not give voice user') d = self.protocol.grantVoice(self.user_jid, self.room_jid, give_voice) d.addCallback(cb) iq = self.stub.output[-1] self.failUnless( xpath.matches( "/iq[@type='set' and @to='%s']/query/item[@role='participant']" % (self.room_jid.userhost(), ), iq), 'Wrong voice stanza') response = toResponse(iq, 'result') self.stub.send(response) return d