1 package rero.client.functions; 2 3 import sleep.engine.*; 4 import sleep.runtime.*; 5 import sleep.interfaces.*; 6 import sleep.bridges.BridgeUtilities; 7 8 import rero.client.*; 9 import rero.client.user.*; 10 import rero.client.output.*; 11 import rero.ircfw.data.*; 12 import rero.ircfw.*; 13 14 import rero.util.*; 15 import rero.config.*; 16 17 import java.util.*; 18 19 public class BuiltInOperators extends Feature implements Predicate, Function, Loadable 20 { 21 protected InternalDataList data; 22 protected UserHandler commands; 23 24 public void init() 25 { 26 getCapabilities().getScriptCore().addBridge(this); 27 28 data = (InternalDataList)getCapabilities().getDataStructure("clientInformation"); 29 commands = (UserHandler)getCapabilities().getDataStructure("commands"); 30 } 31 32 public boolean scriptLoaded(ScriptInstance script) 33 { 34 String [] contents = new String [] { 35 "&call", 36 "&mask", 37 "&sendRaw", 38 "&sendNotice", 39 "&sendAction", 40 "&sendMessage", 41 "&sendReply", 42 "&sendRequest", 43 "&nickComplete", 44 "&nickCompleteAll", 45 "&echo", 46 "&echoRaw", 47 "&echoStatus", 48 "&echoAll", 49 "&setQuery", 50 "&cycleQuery", 51 "&processInput", 52 "&openCommand", 53 "&getSupportHints" 54 }; 55 56 for (int x = 0; x < contents.length; x++) 57 { 58 script.getScriptEnvironment().getEnvironment().put(contents[x], this); 59 } 60 61 script.getScriptEnvironment().getEnvironment().put("&say", new say()); 62 script.getScriptEnvironment().getEnvironment().put("&getAliasList", new getAliasList()); 63 script.getScriptEnvironment().getEnvironment().put("&parseSet", new parseSet()); 64 script.getScriptEnvironment().getEnvironment().put("&fireEvent", new fireEvent()); 65 script.getScriptEnvironment().getEnvironment().put("&echoColumns", new echoColumns()); 66 67 return true; 68 } 69 70 private class echoColumns implements Function 71 { 72 public Scalar evaluate(String function, ScriptInstance si, Stack locals) 73 { 74 String window = BridgeUtilities.getString(locals, ""); 75 String text = BridgeUtilities.getString(locals, ""); 76 double percen = BridgeUtilities.getDouble(locals, 1.0); 77 78 getCapabilities().getUserInterface().printChunk(window, text.replace('\t', ' '), text.split("\t"), percen); 79 return SleepUtils.getEmptyScalar(); 80 } 81 } 82 83 84 private class getAliasList implements Function 85 { 86 public Scalar evaluate(String function, ScriptInstance si, Stack parms) 87 { 88 LinkedList rv = new LinkedList(); 89 rv.addAll(rero.dialogs.HelpWindow.getBuiltInAliases()); 90 rv.addAll(commands.getScriptedAliases()); 91 92 return SleepUtils.getArrayWrapper(rv); 93 } 94 } 95 96 private class parseSet implements Function 97 { 98 public Scalar evaluate(String name, ScriptInstance si, Stack parms) 99 { 100 String set = BridgeUtilities.getString(parms, "UNKNOWN_SET"); 101 102 if (!parms.isEmpty() && ((Scalar)parms.peek()).getHash() != null) 103 { 104 ScalarHash hashish = ((Scalar)parms.pop()).getHash(); 105 106 HashMap temp = new HashMap(); 107 Iterator i = hashish.keys().scalarIterator(); 108 while (i.hasNext()) 109 { 110 Scalar key = (Scalar)i.next(); 111 temp.put(key.toString(), hashish.getAt(key).toString()); 112 } 113 114 return SleepUtils.getScalar(getCapabilities().getOutputCapabilities().parseSet(temp, set)); 115 } 116 else 117 { 118 String target = BridgeUtilities.getString(parms, "<unknown>"); 119 String data = BridgeUtilities.getString(parms, ""); 120 121 return SleepUtils.getScalar(getCapabilities().getOutputCapabilities().parseSet(ClientUtils.getEventHashMap(target, data), set)); 122 } 123 } 124 } 125 126 private class fireEvent implements Function 127 { 128 public Scalar evaluate(String fname, ScriptInstance si, Stack parms) 129 { 130 if (parms.size() == 1) 131 { 132 getCapabilities().injectEvent(BridgeUtilities.getString(parms, "")); 133 } 134 else 135 { 136 String name = BridgeUtilities.getString(parms, "UNKNOWN_EVENT"); 137 Scalar temp = BridgeUtilities.getScalar(parms); 138 139 if (temp.getHash() != null) 140 { 141 HashMap eventData = new HashMap(); 142 eventData.put("$event", name); 143 144 Iterator i = temp.getHash().keys().scalarIterator(); 145 while (i.hasNext()) 146 { 147 Scalar key = (Scalar)i.next(); 148 eventData.put(key.toString(), temp.getHash().getAt(key).toString()); 149 } 150 151 getCapabilities().dispatchEvent(eventData); 152 } 153 } 154 155 return SleepUtils.getEmptyScalar(); 156 } 157 } 158 159 private class say implements Function 160 { 161 public Scalar evaluate(String name, ScriptInstance si, Stack parms) 162 { 163 getCapabilities().getChatCapabilities().sendMessage(getCapabilities().getUserInterface().getQuery(), parms.pop().toString()); 164 return SleepUtils.getEmptyScalar(); 165 } 166 } 167 168 public boolean scriptUnloaded(ScriptInstance script) 169 { 170 return true; 171 } 172 173 public Scalar evaluate(String function, ScriptInstance script, Stack locals) 174 { 175 if (function.equals("&echo")) 176 { 177 switch (locals.size()) 178 { 179 case 1: 180 getCapabilities().getUserInterface().printActive(getString(locals)); 181 break; 182 case 2: 183 getCapabilities().getUserInterface().printNormal(getString(locals), getString(locals)); 184 break; 185 case 3: 186 int ck = BridgeUtilities.getInt(locals); 187 getCapabilities().getOutputCapabilities().echoToTarget(getString(locals), getString(locals), ck == 2); 188 break; 189 default: 190 } 191 192 return SleepUtils.getEmptyScalar(); 193 } 194 else if (function.equals("&openCommand") && locals.size() == 1) 195 { 196 try 197 { 198 Runtime.getRuntime().exec(ClientState.getClientState().getString("ui.openfiles", ClientDefaults.ui_openfiles) + " " + locals.pop().toString()); 199 } 200 catch (Exception ex) 201 { 202 ex.printStackTrace(); 203 } 204 } 205 else if (function.equals("&echoStatus") && locals.size() == 1) 206 { 207 getCapabilities().getUserInterface().printStatus(getString(locals)); 208 209 } 210 else if (function.equals("&echoRaw") && locals.size() == 2) 211 { 212 getCapabilities().getUserInterface().printRaw(BridgeUtilities.getString(locals, ""), BridgeUtilities.getString(locals, "")); 213 } 214 else if (function.equals("&echoAll") && locals.size() == 1) 215 { 216 getCapabilities().getUserInterface().printAll(getString(locals)); 217 } 218 else if (function.equals("&setQuery") && locals.size() == 1) 219 { 220 getCapabilities().getUserInterface().setQuery(getString(locals)); 221 } 222 else if (function.equals("&cycleQuery")) 223 { 224 getCapabilities().getOutputCapabilities().cycleQuery(); 225 } 226 else if (function.equals("&sendRaw") && locals.size() == 1) 227 { 228 getCapabilities().sendln(getString(locals)); 229 } 230 else if (function.equals("&mask") && locals.size() == 2) 231 { 232 return SleepUtils.getScalar(ClientUtils.mask(getString(locals), getInt(locals))); 233 } 234 else if (function.equals("&call") && locals.size() == 1) 235 { 236 String temp = getString(locals); 237 238 if (temp.charAt(0) != '/') 239 temp = "/" + temp; 240 241 commands.processCommand(temp); 242 } 243 else if (function.equals("&call") && locals.size() == 2) 244 { 245 String temp = getString(locals); 246 247 if (temp.charAt(0) != '/') 248 temp = "/" + temp; 249 250 commands.processCommandBuiltIn(temp); 251 } 252 else if (function.equals("&processInput") && locals.size() == 1) 253 { 254 commands.processInput(getString(locals)); 255 } 256 else if (function.equals("&nickComplete") && locals.size() == 2) 257 { 258 return SleepUtils.getScalar(data.nickComplete(getString(locals), getString(locals))); 259 } 260 else if (function.equals("&nickCompleteAll") && locals.size() == 2) 261 { 262 Scalar rv = SleepUtils.getArrayScalar(); 263 264 Iterator i = data.nickCompleteAll(getString(locals), getString(locals)).iterator(); 265 while (i.hasNext()) 266 { 267 rv.getArray().push(SleepUtils.getScalar(i.next().toString())); 268 } 269 return rv; 270 } 271 else if (function.substring(1, 5).equals("send") && locals.size() >= 2) 272 { 273 String target = getString(locals); 274 String text = getString(locals); 275 276 if (function.equals("&sendNotice")) { getCapabilities().getChatCapabilities().sendNotice(target, text); } 277 else if (function.equals("&sendMessage")) { getCapabilities().getChatCapabilities().sendMessage(target, text); } 278 else if (function.equals("&sendAction")) { getCapabilities().getChatCapabilities().sendAction(target, text); } 279 else if (function.equals("&sendReply")) { getCapabilities().getChatCapabilities().sendReply(target, text, getString(locals)); } 280 else if (function.equals("&sendRequest")) { getCapabilities().getChatCapabilities().sendRequest(target, text, getString(locals)); } 281 282 return SleepUtils.getEmptyScalar(); 283 } 284 if (function.equals("&getSupportHints")) 285 { 286 return SleepUtils.getHashWrapper(data.getSupportInfo()); 287 } 288 289 return SleepUtils.getEmptyScalar(); 290 } 291 292 private String getString(Stack l) 293 { 294 if (l.isEmpty()) 295 return ""; 296 297 return ((Scalar)l.pop()).getValue().toString(); 298 } 299 300 private int getInt(Stack l) 301 { 302 if (l.isEmpty()) 303 return 0; 304 305 return ((Scalar)l.pop()).getValue().intValue(); 306 } 307 308 public boolean decide(String predicate, ScriptInstance script, Stack terms) 309 { 310 return false; 311 } 312 } 313 | Popular Tags |