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.3
Committed: Fri Mar 2 00:37:03 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.2: +3 -2 lines
Log Message:
Added the source to the appropriate places.

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