XMPP vCard Python Script

3 June 2009 by Dan

Couldn't find a script to update my Jabber/XMPP vCard photo (a/k/a avatar), so I wrote one. It requires xmpppy (a/k/a python-xmpp). It should work with gTalk, but I have not tested it.

Credit to pastebin for some code snippets.

Hope this saves someone some time and effort.

#!/usr/bin/python
'''vcard.py - Update your XMPP vcard photo with the image you provide

Usage: vcard.py image_file jid password
'''

from xmpp import JID, Client, Iq, Presence, NS_VERSION, NS_VCARD
import sys
import os
import time
from base64 import encode, decode
from hashlib import sha1

try:
    file=os.path.expanduser(sys.argv[1])
    jid=sys.argv[2]
    password=sys.argv[3]
    resource='vcard'
except:
    print >>sys.stderr, __doc__
    sys.exit(2)

NS_VCARD_UPDATE = 'vcard-temp:x:update'
NS_NICK = 'http://jabber.org/protocol/nick'

def hash_img(img):
    return sha1(img).hexdigest()

def base64_img(img):
    return img.encode('base64')

def get_img(file):
    try:
        os.stat(file)[6]
        fh = open(file, 'rb')
        img = fh.read()
        return img
    except Exception, e:
        print >>sys.stderr, e
        sys.exit(2)

def get_mime_type(file):
    try:
        ext = file[-4:]
        if ext == '.png':
            mime_type = 'image/png'
        elif ext == '.gif':
            mime_type = 'image/gif'
        elif ext == '.jpg' or ext == '.jpeg':
            mime_type = 'image/jpeg'
        else:
            raise ValueError, "Wrong mime-type detected. Check file suffix."
    except ValueError, e:
        print >>sys.stderr, e
        sys.exit(2)
    return mime_type

def send_vcard(conn, base64_img, mime_type, nick):
    iq_vcard = Iq(typ='set')
    vcard = iq_vcard.addChild(name='vCard', namespace=NS_VCARD)
    vcard.addChild(name='NICKNAME', payload=[nick])
    photo = vcard.addChild(name='PHOTO')
    photo.setTagData(tag='TYPE', val=mime_type)
    photo.setTagData(tag='BINVAL', val=base64_img)
    conn.send(iq_vcard)

def send_presence(conn, status, hash1, nick):
    presence = Presence(status = status, show = 'xa', priority = '-1')
    presence.setTag(name='x',namespace=NS_VCARD_UPDATE).setTag(name='photo',namespace=NS_VCARD_UPDATE).setData(hash1)
    presence.setTag(name='nick',namespace=NS_NICK).setData(nick)
    conn.send(presence)

if __name__ == '__main__':
    img = get_img(file)
    j=JID(jid)
    cl=Client(j.getDomain(),debug=[])
    conn=cl.connect()
    if not conn:
        raise Exception, 'failed to start connection'
    auth=cl.auth(j.getNode(),password,resource,sasl=1)
    if not auth:
        raise Exception, 'could not authenticate'
    send_vcard(cl, base64_img(img), get_mime_type(file), j.getNode())
    send_presence(cl, 'Updated vCard Image', hash_img(img), j.getNode())
    time.sleep(1)
    cl.disconnect()

On Bootstrapping

13 April 2009 by Dan

On Friday, Dave Winer released a terrific thought-piece-of-a-podcast on how journalists need to learn about bootstraps. In his most recent podcast with NYU's Jay Rosen, he and Jay discussed the topic, as well, but I want to focus on bootstrapping, the metaphor.

bootstrapDave offered the well-worn phrase "haul yourself up by your bootstraps" as the mental image we should have when we use the bootstrapping metaphor. Imagine that you're wearing your boots, you grab your bootstraps and pull on them. Well, the best outcome I can imagine is that you'd fail to accomplish anything. If you could accomplish anything, I think all you'd do is pull your feet out from under yourself. But the phrase is supposed to connote (I think) strength by self-determination and self-motivation. That's why MBA-types say they're going to "bootstrap" their start-ups when what they really mean is that their start-ups will be self-funded at the outset.

But I recall learning that before bootstraps became merely decorative, they actually served a useful purpose: namely, to strap your boots to the top of heavy items so you could carry them. Maybe it's apocryphal, but here's MY mental image of bootstrapping: a person on a horse, laden with a pack on its rump, and a heavy wooden storage box strapped to each of the rider's boots.

And that more closely matches what bootstrapping means to me: taking advantage of what's already up-and-running and using that existing momentum to get something else moving.

BlackBerry Browser Bug with Mailto: Links

5 April 2009 by Dan

Mailto links give error dialog in browser when email address is missing .

Happy Birthday! Go to Hell! Many Thanks!

31 March 2009 by Dan

Happy Birthday! … Go to Hell! Many Thanks!

12 March 2009 by Dan

Earth 2?

7 March 2009 by Dan

Earth 2?

3 March 2009 by Dan

Sure, there are probably whittlers who could take a log, go after it with a knife for a couple of days and come back with a fair representation of a bobcat or Jefferson Davis. And yet, it's still whittling.
Music: What Happened? By Michael J. Nelson

Fantastic!

5 February 2009 by Dan

Fantastic!

Good Bank, Bad Bank; Good Plan, Better Plan

1 February 2009 by Dan

Good Bank, Bad Bank; Good Plan, Better Plan

This is a good spin on the "bad bank" idea. But what I don't understand is why we need a bad bank at all. I thought the whole point of a bad bank would be to enable new capital investment in banks by clearing out their balance sheets. Well, the banks already received the new capital investment -- TARP funds. Why do they need to get the bad assets off of their balance sheets? They've got their new capital. Now, just hang on to the bad assets and liquidate them over the next several years, possibly at a profit -- just like the bad bank would do! What am I missing?

Convenience Fail

29 January 2009 by Dan

Convenience Fail