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.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
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
143 if (st.countTokens() < 2) {
144 sendMessage(buddy, "ERROR:\n" + help());
145 return;
146 }
147
148
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
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 }