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  
34  package com.levelonelabs.aimbot.modules;
35  
36  
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.Iterator;
40  import java.util.StringTokenizer;
41  
42  import com.levelonelabs.aim.AIMBuddy;
43  import com.levelonelabs.aimbot.AIMBot;
44  import com.levelonelabs.aimbot.BotModule;
45  
46  
47  /***
48   * Handles requests to store and retrieve preferences
49   *
50   * @author Will Gorman
51   *
52   * @created January 31, 2002
53   */
54  public class PreferenceModule extends BotModule {
55      private static ArrayList services;
56  
57      /***
58       * Initialize the service commands.
59       */
60      static {
61          services=new ArrayList();
62          services.add("listprefs");
63          services.add("setpref");
64      }
65  
66      /***
67       * Constructor for the PreferenceModule object
68       *
69       * @param bot
70       */
71      public PreferenceModule(AIMBot bot) {
72          super(bot);
73      }
74  
75      /***
76       * Gets the services attribute of the PreferenceModule 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 PreferenceModule object
87       *
88       * @return The name value
89       */
90      public String getName() {
91          return "Preference 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.append("<B>listprefs</B> (displays current preferences)\n");
103         sb.append("<B>setpref <i>PREFERENCE</i> <i>VALUE</i></B> (sets a preference for the user)\n");
104         sb.append("\n<b>EXAMPLE: setpref zipcode 12345</b>\n");
105         return sb.toString();
106     }
107 
108 
109     /***
110      * Handle a preference query
111      *
112      * @param buddy
113      * @param query
114      */
115     public void performService(AIMBuddy buddy, String query) {
116         if(query.toLowerCase().startsWith("listprefs")) {
117             // RETURN A LIST OF PREFERENCES
118             HashMap prefs=buddy.getPreferences();
119             Iterator iter=prefs.keySet().iterator();
120             StringBuffer sb=new StringBuffer();
121             sb.append("Preferences:\n");
122             while(iter.hasNext()) {
123                 String key=(String) iter.next();
124                 sb.append(key).append(" = ").append(prefs.get(key)).append("\n");
125             }
126             super.sendMessage(buddy, sb.toString());
127         } else if(query.toLowerCase().startsWith("setpref")) {
128             //store the desired pref
129             StringTokenizer st=new StringTokenizer(query, " ");
130             if(st.countTokens() < 3) {
131                 super.sendMessage(buddy, "ERROR:\n"+help());
132             } else {
133                 String imcommand=st.nextToken();
134                 String preference=st.nextToken();
135                 String value=st.nextToken();
136                 while(st.hasMoreTokens()) {
137                     value+=(" "+st.nextToken());
138                 }
139                 buddy.setPreference(preference, value);
140                 super.sendMessage(buddy, "Preference ("+preference+" = "+value+") added.");
141             }
142         }
143     }
144 }