1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.org.iscream.client.alerters; |
2 |
> |
package uk.org.iscream.cms.server.client.alerters; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.org.iscream.client.*; |
6 |
< |
import uk.org.iscream.core.*; |
7 |
< |
import uk.org.iscream.util.*; |
8 |
< |
import uk.org.iscream.componentmanager.*; |
9 |
< |
|
5 |
> |
import uk.org.iscream.cms.server.client.*; |
6 |
> |
import uk.org.iscream.cms.server.core.*; |
7 |
> |
import uk.org.iscream.cms.server.util.*; |
8 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
9 |
|
import java.io.*; |
10 |
|
import java.net.*; |
11 |
|
import java.util.*; |
12 |
< |
import java.text.*; |
12 |
> |
import java.text.DateFormat; |
13 |
> |
import org.jibble.pircbot.*; |
14 |
|
|
15 |
|
/** |
16 |
|
* This Alert sends an IRC message. |
21 |
|
* @author $Author$ |
22 |
|
* @version $Id$ |
23 |
|
*/ |
24 |
< |
public class IRC__Alerter implements PluginAlerter { |
24 |
> |
public class IRC__Alerter extends AlerterSkeleton { |
25 |
|
|
26 |
|
//---FINAL ATTRIBUTES--- |
27 |
|
|
35 |
|
*/ |
36 |
|
public final String DESC = "Sends alerts on an IRC channel"; |
37 |
|
|
38 |
– |
/** |
39 |
– |
* The default reconnect delay in seconds |
40 |
– |
*/ |
41 |
– |
public final int DEFAULT_RECONNECT_DELAY = 30; |
42 |
– |
|
43 |
– |
public final String DEFAULT_LEVEL = Alert.alertLevels[0]; |
44 |
– |
|
45 |
– |
public final String NOT_CONFIGURED = "<NOT CONFIGURED>"; |
46 |
– |
|
38 |
|
//---STATIC METHODS--- |
39 |
|
|
40 |
|
//---CONSTRUCTORS--- |
41 |
< |
|
41 |
> |
|
42 |
|
public IRC__Alerter() { |
43 |
< |
|
44 |
< |
// connect to the IRC server |
43 |
> |
super(); |
44 |
> |
// construct and initialise the bot |
45 |
|
_ircbot = new IRCBot(); |
46 |
+ |
//_ircbot.setVerbose(true); |
47 |
+ |
Thread ircThread = new Thread(_ircbot); |
48 |
|
// set it's name and start it |
49 |
< |
_ircbot.setName("client.IRC__Alerter$IRCBot"); |
50 |
< |
_ircbot.start(); |
49 |
> |
ircThread.setName("client.IRC__Alerter$IRCBot"); |
50 |
> |
ircThread.start(); |
51 |
> |
// log our start time |
52 |
|
_startTime = System.currentTimeMillis(); |
59 |
– |
|
53 |
|
_logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); |
54 |
|
} |
55 |
|
|
56 |
|
//---PUBLIC METHODS--- |
57 |
|
|
58 |
+ |
/** |
59 |
+ |
* Implements the abstract method from the skeleton class. |
60 |
+ |
* This method will attempt to send an alert |
61 |
+ |
* message over the IRC channel. |
62 |
+ |
* |
63 |
+ |
* @param alert the alert to send |
64 |
+ |
*/ |
65 |
|
public void sendAlert(Alert alert) { |
66 |
+ |
// sort out the message |
67 |
+ |
String alertType = Alert.alertLevels[alert.getLevel()]; |
68 |
+ |
String message; |
69 |
+ |
try { |
70 |
+ |
message = _cp.getProperty(_name, "Alerter.IRC.message"); |
71 |
+ |
} catch (PropertyNotFoundException e) { |
72 |
+ |
message = NOT_CONFIGURED; |
73 |
+ |
_logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message); |
74 |
+ |
} |
75 |
+ |
message = processAlertMessage(message, alert); |
76 |
|
// only send alerts if we're active |
77 |
< |
if(_active) { |
78 |
< |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
79 |
< |
|
80 |
< |
String levelName; |
81 |
< |
try { |
82 |
< |
levelName = cp.getProperty(_name, "Alerter.IRC.level"); |
83 |
< |
} catch (PropertyNotFoundException e) { |
84 |
< |
levelName = DEFAULT_LEVEL; |
85 |
< |
_logger.write(toString(), Logger.WARNING, "Alerter.IRC.level value unavailable using default of " + levelName); |
76 |
< |
} |
77 |
< |
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
78 |
< |
// only send if it's equal (or above) our level |
79 |
< |
if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) { |
80 |
< |
String alertType = Alert.alertLevels[alert.getLevel()]; |
81 |
< |
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
82 |
< |
String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
83 |
< |
String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime())); |
84 |
< |
// sort out the message |
85 |
< |
String message; |
86 |
< |
try { |
87 |
< |
message = cp.getProperty(_name, "Alerter.IRC.message"); |
88 |
< |
} catch (PropertyNotFoundException e) { |
89 |
< |
message = NOT_CONFIGURED; |
90 |
< |
_logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message); |
91 |
< |
} |
92 |
< |
|
93 |
< |
message = StringUtils.replaceText(message, "%level%", alertType); |
94 |
< |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
95 |
< |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
96 |
< |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
97 |
< |
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
98 |
< |
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
99 |
< |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
100 |
< |
message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince); |
101 |
< |
message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured); |
102 |
< |
|
103 |
< |
// send the message |
104 |
< |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); |
105 |
< |
_ircbot.sendMsg(message); |
106 |
< |
_lastAlert = message; |
107 |
< |
_lastAlertTime = System.currentTimeMillis(); |
108 |
< |
_alertCount ++; |
109 |
< |
} |
77 |
> |
if(_active) { |
78 |
> |
// send the message |
79 |
> |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); |
80 |
> |
_ircbot.sendMsg(message); |
81 |
> |
// count sent alerts |
82 |
> |
_alertCount++; |
83 |
> |
} else { |
84 |
> |
// don't send, but keep a count that we ignored it |
85 |
> |
_ignoredCount++; |
86 |
|
} |
87 |
< |
else { |
88 |
< |
_ignoredCount ++; |
89 |
< |
} |
87 |
> |
// we'll always store the "last alert", regardless |
88 |
> |
// of whether we actually display it or not |
89 |
> |
_lastAlert = message; |
90 |
> |
_lastAlertTime = System.currentTimeMillis(); |
91 |
|
} |
92 |
|
|
93 |
|
/** |
94 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
95 |
|
* method to provide clean logging (every class should have this). |
96 |
|
* |
97 |
< |
* This uses the uk.org.iscream.util.NameFormat class |
97 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
98 |
|
* to format the toString() |
99 |
|
* |
100 |
|
* @return the name of this class and its CVS revision |
106 |
|
REVISION); |
107 |
|
} |
108 |
|
|
109 |
< |
/** |
110 |
< |
* return the String representation of what the filter does |
109 |
> |
/** |
110 |
> |
* Return the String representation of what the alerter does |
111 |
> |
* |
112 |
> |
* @return the description |
113 |
|
*/ |
114 |
|
public String getDescription(){ |
115 |
|
return DESC; |
117 |
|
|
118 |
|
//---PRIVATE METHODS--- |
119 |
|
|
141 |
– |
private String getTimeString(long time) { |
142 |
– |
String timeString = null; |
143 |
– |
if (time >= 60) { |
144 |
– |
timeString = (time / 60) + " minute(s)"; |
145 |
– |
} else if (time >= 3600) { |
146 |
– |
timeString = ((time/60) / 60) + " hour(s)"; |
147 |
– |
} else { |
148 |
– |
timeString = time + " second(s)"; |
149 |
– |
} |
150 |
– |
return timeString; |
151 |
– |
} |
152 |
– |
|
120 |
|
//---ACCESSOR/MUTATOR METHODS--- |
121 |
|
|
122 |
+ |
/** |
123 |
+ |
* Returns the "friendly" name of this class. This |
124 |
+ |
* is simply an accessor for _name, required due to |
125 |
+ |
* inheritance issues with extending AlerterSkeleton. |
126 |
+ |
* |
127 |
+ |
* @return the friendly name |
128 |
+ |
*/ |
129 |
+ |
protected String getFName() { |
130 |
+ |
return _name; |
131 |
+ |
} |
132 |
+ |
|
133 |
|
//---ATTRIBUTES--- |
134 |
|
|
135 |
|
/** |
168 |
|
private long _startTime; |
169 |
|
|
170 |
|
/** |
171 |
+ |
* This holds a reference to the |
172 |
+ |
* system logger that is being used. |
173 |
+ |
*/ |
174 |
+ |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
175 |
+ |
|
176 |
+ |
/** |
177 |
|
* This is the friendly identifier of the |
178 |
|
* component this class is running in. |
179 |
|
* eg, a Filter may be called "filter1", |
182 |
|
* can be placed here. This name could also |
183 |
|
* be changed to null for utility classes. |
184 |
|
*/ |
185 |
< |
private String _name = "IRC Alert"; |
185 |
> |
private String _name = "IRC"; |
186 |
|
|
203 |
– |
/** |
204 |
– |
* This holds a reference to the |
205 |
– |
* system logger that is being used. |
206 |
– |
*/ |
207 |
– |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
208 |
– |
|
187 |
|
//---STATIC ATTRIBUTES--- |
188 |
|
|
189 |
|
//---INNER CLASSES--- |
190 |
|
|
191 |
< |
/** |
214 |
< |
* This class provides some basic IRCBot functionality. It connects |
215 |
< |
* to a specified server, and will remain there until told to |
216 |
< |
* leave. Whilst connected it can send a message or a notice to |
217 |
< |
* the server. |
218 |
< |
*/ |
219 |
< |
class IRCBot extends Thread { |
191 |
> |
class IRCBot extends PircBot implements Runnable { |
192 |
|
|
221 |
– |
public static final String DEFAULT_STARTUP_NOTICE = "i-scream ircbot starting..."; |
222 |
– |
|
193 |
|
/** |
194 |
< |
* Main thread loop, this part of the class listens for |
225 |
< |
* messages from the server, and acts accordingly. At the |
226 |
< |
* present moment it only responds to pings. |
194 |
> |
* The default reconnect delay in seconds |
195 |
|
*/ |
196 |
+ |
public final int DEFAULT_RECONNECT_DELAY = 30; |
197 |
+ |
|
198 |
|
public void run() { |
199 |
< |
// so we can stop if required |
230 |
< |
boolean run = true; |
231 |
< |
while(run) { |
232 |
< |
// flag so we can stop the loop |
233 |
< |
boolean doRead = true; |
234 |
< |
// get the startup notice |
235 |
< |
String startupNotice; |
199 |
> |
while(true) { |
200 |
|
try { |
201 |
< |
startupNotice = ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice"); |
202 |
< |
} catch (PropertyNotFoundException e) { |
239 |
< |
startupNotice = DEFAULT_STARTUP_NOTICE; |
240 |
< |
_logger.write(this.toString(), Logger.WARNING, "Configuration error: "+e); |
201 |
> |
init(); |
202 |
> |
break; |
203 |
|
} |
204 |
< |
// connect to the IRC server |
205 |
< |
try { |
206 |
< |
connect(); |
245 |
< |
sendNotice(startupNotice); |
246 |
< |
} catch(IOException e) { |
247 |
< |
doRead=false; |
248 |
< |
_logger.write(this.toString(), Logger.ERROR, "Error connecting: "+e); |
204 |
> |
catch (IOException e) { |
205 |
> |
_logger.write(this.toString(), Logger.ERROR, "Error initialising IRCBot: "+e); |
206 |
> |
reconnectSleep(); |
207 |
|
} |
250 |
– |
while(doRead) { |
251 |
– |
try { |
252 |
– |
// read a command |
253 |
– |
String cmd = _socketIn.readLine(); |
254 |
– |
// if we have a null, we've lost contact |
255 |
– |
if(cmd == null) { |
256 |
– |
throw new IOException("End of stream reached"); |
257 |
– |
} |
258 |
– |
// let another method deal with the input |
259 |
– |
handleInput(cmd); |
260 |
– |
} catch (IOException e) { |
261 |
– |
// comms failure, maybe our link is dead. |
262 |
– |
_logger.write(this.toString(), Logger.ERROR, "Communication error: "+e); |
263 |
– |
// stop, and loop round for a reconnect. |
264 |
– |
doRead = false; |
265 |
– |
} |
266 |
– |
} |
267 |
– |
// make sure we're disconnected |
268 |
– |
try { |
269 |
– |
disconnect(); |
270 |
– |
} catch (IOException e) { |
271 |
– |
_logger.write(this.toString(), Logger.ERROR, "Communication error: "+e); |
272 |
– |
} |
273 |
– |
|
274 |
– |
// comms have failed, so wait a while and reconnect |
275 |
– |
int delayTime = 0; |
276 |
– |
try { |
277 |
– |
delayTime = Integer.parseInt(ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.reconnectDelay")); |
278 |
– |
} catch (NumberFormatException e) { |
279 |
– |
delayTime = DEFAULT_RECONNECT_DELAY; |
280 |
– |
_logger.write(this.toString(), Logger.WARNING, "Erronous Alerter.IRC.reconnectDelay value in configuration using default of " + delayTime + " seconds"); |
281 |
– |
} catch (PropertyNotFoundException e) { |
282 |
– |
delayTime = DEFAULT_RECONNECT_DELAY; |
283 |
– |
_logger.write(this.toString(), Logger.WARNING, "Alerter.IRC.reconnectDelay value unavailable using default of " + delayTime + " seconds"); |
284 |
– |
} |
285 |
– |
try { |
286 |
– |
Thread.sleep(delayTime * 1000); |
287 |
– |
} catch (InterruptedException e) {} |
208 |
|
} |
209 |
< |
// maybe disconnect here ? - shutdown method not implemented yet |
290 |
< |
//disconnect(); |
209 |
> |
//System.out.println("falling out!"); |
210 |
|
} |
211 |
|
|
212 |
< |
/** |
213 |
< |
* Sends a message to the channel. |
295 |
< |
* |
296 |
< |
* @param msg The message to send |
297 |
< |
*/ |
298 |
< |
public void sendMsg(String msg) { |
299 |
< |
_socketOut.println("PRIVMSG "+_channel+" :"+msg); |
300 |
< |
// wait a second before returning... |
301 |
< |
// this ensures messages can't be sent too fast |
302 |
< |
try {Thread.sleep(1000);} catch (InterruptedException e) {} |
303 |
< |
} |
304 |
< |
|
305 |
< |
/** |
306 |
< |
* Sends a message to the channel. |
307 |
< |
* |
308 |
< |
* @param user The user to send to |
309 |
< |
* @param msg The message to send |
310 |
< |
*/ |
311 |
< |
public void sendPrivMsg(String user, String msg) { |
312 |
< |
_socketOut.println("PRIVMSG "+user+" :"+msg); |
313 |
< |
// wait a second before returning... |
314 |
< |
// this ensures messages can't be sent too fast |
315 |
< |
try {Thread.sleep(1000);} catch (InterruptedException e) {} |
316 |
< |
} |
317 |
< |
|
318 |
< |
/** |
319 |
< |
* Sends an action to the channel. |
320 |
< |
* |
321 |
< |
* @param msg the action message |
322 |
< |
*/ |
323 |
< |
public void sendAction(String msg) { |
324 |
< |
char esc = 001; |
325 |
< |
sendMsg(esc+"ACTION "+msg+esc); |
326 |
< |
// wait a second before returning... |
327 |
< |
// this ensures messages can't be sent too fast |
328 |
< |
try {Thread.sleep(1000);} catch (InterruptedException e) {} |
329 |
< |
} |
330 |
< |
|
331 |
< |
/** |
332 |
< |
* Sends a notice to the channel. |
333 |
< |
* |
334 |
< |
* @param msg The notice to send |
335 |
< |
*/ |
336 |
< |
public void sendNotice(String msg) { |
337 |
< |
_socketOut.println("NOTICE "+_channel+" :"+msg); |
338 |
< |
// wait a second before returning... |
339 |
< |
// this ensures messages can't be sent too fast |
340 |
< |
try {Thread.sleep(1000);} catch (InterruptedException e) {} |
341 |
< |
} |
342 |
< |
|
343 |
< |
/** |
344 |
< |
* Connect to the IRC server, log in, and join the channel. |
345 |
< |
* |
346 |
< |
* @throws IOException if the connection fails |
347 |
< |
*/ |
348 |
< |
public void connect() throws IOException { |
212 |
> |
public void init() throws IOException { |
213 |
> |
_logger.write(this.toString(), Logger.DEBUG, "Initialising IRCBot..."); |
214 |
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
350 |
– |
// setup the socket, reader and writer |
215 |
|
String server; |
216 |
|
int port; |
217 |
|
try { |
224 |
|
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
225 |
|
throw new IOException("Can't get irc server details due to malformed configuration"); |
226 |
|
} |
363 |
– |
_socket = new Socket(server, port); |
364 |
– |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
365 |
– |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
366 |
– |
//_socketOut.println("PASS"); |
367 |
– |
// send USER details |
227 |
|
String user, comment; |
228 |
|
try { |
229 |
|
user = cp.getProperty(_name, "Alerter.IRC.user"); |
232 |
|
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
233 |
|
throw new IOException("Can't get user details due to configuration error"); |
234 |
|
} |
235 |
< |
_socketOut.println("USER "+user+" 8 * :"+comment); |
236 |
< |
// attempt to get a nick |
235 |
> |
this.setLogin(user); |
236 |
> |
this.setVersion(comment); |
237 |
> |
this.setFinger(comment); // ? |
238 |
|
String nickList; |
239 |
|
try { |
240 |
|
nickList = cp.getProperty(_name, "Alerter.IRC.nickList"); |
244 |
|
} |
245 |
|
StringTokenizer st = new StringTokenizer(nickList, ";"); |
246 |
|
boolean ok = false; |
387 |
– |
// try until we exhaust our list |
247 |
|
while(!ok && st.hasMoreTokens()) { |
248 |
|
String nick = st.nextToken(); |
249 |
< |
_socketOut.println("NICK "+nick); |
250 |
< |
// get a "yes" or "no" response back |
251 |
< |
String response = ""; |
252 |
< |
do { |
253 |
< |
response = _socketIn.readLine(); |
395 |
< |
if(response==null) { |
396 |
< |
throw new IOException("Communication error whilst logging in"); |
397 |
< |
} |
398 |
< |
} while(response.indexOf("001")==-1 && response.indexOf("433")==-1); |
399 |
< |
// see if it was a yes |
400 |
< |
if(response.indexOf("001")!=-1) { |
401 |
< |
// great, we're logged in ! |
402 |
< |
ok = true; |
403 |
< |
// store the name we're using |
249 |
> |
try { |
250 |
> |
_logger.write(this.toString(), Logger.DEBUG, "Trying nick: "+nick); |
251 |
> |
this.setName(nick); |
252 |
> |
this.connect(server, port); |
253 |
> |
// must be ok if we get here |
254 |
|
_nickname = nick; |
255 |
+ |
ok = true; |
256 |
|
} |
257 |
< |
else { |
258 |
< |
// log we couldn't get the name |
259 |
< |
_logger.write(this.toString(), Logger.WARNING, "Nickname in use: "+nick); |
257 |
> |
catch(IOException e) { |
258 |
> |
_logger.write(this.toString(), Logger.ERROR, "IO error when connecting to server: "+e); |
259 |
> |
throw new IOException("IO error when connecting to server"); |
260 |
|
} |
261 |
+ |
catch(IrcException e) { |
262 |
+ |
_logger.write(this.toString(), Logger.ERROR, "IRC error when connecting to server: "+e); |
263 |
+ |
throw new IOException("IRC error when connecting to server"); |
264 |
+ |
} |
265 |
+ |
catch(NickAlreadyInUseException e) { |
266 |
+ |
_logger.write(this.toString(), Logger.ERROR, "Nickname "+nick+" is already in use: "+e); |
267 |
+ |
// just carry on around while loop |
268 |
+ |
} |
269 |
|
} |
270 |
|
if(!ok) { |
271 |
|
// oh dear, we couldn't get on. |
272 |
< |
throw new IOException("All nicknames in use"); |
272 |
> |
_logger.write(this.toString(), Logger.ERROR, "All nicknames already in use"); |
273 |
> |
throw new IOException("All nicknames already in use"); |
274 |
|
} |
415 |
– |
// join the channel |
275 |
|
try { |
276 |
|
_channel = cp.getProperty(_name, "Alerter.IRC.channel"); |
277 |
+ |
this.joinChannel(_channel); |
278 |
|
} catch (PropertyNotFoundException e) { |
279 |
|
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
280 |
|
throw new IOException("Can't get channel name due to configuration error"); |
281 |
|
} |
282 |
< |
_socketOut.println("JOIN "+_channel); |
283 |
< |
// allow alerts |
282 |
> |
|
283 |
> |
String startupNotice; |
284 |
> |
try { |
285 |
> |
startupNotice = ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice"); |
286 |
> |
sendNotice(_channel, startupNotice); |
287 |
> |
} catch (PropertyNotFoundException e) { |
288 |
> |
_logger.write(this.toString(), Logger.DEBUG, "Startup notice not defined, so not sending: "+e); |
289 |
> |
} |
290 |
> |
|
291 |
> |
// we should set this when all is ready! |
292 |
|
_active = true; |
293 |
+ |
//System.out.println("leaving init"); |
294 |
|
} |
295 |
|
|
296 |
< |
/** |
297 |
< |
* Disconnect "nicely" from the IRC server. |
298 |
< |
* |
299 |
< |
* @throws IOException if the disconnection fails |
300 |
< |
*/ |
432 |
< |
public void disconnect() throws IOException { |
433 |
< |
// stop alerts |
296 |
> |
public void sendMsg(String msg) { |
297 |
> |
sendMessage(_channel, msg); |
298 |
> |
} |
299 |
> |
|
300 |
> |
public void onDisconnect() { |
301 |
|
_active = false; |
302 |
< |
// send proper QUIT |
303 |
< |
_socketOut.println("QUIT : iscreamBot component shutting down..."); |
304 |
< |
// close the socket |
305 |
< |
_socketOut.close(); |
306 |
< |
_socketIn.close(); |
307 |
< |
_socket.close(); |
302 |
> |
while(true) { |
303 |
> |
reconnectSleep(); |
304 |
> |
try { |
305 |
> |
init(); |
306 |
> |
break; |
307 |
> |
} |
308 |
> |
catch (IOException e) { |
309 |
> |
_logger.write(this.toString(), Logger.ERROR, "Error initialising IRCBot: "+e); |
310 |
> |
} |
311 |
> |
} |
312 |
> |
//System.out.println("falling out of disconnect!"); |
313 |
|
} |
314 |
|
|
315 |
< |
/** |
316 |
< |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
317 |
< |
* method to provide clean logging (every class should have this). |
318 |
< |
* |
447 |
< |
* This uses the uk.org.iscream.util.NameFormat class |
448 |
< |
* to format the toString() |
449 |
< |
* |
450 |
< |
* @return the name of this class and its CVS revision |
451 |
< |
*/ |
452 |
< |
public String toString() { |
453 |
< |
return FormatName.getName( |
454 |
< |
_name, |
455 |
< |
getClass().getName(), |
456 |
< |
REVISION); |
315 |
> |
public void onMessage(String channel, String sender, String login, String hostname, String message) { |
316 |
> |
if(isForMe(message)) { |
317 |
> |
handleInput(message, channel); |
318 |
> |
} |
319 |
|
} |
320 |
|
|
321 |
< |
/** |
322 |
< |
* Deals with incoming lines from the server. |
323 |
< |
* |
324 |
< |
* @param line the line to deal with |
325 |
< |
*/ |
326 |
< |
private void handleInput(String line) { |
327 |
< |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
466 |
< |
// if it's a PING... |
467 |
< |
if(line.startsWith("PING")) { |
468 |
< |
// ...send a PONG |
469 |
< |
_socketOut.println("PONG" + line.substring(4)); |
321 |
> |
public void onPrivateMessage(String sender, String login, String hostname, String message) { |
322 |
> |
handleInput(message, sender); |
323 |
> |
} |
324 |
> |
|
325 |
> |
public void onNickChange(String oldNick, String login, String hostname, String newNick) { |
326 |
> |
if(oldNick.equals(_nickname)) { |
327 |
> |
_nickname = newNick; |
328 |
|
} |
329 |
< |
// see if it's for us |
330 |
< |
else if(getMsg(line).startsWith(_nickname+",") || getMsg(line).startsWith(_nickname+":") || getMsg(line).startsWith(_nickname+" ")) { |
331 |
< |
// setup some String's |
332 |
< |
String stopCommand, startCommand, timeSinceLastAlertCommand, lastAlertCommand, joinCommand; |
333 |
< |
String nickChangeCommand, versionCommand, helpCommand, statCommand, uptimeCommand; |
476 |
< |
// get the command set |
329 |
> |
} |
330 |
> |
|
331 |
> |
public void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason) { |
332 |
> |
if(recipientNick.equals(_nickname) && channel.equals(_channel)) { |
333 |
> |
sendMessage(kickerNick, "That wasn't a nice thing to do..."); |
334 |
|
try { |
335 |
< |
stopCommand = cp.getProperty(_name, "Alerter.IRC.stopCommand"); |
336 |
< |
startCommand = cp.getProperty(_name, "Alerter.IRC.startCommand"); |
480 |
< |
timeSinceLastAlertCommand = cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand"); |
481 |
< |
lastAlertCommand = cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"); |
482 |
< |
joinCommand = cp.getProperty(_name, "Alerter.IRC.joinCommand"); |
483 |
< |
nickChangeCommand = cp.getProperty(_name, "Alerter.IRC.nickChangeCommand"); |
484 |
< |
versionCommand = cp.getProperty(_name, "Alerter.IRC.versionCommand"); |
485 |
< |
helpCommand = cp.getProperty(_name, "Alerter.IRC.helpCommand"); |
486 |
< |
statCommand = cp.getProperty(_name, "Alerter.IRC.statCommand"); |
487 |
< |
uptimeCommand = cp.getProperty(_name, "Alerter.IRC.uptimeCommand"); |
488 |
< |
} catch (PropertyNotFoundException e) { |
335 |
> |
_channel = ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.channel"); |
336 |
> |
} catch(PropertyNotFoundException e) { |
337 |
|
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
490 |
– |
// lets bail from handling this line... |
491 |
– |
// ...it's gonna be hard without a command set! |
492 |
– |
return; |
338 |
|
} |
339 |
< |
|
340 |
< |
// we have a message for us |
341 |
< |
String message = getMsg(line).substring(_nickname.length()); |
342 |
< |
if(message.indexOf(stopCommand)!=-1) { |
343 |
< |
_active = false; |
344 |
< |
sendMsg(getMsgSender(line)+", alerts have been stopped"); |
339 |
> |
// should this be in the try? |
340 |
> |
joinChannel(_channel); |
341 |
> |
} |
342 |
> |
} |
343 |
> |
|
344 |
> |
private void handleInput(String message, String source) { |
345 |
> |
System.out.println("message: "+message); |
346 |
> |
System.out.println("from: "+source); |
347 |
> |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
348 |
> |
// setup some String's |
349 |
> |
String stopCommand, startCommand, timeSinceLastAlertCommand, lastAlertCommand, joinCommand; |
350 |
> |
String nickChangeCommand, versionCommand, helpCommand, statCommand, uptimeCommand; |
351 |
> |
// get the command set |
352 |
> |
try { |
353 |
> |
stopCommand = cp.getProperty(_name, "Alerter.IRC.stopCommand"); |
354 |
> |
startCommand = cp.getProperty(_name, "Alerter.IRC.startCommand"); |
355 |
> |
timeSinceLastAlertCommand = cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand"); |
356 |
> |
lastAlertCommand = cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"); |
357 |
> |
joinCommand = cp.getProperty(_name, "Alerter.IRC.joinCommand"); |
358 |
> |
nickChangeCommand = cp.getProperty(_name, "Alerter.IRC.nickChangeCommand"); |
359 |
> |
versionCommand = cp.getProperty(_name, "Alerter.IRC.versionCommand"); |
360 |
> |
helpCommand = cp.getProperty(_name, "Alerter.IRC.helpCommand"); |
361 |
> |
statCommand = cp.getProperty(_name, "Alerter.IRC.statCommand"); |
362 |
> |
uptimeCommand = cp.getProperty(_name, "Alerter.IRC.uptimeCommand"); |
363 |
> |
} catch (PropertyNotFoundException e) { |
364 |
> |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
365 |
> |
// lets bail from handling this line... |
366 |
> |
// ...it's gonna be hard without a command set! |
367 |
> |
return; |
368 |
> |
} |
369 |
> |
|
370 |
> |
if(message.indexOf(stopCommand) != -1) { |
371 |
> |
_active = false; |
372 |
> |
sendMessage(source, "alerts have been stopped"); |
373 |
> |
} |
374 |
> |
else if(message.indexOf(startCommand) != -1) { |
375 |
> |
_active = true; |
376 |
> |
sendMessage(source, "alerts have been activated"); |
377 |
> |
} |
378 |
> |
// this needs to go here if it contains the same words as the lastAlertCommand |
379 |
> |
else if(message.indexOf(timeSinceLastAlertCommand) != -1) { |
380 |
> |
if(_lastAlertTime != -1) { |
381 |
> |
long uptime = (System.currentTimeMillis() - _lastAlertTime) / 1000; |
382 |
> |
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
383 |
> |
sendMessage(source, "I last sent an alert "+uptimeText+ " ago"); |
384 |
|
} |
385 |
< |
else if(message.indexOf(startCommand)!=-1) { |
386 |
< |
_active = true; |
503 |
< |
sendMsg(getMsgSender(line)+", alerts have been activated"); |
385 |
> |
else { |
386 |
> |
sendMessage(source, "I've never sent an alert!"); |
387 |
|
} |
388 |
< |
// this needs to go here if it contains the same words as the lastAlertCommand |
389 |
< |
else if(message.indexOf(timeSinceLastAlertCommand)!=-1) { |
390 |
< |
if(_lastAlertTime != -1) { |
391 |
< |
long uptime = (System.currentTimeMillis() - _lastAlertTime) / 1000; |
392 |
< |
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
510 |
< |
sendMsg(getMsgSender(line)+", I last sent an alert "+uptimeText+ " ago"); |
511 |
< |
} |
512 |
< |
else { |
513 |
< |
sendMsg(getMsgSender(line)+", I've never sent an alert!"); |
514 |
< |
} |
388 |
> |
} |
389 |
> |
else if(message.indexOf(lastAlertCommand) != -1) { |
390 |
> |
if(_lastAlertTime != -1) { |
391 |
> |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(_lastAlertTime)); |
392 |
> |
sendMessage(source, "last alert was at "+date+"; "+_lastAlert); |
393 |
|
} |
394 |
< |
else if(message.indexOf(lastAlertCommand)!=-1) { |
395 |
< |
if(_lastAlertTime != -1) { |
518 |
< |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(_lastAlertTime)); |
519 |
< |
sendMsg(getMsgSender(line)+", last alert was at "+date+"; "+_lastAlert); |
520 |
< |
} |
521 |
< |
else { |
522 |
< |
sendMsg(getMsgSender(line)+", I've never sent an alert!"); |
523 |
< |
} |
524 |
< |
|
394 |
> |
else { |
395 |
> |
sendMessage(source, "I've never sent an alert!"); |
396 |
|
} |
397 |
< |
else if(message.indexOf(joinCommand)!=-1) { |
398 |
< |
String joinCmd = joinCommand; |
399 |
< |
String newChan = message.substring(message.indexOf(joinCmd) + joinCmd.length() + 1); |
400 |
< |
int endOfChan = newChan.indexOf(" "); |
401 |
< |
if(endOfChan == -1) { |
402 |
< |
endOfChan = newChan.length(); |
403 |
< |
} |
404 |
< |
newChan = newChan.substring(0, endOfChan); |
534 |
< |
if(newChan.equals(_channel)) { |
535 |
< |
sendMsg(getMsgSender(line)+", I'm already on "+newChan+"!"); |
536 |
< |
} else { |
537 |
< |
sendMsg(getMsgSender(line)+", okay, I'm off to "+newChan); |
538 |
< |
_socketOut.println("PART "+_channel); |
539 |
< |
_socketOut.println("JOIN "+newChan); |
540 |
< |
_channel = newChan; |
541 |
< |
} |
397 |
> |
|
398 |
> |
} |
399 |
> |
else if(message.indexOf(joinCommand) != -1) { |
400 |
> |
String joinCmd = joinCommand; |
401 |
> |
String newChan = message.substring(message.indexOf(joinCmd) + joinCmd.length() + 1); |
402 |
> |
int endOfChan = newChan.indexOf(" "); |
403 |
> |
if(endOfChan == -1) { |
404 |
> |
endOfChan = newChan.length(); |
405 |
|
} |
406 |
< |
else if(message.indexOf(nickChangeCommand)!=-1) { |
407 |
< |
String nickChangeCmd = nickChangeCommand; |
408 |
< |
String newNick = message.substring(message.indexOf(nickChangeCmd) + nickChangeCmd.length() + 1); |
409 |
< |
int endOfNick = newNick.indexOf(" "); |
410 |
< |
if(endOfNick == -1) { |
411 |
< |
endOfNick = newNick.length(); |
412 |
< |
} |
413 |
< |
newNick = newNick.substring(0, endOfNick); |
551 |
< |
sendMsg(getMsgSender(line)+", okay, changing my nickname to "+newNick); |
552 |
< |
_socketOut.println("NICK "+newNick); |
553 |
< |
_nickname = newNick; |
406 |
> |
newChan = newChan.substring(0, endOfChan); |
407 |
> |
if(newChan.equals(_channel)) { |
408 |
> |
sendMessage(source, "I'm already on "+newChan+"!"); |
409 |
> |
} else { |
410 |
> |
partChannel(_channel); |
411 |
> |
joinChannel(newChan); |
412 |
> |
_channel = newChan; |
413 |
> |
//return null; // ??? |
414 |
|
} |
415 |
< |
else if(message.indexOf(versionCommand)!=-1) { |
416 |
< |
sendMsg(getMsgSender(line)+", I am version "+REVISION.substring(11, REVISION.length() -2)+" of the i-scream alerting bot"); |
415 |
> |
} |
416 |
> |
else if(message.indexOf(nickChangeCommand) != -1) { |
417 |
> |
String nickChangeCmd = nickChangeCommand; |
418 |
> |
String newNick = message.substring(message.indexOf(nickChangeCmd) + nickChangeCmd.length() + 1); |
419 |
> |
int endOfNick = newNick.indexOf(" "); |
420 |
> |
if(endOfNick == -1) { |
421 |
> |
endOfNick = newNick.length(); |
422 |
|
} |
423 |
< |
else if(message.indexOf(helpCommand)!=-1) { |
424 |
< |
sendPrivMsg(getMsgSender(line), "Hello, I am the i-scream alerting bot version "+REVISION.substring(11, REVISION.length() -2)); |
425 |
< |
sendPrivMsg(getMsgSender(line), "I understand the following commands;"); |
426 |
< |
sendPrivMsg(getMsgSender(line), stopCommand); |
427 |
< |
sendPrivMsg(getMsgSender(line), startCommand); |
563 |
< |
sendPrivMsg(getMsgSender(line), lastAlertCommand); |
564 |
< |
sendPrivMsg(getMsgSender(line), joinCommand); |
565 |
< |
sendPrivMsg(getMsgSender(line), nickChangeCommand); |
566 |
< |
sendPrivMsg(getMsgSender(line), statCommand); |
567 |
< |
sendPrivMsg(getMsgSender(line), uptimeCommand); |
568 |
< |
sendPrivMsg(getMsgSender(line), timeSinceLastAlertCommand); |
569 |
< |
sendPrivMsg(getMsgSender(line), helpCommand); |
570 |
< |
} |
571 |
< |
else if(message.indexOf(statCommand)!=-1) { |
572 |
< |
sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!"); |
573 |
< |
} |
574 |
< |
else if(message.indexOf(uptimeCommand)!=-1) { |
575 |
< |
long uptime = (System.currentTimeMillis() - _startTime) / 1000; |
576 |
< |
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
577 |
< |
sendMsg(getMsgSender(line)+", I have been running for "+uptimeText); |
578 |
< |
} |
579 |
< |
else if(message.indexOf("ping")!=-1) { |
580 |
< |
sendMsg("pong"); |
581 |
< |
} |
582 |
< |
else if(message.indexOf("do a jibble dance")!=-1) { |
583 |
< |
// little joke :) |
584 |
< |
sendAction("jives to the funky beat shouting \"ii--screeeaaammm\""); |
585 |
< |
} |
586 |
< |
else { |
587 |
< |
String rejectMessage = NOT_CONFIGURED; |
588 |
< |
try { |
589 |
< |
rejectMessage = cp.getProperty(_name, "Alerter.IRC.rejectMessage"); |
590 |
< |
} catch(PropertyNotFoundException e) { |
591 |
< |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
592 |
< |
} |
593 |
< |
sendMsg(getMsgSender(line)+", "+rejectMessage); |
594 |
< |
} |
423 |
> |
newNick = newNick.substring(0, endOfNick); |
424 |
> |
changeNick(newNick); |
425 |
> |
// should we check this worked? |
426 |
> |
//_nickname = newNick; |
427 |
> |
//return null; // ??? |
428 |
|
} |
429 |
< |
else if(line.indexOf(_nickname)!=-1 && line.indexOf(_channel)!=-1 && line.indexOf("KICK")!=-1) { |
430 |
< |
sendPrivMsg(getMsgSender(line), "That wasn't a nice thing to do..."); |
429 |
> |
else if(message.indexOf(versionCommand) != -1) { |
430 |
> |
sendMessage(source, "I am version "+REVISION.substring(11, REVISION.length() -2)+" of the i-scream alerting bot"); |
431 |
> |
} |
432 |
> |
else if(message.indexOf(helpCommand) != -1) { |
433 |
> |
//System.out.println("--starting help"); |
434 |
> |
sendMessage(source, "Hello, I am the i-scream alerting bot version "+REVISION.substring(11, REVISION.length() -2)); |
435 |
> |
sendMessage(source, "I understand the following commands;"); |
436 |
> |
sendMessage(source, stopCommand); |
437 |
> |
sendMessage(source, startCommand); |
438 |
> |
sendMessage(source, lastAlertCommand); |
439 |
> |
sendMessage(source, joinCommand); |
440 |
> |
sendMessage(source, nickChangeCommand); |
441 |
> |
sendMessage(source, statCommand); |
442 |
> |
sendMessage(source, uptimeCommand); |
443 |
> |
sendMessage(source, timeSinceLastAlertCommand); |
444 |
> |
sendMessage(source, helpCommand); |
445 |
> |
//System.out.println("--ending help"); |
446 |
> |
} |
447 |
> |
else if(message.indexOf(statCommand) != -1) { |
448 |
> |
sendMessage(source, "I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!"); |
449 |
> |
} |
450 |
> |
else if(message.indexOf(uptimeCommand) != -1) { |
451 |
> |
long uptime = (System.currentTimeMillis() - _startTime) / 1000; |
452 |
> |
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
453 |
> |
sendMessage(source, "I have been running for "+uptimeText); |
454 |
> |
} |
455 |
> |
else if(message.indexOf("ping") != -1) { |
456 |
> |
sendMessage(source, "pong"); |
457 |
> |
} |
458 |
> |
else if(message.indexOf("do a jibble dance") != -1) { |
459 |
> |
// little joke :) |
460 |
> |
sendAction(source, "jives to the funky beat shouting \"ii--screeeaaammm\""); |
461 |
> |
} |
462 |
> |
|
463 |
> |
else { |
464 |
> |
String rejectMessage = NOT_CONFIGURED; |
465 |
|
try { |
466 |
< |
_channel = cp.getProperty(_name, "Alerter.IRC.channel"); |
466 |
> |
rejectMessage = cp.getProperty(_name, "Alerter.IRC.rejectMessage"); |
467 |
|
} catch(PropertyNotFoundException e) { |
468 |
|
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
469 |
|
} |
470 |
< |
_socketOut.println("JOIN "+_channel); |
470 |
> |
sendMessage(source, rejectMessage); |
471 |
|
} |
472 |
|
} |
473 |
|
|
474 |
< |
/** |
475 |
< |
* Strips the header from a message line |
476 |
< |
* |
477 |
< |
* @param line the line to strip |
478 |
< |
* @return the message from the line |
479 |
< |
*/ |
480 |
< |
private String getMsg(String line) { |
481 |
< |
String result = ""; |
615 |
< |
if(line.indexOf("PRIVMSG")!=-1) { |
616 |
< |
int firstColon = line.indexOf(":"); |
617 |
< |
if(firstColon != -1) { |
618 |
< |
int secondColon = line.indexOf(":", firstColon+1); |
619 |
< |
if(secondColon != -1) { |
620 |
< |
result = line.substring(secondColon+1); |
621 |
< |
} |
622 |
< |
} |
474 |
> |
private boolean isForMe(String message) { |
475 |
> |
// change this! |
476 |
> |
String nick = _nickname.toLowerCase(); |
477 |
> |
String msg = message.toLowerCase(); |
478 |
> |
if(msg.startsWith(nick + ", ") || |
479 |
> |
msg.startsWith(nick + ": ") || |
480 |
> |
msg.startsWith(nick + " ")) { |
481 |
> |
return true; |
482 |
|
} |
483 |
< |
return result; |
483 |
> |
else { |
484 |
> |
return false; |
485 |
> |
} |
486 |
|
} |
487 |
|
|
488 |
< |
/** |
489 |
< |
* Finds out the sender of the message |
490 |
< |
* |
491 |
< |
* @param line the line to look for a sender in |
492 |
< |
* @return the sender |
493 |
< |
*/ |
494 |
< |
private String getMsgSender(String line) { |
495 |
< |
String result = ""; |
496 |
< |
int colon = line.indexOf(":"); |
497 |
< |
int excl = line.indexOf("!"); |
637 |
< |
if(colon!=-1 && excl!=-1) { |
638 |
< |
result = line.substring(colon+1, excl); |
488 |
> |
private void reconnectSleep() { |
489 |
> |
int delayTime = 0; |
490 |
> |
try { |
491 |
> |
delayTime = Integer.parseInt(ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.reconnectDelay")); |
492 |
> |
} catch (NumberFormatException e) { |
493 |
> |
delayTime = DEFAULT_RECONNECT_DELAY; |
494 |
> |
_logger.write(this.toString(), Logger.WARNING, "Erronous Alerter.IRC.reconnectDelay value in configuration using default of " + delayTime + " seconds"); |
495 |
> |
} catch (PropertyNotFoundException e) { |
496 |
> |
delayTime = DEFAULT_RECONNECT_DELAY; |
497 |
> |
_logger.write(this.toString(), Logger.WARNING, "Alerter.IRC.reconnectDelay value unavailable using default of " + delayTime + " seconds"); |
498 |
|
} |
499 |
< |
return result; |
499 |
> |
_logger.write(this.toString(), Logger.ERROR, "Waiting "+delayTime+" seconds for reconnect..."); |
500 |
> |
try { |
501 |
> |
Thread.sleep(delayTime * 1000); |
502 |
> |
} catch (InterruptedException e) {} |
503 |
|
} |
504 |
|
|
505 |
|
/** |
506 |
< |
* The socket connected to the server |
506 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
507 |
> |
* method to provide clean logging (every class should have this). |
508 |
> |
* |
509 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
510 |
> |
* to format the toString() |
511 |
> |
* |
512 |
> |
* @return the name of this class and its CVS revision |
513 |
|
*/ |
514 |
< |
private Socket _socket; |
515 |
< |
|
516 |
< |
/** |
517 |
< |
* The writer |
518 |
< |
*/ |
519 |
< |
private PrintWriter _socketOut; |
652 |
< |
|
653 |
< |
/** |
654 |
< |
* The reader |
655 |
< |
*/ |
656 |
< |
private BufferedReader _socketIn; |
514 |
> |
public String toString() { |
515 |
> |
return FormatName.getName( |
516 |
> |
_name, |
517 |
> |
getClass().getName(), |
518 |
> |
REVISION); |
519 |
> |
} |
520 |
|
|
521 |
|
/** |
522 |
|
* Just a reminder to what channel we're on... |