|
13 | 13 |
|
14 | 14 | import sys
|
15 | 15 | import logging
|
16 |
| -import getpass |
17 |
| -from optparse import OptionParser |
18 | 16 |
|
19 | 17 | import sleekxmpp
|
20 | 18 | from sleekxmpp.exceptions import IqError, IqTimeout
|
| 19 | +from dns.resolver import NoNameservers |
21 | 20 |
|
22 | 21 |
|
23 | 22 | # Get a logger
|
|
31 | 30 | if sys.version_info < (3, 0):
|
32 | 31 | from sleekxmpp.util.misc_ops import setdefaultencoding
|
33 | 32 | setdefaultencoding('utf8')
|
34 |
| -else: |
35 |
| - raw_input = input |
36 | 33 |
|
37 | 34 |
|
38 |
| -class ChangePasswordBot(sleekxmpp.ClientXMPP): |
| 35 | +TIMEOUT = 10 |
39 | 36 |
|
40 |
| - """ |
41 |
| - A basic bot that will attempt to change the password for an account |
42 |
| - with a XMPP server. |
43 | 37 |
|
44 |
| - NOTE: It requires XEP-0077 plugin. |
45 |
| - """ |
| 38 | +def register(client, username, password, name, email): |
| 39 | + iq = client.Iq() |
| 40 | + iq['type'] = 'set' |
| 41 | + iq['register']['username'] = username |
| 42 | + iq['register']['password'] = password |
| 43 | + iq['register']['name'] = name |
| 44 | + iq['register']['email'] = email |
| 45 | + iq.send(now=True) #, timeout=TIMEOUT) |
46 | 46 |
|
47 |
| - def __init__(self, jid, current_password, new_password): |
48 |
| - sleekxmpp.ClientXMPP.__init__(self, jid, current_password) |
49 | 47 |
|
50 |
| - self.new_password = new_password |
51 |
| - self.add_event_handler("session_start", self.start) |
| 48 | +def registration_wrapper(client, function, logger_success, logger_error, |
| 49 | + use_client, args, kwargs): |
52 | 50 |
|
53 |
| - def start(self, event): |
54 |
| - self.send_presence() |
| 51 | + connected = client.connect(reattempt=False) |
55 | 52 |
|
56 |
| - try: |
57 |
| - self['xep_0077'].change_password(self.new_password, self.boundjid.server, self.boundjid.bare) |
58 |
| - logger.info("Password changed for %s!" % self.boundjid) |
59 |
| - except IqError as e: |
60 |
| - logger.error("Could not change password for account: %s" % |
61 |
| - e.iq['error']['text']) |
62 |
| - self.disconnect() |
63 |
| - except IqTimeout: |
64 |
| - logger.error("No response from server.") |
65 |
| - self.disconnect() |
66 |
| - else: |
67 |
| - self.disconnect(send_close=False) |
68 |
| - |
69 |
| - |
70 |
| -class RegisterBot(sleekxmpp.ClientXMPP): |
71 |
| - |
72 |
| - """ |
73 |
| - A basic bot that will attempt to register an account |
74 |
| - with an XMPP server. |
75 |
| -
|
76 |
| - NOTE: This follows the very basic registration workflow |
77 |
| - from XEP-0077. More advanced server registration |
78 |
| - workflows will need to check for data forms, etc. |
79 |
| - """ |
80 |
| - |
81 |
| - def __init__(self, jid, password, name=None, email=None): |
82 |
| - sleekxmpp.ClientXMPP.__init__(self, jid, password) |
83 |
| - |
84 |
| - self.name = name |
85 |
| - self.email = email |
86 |
| - |
87 |
| - # The session_start event will be triggered when |
88 |
| - # the bot establishes its connection with the server |
89 |
| - # and the XML streams are ready for use. We want to |
90 |
| - # listen for this event so that we we can initialize |
91 |
| - # our roster. |
92 |
| - self.add_event_handler("session_start", self.start) |
93 |
| - |
94 |
| - # The register event provides an Iq result stanza with |
95 |
| - # a registration form from the server. This may include |
96 |
| - # the basic registration fields, a data form, an |
97 |
| - # out-of-band URL, or any combination. For more advanced |
98 |
| - # cases, you will need to examine the fields provided |
99 |
| - # and respond accordingly. SleekXMPP provides plugins |
100 |
| - # for data forms and OOB links that will make that easier. |
101 |
| - self.add_event_handler("register", self.register) |
102 |
| - |
103 |
| - # Do not terminate session after disconnecting |
104 |
| - self.end_session_on_disconnect = False |
105 |
| - |
106 |
| - def start(self, event): |
107 |
| - """ |
108 |
| - Process the session_start event. |
109 |
| -
|
110 |
| - Typical actions for the session_start event are |
111 |
| - requesting the roster and broadcasting an initial |
112 |
| - presence stanza. |
113 |
| -
|
114 |
| - Arguments: |
115 |
| - event -- An empty dictionary. The session_start |
116 |
| - event does not provide any additional |
117 |
| - data. |
118 |
| - """ |
119 |
| - self.send_presence() |
120 |
| - self.get_roster() |
121 |
| - |
122 |
| - # We're only concerned about registering, so nothing more to do here. |
123 |
| - self.disconnect() |
124 |
| - |
125 |
| - def register(self, iq): |
126 |
| - """ |
127 |
| - Fill out and submit a registration form. |
128 |
| -
|
129 |
| - The form may be composed of basic registration fields, a data form, |
130 |
| - an out-of-band link, or any combination thereof. Data forms and OOB |
131 |
| - links can be checked for as so: |
132 |
| -
|
133 |
| - if iq.match('iq/register/form'): |
134 |
| - # do stuff with data form |
135 |
| - # iq['register']['form']['fields'] |
136 |
| - if iq.match('iq/register/oob'): |
137 |
| - # do stuff with OOB URL |
138 |
| - # iq['register']['oob']['url'] |
139 |
| -
|
140 |
| - To get the list of basic registration fields, you can use: |
141 |
| - iq['register']['fields'] |
142 |
| - """ |
143 |
| - |
144 |
| - resp = self.Iq() |
145 |
| - resp['type'] = 'set' |
146 |
| - resp['register']['username'] = self.boundjid.user |
147 |
| - resp['register']['password'] = self.password |
148 |
| - |
149 |
| - if self.name: |
150 |
| - resp['register']['name'] = self.name |
151 |
| - |
152 |
| - if self.email: |
153 |
| - resp['register']['email'] = self.email |
154 |
| - |
155 |
| - # TODO: Raise exception if fails |
156 |
| - try: |
157 |
| - resp.send(now=True) |
158 |
| - logger.info("Account created for %s!" % self.boundjid) |
159 |
| - except IqError as e: |
160 |
| - logger.error("Could not register account: %s" % |
161 |
| - e.iq['error']['text']) |
162 |
| - self.disconnect() |
163 |
| - except IqTimeout: |
164 |
| - logger.error("No response from server.") |
165 |
| - self.disconnect() |
166 |
| - else: |
167 |
| - self.disconnect(send_close=False) |
168 |
| - |
169 |
| -def register_account(jid, password, name=None, email=None): |
170 |
| - # Setup the RegisterBot and register plugins. Note that while plugins may |
171 |
| - # have interdependencies, the order in which you register them does |
172 |
| - # not matter. |
173 |
| - xmpp = RegisterBot(jid, password, name, email) |
174 |
| - xmpp.register_plugin('xep_0030') # Service Discovery |
175 |
| - xmpp.register_plugin('xep_0004') # Data forms |
176 |
| - xmpp.register_plugin('xep_0066') # Out-of-band Data |
177 |
| - xmpp.register_plugin('xep_0077') # In-band Registration |
178 |
| - |
179 |
| - # Some servers don't advertise support for inband registration, even |
180 |
| - # though they allow it. If this applies to your server, use: |
181 |
| - xmpp['xep_0077'].force_registration = True |
182 |
| - |
183 |
| - # If you are working with an OpenFire server, you may need |
184 |
| - # to adjust the SSL version used: |
185 |
| - # xmpp.ssl_version = ssl.PROTOCOL_SSLv3 |
186 |
| - |
187 |
| - # If you want to verify the SSL certificates offered by a server: |
188 |
| - # xmpp.ca_certs = "path/to/ca/cert" |
189 |
| - |
190 |
| - # Connect to the XMPP server and start processing XMPP stanzas. |
191 |
| - if xmpp.connect(): |
192 |
| - xmpp.process(block=True) |
193 |
| - else: |
| 53 | + if not connected: |
194 | 54 | logger.error('Unable to connect to XMPP server.')
|
195 | 55 | return False
|
196 | 56 |
|
197 |
| - return True |
198 |
| - |
199 |
| - |
200 |
| -def change_password(jid, old_password, new_password): |
201 |
| - # To change a user's password you it's necessary the jid, current |
202 |
| - # password and new password. |
203 |
| - client = ChangePasswordBot(jid, old_password, new_password) |
204 |
| - client.register_plugin('xep_0077') |
| 57 | + client.process() |
205 | 58 |
|
206 |
| - client['xep_0077'].create_account = False |
| 59 | + success = False |
| 60 | + try: |
| 61 | + if use_client: |
| 62 | + function(client, *args, **kwargs) |
| 63 | + else: |
| 64 | + function(*args, **kwargs) |
| 65 | + logger.info(logger_success % client.boundjid) |
| 66 | + success = True |
| 67 | + except IqError as e: |
| 68 | + logger.error(logger_error % e.iq['error']['text']) |
| 69 | + except IqTimeout: |
| 70 | + logger.error("No response from server.") |
| 71 | + finally: |
| 72 | + client.disconnect() |
| 73 | + return success |
| 74 | + |
| 75 | + |
| 76 | +def register_account(jid, password, name='', email=''): |
| 77 | + client = sleekxmpp.ClientXMPP(jid, password) |
| 78 | + client.register_plugin('xep_0077') # In-band Registration |
| 79 | + |
| 80 | + registration_wrapper( |
| 81 | + client=client, |
| 82 | + function=register, |
| 83 | + logger_success="Account created for %s!", |
| 84 | + logger_error="Could not register account: %s", |
| 85 | + use_client=True, |
| 86 | + args=[client.boundjid.user, password, name, email], |
| 87 | + kwargs={} |
| 88 | + ) |
207 | 89 |
|
208 |
| - if client.connect(): |
209 |
| - client.process(block=True) |
210 |
| - else: |
211 |
| - logger.error('Unable to connect to XMPP server.') |
212 |
| - return False |
213 | 90 |
|
214 |
| - return True |
| 91 | +def change_password(jid, old_password, new_password): |
| 92 | + client = sleekxmpp.ClientXMPP(jid, old_password) |
| 93 | + client.register_plugin('xep_0077') # In-band Registration |
| 94 | + |
| 95 | + registration_wrapper( |
| 96 | + client=client, |
| 97 | + function=client['xep_0077'].change_password, |
| 98 | + logger_success="Password changed for %s!", |
| 99 | + logger_error="Could not change password for account: %s", |
| 100 | + use_client=False, |
| 101 | + args=[new_password, client.boundjid.server, client.boundjid.bare], |
| 102 | + kwargs=dict(timeout=TIMEOUT) |
| 103 | + ) |
0 commit comments