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.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.StringTokenizer;
39 import java.util.logging.Logger;
40
41 import com.levelonelabs.aim.AIMBuddy;
42 import com.levelonelabs.aimbot.AIMBot;
43 import com.levelonelabs.aimbot.BotModule;
44
45
46 /***
47 * Used to hanle administration of buddies.
48 *
49 * @author Scott Oster
50 * @created January 09, 2004
51 */
52 public class BuddyManagementModule extends BotModule {
53 private static ArrayList services;
54 static Logger logger = Logger.getLogger(BuddyManagementModule.class.getName());
55
56 /***
57 * Initialize the service commands.
58 */
59 static {
60 services = new ArrayList();
61 services.add("makeusers");
62 services.add("prune");
63 }
64
65
66 /***
67 * Constructor for the BuddyManagementModule object
68 *
69 * @param bot
70 */
71 public BuddyManagementModule(AIMBot bot) {
72 super(bot);
73 }
74
75
76 /***
77 * Gets the services attribute of the BuddyManagementModule object
78 *
79 * @return The services value
80 */
81 public ArrayList getServices() {
82 return services;
83 }
84
85
86 /***
87 * Gets the name attribute of the BuddyManagementModule object
88 *
89 * @return The name value
90 */
91 public String getName() {
92 return "Buddy Management Module";
93 }
94
95
96 /***
97 * Describes the usage of the module
98 *
99 * @return the usage of the module
100 */
101 public String help() {
102 StringBuffer sb = new StringBuffer();
103 sb.append("<B>makeusers</B> (converts all non-user buddies to users *ADMIN ONLY*)\n");
104 sb.append("<B>prune buddies</B> (removes all non-user buddies *ADMIN ONLY*)\n");
105 sb.append("<B>prune inactive</B> (removes \"inactive\" users *ADMIN ONLY*)\n");
106 return sb.toString();
107 }
108
109
110 /***
111 * Perform buddy maintenance
112 *
113 * @param buddy
114 * @param query
115 */
116 public void performService(AIMBuddy buddy, String query) {
117 StringTokenizer st = new StringTokenizer(query, " ");
118 if (!st.hasMoreTokens()) {
119 return;
120 }
121 String imcommand = st.nextToken().trim();
122
123
124 if (!buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
125 super.sendMessage(buddy, "Sorry, you must be an admin to use this command.");
126 return;
127 }
128
129 if (imcommand.equalsIgnoreCase("makeusers")) {
130 if (st.hasMoreTokens()) {
131 super.sendMessage(buddy, "ERROR:\n" + help());
132 }
133 int numConverted = 0;
134 Iterator iter = super.getBuddyNames();
135 while (iter.hasNext()) {
136 String name = (String) iter.next();
137 AIMBuddy bud = super.getBuddy(name);
138
139 if (!bud.hasRole(AIMBot.ROLE_USER) && !super.bot.getUsername().equals(name)) {
140 logger.fine("Adding " + AIMBot.ROLE_USER + " role to " + bud.getName());
141 bud.addRole(AIMBot.ROLE_USER);
142 numConverted++;
143 }
144 }
145 super.sendMessage(buddy, "Converted " + numConverted + " buddies to users.");
146 return;
147 } else if (imcommand.equalsIgnoreCase("prune")) {
148 if (st.countTokens() != 1) {
149 super.sendMessage(buddy, "ERROR:\n" + help());
150 return;
151 }
152 String option = st.nextToken().trim();
153 if (option.equalsIgnoreCase("buddies")) {
154 int numRemoved = 0;
155 Iterator iter = super.getBuddyNames();
156 List toRemoveList = new ArrayList();
157 while (iter.hasNext()) {
158 String name = (String) iter.next();
159 AIMBuddy bud = super.getBuddy(name);
160
161
162 if (!bud.hasRole(AIMBot.ROLE_USER) && !bud.hasRole(AIMBot.ROLE_ADMINISTRATOR)
163 && !super.bot.getUsername().equalsIgnoreCase(name)) {
164 logger.fine("Removing non-user buddy " + bud.getName());
165 toRemoveList.add(bud);
166 numRemoved++;
167 }
168 }
169 super.removeBuddies(toRemoveList);
170 super.sendMessage(buddy, "Removed " + numRemoved + " non-user buddies.");
171 return;
172 } else if (option.equalsIgnoreCase("inactive")) {
173 int numRemoved = 0;
174 Iterator iter = super.getBuddyNames();
175 List toRemoveList = new ArrayList();
176 while (iter.hasNext()) {
177 String name = (String) iter.next();
178 AIMBuddy bud = super.getBuddy(name);
179
180 if (bud.hasRole(AIMBot.ROLE_USER) && !bud.hasRole(AIMBot.ROLE_ADMINISTRATOR)
181 && !super.bot.getUsername().equalsIgnoreCase(name)) {
182
183 if (isInactive(bud)) {
184 logger.fine("Removing inactive buddy " + bud.getName());
185 toRemoveList.add(bud);
186 numRemoved++;
187 }
188 }
189 }
190 super.removeBuddies(toRemoveList);
191 super.sendMessage(buddy, "Removed " + numRemoved + " inactive users.");
192 return;
193 } else {
194 super.sendMessage(buddy, "ERROR:\n" + help());
195 return;
196 }
197
198 }
199 }
200
201
202 /***
203 * Makes the decision of a user should be pruned
204 *
205 * @todo add agressiveness option
206 * @param bud
207 * @return true if we consider the buddy inactive
208 */
209 private static boolean isInactive(AIMBuddy bud) {
210 return !bud.hasMessages() && bud.getPreferences().size() == 0;
211 }
212 }