2 * Sone - PluginWebOfTrustConnector.kt - Copyright © 2010–2020 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.Inject
21 import freenet.support.SimpleFieldSet
22 import kotlinx.coroutines.runBlocking
23 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder
24 import net.pterodactylus.sone.freenet.plugin.PluginConnector
25 import net.pterodactylus.sone.freenet.plugin.PluginException
26 import net.pterodactylus.sone.freenet.plugin.PluginReply
27 import java.lang.String.format
28 import java.util.logging.Level
29 import java.util.logging.Logger
30 import java.util.logging.Logger.getLogger
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 @Throws(PluginException::class)
40 override fun loadAllOwnIdentities(): Set<OwnIdentity> =
41 performRequest(SimpleFieldSetBuilder().put("Message", "GetOwnIdentities").get())
43 .parseIdentities { parseOwnIdentity(it) }
45 @Throws(PluginException::class)
46 override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?): Set<Identity> =
47 performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.id).put("Selection", "+").put("Context", context ?: "").put("WantTrustValues", "true").get())
49 .parseIdentities { parseTrustedIdentity(it, ownIdentity) }
51 override fun loadAllIdentities(ownIdentity: OwnIdentity, context: String?): Set<Identity> =
52 performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.id).put("Selection", "+").put("Context", context ?: "").put("WantTrustValues", "true").get())
54 .parseIdentities { parseTrustedIdentity(it, ownIdentity) } +
55 performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.id).put("Selection", "-").put("Context", context ?: "").put("WantTrustValues", "true").get())
57 .parseIdentities { parseTrustedIdentity(it, ownIdentity) }
59 @Throws(PluginException::class)
60 override fun addContext(ownIdentity: OwnIdentity, context: String) {
61 performRequest(SimpleFieldSetBuilder().put("Message", "AddContext").put("Identity", ownIdentity.id).put("Context", context).get())
64 @Throws(PluginException::class)
65 override fun removeContext(ownIdentity: OwnIdentity, context: String) {
66 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveContext").put("Identity", ownIdentity.id).put("Context", context).get())
69 override fun setProperty(ownIdentity: OwnIdentity, name: String, value: String) {
70 performRequest(SimpleFieldSetBuilder().put("Message", "SetProperty").put("Identity", ownIdentity.id).put("Property", name).put("Value", value).get())
73 override fun removeProperty(ownIdentity: OwnIdentity, name: String) {
74 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveProperty").put("Identity", ownIdentity.id).put("Property", name).get())
78 performRequest(SimpleFieldSetBuilder().put("Message", "Ping").get())
81 private fun performRequest(fields: SimpleFieldSet): PluginReply {
82 logger.log(Level.FINE, format("Sending FCP Request: %s", fields.get("Message")))
84 pluginConnector.sendRequest(WOT_PLUGIN_NAME, fields).also {
85 logger.log(Level.FINEST, format("Received FCP Response for %s: %s", fields.get("Message"), it.fields.get("Message")))
86 if ("Error" == it.fields.get("Message")) {
87 throw PluginException("Could not perform request for " + fields.get("Message"))
95 private const val WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust"
97 private fun <I> SimpleFieldSet.parseIdentities(parser: SimpleFieldSet.(Int) -> I) =
98 scanPrefix { "Identity$it" }
99 .map { parser(this, it) }
102 private fun SimpleFieldSet.parseOwnIdentity(index: Int) =
103 DefaultOwnIdentity(get("Identity$index"), get("Nickname$index"), get("RequestURI$index"), get("InsertURI$index"))
104 .setContextsAndProperties(this@parseOwnIdentity, index)
106 private fun SimpleFieldSet.parseTrustedIdentity(index: Int, ownIdentity: OwnIdentity) =
107 DefaultIdentity(get("Identity$index"), get("Nickname$index"), get("RequestURI$index"))
108 .setContextsAndProperties(this@parseTrustedIdentity, index)
109 .apply { setTrust(ownIdentity, this@parseTrustedIdentity.parseTrust(index.toString())) }
111 private fun <I : Identity> I.setContextsAndProperties(simpleFieldSet: SimpleFieldSet, index: Int) = apply {
112 contexts = simpleFieldSet.contexts("Contexts$index.")
113 properties = simpleFieldSet.properties("Properties$index.")
116 private fun SimpleFieldSet.parseTrust(index: String = "") =
117 Trust(get("Trust$index")?.toIntOrNull(), get("Score$index")?.toIntOrNull(), get("Rank$index")?.toIntOrNull())
119 private fun SimpleFieldSet.contexts(prefix: String) =
120 scanPrefix { "${prefix}Context$it" }
121 .map { get("${prefix}Context$it") }
124 private fun SimpleFieldSet.properties(prefix: String) =
125 scanPrefix { "${prefix}Property${it}.Name" }
126 .map { get("${prefix}Property${it}.Name") to get("${prefix}Property${it}.Value") }
129 private fun SimpleFieldSet.scanPrefix(prefix: (Int) -> String) =
130 generateSequence(0, Int::inc)
131 .takeWhile { get(prefix(it)) != null }