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.Date;
37  import java.util.Enumeration;
38  import java.util.ListIterator;
39  import java.util.StringTokenizer;
40  
41  import com.levelonelabs.aim.AIMBuddy;
42  import com.levelonelabs.aim.AIMGroup;
43  import com.levelonelabs.aimbot.AIMBot;
44  import com.levelonelabs.aimbot.BotModule;
45  
46  
47  /***
48   * Implements list-based messaging services.
49   * 
50   * @author Scott Oster
51   * 
52   * @created May 28, 2002
53   */
54  public class ListModule extends BotModule {
55  	private static ArrayList services;
56  
57  	/***
58  	 * Initialize the service commands.
59  	 */
60  	static {
61  		services = new ArrayList();
62  		services.add("subscribe");
63  		services.add("unsubscribe");
64  		services.add("post");
65  		services.add("members");
66  		services.add("makelist");
67  		services.add("lists");
68  		services.add("removelist");
69  		services.add("addlistmember");
70  		services.add("rmlistmember");
71  	}
72  
73  
74  	/***
75  	 * Constructor for the ListModule object
76  	 * 
77  	 * @param bot
78  	 */
79  	public ListModule(AIMBot bot) {
80  		super(bot);
81  	}
82  
83  
84  	/***
85  	 * Gets the services attribute of the ListModule object
86  	 * 
87  	 * @return The services value
88  	 */
89  	public ArrayList getServices() {
90  		return services;
91  	}
92  
93  
94  	/***
95  	 * Gets the name attribute of the ListModule object
96  	 * 
97  	 * @return The name value
98  	 */
99  	public String getName() {
100 		return "List Module";
101 	}
102 
103 
104 	/***
105 	 * Describes the usage of the module
106 	 * 
107 	 * @return the usage of the module
108 	 */
109 	public String help() {
110 		StringBuffer sb = new StringBuffer();
111 		sb.append("<B>lists</B> (shows all the current lists.)\n");
112 		sb.append("<B>subscribe <i>LISTNAME</i> </B> (subscribe yourself to the named list.)\n");
113 		sb.append("<B>unsubscribe <i>LISTNAME</i> </B> (unsubscribe yourself from the named list.)\n");
114 		sb.append("<B>post <i>LISTNAME</i> <i>MESSAGE</i></B> (will post the message to everyone in the list."
115 			+ "  Offline members will be told when they sign on.)\n");
116 		sb.append("<B>members <i>LISTNAME</i></B> (will show the members of everyone in the list. *ADMIN ONLY*)\n");
117 		sb.append("<B>makelist <i>LISTNAME</i></B> (creates a new list. *ADMIN ONLY*)\n");
118 		sb.append("<B>removelist <i>LISTNAME</i></B> (Removes a list *ADMIN ONLY*)\n");
119 		sb.append("<B>addlistmember <i>LISTNAME BUDDYNAME</i></B> (adds a user to a list) *ADMIN ONLY*)\n");
120 		sb.append("<B>rmlistmember <i>LISTNAME BUDDYNAME</i> </B> (removes a user from a list) *ADMIN ONLY*)\n");
121 		return sb.toString();
122 	}
123 
124 
125 	/***
126 	 * Handle a list query
127 	 * 
128 	 * @param buddy
129 	 * @param query
130 	 */
131 	public void performService(AIMBuddy buddy, String query) {
132 		if (query.toLowerCase().trim().equals("lists")) {
133 			processLists(buddy);
134 		} else {
135 			StringTokenizer st = new StringTokenizer(query, " ");
136 			if (st.countTokens() < 2) {
137 				super.sendMessage(buddy, "ERROR:\n" + help());
138 				return;
139 			}
140 
141 			if (query.toLowerCase().startsWith("subscribe")) {
142 				processSubscribe(buddy, st);
143 			} else if (query.toLowerCase().startsWith("unsubscribe")) {
144 				processUnsubscribe(buddy, st);
145 			} else if (query.toLowerCase().startsWith("members")) {
146 				processMembers(buddy, st);
147 			} else if (query.toLowerCase().startsWith("makelist")) {
148 				processMakeList(buddy, st);
149 			} else if (query.toLowerCase().startsWith("post")) {
150 				processPost(buddy, st);
151 			} else if (query.toLowerCase().startsWith("removelist")) {
152 				processRemoveList(buddy, st);
153 			} else if (query.toLowerCase().startsWith("addlistmember")) {
154 				processAddListMember(buddy, st);
155 			} else if (query.toLowerCase().startsWith("rmlistmember")) {
156 				processRemoveListMember(buddy, st);
157 			}
158 		}
159 
160 	}
161 
162 
163 	/***
164 	 * @param buddy
165 	 */
166 	private void processLists(AIMBuddy buddy) {
167 		StringBuffer lists = new StringBuffer("The current lists are:\n");
168 		for (Enumeration enumer = super.bot.getGroups(); enumer.hasMoreElements();) {
169 			AIMGroup next = (AIMGroup) enumer.nextElement();
170 			lists.append(next.getName() + " with " + next.size() + " members.\n");
171 		}
172 		super.sendMessage(buddy, lists.toString());
173 	}
174 
175 
176 	/***
177 	 * @param buddy
178 	 * @param st
179 	 */
180 	private void processRemoveListMember(AIMBuddy buddy, StringTokenizer st) {
181 		if (!buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
182 			super.sendMessage(buddy, "Sorry. This command is for admins only.");
183 		} else if (st.countTokens() < 3) {
184 			super.sendMessage(buddy, "Error:\n" + help());
185 		} else {
186 			String imcommand = st.nextToken();
187 			String list = st.nextToken();
188 			String buddyname = st.nextToken();
189 
190 			AIMGroup group = super.bot.getGroup(list);
191 			AIMBuddy ibuddy = getBuddy(buddyname);
192 			if (ibuddy == null) {
193 				super.sendMessage(buddy, "the user " + buddyname + " does not exist");
194 			} else {
195 				if (group != null) {
196 					if (group.remove(ibuddy.getName())) {
197 						super.sendMessage(buddy, buddyname + " was successfully removed from list:" + list);
198 					} else {
199 						super.sendMessage(buddy, buddyname + " was not a member of list:" + list);
200 					}
201 				} else {
202 					super.sendMessage(buddy, "The list, " + list + ", does not exist.");
203 				}
204 			}
205 		}
206 	}
207 
208 
209 	/***
210 	 * @param buddy
211 	 * @param st
212 	 */
213 	private void processAddListMember(AIMBuddy buddy, StringTokenizer st) {
214 		if (!buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
215 			super.sendMessage(buddy, "Sorry. This command is for admins only.");
216 		} else if (st.countTokens() < 3) {
217 			super.sendMessage(buddy, "Error: \n" + help());
218 
219 		} else {
220 			String imcommand = st.nextToken();
221 			String list = st.nextToken();
222 			String buddyname = st.nextToken();
223 			AIMBuddy ibuddy = getBuddy(buddyname);
224 			if (ibuddy == null) {
225 				super.sendMessage(buddy, buddyname + " is not a user, please ADDUSER");
226 			} else {
227 				AIMGroup group = super.bot.getGroup(list);
228 				if (group != null) {
229 					if (group.add(ibuddy.getName())) {
230 						super.sendMessage(buddy, buddyname + " was successfully added to list:" + list);
231 					} else {
232 						super.sendMessage(buddy, buddyname + " was already a member of list:" + list);
233 					}
234 				} else {
235 					super.sendMessage(buddy, "The list, " + list + ", does not exist.");
236 				}
237 			}
238 		}
239 	}
240 
241 
242 	/***
243 	 * @param buddy
244 	 * @param st
245 	 */
246 	private void processRemoveList(AIMBuddy buddy, StringTokenizer st) {
247 		if (!buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
248 			super.sendMessage(buddy, "Sorry. This command is for admins only.");
249 		} else if (st.countTokens() < 1) {
250 			super.sendMessage(buddy, "Error: \n" + help());
251 		} else {
252 			String list = st.nextToken();
253 			list = st.nextToken();
254 			AIMGroup group = super.bot.getGroup(list);
255 			if (group != null) {
256 				super.bot.removeGroup(group);
257 				super.sendMessage(buddy, "The list " + list + " has been removed");
258 			} else {
259 				super.sendMessage(buddy, "The list " + list + " does not exist");
260 			}
261 		}
262 	}
263 
264 
265 	/***
266 	 * @param buddy
267 	 * @param st
268 	 */
269 	private void processPost(AIMBuddy buddy, StringTokenizer st) {
270 		if (st.countTokens() < 3) {
271 			super.sendMessage(buddy, "ERROR:\n" + help());
272 		} else {
273 			String imcommand = st.nextToken();
274 			String list = st.nextToken();
275 
276 			//grab the rest of the message and send it to the group
277 			String message = "";
278 			while (st.hasMoreTokens()) {
279 				message += (" " + st.nextToken());
280 			}
281 
282 			AIMGroup group = super.bot.getGroup(list);
283 			if (group != null) {
284 				sendGroupMessage(group, message, buddy);
285 			} else {
286 				super.sendMessage(buddy, "The list, " + list + ", does not exist.");
287 			}
288 		}
289 	}
290 
291 
292 	/***
293 	 * @param buddy
294 	 * @param st
295 	 */
296 	private void processMakeList(AIMBuddy buddy, StringTokenizer st) {
297 		if (!buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
298 			super.sendMessage(buddy, "Sorry. This command is for admins only.");
299 		} else {
300 			String imcommand = st.nextToken();
301 			String list = st.nextToken();
302 			AIMGroup group = super.bot.getGroup(list);
303 			if (group != null) {
304 				super.sendMessage(buddy, "The list, " + list + ", already exists.");
305 			} else {
306 				super.bot.addGroup(new AIMGroup(list));
307 				super.sendMessage(buddy, "The list, " + list + ", was created with no members.");
308 			}
309 		}
310 	}
311 
312 
313 	/***
314 	 * @param buddy
315 	 * @param st
316 	 */
317 	private void processMembers(AIMBuddy buddy, StringTokenizer st) {
318 		if (!buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
319 			super.sendMessage(buddy, "Sorry. This command is for admins only.");
320 		} else {
321 			String imcommand = st.nextToken();
322 			String list = st.nextToken();
323 			AIMGroup group = super.bot.getGroup(list);
324 			if (group != null) {
325 				super.sendMessage(buddy, "The list, " + list + ", contains: " + group.toString());
326 			} else {
327 				super.sendMessage(buddy, "The list, " + list + ", does not exist.");
328 			}
329 		}
330 	}
331 
332 
333 	/***
334 	 * @param buddy
335 	 * @param st
336 	 */
337 	private void processUnsubscribe(AIMBuddy buddy, StringTokenizer st) {
338 		String imcommand = st.nextToken();
339 		String list = st.nextToken();
340 		AIMGroup group = super.bot.getGroup(list);
341 		if (group != null) {
342 			if (group.remove(buddy.getName())) {
343 				super.sendMessage(buddy, "You were successfully removed from list:" + list);
344 			} else {
345 				super.sendMessage(buddy, "You were not a member of list:" + list);
346 			}
347 		} else {
348 			super.sendMessage(buddy, "The list, " + list + ", does not exist.");
349 		}
350 	}
351 
352 
353 	/***
354 	 * @param buddy
355 	 * @param st
356 	 */
357 	private void processSubscribe(AIMBuddy buddy, StringTokenizer st) {
358 		String imcommand = st.nextToken();
359 		String list = st.nextToken();
360 		AIMGroup group = super.bot.getGroup(list);
361 		if (group != null) {
362 			if (group.add(buddy.getName())) {
363 				super.sendMessage(buddy, "You were successfully added to list:" + list);
364 			} else {
365 				super.sendMessage(buddy, "You were already a member of list:" + list);
366 			}
367 		} else {
368 			super.sendMessage(buddy, "The list, " + list + ", does not exist.");
369 		}
370 	}
371 
372 
373 	/***
374 	 * Sends a message to a group of buddies
375 	 * 
376 	 * @param group
377 	 * @param message
378 	 * @param from
379 	 */
380 	private void sendGroupMessage(AIMGroup group, String message, AIMBuddy from) {
381 		int toldNow = 0;
382 		for (ListIterator it = group.getList().listIterator(); it.hasNext();) {
383 			AIMBuddy next = getBuddy((String) it.next());
384 			if (next.isOnline()) {
385 				super.sendMessage(next, from.getName() + " posted to the " + group.getName() + " list:" + message);
386 				toldNow++;
387 			} else {
388 				next.addMessage(from.getName() + " posted to the " + group.getName() + " list: \"" + message + "\" at "
389 					+ new Date());
390 			}
391 		}
392 		super.sendMessage(from, "Told " + toldNow + " of " + group.size() + " list members.");
393 	}
394 }