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 |
< |
|
35 |
> |
|
36 |
|
// connect to the IRC server |
37 |
|
_ircbot = null; |
38 |
|
try { |
39 |
|
_ircbot = new IRCBot(); |
40 |
|
_ircbot.connect(); |
41 |
< |
_ircbot.sendNotice("iscreamBot activated"); |
41 |
> |
_ircbot.sendNotice("i-scream alerting bot activated"); |
42 |
|
} catch(IOException e) { |
43 |
|
_logger.write(toString(), Logger.ERROR, "Error starting IRCBot: "+e); |
44 |
|
} |
49 |
|
//---PUBLIC METHODS--- |
50 |
|
|
51 |
|
public void sendAlert(Alert alert) { |
52 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
53 |
+ |
int level = Integer.parseInt(cp.getProperty(_name, "Alerter.IRC.level")); |
54 |
|
// only send if it's equal (or above) our level |
55 |
< |
if(alert.getLevel() >= _level) { |
55 |
> |
if(alert.getLevel() >= level) { |
56 |
|
String alertType = Alert.alerts[alert.getLevel()]+"["+String.valueOf(alert.getLevel())+"]"; |
57 |
|
// sort out the message |
58 |
< |
String message = _message; |
58 |
> |
String message = cp.getProperty(_name, "Alerter.IRC.message"); |
59 |
|
message = StringUtils.replaceText(message, "%level%", alertType); |
60 |
|
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
61 |
|
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
96 |
|
//---ACCESSOR/MUTATOR METHODS--- |
97 |
|
|
98 |
|
//---ATTRIBUTES--- |
112 |
– |
|
113 |
– |
// an integer value |
114 |
– |
private int _level; |
99 |
|
|
100 |
< |
// the hostname of the IRC server |
101 |
< |
private String _IRCServer; |
102 |
< |
|
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 |
100 |
> |
/** |
101 |
> |
* A reference to the IRCBot |
102 |
> |
*/ |
103 |
|
private IRCBot _ircbot; |
104 |
|
|
131 |
– |
// a message with the following: %level% and %message% |
132 |
– |
private String _message; |
133 |
– |
|
105 |
|
/** |
106 |
|
* This is the friendly identifier of the |
107 |
|
* component this class is running in. |
180 |
|
* @throws IOException if the connection fails |
181 |
|
*/ |
182 |
|
public void connect() throws IOException { |
183 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
184 |
|
// setup the socket, reader and writer |
185 |
< |
_socket = new Socket(_IRCServer, _IRCPort); |
185 |
> |
String server = cp.getProperty(_name, "Alerter.IRC.IRCServer"); |
186 |
> |
int port = Integer.parseInt(cp.getProperty(_name, "Alerter.IRC.IRCPort")); |
187 |
> |
_socket = new Socket(server, port); |
188 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
189 |
|
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
190 |
|
// send the various log in messages |
191 |
|
_socketOut.println("PASS"); |
192 |
< |
_socketOut.println("NICK "+_nick); |
193 |
< |
_socketOut.println("USER iscream 8 * :i-scream Bot"); |
192 |
> |
_socketOut.println("NICK "+cp.getProperty(_name, "Alerter.IRC.nick")); |
193 |
> |
_socketOut.println("USER i-scream 8 * :i-scream alerter bot"); |
194 |
|
// join the channel |
195 |
+ |
_channel = cp.getProperty(_name, "Alerter.IRC.channel"); |
196 |
|
_socketOut.println("JOIN "+_channel); |
197 |
|
// start our listening thread |
198 |
|
this.start(); |
211 |
|
_socketIn.close(); |
212 |
|
_socket.close(); |
213 |
|
} |
214 |
< |
|
214 |
> |
|
215 |
|
/** |
216 |
|
* The socket connected to the server |
217 |
|
*/ |
227 |
|
*/ |
228 |
|
private BufferedReader _socketIn; |
229 |
|
|
230 |
+ |
/** |
231 |
+ |
* Just a reminder to what channel we're on... |
232 |
+ |
* this can't be dynamic :) |
233 |
+ |
*/ |
234 |
+ |
private String _channel; |
235 |
|
} |
236 |
|
|
237 |
|
} |