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.StringTokenizer;
38  import java.util.logging.Logger;
39  
40  import com.levelonelabs.aim.AIMAdapter;
41  import com.levelonelabs.aim.AIMBuddy;
42  import com.levelonelabs.aimbot.AIMBot;
43  import com.levelonelabs.aimbot.BotModule;
44  
45  
46  /***
47   * A class to handle on-offline messaging
48   * 
49   * @author Will Gorman, Scott Oster
50   * 
51   * @created January 28, 2002
52   */
53  public class MessengerModule extends BotModule {
54  	private static ArrayList services;
55  	private static Logger logger = Logger.getLogger(MessengerModule.class.getName());
56  
57  	/***
58  	 * Initialize the service commands.
59  	 */
60  	static {
61  		services = new ArrayList();
62  		services.add("tell");
63  		services.add("offline");
64  		services.add("clear");
65  		services.add("show");
66  	}
67  
68  
69  	/***
70  	 * Constructor for the MessengerModule object
71  	 * 
72  	 * @param bot
73  	 */
74  	public MessengerModule(AIMBot bot) {
75  		super(bot);
76  		//register to here about signon events
77  		super.addAIMListener(new AIMAdapter() {
78  			public void handleBuddySignOn(AIMBuddy buddy, String info) {
79  				retrieveMessages(buddy);
80  			}
81  		});
82  	}
83  
84  
85  	/***
86  	 * Gets the name attribute of the MessengerModule object
87  	 * 
88  	 * @return The name value
89  	 */
90  	public String getName() {
91  		return "Messenger Module";
92  	}
93  
94  
95  	/***
96  	 * Gets the services attribute of the MessengerModule object
97  	 * 
98  	 * @return The services value
99  	 */
100 	public ArrayList getServices() {
101 		return services;
102 	}
103 
104 
105 	/***
106 	 * Describes the usage of the module
107 	 * 
108 	 * @return the usage of the module
109 	 */
110 	public String help() {
111 		StringBuffer sb = new StringBuffer();
112 		sb.append("<B>tell <i>BUDDY MESSAGE</i></B> (sends a message to a person in ");
113 		sb.append("the system, if they are offline they will recieve the message ");
114 		sb.append("next time they log in)\n");
115 		sb.append("<B>offline <i>BUDDY MESSAGE</i></B> (will notify the person the next time they log in)\n");
116 		sb.append("<B>clear messages</B> (will erase messages in system for user)\n");
117 		sb.append("<B>show messages</B> (will display all the messages left for a user)\n");
118 		sb.append("* If the preference \"savemessages\" is set to true, need to manually \"clear messages\".\n");
119 
120 		return sb.toString();
121 	}
122 
123 
124 	/***
125 	 * Called when someone leaves a message for someone else to add the message.
126 	 * 
127 	 * @param to
128 	 *            target buddy
129 	 * @param from
130 	 *            sending buddy
131 	 * @param message
132 	 *            message sent
133 	 */
134 	public void addMessage(AIMBuddy to, AIMBuddy from, String message) {
135 		String fromName = "Someone";
136 
137 		if (to == null) {
138 			return;
139 		}
140 
141 		if (from != null) {
142 			fromName = from.getName();
143 		}
144 
145 		to.addMessage(fromName + " said \"" + message + "\" at " + new Date());
146 	}
147 
148 
149 	/***
150 	 * Grabs and sends any stored messages for a buddy when they sign on
151 	 * 
152 	 * @param buddy
153 	 * @return whether any messages where found
154 	 */
155 	public boolean retrieveMessages(AIMBuddy buddy) {
156 		//check for messages
157 		if (buddy.hasMessages()) {
158 			ArrayList messages = buddy.getMessages();
159 			String message = "";
160 
161 			//collect the messages
162 			for (int i = 0; i < messages.size(); i++) {
163 				message += (messages.get(i) + "<BR>");
164 			}
165 
166 			//send the list
167 			super.sendMessage(buddy, message);
168 			//check if they should be saved
169 			String savePref = buddy.getPreference("savemessages");
170 			if ((savePref == null) || savePref.equals("false")) {
171 				buddy.clearMessages();
172 			}
173 			return true;
174 		}
175 		return false;
176 	}
177 
178 
179 	/***
180 	 * Handle a messaging query
181 	 * 
182 	 * @param buddy
183 	 * @param query
184 	 */
185 	public void performService(AIMBuddy buddy, String query) {
186 		if (query.toLowerCase().startsWith("tell")) {
187 			handleTell(buddy, query);
188 		} else if (query.toLowerCase().startsWith("offline")) {
189 			handleOffline(buddy, query);
190 		} else if (query.toLowerCase().startsWith("clear messages")) {
191 			logger.info("Clearing Messages for user:" + buddy + ".");
192 			buddy.clearMessages();
193 			super.sendMessage(buddy, "Messages Cleared");
194 		} else if (query.toLowerCase().startsWith("show messages")) {
195 			if (!retrieveMessages(buddy)) {
196 				super.sendMessage(buddy, "No Messages");
197 			}
198 		}
199 	}
200 
201 
202 	/***
203 	 * Tell funcionality
204 	 * 
205 	 * @param buddy
206 	 * @param query
207 	 */
208 	private void handleTell(AIMBuddy buddy, String query) {
209 		String name = "Someone";
210 
211 		//sendMessage(getBuddy("osterCRD"), "YEP, POOP");
212 		if (query.toLowerCase().startsWith("tell")) {
213 			StringTokenizer st = new StringTokenizer(query, " ");
214 
215 			//check for right number of arguments
216 			if (st.countTokens() < 3) {
217 				sendMessage(buddy, "ERROR:\n" + help());
218 				return;
219 			}
220 
221 			//grab the command and target
222 			String imcommand = st.nextToken();
223 			if (!imcommand.toLowerCase().equals("tell")) {
224 				sendMessage(buddy, "ERROR:\n" + help());
225 				return;
226 			}
227 			String imcommandTo = st.nextToken();
228 			AIMBuddy to = getBuddy(imcommandTo);
229 
230 			//verify they are a user of the bot
231 			if (to == null) {
232 				sendMessage(buddy, "User " + imcommandTo
233 					+ " does not exist in the system.\nUse the ADDUSER command to add them.");
234 				return;
235 			}
236 
237 			//grab the rest of the message and send it to the target
238 			String imcommandText = "";
239 			while (st.hasMoreTokens()) {
240 				imcommandText = imcommandText + " " + st.nextToken();
241 			}
242 
243 			//only send a message if there is somethign there.
244 			if (!imcommandText.equals("")) {
245 				//if the target if not online we need to store the message for
246 				// when they sign on
247 				if (to.isOnline()) {
248 					if (buddy != null) {
249 						name = buddy.getName();
250 					}
251 
252 					sendMessage(to, name + " said: " + imcommandText);
253 				} else {
254 					sendMessage(buddy, imcommandTo + " is offline and will be told when they signon.");
255 					addMessage(to, buddy, imcommandText);
256 				}
257 			}
258 		}
259 	}
260 
261 
262 	/***
263 	 * Offline functionality
264 	 * 
265 	 * @param buddy
266 	 * @param query
267 	 */
268 	private void handleOffline(AIMBuddy buddy, String query) {
269 		//handle offline request
270 		if (query.toLowerCase().startsWith("offline")) {
271 			StringTokenizer st = new StringTokenizer(query, " ");
272 
273 			//check for proper params
274 			if (st.countTokens() < 3) {
275 				super.sendMessage(buddy, "ERROR:\n" + help());
276 				return;
277 			}
278 
279 			//grab the target
280 			String imcommand = st.nextToken();
281 			String imcommandTo = st.nextToken();
282 			AIMBuddy to = super.getBuddy(imcommandTo);
283 			if (to == null) {
284 				super.sendMessage(buddy, "User " + imcommandTo
285 					+ " does not exist in the system.  Use the ADDUSER command to add them.");
286 				return;
287 			}
288 
289 			//grab the message
290 			String imcommandText = "";
291 			while (st.hasMoreTokens()) {
292 				imcommandText = imcommandText + " " + st.nextToken();
293 			}
294 			if (!imcommandText.equals("")) {
295 				sendMessage(buddy, imcommandTo + " will be told next time they signon.");
296 				addMessage(to, buddy, imcommandText);
297 			}
298 		}
299 	}
300 }