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
|
|
|
|
|
|
|
'''
|
|
|
|
Base class for implementing plugin.
|
|
|
|
|
|
|
|
:author: Mateusz Biliński <mateusz@bilinski.it>
|
2008-06-18 22:45:22 +02:00
|
|
|
:since: 1st June 2008
|
2008-06-02 01:33:51 +02:00
|
|
|
:copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
|
|
|
|
:license: GPL
|
|
|
|
'''
|
|
|
|
|
2008-06-14 20:20:24 +02:00
|
|
|
import os
|
2010-09-10 21:31:25 +02:00
|
|
|
import locale
|
2008-06-14 20:20:24 +02:00
|
|
|
|
2018-04-25 20:49:37 +02:00
|
|
|
from gajim.common import configpaths
|
2008-07-05 18:44:27 +02:00
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.plugins.helpers import log_calls, log
|
|
|
|
from gajim.plugins.gui import GajimPluginConfigDialog
|
2008-06-02 01:33:51 +02:00
|
|
|
|
2011-10-10 16:43:22 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger('gajim.p.plugin')
|
|
|
|
|
2008-07-05 18:44:27 +02:00
|
|
|
|
2018-09-16 11:56:56 +02:00
|
|
|
class GajimPlugin:
|
2010-04-08 01:20:17 +02:00
|
|
|
'''
|
|
|
|
Base class for implementing Gajim plugins.
|
|
|
|
'''
|
2013-01-01 19:44:25 +01:00
|
|
|
name = ''
|
2010-04-08 01:20:17 +02:00
|
|
|
'''
|
|
|
|
Name of plugin.
|
|
|
|
|
|
|
|
Will be shown in plugins management GUI.
|
|
|
|
|
2013-01-01 21:06:16 +01:00
|
|
|
:type: str
|
2010-04-08 01:20:17 +02:00
|
|
|
'''
|
2013-01-01 19:44:25 +01:00
|
|
|
short_name = ''
|
2010-04-08 01:20:17 +02:00
|
|
|
'''
|
|
|
|
Short name of plugin.
|
|
|
|
|
2018-06-21 01:46:16 +02:00
|
|
|
Used for quick identification of plugin.
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2013-01-01 21:06:16 +01:00
|
|
|
:type: str
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
:todo: decide whether we really need this one, because class name (with
|
|
|
|
module name) can act as such short name
|
2017-04-13 22:58:51 +02:00
|
|
|
'''
|
|
|
|
encryption_name = ''
|
|
|
|
'''
|
|
|
|
Name of the encryption scheme.
|
|
|
|
|
|
|
|
The name that Gajim displays in the encryption menu.
|
|
|
|
Leave empty if the plugin is not an encryption plugin.
|
|
|
|
|
|
|
|
:type: str
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
'''
|
2013-01-01 19:44:25 +01:00
|
|
|
version = ''
|
2010-04-08 01:20:17 +02:00
|
|
|
'''
|
|
|
|
Version of plugin.
|
|
|
|
|
2013-01-01 21:06:16 +01:00
|
|
|
:type: str
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
:todo: decide how to compare version between each other (which one
|
|
|
|
is higher). Also rethink: do we really need to compare versions
|
|
|
|
of plugins between each other? This would be only useful if we detect
|
|
|
|
same plugin class but with different version and we want only the newest
|
|
|
|
one to be active - is such policy good?
|
|
|
|
'''
|
2013-01-01 19:44:25 +01:00
|
|
|
description = ''
|
2010-04-08 01:20:17 +02:00
|
|
|
'''
|
|
|
|
Plugin description.
|
|
|
|
|
2013-01-01 21:06:16 +01:00
|
|
|
:type: str
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
:todo: should be allow rich text here (like HTML or reStructuredText)?
|
|
|
|
'''
|
|
|
|
authors = []
|
|
|
|
'''
|
|
|
|
Plugin authors.
|
|
|
|
|
2013-01-01 21:06:16 +01:00
|
|
|
:type: [] of str
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
:todo: should we decide on any particular format of author strings?
|
|
|
|
Especially: should we force format of giving author's e-mail?
|
|
|
|
'''
|
2013-01-01 19:44:25 +01:00
|
|
|
homepage = ''
|
2010-04-08 01:20:17 +02:00
|
|
|
'''
|
|
|
|
URL to plug-in's homepage.
|
|
|
|
|
2013-01-01 21:06:16 +01:00
|
|
|
:type: str
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
:todo: should we check whether provided string is valid URI? (Maybe
|
|
|
|
using 'property')
|
|
|
|
'''
|
|
|
|
gui_extension_points = {}
|
|
|
|
'''
|
|
|
|
Extension points that plugin wants to connect with and handlers to be used.
|
|
|
|
|
|
|
|
Keys of this string should be strings with name of GUI extension point
|
|
|
|
to handles. Values should be 2-element tuples with references to handling
|
|
|
|
functions. First function will be used to connect plugin with extpoint,
|
2018-06-21 01:46:16 +02:00
|
|
|
the second one to successfully disconnect from it. Connecting takes places
|
2010-04-08 01:20:17 +02:00
|
|
|
when plugin is activated and extpoint already exists, or when plugin is
|
|
|
|
already activated but extpoint is being created (eg. chat window opens).
|
|
|
|
Disconnecting takes place when plugin is deactivated and extpoint exists
|
|
|
|
or when extpoint is destroyed and plugin is activate (eg. chat window
|
|
|
|
closed).
|
|
|
|
'''
|
|
|
|
config_default_values = {}
|
|
|
|
'''
|
|
|
|
Default values for keys that should be stored in plug-in config.
|
|
|
|
|
|
|
|
This dict is used when when someone calls for config option but it has not
|
|
|
|
been set yet.
|
|
|
|
|
|
|
|
Values are tuples: (default_value, option_description). The first one can
|
|
|
|
be anything (this is the advantage of using shelve/pickle instead of
|
2013-01-01 21:06:16 +01:00
|
|
|
custom-made config I/O handling); the second one should be str (gettext
|
2010-04-08 01:20:17 +02:00
|
|
|
can be used if need and/or translation is planned).
|
|
|
|
|
|
|
|
:type: {} of 2-element tuples
|
|
|
|
'''
|
|
|
|
events_handlers = {}
|
|
|
|
'''
|
|
|
|
Dictionary with events handlers.
|
|
|
|
|
|
|
|
Keys are event names. Values should be 2-element tuples with handler
|
|
|
|
priority as first element and reference to handler function as second
|
|
|
|
element. Priority is integer. See `ged` module for predefined priorities
|
|
|
|
like `ged.PRECORE`, `ged.CORE` or `ged.POSTCORE`.
|
|
|
|
|
|
|
|
:type: {} with 2-element tuples
|
|
|
|
'''
|
|
|
|
events = []
|
|
|
|
'''
|
|
|
|
New network event classes to be registered in Network Events Controller.
|
|
|
|
|
|
|
|
:type: [] of `nec.NetworkIncomingEvent` or `nec.NetworkOutgoingEvent`
|
|
|
|
subclasses.
|
|
|
|
'''
|
|
|
|
|
|
|
|
@log_calls('GajimPlugin')
|
|
|
|
def __init__(self):
|
|
|
|
self.config = GajimPluginConfig(self)
|
|
|
|
'''
|
|
|
|
Plug-in configuration dictionary.
|
|
|
|
|
|
|
|
Automatically saved and loaded and plug-in (un)load.
|
|
|
|
|
|
|
|
:type: `plugins.plugin.GajimPluginConfig`
|
|
|
|
'''
|
2012-04-30 00:43:18 +02:00
|
|
|
self.activatable = True
|
2012-04-30 21:04:37 +02:00
|
|
|
self.available_text = ''
|
2010-04-08 01:20:17 +02:00
|
|
|
self.load_config()
|
|
|
|
self.config_dialog = GajimPluginConfigDialog(self)
|
|
|
|
self.init()
|
|
|
|
|
|
|
|
@log_calls('GajimPlugin')
|
|
|
|
def save_config(self):
|
|
|
|
self.config.save()
|
|
|
|
|
|
|
|
@log_calls('GajimPlugin')
|
|
|
|
def load_config(self):
|
|
|
|
self.config.load()
|
|
|
|
|
|
|
|
def __eq__(self, plugin):
|
|
|
|
if self.short_name == plugin.short_name:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __ne__(self, plugin):
|
|
|
|
if self.short_name != plugin.short_name:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
@log_calls('GajimPlugin')
|
|
|
|
def local_file_path(self, file_name):
|
|
|
|
return os.path.join(self.__path__, file_name)
|
|
|
|
|
|
|
|
@log_calls('GajimPlugin')
|
|
|
|
def init(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@log_calls('GajimPlugin')
|
|
|
|
def activate(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@log_calls('GajimPlugin')
|
|
|
|
def deactivate(self):
|
|
|
|
pass
|
|
|
|
|
2013-01-02 13:54:02 +01:00
|
|
|
import pickle
|
2008-06-12 20:26:08 +02:00
|
|
|
|
2011-10-10 16:43:22 +02:00
|
|
|
class GajimPluginConfig():
|
2010-04-08 01:20:17 +02:00
|
|
|
@log_calls('GajimPluginConfig')
|
|
|
|
def __init__(self, plugin):
|
|
|
|
self.plugin = plugin
|
2017-03-03 00:34:22 +01:00
|
|
|
self.FILE_PATH = os.path.join(
|
2018-04-25 20:49:37 +02:00
|
|
|
configpaths.get('PLUGINS_CONFIG_DIR'), self.plugin.short_name)
|
2011-10-11 21:27:41 +02:00
|
|
|
self.data = {}
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
@log_calls('GajimPluginConfig')
|
|
|
|
def __getitem__(self, key):
|
|
|
|
if not key in self.data:
|
|
|
|
self.data[key] = self.plugin.config_default_values[key][0]
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
return self.data[key]
|
|
|
|
|
|
|
|
@log_calls('GajimPluginConfig')
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
self.data[key] = value
|
|
|
|
self.save()
|
|
|
|
|
2011-06-13 23:06:43 +02:00
|
|
|
@log_calls('GajimPluginConfig')
|
|
|
|
def __delitem__(self, key):
|
|
|
|
del self.data[key]
|
|
|
|
self.save()
|
|
|
|
|
2011-10-10 16:43:22 +02:00
|
|
|
@log_calls('GajimPluginConfig')
|
|
|
|
def __contains__(self, key):
|
|
|
|
return key in self.data
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for k in self.data.keys():
|
|
|
|
yield k
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
def keys(self):
|
|
|
|
return self.data.keys()
|
|
|
|
|
2011-10-10 21:06:30 +02:00
|
|
|
def items(self):
|
|
|
|
return self.data.items()
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
@log_calls('GajimPluginConfig')
|
|
|
|
def save(self):
|
2011-10-10 16:43:22 +02:00
|
|
|
fd = open(self.FILE_PATH, 'wb')
|
2013-01-02 13:54:02 +01:00
|
|
|
pickle.dump(self.data, fd)
|
2011-10-10 16:43:22 +02:00
|
|
|
fd.close()
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
@log_calls('GajimPluginConfig')
|
|
|
|
def load(self):
|
2011-10-10 21:06:30 +02:00
|
|
|
if os.path.isfile(self.FILE_PATH):
|
2011-10-10 16:43:22 +02:00
|
|
|
fd = open(self.FILE_PATH, 'rb')
|
|
|
|
try:
|
2013-01-02 13:54:02 +01:00
|
|
|
self.data = pickle.load(fd)
|
2011-10-10 16:43:22 +02:00
|
|
|
fd.close()
|
2018-09-16 23:45:01 +02:00
|
|
|
except Exception:
|
2011-10-10 16:43:22 +02:00
|
|
|
fd.close()
|
|
|
|
try:
|
|
|
|
import shelve
|
|
|
|
s = shelve.open(self.FILE_PATH)
|
2013-01-02 13:54:02 +01:00
|
|
|
for (k, v) in s.items():
|
2011-10-10 16:43:22 +02:00
|
|
|
self.data[k] = v
|
2012-08-22 20:21:26 +02:00
|
|
|
if not isinstance(self.data, dict):
|
|
|
|
raise GajimPluginException
|
2011-10-10 16:43:22 +02:00
|
|
|
s.close()
|
|
|
|
self.save()
|
2018-09-16 23:45:01 +02:00
|
|
|
except Exception:
|
2018-09-17 18:57:00 +02:00
|
|
|
enc = locale.getpreferredencoding()
|
|
|
|
filename = self.FILE_PATH.decode(enc) + '.bak'
|
|
|
|
log.warning(
|
|
|
|
'%s plugin config file not readable. Saving it as '
|
|
|
|
'%s and creating a new one',
|
|
|
|
self.plugin.short_name, filename)
|
2011-10-10 16:43:22 +02:00
|
|
|
if os.path.exists(self.FILE_PATH + '.bak'):
|
2011-10-11 21:44:02 +02:00
|
|
|
os.remove(self.FILE_PATH + '.bak')
|
|
|
|
os.rename(self.FILE_PATH, self.FILE_PATH + '.bak')
|
|
|
|
self.data = {}
|
|
|
|
self.save()
|
2011-10-10 16:43:22 +02:00
|
|
|
else:
|
|
|
|
self.data = {}
|
|
|
|
self.save()
|
|
|
|
|
2009-04-28 17:14:38 +02:00
|
|
|
|
|
|
|
class GajimPluginException(Exception):
|
2010-04-08 01:20:17 +02:00
|
|
|
pass
|
2009-04-28 17:14:38 +02:00
|
|
|
|
|
|
|
class GajimPluginInitError(GajimPluginException):
|
2010-04-08 01:20:17 +02:00
|
|
|
pass
|