def test_count_message(self, hipchat): msg = Message(hipchat.client, stype='normal', sfrom='123_homer@localhost/Oklahomer') msg['body'] = '.count ham' msg.reply = MagicMock() self.wait_future_finish(hipchat.message(msg)) assert_that(msg.reply.call_count).is_equal_to(1) assert_that(msg.reply.call_args).is_equal_to(call('1')) self.wait_future_finish(hipchat.message(msg)) assert_that(msg.reply.call_count).is_equal_to(2) assert_that(msg.reply.call_args).is_equal_to(call('2')) msg['body'] = '.count egg' self.wait_future_finish(hipchat.message(msg)) assert_that(msg.reply.call_count).is_equal_to(3) assert_that(msg.reply.call_args).is_equal_to(call('1')) stash = vars(sarah.bot.plugins.simple_counter) \ .get('__stash') \ .get('hipchat') assert_that(stash) \ .is_equal_to({'123_homer@localhost/Oklahomer': {'ham': 2, 'egg': 1}})
def group(self, msg: JabberMessage) -> None: if options.mode == 'personal' or msg["thread"] == '\u1FFFE': return subject = msg["subject"] if len(subject) == 0: subject = "(no topic)" stream = room_to_stream(msg['from'].local) sender_nick = msg.get_mucnick() if not sender_nick: # Messages from the room itself have no nickname. We should not try # to mirror these return jid = self.nickname_to_jid(msg.get_mucroom(), sender_nick) sender = jid_to_zulip(jid) zulip_message = dict( forged = "yes", sender = sender, type = "stream", subject = subject, to = stream, content = msg["body"], ) ret = self.zulipToJabber.client.send_message(zulip_message) if ret.get("result") != "success": logging.error(str(ret))
def test_skip_message(self, hipchat): msg = Message(hipchat.client, stype='normal') msg['body'] = 'test body' msg.reply = MagicMock() self.wait_future_finish(hipchat.message(msg)) assert_that(msg.reply.call_count).is_equal_to(0)
def test_echo_message(self, hipchat): msg = Message(hipchat.client, stype='normal') msg['body'] = '.echo spam' msg.reply = MagicMock() self.wait_future_finish(hipchat.message(msg)) assert_that(msg.reply.call_count).is_equal_to(1) assert_that(msg.reply.call_args).is_equal_to(call('spam'))
def testStanzaStr(self): """ Test that stanza objects are serialized properly. """ utf8_message = "\xe0\xb2\xa0_\xe0\xb2\xa0" if not hasattr(utf8_message, "decode"): # Python 3 utf8_message = bytes(utf8_message, encoding="utf-8") msg = Message() msg["body"] = utf8_message.decode("utf-8") expected = "<message><body>\xe0\xb2\xa0_\xe0\xb2\xa0</body></message>" result = msg.__str__() self.failUnless(result == expected, "Stanza Unicode handling is incorrect: %s" % result)
def test_echo_message(self, hipchat): msg = Message(hipchat.client, stype='normal') msg['body'] = '.echo spam' msg.reply = MagicMock() with patch.object(hipchat, 'respond', return_value="spam"): with patch.object(hipchat, 'enqueue_sending_message', return_value=Future()): hipchat.message(msg) assert_that(hipchat.respond.called).is_true() assert_that(hipchat.enqueue_sending_message.called).is_true()
def testStanzaStr(self): """ Test that stanza objects are serialized properly. """ utf8_message = '\xe0\xb2\xa0_\xe0\xb2\xa0' if not hasattr(utf8_message, 'decode'): # Python 3 utf8_message = bytes(utf8_message, encoding='utf-8') msg = Message() msg['body'] = utf8_message.decode('utf-8') expected = '<message><body>\xe0\xb2\xa0_\xe0\xb2\xa0</body></message>' result = msg.__str__() self.failUnless(result == expected, "Stanza Unicode handling is incorrect: %s" % result)
def send_connect_message(self, key, aliases): (local_address, remote_address)=(key[0], key[2]) packet=format_header(local_address, remote_address, ElementTree.Element("connect")) packet.attrib['xmlns']="hexchat:connect" packet=self.add_aliases(packet, aliases) logging.debug("%s:%d" % local_address + " sending connect request to %s:%d" % remote_address) message=Message() message['to']=key[1] message['type']='chat' message.append(packet) self.send(message, aliases)
def send_disconnect_error(self, key, from_aliases, to_alias, message=False): (local_address, remote_address)=(key[0], key[2]) packet=format_header(local_address, remote_address, ElementTree.Element("disconnect_error")) packet.attrib['xmlns']="hexchat:disconnect_error" packet=self.add_aliases(packet, from_aliases) logging.debug("%s:%d" % local_address + " sending disconnect_error request to %s:%d" % remote_address) if message: msg=Message() msg['type']='chat' else: msg=Iq() msg['type']='set' msg['to']=to_alias msg.append(packet) self.send(msg, from_aliases)
def test_skip_message(self, hipchat): msg = Message(hipchat.client, stype='normal') msg['body'] = 'test body' with patch.object(hipchat, 'respond', return_value=None): with patch.object(hipchat, 'enqueue_sending_message', return_value=Future()): hipchat.message(msg) assert_that(hipchat.enqueue_sending_message.called).is_false()
def xmpp(request, to): if request.method == 'POST': try: s = stanza_from_string(request.body) sfrom = JID(s.get_from().full) sto = JID(s.get_to().full) except: return HttpResponse('invalid stanza\n', content_type='text/plain') if isinstance(s, Presence): stype = s._get_attr('type') if stype == 'subscribe' or stype == 'unsubscribe': out = [] # ack request resp = Presence() resp.set_from(sto) resp.set_to(sfrom) resp.set_type(stype + 'd') out.append(resp) # send presence resp = Presence() sto.resource = 'chillpad' sto.regenerate() resp.set_from(sto) resp.set_to(sfrom) if stype == 'unsubscribe': resp.set_type('unavailable') out.append(resp) return stanzas_to_response(out) elif stype == 'probe': resp = Presence() sto.resource = 'chillpad' sto.regenerate() resp.set_from(sto) resp.set_to(sfrom) return stanzas_to_response([resp]) else: # ignore return HttpResponse() elif isinstance(s, Message): is_chat = (s._get_attr('type') == 'chat') resp = Message() resp.set_from(sto) resp.set_to(sfrom) if is_chat: resp.set_type('chat') resp['body'] = 'You sent: %s' % s['body'] return stanzas_to_response([resp]) else: # be a jerk and ignore IQs return HttpResponse() else: return HttpResponseNotAllowed(['POST'])
def Message(self, *args, **kwargs): """ Create a Message stanza. Uses same arguments as StanzaBase.__init__ Arguments: xml -- An XML object to use for the Message's values. """ return Message(self.xmpp, *args, **kwargs)
def test_skip_own_message(self, hipchat): msg = Message(hipchat.client, stype='groupchat') msg['body'] = 'test body' hipchat.respond = MagicMock() with patch.object(msg, "get_mucroom", return_value="abc"): with patch.dict(hipchat.client.plugin['xep_0045'].ourNicks, {'abc': "homer"}, clear=True): with patch.object(msg, "get_mucnick", return_value="homer"): hipchat.message(msg) assert_that(hipchat.respond.called).is_false()
def stanza_from_string(s): if not isinstance(s, unicode): s = s.decode('utf-8') e = ET.fromstring(s) if e.tag == '{jabber:client}message': stanza = Message(xml=e) elif e.tag == '{jabber:client}presence': stanza = Presence(xml=e) elif e.tag == '{jabber:client}iq': stanza = Iq(xml=e) else: raise ValueError('string is not valid stanza') return stanza
def Message(self, *args, **kwargs): """Create a Message stanza associated with this stream.""" return Message(self, *args, **kwargs)
def Message(self, *args, **kwargs): """Create a Message stanza associated with this stream.""" msg = Message(self, *args, **kwargs) msg['lang'] = self.default_lang return msg