ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/IRCBot/IRCBot.java
Revision: 1.1
Committed: Wed Feb 21 02:24:23 2001 UTC (23 years, 1 month ago) by tdb
Branch: MAIN
CVS Tags: PROJECT_COMPLETION, HEAD
Log Message:
An IRCBot class. Provides only basic functionality, such as connecting with
a given name, to a given server, on a given channel. It will respond to pings
and can thus stay as long as required. It will send a message or notice upon
request.

File Contents

# Content
1 //---PACKAGE DECLARATION---
2
3 //---IMPORTS---
4 import java.io.*;
5 import java.net.*;
6
7 /**
8 * This class provides some basic IRCBot functionality. It connects
9 * to a specified server, and will remain there until told to
10 * leave. Whilst connected it can send a message or a notice to
11 * the server.
12 *
13 * @author $Author$
14 * @version $Id$
15 */
16 class IRCBot extends Thread {
17
18 //---FINAL ATTRIBUTES---
19
20 /**
21 * The current CVS revision of this class
22 */
23 public static final String REVISION = "$Revision: 1.1 $";
24
25 //---STATIC METHODS---
26
27 //---CONSTRUCTORS---
28
29 /**
30 * Constructs a new instance of the IRCBot class.
31 *
32 * @param host The hostname of the IRC server
33 * @param port The port number of the IRC server
34 * @param nick The nickname to use
35 * @param channel The channel to join
36 */
37 public IRCBot (String host, int port, String nick, String channel) {
38 _host = host;
39 _port = port;
40 _nick = nick;
41 _channel = channel;
42 }
43
44 //---PUBLIC METHODS---
45
46 /**
47 * Main thread loop, this part of the class listens for
48 * messages from the server, and acts accordingly. At the
49 * present moment it only responds to pings.
50 */
51 public void run() {
52 // flag so we can stop the loop
53 boolean run = true;
54 while(run) {
55 try {
56 // read a command
57 String cmd = _socketIn.readLine();
58 // if it's a PING...
59 if(cmd.startsWith("PING")) {
60 // ...send a PONG
61 _socketOut.println("PONG" + cmd.substring(4));
62 }
63 } catch (IOException e) {
64 // comms failure, lets stop
65 System.out.println(e);
66 run = false;
67 }
68 }
69 }
70
71 /**
72 * Sends a message to the channel.
73 *
74 * @param msg The message to send
75 */
76 public void sendMsg(String msg) {
77 _socketOut.println("PRIVMSG "+_channel+" :"+msg);
78 }
79
80 /**
81 * Sends a notice to the channel.
82 *
83 * @param msg The notice to send
84 */
85 public void sendNotice(String msg) {
86 _socketOut.println("NOTICE "+_channel+" :"+msg);
87 }
88
89 /**
90 * Connect to the IRC server, log in, and join the channel.
91 *
92 * @throws IOException if the connection fails
93 */
94 public void connect() throws IOException {
95 // setup the socket, reader and writer
96 _socket = new Socket(_host, _port);
97 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
98 _socketOut = new PrintWriter(_socket.getOutputStream(), true);
99 // send the various log in messages
100 _socketOut.println("PASS");
101 _socketOut.println("NICK "+_nick);
102 _socketOut.println("USER iscream 8 * :i-scream Bot");
103 // join the channel
104 _socketOut.println("JOIN "+_channel);
105 // start our listening thread
106 this.start();
107 }
108
109 /**
110 * Disconnect "nicely" from the IRC server.
111 *
112 * @throws IOException if the disconnection fails
113 */
114 public void disconnect() throws IOException {
115 // send proper QUIET
116 _socketOut.println("QUIT : iscreamBot component shutting down...");
117 // close the socket
118 _socketOut.close();
119 _socketIn.close();
120 _socket.close();
121 }
122
123 /**
124 * Overrides the {@link java.lang.Object#toString() Object.toString()}
125 * method to provide clean logging (every class should have this).
126 *
127 * This uses the uk.ac.ukc.iscream.util.FormatName class
128 * to format the toString()
129 *
130 * @return the name of this class and its CVS revision
131 */
132 /*public String toString() {
133 return FormatName.getName(
134 _name,
135 getClass().getName(),
136 REVISION);
137 }*/
138
139 //---PRIVATE METHODS---
140
141 //---ACCESSOR/MUTATOR METHODS---
142
143 //---ATTRIBUTES---
144
145 /**
146 * This is the friendly identifier of the
147 * component this class is running in.
148 * eg, a Filter may be called "filter1",
149 * If this class does not have an owning
150 * component, a name from the configuration
151 * can be placed here. This name could also
152 * be changed to null for utility classes.
153 */
154 private String _name = null;
155
156 /**
157 * The hostname of the IRC server
158 */
159 private String _host;
160
161 /**
162 * The port of the IRC server
163 */
164 private int _port;
165
166 /**
167 * The nickname we're using
168 */
169 private String _nick;
170
171 /**
172 * The channel we've joined
173 */
174 private String _channel;
175
176 /**
177 * The socket connected to the server
178 */
179 private Socket _socket;
180
181 /**
182 * The writer
183 */
184 private PrintWriter _socketOut;
185
186 /**
187 * The reader
188 */
189 private BufferedReader _socketIn;
190
191 //---STATIC ATTRIBUTES---
192
193 }