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