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.
36 class WebOfTrustConnector @Inject constructor(private val pluginConnector: PluginConnector) {
38 private val logger: Logger = getLogger(WebOfTrustConnector::class.java.name)
45 * Loads all own identities from the Web of Trust plugin.
47 * @return All own identity
48 * @throws WebOfTrustException if the own identities can not be loaded
50 @Throws(WebOfTrustException::class)
51 fun loadAllOwnIdentities(): Set<OwnIdentity> {
52 val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetOwnIdentities").get())
53 var ownIdentityCounter = -1
54 val ownIdentities = HashSet<OwnIdentity>()
56 val id = fields.get("Identity" + ++ownIdentityCounter) ?: break
57 val requestUri = fields.get("RequestURI$ownIdentityCounter")
58 val insertUri = fields.get("InsertURI$ownIdentityCounter")
59 val nickname = fields.get("Nickname$ownIdentityCounter")
60 val ownIdentity = DefaultOwnIdentity(id, nickname, requestUri, insertUri)
61 ownIdentity.setContexts(parseContexts("Contexts$ownIdentityCounter.", fields))
62 ownIdentity.properties = parseProperties("Properties$ownIdentityCounter.", fields)
63 ownIdentities.add(ownIdentity)
69 * Loads all identities that the given identities trusts with a score of
70 * more than 0 and the (optional) given context.
72 * @param ownIdentity The own identity
73 * @param context The context to filter, or `null`
74 * @return All trusted identities
75 * @throws PluginException if an error occured talking to the Web of Trust plugin
77 @Throws(PluginException::class)
79 fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String? = null): Set<Identity> {
80 val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.id).put("Selection", "+").put("Context", context ?: "").put("WantTrustValues", "true").get())
81 val identities = HashSet<Identity>()
82 var identityCounter = -1
84 val id = fields.get("Identity" + ++identityCounter) ?: break
85 val nickname = fields.get("Nickname$identityCounter")
86 val requestUri = fields.get("RequestURI$identityCounter")
87 val identity = DefaultIdentity(id, nickname, requestUri)
88 identity.setContexts(parseContexts("Contexts$identityCounter.", fields))
89 identity.properties = parseProperties("Properties$identityCounter.", fields)
90 val trust = parseInt(fields.get("Trust$identityCounter"), null)
91 val score = parseInt(fields.get("Score$identityCounter"), 0)!!
92 val rank = parseInt(fields.get("Rank$identityCounter"), 0)!!
93 identity.setTrust(ownIdentity, Trust(trust, score, rank))
94 identities.add(identity)
100 * Adds the given context to the given identity.
102 * @param ownIdentity The identity to add the context to
103 * @param context The context to add
104 * @throws PluginException if an error occured talking to the Web of Trust plugin
106 @Throws(PluginException::class)
107 fun addContext(ownIdentity: OwnIdentity, context: String) {
108 performRequest(SimpleFieldSetBuilder().put("Message", "AddContext").put("Identity", ownIdentity.id).put("Context", context).get())
112 * Removes the given context from the given identity.
114 * @param ownIdentity The identity to remove the context from
115 * @param context The context to remove
116 * @throws PluginException if an error occured talking to the Web of Trust plugin
118 @Throws(PluginException::class)
119 fun removeContext(ownIdentity: OwnIdentity, context: String) {
120 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveContext").put("Identity", ownIdentity.id).put("Context", context).get())
124 * Sets the property with the given name to the given value.
126 * @param ownIdentity The identity to set the property on
127 * @param name The name of the property to set
128 * @param value The value to set
129 * @throws PluginException if an error occured talking to the Web of Trust plugin
131 @Throws(PluginException::class)
132 fun setProperty(ownIdentity: OwnIdentity, name: String, value: String) {
133 performRequest(SimpleFieldSetBuilder().put("Message", "SetProperty").put("Identity", ownIdentity.id).put("Property", name).put("Value", value).get())
137 * Removes the property with the given name.
139 * @param ownIdentity The identity to remove the property from
140 * @param name The name of the property to remove
141 * @throws PluginException if an error occured talking to the Web of Trust plugin
143 @Throws(PluginException::class)
144 fun removeProperty(ownIdentity: OwnIdentity, name: String) {
145 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveProperty").put("Identity", ownIdentity.id).put("Property", name).get())
149 * Returns the trust for the given identity assigned to it by the given own
152 * @param ownIdentity The own identity
153 * @param identity The identity to get the trust for
154 * @return The trust for the given identity
155 * @throws PluginException if an error occured talking to the Web of Trust plugin
157 @Throws(PluginException::class)
158 fun getTrust(ownIdentity: OwnIdentity, identity: Identity): Trust {
159 val (fields) = performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentity").put("Truster", ownIdentity.id).put("Identity", identity.id).get())
160 val trust = fields.get("Trust")
161 val score = fields.get("Score")
162 val rank = fields.get("Rank")
163 var explicit: Int? = null
164 var implicit: Int? = null
165 var distance: Int? = null
167 explicit = Integer.valueOf(trust)
168 } catch (nfe1: NumberFormatException) {
173 implicit = Integer.valueOf(score)
174 } catch (nfe1: NumberFormatException) {
179 distance = Integer.valueOf(rank)
180 } catch (nfe1: NumberFormatException) {
184 return Trust(explicit, implicit, distance)
188 * Sets the trust for the given identity.
190 * @param ownIdentity The trusting identity
191 * @param identity The trusted identity
192 * @param trust The amount of trust (-100 thru 100)
193 * @param comment The comment or explanation of the trust value
194 * @throws PluginException if an error occured talking to the Web of Trust plugin
196 @Throws(PluginException::class)
197 fun setTrust(ownIdentity: OwnIdentity, identity: Identity, trust: Int, comment: String) {
198 performRequest(SimpleFieldSetBuilder().put("Message", "SetTrust").put("Truster", ownIdentity.id).put("Trustee", identity.id).put("Value", trust.toString()).put("Comment", comment).get())
202 * Removes any trust assignment of the given own identity for the given
205 * @param ownIdentity The own identity
206 * @param identity The identity to remove all trust for
207 * @throws WebOfTrustException if an error occurs
209 @Throws(WebOfTrustException::class)
210 fun removeTrust(ownIdentity: OwnIdentity, identity: Identity) {
211 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveTrust").put("Truster", ownIdentity.id).put("Trustee", identity.id).get())
215 * Pings the Web of Trust plugin. If the plugin can not be reached, a
216 * [PluginException] is thrown.
218 * @throws PluginException if the plugin is not loaded
220 @Throws(PluginException::class)
222 performRequest(SimpleFieldSetBuilder().put("Message", "Ping").get())
225 private fun performRequest(fields: SimpleFieldSet, data: Bucket? = null): PluginReply {
226 logger.log(Level.FINE, format("Sending FCP Request: %s", fields.get("Message")))
227 val pluginReply = pluginConnector.sendRequest(WOT_PLUGIN_NAME, "", fields, data)
228 logger.log(Level.FINEST, format("Received FCP Response for %s: %s", fields.get("Message"), pluginReply.fields.get("Message")))
229 if ("Error" == pluginReply.fields.get("Message")) {
230 throw PluginException("Could not perform request for " + fields.get("Message"))
237 private const val WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust"
239 private fun parseContexts(prefix: String, fields: SimpleFieldSet): Set<String> {
240 val contexts = HashSet<String>()
241 var contextCounter = -1
243 val context = fields.get(prefix + "Context" + ++contextCounter) ?: break
244 contexts.add(context)
249 private fun parseProperties(prefix: String, fields: SimpleFieldSet): Map<String, String> {
250 val properties = HashMap<String, String>()
251 var propertiesCounter = -1
253 val propertyName = fields.get(prefix + "Property" + ++propertiesCounter + ".Name") ?: break
254 val propertyValue = fields.get(prefix + "Property" + propertiesCounter + ".Value")
255 properties[propertyName] = propertyValue