7131d2278a6d17acb176fb287994ffab1ec61c28
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / freenet / wot / WebOfTrustConnector.kt
1 /*
2  * Sone - WebOfTrustConnector.java - Copyright © 2010–2019 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.freenet.wot
19
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.*
27 import java.util.*
28 import java.util.logging.*
29 import java.util.logging.Logger
30 import java.util.logging.Logger.*
31
32 /**
33  * Connector for the Web of Trust plugin.
34  */
35 @Singleton
36 class WebOfTrustConnector @Inject constructor(private val pluginConnector: PluginConnector) {
37
38         private val logger: Logger = getLogger(WebOfTrustConnector::class.java.name)
39
40         fun stop() {
41                 /* does nothing. */
42         }
43
44         /**
45          * Loads all own identities from the Web of Trust plugin.
46          *
47          * @return All own identity
48          * @throws WebOfTrustException if the own identities can not be loaded
49          */
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>()
55                 while (true) {
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)
64                 }
65                 return ownIdentities
66         }
67
68         /**
69          * Loads all identities that the given identities trusts with a score of
70          * more than 0 and the (optional) given context.
71          *
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
76          */
77         @Throws(PluginException::class)
78         @JvmOverloads
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
83                 while (true) {
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)
95                 }
96                 return identities
97         }
98
99         /**
100          * Adds the given context to the given identity.
101          *
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
105          */
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())
109         }
110
111         /**
112          * Removes the given context from the given identity.
113          *
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
117          */
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())
121         }
122
123         /**
124          * Sets the property with the given name to the given value.
125          *
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
130          */
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())
134         }
135
136         /**
137          * Removes the property with the given name.
138          *
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
142          */
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())
146         }
147
148         /**
149          * Returns the trust for the given identity assigned to it by the given own
150          * identity.
151          *
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
156          */
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
166                 try {
167                         explicit = Integer.valueOf(trust)
168                 } catch (nfe1: NumberFormatException) {
169                         /* ignore. */
170                 }
171
172                 try {
173                         implicit = Integer.valueOf(score)
174                 } catch (nfe1: NumberFormatException) {
175                         /* ignore. */
176                 }
177
178                 try {
179                         distance = Integer.valueOf(rank)
180                 } catch (nfe1: NumberFormatException) {
181                         /* ignore. */
182                 }
183
184                 return Trust(explicit, implicit, distance)
185         }
186
187         /**
188          * Sets the trust for the given identity.
189          *
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
195          */
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())
199         }
200
201         /**
202          * Removes any trust assignment of the given own identity for the given
203          * identity.
204          *
205          * @param ownIdentity The own identity
206          * @param identity The identity to remove all trust for
207          * @throws WebOfTrustException if an error occurs
208          */
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())
212         }
213
214         /**
215          * Pings the Web of Trust plugin. If the plugin can not be reached, a
216          * [PluginException] is thrown.
217          *
218          * @throws PluginException if the plugin is not loaded
219          */
220         @Throws(PluginException::class)
221         fun ping() {
222                 performRequest(SimpleFieldSetBuilder().put("Message", "Ping").get())
223         }
224
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"))
231                 }
232                 return pluginReply
233         }
234
235 }
236
237 private const val WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust"
238
239 private fun parseContexts(prefix: String, fields: SimpleFieldSet): Set<String> {
240         val contexts = HashSet<String>()
241         var contextCounter = -1
242         while (true) {
243                 val context = fields.get(prefix + "Context" + ++contextCounter) ?: break
244                 contexts.add(context)
245         }
246         return contexts
247 }
248
249 private fun parseProperties(prefix: String, fields: SimpleFieldSet): Map<String, String> {
250         val properties = HashMap<String, String>()
251         var propertiesCounter = -1
252         while (true) {
253                 val propertyName = fields.get(prefix + "Property" + ++propertiesCounter + ".Name") ?: break
254                 val propertyValue = fields.get(prefix + "Property" + propertiesCounter + ".Value")
255                 properties[propertyName] = propertyValue
256         }
257         return properties
258 }
259