daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest May 27th, 2018 205 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python_browser
  2. #
  3. # Example usage: ./disco.py items sleek@conference.jabber.org
  4. #
  5.  
  6. import sys
  7. import time
  8. import logging
  9. import getpass
  10. import sleekxmpp
  11. from optparse import OptionParser
  12.  
  13.  
  14. class Disco(sleekxmpp.ClientXMPP):
  15.    
  16.     def __init__(self, jid, password, target, target_node='main', get=''):
  17.         sleekxmpp.ClientXMPP.__init__(self, jid, password)
  18.         self.add_event_handler("session_start", self.start)
  19.         self.add_event_handler("disco_info", self.disco_info)
  20.         self.add_event_handler("disco_items", self.disco_items)
  21.  
  22.         self.get = get
  23.         self.target = target
  24.         self.target_node = target_node
  25.         self.results = {'identities': None,
  26.                         'features': None,
  27.                         'items': None}
  28.  
  29.         # Values for self.get to control which disco entities are reported
  30.         self.info_types = ['', 'all', 'info', 'identities', 'features']
  31.         self.identity_types = ['', 'all', 'info', 'identities']
  32.         self.feature_types = ['', 'all', 'info', 'features']
  33.         self.items_types = ['', 'all', 'items']
  34.    
  35.     def start(self, event):
  36.         self.getRoster()
  37.         self.sendPresence()
  38.         if self.get in self.info_types:
  39.             self['xep_0030'].getInfo(self.target, node=self.target_node)
  40.         elif self.get in self.items_types:
  41.             self['xep_0030'].getItems(self.target, node=self.target_node)
  42.         else:
  43.             logging.error("Invalid disco request type.")
  44.             self.disconnect()
  45.        
  46.     def disco_info(self, iq):
  47.         self.results['identities'] = iq['disco_info'].getIdentities()
  48.         self.results['features'] = iq['disco_info'].getFeatures()
  49.         if self.get in self.items_types:
  50.             self['xep_0030'].getItems(self.target, node=self.target_node)
  51.         else:
  52.             self.print_results()
  53.  
  54.     def disco_items(self, iq):
  55.         self.results['items'] = iq['disco_items'].getItems()
  56.         self.print_results()
  57.  
  58.     def print_results(self):
  59.         header = 'XMPP Service Discovery: %s' % self.target
  60.         print header
  61.         print '-' * len(header)
  62.         if self.target_node != '':
  63.             print 'Node: %s' % self.target_node
  64.             print '-' * len(header)
  65.  
  66.         if self.get in self.identity_types:
  67.             print 'Identities:'
  68.             for identity in self.results['identities']:
  69.                 print '  - ', identity
  70.  
  71.         if self.get in self.feature_types:
  72.             print 'Features:'
  73.             for feature in self.results['features']:
  74.                 print '  - %s' % feature
  75.  
  76.         if self.get in self.items_types:
  77.             print 'Items:'
  78.             for item in self.results['items']:
  79.                 print '  - %s' % str(item)
  80.  
  81.         self.disconnect()
  82.  
  83. if __name__ == '__main__':
  84.     optp = OptionParser()
  85.     optp.usage = "Usage: %prog [options] info|items|identities|features <jid> [<node>]"
  86.     optp.version = '%%prog 0.1'
  87.  
  88.     optp.add_option('-q','--quiet', help='set logging to ERROR',
  89.                     action='store_const',
  90.                     dest='loglevel',
  91.                     const=logging.ERROR,
  92.                     default=logging.ERROR)
  93.     optp.add_option('-d','--debug', help='set logging to DEBUG',
  94.                     action='store_const',
  95.                     dest='loglevel',
  96.                     const=logging.DEBUG,
  97.                     default=logging.ERROR)
  98.     optp.add_option('-v','--verbose', help='set logging to COMM',
  99.                     action='store_const',
  100.                     dest='loglevel',
  101.                     const=5,
  102.                     default=logging.ERROR)
  103.     opts,args = optp.parse_args()
  104.    
  105.     logging.basicConfig(level=opts.loglevel, format='%(levelname)-8s %(message)s')
  106.            
  107.     if len(args) < 2:
  108.         optp.print_help()
  109.         exit()
  110.  
  111.     if len(args) == 2:
  112.         args = (args[0], args[1], '')
  113.  
  114.     username = raw_input("Username: ")
  115.     password = getpass.getpass("Password: ")
  116.  
  117.     xmpp = Disco(username, password, args[1], args[2], args[0])
  118.     xmpp.registerPlugin('xep_0030')
  119.     if xmpp.connect():
  120.         xmpp.process(threaded=False)
  121.     else:
  122.         print("Unable to connect.")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top