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