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.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         // verify admin
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                 // if we found a buddy without user role, that isnt us
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                     // if we found a buddy without user/admin role, that isnt
161                     // us
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                     // if we found a user without admin role, that isnt us
180                     if (bud.hasRole(AIMBot.ROLE_USER) && !bud.hasRole(AIMBot.ROLE_ADMINISTRATOR)
181                         && !super.bot.getUsername().equalsIgnoreCase(name)) {
182                         // if they are inactive, remove them
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 }