# Copyright (C) 2019 Philipp Hörist # Copyright (C) 2019 Pavel R. # This file is part of Gajim OTR Plugin. # # Gajim OTR Plugin is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published # by the Free Software Foundation; version 3 only. # # This software is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You can always obtain full license text at . # TODO: Fingerprints authentication GUI # TODO: SMP authentication GUI import logging from gajim.plugins import GajimPlugin from gajim.common import app from otrplugin.otr import OTR class OTRPlugin(GajimPlugin): log = logging.getLogger('gajim.p.otr') def init(self): self.encryption_name = 'OTR' self.description = 'Provides Off-the-Record encryption' self.instances = {} self.get_instance = lambda acct: self.instances.get(acct) or self.instances.setdefault(acct,OTR(self,acct)) self.events_handlers = { 'before-change-show': (10, self._on_status_change), } self.gui_extension_points = { 'encrypt' + self.encryption_name: (self._encrypt_message, None), 'decrypt': (self._decrypt_message, None), } @staticmethod def activate_encryption(ctl): return True @staticmethod def encrypt_file(file, account, callback): callback(file) def _encrypt_message(self,con,event,callback): if not event.message or event.type_ != 'chat': return # drop empty and non-chat messages self.get_instance(event.conn.name).encrypt(event,callback) def _decrypt_message(self,con,event,callback): if (event.encrypted) or (event.name[0:2] == 'gc') or not (event.msgtxt or '').startswith('?OTR'): return # skip non-OTR messages.. if (event.name[0:3] == 'mam'): return setattr(event,'msgtxt','') # skip MAM messages (we can not decrypt OTR out of session).. if (app.config.get_per('encryption','%s-%s'%(event.conn.name,event.jid),'encryption')!=self.encryption_name): return # skip all when encryption not set to OTR self.get_instance(event.conn.name).decrypt(event,callback) def _on_status_change(self,event): if event.show == 'offline': for ctx in self.get_instance(event.conn.name).ctxs.values(): ctx.state and ctx.disconnect()