changeset 1534:a5e0393a06cd

plugin ip, params: plugin IP discovery, first draft: - a dedicated plugin now manage IP discovery, for now it uses external website, but it should implement XEP-0279 soon - a permission is requested to user for calling an external website. Once the permission is granted (it's global), it is not asked anymore. - the ip is discovered on demand, and cache is kept for the session. - memory.params.getParamA has a new parameter use_default, which allow to get None when a parameter is not set, instead of the default value. This allow to detected when we need to do the first permission request. memory.getParamA don't have this parameter.
author Goffi <goffi@goffi.org>
date Tue, 29 Sep 2015 17:54:22 +0200
parents d749922300d0
children c9ef16de3f13
files src/memory/memory.py src/memory/params.py src/plugins/plugin_misc_ip.py
diffstat 3 files changed, 112 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/memory/memory.py	Tue Sep 29 17:54:21 2015 +0200
+++ b/src/memory/memory.py	Tue Sep 29 17:54:22 2015 +0200
@@ -853,7 +853,7 @@
         return self.params.getStringParamA(name, category, attr, profile_key)
 
     def getParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE):
-        return self.params.getParamA(name, category, attr, profile_key)
+        return self.params.getParamA(name, category, attr, profile_key=profile_key)
 
     def asyncGetParamA(self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
         return self.params.asyncGetParamA(name, category, attr, security_limit, profile_key)
--- a/src/memory/params.py	Tue Sep 29 17:54:21 2015 +0200
+++ b/src/memory/params.py	Tue Sep 29 17:54:22 2015 +0200
@@ -348,7 +348,7 @@
         @param node: XML param node
         @param attr: name of the attribute to get (e.g.: 'value' or 'type')
         @param value: user defined value
-        @return: str
+        @return: value (can be str, bool, int, list, None)
         """
         if attr == 'value':
             value_to_use = value if value is not None else node.getAttribute(attr)  # we use value (user defined) if it exist, else we use node's default value
@@ -444,7 +444,7 @@
         """ Same as getParamA but for bridge: convert non string value to string """
         return self.__type_to_string(self.getParamA(name, category, attr, profile_key))
 
-    def getParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE):
+    def getParamA(self, name, category, attr="value", use_default=True, profile_key=C.PROF_KEY_NONE):
         """Helper method to get a specific attribute.
 
         /!\ This method would return encrypted password values,
@@ -452,6 +452,8 @@
         @param name: name of the parameter
         @param category: category of the parameter
         @param attr: name of the attribute (default: "value")
+        @parm use_default(bool): if True and attr=='value', return default value if not set
+            else return None if not set
         @param profile: owner of the param (@ALL@ for everyone)
         @return: attribute
         """
@@ -466,6 +468,8 @@
 
         if node[0] == C.GENERAL:
             value = self._getParam(category, name, C.GENERAL)
+            if value is None and attr=='value' and not use_default:
+                return value
             return self._getAttr(node[1], attr, value)
 
         assert node[0] == C.INDIVIDUAL
@@ -481,6 +485,8 @@
 
         if attr == "value":
             value = self._getParam(category, name, profile=profile)
+            if value is None and attr=='value' and not use_default:
+                return value
             return self._getAttr(node[1], attr, value)
 
     def asyncGetStringParamA(self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
@@ -495,7 +501,7 @@
         @param category: category of the parameter
         @param attr: name of the attribute (default: "value")
         @param profile: owner of the param (@ALL@ for everyone)
-        @return: Deferred
+        @return (defer.Deferred): parameter value, with corresponding type (bool, int, list, etc)
         """
         node = self._getParamNode(name, category)
         if not node:
@@ -769,6 +775,7 @@
         @param profile_key (str): %(doc_profile_key)s
         @return: a deferred None value when everything is done
         """
+        # FIXME: setParam should accept the right type for value, not only str !
         if profile_key != C.PROF_KEY_NONE:
             profile = self.getProfileName(profile_key)
             if not profile:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/plugins/plugin_misc_ip.py	Tue Sep 29 17:54:22 2015 +0200
@@ -0,0 +1,101 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# SAT plugin for IP address discovery
+# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program 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 Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from sat.core.i18n import _, D_
+from sat.core.constants import Const as C
+from sat.core.log import getLogger
+log = getLogger(__name__)
+from sat.tools import xml_tools
+from twisted.web.client import getPage
+import urlparse
+
+
+PLUGIN_INFO = {
+    "name": "IP discovery",
+    "import_name": "IP",
+    "type": C.PLUG_TYPE_MISC,
+    "main": "IPPlugin",
+    "handler": "no",
+    "description": _("""This plugin help to discover our external IP address.""")
+}
+
+GET_IP_PAGE = "http://www.goffi.org/sat_tools/get_ip.php" # This page must only return external IP of the requester
+GET_IP_LABEL = D_(u"Allow external get IP")
+GET_IP_CATEGORY = "General"
+GET_IP_NAME = "allow_get_ip"
+GET_IP_CONFIRM_TITLE = D_(u"Confirm external site request")
+GET_IP_CONFIRM = D_(u"""To facilitate data transfer, we need to contact a website.
+A request will be done on {page}
+That means that administrators of {domain} can know that you use "{app_name}" and your IP Address.
+
+IP address is an identifier to locate you on Internet (similar to a phone number).
+
+Do you agree to do this request ?
+""").format(
+    page = GET_IP_PAGE,
+    domain = urlparse.urlparse(GET_IP_PAGE).netloc,
+    app_name = C.APP_NAME)
+
+PARAMS = """
+    <params>
+    <general>
+    <category name="{category}">
+        <param name="{name}" label="{label}" type="bool" />
+    </category>
+    </general>
+    </params>
+    """.format(category=GET_IP_CATEGORY, name=GET_IP_NAME, label=GET_IP_LABEL)
+
+class IPPlugin(object):
+
+    def __init__(self, host):
+        log.info(_("plugin IP discovery initialization"))
+        self.host = host
+        host.memory.updateParams(PARAMS)
+
+    def profileConnected(self, profile):
+        client = self.host.getClient(profile)
+        # XXX: we keep cache only for profile session as ip can change between them
+        client._ip_cache = None
+
+    def getIP(self, profile):
+        """Try to discover external IP
+
+        @param profile: %(doc_profile)s
+        @return (deferred): external IP address or None if it can't be discovered
+        """
+        client = self.host.getClient(profile)
+        if client._ip_cache is not None:
+            return client._ip_cache
+
+        allow_get_ip = self.host.memory.params.getParamA(GET_IP_NAME, GET_IP_CATEGORY, use_default=False)
+
+        if allow_get_ip is None:
+            # we don't have autorisation from user yet to use get_ip, we ask him
+            confirm_d = xml_tools.deferConfirm(self.host, _(GET_IP_CONFIRM), _(GET_IP_CONFIRM_TITLE), profile=profile)
+            def setParam(allowed):
+                # FIXME: we need to use boolConst as setParam only manage str/unicode
+                #        need to be fixed when params will be refactored
+                self.host.memory.setParam(GET_IP_NAME, C.boolConst(allowed), GET_IP_CATEGORY)
+                return self.getIP(profile)
+
+            return confirm_d.addCallback(setParam)
+
+
+        return getPage(GET_IP_PAGE) if allow_get_ip else None