コード例 #1
0
ファイル: streambase.py プロジェクト: zeeying/pyxmpp2
 def test_stanza_send(self):
     handler = IgnoreEventHandler()
     route = RecordingRoute()
     self.stream = StreamBase(u"jabber:client", route, [])
     self.start_transport([handler])
     self.stream.initiate(self.transport)
     self.connect_transport()
     self.server.write(C2S_SERVER_STREAM_HEAD)
     self.wait_short(0.25)
     self.wait_short(0.25)
     self.assertTrue(self.stream.is_connected())
     self.stream.send(Message(to_jid=JID(u"*****@*****.**"),
                              body=u"Test"))
     xml = self.wait(expect=re.compile(b".*(<message.*</message>)"))
     self.assertIsNotNone(xml)
     if b"xmlns" not in xml:
         xml = xml.replace(b"<message", b"<message xmlns='jabber:client'")
     element = XML(xml)
     stanza = Message(element)
     self.assertEqual(stanza.body, u"Test")
     self.stream.disconnect()
     self.server.write(STREAM_TAIL)
     self.server.disconnect()
     self.wait()
     self.assertEqual(route.sent, [])
     self.assertEqual(route.received, [])
     event_classes = [e.__class__ for e in handler.events_received]
     self.assertEqual(event_classes, [
         ConnectingEvent, ConnectedEvent, StreamConnectedEvent,
         DisconnectedEvent
     ])
コード例 #2
0
ファイル: xmpp_receipt.py プロジェクト: Lucnsy/winterpy
    def handle_receipt_request(self, stanza):
        if not self.stream:
            return

        mid = stanza.stanza_id
        if mid:
            x = stanza.get_xml()
            if x.find('{%s}request' % NS) is None:
                # not requested
                return
            response = Message(to_jid = stanza.from_jid)
            payload = ET.Element("{%s}received" % NS, {'id': mid})
            response.set_payload(payload)
            self.stream.send(response)
        # keep handling it
        return False
コード例 #3
0
    def _send(self, tgt_jid, subject, body):
        """
        发送消息
        :param tgt_jid:目的jid
        :param subject: 主题
        :param body:  文本
        :return:
        """
        if not self.xmpp_client.stream:
            self.restart()
        self.wait_for_auth()

        tgt_jid = tgt_jid if isinstance(tgt_jid, JID) else JID(tgt_jid)
        msg = Message(to_jid=tgt_jid,
                      body=body,
                      subject=subject,
                      stanza_type="normal")

        # _send与send的区别,send的锁有点奇怪,有时候会卡主,暂时不用锁
        # def send(self, stanza):
        # """Write stanza to the stream.
        #
        # :Parameters:
        #     - `stanza`: XMPP stanza to send.
        # :Types:
        #     - `stanza`: `pyxmpp2.stanza.Stanza`
        # """
        # with self.lock:
        #     return self._send(stanza)
        self.xmpp_client.stream._send(msg)
        logger.info("XMPPClient::_send, src_jid:%s ,src_pass:%s, des_jid:%s, subject:%s, body:%s" %\
                    (self.JID,self.password, tgt_jid, subject, body))
コード例 #4
0
ファイル: xmpp_receipt.py プロジェクト: zouchao2010/winterpy
    def handle_receipt_request(self, stanza):
        if not self.stream:
            return

        mid = stanza.stanza_id
        if mid:
            x = stanza.get_xml()
            if x.find('{%s}request' % NS) is None:
                # not requested
                return
            response = Message(to_jid=stanza.from_jid)
            payload = ET.Element("{%s}received" % NS, {'id': mid})
            response.set_payload(payload)
            self.stream.send(response)
        # keep handling it
        return False
コード例 #5
0
def send_xmpp_message(from_jid, to_jid, txt):
    msg = Message(stanza_type='chat',
                  from_jid=JID(from_jid),
                  to_jid=JID(to_jid),
                  subject=None,
                  body=txt,
                  thread=None)
    bot.client.send(msg)
コード例 #6
0
 def send_message(self, receiver, msg):
     m = Message(
         stanza_type='chat',
         from_jid=self.client.jid,
         to_jid=receiver,
         body=msg,
     )
     self.send(m)
コード例 #7
0
ファイル: main.py プロジェクト: ghy200692162/group_talk_bot
 def send_message(self,receiver,msg):
     if isinstance(receiver,str):
         receiver=JID(receiver)
     message = Message(
                     stanza_type='chat',
                     from_jid = self.client.jid,
                     to_jid = receiver,
                     body = msg,)
     self.send(message)
コード例 #8
0
ファイル: sms.py プロジェクト: jhol/churchbot
 def run(self):
     while self.messages:
         if self.connected and not self.established:
             print("Connecting...")
             self.client.stream.send(
                 Message(to_jid=self.target_jid,
                         body="?",
                         stanza_type='chat'))
         self.client.run(timeout=2)
コード例 #9
0
ファイル: message.py プロジェクト: AdamPrzybyla/pyxmpp2
 def test_message_full(self):
     msg = Message(
             from_jid = JID("[email protected]/res"),
             to_jid = JID("*****@*****.**"),
             stanza_type = "normal",
             stanza_id = u"1",
             subject = u"Subject",
             body = u"The body",
             thread = u"thread-id")
     payload = ElementTree.Element(
                         "{http://pyxmpp.jajcus.net/xmlns/test}payload")
     ElementTree.SubElement(payload, 
                             "{http://pyxmpp.jajcus.net/xmlns/test}abc")
     payload = XMLPayload(payload)
     msg.add_payload(payload)
     self.check_message_full(msg)
     xml = msg.as_xml()
     self.check_message_full(Message(xml))
コード例 #10
0
ファイル: adapt.py プロジェクト: kgodlewski/xmpp-to-tlen
def outgoing_message(stanza):
	"""
	Adapt an outgoint Message stanza. Either properly encode the
	message contents, or convert it to a typing notification,
	if no body is included.
	"""

	logger.debug('outgoing message, body=%r', stanza.body)

	if stanza.body:
		body = tlen_encode(stanza.body)
		# This strips all additional XML tags, like html part etc.
		stanza = Message(stanza_type = stanza.stanza_type,
				from_jid = stanza.from_jid, to_jid = stanza.to_jid,
				subject = stanza.subject, body = body,
				thread = stanza.thread)
		return stanza

	# If the message had no body, assume it's a typing notification.

	xml = stanza.as_xml()

	for chatstate in xml:
		if chatstate.tag.startswith(const.CHATSTATES_NS_QNP):
			break
	# If no chatstate found, give up and send unmodified stanza
	else:
		return stanza

	logger.debug('chatstate=%s', chatstate)

	# Need to add a namespace to keep Stanza happy
	# For XML format, see docstring for incoming_chatstate()
	m = ElementTree.Element('{jabber:client}m')
	m.set('to', stanza.to_jid.as_unicode())

	if chatstate.tag.endswith('composing'):
		m.set('tp', 't')
	else:
		m.set('tp', 'u')

	return Stanza(m)
コード例 #11
0
ファイル: stanzaprocessor.py プロジェクト: zeeying/pyxmpp2
 def echo_message(self, stanza):
     self.handlers_called.append("echo_message")
     self.assertIsInstance(stanza, Message)
     self.assertNotEqual(stanza.stanza_type, "error")
     message = Message(to_jid=stanza.from_jid,
                       from_jid=stanza.to_jid,
                       stanza_type=stanza.stanza_type,
                       thread=stanza.thread,
                       subject=stanza.subject,
                       body=stanza.body)
     return message
コード例 #12
0
ファイル: sms.py プロジェクト: jhol/churchbot
    def handle_message(self, stanza):
        if not self.established:
            if '"help"' not in stanza.body:
                return
            self.established = True

        if not self.messages:
            self.client.disconnect()
            return None

        body = 'sms:%s:%s' % self.messages.pop()
        print(body)
        return Message(to_jid=self.target_jid, body=body, stanza_type='chat')
コード例 #13
0
 def make_message(self, to, typ, body):
     """ 构造消息
         `to` - 接收人 JID
         `typ` - 消息类型
         `body` - 消息主体
     """
     if typ not in ['normal', 'chat', 'groupchat', 'headline']:
         typ = 'chat'
     m = Message(from_jid=self.bot_jid,
                 to_jid=to,
                 stanza_type=typ,
                 body=body)
     return m
コード例 #14
0
    def send_xmpp_message(self, receivers, req_id, message):
        """
        Formats and sends a XMPP notification with the current domain and review request
        being added to the template context. Returns the resulting message ID.
        """
        logging.info("XMPP notification send message for request #%s: %s",
                     req_id, message)

        host = self.extension.settings['xmpp_host']
        port = self.extension.settings['xmpp_port']
        timeout = self.extension.settings['xmpp_timeout']
        from_jid = self.extension.settings["xmpp_sender_jid"]
        password = self.extension.settings["xmpp_sender_password"]
        use_tls = self.extension.settings["xmpp_use_tls"]
        tls_verify_peer = self.extension.settings["xmpp_tls_verify_peer"]

        if sys.version_info[0] < 3:
            from_jid = from_jid.decode("utf-8")
            password = password.decode("utf-8")
            message = message.decode("utf-8")

        if self.extension.settings["xmpp_partychat_only"]:
            receivers = set()

        rooms = self.extension.settings["xmpp_partychat"].split()
        if sys.version_info[0] < 3:
            rooms = [room.decode("utf-8") for room in rooms]
        receivers.update(rooms)

        try:
            from_jid = JID(from_jid)
            stanzas = set()
            for receiver in receivers:
                if "@" in str(receiver):
                    receiver_jid = JID(local_or_jid=receiver)
                else:
                    receiver_jid = JID(local_or_jid=receiver,
                                       domain=from_jid.domain)
                stanzas.add(
                    Message(to_jid=receiver_jid,
                            body=message,
                            stanza_type="chat"))
            client = XmppClient(host, port, timeout, from_jid, password,
                                use_tls, tls_verify_peer)
            client.send(req_id, stanzas)
        except Exception, e:
            logging.error(
                "Error sending XMPP notification for request #%s: %s",
                req_id,
                e,
                exc_info=1)
コード例 #15
0
ファイル: XMPPEchoBot.py プロジェクト: Machi427/python
    def handle_message(self, stanza):
        # (4-1)メッセージのタイトルを設定
        if stanza.subject:
            subject = "Re: " + stanza.subject
        else:
            subject = None

        if stanza.body:
            # (4-2)メッセージの本文の作成
            body = 'You said "' + stanza.body + '".'
            # (5) メッセージオブジェクトの作成
            msg = Message(stanza_type=stanza.stanza_type,
                          from_jid=stanza.to_jid,
                          to_jid=stanza.from_jid,
                          subject=subject,
                          body=body,
                          thread=stanza.thread)
            return msg
コード例 #16
0
ファイル: message.py プロジェクト: zeeying/pyxmpp2
 def test_message_full(self):
     msg = Message(
             from_jid = JID("[email protected]/res"),
             to_jid = JID("*****@*****.**"),
             stanza_type = "normal",
             stanza_id = u"1",
             subject = u"Subject",
             body = u"The body",
             thread = u"thread-id")
     payload = ElementTree.Element(
                         "{http://pyxmpp.jajcus.net/xmlns/test}payload")
     ElementTree.SubElement(payload,
                             "{http://pyxmpp.jajcus.net/xmlns/test}abc")
     payload = XMLPayload(payload)
     msg.add_payload(payload)
     self.check_message_full(msg)
     xml = msg.as_xml()
     self.check_message_full(Message(xml))
コード例 #17
0
ファイル: message.py プロジェクト: zeeying/pyxmpp2
 def test_message_full_from_xml(self):
     msg = Message(ElementTree.XML(MESSAGE1))
     self.check_message_full(msg)
コード例 #18
0
 def handle_authorized(self, event):
     message = Message(to_jid=self.target_jid, body=self.message)
     event.stream.send(message)
     event.stream.disconnect()
コード例 #19
0
ファイル: message.py プロジェクト: zeeying/pyxmpp2
 def test_message_empty_from_xml(self):
     msg = Message(ElementTree.XML(MESSAGE2))
     self.check_message_empty(msg)
コード例 #20
0
ファイル: roster.py プロジェクト: disda/deepin-talk
 def send_message(self, jid, message, message_type="chat", thread=None):
     message = Message(to_jid=JID(jid),
                       body=message,
                       stanza_type=message_type,
                       thread=thread)
     self.send(message)
コード例 #21
0
ファイル: message.py プロジェクト: zeeying/pyxmpp2
 def test_message_empty(self):
     msg = Message()
     self.check_message_empty(msg)
     xml = msg.as_xml()
     self.check_message_empty(Message(xml))
コード例 #22
0
ファイル: message.py プロジェクト: AdamPrzybyla/pyxmpp2
 def test_message_empty(self):
     msg = Message()
     self.check_message_empty(msg)
     xml = msg.as_xml()
     self.check_message_empty(Message(xml))