Implement GetTrusters message.
[jFCPlib.git] / src / net / pterodactylus / fcp / plugin / WebOfTrustPlugin.java
index ba9ad3a..0650d76 100644 (file)
@@ -133,6 +133,152 @@ public class WebOfTrustPlugin {
                return ownIdentities;
        }
 
+       /**
+        * Returns the trust given to the identity with the given identifier by the
+        * given own identity.
+        *
+        * @param ownIdentity
+        *            The own identity that is used to calculate trust values
+        * @param identifier
+        *            The identifier of the identity whose trust to get
+        * @return The request identity trust
+        * @throws IOException
+        *             if an I/O error occurs
+        * @throws FcpException
+        *             if an FCP error occurs
+        */
+       public CalculatedTrust getIdentityTrust(OwnIdentity ownIdentity, String identifier) throws IOException, FcpException {
+               Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetIdentity", "TreeOwner", ownIdentity.getIdentifier(), "Identity", identifier));
+               if (!replies.get("Message").equals("Identity")) {
+                       throw new FcpException("WebOfTrust Plugin did not reply with “Identity” message!");
+               }
+               Byte trust = null;
+               try {
+                       trust = Byte.valueOf(replies.get("Trust"));
+               } catch (NumberFormatException nfe1) {
+                       /* ignore. */
+               }
+               Integer score = null;
+               try {
+                       score = Integer.valueOf(replies.get("Score"));
+               } catch (NumberFormatException nfe1) {
+                       /* ignore. */
+               }
+               Integer rank = null;
+               try {
+                       rank = Integer.valueOf(replies.get("Rank"));
+               } catch (NumberFormatException nfe1) {
+                       /* ignore. */
+               }
+               return new CalculatedTrust(trust, score, rank);
+       }
+
+       /**
+        * Adds a new identity by its request URI.
+        *
+        * @param requestUri
+        *            The request URI of the identity to add
+        * @return The added identity
+        * @throws IOException
+        *             if an I/O error occurs
+        * @throws FcpException
+        *             if an FCP error occurs
+        */
+       public Identity addIdentity(String requestUri) throws IOException, FcpException {
+               Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "AddIdentity", "RequestURI", requestUri));
+               if (!replies.get("Message").equals("IdentityAdded")) {
+                       throw new FcpException("WebOfTrust Plugin did not reply with “IdentityAdded” message!");
+               }
+               String identifier = replies.get("ID");
+               String nickname = replies.get("Nickname");
+               return new Identity(identifier, nickname, requestUri);
+       }
+
+       /**
+        * Returns identities by the given score.
+        *
+        * @param ownIdentity
+        *            The own identity
+        * @param context
+        *            The context to get the identities for
+        * @param positive
+        *            {@code null} to return neutrally trusted identities, {@code
+        *            true} to return positively trusted identities, {@code false}
+        *            for negatively trusted identities
+        * @return The trusted identites
+        * @throws IOException
+        *             if an I/O error occurs
+        * @throws FcpException
+        *             if an FCP error occurs
+        */
+       public Set<Identity> getIdentitesByScore(OwnIdentity ownIdentity, String context, Boolean positive) throws IOException, FcpException {
+               Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetIdentitesByScore", "TreeOwner", ownIdentity.getIdentifier(), "Context", context, "Selection", ((positive == null) ? "0" : (positive ? "+" : "-"))));
+               if (!replies.get("Message").equals("Identities")) {
+                       throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
+               }
+               Set<Identity> identities = new HashSet<Identity>();
+               for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
+                       String identifier = replies.get("Identity" + identityIndex);
+                       String nickname = replies.get("Nickname" + identityIndex);
+                       String requestUri = replies.get("RequestURI" + identityIndex);
+                       identities.add(new Identity(identifier, nickname, requestUri));
+               }
+               return identities;
+       }
+
+       /**
+        * Returns the identities that trust the given identity.
+        *
+        * @param identity
+        *            The identity to get the trusters for
+        * @param context
+        *            The context to get the trusters for
+        * @return The identities and their trust values
+        * @throws IOException
+        *             if an I/O error occurs
+        * @throws FcpException
+        *             if an FCP error occurs
+        */
+       public Map<Identity, IdentityTrust> getTrusters(Identity identity, String context) throws IOException, FcpException {
+               Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetTrusters", "Identity", identity.getIdentifier(), "Context", context));
+               if (!replies.get("Message").equals("Identities")) {
+                       throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
+               }
+               Map<Identity, IdentityTrust> identityTrusts = new HashMap<Identity, IdentityTrust>();
+               for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
+                       String identifier = replies.get("Identity" + identityIndex);
+                       String nickname = replies.get("Nickname" + identityIndex);
+                       String requestUri = replies.get("RequestURI" + identityIndex);
+                       byte trust = Byte.parseByte(replies.get("Value" + identityIndex));
+                       String comment = replies.get("Comment" + identityIndex);
+                       identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment));
+               }
+               return identityTrusts;
+       }
+
+       /**
+        * Sets the trust given to the given identify by the given own identity.
+        *
+        * @param ownIdentity
+        *            The identity that gives the trust
+        * @param identity
+        *            The identity that receives the trust
+        * @param trust
+        *            The trust value (ranging from {@code -100} to {@code 100}
+        * @param comment
+        *            The comment for setting the trust
+        * @throws IOException
+        *             if an I/O error occurs
+        * @throws FcpException
+        *             if an FCP error occurs
+        */
+       public void setTrust(OwnIdentity ownIdentity, Identity identity, byte trust, String comment) throws IOException, FcpException {
+               Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "SetTrust", "Truster", ownIdentity.getIdentifier(), "Trustee", identity.getIdentifier(), "Value", String.valueOf(trust), "Comment", comment));
+               if (!replies.get("Message").equals("TrustSet")) {
+                       throw new FcpException("WebOfTrust Plugin did not reply with “TrustSet” message!");
+               }
+       }
+
        //
        // PRIVATE METHODS
        //
@@ -171,15 +317,6 @@ public class WebOfTrustPlugin {
                /** The identity’s request URI. */
                private final String requestUri;
 
-               /** The identity’s trust value. */
-               private final Byte trust;
-
-               /** The identity’s score value. */
-               private final Integer score;
-
-               /** The identity’s rank. */
-               private final Integer rank;
-
                /**
                 * Creates a new identity.
                 *
@@ -189,20 +326,11 @@ public class WebOfTrustPlugin {
                 *            The nickname of the identity
                 * @param requestUri
                 *            The request URI of the identity
-                * @param trust
-                *            The trust value of the identity
-                * @param score
-                *            The score value of the identity
-                * @param rank
-                *            The rank of the identity
                 */
-               public Identity(String identifier, String nickname, String requestUri, Byte trust, Integer score, Integer rank) {
+               public Identity(String identifier, String nickname, String requestUri) {
                        this.identifier = identifier;
                        this.nickname = nickname;
                        this.requestUri = requestUri;
-                       this.trust = trust;
-                       this.score = score;
-                       this.rank = rank;
                }
 
                /**
@@ -232,31 +360,111 @@ public class WebOfTrustPlugin {
                        return requestUri;
                }
 
+       }
+
+       /**
+        * Container for the trust given from one identity to another.
+        *
+        * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+        */
+       public static class IdentityTrust {
+
+               /** The trust given to the identity. */
+               private final byte trust;
+
+               /** The command for the trust value. */
+               private final String comment;
+
+               /**
+                * Creates a new identity trust container.
+                *
+                * @param trust
+                *            The trust given to the identity
+                * @param comment
+                *            The comment for the trust value
+                */
+               public IdentityTrust(byte trust, String comment) {
+                       this.trust = trust;
+                       this.comment = comment;
+               }
+
+               /**
+                * Returns the trust value given to the identity.
+                *
+                * @return The trust value
+                */
+               public byte getTrust() {
+                       return trust;
+               }
+
+               /**
+                * Returns the comment for the trust value.
+                *
+                * @return The comment for the trust value
+                */
+               public String getComment() {
+                       return comment;
+               }
+
+       }
+
+       /**
+        * Container that stores the trust that is calculated by taking all trustees
+        * and their trust lists into account.
+        *
+        * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+        */
+       public static class CalculatedTrust {
+
+               /** The calculated trust value. */
+               private final Byte trust;
+
+               /** The calculated score value. */
+               private final Integer score;
+
+               /** The calculated rank. */
+               private final Integer rank;
+
+               /**
+                * Creates a new calculated trust container.
+                *
+                * @param trust
+                *            The calculated trust value
+                * @param score
+                *            The calculated score value
+                * @param rank
+                *            The calculated rank of the
+                */
+               public CalculatedTrust(Byte trust, Integer score, Integer rank) {
+                       this.trust = trust;
+                       this.score = score;
+                       this.rank = rank;
+               }
+
                /**
-                * Returns the trust value of this identity.
+                * Returns the calculated trust value.
                 *
-                * @return This identity’s trust value, or {@code null} if this
-                *         identity’s trust value is not known
+                * @return The calculated trust value, or {@code null} if the trust
+                *         value is not known
                 */
                public Byte getTrust() {
                        return trust;
                }
 
                /**
-                * Returns the score value of this identity.
+                * Returns the calculated score value.
                 *
-                * @return This identity’s score value, or {@code null} if this
-                *         identity’s score value is not known
+                * @return The calculated score value, or {@code null} if the score
+                *         value is not known
                 */
                public Integer getScore() {
                        return score;
                }
 
                /**
-                * Returns the rank of this identity.
+                * Returns the calculated rank.
                 *
-                * @return This identity’s rank, or {@code null} if this identity’s rank
-                *         is not known
+                * @return The calculated rank, or {@code null} if the rank is not known
                 */
                public Integer getRank() {
                        return rank;