src/org/sonews/daemon/command/XDaemonCommand.java
author cli
Wed, 14 Sep 2011 22:39:22 +0200
changeset 61 da0c30d28e22
parent 48 b78e77619152
child 62 be4e87479855
permissions -rwxr-xr-x
SMTPSender should now support PLAIN SMTP Authentication (issue #8)
     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.Group;
    31 import org.sonews.util.Stats;
    32 
    33 /**
    34  * The XDAEMON command allows a client to get/set properties of the
    35  * running server daemon. Only locally connected clients are allowed to
    36  * use this command.
    37  * The restriction to localhost connection can be suppressed by overriding
    38  * the sonews.xdaemon.host bootstrap config property.
    39  * @author Christian Lins
    40  * @since sonews/0.5.0
    41  */
    42 public class XDaemonCommand implements Command
    43 {
    44 
    45 	@Override
    46 	public String[] getSupportedCommandStrings()
    47 	{
    48 		return new String[] {"XDAEMON"};
    49 	}
    50 
    51 	@Override
    52 	public boolean hasFinished()
    53 	{
    54 		return true;
    55 	}
    56 
    57 	@Override
    58 	public String impliedCapability()
    59 	{
    60 		return null;
    61 	}
    62 
    63 	@Override
    64 	public boolean isStateful()
    65 	{
    66 		return false;
    67 	}
    68 
    69 	private void channelAdd(String[] commands, NNTPConnection conn)
    70 		throws IOException, StorageBackendException
    71 	{
    72 		String groupName = commands[2];
    73 		if (StorageManager.current().isGroupExisting(groupName)) {
    74 			conn.println("400 group " + groupName + " already existing!");
    75 		} else {
    76 			StorageManager.current().addGroup(groupName, Integer.parseInt(commands[3]));
    77 			conn.println("200 group " + groupName + " created");
    78 		}
    79 	}
    80 
    81 	// TODO: Refactor this method to reduce complexity!
    82 	@Override
    83 	public void processLine(NNTPConnection conn, String line, byte[] raw)
    84 		throws IOException, StorageBackendException
    85 	{
    86 		InetSocketAddress addr = (InetSocketAddress) conn.getSocketChannel().socket().getRemoteSocketAddress();
    87 		if (addr.getHostName().equals(
    88 			Config.inst().get(Config.XDAEMON_HOST, "localhost"))) {
    89 			String[] commands = line.split(" ", 4);
    90 			if (commands.length == 3 && commands[1].equalsIgnoreCase("LIST")) {
    91 				if (commands[2].equalsIgnoreCase("CONFIGKEYS")) {
    92 					conn.println("100 list of available config keys follows");
    93 					for (String key : Config.AVAILABLE_KEYS) {
    94 						conn.println(key);
    95 					}
    96 					conn.println(".");
    97 				} else if (commands[2].equalsIgnoreCase("PEERINGRULES")) {
    98 					List<Subscription> pull =
    99 						StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL);
   100 					List<Subscription> push =
   101 						StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH);
   102 					conn.println("100 list of peering rules follows");
   103 					for (Subscription sub : pull) {
   104 						conn.println("PULL " + sub.getHost() + ":" + sub.getPort()
   105 							+ " " + sub.getGroup());
   106 					}
   107 					for (Subscription sub : push) {
   108 						conn.println("PUSH " + sub.getHost() + ":" + sub.getPort()
   109 							+ " " + sub.getGroup());
   110 					}
   111 					conn.println(".");
   112 				} else {
   113 					conn.println("401 unknown sub command");
   114 				}
   115 			} else if (commands.length == 3 && commands[1].equalsIgnoreCase("DELETE")) {
   116 				StorageManager.current().delete(commands[2]);
   117 				conn.println("200 article " + commands[2] + " deleted");
   118 			} else if (commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD")) {
   119 				channelAdd(commands, conn);
   120 			} else if (commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL")) {
   121 				Group group = StorageManager.current().getGroup(commands[2]);
   122 				if (group == null) {
   123 					conn.println("400 group not found");
   124 				} else {
   125 					group.setFlag(Group.DELETED);
   126 					group.update();
   127 					conn.println("200 group " + commands[2] + " marked as deleted");
   128 				}
   129 			} else if(commands.length == 5 && commands[1].equalsIgnoreCase("GROUPFLAG")) {
   130 				Group group = StorageManager.current().getGroup(commands[2]);
   131 				String flagName = commands[4];
   132 				if(commands[3].equalsIgnoreCase("SET")) {
   133 					if(flagName.equals("MAILINGLIST")) {
   134 						group.setFlag(Group.MAILINGLIST);
   135 					} else if(flagName.equals("DELETED")) {
   136 						group.setFlag(Group.DELETED);
   137 					} else if(flagName.equals("READONLY")) {
   138 						group.setFlag(Group.READONLY);
   139 					}
   140 				} else if(commands[3].equalsIgnoreCase("UNSET")) {
   141 					if(flagName.equals("MAILINGLIST")) {
   142 						group.unsetFlag(Group.MAILINGLIST);
   143 					} else if(flagName.equals("DELETED")) {
   144 						group.unsetFlag(Group.DELETED);
   145 					} else if(flagName.equals("READONLY")) {
   146 						group.unsetFlag(Group.READONLY);
   147 					}
   148 				} else {
   149 					conn.println("500 invalid command usage");
   150 				}
   151 				StorageManager.current().update(group);
   152 			} else if (commands.length == 4 && commands[1].equalsIgnoreCase("SET")) {
   153 				String key = commands[2];
   154 				String val = commands[3];
   155 				Config.inst().set(key, val);
   156 				conn.println("200 new config value set");
   157 			} else if (commands.length == 3 && commands[1].equalsIgnoreCase("GET")) {
   158 				String key = commands[2];
   159 				String val = Config.inst().get(key, null);
   160 				if (val != null) {
   161 					conn.println("100 config value for " + key + " follows");
   162 					conn.println(val);
   163 					conn.println(".");
   164 				} else {
   165 					conn.println("400 config value not set");
   166 				}
   167 			} else if (commands.length >= 3 && commands[1].equalsIgnoreCase("LOG")) {
   168 				Group group = null;
   169 				if (commands.length > 3) {
   170 					group = StorageManager.current().getGroup(commands[3]);
   171 				}
   172 
   173 				if (commands[2].equalsIgnoreCase("CONNECTED_CLIENTS")) {
   174 					conn.println("100 number of connections follow");
   175 					conn.println(Integer.toString(Stats.getInstance().connectedClients()));
   176 					conn.println(".");
   177 				} else if (commands[2].equalsIgnoreCase("POSTED_NEWS")) {
   178 					conn.println("100 hourly numbers of posted news yesterday");
   179 					for (int n = 0; n < 24; n++) {
   180 						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
   181 					}
   182 					conn.println(".");
   183 				} else if (commands[2].equalsIgnoreCase("GATEWAYED_NEWS")) {
   184 					conn.println("100 hourly numbers of gatewayed news yesterday");
   185 					for (int n = 0; n < 24; n++) {
   186 						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
   187 					}
   188 					conn.println(".");
   189 				} else if (commands[2].equalsIgnoreCase("TRANSMITTED_NEWS")) {
   190 					conn.println("100 hourly numbers of news transmitted to peers yesterday");
   191 					for (int n = 0; n < 24; n++) {
   192 						conn.println(n + " " + Stats.getInstance().getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
   193 					}
   194 					conn.println(".");
   195 				} else if (commands[2].equalsIgnoreCase("HOSTED_NEWS")) {
   196 					conn.println("100 number of overall hosted news");
   197 					conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
   198 					conn.println(".");
   199 				} else if (commands[2].equalsIgnoreCase("HOSTED_GROUPS")) {
   200 					conn.println("100 number of hosted groups");
   201 					conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
   202 					conn.println(".");
   203 				} else if (commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR")) {
   204 					conn.println("100 posted news per hour");
   205 					conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
   206 					conn.println(".");
   207 				} else if (commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR")) {
   208 					conn.println("100 feeded news per hour");
   209 					conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
   210 					conn.println(".");
   211 				} else if (commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR")) {
   212 					conn.println("100 gatewayed news per hour");
   213 					conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
   214 					conn.println(".");
   215 				} else {
   216 					conn.println("401 unknown sub command");
   217 				}
   218 			} else if (commands.length >= 3 && commands[1].equalsIgnoreCase("PLUGIN")) {
   219 				conn.println("400 invalid command usage");
   220 			} else {
   221 				conn.println("400 invalid command usage");
   222 			}
   223 		} else {
   224 			conn.println("501 not allowed");
   225 		}
   226 	}
   227 }