Retrieve gtalk nickname in python xmpp

799 Views Asked by At

In python xmpp module, I'm able to retrieve the nickname of any contacts as follows:

self.connection.auth(userJid.getNode(), self.password)
self.roster = self.connection.getRoster()  
name = self.roster.getName(buddyJid)

..where buddyJid is of the form [email protected].
Now, I need to retrieve the nickname of the user who authenticates the connection (userJid). I cannot find the name using the above method. Which method can I use retrieve the name of the current user?

2

There are 2 best solutions below

0
On

This information is not in the roster. You will need to query the clients individually and get their vCard by sending this IQ :

<iq from='[email protected]/roundabout'
    id='v1'
    type='get'>
  <vCard xmlns='vcard-temp'/>
</iq>
0
On

Thank you nicholas_o, this is a sample function I put together based your suggestion. (The XML logic isn't ideal, but it was sufficient for the simple task I needed this for)

def vcard(disp, jid):
    msg = xmpp.protocol.Iq()
    msg.setType('get')
    msg.setTo(jid)
    qc = msg.addChild('vCard')
    qc.setAttr('xmlns', 'vcard-temp')
    rep = disp.SendAndWaitForResponse(msg)
    # to see what other fields are available in the XML output:
    # print rep
    userid=fname=lname=title=department=region=None
    for i in rep.getChildren():
        for j in i.getChildren():
            if j.getName() == "TITLE":
                title = j.getData().encode('utf-8')
            for k in j.getChildren():
                if k.getName() == "GIVEN":
                    fname = k.getData().encode('utf-8')
                if k.getName() == "FAMILY":
                    lname = k.getData().encode('utf-8')
                if k.getName() == "ORGUNIT":
                    department = k.getData().encode('utf-8')
                if k.getName() == "REGION":
                    region = k.getData().encode('utf-8')
    return fname, lname, title, department, region