ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java
Revision: 1.2
Committed: Fri Mar 2 00:14:13 2001 UTC (23 years, 6 months ago) by tdb
Branch: MAIN
Changes since 1.1: +5 -3 lines
Log Message:
Modified to fit with the new Alert object.

File Contents

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