2 # -*- coding: utf-8 -*-
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.
15 from optparse
import OptionParser
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
23 if sys
.version_info
< (3, 0):
24 from slixmpp
.util
.misc_ops
import setdefaultencoding
25 setdefaultencoding('utf8')
30 class SendMsgBot(slixmpp
.ClientXMPP
):
33 A basic Slixmpp bot that will log in, send a message,
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
42 self
.recipient
= recipient
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
50 self
.add_event_handler("session_start", self
.start
, threaded
=True)
52 def start(self
, event
):
54 Process the session_start event.
56 Typical actions for the session_start event are
57 requesting the roster and broadcasting an initial
61 event -- An empty dictionary. The session_start
62 event does not provide any additional
68 self
.send_message(mto
=self
.recipient
,
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.
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",
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()
105 logging
.basicConfig(level
=opts
.loglevel
,
106 format
='%(levelname)-8s %(message)s')
109 opts
.jid
= raw_input("Username: ")
110 if opts
.password
is None:
111 opts
.password
= getpass
.getpass("Password: ")
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
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.
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
138 # if xmpp.connect(('talk.google.com', 5222)):
140 xmpp
.process(block
=True)
143 print("Unable to connect.")