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