2018-09-05 02:59:34 +02:00
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim 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.
|
|
|
|
#
|
|
|
|
# Gajim 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 should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2008-06-02 01:33:51 +02:00
|
|
|
|
|
|
|
'''
|
2008-06-03 01:15:08 +02:00
|
|
|
Plug-in management related classes.
|
2008-06-02 01:33:51 +02:00
|
|
|
|
|
|
|
:author: Mateusz Biliński <mateusz@bilinski.it>
|
2008-06-18 22:45:22 +02:00
|
|
|
:since: 30th May 2008
|
2008-06-02 01:33:51 +02:00
|
|
|
:copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
|
|
|
|
:license: GPL
|
|
|
|
'''
|
|
|
|
|
|
|
|
__all__ = ['PluginManager']
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import fnmatch
|
2010-09-14 19:31:35 +02:00
|
|
|
import zipfile
|
|
|
|
from shutil import rmtree
|
2013-01-02 13:54:02 +01:00
|
|
|
import configparser
|
2016-08-20 00:39:45 +02:00
|
|
|
from pkg_resources import parse_version
|
2008-06-02 01:33:51 +02:00
|
|
|
|
2018-05-01 00:36:42 +02:00
|
|
|
import gajim
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import nec
|
2018-04-25 20:49:37 +02:00
|
|
|
from gajim.common import configpaths
|
2018-08-24 21:39:03 +02:00
|
|
|
from gajim.common import modules
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.exceptions import PluginsystemError
|
2008-06-02 01:33:51 +02:00
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.plugins.helpers import log, log_calls, Singleton
|
|
|
|
from gajim.plugins.helpers import GajimPluginActivateException
|
|
|
|
from gajim.plugins.gajimplugin import GajimPlugin, GajimPluginException
|
2008-06-02 01:33:51 +02:00
|
|
|
|
2013-01-27 19:51:11 +01:00
|
|
|
class PluginManager(metaclass=Singleton):
|
2010-04-08 01:20:17 +02:00
|
|
|
'''
|
|
|
|
Main plug-in management class.
|
|
|
|
|
|
|
|
Currently:
|
|
|
|
- scans for plugins
|
|
|
|
- activates them
|
2010-09-18 23:00:43 +02:00
|
|
|
- handles GUI extension points, when called by GUI objects after
|
|
|
|
plugin is activated (by dispatching info about call to handlers
|
|
|
|
in plugins)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
:todo: add more info about how GUI extension points work
|
|
|
|
:todo: add list of available GUI extension points
|
|
|
|
:todo: implement mechanism to dynamically load plugins where GUI extension
|
|
|
|
points have been already called (i.e. when plugin is activated
|
|
|
|
after GUI object creation). [DONE?]
|
2018-06-21 01:46:16 +02:00
|
|
|
:todo: implement mechanism to dynamically deactivate plugins (call plugin's
|
2010-04-08 01:20:17 +02:00
|
|
|
deactivation handler) [DONE?]
|
|
|
|
:todo: when plug-in is deactivated all GUI extension points are removed
|
|
|
|
from `PluginManager.gui_extension_points_handlers`. But when
|
2010-09-18 23:00:43 +02:00
|
|
|
object that invoked GUI extension point is abandoned by Gajim,
|
|
|
|
eg. closed ChatControl object, the reference to called GUI
|
|
|
|
extension points is still in `PluginManager.gui_extension_points`
|
|
|
|
These should be removed, so that object can be destroyed by
|
|
|
|
Python.
|
2010-04-08 01:20:17 +02:00
|
|
|
Possible solution: add call to clean up method in classes
|
|
|
|
'destructors' (classes that register GUI extension points)
|
|
|
|
'''
|
|
|
|
|
|
|
|
__metaclass__ = Singleton
|
|
|
|
|
|
|
|
#@log_calls('PluginManager')
|
|
|
|
def __init__(self):
|
|
|
|
self.plugins = []
|
|
|
|
'''
|
|
|
|
Detected plugin classes.
|
|
|
|
|
|
|
|
Each class object in list is `GajimPlugin` subclass.
|
|
|
|
|
|
|
|
:type: [] of class objects
|
|
|
|
'''
|
|
|
|
self.active_plugins = []
|
|
|
|
'''
|
|
|
|
Instance objects of active plugins.
|
|
|
|
|
|
|
|
These are object instances of classes held `plugins`, but only those
|
|
|
|
that were activated.
|
|
|
|
|
|
|
|
:type: [] of `GajimPlugin` based objects
|
|
|
|
'''
|
|
|
|
self.gui_extension_points = {}
|
|
|
|
'''
|
|
|
|
Registered GUI extension points.
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.gui_extension_points_handlers = {}
|
|
|
|
'''
|
|
|
|
Registered handlers of GUI extension points.
|
|
|
|
'''
|
2017-04-13 22:58:51 +02:00
|
|
|
|
|
|
|
self.encryption_plugins = {}
|
|
|
|
'''
|
|
|
|
Registered names with instances of encryption Plugins.
|
|
|
|
'''
|
|
|
|
|
2018-04-25 20:49:37 +02:00
|
|
|
for path in reversed(configpaths.get('PLUGINS_DIRS')):
|
2010-09-19 09:59:44 +02:00
|
|
|
pc = PluginManager.scan_dir_for_plugins(path)
|
2010-09-19 10:03:25 +02:00
|
|
|
self.add_plugins(pc)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
@log_calls('PluginManager')
|
|
|
|
def _plugin_has_entry_in_global_config(self, plugin):
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.config.get_per('plugins', plugin.short_name) is None:
|
2010-04-08 01:20:17 +02:00
|
|
|
return False
|
2018-09-18 10:14:04 +02:00
|
|
|
return True
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
@log_calls('PluginManager')
|
|
|
|
def _create_plugin_entry_in_global_config(self, plugin):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.config.add_per('plugins', plugin.short_name)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2010-09-17 21:16:17 +02:00
|
|
|
def _remove_plugin_entry_in_global_config(self, plugin):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.config.del_per('plugins', plugin.short_name)
|
2010-09-17 21:16:17 +02:00
|
|
|
|
2017-11-15 22:26:55 +01:00
|
|
|
@log_calls('PluginManager')
|
|
|
|
def init_plugins(self):
|
|
|
|
self._activate_all_plugins_from_global_config()
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
@log_calls('PluginManager')
|
|
|
|
def add_plugin(self, plugin_class):
|
|
|
|
'''
|
|
|
|
:todo: what about adding plug-ins that are already added? Module reload
|
|
|
|
and adding class from reloaded module or ignoring adding plug-in?
|
|
|
|
'''
|
|
|
|
plugin = plugin_class()
|
|
|
|
|
|
|
|
if plugin not in self.plugins:
|
|
|
|
if not self._plugin_has_entry_in_global_config(plugin):
|
|
|
|
self._create_plugin_entry_in_global_config(plugin)
|
|
|
|
|
|
|
|
self.plugins.append(plugin)
|
|
|
|
plugin.active = False
|
|
|
|
else:
|
2018-09-17 18:57:00 +02:00
|
|
|
log.info('Not loading plugin %s v%s from module %s '
|
|
|
|
'(identified by short name: %s). Plugin already loaded.',
|
|
|
|
plugin.name, plugin.version,
|
|
|
|
plugin.__module__, plugin.short_name)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2018-04-25 21:55:54 +02:00
|
|
|
@log_calls('PluginManager')
|
|
|
|
def remove_plugin(self, plugin):
|
|
|
|
'''
|
2018-09-18 13:38:22 +02:00
|
|
|
removes the plugin from the plugin list and deletes all loaded modules
|
2018-04-25 21:55:54 +02:00
|
|
|
from sys. This way we will have a fresh start when the plugin gets added
|
2018-06-21 01:46:16 +02:00
|
|
|
again.
|
2018-04-25 21:55:54 +02:00
|
|
|
'''
|
|
|
|
if plugin.active:
|
|
|
|
self.deactivate_plugin(plugin)
|
2018-09-18 13:38:22 +02:00
|
|
|
|
2018-04-25 21:55:54 +02:00
|
|
|
self.plugins.remove(plugin)
|
2018-09-18 13:38:22 +02:00
|
|
|
|
2018-04-25 21:55:54 +02:00
|
|
|
# remove modules from cache
|
2018-09-18 13:38:22 +02:00
|
|
|
base_package = plugin.__module__.split('.')[0]
|
2018-04-25 21:55:54 +02:00
|
|
|
# get the subpackages/-modules of the base_package. Add a dot to the
|
|
|
|
# name to avoid name problems (removing module_abc if base_package is
|
|
|
|
# module_ab)
|
|
|
|
modules_to_remove = [module for module in sys.modules
|
|
|
|
if module.startswith('{}.'.format(base_package))]
|
|
|
|
# remove the base_package itself
|
|
|
|
if base_package in sys.modules:
|
|
|
|
modules_to_remove.append(base_package)
|
2018-09-18 13:38:22 +02:00
|
|
|
|
2018-04-25 21:55:54 +02:00
|
|
|
for module_to_remove in modules_to_remove:
|
2018-09-18 13:38:22 +02:00
|
|
|
del sys.modules[module_to_remove]
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
@log_calls('PluginManager')
|
|
|
|
def add_plugins(self, plugin_classes):
|
|
|
|
for plugin_class in plugin_classes:
|
|
|
|
self.add_plugin(plugin_class)
|
|
|
|
|
2011-09-23 19:00:32 +02:00
|
|
|
@log_calls('PluginManager')
|
|
|
|
def get_active_plugin(self, plugin_name):
|
|
|
|
for plugin in self.active_plugins:
|
|
|
|
if plugin.short_name == plugin_name:
|
|
|
|
return plugin
|
|
|
|
return None
|
|
|
|
|
2017-06-27 20:51:55 +02:00
|
|
|
@log_calls('PluginManager')
|
|
|
|
def extension_point(self, gui_extpoint_name, *args):
|
|
|
|
'''
|
|
|
|
Invokes all handlers (from plugins) for a particular extension point, but
|
2018-06-21 01:46:16 +02:00
|
|
|
doesn't add it to collection for further processing.
|
2017-06-27 20:51:55 +02:00
|
|
|
For example if you pass a message for encryption via extension point to a
|
|
|
|
plugin, its undesired that the call is stored and replayed on activating the
|
|
|
|
plugin. For example after an update.
|
|
|
|
|
|
|
|
:param gui_extpoint_name: name of GUI extension point.
|
|
|
|
:type gui_extpoint_name: str
|
|
|
|
:param args: parameters to be passed to extension point handlers
|
|
|
|
(typically and object that invokes `gui_extension_point`;
|
|
|
|
however, this can be practically anything)
|
|
|
|
:type args: tuple
|
|
|
|
'''
|
|
|
|
|
|
|
|
self._execute_all_handlers_of_gui_extension_point(gui_extpoint_name,
|
|
|
|
*args)
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
@log_calls('PluginManager')
|
|
|
|
def gui_extension_point(self, gui_extpoint_name, *args):
|
|
|
|
'''
|
|
|
|
Invokes all handlers (from plugins) for particular GUI extension point
|
2010-09-18 23:00:43 +02:00
|
|
|
and adds it to collection for further processing (eg. by plugins not
|
|
|
|
active yet).
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
:param gui_extpoint_name: name of GUI extension point.
|
2013-01-01 21:06:16 +01:00
|
|
|
:type gui_extpoint_name: str
|
2010-04-08 01:20:17 +02:00
|
|
|
:param args: parameters to be passed to extension point handlers
|
2010-09-18 23:00:43 +02:00
|
|
|
(typically and object that invokes `gui_extension_point`;
|
|
|
|
however, this can be practically anything)
|
2010-04-08 01:20:17 +02:00
|
|
|
:type args: tuple
|
|
|
|
|
|
|
|
:todo: GUI extension points must be documented well - names with
|
|
|
|
parameters that will be passed to handlers (in plugins). Such
|
|
|
|
documentation must be obeyed both in core and in plugins. This
|
|
|
|
is a loosely coupled approach and is pretty natural in Python.
|
|
|
|
|
|
|
|
:bug: what if only some handlers are successfully connected? we should
|
|
|
|
revert all those connections that where successfully made. Maybe
|
|
|
|
call 'self._deactivate_plugin()' or sth similar.
|
2010-09-18 23:00:43 +02:00
|
|
|
Looking closer - we only rewrite tuples here. Real check should
|
|
|
|
be made in method that invokes gui_extpoints handlers.
|
2010-04-08 01:20:17 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
self._add_gui_extension_point_call_to_list(gui_extpoint_name, *args)
|
2010-09-18 23:00:43 +02:00
|
|
|
self._execute_all_handlers_of_gui_extension_point(gui_extpoint_name,
|
|
|
|
*args)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
@log_calls('PluginManager')
|
|
|
|
def remove_gui_extension_point(self, gui_extpoint_name, *args):
|
|
|
|
'''
|
|
|
|
Removes GUI extension point from collection held by `PluginManager`.
|
|
|
|
|
|
|
|
From this point this particular extension point won't be visible
|
|
|
|
to plugins (eg. it won't invoke any handlers when plugin is activated).
|
|
|
|
|
|
|
|
GUI extension point is removed completely (there is no way to recover it
|
|
|
|
from inside `PluginManager`).
|
|
|
|
|
|
|
|
Removal is needed when instance object that given extension point was
|
|
|
|
connect with is destroyed (eg. ChatControl is closed or context menu
|
|
|
|
is hidden).
|
|
|
|
|
|
|
|
Each `PluginManager.gui_extension_point` call should have a call of
|
|
|
|
`PluginManager.remove_gui_extension_point` related to it.
|
|
|
|
|
|
|
|
:note: in current implementation different arguments mean different
|
|
|
|
extension points. The same arguments and the same name mean
|
|
|
|
the same extension point.
|
|
|
|
:todo: instead of using argument to identify which extpoint should be
|
2010-09-18 23:00:43 +02:00
|
|
|
removed, maybe add additional 'id' argument - this would work
|
|
|
|
similar hash in Python objects. 'id' would be calculated based
|
|
|
|
on arguments passed or on anything else (even could be constant)
|
|
|
|
This would give core developers (that add new extpoints) more
|
|
|
|
freedom, but is this necessary?
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
:param gui_extpoint_name: name of GUI extension point.
|
2013-01-01 21:06:16 +01:00
|
|
|
:type gui_extpoint_name: str
|
2010-04-08 01:20:17 +02:00
|
|
|
:param args: arguments that `PluginManager.gui_extension_point` was
|
|
|
|
called with for this extension point. This is used (along with
|
|
|
|
extension point name) to identify element to be removed.
|
|
|
|
:type args: tuple
|
|
|
|
'''
|
|
|
|
if gui_extpoint_name in self.gui_extension_points:
|
2011-09-26 19:47:33 +02:00
|
|
|
extension_points = list(self.gui_extension_points[gui_extpoint_name])
|
|
|
|
for ext_point in extension_points:
|
|
|
|
if args[0] in ext_point:
|
|
|
|
self.gui_extension_points[gui_extpoint_name].remove(
|
|
|
|
ext_point)
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
@log_calls('PluginManager')
|
|
|
|
def _add_gui_extension_point_call_to_list(self, gui_extpoint_name, *args):
|
|
|
|
'''
|
|
|
|
Adds GUI extension point call to list of calls.
|
|
|
|
|
|
|
|
This is done only if such call hasn't been added already
|
|
|
|
(same extension point name and same arguments).
|
|
|
|
|
|
|
|
:note: This is assumption that GUI extension points are different only
|
|
|
|
if they have different name or different arguments.
|
|
|
|
|
|
|
|
:param gui_extpoint_name: GUI extension point name used to identify it
|
|
|
|
by plugins.
|
|
|
|
:type gui_extpoint_name: str
|
|
|
|
|
|
|
|
:param args: parameters to be passed to extension point handlers
|
2010-09-18 23:00:43 +02:00
|
|
|
(typically and object that invokes `gui_extension_point`;
|
|
|
|
however, this can be practically anything)
|
2010-04-08 01:20:17 +02:00
|
|
|
:type args: tuple
|
|
|
|
|
|
|
|
'''
|
|
|
|
if ((gui_extpoint_name not in self.gui_extension_points)
|
2010-09-18 23:00:43 +02:00
|
|
|
or (args not in self.gui_extension_points[gui_extpoint_name])):
|
2018-09-18 12:06:01 +02:00
|
|
|
self.gui_extension_points.setdefault(gui_extpoint_name, []).append(
|
2010-09-18 23:00:43 +02:00
|
|
|
args)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
@log_calls('PluginManager')
|
2010-09-18 23:00:43 +02:00
|
|
|
def _execute_all_handlers_of_gui_extension_point(self, gui_extpoint_name,
|
|
|
|
*args):
|
2010-04-08 01:20:17 +02:00
|
|
|
if gui_extpoint_name in self.gui_extension_points_handlers:
|
2010-09-18 23:00:43 +02:00
|
|
|
for handlers in self.gui_extension_points_handlers[
|
|
|
|
gui_extpoint_name]:
|
2014-03-09 10:19:24 +01:00
|
|
|
try:
|
|
|
|
handlers[0](*args)
|
2018-09-17 21:11:45 +02:00
|
|
|
except Exception:
|
2018-09-17 18:57:00 +02:00
|
|
|
log.warning('Error executing %s',
|
|
|
|
handlers[0], exc_info=True)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def _register_events_handlers_in_ged(self, plugin):
|
2013-01-02 13:54:02 +01:00
|
|
|
for event_name, handler in plugin.events_handlers.items():
|
2010-04-08 01:20:17 +02:00
|
|
|
priority = handler[0]
|
|
|
|
handler_function = handler[1]
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler(event_name, priority,
|
2010-09-18 23:00:43 +02:00
|
|
|
handler_function)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def _remove_events_handler_from_ged(self, plugin):
|
2013-01-02 13:54:02 +01:00
|
|
|
for event_name, handler in plugin.events_handlers.items():
|
2010-04-08 01:20:17 +02:00
|
|
|
priority = handler[0]
|
|
|
|
handler_function = handler[1]
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.remove_event_handler(event_name, priority,
|
2010-09-18 23:00:43 +02:00
|
|
|
handler_function)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def _register_network_events_in_nec(self, plugin):
|
|
|
|
for event_class in plugin.events:
|
|
|
|
setattr(event_class, 'plugin', plugin)
|
|
|
|
if issubclass(event_class, nec.NetworkIncomingEvent):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.register_incoming_event(event_class)
|
2010-04-08 01:20:17 +02:00
|
|
|
elif issubclass(event_class, nec.NetworkOutgoingEvent):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.register_outgoing_event(event_class)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def _remove_network_events_from_nec(self, plugin):
|
|
|
|
for event_class in plugin.events:
|
|
|
|
if issubclass(event_class, nec.NetworkIncomingEvent):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.unregister_incoming_event(event_class)
|
2010-04-08 01:20:17 +02:00
|
|
|
elif issubclass(event_class, nec.NetworkOutgoingEvent):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.unregister_outgoing_event(event_class)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2017-04-13 22:58:51 +02:00
|
|
|
def _remove_name_from_encryption_plugins(self, plugin):
|
|
|
|
if plugin.encryption_name:
|
|
|
|
del self.encryption_plugins[plugin.encryption_name]
|
|
|
|
|
2018-08-24 21:39:03 +02:00
|
|
|
def _register_modules_with_handlers(self, plugin):
|
|
|
|
if not hasattr(plugin, 'modules'):
|
|
|
|
return
|
|
|
|
for con in app.connections.values():
|
|
|
|
for module in plugin.modules:
|
|
|
|
instance, name = module.get_instance(con)
|
|
|
|
if not module.zeroconf and con.name == 'Local':
|
|
|
|
continue
|
|
|
|
modules.register_single(con, instance, name)
|
|
|
|
|
|
|
|
# If handlers have been registered, register the
|
|
|
|
# plugin handlers. Otherwise this will be done
|
|
|
|
# automatically on connecting
|
|
|
|
# in connection_handlers._register_handlers()
|
|
|
|
if con.handlers_registered:
|
|
|
|
for handler in instance.handlers:
|
|
|
|
con.connection.RegisterHandler(*handler)
|
|
|
|
|
|
|
|
def _unregister_modules_with_handlers(self, plugin):
|
|
|
|
if not hasattr(plugin, 'modules'):
|
|
|
|
return
|
|
|
|
for con in app.connections.values():
|
|
|
|
for module in plugin.modules:
|
|
|
|
instance = con.get_module(module.name)
|
|
|
|
modules.unregister_single(con, module.name)
|
|
|
|
|
|
|
|
# Account is still connected and handlers are registered
|
|
|
|
# So just unregister the plugin handlers
|
|
|
|
if con.handlers_registered:
|
|
|
|
for handler in instance.handlers:
|
|
|
|
con.connection.UnregisterHandler(*handler)
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
@log_calls('PluginManager')
|
|
|
|
def activate_plugin(self, plugin):
|
|
|
|
'''
|
|
|
|
:param plugin: plugin to be activated
|
|
|
|
:type plugin: class object of `GajimPlugin` subclass
|
|
|
|
'''
|
2012-04-30 21:04:37 +02:00
|
|
|
if not plugin.active and plugin.activatable:
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
self._add_gui_extension_points_handlers_from_plugin(plugin)
|
2017-04-13 22:58:51 +02:00
|
|
|
self._add_encryption_name_from_plugin(plugin)
|
2010-04-08 01:20:17 +02:00
|
|
|
self._handle_all_gui_extension_points_with_plugin(plugin)
|
|
|
|
self._register_events_handlers_in_ged(plugin)
|
|
|
|
self._register_network_events_in_nec(plugin)
|
2018-08-24 21:39:03 +02:00
|
|
|
self._register_modules_with_handlers(plugin)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2010-11-01 21:22:43 +01:00
|
|
|
self.active_plugins.append(plugin)
|
|
|
|
try:
|
2010-04-08 01:20:17 +02:00
|
|
|
plugin.activate()
|
2013-01-01 23:18:36 +01:00
|
|
|
except GajimPluginException as e:
|
2010-11-01 21:22:43 +01:00
|
|
|
self.deactivate_plugin(plugin)
|
|
|
|
raise GajimPluginActivateException(str(e))
|
|
|
|
self._set_plugin_active_in_global_config(plugin)
|
|
|
|
plugin.active = True
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def deactivate_plugin(self, plugin):
|
|
|
|
# remove GUI extension points handlers (provided by plug-in) from
|
|
|
|
# handlers list
|
|
|
|
for gui_extpoint_name, gui_extpoint_handlers in \
|
2013-01-02 13:54:02 +01:00
|
|
|
plugin.gui_extension_points.items():
|
2010-09-18 23:00:43 +02:00
|
|
|
self.gui_extension_points_handlers[gui_extpoint_name].remove(
|
|
|
|
gui_extpoint_handlers)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
# detaching plug-in from handler GUI extension points (calling
|
|
|
|
# cleaning up method that must be provided by plug-in developer
|
|
|
|
# for each handled GUI extension point)
|
|
|
|
for gui_extpoint_name, gui_extpoint_handlers in \
|
2013-01-02 13:54:02 +01:00
|
|
|
plugin.gui_extension_points.items():
|
2010-04-08 01:20:17 +02:00
|
|
|
if gui_extpoint_name in self.gui_extension_points:
|
2010-09-18 23:00:43 +02:00
|
|
|
for gui_extension_point_args in self.gui_extension_points[
|
|
|
|
gui_extpoint_name]:
|
2010-04-08 01:20:17 +02:00
|
|
|
handler = gui_extpoint_handlers[1]
|
|
|
|
if handler:
|
2014-03-09 10:19:24 +01:00
|
|
|
try:
|
|
|
|
handler(*gui_extension_point_args)
|
2018-09-17 18:57:00 +02:00
|
|
|
except Exception:
|
|
|
|
log.warning('Error executing %s',
|
|
|
|
handler, exc_info=True)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
self._remove_events_handler_from_ged(plugin)
|
|
|
|
self._remove_network_events_from_nec(plugin)
|
2017-04-13 22:58:51 +02:00
|
|
|
self._remove_name_from_encryption_plugins(plugin)
|
2018-08-24 21:39:03 +02:00
|
|
|
self._unregister_modules_with_handlers(plugin)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
# removing plug-in from active plug-ins list
|
|
|
|
plugin.deactivate()
|
|
|
|
self.active_plugins.remove(plugin)
|
|
|
|
self._set_plugin_active_in_global_config(plugin, False)
|
|
|
|
plugin.active = False
|
|
|
|
|
|
|
|
def _deactivate_all_plugins(self):
|
|
|
|
for plugin_object in self.active_plugins:
|
|
|
|
self.deactivate_plugin(plugin_object)
|
|
|
|
|
|
|
|
@log_calls('PluginManager')
|
|
|
|
def _add_gui_extension_points_handlers_from_plugin(self, plugin):
|
|
|
|
for gui_extpoint_name, gui_extpoint_handlers in \
|
2013-01-02 13:54:02 +01:00
|
|
|
plugin.gui_extension_points.items():
|
2010-09-18 23:00:43 +02:00
|
|
|
self.gui_extension_points_handlers.setdefault(gui_extpoint_name,
|
|
|
|
[]).append(gui_extpoint_handlers)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2017-04-13 22:58:51 +02:00
|
|
|
def _add_encryption_name_from_plugin(self, plugin):
|
|
|
|
if plugin.encryption_name:
|
|
|
|
self.encryption_plugins[plugin.encryption_name] = plugin
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
@log_calls('PluginManager')
|
|
|
|
def _handle_all_gui_extension_points_with_plugin(self, plugin):
|
|
|
|
for gui_extpoint_name, gui_extpoint_handlers in \
|
2013-01-02 13:54:02 +01:00
|
|
|
plugin.gui_extension_points.items():
|
2010-04-08 01:20:17 +02:00
|
|
|
if gui_extpoint_name in self.gui_extension_points:
|
2010-09-18 23:00:43 +02:00
|
|
|
for gui_extension_point_args in self.gui_extension_points[
|
|
|
|
gui_extpoint_name]:
|
2010-04-08 01:20:17 +02:00
|
|
|
handler = gui_extpoint_handlers[0]
|
|
|
|
if handler:
|
2014-03-09 10:19:24 +01:00
|
|
|
try:
|
|
|
|
handler(*gui_extension_point_args)
|
2018-09-17 18:57:00 +02:00
|
|
|
except Exception:
|
|
|
|
log.warning('Error executing %s',
|
|
|
|
handler, exc_info=True)
|
2014-03-09 10:19:24 +01:00
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
@log_calls('PluginManager')
|
|
|
|
def _activate_all_plugins(self):
|
|
|
|
'''
|
|
|
|
Activates all plugins in `plugins`.
|
|
|
|
|
|
|
|
Activated plugins are appended to `active_plugins` list.
|
|
|
|
'''
|
|
|
|
for plugin in self.plugins:
|
2010-11-01 21:22:43 +01:00
|
|
|
try:
|
|
|
|
self.activate_plugin(plugin)
|
|
|
|
except GajimPluginActivateException:
|
|
|
|
pass
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def _activate_all_plugins_from_global_config(self):
|
|
|
|
for plugin in self.plugins:
|
2012-04-30 21:04:37 +02:00
|
|
|
if self._plugin_is_active_in_global_config(plugin) and \
|
|
|
|
plugin.activatable:
|
2010-11-01 21:22:43 +01:00
|
|
|
try:
|
|
|
|
self.activate_plugin(plugin)
|
|
|
|
except GajimPluginActivateException:
|
|
|
|
pass
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2018-08-24 21:39:03 +02:00
|
|
|
def register_modules_for_account(self, con):
|
|
|
|
'''
|
|
|
|
A new account has been added, register modules
|
|
|
|
of all active plugins
|
|
|
|
'''
|
|
|
|
for plugin in self.plugins:
|
|
|
|
if not plugin.active:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not hasattr(plugin, 'modules'):
|
|
|
|
return
|
|
|
|
|
|
|
|
for module in plugin.modules:
|
|
|
|
instance, name = module.get_instance(con)
|
|
|
|
if not module.zeroconf and con.name == 'Local':
|
|
|
|
continue
|
|
|
|
modules.register_single(con, instance, name)
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
def _plugin_is_active_in_global_config(self, plugin):
|
2017-08-13 13:18:56 +02:00
|
|
|
return app.config.get_per('plugins', plugin.short_name, 'active')
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def _set_plugin_active_in_global_config(self, plugin, active=True):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.config.set_per('plugins', plugin.short_name, 'active', active)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@log_calls('PluginManager')
|
2017-02-23 17:56:19 +01:00
|
|
|
def scan_dir_for_plugins(path, scan_dirs=True, package=False):
|
2017-01-01 22:48:44 +01:00
|
|
|
r'''
|
2010-04-08 01:20:17 +02:00
|
|
|
Scans given directory for plugin classes.
|
|
|
|
|
|
|
|
:param path: directory to scan for plugins
|
2013-01-01 21:06:16 +01:00
|
|
|
:type path: str
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2017-02-23 17:56:19 +01:00
|
|
|
:param scan_dirs: folders inside path are processed as modules
|
|
|
|
:type scan_dirs: boolean
|
|
|
|
|
|
|
|
:param package: if path points to a single package folder
|
|
|
|
:type package: boolean
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
:return: list of found plugin classes (subclasses of `GajimPlugin`
|
|
|
|
:rtype: [] of class objects
|
|
|
|
|
|
|
|
:note: currently it only searches for plugin classes in '\*.py' files
|
2018-06-21 01:46:16 +02:00
|
|
|
present in given directory `path` (no recursion here)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
:todo: add scanning zipped modules
|
|
|
|
'''
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.plugins.plugins_i18n import _
|
2010-04-08 01:20:17 +02:00
|
|
|
plugins_found = []
|
2013-01-02 13:54:02 +01:00
|
|
|
conf = configparser.ConfigParser()
|
2010-09-18 23:00:43 +02:00
|
|
|
fields = ('name', 'short_name', 'version', 'description', 'authors',
|
|
|
|
'homepage')
|
|
|
|
if not os.path.isdir(path):
|
2010-09-19 10:03:25 +02:00
|
|
|
return plugins_found
|
2010-09-18 23:00:43 +02:00
|
|
|
|
2017-02-23 17:56:19 +01:00
|
|
|
if package:
|
|
|
|
path, package_name = os.path.split(path)
|
|
|
|
dir_list = [package_name]
|
|
|
|
else:
|
|
|
|
dir_list = os.listdir(path)
|
2010-09-18 23:00:43 +02:00
|
|
|
|
|
|
|
sys.path.insert(0, path)
|
|
|
|
|
|
|
|
for elem_name in dir_list:
|
|
|
|
file_path = os.path.join(path, elem_name)
|
|
|
|
|
|
|
|
if os.path.isfile(file_path) and fnmatch.fnmatch(file_path, '*.py'):
|
|
|
|
module_name = os.path.splitext(elem_name)[0]
|
|
|
|
elif os.path.isdir(file_path) and scan_dirs:
|
|
|
|
module_name = elem_name
|
|
|
|
file_path += os.path.sep
|
2017-02-23 17:56:19 +01:00
|
|
|
else:
|
|
|
|
continue
|
2010-09-20 07:08:47 +02:00
|
|
|
|
|
|
|
manifest_path = os.path.join(os.path.dirname(file_path),
|
|
|
|
'manifest.ini')
|
|
|
|
if scan_dirs and (not os.path.isfile(manifest_path)):
|
|
|
|
continue
|
|
|
|
|
2013-01-26 22:31:17 +01:00
|
|
|
# read metadata from manifest.ini
|
|
|
|
conf.remove_section('info')
|
2016-10-11 10:51:31 +02:00
|
|
|
with open(manifest_path, encoding='utf-8') as conf_file:
|
|
|
|
try:
|
|
|
|
conf.read_file(conf_file)
|
|
|
|
except configparser.Error:
|
2018-09-17 18:57:00 +02:00
|
|
|
log.warning('Plugin %s not loaded, error loading manifest',
|
|
|
|
elem_name, exc_info=True)
|
2016-10-11 10:51:31 +02:00
|
|
|
continue
|
2013-01-26 22:31:17 +01:00
|
|
|
|
2016-08-07 12:53:49 +02:00
|
|
|
min_v = conf.get('info', 'min_gajim_version', fallback=None)
|
|
|
|
max_v = conf.get('info', 'max_gajim_version', fallback=None)
|
|
|
|
|
2018-05-01 00:36:42 +02:00
|
|
|
gajim_v = gajim.__version__.split('+', 1)[0]
|
2016-08-20 00:39:45 +02:00
|
|
|
gajim_v_cmp = parse_version(gajim_v)
|
2013-01-26 22:31:17 +01:00
|
|
|
|
2016-08-20 00:39:45 +02:00
|
|
|
if min_v and gajim_v_cmp < parse_version(min_v):
|
2018-09-17 18:57:00 +02:00
|
|
|
log.warning('Plugin %s not loaded, newer version of'
|
|
|
|
'gajim required: %s < %s',
|
|
|
|
elem_name, gajim_v, min_v)
|
2016-08-07 12:53:49 +02:00
|
|
|
continue
|
2016-08-20 00:39:45 +02:00
|
|
|
if max_v and gajim_v_cmp > parse_version(max_v):
|
2018-09-17 18:57:00 +02:00
|
|
|
log.warning('Plugin %s not loaded, plugin incompatible '
|
|
|
|
'with current version of gajim: '
|
|
|
|
'%s > %s', elem_name, gajim_v, max_v)
|
2016-08-07 12:53:49 +02:00
|
|
|
continue
|
2013-01-26 22:31:17 +01:00
|
|
|
|
|
|
|
module = None
|
|
|
|
try:
|
2018-04-25 21:55:54 +02:00
|
|
|
log.info('Loading %s', module_name)
|
|
|
|
module = __import__(module_name)
|
2018-09-17 18:57:00 +02:00
|
|
|
except Exception:
|
2016-08-07 12:59:16 +02:00
|
|
|
log.warning(
|
2018-09-17 18:57:00 +02:00
|
|
|
'While trying to load %s, exception occurred',
|
|
|
|
elem_name, exc_info=True)
|
2016-08-07 12:59:16 +02:00
|
|
|
continue
|
2013-01-26 22:31:17 +01:00
|
|
|
|
|
|
|
if module is None:
|
|
|
|
continue
|
|
|
|
|
2010-09-18 23:00:43 +02:00
|
|
|
log.debug('Attributes processing started')
|
|
|
|
for module_attr_name in [attr_name for attr_name in dir(module)
|
|
|
|
if not (attr_name.startswith('__') or attr_name.endswith('__'))]:
|
|
|
|
module_attr = getattr(module, module_attr_name)
|
2018-09-17 18:57:00 +02:00
|
|
|
log.debug('%s: %s', module_attr_name, module_attr)
|
2010-09-18 23:00:43 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
if not issubclass(module_attr, GajimPlugin) or \
|
|
|
|
module_attr is GajimPlugin:
|
|
|
|
continue
|
|
|
|
log.debug('is subclass of GajimPlugin')
|
|
|
|
module_attr.__path__ = os.path.abspath(
|
|
|
|
os.path.dirname(file_path))
|
|
|
|
|
|
|
|
for option in fields:
|
2018-09-17 23:03:45 +02:00
|
|
|
if conf.get('info', option) == '':
|
2017-08-24 23:01:06 +02:00
|
|
|
raise configparser.NoOptionError(option, 'info')
|
2013-08-16 21:29:48 +02:00
|
|
|
if option == 'description':
|
|
|
|
setattr(module_attr, option, _(conf.get('info', option)))
|
|
|
|
continue
|
2010-09-18 23:00:43 +02:00
|
|
|
setattr(module_attr, option, conf.get('info', option))
|
|
|
|
|
|
|
|
plugins_found.append(module_attr)
|
2013-01-05 00:03:36 +01:00
|
|
|
except TypeError:
|
2011-09-04 22:23:38 +02:00
|
|
|
# set plugin localization
|
2012-01-07 09:00:04 +01:00
|
|
|
try:
|
|
|
|
module_attr._ = _
|
2013-01-05 00:03:36 +01:00
|
|
|
except AttributeError:
|
2012-01-07 09:00:04 +01:00
|
|
|
pass
|
2013-01-05 00:03:36 +01:00
|
|
|
except configparser.NoOptionError:
|
2010-09-18 23:00:43 +02:00
|
|
|
# all fields are required
|
2018-09-17 18:57:00 +02:00
|
|
|
log.debug(
|
|
|
|
'%s: wrong manifest file. all fields are required!',
|
|
|
|
module_attr_name)
|
2013-01-05 00:03:36 +01:00
|
|
|
except configparser.NoSectionError:
|
2011-10-16 21:40:42 +02:00
|
|
|
# info section are required
|
2018-09-17 18:57:00 +02:00
|
|
|
log.debug(
|
|
|
|
'%s: wrong manifest file. info section are required!',
|
|
|
|
module_attr_name)
|
2013-01-05 00:03:36 +01:00
|
|
|
except configparser.MissingSectionHeaderError:
|
2011-10-16 21:40:42 +02:00
|
|
|
# info section are required
|
2018-09-17 18:57:00 +02:00
|
|
|
log.debug('%s: wrong manifest file. section are required!',
|
|
|
|
module_attr_name)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2017-08-07 19:28:54 +02:00
|
|
|
sys.path.remove(path)
|
2010-04-08 01:20:17 +02:00
|
|
|
return plugins_found
|
2010-09-14 19:31:35 +02:00
|
|
|
|
|
|
|
def install_from_zip(self, zip_filename, owerwrite=None):
|
|
|
|
'''
|
2017-06-25 20:42:15 +02:00
|
|
|
Install plugin from zip and return plugin
|
2010-09-14 19:31:35 +02:00
|
|
|
'''
|
|
|
|
try:
|
|
|
|
zip_file = zipfile.ZipFile(zip_filename)
|
2013-01-05 00:03:36 +01:00
|
|
|
except zipfile.BadZipfile:
|
2010-09-14 19:31:35 +02:00
|
|
|
# it is not zip file
|
|
|
|
raise PluginsystemError(_('Archive corrupted'))
|
2013-01-05 00:03:36 +01:00
|
|
|
except IOError:
|
2010-09-14 19:31:35 +02:00
|
|
|
raise PluginsystemError(_('Archive empty'))
|
|
|
|
|
|
|
|
if zip_file.testzip():
|
|
|
|
# CRC error
|
|
|
|
raise PluginsystemError(_('Archive corrupted'))
|
|
|
|
|
|
|
|
dirs = []
|
2010-09-20 07:08:47 +02:00
|
|
|
manifest = None
|
2010-09-14 19:31:35 +02:00
|
|
|
for filename in zip_file.namelist():
|
|
|
|
if filename.startswith('.') or filename.startswith('/') or \
|
|
|
|
('/' not in filename):
|
|
|
|
# members not safe
|
|
|
|
raise PluginsystemError(_('Archive is malformed'))
|
|
|
|
if filename.endswith('/') and filename.find('/', 0, -1) < 0:
|
2017-06-25 20:42:15 +02:00
|
|
|
dirs.append(filename.strip('/'))
|
2010-09-20 07:08:47 +02:00
|
|
|
if 'manifest.ini' in filename.split('/')[1]:
|
|
|
|
manifest = True
|
|
|
|
if not manifest:
|
|
|
|
return
|
2010-09-14 19:31:35 +02:00
|
|
|
if len(dirs) > 1:
|
|
|
|
raise PluginsystemError(_('Archive is malformed'))
|
|
|
|
|
2018-04-25 21:55:54 +02:00
|
|
|
user_dir = configpaths.get('PLUGINS_USER')
|
2010-09-14 19:31:35 +02:00
|
|
|
plugin_dir = os.path.join(user_dir, dirs[0])
|
|
|
|
|
|
|
|
if os.path.isdir(plugin_dir):
|
2010-09-20 07:08:47 +02:00
|
|
|
# Plugin dir already exists
|
2010-09-14 19:31:35 +02:00
|
|
|
if not owerwrite:
|
|
|
|
raise PluginsystemError(_('Plugin already exists'))
|
2018-04-25 21:55:54 +02:00
|
|
|
self.uninstall_plugin(self.get_plugin_by_path(plugin_dir))
|
2010-09-14 19:31:35 +02:00
|
|
|
|
|
|
|
zip_file.extractall(user_dir)
|
|
|
|
zip_file.close()
|
2017-06-25 20:42:15 +02:00
|
|
|
|
|
|
|
plugins = self.scan_dir_for_plugins(plugin_dir, package=True)
|
2010-09-20 07:08:47 +02:00
|
|
|
if not plugins:
|
|
|
|
return
|
|
|
|
self.add_plugin(plugins[0])
|
2010-09-14 19:31:35 +02:00
|
|
|
plugin = self.plugins[-1]
|
|
|
|
return plugin
|
|
|
|
|
2018-04-25 21:55:54 +02:00
|
|
|
def uninstall_plugin(self, plugin):
|
2010-09-14 19:31:35 +02:00
|
|
|
'''
|
|
|
|
Deactivate and remove plugin from `plugins` list
|
|
|
|
'''
|
|
|
|
def on_error(func, path, error):
|
|
|
|
if func == os.path.islink:
|
|
|
|
# if symlink
|
|
|
|
os.unlink(path)
|
|
|
|
return
|
|
|
|
# access is denied or other
|
2010-09-21 21:44:04 +02:00
|
|
|
raise PluginsystemError(error[1][1])
|
2010-09-14 19:31:35 +02:00
|
|
|
|
2010-09-20 07:08:47 +02:00
|
|
|
if plugin:
|
2018-04-25 21:55:54 +02:00
|
|
|
self.remove_plugin(plugin)
|
2010-09-20 07:08:47 +02:00
|
|
|
rmtree(plugin.__path__, False, on_error)
|
|
|
|
if self._plugin_has_entry_in_global_config(plugin):
|
|
|
|
self._remove_plugin_entry_in_global_config(plugin)
|
2010-09-14 19:31:35 +02:00
|
|
|
|
|
|
|
def get_plugin_by_path(self, plugin_dir):
|
|
|
|
for plugin in self.plugins:
|
|
|
|
if plugin.__path__ in plugin_dir:
|
|
|
|
return plugin
|