2018-07-07 13:52:44 +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/>.
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
from typing import Any
|
|
|
|
from typing import Dict # pylint: disable=unused-import
|
|
|
|
from typing import List
|
|
|
|
from typing import Tuple
|
|
|
|
|
2019-04-18 22:35:12 +02:00
|
|
|
import sys
|
2018-07-07 13:52:44 +02:00
|
|
|
import logging
|
|
|
|
from importlib import import_module
|
2018-07-26 00:12:04 +02:00
|
|
|
from unittest.mock import MagicMock
|
2018-07-07 13:52:44 +02:00
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
from gajim.common.types import ConnectionT
|
|
|
|
|
2018-07-07 13:52:44 +02:00
|
|
|
log = logging.getLogger('gajim.c.m')
|
|
|
|
|
2019-01-02 23:13:07 +01:00
|
|
|
ZEROCONF_MODULES = ['iq',
|
|
|
|
'adhoc_commands',
|
2018-12-23 22:33:10 +01:00
|
|
|
'receipts',
|
|
|
|
'discovery',
|
|
|
|
'chatstates']
|
2018-07-08 18:37:53 +02:00
|
|
|
|
2019-04-18 22:35:12 +02:00
|
|
|
MODULES = [
|
|
|
|
'adhoc_commands',
|
|
|
|
'annotations',
|
|
|
|
'bits_of_binary',
|
|
|
|
'blocking',
|
|
|
|
'bookmarks',
|
|
|
|
'caps',
|
|
|
|
'carbons',
|
|
|
|
'chatstates',
|
|
|
|
'delimiter',
|
|
|
|
'discovery',
|
|
|
|
'entity_time',
|
|
|
|
'gateway',
|
|
|
|
'httpupload',
|
|
|
|
'http_auth',
|
|
|
|
'iq',
|
|
|
|
'last_activity',
|
|
|
|
'mam',
|
|
|
|
'message',
|
|
|
|
'metacontacts',
|
|
|
|
'muc',
|
|
|
|
'pep',
|
|
|
|
'ping',
|
|
|
|
'presence',
|
|
|
|
'privacylists',
|
|
|
|
'pubsub',
|
|
|
|
'receipts',
|
|
|
|
'register',
|
|
|
|
'roster',
|
|
|
|
'roster_item_exchange',
|
|
|
|
'search',
|
|
|
|
'security_labels',
|
|
|
|
'software_version',
|
|
|
|
'user_activity',
|
|
|
|
'user_avatar',
|
|
|
|
'user_location',
|
|
|
|
'user_mood',
|
|
|
|
'user_nickname',
|
|
|
|
'user_tune',
|
|
|
|
'vcard_avatars',
|
|
|
|
'vcard_temp',
|
|
|
|
]
|
|
|
|
|
2018-09-11 20:00:34 +02:00
|
|
|
_imported_modules = [] # type: List[tuple]
|
2018-09-11 00:16:46 +02:00
|
|
|
_modules = {} # type: Dict[str, Dict[str, Any]]
|
2019-01-30 20:23:16 +01:00
|
|
|
_store_publish_modules = [
|
2019-01-31 00:02:25 +01:00
|
|
|
'UserMood',
|
|
|
|
'UserActivity',
|
2019-02-03 00:15:28 +01:00
|
|
|
'UserLocation',
|
|
|
|
'UserTune',
|
2019-01-31 00:02:25 +01:00
|
|
|
] # type: List[str]
|
2018-07-07 13:52:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ModuleMock:
|
2018-09-11 00:16:46 +02:00
|
|
|
def __init__(self, name: str) -> None:
|
2018-07-08 18:37:53 +02:00
|
|
|
self._name = name
|
|
|
|
|
2018-07-27 15:46:02 +02:00
|
|
|
# HTTPUpload, ..
|
2018-07-08 18:37:53 +02:00
|
|
|
self.available = False
|
|
|
|
|
|
|
|
# Blocking
|
2018-09-11 00:16:46 +02:00
|
|
|
self.blocked = [] # type: List[Any]
|
2018-07-08 18:37:53 +02:00
|
|
|
|
|
|
|
# Privacy Lists
|
2018-09-11 00:16:46 +02:00
|
|
|
self.blocked_contacts = [] # type: List[Any]
|
|
|
|
self.blocked_groups = [] # type: List[Any]
|
2018-07-08 18:37:53 +02:00
|
|
|
self.blocked_all = False
|
|
|
|
|
2018-07-27 15:46:02 +02:00
|
|
|
# Delimiter
|
|
|
|
self.delimiter = '::'
|
|
|
|
|
2018-07-20 23:17:05 +02:00
|
|
|
# Bookmarks
|
2018-09-11 00:16:46 +02:00
|
|
|
self.bookmarks = {} # type: Dict[Any, Any]
|
2018-07-20 23:17:05 +02:00
|
|
|
|
2018-07-22 12:18:24 +02:00
|
|
|
# Various Modules
|
|
|
|
self.supported = False
|
|
|
|
|
2018-09-11 00:16:46 +02:00
|
|
|
def __getattr__(self, key: str) -> MagicMock:
|
2018-07-26 00:12:04 +02:00
|
|
|
return MagicMock()
|
2018-07-07 13:52:44 +02:00
|
|
|
|
|
|
|
|
2019-04-18 22:35:12 +02:00
|
|
|
def register_modules(con: ConnectionT, *args: Any, **kwargs: Any) -> None:
|
2018-07-07 13:52:44 +02:00
|
|
|
if con in _modules:
|
|
|
|
return
|
|
|
|
_modules[con.name] = {}
|
2019-04-18 22:35:12 +02:00
|
|
|
for module_name in MODULES:
|
2018-07-08 18:37:53 +02:00
|
|
|
if con.name == 'Local':
|
2019-04-18 22:35:12 +02:00
|
|
|
if module_name not in ZEROCONF_MODULES:
|
2018-07-08 18:37:53 +02:00
|
|
|
continue
|
2019-04-18 22:35:12 +02:00
|
|
|
instance, name = _load_module(module_name, con, *args, **kwargs)
|
2018-07-07 13:52:44 +02:00
|
|
|
_modules[con.name][name] = instance
|
|
|
|
|
|
|
|
|
2019-04-18 22:35:12 +02:00
|
|
|
def register_single_module(con: ConnectionT, instance: Any, name: str) -> None:
|
2018-08-24 21:39:03 +02:00
|
|
|
if con.name not in _modules:
|
|
|
|
raise ValueError('Unknown account name: %s' % con.name)
|
|
|
|
_modules[con.name][name] = instance
|
|
|
|
|
|
|
|
|
2019-04-18 22:35:12 +02:00
|
|
|
def unregister_modules(con: ConnectionT) -> None:
|
2018-08-24 21:39:03 +02:00
|
|
|
for instance in _modules[con.name].values():
|
|
|
|
if hasattr(instance, 'cleanup'):
|
|
|
|
instance.cleanup()
|
2018-07-07 13:52:44 +02:00
|
|
|
del _modules[con.name]
|
|
|
|
|
|
|
|
|
2019-04-18 22:35:12 +02:00
|
|
|
def unregister_single_module(con: ConnectionT, name: str) -> None:
|
2018-08-24 21:39:03 +02:00
|
|
|
if con.name not in _modules:
|
|
|
|
return
|
|
|
|
if name not in _modules[con.name]:
|
|
|
|
return
|
|
|
|
del _modules[con.name][name]
|
|
|
|
|
|
|
|
|
2019-01-30 20:23:16 +01:00
|
|
|
def send_stored_publish(account: str) -> None:
|
|
|
|
for name in _store_publish_modules:
|
|
|
|
_modules[account][name].send_stored_publish()
|
|
|
|
|
|
|
|
|
2018-09-11 00:16:46 +02:00
|
|
|
def get(account: str, name: str) -> Any:
|
2018-07-07 13:52:44 +02:00
|
|
|
try:
|
|
|
|
return _modules[account][name]
|
|
|
|
except KeyError:
|
2018-07-08 18:37:53 +02:00
|
|
|
return ModuleMock(name)
|
2018-07-07 13:52:44 +02:00
|
|
|
|
|
|
|
|
2019-04-18 22:35:12 +02:00
|
|
|
def _load_module(name: str, con: ConnectionT, *args: Any, **kwargs: Any) -> Any:
|
|
|
|
if name not in MODULES:
|
|
|
|
raise ValueError('Module %s does not exist' % name)
|
|
|
|
module = sys.modules.get(name)
|
|
|
|
if module is None:
|
|
|
|
module = import_module('.%s' % name, package='gajim.common.modules')
|
|
|
|
return module.get_instance(con, *args, **kwargs) # type: ignore
|
|
|
|
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def get_handlers(con: ConnectionT) -> List[Tuple[Any, ...]]:
|
|
|
|
handlers = [] # type: List[Tuple[Any, ...]]
|
2018-07-07 13:52:44 +02:00
|
|
|
for module in _modules[con.name].values():
|
|
|
|
handlers += module.handlers
|
|
|
|
return handlers
|