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 megahal.MegaDictionary;
36  import megahal.MegaHalInterface;
37  import megahal.MegaModel;
38  
39  import java.io.File;
40  
41  import java.util.ArrayList;
42  import java.util.StringTokenizer;
43  
44  import com.levelonelabs.aim.AIMBuddy;
45  import com.levelonelabs.aimbot.AIMBot;
46  import com.levelonelabs.aimbot.BotModule;
47  
48  
49  /***
50   * Creates Babel poetry from user input
51   *
52   * @author Will Gorman
53   *
54   * @created February 3, 2002
55   */
56  public class MegaHalModule extends BotModule implements MegaHalInterface {
57  	private static ArrayList services;
58  
59  	/***
60  	 * Initialize the service commands.
61  	 */
62  	static {
63  		//init the services
64  		services = new ArrayList();
65  		services.add("megahaladmin");
66  	}
67  
68  	/*** tells MegaModel which order (how many contexts) it should be. */
69  	protected int order = 5;
70  	/*** used for specifing which brain directory to use */
71  	protected String directory = System.getProperty("user.dir") + File.separator + "conf" + File.separator + "megahal";
72  	/*** remembers previous brain directory */
73  	protected String last = null;
74  	MegaModel model = null;
75  
76  
77  	/***
78  	 * Constructor for the BabelModule object
79  	 * 
80  	 * @param bot
81  	 */
82  	public MegaHalModule(AIMBot bot) {
83  		super(bot);
84  		model = new MegaModel(this); // the initialize function really is the
85  		// constructor...
86  		//set default to 3 seconds instead of 5
87  		MegaModel.timeout = 3;
88  		model.load_personality(order, directory, "");
89  		// INIT MEGAHAL
90  	}
91  
92  
93  	/***
94  	 * warn is used for errors that occur.
95  	 * 
96  	 * @param title
97  	 *            error title.
98  	 * @param error_msg
99  	 *            error message.
100 	 * @return boolean whether printing of error was successful.
101 	 */
102 	public boolean warn(String title, String error_msg) {
103 		return true;
104 	}
105 
106 
107 	/***
108 	 * allow for progress tracking of certain activities in MegaModel.
109 	 * 
110 	 * @param message
111 	 *            message to display
112 	 * @param done
113 	 *            amount completed.
114 	 * @param total
115 	 *            total amount to complete.
116 	 * @return whether printing of progress was complete or not.
117 	 */
118 	public boolean progress(String message, int done, int total) {
119 		return true;
120 	}
121 
122 
123 	/***
124 	 * allows for printing of debug information.
125 	 * 
126 	 * @param str
127 	 *            debug string
128 	 */
129 	public void debug(String str) {
130 	}
131 
132 
133 	/***
134 	 * Gets the services attribute of the BabelModule object
135 	 * 
136 	 * @return The services value
137 	 */
138 	public ArrayList getServices() {
139 		return services;
140 	}
141 
142 
143 	/***
144 	 * Gets the name attribute of the BabelModule object
145 	 * 
146 	 * @return The name value
147 	 */
148 	public String getName() {
149 		return "MegaHal Module";
150 	}
151 
152 
153 	/***
154 	 * Describes the usage of the module
155 	 * 
156 	 * @return the usage of the module
157 	 */
158 	public String help() {
159 		StringBuffer sb = new StringBuffer();
160 		sb.append("<B>MEGAHAL</B> commands:\n");
161 		sb.append("<b>megahaladmin save</b> (saves the megahal brain *ADMIN ONLY*)\n");
162 		sb.append("<b>megahaladmin think <i>N</i></b>  (sets the think time to <N> seconds *ADMIN ONLY*)\n");
163 		return sb.toString();
164 	}
165 
166 
167 	/***
168 	 * Create poetry
169 	 * 
170 	 * @param buddy
171 	 * @param query
172 	 */
173 	public void performService(AIMBuddy buddy, String query) {
174 		if (query.toLowerCase().startsWith("megahaladmin")) {
175 			if (!buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
176 				super.sendMessage(buddy, "Sorry, you are not an " + AIMBot.ROLE_ADMINISTRATOR);
177 				return;
178 			}
179 			StringTokenizer st = new StringTokenizer(query, " ");
180 
181 			//check for enough args
182 			if (st.countTokens() < 2) {
183 				super.sendMessage(buddy, "ERROR:\n" + help());
184 				return;
185 			}
186 			st.nextToken();
187 			String imcommand = st.nextToken();
188 			if (imcommand.toLowerCase().equals("save")) {
189 				synchronized (model) {
190 					model.save_model(directory + File.separator + MegaModel.MEGA_BRAIN);
191 				}
192 				super.sendMessage(buddy, "Saved Model");
193 				return;
194 			} else if (imcommand.toLowerCase().equals("think") && (st.countTokens() == 1)) {
195 				int delay = MegaModel.timeout;
196 				String newDelay = st.nextToken();
197 				try {
198 					delay = Integer.parseInt(newDelay);
199 				} catch (Exception e) {
200 					super.sendMessage(buddy, "Error! " + newDelay + " did not parse.");
201 				}
202 				synchronized (model) {
203 					MegaModel.timeout = delay;
204 				}
205 				super.sendMessage(buddy, "Changing thinking time to " + delay + " seconds.");
206 				return;
207 			} else {
208 				super.sendMessage(buddy, "I don't know about " + imcommand);
209 			}
210 
211 		} else {
212 			if (query.toLowerCase().startsWith("you should talk to")) {
213 				AIMBuddy to = getBuddy(query.substring(18).trim());
214 				if (to != null) {
215 					if (to.getName().toLowerCase().equals(super.bot.getUsername().toLowerCase())) {
216 						super.sendMessage(buddy, "Sorry, I can't talk to myself");
217 					} else {
218 						if (!to.isOnline()) {
219 							to.addMessage(buddy.getName() + " said I should talk to you!");
220 							super.sendMessage(buddy, "I'll talk to them once they're online.");
221 						} else {
222 							super.sendMessage(to, buddy.getName() + " said I should talk to you!");
223 							super.sendMessage(buddy, "I've started a conversation with " + to.getName());
224 						}
225 					}
226 				} else {
227 					super.sendMessage(buddy, "I don't know a " + query.substring(18).trim());
228 				}
229 				return;
230 			}
231 			MegaDictionary words = new MegaDictionary();
232 			words.make_words(query.toUpperCase());
233 			String output = "";
234 			synchronized (model) {
235 				model.learn(words);
236 				output = MegaModel.capitalize(model.generate_reply(words));
237 			}
238 
239 			super.sendMessage(buddy, output);
240 		}
241 	}
242 }