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