comparison src/plugins/plugin_misc_ip.py @ 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
children 6fa9e8c02c34
comparison
equal deleted inserted replaced
1533:d749922300d0 1534:a5e0393a06cd
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for IP address discovery
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _, D_
21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger
23 log = getLogger(__name__)
24 from sat.tools import xml_tools
25 from twisted.web.client import getPage
26 import urlparse
27
28
29 PLUGIN_INFO = {
30 "name": "IP discovery",
31 "import_name": "IP",
32 "type": C.PLUG_TYPE_MISC,
33 "main": "IPPlugin",
34 "handler": "no",
35 "description": _("""This plugin help to discover our external IP address.""")
36 }
37
38 GET_IP_PAGE = "http://www.goffi.org/sat_tools/get_ip.php" # This page must only return external IP of the requester
39 GET_IP_LABEL = D_(u"Allow external get IP")
40 GET_IP_CATEGORY = "General"
41 GET_IP_NAME = "allow_get_ip"
42 GET_IP_CONFIRM_TITLE = D_(u"Confirm external site request")
43 GET_IP_CONFIRM = D_(u"""To facilitate data transfer, we need to contact a website.
44 A request will be done on {page}
45 That means that administrators of {domain} can know that you use "{app_name}" and your IP Address.
46
47 IP address is an identifier to locate you on Internet (similar to a phone number).
48
49 Do you agree to do this request ?
50 """).format(
51 page = GET_IP_PAGE,
52 domain = urlparse.urlparse(GET_IP_PAGE).netloc,
53 app_name = C.APP_NAME)
54
55 PARAMS = """
56 <params>
57 <general>
58 <category name="{category}">
59 <param name="{name}" label="{label}" type="bool" />
60 </category>
61 </general>
62 </params>
63 """.format(category=GET_IP_CATEGORY, name=GET_IP_NAME, label=GET_IP_LABEL)
64
65 class IPPlugin(object):
66
67 def __init__(self, host):
68 log.info(_("plugin IP discovery initialization"))
69 self.host = host
70 host.memory.updateParams(PARAMS)
71
72 def profileConnected(self, profile):
73 client = self.host.getClient(profile)
74 # XXX: we keep cache only for profile session as ip can change between them
75 client._ip_cache = None
76
77 def getIP(self, profile):
78 """Try to discover external IP
79
80 @param profile: %(doc_profile)s
81 @return (deferred): external IP address or None if it can't be discovered
82 """
83 client = self.host.getClient(profile)
84 if client._ip_cache is not None:
85 return client._ip_cache
86
87 allow_get_ip = self.host.memory.params.getParamA(GET_IP_NAME, GET_IP_CATEGORY, use_default=False)
88
89 if allow_get_ip is None:
90 # we don't have autorisation from user yet to use get_ip, we ask him
91 confirm_d = xml_tools.deferConfirm(self.host, _(GET_IP_CONFIRM), _(GET_IP_CONFIRM_TITLE), profile=profile)
92 def setParam(allowed):
93 # FIXME: we need to use boolConst as setParam only manage str/unicode
94 # need to be fixed when params will be refactored
95 self.host.memory.setParam(GET_IP_NAME, C.boolConst(allowed), GET_IP_CATEGORY)
96 return self.getIP(profile)
97
98 return confirm_d.addCallback(setParam)
99
100
101 return getPage(GET_IP_PAGE) if allow_get_ip else None