X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FFcpInterface.java;h=46ee5c2ee4e5d025e33f79976a133179269911d9;hb=d2a84d3d722e9c3df68cd5b7f86d8bf9372d7e2b;hp=8854347e106b38f4100674dc68ebe61a18800cd7;hpb=867514c843526b7e3699e3e6073c01bae0813fc7;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java index 8854347..46ee5c2 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java +++ b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java @@ -24,7 +24,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.sone.core.Core; -import net.pterodactylus.sone.freenet.fcp.Command; import net.pterodactylus.sone.freenet.fcp.Command.AccessType; import net.pterodactylus.sone.freenet.fcp.Command.ErrorResponse; import net.pterodactylus.sone.freenet.fcp.Command.Response; @@ -47,8 +46,14 @@ public class FcpInterface { /** The logger. */ private static final Logger logger = Logging.getLogger(FcpInterface.class); + /** Whether the FCP interface is currently active. */ + private volatile boolean active; + + /** Whether to allow write access from full access hosts only. */ + private volatile boolean allowWriteFromFullAccessOnly; + /** All available FCP commands. */ - private final Map commands = Collections.synchronizedMap(new HashMap()); + private final Map commands = Collections.synchronizedMap(new HashMap()); /** * Creates a new FCP interface. @@ -57,12 +62,50 @@ public class FcpInterface { * The core */ public FcpInterface(Core core) { - commands.put("Version", new VersionCommand()); + commands.put("Version", new VersionCommand(core)); commands.put("GetLocalSones", new GetLocalSonesCommand(core)); + commands.put("GetPost", new GetPostCommand(core)); commands.put("GetPosts", new GetPostsCommand(core)); commands.put("GetPostFeed", new GetPostFeedCommand(core)); + commands.put("LikePost", new LikePostCommand(core)); + commands.put("LikeReply", new LikeReplyCommand(core)); + commands.put("CreatePost", new CreatePostCommand(core)); + commands.put("CreateReply", new CreateReplyCommand(core)); + commands.put("DeletePost", new DeletePostCommand(core)); + commands.put("DeleteReply", new DeleteReplyCommand(core)); + } + + // + // ACCESSORS + // + + /** + * Sets whether the FCP interface should handle requests. If {@code active} + * is {@code false}, all requests are answered with an error. + * + * @param active + * {@code true} to activate the FCP interface, {@code false} to + * deactivate the FCP interface + */ + public void setActive(boolean active) { + this.active = active; + } + + /** + * Sets whether write access is only allowed from full access hosts. + * + * @param allowWriteFromFullAccessOnly + * {@code true} to allow write access only from full access + * hosts, {@code false} to always allow write access + */ + public void setAllowWriteFromFullAccessOnly(boolean allowWriteFromFullAccessOnly) { + this.allowWriteFromFullAccessOnly = allowWriteFromFullAccessOnly; } + // + // ACTIONS + // + /** * Handles a plugin FCP request. * @@ -78,7 +121,23 @@ public class FcpInterface { * {@link FredPluginFCP#ACCESS_FCP_RESTRICTED} */ public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) { - Command command = commands.get(parameters.get("Message")); + if (!active) { + try { + sendReply(pluginReplySender, null, new ErrorResponse(400, "FCP Interface deactivated")); + } catch (PluginNotFoundException pnfe1) { + logger.log(Level.FINE, "Could not set error to plugin.", pnfe1); + } + return; + } + AbstractSoneCommand command = commands.get(parameters.get("Message")); + if (allowWriteFromFullAccessOnly && command.requiresWriteAccess() && (accessType == FredPluginFCP.ACCESS_FCP_RESTRICTED)) { + try { + sendReply(pluginReplySender, null, new ErrorResponse(401, "No Write Access")); + } catch (PluginNotFoundException pnfe1) { + logger.log(Level.FINE, "Could not set error to plugin.", pnfe1); + } + return; + } try { if (command == null) { sendReply(pluginReplySender, null, new ErrorResponse("Unrecognized Message: " + parameters.get("Message")));