1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.client.alerters; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.ac.ukc.iscream.client.*; |
6 |
import uk.ac.ukc.iscream.core.*; |
7 |
import uk.ac.ukc.iscream.util.*; |
8 |
import uk.ac.ukc.iscream.componentmanager.*; |
9 |
|
10 |
import java.io.*; |
11 |
import java.net.*; |
12 |
|
13 |
/** |
14 |
* This Alert sends an IRC message. |
15 |
* |
16 |
* @author $Author: tdb1 $ |
17 |
* @version $Id: IRC__Alerter.java,v 1.7 2001/03/03 01:09:53 tdb1 Exp $ |
18 |
*/ |
19 |
public class IRC__Alerter implements PluginAlerter { |
20 |
|
21 |
//---FINAL ATTRIBUTES--- |
22 |
|
23 |
/** |
24 |
* The current CVS revision of this class |
25 |
*/ |
26 |
public final String REVISION = "$Revision: 1.7 $"; |
27 |
|
28 |
public final String DESC = "Sends alerts on an IRC channel"; |
29 |
|
30 |
//---STATIC METHODS--- |
31 |
|
32 |
//---CONSTRUCTORS--- |
33 |
|
34 |
public IRC__Alerter() { |
35 |
|
36 |
// connect to the IRC server |
37 |
_ircbot = null; |
38 |
try { |
39 |
_ircbot = new IRCBot(); |
40 |
_ircbot.connect(); |
41 |
_ircbot.sendNotice("i-scream alerting bot activated"); |
42 |
} catch(IOException e) { |
43 |
_logger.write(toString(), Logger.ERROR, "Error starting IRCBot: "+e); |
44 |
} |
45 |
|
46 |
_logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); |
47 |
} |
48 |
|
49 |
//---PUBLIC METHODS--- |
50 |
|
51 |
public void sendAlert(Alert alert) { |
52 |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
53 |
String levelName = cp.getProperty(_name, "Alerter.IRC.level"); |
54 |
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
55 |
// only send if it's equal (or above) our level |
56 |
if(alert.getLevel() >= level) { |
57 |
String alertType = Alert.alertLevels[alert.getLevel()]; |
58 |
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
59 |
// sort out the message |
60 |
String message = cp.getProperty(_name, "Alerter.IRC.message"); |
61 |
message = StringUtils.replaceText(message, "%level%", alertType); |
62 |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
63 |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
64 |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
65 |
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
66 |
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
67 |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
68 |
|
69 |
// send the message |
70 |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ levelName + " level"); |
71 |
_ircbot.sendMsg(message); |
72 |
} |
73 |
} |
74 |
|
75 |
/** |
76 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
77 |
* method to provide clean logging (every class should have this). |
78 |
* |
79 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
80 |
* to format the toString() |
81 |
* |
82 |
* @return the name of this class and its CVS revision |
83 |
*/ |
84 |
public String toString() { |
85 |
return FormatName.getName( |
86 |
_name, |
87 |
getClass().getName(), |
88 |
REVISION); |
89 |
} |
90 |
|
91 |
/** |
92 |
* return the String representation of what the filter does |
93 |
*/ |
94 |
public String getDescription(){ |
95 |
return DESC; |
96 |
} |
97 |
|
98 |
//---PRIVATE METHODS--- |
99 |
|
100 |
private String getTimeString(long time) { |
101 |
String timeString = null; |
102 |
if (time >= 60) { |
103 |
timeString = (time / 60) + " minute(s)"; |
104 |
} else if (time >= 3600) { |
105 |
timeString = ((time/60) / 60) + " hour(s)"; |
106 |
} else { |
107 |
timeString = time + " second(s)"; |
108 |
} |
109 |
return timeString; |
110 |
} |
111 |
|
112 |
//---ACCESSOR/MUTATOR METHODS--- |
113 |
|
114 |
//---ATTRIBUTES--- |
115 |
|
116 |
/** |
117 |
* A reference to the IRCBot |
118 |
*/ |
119 |
private IRCBot _ircbot; |
120 |
|
121 |
/** |
122 |
* This is the friendly identifier of the |
123 |
* component this class is running in. |
124 |
* eg, a Filter may be called "filter1", |
125 |
* If this class does not have an owning |
126 |
* component, a name from the configuration |
127 |
* can be placed here. This name could also |
128 |
* be changed to null for utility classes. |
129 |
*/ |
130 |
private String _name = "IRC Alert"; |
131 |
|
132 |
/** |
133 |
* This holds a reference to the |
134 |
* system logger that is being used. |
135 |
*/ |
136 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
137 |
|
138 |
//---STATIC ATTRIBUTES--- |
139 |
|
140 |
//---INNER CLASSES--- |
141 |
|
142 |
/** |
143 |
* This class provides some basic IRCBot functionality. It connects |
144 |
* to a specified server, and will remain there until told to |
145 |
* leave. Whilst connected it can send a message or a notice to |
146 |
* the server. |
147 |
*/ |
148 |
class IRCBot extends Thread { |
149 |
|
150 |
/** |
151 |
* Main thread loop, this part of the class listens for |
152 |
* messages from the server, and acts accordingly. At the |
153 |
* present moment it only responds to pings. |
154 |
*/ |
155 |
public void run() { |
156 |
// flag so we can stop the loop |
157 |
boolean run = true; |
158 |
while(run) { |
159 |
try { |
160 |
// read a command |
161 |
String cmd = _socketIn.readLine(); |
162 |
// if it's a PING... |
163 |
if(cmd.startsWith("PING")) { |
164 |
// ...send a PONG |
165 |
_socketOut.println("PONG" + cmd.substring(4)); |
166 |
} |
167 |
} catch (IOException e) { |
168 |
// comms failure, lets stop |
169 |
_logger.write(this.toString(), Logger.ERROR, "Comms error: "+e); |
170 |
//run = false; |
171 |
} |
172 |
} |
173 |
} |
174 |
|
175 |
/** |
176 |
* Sends a message to the channel. |
177 |
* |
178 |
* @param msg The message to send |
179 |
*/ |
180 |
public void sendMsg(String msg) { |
181 |
_socketOut.println("PRIVMSG "+_channel+" :"+msg); |
182 |
} |
183 |
|
184 |
/** |
185 |
* Sends a notice to the channel. |
186 |
* |
187 |
* @param msg The notice to send |
188 |
*/ |
189 |
public void sendNotice(String msg) { |
190 |
_socketOut.println("NOTICE "+_channel+" :"+msg); |
191 |
} |
192 |
|
193 |
/** |
194 |
* Connect to the IRC server, log in, and join the channel. |
195 |
* |
196 |
* @throws IOException if the connection fails |
197 |
*/ |
198 |
public void connect() throws IOException { |
199 |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
200 |
// setup the socket, reader and writer |
201 |
String server = cp.getProperty(_name, "Alerter.IRC.IRCServer"); |
202 |
int port = Integer.parseInt(cp.getProperty(_name, "Alerter.IRC.IRCPort")); |
203 |
_socket = new Socket(server, port); |
204 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
205 |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
206 |
// send the various log in messages |
207 |
_socketOut.println("PASS"); |
208 |
_socketOut.println("NICK "+cp.getProperty(_name, "Alerter.IRC.nick")); |
209 |
_socketOut.println("USER i-scream 8 * :i-scream alerter bot"); |
210 |
// join the channel |
211 |
_channel = cp.getProperty(_name, "Alerter.IRC.channel"); |
212 |
_socketOut.println("JOIN "+_channel); |
213 |
// start our listening thread |
214 |
this.start(); |
215 |
} |
216 |
|
217 |
/** |
218 |
* Disconnect "nicely" from the IRC server. |
219 |
* |
220 |
* @throws IOException if the disconnection fails |
221 |
*/ |
222 |
public void disconnect() throws IOException { |
223 |
// send proper QUIT |
224 |
_socketOut.println("QUIT : iscreamBot component shutting down..."); |
225 |
// close the socket |
226 |
_socketOut.close(); |
227 |
_socketIn.close(); |
228 |
_socket.close(); |
229 |
} |
230 |
|
231 |
/** |
232 |
* The socket connected to the server |
233 |
*/ |
234 |
private Socket _socket; |
235 |
|
236 |
/** |
237 |
* The writer |
238 |
*/ |
239 |
private PrintWriter _socketOut; |
240 |
|
241 |
/** |
242 |
* The reader |
243 |
*/ |
244 |
private BufferedReader _socketIn; |
245 |
|
246 |
/** |
247 |
* Just a reminder to what channel we're on... |
248 |
* this can't be dynamic :) |
249 |
*/ |
250 |
private String _channel; |
251 |
} |
252 |
|
253 |
} |