1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package com.levelonelabs.aimbot.modules;
34
35 import java.io.InputStream;
36 import java.io.InputStreamReader;
37 import java.io.OutputStream;
38 import java.io.OutputStreamWriter;
39
40 import java.net.ServerSocket;
41 import java.net.Socket;
42
43 import java.util.ArrayList;
44 import java.util.logging.Logger;
45
46 import com.levelonelabs.aim.AIMBuddy;
47 import com.levelonelabs.aimbot.AIMBot;
48 import com.levelonelabs.aimbot.BotModule;
49
50
51 /***
52 * Creates a telnet port to communicate with JaimBot.
53 *
54 * By default, opens port 1234. Allows telnet clients connect to it and send
55 * Jaimbot commands to it. Operations like tell and post will work normally.
56 * However, the Jaimbot command chain does not currently return a module's
57 * output, therefore no output will be produced to the telnet session.
58 *
59 * @author Steve Zingelwicz
60 *
61 * @created May 28, 2002
62 */
63 public class TelnetModule extends BotModule {
64
65
66 private static final String TELNET_BUDDY_NAME = "TelnetMod";
67 private static ArrayList services;
68 static Logger logger = Logger.getLogger(TelnetModule.class.getName());
69
70 /***
71 * Initialize the service commands.
72 */
73 static {
74 services = new ArrayList();
75 }
76
77 private TelnetServer telnetSvr;
78
79
80 /***
81 * Constructor for the ListModule object
82 *
83 * @param bot
84 */
85 public TelnetModule(AIMBot bot) {
86 super(bot);
87
88 telnetSvr = new TelnetServer(bot);
89 telnetSvr.start();
90
91 }
92
93
94 /***
95 * Gets the services attribute of the ListModule object
96 *
97 * @return The services value
98 */
99 public ArrayList getServices() {
100 return services;
101 }
102
103
104 /***
105 * Gets the name attribute of the ListModule object
106 *
107 * @return The name value
108 */
109 public String getName() {
110 return "Telnet Module";
111 }
112
113
114 /***
115 * This will be called when the AIMBot gets a string starting with a keyword
116 * that this module advertised as a service.
117 *
118 * @param buddy
119 * @param query
120 */
121 public void performService(AIMBuddy buddy, String query) {
122 }
123
124
125 /***
126 * Describes the usage of the module
127 *
128 * @return the usage of the module
129 */
130 public String help() {
131 StringBuffer sb = new StringBuffer();
132 sb.append("The Telnet module does not support commands through this interface.\n");
133 return sb.toString();
134 }
135
136
137 /***
138 * Returns or creates and returns the mods buddy
139 *
140 * @return the mods buddy
141 */
142 private AIMBuddy getModBuddy() {
143 AIMBuddy modBuddy = super.getBuddy(TELNET_BUDDY_NAME);
144 if (modBuddy == null) {
145 modBuddy = new AIMBuddy(TELNET_BUDDY_NAME);
146 super.addBuddy(modBuddy);
147 }
148 return modBuddy;
149 }
150
151
152 public class TelnetServer extends Thread {
153 public ServerSocket svrSock;
154 public AIMBot parentBot;
155 public boolean bRunSvr = false;
156
157
158 public TelnetServer(AIMBot bot) {
159 try {
160 svrSock = new ServerSocket(1234);
161
162 parentBot = bot;
163 bRunSvr = true;
164 } catch (Exception e) {
165 logger.severe("Could not create server socket in Telent Module");
166 bRunSvr = false;
167 }
168 }
169
170
171 public void run() {
172 Socket sock;
173 TelnetHandler th;
174
175
176 try {
177 while (bRunSvr == true) {
178 sock = svrSock.accept();
179 logger.info("Accepted connection ");
180 th = new TelnetHandler(sock);
181 th.start();
182 logger.fine("Spun off handler thread");
183 }
184 } catch (Exception e) {
185 logger.severe("Error accepting connection in Telnet Module");
186 }
187 }
188
189
190 public class TelnetHandler extends Thread {
191 Socket sock;
192 InputStream is;
193 OutputStream os;
194 InputStreamReader isr;
195 OutputStreamWriter osw;
196 boolean bRunState = false;
197
198
199 public TelnetHandler(Socket newSock) {
200 sock = newSock;
201
202 try {
203 is = sock.getInputStream();
204 isr = new InputStreamReader(is);
205
206 os = sock.getOutputStream();
207 osw = new OutputStreamWriter(os);
208
209 bRunState = true;
210
211 } catch (Exception e) {
212 logger.severe("Unable to start Telnet Handler");
213 }
214 }
215
216
217 public void run() {
218 StringBuffer cmdBuf = new StringBuffer();
219 String cmd;
220 int ch;
221
222 if (bRunState == false) {
223 return;
224 }
225
226
227 try {
228 osw.write("Welcome to Jaimbot's Telnet Module. Type quit to quit.\n");
229 osw.flush();
230 do {
231 while ((ch = isr.read()) != '\n') {
232 cmdBuf.append((char) ch);
233 osw.write((char) ch);
234
235 osw.flush();
236 }
237 osw.write('\n');
238 osw.flush();
239
240 cmd = cmdBuf.toString();
241 cmdBuf = new StringBuffer();
242 cmd = cmd.trim();
243
244 logger.fine("Received command: ");
245 logger.fine("-" + cmd + "-");
246
247 if (!cmd.equals("quit")) {
248 logger.fine("Executing bot command...");
249 parentBot.handleMessage(getModBuddy(), cmd);
250 logger.fine("Ok");
251 }
252 } while (!cmd.equals("quit"));
253
254
255 isr.close();
256 osw.close();
257 sock.close();
258 } catch (Exception e) {
259 e.printStackTrace();
260 logger.severe("Trouble in TelnetHandler");
261 }
262 }
263 }
264 }
265 }