BarbSchael
BarbSchael

Reputation: 169

Set/Get user information from a xmpp server: python

I am new in python and I am trying to create a testing python script to test different actions on my XMPP server. I already was able to test the login of my user and now I want to get the information that the server is sending (stanza) and set new information.

I have read several webs and I am not very clear with all this information. The main source has been sleekxmpp.com.

I have my stanza:

<iq type='get' to= 'chat.net'  id='id1'>
  <aa xmlns='http://myweb.com' />
</iq>

<iq type='result' to= 'chat.net'  id='id1'>
  <aa xmlns='http://myweb.com' >
    <name>My name as included in sent mails<name>
  <lang>en</lang>
    <mail>My mail as included in sent mails</mail>
  </aa>
</iq>

I want to get the information and also set one of the parameters (lets say name) but I don't know how.

class user_info(sleekxmpp.stanza.Iq):

    self.get_query()

I must do it in python. Any help appreciated

Upvotes: 1

Views: 1157

Answers (1)

user881288
user881288

Reputation:

What you want to do is create a custom stanza class for your stanza. Here's one that will work for the example you have:

from sleekxmpp import Iq
from sleekxmpp.xmlstream import ElementBase, register_stanza_plugin

class AA(ElementBase):
    name = 'aa'
    namespace = 'http://myweb.com'
    plugin_attrib = 'aa'
    interfaces = set(['name', 'lang', 'mail'])
    sub_interfaces = interfaces

register_stanza_plugin(Iq, AA)

Ok, so what does all of that do? The name field specifies that the XML object's root tag is 'aa', and namespace specifies the root tag's namespace; obvious so far I hope.

The plugin_attrib field is the name that can be used to access this stanza from the parent stanza. For example, you should already be familiar with how you can use iq['type'] or iq['from'] to extract data out of an Iq stanza. With plugin_attrib set to "aa", then you can use iq['aa'] to get a reference to the AA content.

The interfaces set is the set of key names that this stanza provides for extracting information, just like working with dictionaries. For example an Iq stanza has 'to', 'from', 'type', etc in its interfaces set. By default, accessing and modifying these keys will create or modify attributes of the stanza's main element. So, at this point, your stanza would behave like this:

aa = AA()
aa['name'] = 'foo'
print aa
"<aa xmlns='http://myweb.com' name='foo' />"

Now, to instead map interface keys to subelements instead of attributes, they need to be in the sub_interfaces set. So by setting sub_interfaces = interfaces the above example would now work like so:

aa = AA()
aa['name'] = 'foo'
print aa
"<aa xmlns='http://myweb.com'><name>foo</name></aa>"

If you needed something more advanced, you could also define methods of the form get_* / set_* / del_* where * is the interface name which will then be used to extract or modify data.

So, all together, you will be able to do:

iq = Iq()
# ... set iq parameters
iq.enable('aa')  # Add an initial, empty aa element.
try:
    resp = iq.send()
    print(resp['aa']['name'])
    # ..., etc
except XMPPError:
    print('There was an error')

Also, don't forget that we have the [email protected] chat room for SleekXMPP help if you need it.

Upvotes: 3

Related Questions