Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. import logging
  5. import getpass
  6. from optparse import OptionParser
  7. import time
  8.  
  9. import sleekxmpp
  10.  
  11. if sys.version_info < (3, 0):
  12. reload(sys)
  13. sys.setdefaultencoding('utf8')
  14. else:
  15. raw_input = input
  16.  
  17. class JabberDemoBot(sleekxmpp.ClientXMPP):
  18.  
  19. def __init__(self, jid, password):
  20. sleekxmpp.ClientXMPP.__init__(self, jid, password)
  21.  
  22. self.add_event_handler("session_start", self.start)
  23. self.add_event_handler("message", self.message)
  24.  
  25. def start(self, event):
  26. self.send_presence()
  27. #self.get_roster()
  28.  
  29. def message(self, msg):
  30. if msg['type'] in ('chat', 'normal'):
  31. if msg['body'] == 'test':
  32. self.send_presence(pstatus='On a call', ptype='away')
  33. time.sleep(5)
  34. self.send_presence()
  35. elif msg['body'] == 'bob':
  36. msg.reply("Found : Bob Benson\n%s" % '+44 1234 567890').send()
  37. elif msg['body'] == '7965':
  38. msg.reply("Found : \nCP-7965G=\nCisco UC Phone 7965, Gig Ethernet, Color\n$620\nType = SPARE").send()
  39. else:
  40. print("Message received: %s" % msg['body'])
  41.  
  42. if __name__ == '__main__':
  43. # Setup the command line arguments.
  44. optp = OptionParser()
  45.  
  46. # Output verbosity options.
  47. optp.add_option('-q', '--quiet', help='set logging to ERROR',
  48. action='store_const', dest='loglevel',
  49. const=logging.ERROR, default=logging.INFO)
  50. optp.add_option('-d', '--debug', help='set logging to DEBUG',
  51. action='store_const', dest='loglevel',
  52. const=logging.DEBUG, default=logging.INFO)
  53. optp.add_option('-v', '--verbose', help='set logging to COMM',
  54. action='store_const', dest='loglevel',
  55. const=5, default=logging.INFO)
  56.  
  57. # JID and password options.
  58. optp.add_option("-j", "--jid", dest="jid",
  59. help="JID to use")
  60. optp.add_option("-p", "--password", dest="password",
  61. help="password to use")
  62.  
  63. opts, args = optp.parse_args()
  64.  
  65. # Setup logging.
  66. logging.basicConfig(level=opts.loglevel,
  67. format='%(levelname)-8s %(message)s')
  68.  
  69. if opts.jid is None:
  70. opts.jid = raw_input("Username: ")
  71. if opts.password is None:
  72. opts.password = getpass.getpass("Password: ")
  73.  
  74. # Setup the JabberDemoBot and register plugins. Note that while plugins may
  75. # have interdependencies, the order in which you register them does
  76. # not matter.
  77. xmpp = JabberDemoBot(opts.jid, opts.password)
  78. xmpp.register_plugin('xep_0030') # Service Discovery
  79. xmpp.register_plugin('xep_0004') # Data Forms
  80. xmpp.register_plugin('xep_0060') # PubSub
  81. xmpp.register_plugin('xep_0199') # XMPP Ping
  82.  
  83. # Connect to the XMPP server and start processing XMPP stanzas.
  84. # if xmpp.connect():
  85. # Specify FQDN and Port if required as below:
  86. if xmpp.connect(('cup.yourdomain.com', 5222)):
  87. xmpp.process(block=False) #if we set this to false, it will let us run other code
  88. print("Done")
  89. else:
  90. print("Unable to connect.")
  91.  
  92. time.sleep(3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement