diff src/browser/libervia_main.py @ 521:69bffcf37ce3

browser_side: add minimal requirements for generic plugins handling
author souliane <souliane@mailoo.org>
date Tue, 02 Sep 2014 21:26:32 +0200
parents d58d4dd0cefe
children 1735aaeac652
line wrap: on
line diff
--- a/src/browser/libervia_main.py	Tue Sep 02 21:21:21 2014 +0200
+++ b/src/browser/libervia_main.py	Tue Sep 02 21:26:32 2014 +0200
@@ -49,6 +49,8 @@
 
 from sat_browser.constants import Const as C
 
+# FIXME: import plugin dynamically
+from sat_browser import plugin_sec_otr
 
 MAX_MBLOG_CACHE = 500  # Max microblog entries kept in memories
 
@@ -190,6 +192,7 @@
         RootPanel().add(self.panel)
         self.notification = notification.Notification()
         DOM.addEventPreview(self)
+        self.importPlugins()
         self._register = RegisterCall()
         self._register.call('getMenus', self.gotMenus)
         self._register.call('registerParams', None)
@@ -198,6 +201,11 @@
         self.init_cache = []  # used to cache events until initialisation is done
         self.cached_params = {}
 
+    def importPlugins(self):
+        self.plugins = {}
+        # FIXME: plugins import should be dynamic and generic like in sat
+        self.plugins['otr'] = plugin_sec_otr.OTR(self)
+
     def addSelectedListener(self, callback):
         self._selected_listeners.add(callback)
 
@@ -321,11 +329,24 @@
 
         @param menus (list[tuple]): menu data
         """
+        def process(menus, inhibited=None):
+            for raw_menu in menus:
+                id_, type_, path, path_i18n = raw_menu
+                if inhibited and path[0] in inhibited:
+                    continue
+                menus_data = self.menus.setdefault(type_, [])
+                menus_data.append((id_, path, path_i18n))
+
         self.menus = {}
-        for raw_menu in menus:
-            id_, type_, path, path_i18n = raw_menu
-            menus_data = self.menus.setdefault(type_, [])
-            menus_data.append((id_, path, path_i18n))
+        inhibited = set()
+        extras = []
+        for plugin in self.plugins.values():
+            if hasattr(plugin, "inhibitMenus"):
+                inhibited.update(plugin.inhibitMenus())
+            if hasattr(plugin, "extraMenus"):
+                extras.extend(plugin.extraMenus())
+        process(menus, inhibited)
+        process(extras)
         self.panel.menu.createMenus()
 
     def _isRegisteredCB(self, result):
@@ -548,6 +569,11 @@
         #we ask for our own microblogs:
         self.bridge.call('getMassiveLastMblogs', self._ownBlogsFills, 'JID', [self.whoami.bare], 10)
 
+        # initialize plugins which waited for the connection to be done
+        for plugin in self.plugins.values():
+            if hasattr(plugin, 'profileConnected'):
+                plugin.profileConnected()
+
     ## Signals callbacks ##
 
     def _personalEventCb(self, sender, event_type, data):
@@ -681,23 +707,31 @@
             lib_wid.refresh()
         return lib_wid
 
-    def _newMessageCb(self, from_jid, msg, msg_type, to_jid, extra):
-        _from = jid.JID(from_jid)
-        _to = jid.JID(to_jid)
-        other = _to if _from.bare == self.whoami.bare else _from
+    def _newMessageCb(self, from_jid_s, msg, msg_type, to_jid_s, extra):
+        from_jid = jid.JID(from_jid_s)
+        to_jid = jid.JID(to_jid_s)
+        for plugin in self.plugins.values():
+            if hasattr(plugin, 'messageReceivedTrigger'):
+                if not plugin.messageReceivedTrigger(from_jid, msg, msg_type, to_jid, extra):
+                    return  # plugin returned False to interrupt the process
+        self.newMessageCb(from_jid, msg, msg_type, to_jid, extra)
+
+    def newMessageCb(self, from_jid, msg, msg_type, to_jid, extra):
+        # FIXME: newMessage should manage system message, so they don't appear as coming from the contact
+        other = to_jid if from_jid.bare == self.whoami.bare else from_jid
         lib_wid = self.getLiberviaWidget(panels.ChatPanel, other, ignoreOtherTabs=False)
-        self.displayNotification(_from, msg)
-        if msg_type == 'headline' and from_jid == self._defaultDomain:
+        self.displayNotification(from_jid, msg)
+        if msg_type == 'headline' and from_jid.full() == self._defaultDomain:
             try:
                 assert(extra['subject'])  # subject is defined and not empty
                 title = extra['subject']
             except (KeyError, AssertionError):
-                title = _('Announcement from %s') % from_jid
+                title = _('Announcement from %s') % from_jid.full()
             msg = strings.addURLToText(html_tools.XHTML2Text(msg))
             dialog.InfoDialog(title, msg).show()
             return
         if lib_wid is not None:
-            lib_wid.printMessage(_from, msg, extra)
+            lib_wid.printMessage(from_jid, msg, extra)
         else:
             if not self.contact_panel.isContactInRoster(other.bare):
                 self.contact_panel.updateContact(other.bare, {}, [C.GROUP_NOT_IN_ROSTER])
@@ -914,7 +948,12 @@
                 log.error("Unknown target type")
         if addresses:
             if len(addresses) == 1 and addresses[0][0] == 'to':
-                self.bridge.call('sendMessage', (None, self.sendError), addresses[0][1], text, '', type_, extra)
+                to_jid_s = addresses[0][1]
+                for plugin in self.plugins.values():
+                    if hasattr(plugin, 'sendMessageTrigger'):
+                        if not plugin.sendMessageTrigger(jid.JID(to_jid_s), text, type_, extra):
+                            return  # plugin returned False to interrupt the process
+                self.bridge.call('sendMessage', (None, self.sendError), to_jid_s, text, '', type_, extra)
             else:
                 extra.update({'address': '\n'.join([('%s:%s' % entry) for entry in addresses])})
                 self.bridge.call('sendMessage', (None, self.sendError), self.whoami.domain, text, '', type_, extra)