code style reformatting using black

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

View file

@ -19,6 +19,7 @@
from sat.core.i18n import _, D_
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core import exceptions
from sat.core.constants import Const as C
@ -27,6 +28,7 @@ from twisted.words.protocols.jabber.error import StanzaError
from twisted.internet import defer
from wokkel import disco, iwokkel
from zope.interface import implements
try:
from twisted.words.protocols.xmlstream import XMPPHandler
except ImportError:
@ -36,7 +38,7 @@ except ImportError:
PARAM_CATEGORY = "Misc"
PARAM_NAME = "carbon"
PARAM_LABEL = D_(u"Message carbons")
NS_CARBONS = 'urn:xmpp:carbons:2'
NS_CARBONS = "urn:xmpp:carbons:2"
PLUGIN_INFO = {
C.PI_NAME: u"XEP-0280 Plugin",
@ -46,12 +48,12 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: [],
C.PI_MAIN: u"XEP_0280",
C.PI_HANDLER: u"yes",
C.PI_DESCRIPTION: D_(u"""Implementation of Message Carbons""")
C.PI_DESCRIPTION: D_(u"""Implementation of Message Carbons"""),
}
class XEP_0280(object):
# TODO: param is only checked at profile connection
#  TODO: param is only checked at profile connection
# activate carbons on param change even after profile connection
# TODO: chat state notifications are not handled yet (and potentially other XEPs?)
@ -64,11 +66,11 @@ class XEP_0280(object):
</individual>
</params>
""".format(
category_name = PARAM_CATEGORY,
category_label = D_(PARAM_CATEGORY),
param_name = PARAM_NAME,
param_label = PARAM_LABEL,
)
category_name=PARAM_CATEGORY,
category_label=D_(PARAM_CATEGORY),
param_name=PARAM_NAME,
param_label=PARAM_LABEL,
)
def __init__(self, host):
log.info(_("Plugin XEP_0280 initialization"))
@ -86,15 +88,17 @@ class XEP_0280(object):
(in particular end 2 end encryption plugins)
@param message_elt(domish.Element): <message> stanza
"""
if message_elt.name != u'message':
if message_elt.name != u"message":
log.error(u"addPrivateElt must be used with <message> stanzas")
return
message_elt.addElement((NS_CARBONS, u'private'))
message_elt.addElement((NS_CARBONS, u"private"))
@defer.inlineCallbacks
def profileConnected(self, client):
"""activate message carbons on connection if possible and activated in config"""
activate = self.host.memory.getParamA(PARAM_NAME, PARAM_CATEGORY, profile_key=client.profile)
activate = self.host.memory.getParamA(
PARAM_NAME, PARAM_CATEGORY, profile_key=client.profile
)
if not activate:
log.info(_(u"Not activating message carbons as requested in params"))
return
@ -105,7 +109,7 @@ class XEP_0280(object):
else:
log.info(_(u"message carbons available, enabling it"))
iq_elt = client.IQ()
iq_elt.addElement((NS_CARBONS, 'enable'))
iq_elt.addElement((NS_CARBONS, "enable"))
try:
yield iq_elt.send()
except StanzaError as e:
@ -126,40 +130,45 @@ class XEP_0280(object):
# we continue normal behaviour
return True
if message_elt['from'] != client.jid.userhost():
log.warning(u"The message carbon received is not from our server, hack attempt?\n{xml}".format(
xml = message_elt.toXml(),
))
if message_elt["from"] != client.jid.userhost():
log.warning(
u"The message carbon received is not from our server, hack attempt?\n{xml}".format(
xml=message_elt.toXml()
)
)
return
forwarded_elt = next(carbons_elt.elements(C.NS_FORWARD, 'forwarded'))
cc_message_elt = next(forwarded_elt.elements(C.NS_CLIENT, 'message'))
if carbons_elt.name == 'received':
forwarded_elt = next(carbons_elt.elements(C.NS_FORWARD, "forwarded"))
cc_message_elt = next(forwarded_elt.elements(C.NS_CLIENT, "message"))
if carbons_elt.name == "received":
# on receive we replace the wrapping message with the CCed one
# and continue the normal behaviour
message_elt['from'] = cc_message_elt['from']
message_elt["from"] = cc_message_elt["from"]
del message_elt.children[:]
for c in cc_message_elt.children:
message_elt.addChild(c)
return True
elif carbons_elt.name == 'sent':
elif carbons_elt.name == "sent":
# on send we parse the message and just add it to history
# and send it to frontends (without normal sending treatments)
mess_data = SatMessageProtocol.parseMessage(cc_message_elt, client)
if not mess_data['message'] and not mess_data['subject']:
if not mess_data["message"] and not mess_data["subject"]:
return False
client.messageAddToHistory(mess_data)
client.messageSendToBridge(mess_data)
else:
log.warning(u"invalid message carbons received:\n{xml}".format(
xml = message_elt.toXml()))
log.warning(
u"invalid message carbons received:\n{xml}".format(
xml=message_elt.toXml()
)
)
return False
class XEP_0280_handler(XMPPHandler):
implements(iwokkel.IDisco)
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
return [disco.DiscoFeature(NS_CARBONS)]
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
def getDiscoItems(self, requestor, target, nodeIdentifier=""):
return []