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