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.1
Committed: Thu Mar 1 23:45:45 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Log Message:
A basic IRC bot, with the IRCBot itself as an inner class.

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