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