Skip to content

Commit

Permalink
new and shiny headstock 0.4
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.defuze.org/oss/headstock@979 5b3156b2-dc0e-0410-86c6-8c750cf32934
  • Loading branch information
sylvain committed Nov 7, 2009
1 parent 8d6e9a1 commit f527882
Show file tree
Hide file tree
Showing 103 changed files with 1,175 additions and 11,046 deletions.
25 changes: 0 additions & 25 deletions LICENSE

This file was deleted.

62 changes: 62 additions & 0 deletions example/basic.py
@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
import headstock
from headstock.lib.stanza import Stanza
from bridge import Element as E
from bridge import Attribute as A
from bridge.common import XMPP_CLIENT_NS, XMPP_ROSTER_NS

class Basic(object):
def ready(self, client):
self.client = client

def cleanup(self):
self.client = None

@headstock.xmpphandler('item', XMPP_ROSTER_NS)
def roster(self, e):
self.client.log("Contact '%s' %s with subscription: %s" % (e.get_attribute_value('name', ''),
e.get_attribute_value('jid', ''),
e.get_attribute_value('subscription', '')))

@headstock.xmpphandler('presence', XMPP_CLIENT_NS)
def presence(self, e):
self.client.log("Received '%s' presence from: %s" % (e.get_attribute_value('type', 'available'),
e.get_attribute_value('from')))

@headstock.xmpphandler('message', XMPP_CLIENT_NS)
def message(self, e):
who = e.get_attribute_value('from')
body = e.get_child('body', ns=e.xml_ns)
print "%s says: %s" % (who, body.xml_text)

# Echo the received message
m = E(u"message", attributes={u'from': unicode(self.client.jid), u'to': who, u'type': u'chat',
u'id': e.get_attribute_value('id')},
namespace=XMPP_CLIENT_NS)
E(u'body', content=body.xml_text, namespace=XMPP_CLIENT_NS, parent=m)

self.client.send_stanza(m)

if __name__ == '__main__':
from headstock.lib.utils import parse_commandline

options = parse_commandline()
if not options.password:
from getpass import getpass
options.password = getpass()
host, port = options.address.split(':')

registercls = None
if options.register:
from headstock.register import Register
registercls = Register

from headstock.client import AsyncClient
c = AsyncClient(unicode(options.jid), unicode(options.password),
hostname=host, port=int(port), tls=options.usetls,
register=registercls)
c.set_log(stdout=True)

c.register(Basic())

c.run()
78 changes: 78 additions & 0 deletions example/pubsub.py
@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-
import headstock
from headstock.client import AsyncClient
from headstock.lib.stanza import Stanza
from bridge import Element as E
from bridge import Attribute as A
from bridge.common import XMPP_CLIENT_NS, XMPP_ROSTER_NS,\
XMPP_PUBSUB_NS

from basic import Basic

class PubSub(object):
def ready(self, client):
self.client = client
self.fetch()
self.unsubscribe(u"http://www.reddit.com/r/WeAreTheMusicMakers/.rss")
self.subscribe(u"http://www.reddit.com/r/WeAreTheMusicMakers/.rss")

def fetch(self):
iq = Stanza.get_iq(self.client.jid, u"firehoser.superfeedr.com")

pubsub = E(u"pubsub", namespace=XMPP_PUBSUB_NS, parent=iq)
A(u"superfeedr", prefix=u"xmlns", parent=pubsub,
namespace=u"http://superfeedr.com/xmpp-pubsub-ext")
sub = E(u"subscriptions", attributes={u"jid": unicode(self.client.jid)},
parent=pubsub, namespace=XMPP_PUBSUB_NS)
A(u"page", value=u"1", prefix=u"superfeedr", parent=sub)

self.client.send_stanza(iq)

def subscribe(self, url):
iq = Stanza.set_iq(self.client.jid, u"firehoser.superfeedr.com")

pubsub = E(u"pubsub", namespace=XMPP_PUBSUB_NS, parent=iq)
A(u"superfeedr", prefix=u"xmlns", parent=pubsub,
namespace=u"http://superfeedr.com/xmpp-pubsub-ext")
E(u"subscribe", attributes={u"jid": u"", u"node": url},
parent=pubsub, namespace=XMPP_PUBSUB_NS)

self.client.send_stanza(iq)

def unsubscribe(self, url):
iq = Stanza.set_iq(self.client.jid, u"firehoser.superfeedr.com")

pubsub = E(u"pubsub", namespace=XMPP_PUBSUB_NS, parent=iq)
A(u"superfeedr", prefix=u"xmlns", parent=pubsub,
namespace=u"http://superfeedr.com/xmpp-pubsub-ext")
E(u"unsubscribe", attributes={u"jid": u"", u"node": url},
parent=pubsub, namespace=XMPP_PUBSUB_NS)

self.client.send_stanza(iq)

@headstock.xmpphandler('subscription', XMPP_PUBSUB_NS)
def subscription(self, e):
print e.xml()

@headstock.xmpphandler('message', XMPP_PUBSUB_NS)
def message(self, e):
print e.xml()


if __name__ == '__main__':
from headstock.lib.utils import parse_commandline

options = parse_commandline()
if not options.password:
from getpass import getpass
options.password = getpass()
host, port = options.address.split(':')

c = AsyncClient(unicode(options.jid), unicode(options.password),
hostname=host, port=int(port), tls=options.usetls)
c.set_log(stdout=True)

c.register(Basic())
c.register(PubSub())

c.run()
28 changes: 26 additions & 2 deletions headstock/__init__.py
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-

__version__ = "0.3.2"
__version__ = "0.4.0"
__authors__ = ["Sylvain Hellegouarch (sh@defuze.org)"]
__date__ = "2009/06/30"
__copyright__ = """
Copyright (c) 2006, 2007, 2008, 2009 Sylvain Hellegouarch
All rights reserved.
Expand Down Expand Up @@ -31,3 +30,28 @@
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
def xmpphandler(name, ns=None, once=False, forget=True):
"""
Decorator to wrap a callable so that it can be used as
a XMPP handler by the headstock dispatcher.
It will set various attributes to the wrapped callable:
* handler: set to True to indicate the callable can
participate to the dispatching
* xmpp_local_name: XML element name
* xmpp_ns: XML element namespace
* fire_once: if True, this handler will be removed
once it has been used.
* forget: if set to True, the dispatched element
will be automatically deleted once the handler
has been called.
"""
def wrapper(func):
func.handler = True
func.fire_once = once
func.forget = forget
func.xmpp_local_name = name
func.xmpp_ns = ns
return func
return wrapper
41 changes: 0 additions & 41 deletions headstock/api/__init__.py

This file was deleted.

65 changes: 0 additions & 65 deletions headstock/api/activity.py

This file was deleted.

0 comments on commit f527882

Please sign in to comment.