Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0100.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | be6d91572633 |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
59 | 59 |
60 class XEP_0100(object): | 60 class XEP_0100(object): |
61 def __init__(self, host): | 61 def __init__(self, host): |
62 log.info(_("Gateways plugin initialization")) | 62 log.info(_("Gateways plugin initialization")) |
63 self.host = host | 63 self.host = host |
64 self.__gateways = {} # dict used to construct the answer to findGateways. Key = target jid | 64 self.__gateways = {} # dict used to construct the answer to gateways_find. Key = target jid |
65 host.bridge.addMethod( | 65 host.bridge.add_method( |
66 "findGateways", | 66 "gateways_find", |
67 ".plugin", | 67 ".plugin", |
68 in_sign="ss", | 68 in_sign="ss", |
69 out_sign="s", | 69 out_sign="s", |
70 method=self._findGateways, | 70 method=self._find_gateways, |
71 ) | 71 ) |
72 host.bridge.addMethod( | 72 host.bridge.add_method( |
73 "gatewayRegister", | 73 "gateway_register", |
74 ".plugin", | 74 ".plugin", |
75 in_sign="ss", | 75 in_sign="ss", |
76 out_sign="s", | 76 out_sign="s", |
77 method=self._gatewayRegister, | 77 method=self._gateway_register, |
78 ) | 78 ) |
79 self.__menu_id = host.registerCallback(self._gatewaysMenu, with_data=True) | 79 self.__menu_id = host.register_callback(self._gateways_menu, with_data=True) |
80 self.__selected_id = host.registerCallback( | 80 self.__selected_id = host.register_callback( |
81 self._gatewaySelectedCb, with_data=True | 81 self._gateway_selected_cb, with_data=True |
82 ) | 82 ) |
83 host.importMenu( | 83 host.import_menu( |
84 (D_("Service"), D_("Gateways")), | 84 (D_("Service"), D_("Gateways")), |
85 self._gatewaysMenu, | 85 self._gateways_menu, |
86 security_limit=1, | 86 security_limit=1, |
87 help_string=D_("Find gateways"), | 87 help_string=D_("Find gateways"), |
88 ) | 88 ) |
89 | 89 |
90 def _gatewaysMenu(self, data, profile): | 90 def _gateways_menu(self, data, profile): |
91 """ XMLUI activated by menu: return Gateways UI | 91 """ XMLUI activated by menu: return Gateways UI |
92 | 92 |
93 @param profile: %(doc_profile)s | 93 @param profile: %(doc_profile)s |
94 """ | 94 """ |
95 client = self.host.getClient(profile) | 95 client = self.host.get_client(profile) |
96 try: | 96 try: |
97 jid_ = jid.JID( | 97 jid_ = jid.JID( |
98 data.get(xml_tools.formEscape("external_jid"), client.jid.host) | 98 data.get(xml_tools.form_escape("external_jid"), client.jid.host) |
99 ) | 99 ) |
100 except RuntimeError: | 100 except RuntimeError: |
101 raise exceptions.DataError(_("Invalid JID")) | 101 raise exceptions.DataError(_("Invalid JID")) |
102 d = self.findGateways(jid_, profile) | 102 d = self.gateways_find(jid_, profile) |
103 d.addCallback(self._gatewaysResult2XMLUI, jid_) | 103 d.addCallback(self._gateways_result_2_xmlui, jid_) |
104 d.addCallback(lambda xmlui: {"xmlui": xmlui.toXml()}) | 104 d.addCallback(lambda xmlui: {"xmlui": xmlui.toXml()}) |
105 return d | 105 return d |
106 | 106 |
107 def _gatewaysResult2XMLUI(self, result, entity): | 107 def _gateways_result_2_xmlui(self, result, entity): |
108 xmlui = xml_tools.XMLUI(title=_("Gateways manager (%s)") % entity.full()) | 108 xmlui = xml_tools.XMLUI(title=_("Gateways manager (%s)") % entity.full()) |
109 xmlui.addText(_(WARNING_MSG)) | 109 xmlui.addText(_(WARNING_MSG)) |
110 xmlui.addDivider("dash") | 110 xmlui.addDivider("dash") |
111 adv_list = xmlui.changeContainer( | 111 adv_list = xmlui.change_container( |
112 "advanced_list", | 112 "advanced_list", |
113 columns=3, | 113 columns=3, |
114 selectable="single", | 114 selectable="single", |
115 callback_id=self.__selected_id, | 115 callback_id=self.__selected_id, |
116 ) | 116 ) |
122 xmlui.addEmpty() | 122 xmlui.addEmpty() |
123 else: | 123 else: |
124 jid_, data = gateway_data | 124 jid_, data = gateway_data |
125 for datum in data: | 125 for datum in data: |
126 identity, name = datum | 126 identity, name = datum |
127 adv_list.setRowIndex(jid_.full()) | 127 adv_list.set_row_index(jid_.full()) |
128 xmlui.addJid(jid_) | 128 xmlui.addJid(jid_) |
129 xmlui.addText(name) | 129 xmlui.addText(name) |
130 xmlui.addText(self._getIdentityDesc(identity)) | 130 xmlui.addText(self._get_identity_desc(identity)) |
131 adv_list.end() | 131 adv_list.end() |
132 xmlui.addDivider("blank") | 132 xmlui.addDivider("blank") |
133 xmlui.changeContainer("advanced_list", columns=3) | 133 xmlui.change_container("advanced_list", columns=3) |
134 xmlui.addLabel(_("Use external XMPP server")) | 134 xmlui.addLabel(_("Use external XMPP server")) |
135 xmlui.addString("external_jid") | 135 xmlui.addString("external_jid") |
136 xmlui.addButton(self.__menu_id, _("Go !"), fields_back=("external_jid",)) | 136 xmlui.addButton(self.__menu_id, _("Go !"), fields_back=("external_jid",)) |
137 return xmlui | 137 return xmlui |
138 | 138 |
139 def _gatewaySelectedCb(self, data, profile): | 139 def _gateway_selected_cb(self, data, profile): |
140 try: | 140 try: |
141 target_jid = jid.JID(data["index"]) | 141 target_jid = jid.JID(data["index"]) |
142 except (KeyError, RuntimeError): | 142 except (KeyError, RuntimeError): |
143 log.warning(_("No gateway index selected")) | 143 log.warning(_("No gateway index selected")) |
144 return {} | 144 return {} |
145 | 145 |
146 d = self.gatewayRegister(target_jid, profile) | 146 d = self.gateway_register(target_jid, profile) |
147 d.addCallback(lambda xmlui: {"xmlui": xmlui.toXml()}) | 147 d.addCallback(lambda xmlui: {"xmlui": xmlui.toXml()}) |
148 return d | 148 return d |
149 | 149 |
150 def _getIdentityDesc(self, identity): | 150 def _get_identity_desc(self, identity): |
151 """ Return a human readable description of identity | 151 """ Return a human readable description of identity |
152 @param identity: tuple as returned by Disco identities (category, type) | 152 @param identity: tuple as returned by Disco identities (category, type) |
153 | 153 |
154 """ | 154 """ |
155 category, type_ = identity | 155 category, type_ = identity |
163 try: | 163 try: |
164 return _(TYPE_DESCRIPTIONS[type_]) | 164 return _(TYPE_DESCRIPTIONS[type_]) |
165 except KeyError: | 165 except KeyError: |
166 return _("Unknown IM") | 166 return _("Unknown IM") |
167 | 167 |
168 def _registrationSuccessful(self, jid_, profile): | 168 def _registration_successful(self, jid_, profile): |
169 """Called when in_band registration is ok, we must now follow the rest of procedure""" | 169 """Called when in_band registration is ok, we must now follow the rest of procedure""" |
170 log.debug(_("Registration successful, doing the rest")) | 170 log.debug(_("Registration successful, doing the rest")) |
171 self.host.addContact(jid_, profile_key=profile) | 171 self.host.contact_add(jid_, profile_key=profile) |
172 self.host.setPresence(jid_, profile_key=profile) | 172 self.host.presence_set(jid_, profile_key=profile) |
173 | 173 |
174 def _gatewayRegister(self, target_jid_s, profile_key=C.PROF_KEY_NONE): | 174 def _gateway_register(self, target_jid_s, profile_key=C.PROF_KEY_NONE): |
175 d = self.gatewayRegister(jid.JID(target_jid_s), profile_key) | 175 d = self.gateway_register(jid.JID(target_jid_s), profile_key) |
176 d.addCallback(lambda xmlui: xmlui.toXml()) | 176 d.addCallback(lambda xmlui: xmlui.toXml()) |
177 return d | 177 return d |
178 | 178 |
179 def gatewayRegister(self, target_jid, profile_key=C.PROF_KEY_NONE): | 179 def gateway_register(self, target_jid, profile_key=C.PROF_KEY_NONE): |
180 """Register gateway using in-band registration, then log-in to gateway""" | 180 """Register gateway using in-band registration, then log-in to gateway""" |
181 profile = self.host.memory.getProfileName(profile_key) | 181 profile = self.host.memory.get_profile_name(profile_key) |
182 assert profile | 182 assert profile |
183 d = self.host.plugins["XEP-0077"].inBandRegister( | 183 d = self.host.plugins["XEP-0077"].in_band_register( |
184 target_jid, self._registrationSuccessful, profile | 184 target_jid, self._registration_successful, profile |
185 ) | 185 ) |
186 return d | 186 return d |
187 | 187 |
188 def _infosReceived(self, dl_result, items, target, client): | 188 def _infos_received(self, dl_result, items, target, client): |
189 """Find disco infos about entity, to check if it is a gateway""" | 189 """Find disco infos about entity, to check if it is a gateway""" |
190 | 190 |
191 ret = [] | 191 ret = [] |
192 for idx, (success, result) in enumerate(dl_result): | 192 for idx, (success, result) in enumerate(dl_result): |
193 if not success: | 193 if not success: |
222 _("Skipping [%(jid)s] which is not a gateway") | 222 _("Skipping [%(jid)s] which is not a gateway") |
223 % {"jid": entity.full()} | 223 % {"jid": entity.full()} |
224 ) | 224 ) |
225 return ret | 225 return ret |
226 | 226 |
227 def _itemsReceived(self, disco, target, client): | 227 def _items_received(self, disco, target, client): |
228 """Look for items with disco protocol, and ask infos for each one""" | 228 """Look for items with disco protocol, and ask infos for each one""" |
229 | 229 |
230 if len(disco._items) == 0: | 230 if len(disco._items) == 0: |
231 log.debug(_("No gateway found")) | 231 log.debug(_("No gateway found")) |
232 return [] | 232 return [] |
235 for item in disco._items: | 235 for item in disco._items: |
236 log.debug(_("item found: %s") % item.entity) | 236 log.debug(_("item found: %s") % item.entity) |
237 _defers.append(client.disco.requestInfo(item.entity)) | 237 _defers.append(client.disco.requestInfo(item.entity)) |
238 dl = defer.DeferredList(_defers) | 238 dl = defer.DeferredList(_defers) |
239 dl.addCallback( | 239 dl.addCallback( |
240 self._infosReceived, items=disco._items, target=target, client=client | 240 self._infos_received, items=disco._items, target=target, client=client |
241 ) | 241 ) |
242 reactor.callLater(GATEWAY_TIMEOUT, dl.cancel) | 242 reactor.callLater(GATEWAY_TIMEOUT, dl.cancel) |
243 return dl | 243 return dl |
244 | 244 |
245 def _findGateways(self, target_jid_s, profile_key): | 245 def _find_gateways(self, target_jid_s, profile_key): |
246 target_jid = jid.JID(target_jid_s) | 246 target_jid = jid.JID(target_jid_s) |
247 profile = self.host.memory.getProfileName(profile_key) | 247 profile = self.host.memory.get_profile_name(profile_key) |
248 if not profile: | 248 if not profile: |
249 raise exceptions.ProfileUnknownError | 249 raise exceptions.ProfileUnknownError |
250 d = self.findGateways(target_jid, profile) | 250 d = self.gateways_find(target_jid, profile) |
251 d.addCallback(self._gatewaysResult2XMLUI, target_jid) | 251 d.addCallback(self._gateways_result_2_xmlui, target_jid) |
252 d.addCallback(lambda xmlui: xmlui.toXml()) | 252 d.addCallback(lambda xmlui: xmlui.toXml()) |
253 return d | 253 return d |
254 | 254 |
255 def findGateways(self, target, profile): | 255 def gateways_find(self, target, profile): |
256 """Find gateways in the target JID, using discovery protocol | 256 """Find gateways in the target JID, using discovery protocol |
257 """ | 257 """ |
258 client = self.host.getClient(profile) | 258 client = self.host.get_client(profile) |
259 log.debug( | 259 log.debug( |
260 _("find gateways (target = %(target)s, profile = %(profile)s)") | 260 _("find gateways (target = %(target)s, profile = %(profile)s)") |
261 % {"target": target.full(), "profile": profile} | 261 % {"target": target.full(), "profile": profile} |
262 ) | 262 ) |
263 d = client.disco.requestItems(target) | 263 d = client.disco.requestItems(target) |
264 d.addCallback(self._itemsReceived, target=target, client=client) | 264 d.addCallback(self._items_received, target=target, client=client) |
265 return d | 265 return d |