Skip to content

Instantly share code, notes, and snippets.

@lehrblogger
Created April 29, 2012 00:49
Show Gist options
  • Save lehrblogger/2522943 to your computer and use it in GitHub Desktop.
Save lehrblogger/2522943 to your computer and use it in GitHub Desktop.
XMPP XEP-0077 Authenticated In-Band Registration (develop branch), gist #2
diff --git a/examples/register_account.py b/examples/register_account.py
index 17f075a..eeb669d 100644
--- a/examples/register_account.py
+++ b/examples/register_account.py
@@ -39,8 +39,9 @@ class RegisterBot(sleekxmpp.ClientXMPP):
workflows will need to check for data forms, etc.
"""
- def __init__(self, jid, password):
+ def __init__(self, jid, password, to_register):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
+ self.to_register = to_register
# The session_start event will be triggered when
# the bot establishes its connection with the server
@@ -56,7 +57,7 @@ class RegisterBot(sleekxmpp.ClientXMPP):
# cases, you will need to examine the fields provided
# and respond accordingly. SleekXMPP provides plugins
# for data forms and OOB links that will make that easier.
- self.add_event_handler("register", self.register)
+ # self.add_event_handler("register", self.register)
def start(self, event):
"""
@@ -71,38 +72,50 @@ class RegisterBot(sleekxmpp.ClientXMPP):
event does not provide any additional
data.
"""
self.send_presence()
self.get_roster()
-
- # We're only concerned about registering, so nothing more to do here.
+
+ iq = self.Iq()
+ iq['type'] = 'set'
+ iq['to'] = self.boundjid.domain
+ iq['register']['username'] = self.to_register
+ iq['register']['password'] = 'FgT5bk3'
+ iq.send()
+
self.disconnect()
def register(self, iq):
"""
Fill out and submit a registration form.
The form may be composed of basic registration fields, a data form,
an out-of-band link, or any combination thereof. Data forms and OOB
links can be checked for as so:
if iq.match('iq/register/form'):
# do stuff with data form
# iq['register']['form']['fields']
if iq.match('iq/register/oob'):
# do stuff with OOB URL
# iq['register']['oob']['url']
To get the list of basic registration fields, you can use:
iq['register']['fields']
"""
resp = self.Iq()
resp['type'] = 'set'
- resp['register']['username'] = self.boundjid.user
- resp['register']['password'] = self.password
-
+ resp['register']['username'] = self.to_register
+ resp['register']['password'] = 'nens3ag7hund3youg2tu'
+
try:
resp.send(now=True)
logging.info("Account created for %s!" % self.boundjid)
+ #self.disconnect(wait=True) # Since otherwise we won't ever disconnect
except IqError as e:
logging.error("Could not register account: %s" %
e.iq['error']['text'])
@@ -110,6 +123,8 @@ class RegisterBot(sleekxmpp.ClientXMPP):
except IqTimeout:
logging.error("No response from server.")
self.disconnect()
if __name__ == '__main__':
@@ -133,6 +148,9 @@ if __name__ == '__main__':
optp.add_option("-p", "--password", dest="password",
help="password to use")
+ optp.add_option("-u", "--username", dest="to_register",
+ help="username to register")
+
opts, args = optp.parse_args()
# Setup logging.
@@ -147,7 +165,7 @@ if __name__ == '__main__':
# Setup the RegisterBot and register plugins. Note that while plugins may
# have interdependencies, the order in which you register them does
# not matter.
- xmpp = RegisterBot(opts.jid, opts.password)
+ xmpp = RegisterBot(opts.jid, opts.password, opts.to_register)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0004') # Data forms
xmpp.register_plugin('xep_0066') # Out-of-band Data
@@ -161,7 +179,7 @@ if __name__ == '__main__':
# xmpp.ca_certs = "path/to/ca/cert"
# Connect to the XMPP server and start processing XMPP stanzas.
- if xmpp.connect():
+ if xmpp.connect(('127.0.0.1', 5222)):
# If you do not have the dnspython library installed, you will need
# to manually specify the name of the server if it does not match
# the one in the JID. For example, to use Google Talk you would
diff --git a/sleekxmpp/plugins/xep_0077/register.py b/sleekxmpp/plugins/xep_0077/register.py
index 53cc9ef..f7b9d6a 100644
--- a/sleekxmpp/plugins/xep_0077/register.py
+++ b/sleekxmpp/plugins/xep_0077/register.py
@@ -39,19 +39,18 @@ class XEP_0077(BasePlugin):
else:
self.xmpp.register_feature('register',
self._handle_register_feature,
- restart=False,
- order=self.config.get('order', 50))
+ restart=True,
+ order=self.config.get('order', 150))
register_stanza_plugin(Register, self.xmpp['xep_0004'].stanza.Form)
register_stanza_plugin(Register, self.xmpp['xep_0066'].stanza.OOB)
def _handle_register_feature(self, features):
- if 'mechanisms' in self.xmpp.features:
- # We have already logged in with an account
- return False
-
if self.create_account:
- form = self.get_registration()
+ if 'mechanisms' in self.xmpp.features:
+ form = self.get_registration(jid=self.xmpp.boundjid.host, ifrom=self.xmpp.boundjid.full)
+ else:
+ form = self.get_registration()
self.xmpp.event('register', form, direct=True)
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment