Remove google modules from setup.py file
[slixmpp.git] / examples / send_client.py
blobd40ac1cbdedda41efd70781a19c5360df95e1e4d
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
19 # Python versions before 3.0 do not use UTF-8 encoding
20 # by default. To ensure that Unicode is handled properly
21 # throughout Slixmpp, we will set the default encoding
22 # ourselves to UTF-8.
23 if sys.version_info < (3, 0):
24 from slixmpp.util.misc_ops import setdefaultencoding
25 setdefaultencoding('utf8')
26 else:
27 raw_input = input
30 class SendMsgBot(slixmpp.ClientXMPP):
32 """
33 A basic Slixmpp bot that will log in, send a message,
34 and then log out.
35 """
37 def __init__(self, jid, password, recipient, message):
38 slixmpp.ClientXMPP.__init__(self, jid, password)
40 # The message we wish to send, and the JID that
41 # will receive it.
42 self.recipient = recipient
43 self.msg = message
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)
52 def start(self, event):
53 """
54 Process the session_start event.
56 Typical actions for the session_start event are
57 requesting the roster and broadcasting an initial
58 presence stanza.
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()
68 self.send_message(mto=self.recipient,
69 mbody=self.msg,
70 mtype='chat')
72 # Using wait=True ensures that the send queue will be
73 # emptied before ending the session.
74 self.disconnect(wait=True)
77 if __name__ == '__main__':
78 # Setup the command line arguments.
79 optp = OptionParser()
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)
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")
102 opts, args = optp.parse_args()
104 # Setup logging.
105 logging.basicConfig(level=opts.loglevel,
106 format='%(levelname)-8s %(message)s')
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: ")
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
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
128 # If you want to verify the SSL certificates offered by a server:
129 # xmpp.ca_certs = "path/to/ca/cert"
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:
138 # if xmpp.connect(('talk.google.com', 5222)):
139 # ...
140 xmpp.process(block=True)
141 print("Done")
142 else:
143 print("Unable to connect.")