2 * jSite - WebOfTrustInterface.java - Copyright © 2012–2019 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 2 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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.application;
21 import static java.util.Collections.emptyList;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.concurrent.atomic.AtomicLong;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import net.pterodactylus.util.logging.Logging;
31 import de.todesbaum.util.freenet.fcp2.Client;
32 import de.todesbaum.util.freenet.fcp2.Connection;
33 import de.todesbaum.util.freenet.fcp2.FcpPluginMessage;
34 import de.todesbaum.util.freenet.fcp2.Message;
35 import de.todesbaum.util.freenet.fcp2.wot.DefaultOwnIdentity;
36 import de.todesbaum.util.freenet.fcp2.wot.OwnIdentity;
39 * FCP interface to the node’s web of trust.
41 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
43 public class WebOfTrustInterface {
46 private static final Logger logger = Logging.getLogger(WebOfTrustInterface.class);
48 /** Unique ID for the command identifier. */
49 private static final AtomicLong commandCounter = new AtomicLong(System.nanoTime());
51 /** The freenet interface. */
52 private final Freenet7Interface freenetInterface;
55 * Creates a new web of trust interface.
57 * @param freenetInterface
58 * The freenet interface
60 public WebOfTrustInterface(Freenet7Interface freenetInterface) {
61 this.freenetInterface = freenetInterface;
69 * Returns a list of own identities. If the identities have not yet been
70 * retrieved, an empty list is returned.
72 * @return The list of own identities
74 public List<OwnIdentity> getOwnIdentities() {
78 Connection connection = freenetInterface.getConnection("jSite-WoT-Connector");
79 logger.log(Level.INFO, String.format("Trying to connect to node at %s...", freenetInterface.getNode()));
80 if (!connection.connect()) {
81 logger.log(Level.WARNING, "Connection failed.");
84 Client client = new Client(connection);
86 /* send FCP command to WebOfTrust plugin. */
87 sendFcpCommandToWotPlugin(client);
90 Message message = null;
91 while (!client.isDisconnected() && (message == null)) {
92 message = client.readMessage(1000);
94 if (message == null) {
98 /* evaluate message. */
99 List<OwnIdentity> ownIdentities = parseOwnIdentitiesFromMessage(message);
102 logger.log(Level.INFO, "Disconnecting from Node.");
103 connection.disconnect();
104 return ownIdentities;
105 } catch (IOException ioe1) {
106 logger.log(Level.WARNING, String.format("Communication with node at %s failed.", freenetInterface.getNode()), ioe1);
111 private void sendFcpCommandToWotPlugin(Client client) throws IOException {
112 String messageIdentifier = "jSite-WoT-Command-" + commandCounter.getAndIncrement();
113 FcpPluginMessage pluginMessage = new FcpPluginMessage(messageIdentifier);
114 pluginMessage.setPluginName("plugins.WebOfTrust.WebOfTrust");
115 pluginMessage.setParameter("Message", "GetOwnIdentities");
116 client.execute(pluginMessage);
119 private List<OwnIdentity> parseOwnIdentitiesFromMessage(Message message) {
120 List<OwnIdentity> ownIdentities = new ArrayList<OwnIdentity>();
121 if (message.getName().equals("FCPPluginReply")) {
122 logger.log(Level.FINE, "Got matching Reply from WebOfTrust.");
123 /* parse identities. */
124 int identityCounter = -1;
125 while (message.get("Replies.Identity" + ++identityCounter) != null) {
126 String id = message.get("Replies.Identity" + identityCounter);
127 String nickname = message.get("Replies.Nickname" + identityCounter);
128 String requestUri = shortenUri(message.get("Replies.RequestURI" + identityCounter));
129 String insertUri = shortenUri(message.get("Replies.InsertURI" + identityCounter));
130 DefaultOwnIdentity ownIdentity = new DefaultOwnIdentity(id, nickname, requestUri, insertUri);
131 logger.log(Level.FINE, String.format("Parsed Own Identity %s.", ownIdentity));
132 ownIdentities.add(ownIdentity);
134 logger.log(Level.INFO, String.format("Parsed %d Own Identities.", ownIdentities.size()));
135 } else if ("ProtocolError".equals(message.getName())) {
136 logger.log(Level.WARNING, "WebOfTrust Plugin not found!");
137 } else if ("Error".equals(message.getName())) {
138 logger.log(Level.WARNING, "WebOfTrust Plugin returned an error!");
140 return ownIdentities;
144 * Returns the essential parts of an URI, consisting of only the
145 * private/public key, decryption key, and the flags.
149 * @return The shortened URI
151 private static String shortenUri(String uri) {
152 String shortenedUri = uri;
153 if (shortenedUri.charAt(3) == '@') {
154 shortenedUri = shortenedUri.substring(4);
156 if (shortenedUri.indexOf('/') > -1) {
157 shortenedUri = shortenedUri.substring(0, shortenedUri.indexOf('/'));