code style reformatting using black

This commit is contained in:
Goffi 2018-06-27 20:14:46 +02:00
commit 744cb0c503

View file

@ -22,6 +22,7 @@ from sat.core.constants import Const as C
from sat.core import exceptions
from sat.tools import xml_tools
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.words.protocols.jabber import jid
from twisted.internet import reactor, defer
@ -33,37 +34,58 @@ PLUGIN_INFO = {
C.PI_PROTOCOLS: ["XEP-0100"],
C.PI_DEPENDENCIES: ["XEP-0077"],
C.PI_MAIN: "XEP_0100",
C.PI_DESCRIPTION: _("""Implementation of Gateways protocol""")
C.PI_DESCRIPTION: _("""Implementation of Gateways protocol"""),
}
WARNING_MSG = D_(u"""Be careful ! Gateways allow you to use an external IM (legacy IM), so you can see your contact as XMPP contacts.
But when you do this, all your messages go throught the external legacy IM server, it is a huge privacy issue (i.e.: all your messages throught the gateway can be monitored, recorded, analysed by the external server, most of time a private company).""")
WARNING_MSG = D_(
u"""Be careful ! Gateways allow you to use an external IM (legacy IM), so you can see your contact as XMPP contacts.
But when you do this, all your messages go throught the external legacy IM server, it is a huge privacy issue (i.e.: all your messages throught the gateway can be monitored, recorded, analysed by the external server, most of time a private company)."""
)
GATEWAY_TIMEOUT = 10 # time to wait before cancelling a gateway disco info, in seconds
TYPE_DESCRIPTIONS = {'irc': D_("Internet Relay Chat"),
'xmpp': D_("XMPP"),
'qq': D_("Tencent QQ"),
'simple': D_("SIP/SIMPLE"),
'icq': D_("ICQ"),
'yahoo': D_("Yahoo! Messenger"),
'gadu-gadu': D_("Gadu-Gadu"),
'aim': D_("AOL Instant Messenger"),
'msn': D_("Windows Live Messenger"),
}
TYPE_DESCRIPTIONS = {
"irc": D_("Internet Relay Chat"),
"xmpp": D_("XMPP"),
"qq": D_("Tencent QQ"),
"simple": D_("SIP/SIMPLE"),
"icq": D_("ICQ"),
"yahoo": D_("Yahoo! Messenger"),
"gadu-gadu": D_("Gadu-Gadu"),
"aim": D_("AOL Instant Messenger"),
"msn": D_("Windows Live Messenger"),
}
class XEP_0100(object):
def __init__(self, host):
log.info(_("Gateways plugin initialization"))
self.host = host
self.__gateways = {} # dict used to construct the answer to findGateways. Key = target jid
host.bridge.addMethod("findGateways", ".plugin", in_sign='ss', out_sign='s', method=self._findGateways)
host.bridge.addMethod("gatewayRegister", ".plugin", in_sign='ss', out_sign='s', method=self._gatewayRegister)
host.bridge.addMethod(
"findGateways",
".plugin",
in_sign="ss",
out_sign="s",
method=self._findGateways,
)
host.bridge.addMethod(
"gatewayRegister",
".plugin",
in_sign="ss",
out_sign="s",
method=self._gatewayRegister,
)
self.__menu_id = host.registerCallback(self._gatewaysMenu, with_data=True)
self.__selected_id = host.registerCallback(self._gatewaySelectedCb, with_data=True)
host.importMenu((D_("Service"), D_("Gateways")), self._gatewaysMenu, security_limit=1, help_string=D_("Find gateways"))
self.__selected_id = host.registerCallback(
self._gatewaySelectedCb, with_data=True
)
host.importMenu(
(D_("Service"), D_("Gateways")),
self._gatewaysMenu,
security_limit=1,
help_string=D_("Find gateways"),
)
def _gatewaysMenu(self, data, profile):
""" XMLUI activated by menu: return Gateways UI
@ -72,24 +94,31 @@ class XEP_0100(object):
"""
client = self.host.getClient(profile)
try:
jid_ = jid.JID(data.get(xml_tools.formEscape('external_jid'), client.jid.host))
jid_ = jid.JID(
data.get(xml_tools.formEscape("external_jid"), client.jid.host)
)
except RuntimeError:
raise exceptions.DataError(_("Invalid JID"))
d = self.findGateways(jid_, profile)
d.addCallback(self._gatewaysResult2XMLUI, jid_)
d.addCallback(lambda xmlui: {'xmlui': xmlui.toXml()})
d.addCallback(lambda xmlui: {"xmlui": xmlui.toXml()})
return d
def _gatewaysResult2XMLUI(self, result, entity):
xmlui = xml_tools.XMLUI(title=_('Gateways manager (%s)') % entity.full())
xmlui = xml_tools.XMLUI(title=_("Gateways manager (%s)") % entity.full())
xmlui.addText(_(WARNING_MSG))
xmlui.addDivider('dash')
adv_list = xmlui.changeContainer('advanced_list', columns=3, selectable='single', callback_id=self.__selected_id)
xmlui.addDivider("dash")
adv_list = xmlui.changeContainer(
"advanced_list",
columns=3,
selectable="single",
callback_id=self.__selected_id,
)
for success, gateway_data in result:
if not success:
fail_cond, disco_item = gateway_data
xmlui.addJid(disco_item.entity)
xmlui.addText(_('Failed (%s)') % fail_cond)
xmlui.addText(_("Failed (%s)") % fail_cond)
xmlui.addEmpty()
else:
jid_, data = gateway_data
@ -100,22 +129,22 @@ class XEP_0100(object):
xmlui.addText(name)
xmlui.addText(self._getIdentityDesc(identity))
adv_list.end()
xmlui.addDivider('blank')
xmlui.changeContainer('advanced_list', columns=3)
xmlui.addLabel(_('Use external XMPP server'))
xmlui.addString('external_jid')
xmlui.addButton(self.__menu_id, _(u'Go !'), fields_back=('external_jid',))
xmlui.addDivider("blank")
xmlui.changeContainer("advanced_list", columns=3)
xmlui.addLabel(_("Use external XMPP server"))
xmlui.addString("external_jid")
xmlui.addButton(self.__menu_id, _(u"Go !"), fields_back=("external_jid",))
return xmlui
def _gatewaySelectedCb(self, data, profile):
try:
target_jid = jid.JID(data['index'])
target_jid = jid.JID(data["index"])
except (KeyError, RuntimeError):
log.warning(_("No gateway index selected"))
return {}
d = self.gatewayRegister(target_jid, profile)
d.addCallback(lambda xmlui: {'xmlui': xmlui.toXml()})
d.addCallback(lambda xmlui: {"xmlui": xmlui.toXml()})
return d
def _getIdentityDesc(self, identity):
@ -124,8 +153,13 @@ class XEP_0100(object):
"""
category, type_ = identity
if category != 'gateway':
log.error(_(u'INTERNAL ERROR: identity category should always be "gateway" in _getTypeString, got "%s"') % category)
if category != "gateway":
log.error(
_(
u'INTERNAL ERROR: identity category should always be "gateway" in _getTypeString, got "%s"'
)
% category
)
try:
return _(TYPE_DESCRIPTIONS[type_])
except KeyError:
@ -145,8 +179,10 @@ class XEP_0100(object):
def gatewayRegister(self, target_jid, profile_key=C.PROF_KEY_NONE):
"""Register gateway using in-band registration, then log-in to gateway"""
profile = self.host.memory.getProfileName(profile_key)
assert(profile)
d = self.host.plugins["XEP-0077"].inBandRegister(target_jid, self._registrationSuccessful, profile)
assert profile
d = self.host.plugins["XEP-0077"].inBandRegister(
target_jid, self._registrationSuccessful, profile
)
return d
def _infosReceived(self, dl_result, items, target, client):
@ -165,12 +201,27 @@ class XEP_0100(object):
ret.append((success, (msg, items[idx])))
else:
entity = items[idx].entity
gateways = [(identity, result.identities[identity]) for identity in result.identities if identity[0] == 'gateway']
gateways = [
(identity, result.identities[identity])
for identity in result.identities
if identity[0] == "gateway"
]
if gateways:
log.info(_(u"Found gateway [%(jid)s]: %(identity_name)s") % {'jid': entity.full(), 'identity_name': ' - '.join([gateway[1] for gateway in gateways])})
log.info(
_(u"Found gateway [%(jid)s]: %(identity_name)s")
% {
"jid": entity.full(),
"identity_name": " - ".join(
[gateway[1] for gateway in gateways]
),
}
)
ret.append((success, (entity, gateways)))
else:
log.info(_(u"Skipping [%(jid)s] which is not a gateway") % {'jid': entity.full()})
log.info(
_(u"Skipping [%(jid)s] which is not a gateway")
% {"jid": entity.full()}
)
return ret
def _itemsReceived(self, disco, target, client):
@ -185,7 +236,9 @@ class XEP_0100(object):
log.debug(_(u"item found: %s") % item.entity)
_defers.append(client.disco.requestInfo(item.entity))
dl = defer.DeferredList(_defers)
dl.addCallback(self._infosReceived, items=disco._items, target=target, client=client)
dl.addCallback(
self._infosReceived, items=disco._items, target=target, client=client
)
reactor.callLater(GATEWAY_TIMEOUT, dl.cancel)
return dl
@ -203,7 +256,10 @@ class XEP_0100(object):
"""Find gateways in the target JID, using discovery protocol
"""
client = self.host.getClient(profile)
log.debug(_(u"find gateways (target = %(target)s, profile = %(profile)s)") % {'target': target.full(), 'profile': profile})
log.debug(
_(u"find gateways (target = %(target)s, profile = %(profile)s)")
% {"target": target.full(), "profile": profile}
)
d = client.disco.requestItems(target)
d.addCallback(self._itemsReceived, target=target, client=client)
return d