Presence callback

The next addition to the script is a callback to handle <presence/> elements. The callback in this script takes the form of a subroutine called presenceCB() (“presence callback”). Callbacks, in relation to programming with Jabber, are explained in Section 8.3.4.

This is what the callback for handling <presence/> elements looks like:

def presenceCB(con, prs):

    type = prs.getType()
    parts = split(prs.getFrom(), '/')
    who = parts[0]

    if type == None: type = 'available'

    # Subscription request: 
    # - Accept their subscription
    # - Send request for subscription to their presence
    if type == 'subscribe':
        print "subscribe request from %s" % (who)
        con.send(jabber.Presence(to=who, type='subscribed'))
        con.send(jabber.Presence(to=who, type='subscribe'))
    
    # Unsubscription request: 
    # - Accept their unsubscription
    # - Send request for unsubscription to their presence
    elif type == 'unsubscribe':
        print "unsubscribe request from %s" % (who)
        con.send(jabber.Presence(to=who, type='unsubscribed'))
        con.send(jabber.Presence(to=who, type='unsubscribe'))
    
    elif type == 'subscribed':
        print "we are now subscribed to %s" % (who)

    elif type == 'unsubscribed':
        print "we are now unsubscribed to %s" % (who)

    elif type == 'available':
        print "%s is available (%s/%s)" % (who, prs.getShow(), prs.getStatus())
        if prs.getShow() != 'dnd' and who == cvsuser:
            con.send(jabber.Message(cvsuser, message, subject="CVS Watch Alarm"))

    elif type == 'unavailable':
        print "%s is unavailable" % (who)

Phew! Let’s take it a ...

Get Programming Jabber now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.