0

I'm currently developing a XMPP-based system that consists of an iOS app, an ejabberd server, and a standalone client that acts as a controller that can receive requests from users and take actions accordingly (I'm aware of IQ stanzas, but developing ejabberd modules in Erlang is way out of my league.)

For the iOS app I'm using XMPPFramework, and for the controller I've chosen Swiften, as it was the most recommended C++ library around here. Since this is more than a plain messaging system, I've found the need to include some custom attributes in the messages, like the following:

<message type="chat" 
         to="controller@example" 
         custom_attribute_1="Value 1"
         custom_attribute_2="Value 2"
    <subject>Subject</subject>
    <body>Body</body>
    <thread>Thread</thread>
</message>

This was very easy to do with XMPPFramework, but I've failed miserably at trying to read the custom attributes with Swiften, let alone generate a custom message.

I've tried two approaches. The first one was to get the Raw XML from the message and get the attributes using a boost XML Parser, but I haven't even been able to get the RawXMLPayload out of the message.

The second approach, which I thought would be more straightforward in the end, was to analyze the Swift/Swiften code to find out how they manage the whole XML-to-object conversion. I know they use the AttributeMap class, but I have no idea how those objects come to be, so no luck there either.

How can I accomplish this? Can it be done using Swiften?

Chris W.
  • 3
  • 2
  • **Never** put custom attributes in the top level of the message element. See also http://stackoverflow.com/a/26510821/194894 – Flow Oct 25 '14 at 23:31

1 Answers1

0

I would strongly recommend against adding additional attributes at the top level of the stanza (it is possible, but you have to namespace the elements and many implementations will not be happy with this - the example you gave is illegal XMPP, because the attributes are in the default namespace). The normal way to add additional information to a stanza is to add a (namespaced) payload, so something like:

<message type="chat" 
     to="controller@example">
    <subject>Subject</subject>
    <body>Body</body>
    <thread>Thread</thread>
    <chrispayload xmlns="...blah..." custom_attribute_1="Value 1" custom_attribute_2="Value 2"/>
</message>

To do this in Swiften you'd add a new Payload under Elements/ (copy paste any of the existing ones), create a new PayloadParser (see Parser/PayloadParsers/) and PayloadSerializer (see Serializer/PayloadSerializers), and then add each of these to their respective factories - see http://swift.im/swiften/guide/#Section-Extending .

If you really must add the attributes to the top level of the stanza (and please don't), you'd want to edit the Elements/Message class, and modify Parsers/MessageParser and Serializers/MessageSerializer.

Kev
  • 2,234
  • 1
  • 12
  • 6
  • As an additional note, you can use iq stanzas client to client, so there's nothing stopping you implementing your custom commands as iqs from the mobile devices to the controller - the server won't care and will just route them. – Kev Oct 26 '14 at 10:54
  • You're welcome. It's true that there's less documentation than I'd like, but folks are welcome to ask these sorts of questions in our MUC, and we try to be helpful. – Kev Oct 26 '14 at 22:14