SHARE
TWEET

Untitled

a guest Dec 29th, 2016 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5.     SleekXMPP: The Sleek XMPP Library
  6.     Copyright (C) 2010  Nathanael C. Fritz
  7.     This file is part of SleekXMPP.
  8.  
  9.     See the file LICENSE for copying permission.
  10. """
  11.  
  12. import sys
  13. import logging
  14. import getpass
  15. from optparse import OptionParser
  16.  
  17. import sleekxmpp
  18.  
  19. # Python versions before 3.0 do not use UTF-8 encoding
  20. # by default. To ensure that Unicode is handled properly
  21. # throughout SleekXMPP, we will set the default encoding
  22. # ourselves to UTF-8.
  23. if sys.version_info < (3, 0):
  24.     from sleekxmpp.util.misc_ops import setdefaultencoding
  25.     setdefaultencoding('utf8')
  26. else:
  27.     raw_input = input
  28.  
  29.  
  30. class SendMsgBot(sleekxmpp.ClientXMPP):
  31.  
  32.     """
  33.     A basic SleekXMPP bot that will log in, send a message,
  34.     and then log out.
  35.     """
  36.  
  37.     def __init__(self, jid, password, recipient, message):
  38.         sleekxmpp.ClientXMPP.__init__(self, jid, password)
  39.  
  40.         # The message we wish to send, and the JID that
  41.         # will receive it.
  42.         self.recipient = recipient
  43.         self.msg = message
  44.  
  45.         # The session_start event will be triggered when
  46.         # the bot establishes its connection with the server
  47.         # and the XML streams are ready for use. We want to
  48.         # listen for this event so that we we can initialize
  49.         # our roster.
  50.         self.add_event_handler("session_start", self.start, threaded=True)
  51.  
  52.     def start(self, event):
  53.         """
  54.         Process the session_start event.
  55.  
  56.         Typical actions for the session_start event are
  57.         requesting the roster and broadcasting an initial
  58.         presence stanza.
  59.  
  60.         Arguments:
  61.             event -- An empty dictionary. The session_start
  62.                      event does not provide any additional
  63.                      data.
  64.         """
  65.         self.send_presence()
  66.         self.get_roster()
  67.  
  68.         self.send_message(mto=self.recipient,
  69.                           mbody=self.msg,
  70.                           mtype='chat')
  71.  
  72.         # Using wait=True ensures that the send queue will be
  73.         # emptied before ending the session.
  74.         self.disconnect(wait=True)
  75.  
  76.  
  77. if __name__ == '__main__':
  78.     # Setup the command line arguments.
  79.     optp = OptionParser()
  80.  
  81.     # Output verbosity options.
  82.     optp.add_option('-q', '--quiet', help='set logging to ERROR',
  83.                     action='store_const', dest='loglevel',
  84.                     const=logging.ERROR, default=logging.INFO)
  85.     optp.add_option('-d', '--debug', help='set logging to DEBUG',
  86.                     action='store_const', dest='loglevel',
  87.                     const=logging.DEBUG, default=logging.INFO)
  88.     optp.add_option('-v', '--verbose', help='set logging to COMM',
  89.                     action='store_const', dest='loglevel',
  90.                     const=5, default=logging.INFO)
  91.  
  92.     # JID and password options.
  93.     optp.add_option("-j", "--jid", dest="jid",
  94.                     help="JID to use")
  95.     optp.add_option("-p", "--password", dest="password",
  96.                     help="password to use")
  97.     optp.add_option("-t", "--to", dest="to",
  98.                     help="JID to send the message to")
  99.     optp.add_option("-m", "--message", dest="message",
  100.                     help="message to send")
  101.  
  102.     opts, args = optp.parse_args()
  103.  
  104.     # Setup logging.
  105.     logging.basicConfig(level=opts.loglevel,
  106.                         format='%(levelname)-8s %(message)s')
  107.  
  108.     if opts.jid is None:
  109.         opts.jid = raw_input("Username: ")
  110.     if opts.password is None:
  111.         opts.password = getpass.getpass("Password: ")
  112.     if opts.to is None:
  113.         opts.to = raw_input("Send To: ")
  114.     if opts.message is None:
  115.         opts.message = raw_input("Message: ")
  116.  
  117.     # Setup the EchoBot and register plugins. Note that while plugins may
  118.     # have interdependencies, the order in which you register them does
  119.     # not matter.
  120.     xmpp = SendMsgBot(opts.jid, opts.password, opts.to, opts.message)
  121.     xmpp.register_plugin('xep_0030') # Service Discovery
  122.     xmpp.register_plugin('xep_0199') # XMPP Ping
  123.  
  124.     # If you are working with an OpenFire server, you may need
  125.     # to adjust the SSL version used:
  126.     # xmpp.ssl_version = ssl.PROTOCOL_SSLv3
  127.  
  128.     # If you want to verify the SSL certificates offered by a server:
  129.     # xmpp.ca_certs = "path/to/ca/cert"
  130.  
  131.     # Connect to the XMPP server and start processing XMPP stanzas.
  132.     if xmpp.connect():
  133.         # If you do not have the dnspython library installed, you will need
  134.         # to manually specify the name of the server if it does not match
  135.         # the one in the JID. For example, to use Google Talk you would
  136.         # need to use:
  137.         #
  138.         # if xmpp.connect(('talk.google.com', 5222)):
  139.         #     ...
  140.         xmpp.process(block=True)
  141.         print("Done")
  142.     else:
  143.         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