Commit 59428d2f authored by Philipp Hörist's avatar Philipp Hörist

Register all available modules automatically

parent 3a3b3224
......@@ -61,29 +61,9 @@ from gajim.common import gpg
from gajim.common import passwords
from gajim.common import i18n
from gajim.common import idle
from gajim.common.helpers import ModuleMock
from gajim.common.modules.entity_time import EntityTime
from gajim.common.modules.software_version import SoftwareVersion
from import Ping
from import Search
from gajim.common.modules.annotations import Annotations
from gajim.common.modules.roster_item_exchange import RosterItemExchange
from gajim.common.modules.last_activity import LastActivity
from gajim.common.modules.http_auth import HTTPAuth
from gajim.common.modules.vcard_temp import VCardTemp
from gajim.common.modules.vcard_avatars import VCardAvatars
from gajim.common.modules.pubsub import PubSub
from gajim.common.modules.bookmarks import Bookmarks
from gajim.common.modules.pep import PEP
from gajim.common.modules.user_avatar import UserAvatar
from gajim.common.modules.user_activity import UserActivity
from gajim.common.modules.user_tune import UserTune
from gajim.common.modules.user_mood import UserMood
from gajim.common.modules.user_location import UserLocation
from gajim.common.modules.user_nickname import UserNickname
from gajim.common.modules.httpupload import HTTPUpload
from gajim.common.connection_handlers import *
from gajim.common.contacts import GC_Contact
from gajim.common import modules
from gajim.gtkgui_helpers import get_action
......@@ -182,19 +162,7 @@ class CommonConnection:
app.ged.raise_event(event,, data)
def get_module(self, name):
return self._modules[name]
except KeyError:
return ModuleMock()
def get_module_handlers(self):
handlers = []
for module in self._modules.values():
handlers += module.handlers
return handlers
def register_module(self, name, cls, *args, **kwargs):
self._modules[name] = cls(*args, **kwargs)
return modules.get(, name)
def reconnect(self):
......@@ -661,26 +629,8 @@ class Connection(CommonConnection, ConnectionHandlers): = Smacks(self) # Stream Management
self.register_module('EntityTime', EntityTime, self)
self.register_module('SoftwareVersion', SoftwareVersion, self)
self.register_module('Ping', Ping, self)
self.register_module('Search', Search, self)
self.register_module('Annotations', Annotations, self)
self.register_module('RosterItemExchange', RosterItemExchange, self)
self.register_module('LastActivity', LastActivity, self)
self.register_module('HTTPAuth', HTTPAuth, self)
self.register_module('VCardTemp', VCardTemp, self)
self.register_module('VCardAvatars', VCardAvatars, self)
self.register_module('PubSub', PubSub, self)
self.register_module('PEP', PEP, self)
self.register_module('Bookmarks', Bookmarks, self)
self.register_module('UserAvatar', UserAvatar, self)
self.register_module('UserActivity', UserActivity, self)
self.register_module('UserTune', UserTune, self)
self.register_module('UserMood', UserMood, self)
self.register_module('UserLocation', UserLocation, self)
self.register_module('UserNickname', UserNickname, self)
self.register_module('HTTPUpload', HTTPUpload, self)
# Register all modules
app.ged.register_event_handler('privacy-list-received', ged.CORE,
......@@ -700,6 +650,8 @@ class Connection(CommonConnection, ConnectionHandlers):
def cleanup(self):
app.ged.remove_event_handler('privacy-list-received', ged.CORE,
app.ged.remove_event_handler('agent-info-error-received', ged.CORE,
......@@ -780,6 +732,7 @@ class Connection(CommonConnection, ConnectionHandlers):
self.connection = None
def set_oldst(self): # Set old state
......@@ -37,6 +37,7 @@ from gi.repository import GLib
import nbxmpp
from gajim.common import caps_cache as capscache
from gajim.common import modules
from gajim.common import helpers
from gajim.common import app
from gajim.common import jingle_xtls
......@@ -1451,5 +1452,9 @@ ConnectionHandlersBase, ConnectionJingle, ConnectionIBBytestream):
con.RegisterHandler('iq', self._BlockingResultCB, 'result',
for handler in self.get_module_handlers():
for handler in modules.get_handlers(self):
def _unregister_handlers(self):
for handler in modules.get_handlers(self):
......@@ -165,13 +165,6 @@ class InvalidFormat(Exception):
class ModuleMock:
def __getattr__(self, key):
def _mock(self, *args, **kwargs):
return _mock
def decompose_jid(jidstring):
user = None
server = None
# 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
# 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 <>.
import logging
from importlib import import_module
from pathlib import Path
log = logging.getLogger('gajim.c.m')
imported_modules = []
_modules = {}
for file in Path(__file__).parent.iterdir():
if file.stem == '__init__':
module = import_module('.%s' % file.stem, package='gajim.common.modules')
if hasattr(module, 'get_instance'):'Load module: %s', file.stem)
class ModuleMock:
def __getattr__(self, key):
def _mock(self, *args, **kwargs):
return _mock
def register(con, *args, **kwargs):
if con in _modules:
_modules[] = {}
for module in imported_modules:
instance, name = module.get_instance(con, *args, **kwargs)
_modules[][name] = instance
def unregister(con):
del _modules[]
def get(account, name):
return _modules[account][name]
except KeyError:
return ModuleMock()
def get_handlers(con):
handlers = []
for module in _modules[].values():
handlers += module.handlers
return handlers
......@@ -90,3 +90,7 @@ class Annotations:
log.warning('Storing rosternotes failed: %s', stanza.getError())
return'Storing rosternotes successful')
def get_instance(*args, **kwargs):
return Annotations(*args, **kwargs), 'Annotations'
......@@ -292,3 +292,7 @@ class Bookmarks:
class BookmarksReceivedEvent(NetworkIncomingEvent):
name = 'bookmarks-received'
base_network_events = []
def get_instance(*args, **kwargs):
return Bookmarks(*args, **kwargs), 'Bookmarks'
......@@ -162,3 +162,7 @@ class contact_tz(datetime.tzinfo):
def dst(self, dt):
return ZERO
def get_instance(*args, **kwargs):
return EntityTime(*args, **kwargs), 'EntityTime'
......@@ -75,3 +75,7 @@ class HTTPAuth:
class HttpAuthReceivedEvent(NetworkIncomingEvent):
name = 'http-auth-received'
base_network_events = []
def get_instance(*args, **kwargs):
return HTTPAuth(*args, **kwargs), 'HTTPAuth'
......@@ -436,3 +436,7 @@ class UploadAbortedException(Exception):
class HTTPUploadProgressEvent(NetworkIncomingEvent):
name = 'httpupload-progress'
base_network_events = []
def get_instance(*args, **kwargs):
return HTTPUpload(*args, **kwargs), 'HTTPUpload'
......@@ -50,3 +50,7 @@ class LastActivity:
raise nbxmpp.NodeProcessed
def get_instance(*args, **kwargs):
return LastActivity(*args, **kwargs), 'LastActivity'
......@@ -212,3 +212,7 @@ class AbstractPEPData:
class PEPReceivedEvent(NetworkIncomingEvent):
name = 'pep-received'
base_network_events = []
def get_instance(*args, **kwargs):
return PEP(*args, **kwargs), 'PEP'
......@@ -120,3 +120,7 @@ class PingReplyEvent(NetworkIncomingEvent):
class PingErrorEvent(NetworkIncomingEvent):
name = 'ping-error'
base_network_events = []
def get_instance(*args, **kwargs):
return Ping(*args, **kwargs), 'Ping'
......@@ -254,3 +254,7 @@ class PubSub:
class PubSubConfigReceivedEvent(NetworkIncomingEvent):
name = 'pubsub-config-received'
base_network_events = []
def get_instance(*args, **kwargs):
return PubSub(*args, **kwargs), 'PubSub'
......@@ -121,3 +121,7 @@ class RosterItemExchange:
class RosterItemExchangeEvent(NetworkIncomingEvent):
name = 'roster-item-exchange-received'
base_network_events = []
def get_instance(*args, **kwargs):
return RosterItemExchange(*args, **kwargs), 'RosterItemExchange'
......@@ -112,3 +112,7 @@ class SearchFormReceivedEvent(NetworkIncomingEvent):
class SearchResultReceivedEvent(NetworkIncomingEvent):
name = 'search-result-received'
base_network_events = []
def get_instance(*args, **kwargs):
return Search(*args, **kwargs), 'Search'
......@@ -104,3 +104,7 @@ class VersionResultReceivedEvent(NetworkIncomingEvent):
def generate(self):
return True
def get_instance(*args, **kwargs):
return SoftwareVersion(*args, **kwargs), 'SoftwareVersion'
......@@ -99,3 +99,7 @@ class UserActivity(AbstractPEPModule):
i = item.addChild('text')
return item
def get_instance(*args, **kwargs):
return UserActivity(*args, **kwargs), 'UserActivity'
......@@ -152,3 +152,7 @@ class UserAvatar(AbstractPEPModule):
def retract(self):
# Not implemented yet
def get_instance(*args, **kwargs):
return UserAvatar(*args, **kwargs), 'UserAvatar'
......@@ -83,3 +83,7 @@ class UserLocation(AbstractPEPModule):
if data.get(field, False):
item.addChild(field, payload=data[field])
return item
def get_instance(*args, **kwargs):
return UserLocation(*args, **kwargs), 'UserLocation'
......@@ -86,3 +86,7 @@ class UserMood(AbstractPEPModule):
if text:
item.addChild('text', payload=text)
return item
def get_instance(*args, **kwargs):
return UserMood(*args, **kwargs), 'UserMood'
......@@ -76,3 +76,7 @@ class UserNickname(AbstractPEPModule):
app.nicks[self._account] = app.config.get_per(
'accounts', self._account, 'name')
def get_instance(*args, **kwargs):
return UserNickname(*args, **kwargs), 'UserNickname'
......@@ -96,3 +96,7 @@ class UserTune(AbstractPEPModule):
if length:
item.addChild('length', payload=length)
return item
def get_instance(*args, **kwargs):
return UserTune(*args, **kwargs), 'UserTune'
......@@ -190,3 +190,7 @@ class VCardAvatars:
node.getTo() or own_jid, sha or 'no sha advertised')
update.setTagData('photo', sha)
return node
def get_instance(*args, **kwargs):
return VCardAvatars(*args, **kwargs), 'VCardAvatars'
......@@ -306,3 +306,7 @@ class VcardNotPublishedEvent(NetworkIncomingEvent):
class VcardReceivedEvent(NetworkIncomingEvent):
name = 'vcard-received'
base_network_events = []
def get_instance(*args, **kwargs):
return VCardTemp(*args, **kwargs), 'VCardTemp'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment