core: style improvments (90 chars limit)

This commit is contained in:
Goffi 2018-07-29 18:44:27 +02:00
commit ff79fa3ce0
3 changed files with 219 additions and 196 deletions

View file

@ -17,7 +17,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# XXX: thanks to Darrik L Mazey for his documentation (https://blog.darmasoft.net/2013/06/30/using-pure-python-otr.html)
# XXX: thanks to Darrik L Mazey for his documentation
# (https://blog.darmasoft.net/2013/06/30/using-pure-python-otr.html)
# this implentation is based on it
from sat.core.i18n import _, D_
@ -52,10 +53,14 @@ NS_OTR = "otr_plugin"
PRIVATE_KEY = "PRIVATE KEY"
OTR_MENU = D_(u"OTR")
AUTH_TXT = D_(
u"To authenticate your correspondent, you need to give your below fingerprint *BY AN EXTERNAL CANAL* (i.e. not in this chat), and check that the one he gives you is the same as below. If there is a mismatch, there can be a spy between you!"
u"To authenticate your correspondent, you need to give your below fingerprint "
u"*BY AN EXTERNAL CANAL* (i.e. not in this chat), and check that the one he gives "
u"you is the same as below. If there is a mismatch, there can be a spy between you!"
)
DROP_TXT = D_(
u"You private key is used to encrypt messages for your correspondent, nobody except you must know it, if you are in doubt, you should drop it!\n\nAre you sure you want to drop your private key?"
u"You private key is used to encrypt messages for your correspondent, nobody except "
u"you must know it, if you are in doubt, you should drop it!\n\nAre you sure you "
u"want to drop your private key?"
)
# NO_LOG_AND = D_(u"/!\\Your history is not logged anymore, and") # FIXME: not used at the moment
NO_ADV_FEATURES = D_(u"Some of advanced features are disabled !")
@ -83,8 +88,8 @@ class Context(potr.context.Context):
"""Inject encrypted data in the stream
if appdata is not None, we are sending a message in sendMessageDataTrigger
stanza will be injected directly if appdata is None, else we just update the element
and follow normal workflow
stanza will be injected directly if appdata is None,
else we just update the element and follow normal workflow
@param msg_str(str): encrypted message body
@param appdata(None, dict): None for signal message,
message data when an encrypted message is going to be sent
@ -137,7 +142,8 @@ class Context(potr.context.Context):
).format(trusted=trusted_str, other_jid=self.peer.full())
else:
feedback = D_(
u"{trusted} encrypted OTR conversation started with {other_jid}\n{extra_info}"
u"{trusted} encrypted OTR conversation started with {other_jid}\n"
u"{extra_info}"
).format(
trusted=trusted_str,
other_jid=self.peer.full(),
@ -165,13 +171,18 @@ class Context(potr.context.Context):
super(Context, self).disconnect()
def finish(self):
"""Finish the session - avoid to send any message but the user still has to end the session himself."""
"""Finish the session
avoid to send any message but the user still has to end the session himself.
"""
if self.state == potr.context.STATE_ENCRYPTED:
self.processTLVs([potr.proto.DisconnectTLV()])
class Account(potr.context.Account):
# TODO: manage trusted keys: if a fingerprint is not used anymore, we have no way to remove it from database yet (same thing for a correspondent jid)
# TODO: manage trusted keys: if a fingerprint is not used anymore,
# we have no way to remove it from database yet (same thing for a
# correspondent jid)
# TODO: manage explicit message encryption
def __init__(self, host, client):
@ -353,7 +364,8 @@ class OTR(object):
if not to_jid.resource:
to_jid.resource = self.host.memory.getMainResource(
client, to_jid
) # FIXME: temporary and unsecure, must be changed when frontends are refactored
) # FIXME: temporary and unsecure, must be changed when frontends
# are refactored
otrctx = client._otr_context_manager.getContextForUser(to_jid)
query = otrctx.sendMessage(0, "?OTRv?")
otrctx.inject(query)
@ -378,7 +390,8 @@ class OTR(object):
if not to_jid.resource:
to_jid.resource = self.host.memory.getMainResource(
client, to_jid
) # FIXME: temporary and unsecure, must be changed when frontends are refactored
) # FIXME: temporary and unsecure, must be changed when frontends
# are refactored
otrctx = client._otr_context_manager.getContextForUser(to_jid)
otrctx.disconnect()
return {}
@ -402,7 +415,8 @@ class OTR(object):
if not to_jid.resource:
to_jid.resource = self.host.memory.getMainResource(
client, to_jid
) # FIXME: temporary and unsecure, must be changed when frontends are refactored
) # FIXME: temporary and unsecure, must be changed when frontends
# are refactored
ctxMng = client._otr_context_manager
otrctx = ctxMng.getContextForUser(to_jid)
priv_key = ctxMng.account.privkey
@ -414,7 +428,8 @@ class OTR(object):
dialog_opt={
C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_MESSAGE,
C.XMLUI_DATA_MESS: _(
u"You have no private key yet, start an OTR conversation to have one"
u"You have no private key yet, start an OTR conversation to "
u"have one"
),
C.XMLUI_DATA_LVL: C.XMLUI_DATA_LVL_WARNING,
},
@ -431,7 +446,8 @@ class OTR(object):
dialog_opt={
C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_MESSAGE,
C.XMLUI_DATA_MESS: _(
u"Your fingerprint is:\n{fingerprint}\n\nStart an OTR conversation to have your correspondent one."
u"Your fingerprint is:\n{fingerprint}\n\n"
u"Start an OTR conversation to have your correspondent one."
).format(fingerprint=priv_key),
C.XMLUI_DATA_LVL: C.XMLUI_DATA_LVL_INFO,
},
@ -501,7 +517,8 @@ class OTR(object):
if not to_jid.resource:
to_jid.resource = self.host.memory.getMainResource(
client, to_jid
) # FIXME: temporary and unsecure, must be changed when frontends are refactored
) # FIXME: temporary and unsecure, must be changed when frontends
# are refactored
except KeyError:
log.error(_(u"jid key is not present !"))
return defer.fail(exceptions.DataError)
@ -518,7 +535,8 @@ class OTR(object):
for context in ctxMng.contexts.values():
context.disconnect()
ctxMng.account.privkey = None
ctxMng.account.getPrivkey() # as account.privkey is None, getPrivkey will generate a new key, and save it
ctxMng.account.getPrivkey() # as account.privkey is None, getPrivkey
# will generate a new key, and save it
return {
"xmlui": xml_tools.note(
D_(u"Your private key has been dropped")
@ -550,14 +568,14 @@ class OTR(object):
encrypted = False
if otrctx.state == potr.context.STATE_ENCRYPTED:
log.warning(
u"Received unencrypted message in an encrypted context (from {jid})".format(
jid=from_jid.full()
)
u"Received unencrypted message in an encrypted context (from {jid})"
.format(jid=from_jid.full())
)
feedback = (
D_(
u"WARNING: received unencrypted data in a supposedly encrypted context"
u"WARNING: received unencrypted data in a supposedly encrypted "
u"context"
),
)
client.feedback(from_jid, feedback)
@ -569,17 +587,21 @@ class OTR(object):
if encrypted:
if res[0] != None:
# decrypted messages handling.
# receiveMessage() will return a tuple, the first part of which will be the decrypted message
# receiveMessage() will return a tuple,
# the first part of which will be the decrypted message
data["message"] = {
"": res[0].decode("utf-8")
} # FIXME: Q&D fix for message refactoring, message is now a dict
try:
# we want to keep message in history, even if no store is requested in message hints
# we want to keep message in history, even if no store is
# requested in message hints
del data[u"history"]
except KeyError:
pass
# TODO: add skip history as an option, but by default we don't skip it
# data[u'history'] = C.HISTORY_SKIP # we send the decrypted message to frontends, but we don't want it in history
# data[u'history'] = C.HISTORY_SKIP # we send the decrypted message to
# frontends, but we don't want it in
# history
else:
log.warning(
u"An encrypted message was expected, but got {}".format(
@ -604,8 +626,10 @@ class OTR(object):
except StopIteration:
return data
if message.startswith(potr.proto.OTRTAG):
#  FIXME: it may be better to cancel the message and send it direclty to bridge
# this is used by Libervia, but this may send garbage message to other frontends
#  FIXME: it may be better to cancel the message and send it direclty to
# bridge
# this is used by Libervia, but this may send garbage message to
# other frontends
# if they are used at the same time as Libervia.
# Hard to avoid with decryption on Libervia though.
data[u"history"] = C.HISTORY_SKIP
@ -647,39 +671,38 @@ class OTR(object):
otrctx.sendMessage(0, unicode(body).encode("utf-8"), appdata=mess_data)
else:
feedback = D_(
u"Your message was not sent because your correspondent closed the encrypted conversation on his/her side. "
u"Your message was not sent because your correspondent closed the "
u"encrypted conversation on his/her side. "
u"Either close your own side, or refresh the session."
)
log.warning(_(u"Message discarded because closed encryption channel"))
client.feedback(to_jid, feedback)
raise failure.Failure(exceptions.CancelError(u"Cancelled by OTR plugin"))
def sendMessageTrigger(
self, client, mess_data, pre_xml_treatments, post_xml_treatments
):
def sendMessageTrigger(self, client, mess_data, pre_xml_treatments,
post_xml_treatments):
if mess_data["type"] == "groupchat":
return True
if (
client.profile in self.skipped_profiles
): #  FIXME: should not be done on a per-profile basis
if client.profile in self.skipped_profiles:
#  FIXME: should not be done on a per-profile basis
return True
to_jid = copy.copy(mess_data["to"])
if not to_jid.resource:
to_jid.resource = self.host.memory.getMainResource(
client, to_jid
) # FIXME: full jid may not be known
otrctx = client._otr_context_manager.getContextForUser(to_jid)
if otrctx.state != potr.context.STATE_PLAINTEXT:
self._p_hints.addHint(mess_data, self._p_hints.HINT_NO_COPY)
self._p_hints.addHint(mess_data, self._p_hints.HINT_NO_PERMANENT_STORE)
mess_data[
"OTR"
] = (
otrctx
) #  this indicate that encryption is needed in sendMessageData trigger
if not mess_data[
"to"
].resource: #  if not resource was given, we force it here
mess_data["OTR"] = (otrctx) #  this indicate that encryption is needed in
# sendMessageData trigger
if not mess_data["to"].resource:
# if not resource was given, we force it here
mess_data["to"] = to_jid
return True
@ -691,7 +714,8 @@ class OTR(object):
try:
entity.resource = self.host.memory.getMainResource(
client, entity
) # FIXME: temporary and unsecure, must be changed when frontends are refactored
) # FIXME: temporary and unsecure, must be changed when frontends
# are refactored
except exceptions.UnknownEntityError:
return True # entity was not connected
if entity in client._otr_context_manager.contexts:

View file

@ -520,16 +520,8 @@ class XEP_0060(object):
raise exceptions.DataError(u"Can't find Item in MAM message element")
return item_elt
def _getItems(
self,
service="",
node="",
max_items=10,
item_ids=None,
sub_id=None,
extra_dict=None,
profile_key=C.PROF_KEY_NONE,
):
def _getItems(self, service="", node="", max_items=10, item_ids=None, sub_id=None,
extra_dict=None, profile_key=C.PROF_KEY_NONE):
"""Get items from pubsub node
@param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit
@ -551,17 +543,8 @@ class XEP_0060(object):
d.addCallback(self.serItemsData)
return d
def getItems(
self,
client,
service,
node,
max_items=None,
item_ids=None,
sub_id=None,
rsm_request=None,
extra=None,
):
def getItems(self, client, service, node, max_items=None, item_ids=None, sub_id=None,
rsm_request=None, extra=None):
"""Retrieve pubsub items from a node.
@param service (JID, None): pubsub service.
@ -678,28 +661,15 @@ class XEP_0060(object):
# d_dict[publisher] = self.getItems(service, node, max_items, None, sub_id, rsm, client.profile)
# defer.returnValue(d_dict)
def getOptions(
self,
service,
nodeIdentifier,
subscriber,
subscriptionIdentifier=None,
profile_key=C.PROF_KEY_NONE,
):
def getOptions(self, service, nodeIdentifier, subscriber, subscriptionIdentifier=None,
profile_key=C.PROF_KEY_NONE):
client = self.host.getClient(profile_key)
return client.pubsub_client.getOptions(
service, nodeIdentifier, subscriber, subscriptionIdentifier
)
def setOptions(
self,
service,
nodeIdentifier,
subscriber,
options,
subscriptionIdentifier=None,
profile_key=C.PROF_KEY_NONE,
):
def setOptions(self, service, nodeIdentifier, subscriber, options,
subscriptionIdentifier=None, profile_key=C.PROF_KEY_NONE):
client = self.host.getClient(profile_key)
return client.pubsub_client.setOptions(
service, nodeIdentifier, subscriber, options, subscriptionIdentifier

View file

@ -72,11 +72,14 @@ class XEP_0163(object):
def addPEPEvent(self, event_type, node, in_callback, out_callback=None, notify=True):
"""Add a Personal Eventing Protocol event manager
@param event_type(unicode): type of the event (always uppercase), can be MOOD, TUNE, etc
@param node(unicode): namespace of the node (e.g. http://jabber.org/protocol/mood for User Mood)
@param event_type(unicode): type of the event (always uppercase),
can be MOOD, TUNE, etc
@param node(unicode): namespace of the node (e.g. http://jabber.org/protocol/mood
for User Mood)
@param in_callback(callable): method to call when this event occur
the callable will be called with (itemsEvent, profile) as arguments
@param out_callback(callable,None): method to call when we want to publish this event (must return a deferred)
@param out_callback(callable,None): method to call when we want to publish this
event (must return a deferred)
the callable will be called when sendPEPEvent is called
@param notify(bool): add autosubscribe (+notify) if True
"""
@ -116,7 +119,8 @@ class XEP_0163(object):
def PEPSend(self, event_type, data, profile_key=C.PROF_KEY_NONE):
"""Send personal event after checking the data is alright
@param event_type: type of event (eg: MOOD, TUNE), must be in self.pep_out_cb.keys()
@param event_type: type of event (eg: MOOD, TUNE),
must be in self.pep_out_cb.keys()
@param data: dict of {string:string} of event_type dependant data
@param profile_key: profile who send the event
"""