views:

248

answers:

2

How do I set a GChat or jabber status via python? Right now I've got this:

import xmpp     

new_status = "blah blah blah"
login = 'email' 
pwd   = 'password'

cnx = xmpp.Client('gmail.com')
cnx.connect( server=('talk.google.com',5223) )

cnx.auth(login, pwd, 'botty')

pres = xmpp.Presence()
pres.setStatus(new_status)
cnx.send(pres)

It executes, but the status is not updated. I know I'm connecting to the server successfully, as I can send chat messages to others. What am I doing wrong here?

+1  A: 

You might want to take a look at this file:

http://steliosm.net/projects/picaxejabber/picaxe_xmpp.py

Edit: My bad, first answer was out of context, I've misread your code.

cnx.sendInitPresence()

You haven't send your initial state I guess ...

Xavier
Aren't I already doing that? I have pres.setStatus(new_status)cnx.send(pres)which would appear to do the same thing as that code sample.
snostorm
My bad, I've misread the first time. See edit
Xavier
Hum... that doesn't seem to work either. Now I've got this: http://pastebin.com/hunGAJ84
snostorm
Now I figured it out. The code was right, it's just that it doesn't work if I'm already signed into that account from somewhere else. Thanks.
snostorm
Nice that you figured that out (: That's the kind of thing hard to debug, hehe
Xavier
A: 

NOTE: wanted to mention this to those who want to do what's mentioned in this thread. If one is not familiar with XMPP protocol and stanzas, one might miss some needed info to set proper status. The xmpppy module docs don't seem to explicitly clarify the steps to set presence.

Setting initial presence is easiest, as shown in previous posts in this thread. It sets a default presence (type) of user being available. Not sure what the default "status" and "show" states are, assume blank or "available" also.

However, when setting new status by defining a new presence object to send status, if you initialize the object with defaults (no arguments) as in the original post here, the presence object (or stanza) to be sent is incomplete because it doesn't define a proper presence "type". So depending on the XMPP server you are working with it may or may not take the setting correctly.

The proper way to initialize new presence status object would be like this:

offPres = xmpp.Presence(typ='unavailable',show='unavailable',status='unavailable')

or simply just the following, if toggling between "available/online" and "unavailable/offline" w/o logging on and off XMPP IM session, where we don't care what is shown for status/show state (i.e. the label you see associated with status, like "Offline - away" vs just "offline").

offPres = xmpp.Presence(typ='unavailable')

For custom status like DND, Away, Out to Lunch, etc., that gets a little trickier. I'm not really familiar with XMPP myself but assume you would specify the status and show state value as such (e.g. DND, Away) while setting presence type as "available" or "unavailable" depending on whether you want to appear that way or not.

And based on the xmpppy docs, you can only specify presence type at initialization of the object, can't change it afterwards. But you can change the status and show states for the presence object after initialization. That is done as shown in original post here. For show state, there is a matching setShow method just like setStatus.

Sending the presence is same as in original post.

David