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.util.*;
36  
37  import com.levelonelabs.aim.AIMAdapter;
38  import com.levelonelabs.aim.AIMBuddy;
39  import com.levelonelabs.aimbot.AIMBot;
40  import com.levelonelabs.aimbot.BotModule;
41  
42  
43  /***
44   * Handles requests for user queries
45   *
46   * @author Scott Oster
47   *
48   * @created September 6, 2002
49   *
50   * @todo instead of string, store HistoryObject (signon/off, date), then can easily compute on/off
51   *       line durations
52   * @todo attempt to remove signons caused by bot being offline and coming back
53   * @todo when get to HistObjs attempt to maintain sanity of off/on/off/on cycle
54   */
55  public class UserInformationModule extends BotModule {
56  	private static ArrayList services;
57  	private static final int SIZE = 6;
58  
59  	/***
60  	 * Initialize the service commands.
61  	 */
62  	static {
63  		services = new ArrayList();
64  		services.add("history");
65  		services.add("users");
66  		services.add("enemies");
67  	}
68  
69  	private Hashtable userHash = new Hashtable();
70  
71  
72  	/***
73  	 * Constructor for AdminModule.
74  	 * 
75  	 * @param bot
76  	 */
77  	public UserInformationModule(AIMBot bot) {
78  		super(bot);
79  		//register to here about signon events
80  		super.addAIMListener(new AIMAdapter() {
81  			public void handleBuddySignOn(AIMBuddy buddy, String info) {
82  				handleBuddyEvent(buddy, "Signed on: ");
83  			}
84  
85  
86  			public void handleBuddySignOff(AIMBuddy buddy, String info) {
87  				handleBuddyEvent(buddy, "Signed off: ");
88  			}
89  		});
90  	}
91  
92  
93  	void handleBuddyEvent(AIMBuddy buddy, String type) {
94  		ArrayList arr = (ArrayList) userHash.get(buddy.getName().toLowerCase());
95  		if (arr == null) {
96  			arr = new ArrayList();
97  		}
98  		arr.add(type + " " + new Date());
99  		while (arr.size() > SIZE) {
100 			arr.remove(0);
101 		}
102 		userHash.put(buddy.getName().toLowerCase(), arr);
103 	}
104 
105 
106 	/***
107 	 * @see com.levelonelabs.aimbot.BotModule#getName()
108 	 */
109 	public String getName() {
110 		return "User Information Module";
111 	}
112 
113 
114 	/***
115 	 * @see com.levelonelabs.aimbot.BotModule#getServices()
116 	 */
117 	public ArrayList getServices() {
118 		return services;
119 	}
120 
121 
122 	/***
123 	 * @see com.levelonelabs.aimbot.BotModule#help()
124 	 */
125 	public String help() {
126 		StringBuffer sb = new StringBuffer();
127 		sb.append("<B>history <i>USER</i></B> (displays user's recent sign on and off history)\n");
128 		sb.append("<B>users</B> (displays status of all users)\n");
129 		sb.append("A=" + AIMBot.ROLE_ADMINISTRATOR + ", M = messages pending, * = new user, E = Enemy, - = banned\n");
130 		sb.append("<B>enemies</B> (displays status of all enemies)\n");
131 		return sb.toString();
132 	}
133 
134 
135 	/***
136 	 * @see com.levelonelabs.aimbot.BotModule#performService(AIMBuddy, String)
137 	 */
138 	public void performService(AIMBuddy buddy, String query) {
139 		if (query.toLowerCase().startsWith("history")) {
140 			StringTokenizer st = new StringTokenizer(query.trim(), " ");
141 
142 			//check for right number of arguments
143 			if (st.countTokens() < 2) {
144 				sendMessage(buddy, "ERROR:\n" + help());
145 				return;
146 			}
147 
148 			//grab the command and target
149 			String imcommand = st.nextToken();
150 			if (!imcommand.toLowerCase().equals("history")) {
151 				sendMessage(buddy, "ERROR:\n" + help());
152 				return;
153 			}
154 			String imcommandTo = st.nextToken();
155 			AIMBuddy to = getBuddy(imcommandTo);
156 
157 			//verify they are a user of the bot
158 			if (to == null) {
159 				sendMessage(buddy, "User " + imcommandTo
160 					+ " does not exist in the system.\nUse the ADDUSER command to add them.");
161 				return;
162 			}
163 			ArrayList hist = (ArrayList) userHash.get(to.getName().toLowerCase());
164 			if (hist == null) {
165 				sendMessage(buddy, "Sorry, I have no history for user: " + to.getName());
166 				return;
167 			} else {
168 				String result = "";
169 				for (Iterator iter = hist.iterator(); iter.hasNext();) {
170 					String element = (String) iter.next();
171 					result += (element + "\n");
172 				}
173 				sendMessage(buddy, result);
174 				return;
175 			}
176 		} else if (query.toLowerCase().trim().equals("users")) {
177 			String result = "";
178 			int num = 0;
179 
180 			for (Iterator iter = getBuddyNames(); iter.hasNext();) {
181 				String name = (String) iter.next();
182 				AIMBuddy bud = getBuddy(name);
183 				if (bud == null) {
184 					continue;
185 				}
186 				num++;
187 				String markup = "";
188 				if (userHash.get(name.toLowerCase()) == null) {
189 					markup += "*";
190 				}
191 				if (bud.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
192 					markup += "A";
193 				}
194 				if (bud.hasRole(AIMBot.ROLE_ENEMY)) {
195 					markup += "E";
196 				}
197 				if (bud.isBanned()) {
198 					markup += "-";
199 				}
200 				if (bud.hasMessages()) {
201 					markup += "M";
202 				}
203 				result += (bud.getName() + "[" + (bud.isOnline() ? "<B>ON</B>" : "<I>OFF</I>") + "]" + markup + "    ");
204 			}
205 			if (result.trim().equals("")) {
206 				sendMessage(buddy, "Sorry, I have no information about active users.");
207 				return;
208 			} else {
209 				sendMessage(buddy, "Current (<b>" + num + "</b>) Users are:\n" + result);
210 				return;
211 			}
212 		} else if (query.toLowerCase().trim().equals("enemies")) {
213 			String result = "";
214 			int num = 0;
215 
216 			for (Iterator iter = getBuddyNames(); iter.hasNext();) {
217 				String name = (String) iter.next();
218 				AIMBuddy bud = getBuddy(name);
219 				if (bud == null) {
220 					continue;
221 				}
222 				if (bud.hasRole(AIMBot.ROLE_ENEMY)) {
223 					result += (bud.getName() + "[" + (bud.isOnline() ? "<B>ON</B>" : "<I>OFF</I>") + "]    ");
224 					num++;
225 				}
226 			}
227 			if (result.trim().equals("")) {
228 				sendMessage(buddy, "I have no enemies.");
229 				return;
230 			} else {
231 				sendMessage(buddy, "Current (<b>" + num + "</b>) Enemies are:\n" + result);
232 				return;
233 			}
234 		}
235 	}
236 }