2 * Sone - WebOfTrustConnector.java - Copyright © 2010–2019 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.freenet.wot
20 import com.google.inject.*
21 import freenet.support.*
22 import freenet.support.api.*
23 import net.pterodactylus.sone.freenet.*
24 import net.pterodactylus.sone.freenet.plugin.*
25 import net.pterodactylus.sone.utils.NumberParsers.*
26 import java.lang.String.*
28 import java.util.logging.*
29 import java.util.logging.Logger
30 import java.util.logging.Logger.*
33 * Connector for the Web of Trust plugin.
35 class PluginWebOfTrustConnector @Inject constructor(private val pluginConnector: PluginConnector) : WebOfTrustConnector {
37 private val logger: Logger = getLogger(PluginWebOfTrustConnector::class.java.name)
39 override fun loadAllOwnIdentities(): Set<OwnIdentity> {
40 val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetOwnIdentities").get())
41 var ownIdentityCounter = -1
42 val ownIdentities = HashSet<OwnIdentity>()
44 val id = fields.get("Identity" + ++ownIdentityCounter) ?: break
45 val requestUri = fields.get("RequestURI$ownIdentityCounter")
46 val insertUri = fields.get("InsertURI$ownIdentityCounter")
47 val nickname = fields.get("Nickname$ownIdentityCounter")
48 val ownIdentity = DefaultOwnIdentity(id, nickname, requestUri, insertUri)
49 ownIdentity.setContexts(parseContexts("Contexts$ownIdentityCounter.", fields))
50 ownIdentity.properties = parseProperties("Properties$ownIdentityCounter.", fields)
51 ownIdentities.add(ownIdentity)
56 override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?): Set<Identity> {
57 val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.id).put("Selection", "+").put("Context", context ?: "").put("WantTrustValues", "true").get())
58 val identities = HashSet<Identity>()
59 var identityCounter = -1
61 val id = fields.get("Identity" + ++identityCounter) ?: break
62 val nickname = fields.get("Nickname$identityCounter")
63 val requestUri = fields.get("RequestURI$identityCounter")
64 val identity = DefaultIdentity(id, nickname, requestUri)
65 identity.setContexts(parseContexts("Contexts$identityCounter.", fields))
66 identity.properties = parseProperties("Properties$identityCounter.", fields)
67 val trust = parseInt(fields.get("Trust$identityCounter"), null)
68 val score = parseInt(fields.get("Score$identityCounter"), 0)!!
69 val rank = parseInt(fields.get("Rank$identityCounter"), 0)!!
70 identity.setTrust(ownIdentity, Trust(trust, score, rank))
71 identities.add(identity)
76 @Throws(PluginException::class)
77 override fun addContext(ownIdentity: OwnIdentity, context: String) {
78 performRequest(SimpleFieldSetBuilder().put("Message", "AddContext").put("Identity", ownIdentity.id).put("Context", context).get())
81 @Throws(PluginException::class)
82 override fun removeContext(ownIdentity: OwnIdentity, context: String) {
83 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveContext").put("Identity", ownIdentity.id).put("Context", context).get())
86 override fun setProperty(ownIdentity: OwnIdentity, name: String, value: String) {
87 performRequest(SimpleFieldSetBuilder().put("Message", "SetProperty").put("Identity", ownIdentity.id).put("Property", name).put("Value", value).get())
90 override fun removeProperty(ownIdentity: OwnIdentity, name: String) {
91 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveProperty").put("Identity", ownIdentity.id).put("Property", name).get())
94 override fun getTrust(ownIdentity: OwnIdentity, identity: Identity): Trust {
95 val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentity").put("Truster", ownIdentity.id).put("Identity", identity.id).get())
96 val trust = fields.get("Trust")
97 val score = fields.get("Score")
98 val rank = fields.get("Rank")
99 var explicit: Int? = null
100 var implicit: Int? = null
101 var distance: Int? = null
103 explicit = Integer.valueOf(trust)
104 } catch (nfe1: NumberFormatException) {
109 implicit = Integer.valueOf(score)
110 } catch (nfe1: NumberFormatException) {
115 distance = Integer.valueOf(rank)
116 } catch (nfe1: NumberFormatException) {
120 return Trust(explicit, implicit, distance)
123 override fun setTrust(ownIdentity: OwnIdentity, identity: Identity, trust: Int, comment: String) {
124 performRequest(SimpleFieldSetBuilder().put("Message", "SetTrust").put("Truster", ownIdentity.id).put("Trustee", identity.id).put("Value", trust.toString()).put("Comment", comment).get())
127 override fun removeTrust(ownIdentity: OwnIdentity, identity: Identity) {
128 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveTrust").put("Truster", ownIdentity.id).put("Trustee", identity.id).get())
131 override fun ping() {
132 performRequest(SimpleFieldSetBuilder().put("Message", "Ping").get())
135 private fun performRequest(fields: SimpleFieldSet, data: Bucket? = null): PluginReply {
136 logger.log(Level.FINE, format("Sending FCP Request: %s", fields.get("Message")))
137 val pluginReply = pluginConnector.sendRequest(WOT_PLUGIN_NAME, "", fields, data)
138 logger.log(Level.FINEST, format("Received FCP Response for %s: %s", fields.get("Message"), pluginReply.fields.get("Message")))
139 if ("Error" == pluginReply.fields.get("Message")) {
140 throw PluginException("Could not perform request for " + fields.get("Message"))
147 private const val WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust"
149 private fun parseContexts(prefix: String, fields: SimpleFieldSet): Set<String> {
150 val contexts = HashSet<String>()
151 var contextCounter = -1
153 val context = fields.get(prefix + "Context" + ++contextCounter) ?: break
154 contexts.add(context)
159 private fun parseProperties(prefix: String, fields: SimpleFieldSet): Map<String, String> {
160 val properties = HashMap<String, String>()
161 var propertiesCounter = -1
163 val propertyName = fields.get(prefix + "Property" + ++propertiesCounter + ".Name") ?: break
164 val propertyValue = fields.get(prefix + "Property" + propertiesCounter + ".Value")
165 properties[propertyName] = propertyValue