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 org.w3c.dom.CDATASection;
36  import org.w3c.dom.Document;
37  import org.w3c.dom.Element;
38  import org.w3c.dom.Node;
39  import org.w3c.dom.NodeList;
40  
41  import java.util.ArrayList;
42  import java.util.HashMap;
43  import java.util.Iterator;
44  import java.util.StringTokenizer;
45  import java.util.logging.Logger;
46  
47  import com.levelonelabs.aim.AIMAdapter;
48  import com.levelonelabs.aim.AIMBuddy;
49  import com.levelonelabs.aim.XMLizable;
50  import com.levelonelabs.aimbot.AIMBot;
51  import com.levelonelabs.aimbot.BotModule;
52  
53  
54  /***
55   * A class to handle personal reminders
56   * 
57   * @author Will Gorman
58   * @created May 7th, 2003
59   */
60  public class ReminderModule extends BotModule implements XMLizable {
61      private static ArrayList services;
62      private static Logger logger = Logger.getLogger(ReminderModule.class.getName());
63      private HashMap reminders = new HashMap();
64  
65      /***
66       * Initialize the service commands.
67       */
68      static {
69          services = new ArrayList();
70          services.add("remind");
71          services.add("remember");
72          services.add("forget");
73      }
74  
75  
76      /***
77       * Constructor for the ReminderModule object
78       * 
79       * @param bot
80       */
81      public ReminderModule(AIMBot bot) {
82          super(bot);
83          // register to here about signon events
84          super.addAIMListener(new AIMAdapter() {
85              public void handleBuddySignOn(AIMBuddy buddy, String info) {
86                  String savePref = buddy.getPreference("reminderatsignon");
87                  if ((savePref != null) && savePref.equals("true")) {
88                      retrieveReminders(buddy);
89                  }
90              }
91          });
92      }
93  
94  
95      /***
96       * Gets the name attribute of the ReminderModule object
97       * 
98       * @return The name value
99       */
100     public String getName() {
101         return "Reminder Module";
102     }
103 
104 
105     /***
106      * Gets the services attribute of the ReminderModule object
107      * 
108      * @return The services value
109      */
110     public ArrayList getServices() {
111         return services;
112     }
113 
114 
115     /***
116      * Describes the usage of the module
117      * 
118      * @return the usage of the module
119      */
120     public String help() {
121         StringBuffer sb = new StringBuffer();
122         sb.append("<B>remember <i>TEXT</i></B> (stores text in the system for retrieval later)\n");
123         sb.append("<B>forget <i>All or #</i></B> (removes all items or a specific item)\n");
124         sb.append("<B>remind</B> (lists all reminders)\n");
125         sb
126             .append("* If the preference \"reminderatsignon\" is set to true, you will automatically get a list when you login to aim.\n");
127 
128         return sb.toString();
129     }
130 
131 
132     /***
133      * Grabs and sends any stored reminders for a buddy
134      * 
135      * @param buddy
136      * @return whether any messages where found
137      */
138     public boolean retrieveReminders(AIMBuddy buddy) {
139         // check for messages
140         ArrayList br = (ArrayList) reminders.get(buddy.getName());
141         if (br != null) {
142             if (br.size() > 0) {
143                 String reminder = "Reminders:<BR>";
144 
145                 // collect the messages
146                 for (int i = 0; i < br.size(); i++) {
147                     reminder += ((i + 1) + ")" + (br.get(i) + "<BR>"));
148                 }
149 
150                 // send the list
151                 super.sendMessage(buddy, reminder);
152                 return true;
153             }
154         }
155         return false;
156     }
157 
158 
159     /***
160      * Handle a messaging query
161      * 
162      * @param buddy
163      * @param query
164      */
165     public void performService(AIMBuddy buddy, String query) {
166         if (query.toLowerCase().startsWith("remember")) {
167             handleRemember(buddy, query);
168         } else if (query.toLowerCase().startsWith("forget")) {
169             handleForget(buddy, query);
170         } else if (query.toLowerCase().startsWith("remind")) {
171             if (!retrieveReminders(buddy)) {
172                 sendMessage(buddy, "No reminders available.");
173             }
174         }
175     }
176 
177 
178     /***
179      * Remember funcionality
180      * 
181      * @param buddy
182      * @param query
183      */
184     private void handleRemember(AIMBuddy buddy, String query) {
185         String str = query.substring(8);
186         if ((str != null) && (str.trim().length() != 0)) {
187             addReminder(buddy, str);
188             sendMessage(buddy, "Reminder stored.");
189         } else {
190             if (!retrieveReminders(buddy)) {
191                 sendMessage(buddy, "No reminders available.");
192             }
193         }
194     }
195 
196 
197     /***
198      * Forget functionality
199      * 
200      * @param buddy
201      * @param query
202      */
203     private void handleForget(AIMBuddy buddy, String query) {
204         // handle offline request
205         StringTokenizer st = new StringTokenizer(query, " ");
206 
207         // check for proper params
208         if (st.countTokens() != 2) {
209             sendMessage(buddy, "ERROR:\n" + help());
210             return;
211         }
212         st.nextToken();
213         String tok = st.nextToken();
214         if (tok.equalsIgnoreCase("all")) {
215             clearReminders(buddy);
216             sendMessage(buddy, "All reminders removed.");
217         } else {
218             try {
219                 int loc = Integer.parseInt(tok) - 1;
220                 if (removeReminder(buddy, loc)) {
221                     sendMessage(buddy, "Reminder removed.");
222                 } else {
223                     sendMessage(buddy, "ERROR: number " + tok + " not valid.");
224                 }
225             } catch (Exception e) {
226                 e.printStackTrace();
227                 sendMessage(buddy, "ERROR: number invalid: " + tok);
228             }
229         }
230     }
231 
232 
233     /***
234      * Remove Specific Reminder
235      * 
236      * @param buddy
237      *            buddy
238      * @param i
239      *            list item number
240      * @return true if remove took place
241      */
242     public boolean removeReminder(AIMBuddy buddy, int i) {
243         ArrayList br = (ArrayList) reminders.get(buddy.getName());
244         if ((br != null) && (i >= 0) && (i < br.size())) {
245             br.remove(i);
246             return true;
247         }
248         return false;
249     }
250 
251 
252     /***
253      * Clear reminders for a buddy
254      * 
255      * @param buddy
256      *            buddy
257      */
258     public void clearReminders(AIMBuddy buddy) {
259         ArrayList br = (ArrayList) reminders.get(buddy.getName());
260         if (br != null) {
261             br.clear();
262         }
263     }
264 
265 
266     /***
267      * Add a reminder for a buddy
268      * 
269      * @param buddy
270      *            buddy
271      * @param reminder
272      *            reminder
273      */
274     public void addReminder(AIMBuddy buddy, String reminder) {
275         ArrayList br = (ArrayList) reminders.get(buddy.getName());
276         if (br == null) {
277             br = new ArrayList();
278             reminders.put(buddy.getName(), br);
279         }
280         br.add(reminder);
281     }
282 
283 
284     /***
285      * @see com.levelonelabs.aim.XMLizable#writeState(Element)
286      */
287     public void writeState(Element emptyStateElement) {
288         Document doc = emptyStateElement.getOwnerDocument();
289         Iterator iter = reminders.keySet().iterator();
290 
291         while (iter.hasNext()) {
292             String name = (String) iter.next();
293             ArrayList br = (ArrayList) reminders.get(name);
294             if (br.size() > 0) {
295                 Element buddyElem = doc.createElement("buddy");
296                 buddyElem.setAttribute("name", name);
297                 for (int i = 0; i < br.size(); i++) {
298                     Element remElem = doc.createElement("reminder");
299                     String reminder = (String) br.get(i);
300                     CDATASection data = doc.createCDATASection(reminder);
301                     remElem.appendChild(data);
302                     buddyElem.appendChild(remElem);
303                 }
304                 emptyStateElement.appendChild(buddyElem);
305             }
306         }
307     }
308 
309 
310     /***
311      * @see com.levelonelabs.aim.XMLizable#readState(Element)
312      */
313     public void readState(Element fullStateElement) {
314         // parse reminders
315         reminders = new HashMap();
316         NodeList list = fullStateElement.getElementsByTagName("buddy");
317         for (int i = 0; i < list.getLength(); i++) {
318             Element buddyElem = (Element) list.item(i);
319             String name = buddyElem.getAttribute("name");
320             AIMBuddy buddy = getBuddy(name);
321             if (buddy != null) {
322                 NodeList rs = buddyElem.getElementsByTagName("reminder");
323                 for (int j = 0; j < rs.getLength(); j++) {
324                     Element remindElem = (Element) rs.item(j);
325                     NodeList cdatas = remindElem.getChildNodes();
326                     for (int k = 0; k < cdatas.getLength(); k++) {
327                         Node node = cdatas.item(k);
328                         if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
329                             String reminder = node.getNodeValue();
330                             addReminder(buddy, reminder);
331                             break;
332                         }
333                     }
334                 }
335             }
336         }
337     }
338 }