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