Philipp Hörist pushed to branch master at gajim / gajim
Commits:
2 changed files:
Changes:
| ... | ... | @@ -14,14 +14,10 @@ |
| 14 | 14 |
|
| 15 | 15 |
# XEP-0077: In-Band Registration
|
| 16 | 16 |
|
| 17 |
-import weakref
|
|
| 18 | 17 |
|
| 19 |
-import nbxmpp
|
|
| 20 | 18 |
from nbxmpp.namespaces import Namespace
|
| 21 | 19 |
|
| 22 |
-from gajim.common import app
|
|
| 23 | 20 |
from gajim.common.modules.base import BaseModule
|
| 24 |
-from gajim.common.modules.bits_of_binary import parse_bob_data
|
|
| 25 | 21 |
|
| 26 | 22 |
|
| 27 | 23 |
class Register(BaseModule):
|
| ... | ... | @@ -31,101 +27,18 @@ class Register(BaseModule): |
| 31 | 27 |
'unregister',
|
| 32 | 28 |
'change_password',
|
| 33 | 29 |
'change_password_with_form',
|
| 30 |
+ 'request_register_form',
|
|
| 31 |
+ 'submit_register_form',
|
|
| 34 | 32 |
]
|
| 35 | 33 |
|
| 36 | 34 |
def __init__(self, con):
|
| 37 | 35 |
BaseModule.__init__(self, con)
|
| 38 | 36 |
|
| 39 |
- self.agent_registrations = {}
|
|
| 40 |
- |
|
| 41 | 37 |
self.supported = False
|
| 42 | 38 |
|
| 43 | 39 |
def pass_disco(self, info):
|
| 44 | 40 |
self.supported = Namespace.REGISTER in info.features
|
| 45 | 41 |
|
| 46 |
- def register_agent(self, agent, form, is_form, success_cb, error_cb):
|
|
| 47 |
- if not app.account_is_available(self._account):
|
|
| 48 |
- return
|
|
| 49 |
- |
|
| 50 |
- weak_success_cb = weakref.WeakMethod(success_cb)
|
|
| 51 |
- weak_error_cb = weakref.WeakMethod(error_cb)
|
|
| 52 |
- |
|
| 53 |
- iq = nbxmpp.Iq('set', Namespace.REGISTER, to=agent)
|
|
| 54 |
- if is_form:
|
|
| 55 |
- query = iq.setQuery()
|
|
| 56 |
- form.setAttr('type', 'submit')
|
|
| 57 |
- query.addChild(node=form)
|
|
| 58 |
- else:
|
|
| 59 |
- for field in form.keys():
|
|
| 60 |
- iq.setTag('query').setTagData(field, form[field])
|
|
| 61 |
- |
|
| 62 |
- self._con.connection.SendAndCallForResponse(
|
|
| 63 |
- iq, self._register_agent_response, {'agent': agent,
|
|
| 64 |
- 'success_cb': weak_success_cb,
|
|
| 65 |
- 'error_cb': weak_error_cb})
|
|
| 66 |
- |
|
| 67 |
- def _register_agent_response(self, _nbxmpp_client, stanza, agent,
|
|
| 68 |
- success_cb, error_cb):
|
|
| 69 |
- if not nbxmpp.isResultNode(stanza):
|
|
| 70 |
- error = stanza.getErrorMsg()
|
|
| 71 |
- self._log.info('Error: %s', error)
|
|
| 72 |
- if error_cb() is not None:
|
|
| 73 |
- form = is_form = None
|
|
| 74 |
- if stanza.getErrorType() == 'modify':
|
|
| 75 |
- form, is_form = self._get_register_form(stanza)
|
|
| 76 |
- error_cb()(error, form, is_form)
|
|
| 77 |
- return
|
|
| 78 |
- |
|
| 79 |
- self._con.get_module('Presence').subscribe(agent, auto_auth=True)
|
|
| 80 |
- |
|
| 81 |
- if success_cb() is not None:
|
|
| 82 |
- success_cb()()
|
|
| 83 |
- |
|
| 84 |
- def get_register_form(self, jid, success_cb, error_cb):
|
|
| 85 |
- if not app.account_is_available(self._account):
|
|
| 86 |
- return
|
|
| 87 |
- |
|
| 88 |
- weak_success_cb = weakref.WeakMethod(success_cb)
|
|
| 89 |
- weak_error_cb = weakref.WeakMethod(error_cb)
|
|
| 90 |
- |
|
| 91 |
- iq = nbxmpp.Iq('get', Namespace.REGISTER, to=jid)
|
|
| 92 |
- self._con.connection.SendAndCallForResponse(
|
|
| 93 |
- iq, self._register_info_response, {'success_cb': weak_success_cb,
|
|
| 94 |
- 'error_cb': weak_error_cb})
|
|
| 95 |
- |
|
| 96 |
- def _register_info_response(self, _nbxmpp_client, stanza,
|
|
| 97 |
- success_cb, error_cb):
|
|
| 98 |
- if not nbxmpp.isResultNode(stanza):
|
|
| 99 |
- error = stanza.getErrorMsg()
|
|
| 100 |
- self._log.info('Error: %s', error)
|
|
| 101 |
- if error_cb() is not None:
|
|
| 102 |
- error_cb()(error)
|
|
| 103 |
- else:
|
|
| 104 |
- self._log.info('Register form received')
|
|
| 105 |
- |
|
| 106 |
- if success_cb() is not None:
|
|
| 107 |
- form, is_form = self._get_register_form(stanza)
|
|
| 108 |
- success_cb()(form, is_form)
|
|
| 109 |
- |
|
| 110 |
- @staticmethod
|
|
| 111 |
- def _get_register_form(stanza):
|
|
| 112 |
- parse_bob_data(stanza.getQuery())
|
|
| 113 |
- form = stanza.getQuery().getTag('x', namespace=Namespace.DATA)
|
|
| 114 |
- is_form = form is not None
|
|
| 115 |
- if not is_form:
|
|
| 116 |
- form = {}
|
|
| 117 |
- oob = stanza.getQuery().getTag('x', namespace=Namespace.X_OOB)
|
|
| 118 |
- if oob is not None:
|
|
| 119 |
- form['redirect-url'] = oob.getTagData('url')
|
|
| 120 |
- for field in stanza.getQueryPayload():
|
|
| 121 |
- if not isinstance(field, nbxmpp.Node):
|
|
| 122 |
- continue
|
|
| 123 |
- if field.getName() == 'x':
|
|
| 124 |
- continue
|
|
| 125 |
- form[field.getName()] = field.getData()
|
|
| 126 |
- |
|
| 127 |
- return form, is_form
|
|
| 128 |
- |
|
| 129 | 42 |
|
| 130 | 43 |
def get_instance(*args, **kwargs):
|
| 131 | 44 |
return Register(*args, **kwargs), 'Register'
|
| ... | ... | @@ -17,13 +17,14 @@ |
| 17 | 17 |
|
| 18 | 18 |
from gi.repository import Gtk
|
| 19 | 19 |
|
| 20 |
-from nbxmpp.modules import dataforms
|
|
| 20 |
+from nbxmpp.errors import StanzaError
|
|
| 21 |
+from nbxmpp.errors import MalformedStanzaError
|
|
| 22 |
+from nbxmpp.errors import RegisterStanzaError
|
|
| 21 | 23 |
|
| 22 | 24 |
from gajim.common import app
|
| 23 | 25 |
from gajim.common.i18n import _
|
| 24 | 26 |
|
| 25 | 27 |
from .dataform import DataFormWidget
|
| 26 |
-from .dataform import FakeDataFormWidget
|
|
| 27 | 28 |
|
| 28 | 29 |
log = logging.getLogger('gajim.gui.registration')
|
| 29 | 30 |
|
| ... | ... | @@ -44,7 +45,6 @@ def __init__(self, account, agent): |
| 44 | 45 |
self._agent = agent
|
| 45 | 46 |
self._account = account
|
| 46 | 47 |
self._data_form_widget = None
|
| 47 |
- self._is_form = None
|
|
| 48 | 48 |
|
| 49 | 49 |
self.set_application(app.app)
|
| 50 | 50 |
self.set_resizable(True)
|
| ... | ... | @@ -77,63 +77,55 @@ def _remove_sidebar(self): |
| 77 | 77 |
sidebar = main_box.get_children()[0]
|
| 78 | 78 |
main_box.remove(sidebar)
|
| 79 | 79 |
|
| 80 |
- def _build_dataform(self, form, is_form):
|
|
| 81 |
- if not is_form:
|
|
| 82 |
- return FakeDataFormWidget(form)
|
|
| 83 |
- |
|
| 84 |
- dataform = dataforms.extend_form(node=form)
|
|
| 85 |
- |
|
| 86 |
- form_widget = DataFormWidget(dataform)
|
|
| 87 |
- form_widget.connect('is-valid', self._on_is_valid)
|
|
| 88 |
- form_widget.validate()
|
|
| 89 |
- return form_widget
|
|
| 90 |
- |
|
| 91 | 80 |
def _on_page_change(self, _assistant, _page):
|
| 92 | 81 |
if self.get_current_page() == Page.REQUEST:
|
| 93 |
- self._con.get_module('Register').get_register_form(
|
|
| 94 |
- self._agent, self._on_get_success, self._on_error)
|
|
| 82 |
+ self._con.get_module('Register').request_register_form(
|
|
| 83 |
+ self._agent, callback=self._on_register_form)
|
|
| 95 | 84 |
elif self.get_current_page() == Page.SENDING:
|
| 96 | 85 |
self._register()
|
| 97 | 86 |
self.commit()
|
| 98 | 87 |
|
| 99 |
- def _on_get_success(self, form, is_form):
|
|
| 100 |
- log.info('Show Form page')
|
|
| 101 |
- self._is_form = is_form
|
|
| 102 |
- self._data_form_widget = self._build_dataform(form, is_form)
|
|
| 88 |
+ def _on_register_form(self, task):
|
|
| 89 |
+ try:
|
|
| 90 |
+ result = task.finish()
|
|
| 91 |
+ except (StanzaError, MalformedStanzaError) as error:
|
|
| 92 |
+ self.get_nth_page(Page.ERROR).set_text(error.get_text())
|
|
| 93 |
+ self.set_current_page(Page.ERROR)
|
|
| 94 |
+ return
|
|
| 95 |
+ |
|
| 96 |
+ form = result.form
|
|
| 97 |
+ if result.form is None:
|
|
| 98 |
+ form = result.fields_form
|
|
| 99 |
+ |
|
| 100 |
+ self._data_form_widget = DataFormWidget(form)
|
|
| 101 |
+ self._data_form_widget.connect('is-valid', self._on_is_valid)
|
|
| 102 |
+ self._data_form_widget.validate()
|
|
| 103 | 103 |
self.get_nth_page(Page.FORM).set_form(self._data_form_widget)
|
| 104 | 104 |
self.set_current_page(Page.FORM)
|
| 105 | 105 |
|
| 106 | 106 |
def _on_is_valid(self, _widget, is_valid):
|
| 107 | 107 |
self.set_page_complete(self.get_nth_page(Page.FORM), is_valid)
|
| 108 | 108 |
|
| 109 |
- def _on_error(self, error_text, form=None, is_form=False):
|
|
| 110 |
- if form is not None:
|
|
| 111 |
- log.info('Show Form page')
|
|
| 112 |
- self._is_form = is_form
|
|
| 113 |
- self._data_form_widget = self._build_dataform(form, is_form)
|
|
| 114 |
- self.get_nth_page(Page.FORM).set_form(self._data_form_widget,
|
|
| 115 |
- error_text=error_text)
|
|
| 116 |
- self.set_current_page(Page.FORM)
|
|
| 117 |
- else:
|
|
| 118 |
- log.info('Show Error page')
|
|
| 119 |
- self.get_nth_page(Page.ERROR).set_text(error_text)
|
|
| 120 |
- self.set_current_page(Page.ERROR)
|
|
| 121 |
- |
|
| 122 | 109 |
def _on_cancel(self, _widget):
|
| 123 | 110 |
self.destroy()
|
| 124 | 111 |
|
| 125 | 112 |
def _register(self):
|
| 126 |
- log.info('Show Sending page')
|
|
| 127 | 113 |
form = self._data_form_widget.get_submit_form()
|
| 128 |
- self._con.get_module('Register').register_agent(
|
|
| 129 |
- self._agent,
|
|
| 114 |
+ self._con.get_module('Register').submit_register_form(
|
|
| 130 | 115 |
form,
|
| 131 |
- self._is_form,
|
|
| 132 |
- self._on_register_success,
|
|
| 133 |
- self._on_error)
|
|
| 116 |
+ self._agent,
|
|
| 117 |
+ callback=self._on_register_result)
|
|
| 118 |
+ |
|
| 119 |
+ def _on_register_result(self, task):
|
|
| 120 |
+ try:
|
|
| 121 |
+ task.finish()
|
|
| 122 |
+ except (StanzaError,
|
|
| 123 |
+ MalformedStanzaError,
|
|
| 124 |
+ RegisterStanzaError) as error:
|
|
| 125 |
+ self.get_nth_page(Page.ERROR).set_text(error.get_text())
|
|
| 126 |
+ self.set_current_page(Page.ERROR)
|
|
| 127 |
+ return
|
|
| 134 | 128 |
|
| 135 |
- def _on_register_success(self):
|
|
| 136 |
- log.info('Show Success page')
|
|
| 137 | 129 |
self.set_current_page(Page.SUCCESS)
|
| 138 | 130 |
|
| 139 | 131 |
|