chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package org.sonews.daemon.command; chris@1: chris@1: import java.io.IOException; chris@1: import java.net.InetSocketAddress; chris@1: import java.util.List; chris@3: import org.sonews.config.Config; chris@1: import org.sonews.daemon.NNTPConnection; chris@3: import org.sonews.storage.StorageBackendException; chris@3: import org.sonews.storage.StorageManager; chris@1: import org.sonews.feed.FeedManager; chris@1: import org.sonews.feed.Subscription; chris@3: import org.sonews.storage.Group; chris@1: import org.sonews.util.Stats; chris@1: chris@1: /** chris@1: * The XDAEMON command allows a client to get/set properties of the chris@1: * running server daemon. Only locally connected clients are allowed to chris@1: * use this command. chris@1: * The restriction to localhost connection can be suppressed by overriding chris@1: * the sonews.xdaemon.host bootstrap config property. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ chris@3: public class XDaemonCommand implements Command chris@1: { chris@3: chris@3: @Override chris@3: public String[] getSupportedCommandStrings() chris@1: { chris@3: return new String[]{"XDAEMON"}; chris@1: } chris@1: chris@1: @Override chris@1: public boolean hasFinished() chris@1: { chris@1: return true; chris@1: } chris@1: chris@3: @Override cli@20: public String impliedCapability() cli@20: { cli@20: return null; cli@20: } cli@20: cli@20: @Override chris@3: public boolean isStateful() chris@3: { chris@3: return false; chris@3: } chris@3: cli@23: private void channelAdd(String[] commands, NNTPConnection conn) cli@23: throws IOException, StorageBackendException cli@23: { cli@23: String groupName = commands[2]; cli@23: if(StorageManager.current().isGroupExisting(groupName)) cli@23: { cli@23: conn.println("400 group " + groupName + " already existing!"); cli@23: } cli@23: else cli@23: { cli@23: StorageManager.current().addGroup(groupName, Integer.parseInt(commands[3])); cli@23: conn.println("200 group " + groupName + " created"); cli@23: } cli@23: } cli@23: chris@1: // TODO: Refactor this method to reduce complexity! chris@1: @Override chris@3: public void processLine(NNTPConnection conn, String line, byte[] raw) chris@3: throws IOException, StorageBackendException chris@1: { chris@3: InetSocketAddress addr = (InetSocketAddress)conn.getSocketChannel().socket() chris@1: .getRemoteSocketAddress(); chris@1: if(addr.getHostName().equals( chris@3: Config.inst().get(Config.XDAEMON_HOST, "localhost"))) chris@1: { chris@1: String[] commands = line.split(" ", 4); chris@1: if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST")) chris@1: { chris@1: if(commands[2].equalsIgnoreCase("CONFIGKEYS")) chris@1: { chris@3: conn.println("100 list of available config keys follows"); chris@1: for(String key : Config.AVAILABLE_KEYS) chris@1: { chris@3: conn.println(key); chris@1: } chris@3: conn.println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("PEERINGRULES")) chris@1: { chris@1: List pull = chris@3: StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL); chris@1: List push = chris@3: StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH); chris@3: conn.println("100 list of peering rules follows"); chris@1: for(Subscription sub : pull) chris@1: { chris@3: conn.println("PULL " + sub.getHost() + ":" + sub.getPort() chris@1: + " " + sub.getGroup()); chris@1: } chris@1: for(Subscription sub : push) chris@1: { chris@3: conn.println("PUSH " + sub.getHost() + ":" + sub.getPort() chris@1: + " " + sub.getGroup()); chris@1: } chris@3: conn.println("."); chris@1: } chris@1: else chris@1: { chris@3: conn.println("401 unknown sub command"); chris@1: } chris@1: } chris@1: else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE")) chris@1: { chris@3: StorageManager.current().delete(commands[2]); chris@3: conn.println("200 article " + commands[2] + " deleted"); chris@1: } chris@1: else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD")) chris@1: { cli@23: channelAdd(commands, conn); chris@1: } chris@1: else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL")) chris@1: { chris@3: Group group = StorageManager.current().getGroup(commands[2]); chris@1: if(group == null) chris@1: { chris@3: conn.println("400 group not found"); chris@1: } chris@1: else chris@1: { chris@1: group.setFlag(Group.DELETED); chris@3: group.update(); chris@3: conn.println("200 group " + commands[2] + " marked as deleted"); chris@1: } chris@1: } chris@1: else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET")) chris@1: { chris@1: String key = commands[2]; chris@1: String val = commands[3]; chris@3: Config.inst().set(key, val); chris@3: conn.println("200 new config value set"); chris@1: } chris@1: else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET")) chris@1: { chris@1: String key = commands[2]; chris@3: String val = Config.inst().get(key, null); chris@1: if(val != null) chris@1: { chris@3: conn.println("100 config value for " + key + " follows"); chris@3: conn.println(val); chris@3: conn.println("."); chris@1: } chris@1: else chris@1: { chris@3: conn.println("400 config value not set"); chris@1: } chris@1: } chris@1: else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG")) chris@1: { chris@1: Group group = null; chris@1: if(commands.length > 3) chris@1: { chris@1: group = Group.getByName(commands[3]); chris@1: } chris@1: chris@1: if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS")) chris@1: { chris@3: conn.println("100 number of connections follow"); chris@3: conn.println(Integer.toString(Stats.getInstance().connectedClients())); chris@3: conn.println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("POSTED_NEWS")) chris@1: { chris@3: conn.println("100 hourly numbers of posted news yesterday"); chris@1: for(int n = 0; n < 24; n++) chris@1: { chris@3: conn.println(n + " " + Stats.getInstance() chris@1: .getYesterdaysEvents(Stats.POSTED_NEWS, n, group)); chris@1: } chris@3: conn.println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS")) chris@1: { chris@3: conn.println("100 hourly numbers of gatewayed news yesterday"); chris@1: for(int n = 0; n < 24; n++) chris@1: { chris@3: conn.println(n + " " + Stats.getInstance() chris@1: .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group)); chris@1: } chris@3: conn.println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS")) chris@1: { chris@3: conn.println("100 hourly numbers of news transmitted to peers yesterday"); chris@1: for(int n = 0; n < 24; n++) chris@1: { chris@3: conn.println(n + " " + Stats.getInstance() chris@1: .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group)); chris@1: } chris@3: conn.println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("HOSTED_NEWS")) chris@1: { chris@3: conn.println("100 number of overall hosted news"); chris@3: conn.println(Integer.toString(Stats.getInstance().getNumberOfNews())); chris@3: conn.println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS")) chris@1: { chris@3: conn.println("100 number of hosted groups"); chris@3: conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups())); chris@3: conn.println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR")) chris@1: { chris@3: conn.println("100 posted news per hour"); chris@3: conn.println(Double.toString(Stats.getInstance().postedPerHour(-1))); chris@3: conn.println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR")) chris@1: { chris@3: conn.println("100 feeded news per hour"); chris@3: conn.println(Double.toString(Stats.getInstance().feededPerHour(-1))); chris@3: conn.println("."); chris@1: } chris@1: else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR")) chris@1: { chris@3: conn.println("100 gatewayed news per hour"); chris@3: conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1))); chris@3: conn.println("."); chris@1: } chris@1: else chris@1: { chris@3: conn.println("401 unknown sub command"); chris@1: } chris@1: } cli@21: else if(commands.length >= 3 && commands[1].equalsIgnoreCase("PLUGIN")) cli@21: { cli@21: cli@21: } chris@1: else chris@1: { chris@3: conn.println("400 invalid command usage"); chris@1: } chris@1: } chris@1: else chris@1: { chris@3: conn.println("501 not allowed"); chris@1: } chris@1: } chris@1: chris@1: }