This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
PushBot.java
325 lines (269 loc) · 9.25 KB
/
PushBot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.etsy.pushbot;
import com.etsy.pushbot.command.TrainCommand;
import com.etsy.pushbot.config.ConfigServer;
import com.etsy.pushbot.config.Status;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.net.ssl.SSLSocketFactory;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.jibble.pircbot.PircBot;
/**
* This is the main entry-point for PushBot
*/
public class PushBot extends PircBot
{
HashMap<String,ChannelInfo> channelInfoMap =
new HashMap<String,ChannelInfo>();
private GraphiteLogger graphiteLogger = null;
private final String primaryChannel;
private List<String> channels = new LinkedList<String>();
/**
* @param name
* The nick of this bot
*
* @param channels
* A list of channel names (each prefixed with '#')
*
* @param ircHost
* The hostname of the iRCd server to connect to
*
* @param ircPort
* The port that IRCd is listening on
*
* @param ircPassword
* If non-null, the given password will be used when connecting to
* IRCd
*
* @param graphiteEnabled
* If true, stats will be logged to a graphite server
*
* @param graphiteHost
* An optional hostname where stats will be logged (if enabled)
*
* @param graphitePort
* An optional port that graphite is listening on
*/
public PushBot(String name, List<String> channels,
boolean graphiteEnabled, String graphiteHost, int graphitePort) {
super();
// Configure pushbot's identity
setName(name);
setFinger(name);
setLogin(name);
setVerbose(true);
// Configure pushbot's channels
this.channels = channels;
this.primaryChannel = channels.get(0);
// Configure graphite logging
if(graphiteEnabled) {
graphiteLogger = new GraphiteLogger(graphiteHost, graphitePort);
}
}
@Override
protected void onConnect() {
for(String channel : channels) {
joinChannel(channel);
}
}
@Override
protected void onJoin(String channel, String sender, String login, String hostname) {
}
@Override
protected void onDisconnect() {
try {
reconnect();
} catch(Exception e) {
e.printStackTrace();
}
}
/**
*
*/
public Status getStatus(String channel)
throws Exception {
Status status = new Status();
String topic = getTopic(channel);
PushTrain pushTrain = PushTrainReader.parse(topic);
status.isHold = pushTrain.isHold();
if(!status.isHold) {
status.driver = pushTrain.getDriver().toString();
status.head = pushTrain.getHeadMember().trim();
}
status.memberCount = pushTrain.getMemberCount();
status.isEveryoneReady = pushTrain.isHeadReady();
status.headState = pushTrain.getHeadState();
return status;
}
@Override
protected void onTopic(String channel, String topic, String setBy, long date, boolean changed) {
ChannelInfo channelInfo = channelInfoMap.get(channel);
if(channelInfo == null) {
channelInfoMap.put(channel, new ChannelInfo(channel));
channelInfo = channelInfoMap.get(channel);
}
PushTrain previousPushTrain = channelInfo.getPushTrain();
PushTrain newPushTrain;
try {
newPushTrain = PushTrainReader.parse(topic);
if(newPushTrain == null) {
channelInfo.setHasBadTopic(true);
sendMessage(channel, "Sorry, I don't understand the current topic");
sendMessage(channel, "I won't accept new commands until the topic is fixed.");
return;
}
channelInfo.setHasBadTopic(false);
} catch(Throwable t) {
t.printStackTrace();
return;
}
if(previousPushTrain != null && newPushTrain != null
&& !newPushTrain.getHeadMember()
.equals(previousPushTrain.getHeadMember())
&& !newPushTrain.getHeadMember().equals("clear")) {
newPushTrain.onNewHead(this, channel, setBy);
}
if(graphiteLogger != null) {
graphiteLogger.logToGraphite(channel+".queueSize",
newPushTrain.size());
graphiteLogger.logToGraphite(channel+".members",
newPushTrain.getMemberCount());
}
channelInfo.setTopic(topic);
}
protected String getTopic(String channel) throws RuntimeException {
if(channelInfoMap.get(channel) == null) {
return null;
}
ChannelInfo channelInfo = channelInfoMap.get(channel);
if(channelInfo.getHasBadTopic()) {
throw new RuntimeException("Bad Topic");
}
return channelInfo.getTopic();
}
@Override
protected synchronized void onMessage(String channel, String sender, String login, String hostname, String message) {
List<TrainCommand> trainCommands;
try {
trainCommands = CommandReader.parse(message);
if(trainCommands == null || trainCommands.size() == 0) {
return;
}
} catch(Throwable t) {
t.printStackTrace();
return;
}
String topic = null;
try {
topic = getTopic(channel);
}
catch(Exception exception) {
sendMessage(channel, "Sorry, I don't understand the current topic");
return;
}
PushTrain pushTrain;
try {
pushTrain = PushTrainReader.parse(topic);
if(pushTrain == null) {
sendMessage(channel, "Sorry, I don't understand the current topic");
return;
}
} catch(Throwable t) {
t.printStackTrace();
return;
}
for(TrainCommand trainCommand : trainCommands) {
trainCommand.onCommand(this, pushTrain, channel, sender);
}
log(pushTrain.toString());
if(!pushTrain.toString().equals(topic)) {
setTopic(channel, pushTrain.toString());
}
}
@Override
protected void onPrivateMessage(String sender, String login, String hostname, String message) {
onMessage(this.primaryChannel, sender, login, hostname, message);
}
@Override
protected void onInvite(String targetNick, String sourceNick, String sourceLogin, String sourceHost, String channel) {
joinChannel(channel);
}
public static void main(String[] args)
throws Exception
{
Options options = new Options();
Option option;
option = new Option("n", "name", true, "Name");
option.setRequired(true);
options.addOption(option);
option = new Option("c", "channels", true, "A comma delimited set of channels to join");
option.setRequired(true);
options.addOption(option);
option = new Option("h", "irc-host", true, "The IRCD host");
option.setRequired(true);
options.addOption(option);
option = new Option("p", "irc-port", true, "The IRCD port");
option.setRequired(true);
options.addOption(option);
option = new Option("a", "irc-pass", true, "IRCd Server password");
option.setRequired(false);
options.addOption(option);
option = new Option("s", "ssl", false, "Use SSL");
option.setRequired(false);
options.addOption(option);
option = new Option("g", "graphite-enabled", false, "Set to true to log stats to graphite");
options.addOption(option);
option = new Option("r", "graphite-host", true, "Graphite server hostname");
options.addOption(option);
option = new Option("t", "graphite-port", true, "Graphite server port");
options.addOption(option);
CommandLineParser parser = new PosixParser();
CommandLine commandLine = null;
try {
commandLine = parser.parse(options, args);
}
catch(ParseException exception) {
System.err.println(exception.getMessage());
System.err.println("Usage: " + PushBot.class + " ARGS");
System.err.println(" -n,--name IRC Nick of the Bot");
System.err.println(" -c,--channels Comma delimited list of channels to join");
System.err.println(" -h,--irc-host IRCD hostname");
System.err.println(" -p,--irc-port IRCD port");
System.err.println(" -a,--irc-passwod Optional IRCD server password");
System.err.println(" -s,--ssl Connect using SSL");
System.err.println(" -g,--graphite-enabled Enable graphite logging");
System.err.println(" -r,--graphite-host Graphite hostname");
System.err.println(" -t,--graphite-port Graphite port");
System.exit(1);
}
List<String> channels = new LinkedList<String>();
for(String channel : commandLine.getOptionValue('c').split("/\\s*,\\s*/")) {
channels.add(channel);
}
// Build PushBot
PushBot pushBot =
new PushBot(commandLine.getOptionValue('n'),
channels,
commandLine.hasOption('g'),
commandLine.getOptionValue('r', null),
Integer.valueOf(commandLine.getOptionValue('t', "2003")));
SSLSocketFactory socketFactory = null;
if (commandLine.hasOption('s')) {
socketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
}
// Connect to IRCD
pushBot.connect(
commandLine.getOptionValue('h'),
Integer.valueOf(commandLine.getOptionValue('p')),
commandLine.getOptionValue('a', null),
socketFactory
);
// Launch the web interface
ConfigServer configServer = new ConfigServer(pushBot);
}
}