How to send multicast message (multiple users) using xmpp and Python xmpppy (XEP-0033: Extended Stanza Addressing)

I am using xmpppy http://xmpppy.sourceforge.net/ to send Jabber notifications, it is working well for single destinations using the following code:

# pip install https://github.com/rochacbruno/xmpppy/tarball/master

import xmpp

JABBER_SETTINGS = {"USERNAME": None, "PASSWORD": None, "DOMAIN": None, "RESOURCE": None}    

def get_jabber_client():
    client = xmpp.Client(JABBER_SETTINGS.get('DOMAIN'))
    client.connect(server=(JABBER_SETTINGS.get('DOMAIN'), '5222'))
    client.auth(
        JABBER_SETTINGS.get('USERNAME'),
        JABBER_SETTINGS.get('PASSWORD'), 
        JABBER_SETTINGS.get('RESOURCE')
    )
    client.sendInitPresence()
    return client

def send_message(to, message):
    client = get_jabber_client()
    xmpp_message = xmpp.Message(to, message)
    client.send(xmpp_message)
    client.disconnect()

send_message("[email protected]", "Hello World!")

But now I need to send the message to multiple destinations, for now I am doing.

for users in list_of_users:
    send_message(user, "Hello World!")

Which works fine, but every time I call it starts the process of authentication and takes a lot of time.

I've tried to create a single client and use the same client to send the message.

def send_message(to, message):
    if isinstance(to, basestring):
        to = [to]
    assert isinstance(to, (list, tuple))
    client = get_jabber_client()
    for destination in to:
        xmpp_message = xmpp.Message(destination, message)
        client.send(xmpp_message)
    client.disconnect()

send_message(['user1...', 'user2...'], "Hello World!")

The code above works, but only the first user in the list gets the message well formatted, the other users receives the message in pure XML.

I saw this code (in .net), mentioning XEP-0033: Extended Stanza Addressing http://forum.ag-software.net/thread/1482-Send-Message-To-all-users-in-contact-list

var addresses = new Addresses();
addresses.AddAddress(new Address
              {
                  Type = Type.to,
                  Jid = "[email protected]/Work",
                  Description = "Joe Hildebrand"
              });

addresses.AddAddress(new Address
        {
            Type = Type.cc,
            Jid = "[email protected]/Home",
            Description = "Jeremie Miller"
        });

var msg = new Matrix.Xmpp.Client.Message();

msg.Add(addresses);
msg.To = "multicast.jabber.org";
msg.Body = "Hello, world!";

builds the following Xml:

<message to='multicast.jabber.org'>
   <addresses xmlns='http://jabber.org/protocol/address'>
       <address type='to' jid='[email protected]/Work' desc='Joe Hildebrand'/>
       <address type='cc' jid='[email protected]/Home' desc='Jeremie Miller'/>
   </addresses>
   <body>Hello, world!</body>
</message>

But I did not found the way to do the same in Python using xmpppy, any idea on how to build the multicast stanza and send the message to multiple users using Python?

Thanks.

Upvotes: 0

Views: 1925

Answers (1)

legoscia
legoscia

Reputation: 41528

You could probably get this done without XEP-0033. In this loop, you overwrite the value of the variable message the first time, and subsequent messages will get garbled contents:

for destination in to:
    message = xmpp.Message(destination, message)
    client.send(message)

Try this instead:

for destination in to:
    xmpp_message = xmpp.Message(destination, message)
    client.send(xmpp_message)

Upvotes: 2

Related Questions