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.StringTokenizer;
37  
38  import com.levelonelabs.aim.AIMBuddy;
39  import com.levelonelabs.aimbot.AIMBot;
40  import com.levelonelabs.aimbot.BotModule;
41  
42  
43  /***
44   * Used to hanle administration of users.
45   * 
46   * @author Will Gorman, Scott Oster
47   * @created January 31, 2002
48   */
49  public class UserAdminModule extends BotModule {
50      private static ArrayList services;
51  
52      /***
53       * Initialize the service commands.
54       */
55      static {
56          services = new ArrayList();
57          services.add("adduser");
58          services.add("rmuser");
59          services.add("denyuser");
60          services.add("permituser");
61          services.add("permitmode");
62      }
63  
64  
65      /***
66       * Constructor for the UserAdminModule object
67       * 
68       * @param bot
69       */
70      public UserAdminModule(AIMBot bot) {
71          super(bot);
72      }
73  
74  
75      /***
76       * Gets the services attribute of the UserAdminModule object
77       * 
78       * @return The services value
79       */
80      public ArrayList getServices() {
81          return services;
82      }
83  
84  
85      /***
86       * Gets the name attribute of the UserAdminModule object
87       * 
88       * @return The name value
89       */
90      public String getName() {
91          return "User Administration Module";
92      }
93  
94  
95      /***
96       * Describes the usage of the module
97       * 
98       * @return the usage of the module
99       */
100     public String help() {
101         StringBuffer sb = new StringBuffer();
102         sb
103             .append("<B>adduser <i>BUDDY</i> <ADMIN></B> (adds a user to the list, along with allowing for admin perms)\n");
104         sb.append("<B>rmuser <i>BUDDY</i></B> (removes a user from the list)\n");
105         sb.append("<B>denyuser <i>BUDDY</i></B> (deny a user *ADMIN ONLY*)\n");
106         sb.append("<B>permituser <i>BUDDY</i></B> (permit a user *ADMIN ONLY*)\n");
107         sb.append("<B>permitmode <i>[MODE]</i></B> (set or display the permitmode*ADMIN ONLY*)\n");
108         return sb.toString();
109     }
110 
111 
112     /***
113      * Perform user maintenance
114      * 
115      * @param buddy
116      * @param query
117      */
118     public void performService(AIMBuddy buddy, String query) {
119         if (query.toLowerCase().startsWith("adduser")) {
120             // add a user
121             StringTokenizer st = new StringTokenizer(query, " ");
122             if (st.countTokens() < 2) {
123                 super.sendMessage(buddy, "ERROR:\n" + help());
124                 return;
125             }
126 
127             // grab the user to add
128             String imcommand = st.nextToken();
129             String imcommandTo = st.nextToken();
130             AIMBuddy to = super.getBuddy(imcommandTo);
131             if (to != null) {
132                 super.sendMessage(buddy, "User " + imcommandTo + " already exists in the system.");
133                 return;
134             }
135 
136             // only admins can add other admins
137             to = new AIMBuddy(imcommandTo);
138             to.addRole(AIMBot.ROLE_USER);
139             if (buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR) && st.hasMoreTokens()
140                 && st.nextToken().equalsIgnoreCase("ADMIN")) {
141                 to.addRole(AIMBot.ROLE_ADMINISTRATOR);
142             }
143             super.addBuddy(to);
144             super.sendMessage(buddy, "User " + imcommandTo + " added.");
145         } else if (query.toLowerCase().startsWith("rmuser")) {
146             // remove a user
147             StringTokenizer st = new StringTokenizer(query, " ");
148             if (st.countTokens() < 2) {
149                 super.sendMessage(buddy, "ERROR:\n" + help());
150                 return;
151             }
152 
153             // grab the user to remove
154             String imcommand = st.nextToken();
155             String imcommandTo = st.nextToken();
156             AIMBuddy to = super.getBuddy(imcommandTo);
157             if (to == null) {
158                 super.sendMessage(buddy, "User " + imcommandTo + " does not exist in the system.");
159             } else if (buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
160                 super.removeBuddy(to);
161                 super.sendMessage(buddy, "User " + imcommandTo + " removed.");
162             } else {
163                 super.sendMessage(buddy, "Only admins can remove users.");
164             }
165         } else if (query.toLowerCase().startsWith("denyuser")) {
166             // deny a user
167             StringTokenizer st = new StringTokenizer(query, " ");
168             if (st.countTokens() < 2) {
169                 super.sendMessage(buddy, "ERROR:\n" + help());
170                 return;
171             }
172 
173             // grab the user to deny
174             String imcommand = st.nextToken();
175             String imcommandTo = st.nextToken();
176             AIMBuddy to = super.getBuddy(imcommandTo);
177             if (to == null) {
178                 super.sendMessage(buddy, "User " + imcommandTo + " does not exist in the system.");
179             } else if (buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
180                 super.denyBuddy(to);
181                 super.sendMessage(buddy, "User " + imcommandTo + " denied.");
182             } else {
183                 super.sendMessage(buddy, "Only admins can deny users.");
184             }
185         } else if (query.toLowerCase().startsWith("permituser")) {
186             // permit a user
187             StringTokenizer st = new StringTokenizer(query, " ");
188             if (st.countTokens() < 2) {
189                 super.sendMessage(buddy, "ERROR:\n" + help());
190                 return;
191             }
192 
193             // grab the user to permit
194             String imcommand = st.nextToken();
195             String imcommandTo = st.nextToken();
196             AIMBuddy to = super.getBuddy(imcommandTo);
197             if (to == null) {
198                 super.sendMessage(buddy, "User " + imcommandTo + " does not exist in the system.");
199             } else if (buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
200                 super.permitBuddy(to);
201                 super.sendMessage(buddy, "User " + imcommandTo + " permitted.");
202             } else {
203                 super.sendMessage(buddy, "Only admins can permit users.");
204             }
205         } else if (query.toLowerCase().startsWith("permitmode")) {
206             // set or display the permit mode
207             StringTokenizer st = new StringTokenizer(query, " ");
208             if (st.countTokens() < 1) {
209                 super.sendMessage(buddy, "ERROR:\n" + help());
210                 return;
211             }
212 
213             if (!buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
214                 super.sendMessage(buddy, "Only admins can permit users.");
215                 return;
216             }
217 
218             String imcommand = st.nextToken();
219             if (st.hasMoreTokens()) {
220                 String newMode = st.nextToken();
221                 try {
222                     int mode = Integer.parseInt(newMode);
223                     if (mode < 1 || mode > 5) {
224                         throw new NumberFormatException("Not within range.");
225                     }
226                     super.setPermitMode(mode);
227                     super.sendMessage(buddy, "Set permit mode to: " + mode
228                         + "\nPERMIT_ALL = 1, DENY_ALL = 2, PERMIT_SOME = 3, DENY_SOME = 4, PERMIT_BUDDIES = 5");
229 
230                 } catch (NumberFormatException nfe) {
231                     super.sendMessage(buddy, "Invalid permit mode.");
232                 }
233             } else {
234                 super.sendMessage(buddy, "Current permit mode is: " + super.getPermitMode()
235                     + "\nPERMIT_ALL = 1, DENY_ALL = 2, PERMIT_SOME = 3, DENY_SOME = 4, PERMIT_BUDDIES = 5");
236             }
237         }
238     }
239 }