Advertisement
Guest User

Untitled

a guest
May 6th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import logging
  5. import getpass
  6. from optparse import OptionParser
  7. import random
  8.  
  9.  
  10. try:
  11.     import json
  12. except ImportError:
  13.     import simplejson as json
  14.  
  15. try:
  16.     import requests
  17. except ImportError:
  18.     print('This demo requires the requests package for using HTTP.')
  19.     sys.exit()
  20.  
  21. from sleekxmpp import ClientXMPP
  22.  
  23.  
  24. class LocationBot(ClientXMPP):
  25.  
  26.     def __init__(self, jid, password):
  27.         super(LocationBot, self).__init__(jid, password)
  28.  
  29.         self.add_event_handler('session_start', self.start, threaded=True)
  30.         self.add_event_handler('user_location_publish',
  31.                                self.user_location_publish)
  32.  
  33.         self.register_plugin('xep_0004')
  34.         self.register_plugin('xep_0030')
  35.         self.register_plugin('xep_0060')
  36.         self.register_plugin('xep_0115')
  37.         self.register_plugin('xep_0128')
  38.         self.register_plugin('xep_0163')
  39.         self.register_plugin('xep_0080')
  40.  
  41.         self.current_tune = None
  42.  
  43.     def start(self, event):
  44.         self.send_presence()
  45.         self.get_roster()
  46.         self['xep_0115'].update_caps()
  47.  
  48.         self['xep_0080'].publish_location(
  49.                 lat=str(random.Random().randint(0, 50)),
  50.                 lon=str(random.Random().randint(0, 50)),
  51.                 locality=str(random.Random().randint(0, 50)),
  52.                 region=str(random.Random().randint(0, 50)),
  53.                 country=str(random.Random().randint(0, 50)),
  54.                 countrycode=str(random.Random().randint(0, 50)),
  55.                 postalcode=str(random.Random().randint(0, 50)))
  56.  
  57.     def user_location_publish(self, msg):
  58.         geo = msg['pubsub_event']['items']['item']['geoloc']
  59.         print("%s is at:" % msg['from'])
  60.         for key, val in geo.values.items():
  61.             if val:
  62.                 print("  %s: %s" % (key, val))
  63.  
  64.  
  65. if __name__ == '__main__':
  66.     # Setup the command line arguments.
  67.     optp = OptionParser()
  68.  
  69.     # Output verbosity options.
  70.     optp.add_option('-q', '--quiet', help='set logging to ERROR',
  71.                     action='store_const', dest='loglevel',
  72.                     const=logging.ERROR, default=logging.INFO)
  73.     optp.add_option('-d', '--debug', help='set logging to DEBUG',
  74.                     action='store_const', dest='loglevel',
  75.                     const=logging.DEBUG, default=logging.INFO)
  76.     optp.add_option('-v', '--verbose', help='set logging to COMM',
  77.                     action='store_const', dest='loglevel',
  78.                     const=5, default=logging.INFO)
  79.  
  80.     # JID and password options.
  81.     optp.add_option("-j", "--jid", dest="jid",
  82.                     help="JID to use")
  83.     optp.add_option("-p", "--password", dest="password",
  84.                     help="password to use")
  85.  
  86.     opts, args = optp.parse_args()
  87.  
  88.     # Setup logging.
  89.     logging.basicConfig(level=opts.loglevel,
  90.                         format='%(levelname)-8s %(message)s')
  91.  
  92.     if opts.jid is None:
  93.         opts.jid = raw_input("Username: ")
  94.     if opts.password is None:
  95.         opts.password = getpass.getpass("Password: ")
  96.  
  97.     xmpp = LocationBot(opts.jid, opts.password)
  98.  
  99.     # If you are working with an OpenFire server, you may need
  100.     # to adjust the SSL version used:
  101.     # xmpp.ssl_version = ssl.PROTOCOL_SSLv3
  102.  
  103.     # If you want to verify the SSL certificates offered by a server:
  104.     # xmpp.ca_certs = "path/to/ca/cert"
  105.  
  106.     # Connect to the XMPP server and start processing XMPP stanzas.
  107.     if xmpp.connect():
  108.         # If you do not have the dnspython library installed, you will need
  109.         # to manually specify the name of the server if it does not match
  110.         # the one in the JID. For example, to use Google Talk you would
  111.         # need to use:
  112.         #
  113.         # if xmpp.connect(('talk.google.com', 5222)):
  114.         #     ...
  115.         xmpp.process(block=True)
  116.         print("Done")
  117.     else:
  118.         print("Unable to connect.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement