diff --git a/src/memory/memory.py b/src/memory/memory.py
index dd1d4577..00d39691 100644
--- a/src/memory/memory.py
+++ b/src/memory/memory.py
@@ -853,7 +853,7 @@ class Memory(object):
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)
diff --git a/src/memory/params.py b/src/memory/params.py
index b34db2b5..0ba9d053 100644
--- a/src/memory/params.py
+++ b/src/memory/params.py
@@ -348,7 +348,7 @@ class Params(object):
@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 @@ class Params(object):
""" 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 @@ class Params(object):
@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 @@ class Params(object):
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 @@ class Params(object):
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 @@ class Params(object):
@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 @@ class Params(object):
@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:
diff --git a/src/plugins/plugin_misc_ip.py b/src/plugins/plugin_misc_ip.py
new file mode 100644
index 00000000..fb7a67cd
--- /dev/null
+++ b/src/plugins/plugin_misc_ip.py
@@ -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 .
+
+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 = """
+
+
+
+
+
+
+
+ """.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