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.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
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
157 if (buddy.hasMessages()) {
158 ArrayList messages = buddy.getMessages();
159 String message = "";
160
161
162 for (int i = 0; i < messages.size(); i++) {
163 message += (messages.get(i) + "<BR>");
164 }
165
166
167 super.sendMessage(buddy, message);
168
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
212 if (query.toLowerCase().startsWith("tell")) {
213 StringTokenizer st = new StringTokenizer(query, " ");
214
215
216 if (st.countTokens() < 3) {
217 sendMessage(buddy, "ERROR:\n" + help());
218 return;
219 }
220
221
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
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
238 String imcommandText = "";
239 while (st.hasMoreTokens()) {
240 imcommandText = imcommandText + " " + st.nextToken();
241 }
242
243
244 if (!imcommandText.equals("")) {
245
246
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
270 if (query.toLowerCase().startsWith("offline")) {
271 StringTokenizer st = new StringTokenizer(query, " ");
272
273
274 if (st.countTokens() < 3) {
275 super.sendMessage(buddy, "ERROR:\n" + help());
276 return;
277 }
278
279
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
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 }