Add “GetPosts” FCP command.
[Sone.git] / src / main / java / net / pterodactylus / sone / fcp / FcpInterface.java
1 /*
2  * Sone - FcpInterface.java - Copyright © 2011 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.fcp;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import net.pterodactylus.sone.core.Core;
25 import net.pterodactylus.sone.freenet.fcp.Command;
26 import net.pterodactylus.sone.freenet.fcp.FcpException;
27 import net.pterodactylus.sone.freenet.fcp.Command.AccessType;
28 import net.pterodactylus.sone.freenet.fcp.Command.Response;
29 import freenet.pluginmanager.FredPluginFCP;
30 import freenet.pluginmanager.PluginNotFoundException;
31 import freenet.pluginmanager.PluginReplySender;
32 import freenet.support.SimpleFieldSet;
33 import freenet.support.api.Bucket;
34
35 /**
36  * Implementation of an FCP interface for other clients or plugins to
37  * communicate with Sone.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class FcpInterface {
42
43         /** All available FCP commands. */
44         private final Map<String, Command> commands = Collections.synchronizedMap(new HashMap<String, Command>());
45
46         /**
47          * Creates a new FCP interface.
48          *
49          * @param core
50          *            The core
51          */
52         public FcpInterface(Core core) {
53                 commands.put("Version", new VersionCommand());
54                 commands.put("GetLocalSones", new GetLocalSonesCommand(core));
55                 commands.put("GetPosts", new GetPostsCommand(core));
56                 commands.put("GetPostFeed", new GetPostFeedCommand(core));
57         }
58
59         /**
60          * Handles a plugin FCP request.
61          *
62          * @param pluginReplySender
63          *            The reply sender
64          * @param parameters
65          *            The message parameters
66          * @param data
67          *            The message data (may be {@code null})
68          * @param accessType
69          *            One of {@link FredPluginFCP#ACCESS_DIRECT},
70          *            {@link FredPluginFCP#ACCESS_FCP_FULL},
71          *            {@link FredPluginFCP#ACCESS_FCP_RESTRICTED}
72          */
73         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
74                 Command command = commands.get(parameters.get("Message"));
75                 if (command == null) {
76                         /* TODO - return error? */
77                         return;
78                 }
79                 String identifier = parameters.get("Identifier");
80                 if ((identifier == null) || (identifier.length() == 0)) {
81                         /* TODO - return error? */
82                         return;
83                 }
84                 try {
85                         Response reply = command.execute(parameters, data, AccessType.values()[accessType]);
86                         SimpleFieldSet replyParameters = reply.getReplyParameters();
87                         replyParameters.putOverwrite("Identifier", identifier);
88                         if (reply.hasData()) {
89                                 pluginReplySender.send(replyParameters, reply.getData());
90                         } else if (reply.hasBucket()) {
91                                 pluginReplySender.send(replyParameters, reply.getBucket());
92                         } else {
93                                 pluginReplySender.send(replyParameters);
94                         }
95                 } catch (FcpException fe1) {
96                         /* TODO - log, report */
97                 } catch (PluginNotFoundException pnfe1) {
98                         /* TODO - log */
99                 }
100         }
101
102 }