Update license to GPLv3, fix header comments
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / plugin / WebOfTrustPlugin.java
1 /*
2  * jFCPlib - WebOfTrustPlugin.java - Copyright © 2009–2016 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.fcp.plugin;
19
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25
26 import net.pterodactylus.fcp.highlevel.FcpClient;
27 import net.pterodactylus.fcp.highlevel.FcpException;
28
29 /**
30  * Simplifies handling of the web-of-trust plugin.
31  *
32  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
33  */
34 public class WebOfTrustPlugin {
35
36         /** The FCP client to use. */
37         private final FcpClient fcpClient;
38
39         /**
40          * Creates a new web-of-trust plugin wrapper around the given FCP client.
41          *
42          * @param fcpClient
43          *            The FCP client to use for communication with the web-of-trust
44          *            plugin
45          */
46         public WebOfTrustPlugin(FcpClient fcpClient) {
47                 this.fcpClient = fcpClient;
48         }
49
50         /**
51          * Creates a new identity.
52          *
53          * @param nickname
54          *            The nickname of the new identity
55          * @param context
56          *            The context for the new identity
57          * @param publishTrustList
58          *            {@code true} if the new identity should publish its trust list
59          * @return The new identity
60          * @throws IOException
61          *             if an I/O error occurs
62          * @throws FcpException
63          *             if an FCP error occurs
64          */
65         public OwnIdentity createIdentity(String nickname, String context, boolean publishTrustList) throws IOException, FcpException {
66                 return createIdentity(nickname, context, publishTrustList, null, null);
67         }
68
69         /**
70          * Creates a new identity from the given request and insert URI.
71          *
72          * @param nickname
73          *            The nickname of the new identity
74          * @param context
75          *            The context for the new identity
76          * @param publishTrustList
77          *            {@code true} if the new identity should publish its trust list
78          * @param requestUri
79          *            The request URI of the identity
80          * @param insertUri
81          *            The insert URI of the identity
82          * @return The new identity
83          * @throws IOException
84          *             if an I/O error occurs
85          * @throws FcpException
86          *             if an FCP error occurs
87          */
88         public OwnIdentity createIdentity(String nickname, String context, boolean publishTrustList, String requestUri, String insertUri) throws IOException, FcpException {
89                 Map<String, String> parameters = new HashMap<String, String>();
90                 parameters.put("Message", "CreateIdentity");
91                 parameters.put("Nickname", nickname);
92                 parameters.put("Context", context);
93                 parameters.put("PublishTrustList", String.valueOf(publishTrustList));
94                 if ((requestUri != null) && (insertUri != null)) {
95                         parameters.put("RequestURI", requestUri);
96                         parameters.put("InsertURI", insertUri);
97                 }
98                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", parameters);
99                 if (!replies.get("Message").equals("IdentityCreated")) {
100                         throw new FcpException("WebOfTrust Plugin did not reply with “IdentityCreated” message!");
101                 }
102                 String identifier = replies.get("ID");
103                 String newRequestUri = replies.get("RequestURI");
104                 String newInsertUri = replies.get("InsertURI");
105                 return new OwnIdentity(identifier, nickname, newRequestUri, newInsertUri);
106         }
107
108         /**
109          * Returns all own identities of the web-of-trust plugins. Almost all other
110          * commands require an {@link OwnIdentity} to return meaningful values.
111          *
112          * @return All own identities of the web-of-trust plugin
113          * @throws IOException
114          *             if an I/O error occurs
115          * @throws FcpException
116          *             if an FCP error occurs
117          */
118         public Set<OwnIdentity> getOwnIdentites() throws IOException, FcpException {
119                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetOwnIdentities"));
120                 if (!replies.get("Message").equals("OwnIdentities")) {
121                         throw new FcpException("WebOfTrust Plugin did not reply with “OwnIdentities” message!");
122                 }
123                 Set<OwnIdentity> ownIdentities = new HashSet<OwnIdentity>();
124                 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
125                         String identity = replies.get("Identity" + identityIndex);
126                         String nickname = replies.get("Nickname" + identityIndex);
127                         String requestUri = replies.get("RequestURI" + identityIndex);
128                         String insertUri = replies.get("InsertURI" + identityIndex);
129                         ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri));
130                 }
131                 return ownIdentities;
132         }
133
134         /**
135          * Returns the trust given to the identity with the given identifier by the
136          * given own identity.
137          *
138          * @param ownIdentity
139          *            The own identity that is used to calculate trust values
140          * @param identifier
141          *            The identifier of the identity whose trust to get
142          * @return The request identity trust
143          * @throws IOException
144          *             if an I/O error occurs
145          * @throws FcpException
146          *             if an FCP error occurs
147          */
148         public CalculatedTrust getIdentityTrust(OwnIdentity ownIdentity, String identifier) throws IOException, FcpException {
149                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetIdentity", "TreeOwner", ownIdentity.getIdentifier(), "Identity", identifier));
150                 if (!replies.get("Message").equals("Identity")) {
151                         throw new FcpException("WebOfTrust Plugin did not reply with “Identity” message!");
152                 }
153                 Byte trust = null;
154                 try {
155                         trust = Byte.valueOf(replies.get("Trust"));
156                 } catch (NumberFormatException nfe1) {
157                         /* ignore. */
158                 }
159                 Integer score = null;
160                 try {
161                         score = Integer.valueOf(replies.get("Score"));
162                 } catch (NumberFormatException nfe1) {
163                         /* ignore. */
164                 }
165                 Integer rank = null;
166                 try {
167                         rank = Integer.valueOf(replies.get("Rank"));
168                 } catch (NumberFormatException nfe1) {
169                         /* ignore. */
170                 }
171                 return new CalculatedTrust(trust, score, rank);
172         }
173
174         /**
175          * Adds a new identity by its request URI.
176          *
177          * @param requestUri
178          *            The request URI of the identity to add
179          * @return The added identity
180          * @throws IOException
181          *             if an I/O error occurs
182          * @throws FcpException
183          *             if an FCP error occurs
184          */
185         public Identity addIdentity(String requestUri) throws IOException, FcpException {
186                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "AddIdentity", "RequestURI", requestUri));
187                 if (!replies.get("Message").equals("IdentityAdded")) {
188                         throw new FcpException("WebOfTrust Plugin did not reply with “IdentityAdded” message!");
189                 }
190                 String identifier = replies.get("ID");
191                 String nickname = replies.get("Nickname");
192                 return new Identity(identifier, nickname, requestUri);
193         }
194
195         /**
196          * Returns identities by the given score.
197          *
198          * @param ownIdentity
199          *            The own identity
200          * @param context
201          *            The context to get the identities for
202          * @param positive
203          *            {@code null} to return neutrally trusted identities, {@code
204          *            true} to return positively trusted identities, {@code false}
205          *            for negatively trusted identities
206          * @return The trusted identites
207          * @throws IOException
208          *             if an I/O error occurs
209          * @throws FcpException
210          *             if an FCP error occurs
211          */
212         public Set<Identity> getIdentitesByScore(OwnIdentity ownIdentity, String context, Boolean positive) throws IOException, FcpException {
213                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetIdentitiesByScore", "TreeOwner", ownIdentity.getIdentifier(), "Context", context, "Selection", ((positive == null) ? "0" : (positive ? "+" : "-"))));
214                 if (!replies.get("Message").equals("Identities")) {
215                         throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
216                 }
217                 Set<Identity> identities = new HashSet<Identity>();
218                 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
219                         String identifier = replies.get("Identity" + identityIndex);
220                         String nickname = replies.get("Nickname" + identityIndex);
221                         String requestUri = replies.get("RequestURI" + identityIndex);
222                         identities.add(new Identity(identifier, nickname, requestUri));
223                 }
224                 return identities;
225         }
226
227         /**
228          * Returns the identities that trust the given identity.
229          *
230          * @param identity
231          *            The identity to get the trusters for
232          * @param context
233          *            The context to get the trusters for
234          * @return The identities and their trust values
235          * @throws IOException
236          *             if an I/O error occurs
237          * @throws FcpException
238          *             if an FCP error occurs
239          */
240         public Map<Identity, IdentityTrust> getTrusters(Identity identity, String context) throws IOException, FcpException {
241                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetTrusters", "Identity", identity.getIdentifier(), "Context", context));
242                 if (!replies.get("Message").equals("Identities")) {
243                         throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
244                 }
245                 Map<Identity, IdentityTrust> identityTrusts = new HashMap<Identity, IdentityTrust>();
246                 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
247                         String identifier = replies.get("Identity" + identityIndex);
248                         String nickname = replies.get("Nickname" + identityIndex);
249                         String requestUri = replies.get("RequestURI" + identityIndex);
250                         byte trust = Byte.parseByte(replies.get("Value" + identityIndex));
251                         String comment = replies.get("Comment" + identityIndex);
252                         identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment));
253                 }
254                 return identityTrusts;
255         }
256
257         /**
258          * Returns the identities that given identity trusts.
259          *
260          * @param identity
261          *            The identity to get the trustees for
262          * @param context
263          *            The context to get the trustees for
264          * @return The identities and their trust values
265          * @throws IOException
266          *             if an I/O error occurs
267          * @throws FcpException
268          *             if an FCP error occurs
269          */
270         public Map<Identity, IdentityTrust> getTrustees(Identity identity, String context) throws IOException, FcpException {
271                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetTrustees", "Identity", identity.getIdentifier(), "Context", context));
272                 if (!replies.get("Message").equals("Identities")) {
273                         throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
274                 }
275                 Map<Identity, IdentityTrust> identityTrusts = new HashMap<Identity, IdentityTrust>();
276                 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
277                         String identifier = replies.get("Identity" + identityIndex);
278                         String nickname = replies.get("Nickname" + identityIndex);
279                         String requestUri = replies.get("RequestURI" + identityIndex);
280                         byte trust = Byte.parseByte(replies.get("Value" + identityIndex));
281                         String comment = replies.get("Comment" + identityIndex);
282                         identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment));
283                 }
284                 return identityTrusts;
285         }
286
287         /**
288          * Sets the trust given to the given identify by the given own identity.
289          *
290          * @param ownIdentity
291          *            The identity that gives the trust
292          * @param identity
293          *            The identity that receives the trust
294          * @param trust
295          *            The trust value (ranging from {@code -100} to {@code 100}
296          * @param comment
297          *            The comment for setting the trust
298          * @throws IOException
299          *             if an I/O error occurs
300          * @throws FcpException
301          *             if an FCP error occurs
302          */
303         public void setTrust(OwnIdentity ownIdentity, Identity identity, byte trust, String comment) throws IOException, FcpException {
304                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "SetTrust", "Truster", ownIdentity.getIdentifier(), "Trustee", identity.getIdentifier(), "Value", String.valueOf(trust), "Comment", comment));
305                 if (!replies.get("Message").equals("TrustSet")) {
306                         throw new FcpException("WebOfTrust Plugin did not reply with “TrustSet” message!");
307                 }
308         }
309
310         /**
311          * Adds the given context to the given identity.
312          *
313          * @param ownIdentity
314          *            The identity to add the context to
315          * @param context
316          *            The context to add
317          * @throws IOException
318          *             if an I/O error occurs
319          * @throws FcpException
320          *             if an FCP error occurs
321          */
322         public void addContext(OwnIdentity ownIdentity, String context) throws IOException, FcpException {
323                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "AddContext", "Identity", ownIdentity.getIdentifier(), "Context", context));
324                 if (!replies.get("Message").equals("ContextAdded")) {
325                         throw new FcpException("WebOfTrust Plugin did not reply with “ContextAdded” message!");
326                 }
327         }
328
329         /**
330          * Removes the given context from the given identity.
331          *
332          * @param ownIdentity
333          *            The identity to remove the context from
334          * @param context
335          *            The context to remove
336          * @throws IOException
337          *             if an I/O error occurs
338          * @throws FcpException
339          *             if an FCP error occurs
340          */
341         public void removeContext(OwnIdentity ownIdentity, String context) throws IOException, FcpException {
342                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "RemoveContext", "Identity", ownIdentity.getIdentifier(), "Context", context));
343                 if (!replies.get("Message").equals("ContextRemoved")) {
344                         throw new FcpException("WebOfTrust Plugin did not reply with “ContextRemoved” message!");
345                 }
346         }
347
348         /**
349          * Sets the given property for the given identity.
350          *
351          * @param ownIdentity
352          *            The identity to set a property for
353          * @param property
354          *            The name of the property to set
355          * @param value
356          *            The value of the property to set
357          * @throws IOException
358          *             if an I/O error occurs
359          * @throws FcpException
360          *             if an FCP error occurs
361          */
362         public void setProperty(OwnIdentity ownIdentity, String property, String value) throws IOException, FcpException {
363                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "SetProperty", "Identity", ownIdentity.getIdentifier(), "Property", property, "Value", value));
364                 if (!replies.get("Message").equals("PropertyAdded")) {
365                         throw new FcpException("WebOfTrust Plugin did not reply with “PropertyAdded” message!");
366                 }
367         }
368
369         /**
370          * Returns the value of the given property for the given identity.
371          *
372          * @param ownIdentity
373          *            The identity to get a property for
374          * @param property
375          *            The name of the property to get
376          * @return The value of the property
377          * @throws IOException
378          *             if an I/O error occurs
379          * @throws FcpException
380          *             if an FCP error occurs
381          */
382         public String getProperty(OwnIdentity ownIdentity, String property) throws IOException, FcpException {
383                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetProperty", "Identity", ownIdentity.getIdentifier(), "Property", property));
384                 if (!replies.get("Message").equals("PropertyValue")) {
385                         throw new FcpException("WebOfTrust Plugin did not reply with “PropertyValue” message!");
386                 }
387                 return replies.get("Property");
388         }
389
390         /**
391          * Removes the given property from the given identity.
392          *
393          * @param ownIdentity
394          *            The identity to remove a property from
395          * @param property
396          *            The name of the property to remove
397          * @throws IOException
398          *             if an I/O error occurs
399          * @throws FcpException
400          *             if an FCP error occurs
401          */
402         public void removeProperty(OwnIdentity ownIdentity, String property) throws IOException, FcpException {
403                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "RemoveProperty", "Identity", ownIdentity.getIdentifier(), "Property", property));
404                 if (!replies.get("Message").equals("PropertyRemoved")) {
405                         throw new FcpException("WebOfTrust Plugin did not reply with “PropertyRemoved” message!");
406                 }
407         }
408
409         //
410         // PRIVATE METHODS
411         //
412
413         /**
414          * Creates a map from each pair of parameters in the given array.
415          *
416          * @param parameters
417          *            The array of parameters
418          * @return The map created from the array
419          * @throws ArrayIndexOutOfBoundsException
420          *             if the given parameter array does not contains an even number
421          *             of elements
422          */
423         private Map<String, String> createParameters(String... parameters) throws ArrayIndexOutOfBoundsException {
424                 Map<String, String> parameterMap = new HashMap<String, String>();
425                 for (int index = 0; index < parameters.length; index += 2) {
426                         parameterMap.put(parameters[index], parameters[index + 1]);
427                 }
428                 return parameterMap;
429         }
430
431 }