Last active
December 18, 2015 01:29
-
-
Save oskaritimperi/5704155 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # usage: | |
| # | |
| # $ virtualenv venv | |
| # $ ./venv/bin/pip install sleekxmpp dnspython | |
| # $ ./venv/bin/python peptest.py -j foo@bar.com -p secretpassword --subscribe other@bar.com/other -d | |
| import sys | |
| import os | |
| import ssl | |
| import logging | |
| import random | |
| from argparse import ArgumentParser | |
| from datetime import datetime | |
| from sleekxmpp import ClientXMPP | |
| from sleekxmpp import Iq | |
| from sleekxmpp import JID | |
| from sleekxmpp.plugins.xep_0030 import DiscoInfo | |
| from sleekxmpp.xmlstream import ET, register_stanza_plugin, ElementBase | |
| import sleekxmpp.plugins.xep_0060 | |
| log = logging.getLogger(__name__) | |
| class Geoloc(ElementBase): | |
| namespace = 'http://novatron.fi/protocol/trak/geoloc' | |
| name = 'geoloc' | |
| plugin_attrib = 'geoloc' | |
| interfaces = set(('lat', 'lon', 'alt', 'timestamp')) | |
| sub_interfaces = interfaces | |
| class Status(ElementBase): | |
| namespace = 'http://novatron.fi/protocol/trak/status' | |
| name = 'status' | |
| plugin_attrib = 'status' | |
| interfaces = set(('project', 'file', 'activity')) | |
| sub_interfaces = interfaces | |
| class PEPClient(ClientXMPP): | |
| def __init__(self, jid, password): | |
| ClientXMPP.__init__(self, jid, password) | |
| self.subscribe = None | |
| self.ssl_version = ssl.PROTOCOL_SSLv3 | |
| # Service Discovery | |
| self.register_plugin('xep_0030') | |
| # Data Forms | |
| self.register_plugin('xep_0004') | |
| # PubSub | |
| self.register_plugin('xep_0060') | |
| # PEP | |
| self.register_plugin('xep_0163') | |
| # XMPP Ping | |
| self.register_plugin('xep_0199') | |
| self.add_event_handler('session_start', | |
| self.start) | |
| def start(self, event): | |
| log.info('session started') | |
| # HOX! Register the pep stuff after we've established a session | |
| # and have a fully bound JID. It seems that if we do this | |
| # in __init__() for example, we are registering the features | |
| # on a wrong JID (foo@domain.tld vs. foo@domain.tld/bar) ... | |
| self['xep_0163'].register_pep('pep_geoloc', Geoloc) | |
| self['xep_0163'].register_pep('pep_status', Status) | |
| self.add_event_handler('pep_geoloc_publish', | |
| self._handle_geoloc_publish) | |
| self.add_event_handler('pep_status_publish', | |
| self._handle_status_publish) | |
| #info = self['xep_0030'].get_info(jid=self.boundjid, | |
| # local=True) | |
| #print info | |
| self.send_presence() | |
| self.get_roster() | |
| self['xep_0115'].update_caps() | |
| if self.subscribe is not None: | |
| log.info('subscribing to <%s>', self.subscribe) | |
| self.send_presence_subscription(pto=self.subscribe) | |
| def _handle_geoloc_publish(self, msg): | |
| g = msg['pubsub_event']['items']['item']['geoloc'] | |
| log.info('got location from <%s>: (%s, %s)', msg['from'], g['lat'], g['lon']) | |
| def _handle_status_publish(self, msg): | |
| s = msg['pubsub_event']['items']['item']['status'] | |
| log.info('got status from <%s>: %s/%s/%s', msg['from'], s['activity'], s['project'], s['file']) | |
| if __name__ == '__main__': | |
| parser = ArgumentParser() | |
| parser.add_argument('-j', '--jid', required=True, | |
| help='Full Jabber ID the client should use to register with TRAK Core', | |
| dest='jid') | |
| parser.add_argument('-p', '--password', required=True, | |
| dest='password') | |
| parser.add_argument('-d', '--debug', | |
| help='Print sent and received XML stanzas', | |
| const=logging.DEBUG, | |
| action='store_const', | |
| dest='loglevel' | |
| ) | |
| parser.add_argument('--host', | |
| help='Server host if different from the one in the JID', | |
| dest='host') | |
| parser.add_argument('--port', | |
| help='Server port', | |
| dest='port', | |
| type=int) | |
| parser.add_argument('--subscribe', | |
| help='JID to subscribe to', | |
| dest='subscribe') | |
| parser.set_defaults( | |
| loglevel=logging.INFO, | |
| host=None, | |
| port=5222, | |
| subscribe=None) | |
| args = parser.parse_args() | |
| logging.basicConfig(level=args.loglevel, | |
| format='%(asctime)s %(levelname)-8s %(message)s') | |
| if args.host is None: | |
| address = None | |
| else: | |
| address = (args.host, args.port) | |
| xmpp = PEPClient(args.jid, args.password) | |
| if args.subscribe: | |
| xmpp.subscribe = JID(args.subscribe) | |
| if xmpp.connect(address=address): | |
| xmpp.process(block=True) | |
| log.info('done') | |
| else: | |
| log.error("couldn't connect") | |
| sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment