Mercurial > libervia-backend
diff sat/plugins/plugin_xep_0249.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 003b8b4b56a7 |
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0249.py Wed Jun 27 07:51:29 2018 +0200 +++ b/sat/plugins/plugin_xep_0249.py Wed Jun 27 20:14:46 2018 +0200 @@ -21,6 +21,7 @@ from sat.core.constants import Const as C from sat.core import exceptions from sat.core.log import getLogger + log = getLogger(__name__) from sat.tools import xml_tools from twisted.words.xish import domish @@ -36,8 +37,8 @@ except ImportError: from wokkel.subprotocols import XMPPHandler -MESSAGE = '/message' -NS_DIRECT_MUC_INVITATION = 'jabber:x:conference' +MESSAGE = "/message" +NS_DIRECT_MUC_INVITATION = "jabber:x:conference" DIRECT_MUC_INVITATION_REQUEST = MESSAGE + '/x[@xmlns="' + NS_DIRECT_MUC_INVITATION + '"]' AUTOJOIN_KEY = "Misc" AUTOJOIN_NAME = "Auto-join MUC on invitation" @@ -52,7 +53,7 @@ C.PI_RECOMMENDATIONS: [C.TEXT_CMDS], C.PI_MAIN: "XEP_0249", C.PI_HANDLER: "yes", - C.PI_DESCRIPTION: _("""Implementation of Direct MUC Invitations""") + C.PI_DESCRIPTION: _("""Implementation of Direct MUC Invitations"""), } @@ -69,20 +70,26 @@ </individual> </params> """ % { - 'category_name': AUTOJOIN_KEY, - 'category_label': _("Misc"), - 'param_name': AUTOJOIN_NAME, - 'param_label': _("Auto-join MUC on invitation"), - 'param_options': '\n'.join(['<option value="%s" %s/>' % \ - (value, 'selected="true"' if value == AUTOJOIN_VALUES[0] else '') \ - for value in AUTOJOIN_VALUES]) + "category_name": AUTOJOIN_KEY, + "category_label": _("Misc"), + "param_name": AUTOJOIN_NAME, + "param_label": _("Auto-join MUC on invitation"), + "param_options": "\n".join( + [ + '<option value="%s" %s/>' + % (value, 'selected="true"' if value == AUTOJOIN_VALUES[0] else "") + for value in AUTOJOIN_VALUES + ] + ), } def __init__(self, host): log.info(_("Plugin XEP_0249 initialization")) self.host = host host.memory.updateParams(self.params) - host.bridge.addMethod("inviteMUC", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._invite) + host.bridge.addMethod( + "inviteMUC", ".plugin", in_sign="ssa{ss}s", out_sign="", method=self._invite + ) try: self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) except KeyError: @@ -99,7 +106,7 @@ @param roomId: name of the room @param profile_key: %(doc_profile_key)s """ - #TODO: check parameters validity + # TODO: check parameters validity client = self.host.getClient(profile_key) self.invite(client, jid.JID(guest_jid_s), jid.JID(room_jid_s, options)) @@ -110,16 +117,16 @@ @param room(jid.JID): jid of the room where the user is invited @param options(dict): attribute with extra info (reason, password) as in #XEP-0249 """ - message = domish.Element((None, 'message')) + message = domish.Element((None, "message")) message["to"] = guest.full() - x_elt = message.addElement((NS_DIRECT_MUC_INVITATION, 'x')) - x_elt['jid'] = room.userhost() + x_elt = message.addElement((NS_DIRECT_MUC_INVITATION, "x")) + x_elt["jid"] = room.userhost() for key, value in options.iteritems(): - if key not in ('password', 'reason', 'thread'): + if key not in ("password", "reason", "thread"): log.warning(u"Ignoring invalid invite option: {}".format(key)) continue x_elt[key] = value - # there is not body in this message, so we can use directly send() + # there is not body in this message, so we can use directly send() client.send(message) def _accept(self, room_jid, profile_key=C.PROF_KEY_NONE): @@ -128,7 +135,10 @@ @param room (jid.JID): JID of the room """ client = self.host.getClient(profile_key) - log.info(_(u'Invitation accepted for room %(room)s [%(profile)s]') % {'room': room_jid.userhost(), 'profile': client.profile}) + log.info( + _(u"Invitation accepted for room %(room)s [%(profile)s]") + % {"room": room_jid.userhost(), "profile": client.profile} + ) d = self.host.plugins["XEP-0045"].join(client, room_jid, client.jid.user, {}) return d @@ -140,10 +150,13 @@ """ client = self.host.getClient(profile) try: - room_jid_s = message.firstChildElement()['jid'] - log.info(_(u'Invitation received for room %(room)s [%(profile)s]') % {'room': room_jid_s, 'profile': profile}) + room_jid_s = message.firstChildElement()["jid"] + log.info( + _(u"Invitation received for room %(room)s [%(profile)s]") + % {"room": room_jid_s, "profile": profile} + ) except: - log.error(_('Error while parsing invitation')) + log.error(_("Error while parsing invitation")) return from_jid_s = message["from"] room_jid = jid.JID(room_jid_s) @@ -152,21 +165,32 @@ except exceptions.NotFound: pass else: - log.info(_(u"Invitation silently discarded because user is already in the room.")) + log.info( + _(u"Invitation silently discarded because user is already in the room.") + ) return - autojoin = self.host.memory.getParamA(AUTOJOIN_NAME, AUTOJOIN_KEY, profile_key=profile) + autojoin = self.host.memory.getParamA( + AUTOJOIN_NAME, AUTOJOIN_KEY, profile_key=profile + ) if autojoin == "always": self._accept(room_jid, profile) elif autojoin == "never": - msg = D_("An invitation from %(user)s to join the room %(room)s has been declined according to your personal settings.") % {'user': from_jid_s, 'room': room_jid_s} + msg = D_( + "An invitation from %(user)s to join the room %(room)s has been declined according to your personal settings." + ) % {"user": from_jid_s, "room": room_jid_s} title = D_("MUC invitation") xml_tools.quickNote(self.host, client, msg, title, C.XMLUI_DATA_LVL_INFO) else: # leave the default value here - confirm_msg = D_("You have been invited by %(user)s to join the room %(room)s. Do you accept?") % {'user': from_jid_s, 'room': room_jid_s} + confirm_msg = D_( + "You have been invited by %(user)s to join the room %(room)s. Do you accept?" + ) % {"user": from_jid_s, "room": room_jid_s} confirm_title = D_("MUC invitation") - d = xml_tools.deferConfirm(self.host, confirm_msg, confirm_title, profile=profile) + d = xml_tools.deferConfirm( + self.host, confirm_msg, confirm_title, profile=profile + ) + def accept_cb(accepted): if accepted: self._accept(room_jid, profile) @@ -184,7 +208,9 @@ try: contact_jid = jid.JID(contact_jid_s) except (RuntimeError, jid.InvalidFormat, AttributeError): - feedback = _(u"You must provide a valid JID to invite, like in '/invite contact@{host}'").format(host=my_host) + feedback = _( + u"You must provide a valid JID to invite, like in '/invite contact@{host}'" + ).format(host=my_host) self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) return False if not contact_jid.user: @@ -201,10 +227,14 @@ self.host = plugin_parent.host def connectionInitialized(self): - self.xmlstream.addObserver(DIRECT_MUC_INVITATION_REQUEST, self.plugin_parent.onInvitation, profile=self.parent.profile) + self.xmlstream.addObserver( + DIRECT_MUC_INVITATION_REQUEST, + self.plugin_parent.onInvitation, + profile=self.parent.profile, + ) - def getDiscoInfo(self, requestor, target, nodeIdentifier=''): + def getDiscoInfo(self, requestor, target, nodeIdentifier=""): return [disco.DiscoFeature(NS_DIRECT_MUC_INVITATION)] - def getDiscoItems(self, requestor, target, nodeIdentifier=''): + def getDiscoItems(self, requestor, target, nodeIdentifier=""): return []