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 net.pterodactylus.sone.freenet.*
23 import net.pterodactylus.sone.freenet.plugin.*
24 import net.pterodactylus.sone.utils.NumberParsers.*
25 import java.lang.String.*
27 import java.util.logging.*
28 import java.util.logging.Logger
29 import java.util.logging.Logger.*
32 * Connector for the Web of Trust plugin.
34 class PluginWebOfTrustConnector @Inject constructor(private val pluginConnector: PluginConnector) : WebOfTrustConnector {
36 private val logger: Logger = getLogger(PluginWebOfTrustConnector::class.java.name)
38 override fun loadAllOwnIdentities(): Set<OwnIdentity> {
39 val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetOwnIdentities").get())
40 var ownIdentityCounter = -1
41 val ownIdentities = HashSet<OwnIdentity>()
43 val id = fields.get("Identity" + ++ownIdentityCounter) ?: break
44 val requestUri = fields.get("RequestURI$ownIdentityCounter")
45 val insertUri = fields.get("InsertURI$ownIdentityCounter")
46 val nickname = fields.get("Nickname$ownIdentityCounter")
47 val ownIdentity = DefaultOwnIdentity(id, nickname, requestUri, insertUri)
48 ownIdentity.contexts = fields.contexts("Contexts$ownIdentityCounter.")
49 ownIdentity.properties = fields.properties("Properties$ownIdentityCounter.")
50 ownIdentities.add(ownIdentity)
55 override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?): Set<Identity> {
56 val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.id).put("Selection", "+").put("Context", context ?: "").put("WantTrustValues", "true").get())
57 val identities = HashSet<Identity>()
58 var identityCounter = -1
60 val id = fields.get("Identity" + ++identityCounter) ?: break
61 val nickname = fields.get("Nickname$identityCounter")
62 val requestUri = fields.get("RequestURI$identityCounter")
63 val identity = DefaultIdentity(id, nickname, requestUri)
64 identity.contexts = fields.contexts("Contexts$identityCounter.")
65 identity.properties = fields.properties("Properties$identityCounter.")
66 val trust = parseInt(fields.get("Trust$identityCounter"), null)
67 val score = parseInt(fields.get("Score$identityCounter"), 0)!!
68 val rank = parseInt(fields.get("Rank$identityCounter"), 0)!!
69 identity.setTrust(ownIdentity, Trust(trust, score, rank))
70 identities.add(identity)
75 @Throws(PluginException::class)
76 override fun addContext(ownIdentity: OwnIdentity, context: String) {
77 performRequest(SimpleFieldSetBuilder().put("Message", "AddContext").put("Identity", ownIdentity.id).put("Context", context).get())
80 @Throws(PluginException::class)
81 override fun removeContext(ownIdentity: OwnIdentity, context: String) {
82 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveContext").put("Identity", ownIdentity.id).put("Context", context).get())
85 override fun setProperty(ownIdentity: OwnIdentity, name: String, value: String) {
86 performRequest(SimpleFieldSetBuilder().put("Message", "SetProperty").put("Identity", ownIdentity.id).put("Property", name).put("Value", value).get())
89 override fun removeProperty(ownIdentity: OwnIdentity, name: String) {
90 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveProperty").put("Identity", ownIdentity.id).put("Property", name).get())
93 override fun getTrust(ownIdentity: OwnIdentity, identity: Identity): Trust {
94 val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentity").put("Truster", ownIdentity.id).put("Identity", identity.id).get())
95 val trust = fields.get("Trust")
96 val score = fields.get("Score")
97 val rank = fields.get("Rank")
98 var explicit: Int? = null
99 var implicit: Int? = null
100 var distance: Int? = null
102 explicit = Integer.valueOf(trust)
103 } catch (nfe1: NumberFormatException) {
108 implicit = Integer.valueOf(score)
109 } catch (nfe1: NumberFormatException) {
114 distance = Integer.valueOf(rank)
115 } catch (nfe1: NumberFormatException) {
119 return Trust(explicit, implicit, distance)
122 override fun setTrust(ownIdentity: OwnIdentity, identity: Identity, trust: Int, comment: String) {
123 performRequest(SimpleFieldSetBuilder().put("Message", "SetTrust").put("Truster", ownIdentity.id).put("Trustee", identity.id).put("Value", trust.toString()).put("Comment", comment).get())
126 override fun removeTrust(ownIdentity: OwnIdentity, identity: Identity) {
127 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveTrust").put("Truster", ownIdentity.id).put("Trustee", identity.id).get())
130 override fun ping() {
131 performRequest(SimpleFieldSetBuilder().put("Message", "Ping").get())
134 private fun performRequest(fields: SimpleFieldSet): PluginReply {
135 logger.log(Level.FINE, format("Sending FCP Request: %s", fields.get("Message")))
136 val pluginReply = pluginConnector.sendRequest(WOT_PLUGIN_NAME, "", fields)
137 logger.log(Level.FINEST, format("Received FCP Response for %s: %s", fields.get("Message"), pluginReply.fields.get("Message")))
138 if ("Error" == pluginReply.fields.get("Message")) {
139 throw PluginException("Could not perform request for " + fields.get("Message"))
146 private const val WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust"
148 private fun SimpleFieldSet.contexts(prefix: String) =
149 generateSequence(0, Int::inc)
150 .map { get("${prefix}Context$it") }
151 .takeWhile { it != null }
154 private fun SimpleFieldSet.properties(prefix: String) =
155 generateSequence(0, Int::inc)
156 .takeWhile { get("${prefix}Property${it}.Name") != null }
157 .map { get("${prefix}Property${it}.Name") to get("${prefix}Property${it}.Value") }