progrium (owner)

Forks

Revisions

gist: 133361 Download_button fork
public
Description:
Simple XMPP to Growl bridge
Public Clone URL: git://gist.github.com/133361.git
Embed All Files: show embed
xmpp-growl.tac #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from twisted.words.protocols.jabber import client, jid
from twisted.words.xish import domish, xmlstream
from twisted.application import internet, service
 
# Python bindings for Growl come with the SDK:
# http://growl.info/downloads_developers.php
from Growl import GrowlNotifier, Image
 
JID = 'test@example.com'
PASSWORD = 'secret'
HOST = 'talk.google.com'
 
def authenticated(stream):
    presence = domish.Element(('jabber:client','presence'))
    presence.addElement('status').addContent('Online')
    stream.send(presence)
    
    stream.addObserver('/message', lambda x: receivedMessage(stream, x))
    stream.addObserver('/presence', lambda x: receivedPresence(stream, x))
    #stream.addObserver('/*', lambda x: print(x.toXml()))
    
    roster_q = domish.Element(('jabber:client', 'iq'))
    roster_q['type'] = 'get'
    roster_q.addElement(('jabber:iq:roster', 'query'))
    stream.send(roster_q)
 
def receivedMessage(stream, x):
    for e in x.elements():
        if e.name == "body" and x.hasAttribute('type') and x['type'] == 'chat':
            g = GrowlNotifier(notifications=['xmpp'], applicationIcon=Image.imageWithIconForFile('/bin/sh'))
            g.register()
            message = str(e).split("\n")
            if len(message) > 1:
                g.notify('xmpp', message[0], "\n".join(message[1:]))
            else:
                g.notify('xmpp', '', "\n".join(message))
            
def receivedPresence(stream, x):
    if x.hasAttribute('type') and x['type'] == 'subscribe':
        presence = domish.Element(('jabber:client', 'presence'))
        presence['to'] = x['from']
        presence['type'] = 'subscribed'
        stream.send(presence)
 
application = service.Application('xmpp-growl')
f = client.XMPPClientFactory(jid.JID(JID), PASSWORD)
f.addBootstrap('//event/stream/authd', authenticated)
internet.TCPClient(HOST, 5222, f).setServiceParent(service.IServiceCollection(application))