def message(self, stanza): to = stanza.get_to() fr = stanza.get_from() typ = stanza.get_type() if typ not in (None, "chat"): typ = None sess = self.get_session(fr, to) if not to.node: if sess: m = Message(to_jid=fr, from_jid=to, body="Connected to: %s" % (sess.server, ), stanza_type=typ) else: m = Message(to_jid=fr, from_jid=to, body="Not connected", stanza_type=typ) return 1 if not to.resource and (to.node[0] in "#+!" or to.node.startswith(",amp,")): self.groupchat_message(stanza) if sess: sess.message_to_user(stanza) else: m = stanza.make_error_response("recipient-unavailable") self.send(m) return 1
def test_message_empty(self): m = Message() self.check_message_empty(m) node, doc = self.stanza_to_xml(m) self.check_message_empty(Message(node)) node, doc = self.xml_to_xml(doc) self.check_message_empty(Message(node))
def send_msg(stream, msg_to, msg_from, msg_subject, msg_body): m = Message(to_jid = msg_to, from_jid=msg_from, subject = msg_subject, body = msg_body) m.set_type('chat') stream.send(m)
def set_register(self, iq): to = iq.get_to() if to and to != self.jid: iq = iq.make_error_response("feature-not-implemented") self.stream.send(iq) return 1 remove = iq.xpath_eval("r:query/r:remove", {"r": "jabber:iq:register"}) if remove: m = Message(from_jid=iq.get_to(), to_jid=iq.get_from(), stanza_type="chat", body=u"Unregistered") self.stream.send(m) p = Presence(from_jid=iq.get_to(), to_jid=iq.get_from(), stanza_type="unsubscribe") self.stream.send(p) p = Presence(from_jid=iq.get_to(), to_jid=iq.get_from(), stanza_type="unsubscribed") self.stream.send(p) return 1 username = iq.xpath_eval("r:query/r:username", {"r": "jabber:iq:register"}) if username: username = username[0].getContent() else: username = u"" password = iq.xpath_eval("r:query/r:password", {"r": "jabber:iq:register"}) if password: password = password[0].getContent() else: password = u"" try: with open("regs/%s" % iq.get_from().bare(), "w") as f: f.write("%s\n%s" % (username, password)) except Exception as ex: m = Message(from_jid=iq.get_to(), to_jid=iq.get_from(), stanza_type="chat", body=u"Exception registering with username '%s': %s" % (username, ex)) self.stream.send(m) return 1 m = Message(from_jid=iq.get_to(), to_jid=iq.get_from(), stanza_type="chat", body=u"Registered with username '%s' and password '%s'" % (username, password)) self.stream.send(m) p = Presence(from_jid=iq.get_to(), to_jid=iq.get_from(), stanza_type="subscribe") self.stream.send(p) iq = iq.make_result_response() self.stream.send(iq) return 1
def pass_message_to_raw_channel(self, msg): fr = JID('#', self.network.jid.domain, None) m = Message(to_jid=self.jid, from_jid=fr, body=msg, stanza_type="groupchat") self.component.send(m)
def send_error_message(self,source,cond,text): text=remove_evil_characters(text) user=self.get_user(source) if user: self.unregister_user(user) if user and user.current_thread: typ,thread,fr=user.current_thread if not fr: fr=self.prefix_to_jid(source) m=Message(stanza_type="error",error_cond=cond,error_text=text, to_jid=self.jid,from_jid=fr,thread=thread) else: fr=self.prefix_to_jid(source) m=Message(stanza_type="error",error_cond=cond,error_text=text, to_jid=self.jid,from_jid=fr) self.component.send(m)
def irc_cmd_332(self, prefix, command, params): # RPL_TOPIC topic = remove_evil_characters(params[1]) m = Message(from_jid=self.room_jid.bare(), to_jid=self.session.jid, stanza_type="groupchat", subject=unicode(topic, self.encoding, "replace")) self.session.component.send(m)
def _send_message(self, message, auto=False, callback=None, **opts): assert isinstance(message, fmtstr) if self.jid_to not in self.buddy.resources: self.reset_chat_states() self.jid_to = self.buddy.jid # PyXMPP will escape the message for us... m = Message(stanza_type='chat', to_jid=self.jid_to, body=message.format_as('plaintext')) #message = unicode(message.encode('xml')) #assert isinstance(message, unicode) append_formatted_html(m, message) if pref('privacy.send_typing_notifications', False): ChatState('active').as_xml(m.xmlnode) X_Event(composing=True).as_xml(m.xmlnode) try: self.protocol.send_message(m) except Exception, e: callback.error(e)
def set_register(self, iq): to = iq.get_to() if to and to != self.jid: iq = iq.make_error_response("feature-not-implemented") self.stream.send(iq) return 1 remove = iq.xpath_eval("r:query/r:remove", {"r": "jabber:iq:register"}) if remove: m = Message(from_jid=iq.get_to(), to_jid=iq.get_from(), stanza_type="chat", body=u"Unregistered") self.stream.send(m) p = Presence(from_jid=iq.get_to(), to_jid=iq.get_from(), stanza_type="unsubscribe") self.stream.send(p) p = Presence(from_jid=iq.get_to(), to_jid=iq.get_from(), stanza_type="unsubscribed") self.stream.send(p) return 1 username = iq.xpath_eval("r:query/r:username", {"r": "jabber:iq:register"}) if username: username = username[0].getContent() else: username = u"" password = iq.xpath_eval("r:query/r:password", {"r": "jabber:iq:register"}) if password: password = password[0].getContent() else: password = u"" m = Message(from_jid=iq.get_to(), to_jid=iq.get_from(), stanza_type="chat", body=u"Registered with username '%s' and password '%s'" " (both ignored)" % (username, password)) self.stream.send(m) p = Presence(from_jid=iq.get_to(), to_jid=iq.get_from(), stanza_type="subscribe") self.stream.send(p) iq = iq.make_result_response() self.stream.send(iq) return 1
def send_notice_message(self, msg, not_in_muc=1): if not self.state or (self.muc and not_in_muc): return m = Message(from_jid=self.room_jid.bare(), to_jid=self.session.jid, stanza_type="groupchat", body=msg) self.session.component.send(m)
def irc_cmd_TOPIC(self, prefix, command, params): self.requests.get("TOPIC") topic = remove_evil_characters(params[1]) m = Message(from_jid=self.prefix_to_jid(prefix), to_jid=self.session.jid, stanza_type="groupchat", subject=unicode(topic, self.encoding, "replace")) self.session.component.send(m)
def test_message_full(self): m = 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") n = m.xmlnode.newChild(None, "payload", None) ns = n.newNs("http://pyxmpp.jajcus.net/xmlns/test", "t") n.setNs(ns) n.newChild(ns, "abc", None) self.check_message_full(m) node, doc = self.stanza_to_xml(m) self.check_message_full(Message(node)) xml = self.xml_to_xml(doc) self.check_message_full(Message(node))
def pass_output_to_raw_channel(self,s): body=`s` if body[0] in '"\'': body=body[1:-1] body=unicode(body,self.default_encoding,"replace") nick=unicode(self.nick,self.default_encoding,"replace") fr=JID('#',self.network.jid.domain,nick) m=Message(to_jid=self.jid,from_jid=fr,body=body,stanza_type="groupchat") self.component.send(m)
def exit(self): if self.chat_states_allowed and pref( 'privacy.send_typing_notifications', False): m = Message(to_jid=self.jid_to, stanza_type='chat') node = m.xmlnode ChatState('gone').as_xml(node) self.protocol.send_message(m) self.protocol.conversations.pop(self.id, None) Conversation.exit(self)
def irc_error_response(self, prefix, command, params, requests, condition): r = self.requests.get(requests) if r: m = r.stanza.make_error_response(condition) else: m = Message(from_jid=self.room_jid.bare(), to_jid=self.session.jid, stanza_type="error", error_cond=condition) self.session.component.send(m)
def send_typing_status(self, status): if not any((self.x_events_allowed, self.chat_states_allowed)): return m = Message(to_jid=self.jid_to, stanza_type='chat') node = m.xmlnode if self.x_events_allowed: X_Event(composing=(status == 'typing')).as_xml(node) if self.chat_states_allowed: ChatState(typing_chatstates.get(status, 'active')).as_xml(node) self.protocol.send_message(m)
def send_msg(self, rid, msg): self.rid = JID(rid) if not self.sid.resource: self.sid = JID(self.sid.node, self.sid.domain, 'send_message') msg = Message(to_jid=self.rid, body=msg) def send(stream): stream.send(msg) self.xmpp_do(send)
def make_xmpp_msg(self): ''' Creates a pyxmpp.message.Message and adds custom attributes ''' msg = Message(None, self.sender, self.recipient, None, None, None, \ self.body, self.thread) if self.router_command: msg.xmlnode.newProp('router_command', self.router_command) if self.router_class: msg.xmlnode.newProp('router_class', self.router_class) if self.xid: msg.xmlnode.newProp('osrf_xid', self.xid) return msg
def set_subject(self, subject): """ Send a subject change request to the room. :Parameters: - `subject`: the new subject. :Types: - `subject`: `unicode` """ m = Message(to_jid=self.room_jid.bare(), stanza_type="groupchat", subject=subject) self.manager.stream.send(m)
def irc_message(self, prefix, command, params): if not self.state or len(params) < 2: self.__logger.debug("ignoring it") return body = unicode(params[1], self.encoding, "replace") if body[0] == "\001" and body[-1] == "\001": self.CTCP(prefix, body[1:-1]) else: m = Message(stanza_type="groupchat", from_jid=self.prefix_to_jid(prefix), to_jid=self.session.jid, body=remove_evil_characters(strip_colors(body))) self.session.component.send(m)
def send_message(self, body): """ Send a message to the room. :Parameters: - `body`: the message body. :Types: - `body`: `unicode` """ m = Message(to_jid=self.room_jid.bare(), stanza_type="groupchat", body=body) self.manager.stream.send(m)
def CTCP(self, prefix, command): if " " in command: command, arg = command.split(" ", 1) else: arg = None if command == "ACTION": m = Message(stanza_type="groupchat", from_jid=self.prefix_to_jid(prefix), to_jid=self.session.jid, body="/me " + remove_evil_characters(strip_colors(arg))) self.session.component.send(m) else: self.__logger.debug("Unknown CTCP command: %r %r" % (command, arg))
def pass_input_to_raw_channel(self,prefix,command,params): body=string.join([command]+params) body=`body` if body[0] in '"\'': body=body[1:-1] body=unicode(body,self.default_encoding,"replace") if prefix: prefix=remove_evil_characters(prefix) prefix=`prefix` if prefix[0] in '"\'': prefix=prefix[1:-1] prefix=unicode(prefix,self.default_encoding,"replace") else: prefix=None fr=JID('#',self.network.jid.domain,prefix) m=Message(to_jid=self.jid,from_jid=fr,body=body,stanza_type="groupchat") self.component.send(m)
def send_message(my_jid, my_password, to_jid, body, subject=None, message_type=None, server=None, port=None): """Star an XMPP session and send a message, then exit. :Parameters: - `my_jid`: sender JID. - `my_password`: sender password. - `to_jid`: recipient JID. - `body`: message body. - `subject`: message subject. - `message_type`: message type. - `server`: server to connect to (default: derivied from `my_jid` using DNS records). - `port`: TCP port number to connect to (default: retrieved using SRV DNS record, or 5222). :Types: - `my_jid`: `pyxmpp.jid.JID` - `my_password`: `unicode` - `to_jid`: `pyxmpp.jid.JID` - `body`: `unicode` - `subject`: `unicode` - `message_type`: `str` - `server`: `unicode` or `str` - `port`: `int` """ from pyxmpp.message import Message msg = Message(to_jid=to_jid, body=body, subject=subject, stanza_type=message_type) def fun(stream): """Send a mesage `msg` via a stream.""" stream.send(msg) xmpp_do(my_jid, my_password, fun, server, port)
def invite(self, buddy, message=None, callback=None): ''' Sends an invite for this room to "buddy" (its actually a <message /> to the room--the room sends an invite "on your behalf"). <message from='[email protected]/desktop' to='*****@*****.**'> <x xmlns='http://jabber.org/protocol/muc#user'> <invite to='*****@*****.**'> <reason> Hey Hecate, this is the place for all good witches! </reason> </invite> </x> </message> ''' room = self try: buddy = buddy.jid.as_unicode() except: buddy = JID(buddy).as_unicode() if message is None: message = _('You have been invited to {roomname}').format( roomname=self.jid.as_unicode()) # Send an invitation "by way" of room m = Message(from_jid=room.protocol.self_buddy.jid, to_jid=room.jid) # <x tag> x = m.xmlnode.newTextChild(None, 'x', None) x.setNs(x.newNs('http://jabber.org/protocol/muc#user', None)) # <invite to="buddy to invite"><reason>Plz come chat</reason></invite> invite = x.newTextChild(None, 'invite', None) invite.setProp('to', buddy) reason = invite.newTextChild(None, 'reason', message) self.protocol.send_cb(m, callback=callback)
def irc_message(self,prefix,command,params): if len(params)<2 or not prefix: self.__logger.debug("ignoring it") return user=self.get_user(prefix) if not user: self.__logger.debug("could not convert %r to IRCUser object" % (prefix,)) return if user.current_thread: typ,thread,fr=user.current_thread else: typ="chat" thread=str(random.random()) fr=None user.current_thread=typ,thread,None do_invite = False if params[0][0]=='#': # This should be a PM but it was still aimed at a channel. # Invite the user and send the message from the channel. fr='%s@%s/%s' % (params[0], self.network.jid.domain, user.nick) do_invite=True if not fr: fr=user.jid() body=unicode(params[1],self.default_encoding,"replace") m=Message(stanza_type=typ,from_jid=fr,to_jid=self.jid,body=remove_evil_characters(strip_colors(body))) if do_invite: # <x tag> x = m.xmlnode.newTextChild(None, 'x', None) x.setNs(x.newNs('http://jabber.org/protocol/muc#user', None)) # <invite to="buddy to invite"><reason>Plz come chat</reason></invite> invite = x.newTextChild(None, 'invite', None) invite.setProp('to', self.jid) reason = invite.newTextChild(None, 'reason', '(You are not in the channel yet.)') self.component.send(m)
def irc_message(self, prefix, command, params): if len(params) < 2 or not prefix: self.__logger.debug("ignoring it") return user = self.get_user(prefix) if not user: self.__logger.debug("could not convert %r to IRCUser object" % (prefix, )) return if user.current_thread: typ, thread, fr = user.current_thread else: typ = "chat" thread = str(random.random()) fr = None user.current_thread = typ, thread, None if not fr: fr = user.jid() body = unicode(params[1], self.default_encoding, "replace") m = Message(stanza_type=typ, from_jid=fr, to_jid=self.jid, body=remove_evil_characters(strip_colors(body))) self.component.send(m)
def from_xml(xml): doc = libxml2.parseDoc(xml) msg = Message(doc.getRootElement()) return NetworkMessage(msg)
def test_message_empty_from_xml(self): m = Message(message2_node) self.check_message_empty(m)
def irc_cmd_331(self, prefix, command, params): # RPL_NOTOPIC m = Message(from_jid=self.room_jid.bare(), to_jid=self.session.jid, stanza_type="groupchat", subject=u"") self.session.component.send(m)
def test_message_full_from_xml(self): m = Message(message1_node) self.check_message_full(m)