2 # -*- coding: utf-8 -*-
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.
16 from optparse
import OptionParser
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
24 if sys
.version_info
< (3, 0):
26 sys
.setdefaultencoding('utf8')
31 class SendMsgBot(sleekxmpp
.ClientXMPP
):
34 A basic SleekXMPP bot that will log in, send a message,
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
43 self
.recipient
= recipient
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
51 self
.add_event_handler("session_start", self
.start
)
53 def start(self
, event
):
55 Process the session_start event.
57 Typical actions for the session_start event are
58 requesting the roster and broadcasting an intial
62 event -- An empty dictionary. The session_start
63 event does not provide any additional
69 self
.send_message(mto
=self
.recipient
,
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.
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",
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()
106 logging
.basicConfig(level
=opts
.loglevel
,
107 format
='%(levelname)-8s %(message)s')
110 opts
.jid
= raw_input("Username: ")
111 if opts
.password
is None:
112 opts
.password
= getpass
.getpass("Password: ")
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
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.
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
139 # if xmpp.connect(('talk.google.com', 5222)):
141 xmpp
.process(threaded
=False)
144 print("Unable to connect.")