gajim-otrplugin/plugin.py

120 lines
3.9 KiB
Python

# Copyright (C) 2019 Philipp Hörist <philipp AT hoerist.com>
# Copyright (C) 2019 Pavel R. <pd at narayana dot im>
# Copyright (C) 2022 Bohdan Horbeshko <bodqhrohro@gmail.com>
# 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 <http://www.gnu.org/licenses/>.
# TODO: Fingerprints authentication GUI
# TODO: SMP authentication GUI
ERROR = None
import logging
from gajim.plugins import GajimPlugin
from gajim.common import app
from gajim.common.modules.contacts import BareContact
from gajim.gtk.util import make_menu_item
from gi.repository import Gtk, Gio
log = logging.getLogger('gajim.p.otr')
try: from Cryptodome import *
except ImportError as e:
log.error(e)
ERROR = 'Cryptodome is missing'
if not ERROR:
try:
from . import module
except Exception as error:
log.error(error)
ERROR = 'Error: %s' % error
# ...
class OTRPlugin(GajimPlugin):
log = logging.getLogger('gajim.p.otr')
# init plugin #
def init(self):
self.activatable = (not ERROR)
if ERROR:
self.available_text = (ERROR)
return
self.encryption_name = 'OTR'
self.description = 'Provides Off-the-Record encryption'
self.modules = [module]
self.gui_extension_points = {
'encrypt' + self.encryption_name: (self._encrypt_message, None),
'message_actions_box': (self._message_actions_box_activate, self._message_actions_box_deactivate),
}
self._menuitem = None
@staticmethod
def _get_grid():
return app.window.get_chat_stack().get_message_action_box()
@staticmethod
def _get_model(grid):
return grid._ui.encryption_menu_button.get_menu_model()
def activate(self):
if app.window is not None:
grid = self._get_grid()
self._actions_hack_activate(grid)
def deactivate(self):
grid = self._get_grid()
self._actions_hack_deactivate(grid)
@staticmethod
def get_otr(account):
return app.connections[account].get_module('OTR')
# activate encryption #
@staticmethod
def activate_encryption(ctl):
return True
# encrypt file (just return same file)
@staticmethod
def encrypt_file(file, account, callback):
callback(file)
# encrypt message #
def _encrypt_message(self,con,event,callback):
if not event.message or event.type_ != 'chat': return # drop empty and non-chat messages
otr = self.get_otr(event.account)
otr.otr.encrypt(event,callback)
def _message_actions_box_activate(self, grid, box):
if self._menuitem is None:
self._actions_hack_activate(grid)
def _message_actions_box_deactivate(self, grid, box):
if self._menuitem is not None:
self._actions_hack_deactivate(grid)
def _actions_hack_activate(self, grid):
model = grid._ui.encryption_menu_button.get_menu_model()
menuitem = make_menu_item('OTR', 'win.set-encryption', '"OTR"')
self._menuitem = menuitem.get_attribute_value(Gio.MENU_ATTRIBUTE_LABEL)
model.append_item(menuitem)
def _actions_hack_deactivate(self, grid):
model = self._get_model(grid)
for i in range(model.get_n_items()):
if model.get_item_attribute_value(i, Gio.MENU_ATTRIBUTE_LABEL, None) == self._menuitem:
model.remove(i)
self._menuitem = None
break