src/org/sonews/daemon/command/XDaemonCommand.java
author cli
Sun Sep 11 14:19:19 2011 +0200 (2011-09-11)
changeset 47 e118b4d60029
parent 46 28870db3b9fd
child 48 b78e77619152
permissions -rwxr-xr-x
Complete XDAEMON GROUPFLAG subcommand.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.daemon.command;
    20 
    21 import java.io.IOException;
    22 import java.net.InetSocketAddress;
    23 import java.util.List;
    24 import org.sonews.config.Config;
    25 import org.sonews.daemon.NNTPConnection;
    26 import org.sonews.storage.StorageBackendException;
    27 import org.sonews.storage.StorageManager;
    28 import org.sonews.feed.FeedManager;
    29 import org.sonews.feed.Subscription;
    30 import org.sonews.storage.Channel;
    31 import org.sonews.storage.Group;
    32 import org.sonews.util.Stats;
    33 
    34 /**
    35  * The XDAEMON command allows a client to get/set properties of the
    36  * running server daemon. Only locally connected clients are allowed to
    37  * use this command.
    38  * The restriction to localhost connection can be suppressed by overriding
    39  * the sonews.xdaemon.host bootstrap config property.
    40  * @author Christian Lins
    41  * @since sonews/0.5.0
    42  */
    43 public class XDaemonCommand implements Command
    44 {
    45 
    46 	@Override
    47 	public String[] getSupportedCommandStrings()
    48 	{
    49 		return new String[] {"XDAEMON"};
    50 	}
    51 
    52 	@Override
    53 	public boolean hasFinished()
    54 	{
    55 		return true;
    56 	}
    57 
    58 	@Override
    59 	public String impliedCapability()
    60 	{
    61 		return null;
    62 	}
    63 
    64 	@Override
    65 	public boolean isStateful()
    66 	{
    67 		return false;
    68 	}
    69 
    70 	private void channelAdd(String[] commands, NNTPConnection conn)
    71 		throws IOException, StorageBackendException
    72 	{
    73 		String groupName = commands[2];
    74 		if (StorageManager.current().isGroupExisting(groupName)) {
    75 			conn.println("400 group " + groupName + " already existing!");
    76 		} else {
    77 			StorageManager.current().addGroup(groupName, Integer.parseInt(commands[3]));
    78 			conn.println("200 group " + groupName + " created");
    79 		}
    80 	}
    81 
    82 	// TODO: Refactor this method to reduce complexity!
    83 	@Override
    84 	public void processLine(NNTPConnection conn, String line, byte[] raw)
    85 		throws IOException, StorageBackendException
    86 	{
    87 		InetSocketAddress addr = (InetSocketAddress) conn.getSocketChannel().socket().getRemoteSocketAddress();
    88 		if (addr.getHostName().equals(
    89 			Config.inst().get(Config.XDAEMON_HOST, "localhost"))) {
    90 			String[] commands = line.split(" ", 4);
    91 			if (commands.length == 3 && commands[1].equalsIgnoreCase("LIST")) {
    92 				if (commands[2].equalsIgnoreCase("CONFIGKEYS")) {
    93 					conn.println("100 list of available config keys follows");
    94 					for (String key : Config.AVAILABLE_KEYS) {
    95 						conn.println(key);
    96 					}
    97 					conn.println(".");
    98 				} else if (commands[2].equalsIgnoreCase("PEERINGRULES")) {
    99 					List<Subscription> pull =
   100 						StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL);
   101 					List<Subscription> push =
   102 						StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH);
   103 					conn.println("100 list of peering rules follows");
   104 					for (Subscription sub : pull) {
   105 						conn.println("PULL " + sub.getHost() + ":" + sub.getPort()
   106 							+ " " + sub.getGroup());
   107 					}
   108 					for (Subscription sub : push) {
   109 						conn.println("PUSH " + sub.getHost() + ":" + sub.getPort()
   110 							+ " " + sub.getGroup());
   111 					}
   112 					conn.println(".");
   113 				} else {
   114 					conn.println("401 unknown sub command");
   115 				}
   116 			} else if (commands.length == 3 && commands[1].equalsIgnoreCase("DELETE")) {
   117 				StorageManager.current().delete(commands[2]);
   118 				conn.println("200 article " + commands[2] + " deleted");
   119 			} else if (commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD")) {
   120 				channelAdd(commands, conn);
   121 			} else if (commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL")) {
   122 				Group group = StorageManager.current().getGroup(commands[2]);
   123 				if (group == null) {
   124 					conn.println("400 group not found");
   125 				} else {
   126 					group.setFlag(Group.DELETED);
   127 					group.update();
   128 					conn.println("200 group " + commands[2] + " marked as deleted");
   129 				}
   130 			} else if(commands.length == 5 && commands[1].equalsIgnoreCase("GROUPFLAG")) {
   131 				Group group = StorageManager.current().getGroup(commands[2]);
   132 				String flagName = commands[4];
   133 				if(commands[3].equalsIgnoreCase("SET")) {
   134 					if(flagName.equals("MAILINGLIST")) {
   135 						group.setFlag(Channel.MAILINGLIST);
   136 					} else if(flagName.equals("DELETED")) {
   137 						group.setFlag(Channel.DELETED);
   138 					} else if(flagName.equals("READONLY")) {
   139 						group.setFlag(Channel.READONLY);
   140 					}
   141 				} else if(commands[3].equalsIgnoreCase("UNSET")) {
   142 					if(flagName.equals("MAILINGLIST")) {
   143 						group.unsetFlag(Channel.MAILINGLIST);
   144 					} else if(flagName.equals("DELETED")) {
   145 						group.unsetFlag(Channel.DELETED);
   146 					} else if(flagName.equals("READONLY")) {
   147 						group.unsetFlag(Channel.READONLY);
   148 					}
   149 				} else {
   150 					conn.println("500 invalid command usage");
   151 				}
   152 				StorageManager.current().update(group);
   153 			} else if (commands.length == 4 && commands[1].equalsIgnoreCase("SET")) {
   154 				String key = commands[2];
   155 				String val = commands[3];
   156 				Config.inst().set(key, val);
   157 				conn.println("200 new config value set");
   158 			} else if (commands.length == 3 && commands[1].equalsIgnoreCase("GET")) {
   159 				String key = commands[2];
   160 				String val = Config.inst().get(key, null);
   161 				if (val != null) {
   162 					conn.println("100 config value for " + key + " follows");
   163 					conn.println(val);
   164 					conn.println(".");
   165 				} else {
   166 					conn.println("400 config value not set");
   167 				}
   168 			} else if (commands.length >= 3 && commands[1].equalsIgnoreCase("LOG")) {
   169 				Group group = null;
   170 				if (commands.length > 3) {
   171 					group = (Group) Channel.getByName(commands[3]);
   172 				}
   173 
   174 				if (commands[2].equalsIgnoreCase("CONNECTED_CLIENTS")) {
   175 					conn.println("100 number of connections follow");
   176 					conn.println(Integer.toString(Stats.getInstance().connectedClients()));
   177 					conn.println(".");
   178 				} else if (commands[2].equalsIgnoreCase("POSTED_NEWS")) {
   179 					conn.println("100 hourly numbers of posted news yesterday");
   180 					for (int n = 0; n < 24; n++) {
   181 						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
   182 					}
   183 					conn.println(".");
   184 				} else if (commands[2].equalsIgnoreCase("GATEWAYED_NEWS")) {
   185 					conn.println("100 hourly numbers of gatewayed news yesterday");
   186 					for (int n = 0; n < 24; n++) {
   187 						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
   188 					}
   189 					conn.println(".");
   190 				} else if (commands[2].equalsIgnoreCase("TRANSMITTED_NEWS")) {
   191 					conn.println("100 hourly numbers of news transmitted to peers yesterday");
   192 					for (int n = 0; n < 24; n++) {
   193 						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
   194 					}
   195 					conn.println(".");
   196 				} else if (commands[2].equalsIgnoreCase("HOSTED_NEWS")) {
   197 					conn.println("100 number of overall hosted news");
   198 					conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
   199 					conn.println(".");
   200 				} else if (commands[2].equalsIgnoreCase("HOSTED_GROUPS")) {
   201 					conn.println("100 number of hosted groups");
   202 					conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
   203 					conn.println(".");
   204 				} else if (commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR")) {
   205 					conn.println("100 posted news per hour");
   206 					conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
   207 					conn.println(".");
   208 				} else if (commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR")) {
   209 					conn.println("100 feeded news per hour");
   210 					conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
   211 					conn.println(".");
   212 				} else if (commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR")) {
   213 					conn.println("100 gatewayed news per hour");
   214 					conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
   215 					conn.println(".");
   216 				} else {
   217 					conn.println("401 unknown sub command");
   218 				}
   219 			} else if (commands.length >= 3 && commands[1].equalsIgnoreCase("PLUGIN")) {
   220 				conn.println("400 invalid command usage");
   221 			} else {
   222 				conn.println("400 invalid command usage");
   223 			}
   224 		} else {
   225 			conn.println("501 not allowed");
   226 		}
   227 	}
   228 }