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.5
Committed: Fri Mar 2 01:32:09 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.4: +3 -3 lines
Log Message:
It's late, and I'm hacked off with the server. :P

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