View Javadoc

1   /*------------------------------------------------------------------------------
2    * The contents of this file are subject to the Mozilla Public License Version
3    * 1.1 (the "License"); you may not use this file except in compliance with
4    * the License. You may obtain a copy of the License at
5    * http://www.mozilla.org/MPL/
6    * Software distributed under the License is distributed on an "AS IS" basis,
7    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8    * the specific language governing rights and limitations under the License.
9    *
10   * The Original Code is levelonelabs.com code.
11   * The Initial Developer of the Original Code is Level One Labs. Portions
12   * created by the Initial Developer are Copyright (C) 2001 the Initial
13   * Developer. All Rights Reserved.
14   *
15   *         Contributor(s):
16   *             Scott Oster      (ostersc@alum.rpi.edu)
17   *             Steve Zingelwicz (sez@po.cwru.edu)
18   *             William Gorman   (willgorman@hotmail.com)
19   *
20   * Alternatively, the contents of this file may be used under the terms of
21   * either the GNU General Public License Version 2 or later (the "GPL"), or
22   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23   * in which case the provisions of the GPL or the LGPL are applicable
24   * instead of those above. If you wish to allow use of your version of this
25   * file only under the terms of either the GPL or the LGPL, and not to allow
26   * others to use your version of this file under the terms of the NPL, indicate
27   * your decision by deleting the provisions above and replace them with the
28   * notice and other provisions required by the GPL or the LGPL. If you do not
29   * delete the provisions above, a recipient may use your version of this file
30   * under the terms of any one of the NPL, the GPL or the LGPL.
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  	//its unfortunate if somebody actually has this name, but I'm not too
65  	// concerned about it
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  		//logger.fine("Starting TelnetModule");
88  		telnetSvr = new TelnetServer(bot);
89  		telnetSvr.start();
90  		//logger.fine("Telnet Server started");
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 				//logger.info("Created server socket on 1234");
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 			//logger.fine("Executing server thread");
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 				//logger.fine("Constructing Telnet Handler");
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 					//logger.fine("Completed constructing Telnet Handler");
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 				//logger.fine("Running Telnet Handler");
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); // append the string
233 							osw.write((char) ch);
234 							// echo the character to the telnet client
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 					// Attempt to clean up after ourselves
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 }