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.io.IOException;
36  import java.io.InputStream;
37  
38  import java.util.ArrayList;
39  import java.util.Properties;
40  
41  import java.util.logging.Logger;
42  
43  import com.levelonelabs.aim.AIMBuddy;
44  
45  import com.levelonelabs.aimbot.AIMBot;
46  import com.levelonelabs.aimbot.BotModule;
47  
48  
49  /***
50   * Handles requests to control X10 devices using Heyu and CM11A. Heyu runs on
51   * Linux/Unix and is written by Daniel B. Suthers. Heyu is available for free
52   * at http://heyu.tanj.com. Daniel requests that NO mirrors of his Heyu source
53   * code be created.
54   * 
55   * HeyuModule officially supports Heyu version 1.35. HeyuModule REQUIRES a
56   * WORKING INSTALLATION of Heyu on the same physical machine (i.e. you must be
57   * running the jaimbot on Linux/Unix with a CM11A attached and Heyu installed,
58   * running and working).
59   * 
60   * Please see the Heyu FAQ at http://heyu.tanj.com for details about X10 and
61   * the CM11A. The CM11A is available from http://www.x10.com and other sources
62   * for under $50. Bundled deals that include X10 modules for the CM11A to
63   * control can often be purchased at that price (a substantial discount).
64   * Please note: The author is NOT trying to generate sales for X10, just simply
65   * stating how you can get started using this module.
66   * 
67   * CM11A and X10 may be Registered Trademarks of X10 Corp. (http://www.x10.com)
68   * 
69   * The author of this software is not affiliated with X10 Corp.
70   * 
71   * @author David Nelson (david@david-nelson.com)
72   * 
73   * @created November 22, 2003
74   */
75  public class HeyuModule extends BotModule {
76  	private static ArrayList services;
77  	private static Logger logger = Logger.getLogger(HeyuModule.class.getName());
78  	private static String requiredRole = AIMBot.ROLE_ADMINISTRATOR;
79  	private static String heyuPath = "/usr/local/bin/heyu";
80  	private static String successMessage = "";
81  	private String modulesText = "";
82  	private boolean allowHelp = false;
83  
84  	/***
85  	 * Initialize the service commands.
86  	 */
87  	static {
88  		services = new ArrayList();
89  		services.add("turn");
90  		services.add("heyu");
91  	}
92  
93  
94  	public ArrayList getServices() {
95  		return services;
96  	}
97  
98  
99  	public String help() {
100 		String helpText = "";
101 		StringBuffer sb = new StringBuffer();
102 
103 		if (allowHelp) {
104 			sb.append("<B>turn</B> (executes Heyu commands)\n");
105 			sb.append("\n<b>EXAMPLE: turn office on</b>\n");
106 			sb.append("<B>heyu</B> (lists admin supplied module text)\n");
107 			helpText = sb.toString();
108 		}
109 		return helpText;
110 	}
111 
112 
113 	public String getModules() {
114 		return modulesText;
115 	}
116 
117 
118 	public void performService(AIMBuddy buddy, String query) {
119 		if (buddy.hasRole(requiredRole)) {
120 			if (query.toLowerCase().startsWith("turn")) {
121 				try {
122 					Runtime.getRuntime().exec(heyuPath + " " + query.toLowerCase());
123 					logger.finest(buddy.getName() + " performed: " + query.toLowerCase());
124 					if (successMessage != null && successMessage.trim().length() > 0) {
125 						super.sendMessage(buddy, successMessage);
126 					}
127 				} catch (IOException ioe) {
128 					super.sendMessage(buddy, "Error processing command: " + ioe.getMessage());
129 					logger.severe("Error processing command: " + ioe.getMessage());
130 				}
131 			} else if (query.toLowerCase().startsWith("heyu")) {
132 				super.sendMessage(buddy, getModules());
133 			}
134 		}
135 	}
136 
137 
138 	public String getName() {
139 		return "Heyu Module";
140 	}
141 
142 
143 	public HeyuModule(AIMBot bot) {
144 		super(bot);
145 
146 		Properties props = new Properties();
147 		try {
148 			InputStream is = ClassLoader.getSystemResourceAsStream("HeyuModule.properties");
149 			props.load(is);
150 			requiredRole = props.getProperty("requiredRole", requiredRole);
151 			heyuPath = props.getProperty("heyuPath", heyuPath);
152 			successMessage = props.getProperty("successMessage", successMessage);
153 			String helpAllowed = props.getProperty("allowHelp");
154 			if (helpAllowed != null) {
155 				helpAllowed = helpAllowed.trim().toLowerCase();
156 			} else {
157 				helpAllowed = "false";
158 			}
159 			allowHelp = (helpAllowed.equals("true"));
160 			int i = 0;
161 			String module = props.getProperty("modules" + i);
162 			while (module != null) {
163 				modulesText = modulesText + module + "\n";
164 				i++;
165 				module = props.getProperty("modules" + i);
166 			}
167 		} catch (Exception e) {
168 			logger.info("Error Reading HeyuModule.properties: " + e.getMessage());
169 		}
170 	}
171 }