✨ Add ticker shutdown wrapper
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / freenet / wot / PluginWebOfTrustConnector.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 kotlinx.coroutines.*
23 import net.pterodactylus.sone.freenet.*
24 import net.pterodactylus.sone.freenet.plugin.*
25 import java.lang.String.*
26 import java.util.logging.*
27 import java.util.logging.Logger
28 import java.util.logging.Logger.*
29
30 /**
31  * Connector for the Web of Trust plugin.
32  */
33 class PluginWebOfTrustConnector @Inject constructor(private val pluginConnector: PluginConnector) : WebOfTrustConnector {
34
35         private val logger: Logger = getLogger(PluginWebOfTrustConnector::class.java.name)
36
37         @Throws(PluginException::class)
38         override fun loadAllOwnIdentities(): Set<OwnIdentity> =
39                         performRequest(SimpleFieldSetBuilder().put("Message", "GetOwnIdentities").get())
40                                         .fields
41                                         .parseIdentities { parseOwnIdentity(it) }
42
43         @Throws(PluginException::class)
44         override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?): Set<Identity> =
45                         performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.id).put("Selection", "+").put("Context", context ?: "").put("WantTrustValues", "true").get())
46                                         .fields
47                                         .parseIdentities { parseTrustedIdentity(it, ownIdentity) }
48
49         @Throws(PluginException::class)
50         override fun addContext(ownIdentity: OwnIdentity, context: String) {
51                 performRequest(SimpleFieldSetBuilder().put("Message", "AddContext").put("Identity", ownIdentity.id).put("Context", context).get())
52         }
53
54         @Throws(PluginException::class)
55         override fun removeContext(ownIdentity: OwnIdentity, context: String) {
56                 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveContext").put("Identity", ownIdentity.id).put("Context", context).get())
57         }
58
59         override fun setProperty(ownIdentity: OwnIdentity, name: String, value: String) {
60                 performRequest(SimpleFieldSetBuilder().put("Message", "SetProperty").put("Identity", ownIdentity.id).put("Property", name).put("Value", value).get())
61         }
62
63         override fun removeProperty(ownIdentity: OwnIdentity, name: String) {
64                 performRequest(SimpleFieldSetBuilder().put("Message", "RemoveProperty").put("Identity", ownIdentity.id).put("Property", name).get())
65         }
66
67         override fun ping() {
68                 performRequest(SimpleFieldSetBuilder().put("Message", "Ping").get())
69         }
70
71         private fun performRequest(fields: SimpleFieldSet): PluginReply {
72                 logger.log(Level.FINE, format("Sending FCP Request: %s", fields.get("Message")))
73                 return runBlocking {
74                         pluginConnector.sendRequest(WOT_PLUGIN_NAME, fields).also {
75                                 logger.log(Level.FINEST, format("Received FCP Response for %s: %s", fields.get("Message"), it.fields.get("Message")))
76                                 if ("Error" == it.fields.get("Message")) {
77                                         throw PluginException("Could not perform request for " + fields.get("Message"))
78                                 }
79                         }
80                 }
81         }
82
83 }
84
85 private const val WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust"
86
87 private fun <I> SimpleFieldSet.parseIdentities(parser: SimpleFieldSet.(Int) -> I) =
88                 scanPrefix { "Identity$it" }
89                                 .map { parser(this, it) }
90                                 .toSet()
91
92 private fun SimpleFieldSet.parseOwnIdentity(index: Int) =
93                 DefaultOwnIdentity(get("Identity$index"), get("Nickname$index"), get("RequestURI$index"), get("InsertURI$index"))
94                                 .setContextsAndProperties(this@parseOwnIdentity, index)
95
96 private fun SimpleFieldSet.parseTrustedIdentity(index: Int, ownIdentity: OwnIdentity) =
97                 DefaultIdentity(get("Identity$index"), get("Nickname$index"), get("RequestURI$index"))
98                                 .setContextsAndProperties(this@parseTrustedIdentity, index)
99                                 .apply { setTrust(ownIdentity, this@parseTrustedIdentity.parseTrust(index.toString())) }
100
101 private fun <I : Identity> I.setContextsAndProperties(simpleFieldSet: SimpleFieldSet, index: Int) = apply {
102         contexts = simpleFieldSet.contexts("Contexts$index.")
103         properties = simpleFieldSet.properties("Properties$index.")
104 }
105
106 private fun SimpleFieldSet.parseTrust(index: String = "") =
107                 Trust(get("Trust$index")?.toIntOrNull(), get("Score$index")?.toIntOrNull(), get("Rank$index")?.toIntOrNull())
108
109 private fun SimpleFieldSet.contexts(prefix: String) =
110                 scanPrefix { "${prefix}Context$it" }
111                                 .map { get("${prefix}Context$it") }
112                                 .toSet()
113
114 private fun SimpleFieldSet.properties(prefix: String) =
115                 scanPrefix { "${prefix}Property${it}.Name" }
116                                 .map { get("${prefix}Property${it}.Name") to get("${prefix}Property${it}.Value") }
117                                 .toMap()
118
119 private fun SimpleFieldSet.scanPrefix(prefix: (Int) -> String) =
120                 generateSequence(0, Int::inc)
121                                 .takeWhile { get(prefix(it)) != null }