cedbot.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. SleekXMPP: The Sleek XMPP Library
  5. Copyright (C) 2010 Nathanael C. Fritz
  6. This file is part of SleekXMPP.
  7. See the file LICENSE for copying permission.
  8. """
  9. import sys
  10. import logging
  11. import getpass
  12. from optparse import OptionParser
  13. import sleekxmpp
  14. # Python versions before 3.0 do not use UTF-8 encoding
  15. # by default. To ensure that Unicode is handled properly
  16. # throughout SleekXMPP, we will set the default encoding
  17. # ourselves to UTF-8.
  18. if sys.version_info < (3, 0):
  19. reload(sys)
  20. sys.setdefaultencoding('utf8')
  21. else:
  22. raw_input = input
  23. class EchoBot(sleekxmpp.ClientXMPP):
  24. """
  25. A simple SleekXMPP bot that will echo messages it
  26. receives, along with a short thank you message.
  27. """
  28. def __init__(self, jid, password):
  29. sleekxmpp.ClientXMPP.__init__(self, jid, password)
  30. # The session_start event will be triggered when
  31. # the bot establishes its connection with the server
  32. # and the XML streams are ready for use. We want to
  33. # listen for this event so that we we can initialize
  34. # our roster.
  35. self.add_event_handler("session_start", self.start)
  36. # The message event is triggered whenever a message
  37. # stanza is received. Be aware that that includes
  38. # MUC messages and error messages.
  39. self.add_event_handler("message", self.message)
  40. def start(self, event):
  41. """
  42. Process the session_start event.
  43. Typical actions for the session_start event are
  44. requesting the roster and broadcasting an initial
  45. presence stanza.
  46. Arguments:
  47. event -- An empty dictionary. The session_start
  48. event does not provide any additional
  49. data.
  50. """
  51. self.send_presence()
  52. self.get_roster()
  53. def message(self, msg):
  54. """
  55. Process incoming message stanzas. Be aware that this also
  56. includes MUC messages and error messages. It is usually
  57. a good idea to check the messages's type before processing
  58. or sending replies.
  59. Arguments:
  60. msg -- The received message stanza. See the documentation
  61. for stanza objects and the Message stanza to see
  62. how it may be used.
  63. """
  64. if msg['type'] in ('chat', 'normal'):
  65. msg.reply("Thanks for sending\n%(body)s" % msg).send()
  66. if __name__ == '__main__':
  67. # Setup the command line arguments.
  68. optp = OptionParser()
  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. # JID and password options.
  80. optp.add_option("-j", "--jid", dest="jid",
  81. help="JID to use")
  82. optp.add_option("-p", "--password", dest="password",
  83. help="password to use")
  84. opts, args = optp.parse_args()
  85. # Setup logging.
  86. logging.basicConfig(level=opts.loglevel,
  87. format='%(levelname)-8s %(message)s')
  88. if opts.jid is None:
  89. opts.jid = raw_input("Username: ")
  90. if opts.password is None:
  91. opts.password = getpass.getpass("Password: ")
  92. # Setup the EchoBot and register plugins. Note that while plugins may
  93. # have interdependencies, the order in which you register them does
  94. # not matter.
  95. xmpp = EchoBot(opts.jid, opts.password)
  96. xmpp.register_plugin('xep_0030') # Service Discovery
  97. xmpp.register_plugin('xep_0004') # Data Forms
  98. xmpp.register_plugin('xep_0060') # PubSub
  99. xmpp.register_plugin('xep_0199') # XMPP Ping
  100. # If you are working with an OpenFire server, you may need
  101. # to adjust the SSL version used:
  102. # xmpp.ssl_version = ssl.PROTOCOL_SSLv3
  103. # If you want to verify the SSL certificates offered by a server:
  104. # xmpp.ca_certs = "path/to/ca/cert"
  105. # Connect to the XMPP server and start processing XMPP stanzas.
  106. if xmpp.connect():
  107. # If you do not have the dnspython library installed, you will need
  108. # to manually specify the name of the server if it does not match
  109. # the one in the JID. For example, to use Google Talk you would
  110. # need to use:
  111. #
  112. # if xmpp.connect(('talk.google.com', 5222)):
  113. # ...
  114. xmpp.process(block=True)
  115. print("Done")
  116. else:
  117. print("Unable to connect.")