Clean a new bunch of stuf
[slixmpp.git] / examples / echo_component.py
bloba20f8c1595758ad7a2dbf42954e73ac17c344906
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 """
5 Slixmpp: The Slick XMPP Library
6 Copyright (C) 2010 Nathanael C. Fritz
7 This file is part of Slixmpp.
9 See the file LICENSE for copying permission.
10 """
12 import sys
13 import logging
14 import getpass
15 from optparse import OptionParser
17 import slixmpp
18 from slixmpp.componentxmpp import ComponentXMPP
20 # Python versions before 3.0 do not use UTF-8 encoding
21 # by default. To ensure that Unicode is handled properly
22 # throughout Slixmpp, we will set the default encoding
23 # ourselves to UTF-8.
24 if sys.version_info < (3, 0):
25 from slixmpp.util.misc_ops import setdefaultencoding
26 setdefaultencoding('utf8')
27 else:
28 raw_input = input
31 class EchoComponent(ComponentXMPP):
33 """
34 A simple Slixmpp component that echoes messages.
35 """
37 def __init__(self, jid, secret, server, port):
38 ComponentXMPP.__init__(self, jid, secret, server, port)
40 # You don't need a session_start handler, but that is
41 # where you would broadcast initial presence.
43 # The message event is triggered whenever a message
44 # stanza is received. Be aware that that includes
45 # MUC messages and error messages.
46 self.add_event_handler("message", self.message)
48 def message(self, msg):
49 """
50 Process incoming message stanzas. Be aware that this also
51 includes MUC messages and error messages. It is usually
52 a good idea to check the messages's type before processing
53 or sending replies.
55 Since a component may send messages from any number of JIDs,
56 it is best to always include a from JID.
58 Arguments:
59 msg -- The received message stanza. See the documentation
60 for stanza objects and the Message stanza to see
61 how it may be used.
62 """
63 # The reply method will use the messages 'to' JID as the
64 # outgoing reply's 'from' JID.
65 msg.reply("Thanks for sending\n%(body)s" % msg).send()
68 if __name__ == '__main__':
69 # Setup the command line arguments.
70 optp = OptionParser()
72 # Output verbosity options.
73 optp.add_option('-q', '--quiet', help='set logging to ERROR',
74 action='store_const', dest='loglevel',
75 const=logging.ERROR, default=logging.INFO)
76 optp.add_option('-d', '--debug', help='set logging to DEBUG',
77 action='store_const', dest='loglevel',
78 const=logging.DEBUG, default=logging.INFO)
79 optp.add_option('-v', '--verbose', help='set logging to COMM',
80 action='store_const', dest='loglevel',
81 const=5, default=logging.INFO)
83 # JID and password options.
84 optp.add_option("-j", "--jid", dest="jid",
85 help="JID to use")
86 optp.add_option("-p", "--password", dest="password",
87 help="password to use")
88 optp.add_option("-s", "--server", dest="server",
89 help="server to connect to")
90 optp.add_option("-P", "--port", dest="port",
91 help="port to connect to")
93 opts, args = optp.parse_args()
95 if opts.jid is None:
96 opts.jid = raw_input("Component JID: ")
97 if opts.password is None:
98 opts.password = getpass.getpass("Password: ")
99 if opts.server is None:
100 opts.server = raw_input("Server: ")
101 if opts.port is None:
102 opts.port = int(raw_input("Port: "))
104 # Setup logging.
105 logging.basicConfig(level=opts.loglevel,
106 format='%(levelname)-8s %(message)s')
108 # Setup the EchoComponent and register plugins. Note that while plugins
109 # may have interdependencies, the order in which you register them does
110 # not matter.
111 xmpp = EchoComponent(opts.jid, opts.password, opts.server, opts.port)
112 xmpp.registerPlugin('xep_0030') # Service Discovery
113 xmpp.registerPlugin('xep_0004') # Data Forms
114 xmpp.registerPlugin('xep_0060') # PubSub
115 xmpp.registerPlugin('xep_0199') # XMPP Ping
117 # Connect to the XMPP server and start processing XMPP stanzas.
118 if xmpp.connect():
119 xmpp.process(block=True)
120 print("Done")
121 else:
122 print("Unable to connect.")