Commits

firefly  committed d93b829

Oh god... sandboxing =js.

  • Participants
  • Parent commits 35d7bc4

Comments (0)

Files changed (9)

 				this.manager = new javax.script.ScriptEngineManager();
 			}
 			this.jsEngine = this.manager.getEngineByName('js');
-			this.jsEngine.eval('for (var k in Packages) delete this[k], delete Packages[k]; delete this.Packages');
+			this.jsEngine.eval('delete java; delete javax; delete com; delete org; delete this.Packages');
 			this.jsEngine.eval(new java.io.FileReader('js/lib/functional.js'));
 			this.jsEngine.eval(new java.io.FileReader('js/lib/complex.js'));
 			this.jsEngine.eval(new java.io.FileReader('js/jsCommand.js'));
 		java.lang.Thread.sleep(400);
 		if (thread.getState() != java.lang.Thread.State.TERMINATED) {
 			send("...timed out!");
-			thread.interrupt();
+			thread.stop();	// FIXME: OH NOES OH GOD DON'T DO THIS AAAAAAAAAAAAAAAAAAAARRRGHHHHH!
 		//	this.executor.awaitTermination(10, java.util.concurrent.TimeUnit.MILLISECONDS);
 		//	send("testy");
 		}
 	
 //	Eldis.require('js/basicCommands.js');
 //	Eldis.require('js/exts/chatter.js');
+	Eldis.require('js/exts/karma.js');
+	Eldis.require('js/exts/giantbomb.js');
 	
 	Eldis.registerRaw(376, onConnect);
 	Eldis.registerRaw(403, numReportError);

File js/internal.js

 	files: {}
 };
 var Data={}, Session={};
+var Global = this;
 
 	// Internal functions.
 Eldis.setProperties = function(server, user, source) {
 Eldis.internalUnloadAll = function() {
 	Eldis.cmds = {};
 	Eldis.numerics = {};
+	Eldis.files = {};
 };
 
 function isArray(a) {
 		this.type = type;
 	};
 	
+	var CallbackTrigger = function(type, cb) {
+		Trigger.call(this, type);
+		this.call = cb;
+	};
+	CallbackTrigger.prototype = new Trigger(null);
+	CallbackTrigger.prototype.initialize = function(args) {
+		this.internalJavaTrigger = Eldis.createTrigger(this.type, args);
+	};
+	
 	var CommandTrigger = function(prefixes) {
 		this.prefixes = prefixes;
 		this.commands = {};
 	};
 	
 	Eldis.CommandTrigger = CommandTrigger;
+	Eldis.CallbackTrigger = CallbackTrigger;
 })();
 
 Eldis.registerTrigger = function(trigger) {
 	if (typeof(file) == 'string' && file.match(/^[^\/](?!.*\.\.)[.\/\w]*$/)) {
 		if (!Eldis.files[file]) {
 			Eldis.files[file] = true;
-			eval(Eldis.read(file).join('\n'));
+			
+			(function() {
+				eval(Eldis.read(file).join('\n'));
+			}).call(Global);
 		}
 	} else {
 		throw new TypeError("require: File must be a vaild path.");
 	}
 	return out;
 };
-Eldis.socketRead = function(url, callback) {
+Eldis.socketRead = function(url, callback, data) {
+	if (data) {
+		var params = [];
+		for (var k in data) params.push(k + "=" + encodeURIComponent(data[k]));
+		
+		if (params.length > 0) {
+			var separator = url.match(/\?/) ? "&" : "?";
+			url += separator + params.join("&");
+		}
+	}
+	
 	var buffer;
 	var url = new java.net.URL(url);
 	var conn = url.openConnection();
 	var reader = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
 	
 	for (var i=0; (buffer = reader.readLine()) instanceof java.lang.String &&
-		callback(Eldis.toRealString(buffer), i); i++);
+			callback(Eldis.toRealString(buffer), i); i++);
 };
 Eldis.socketPost = function(url, callback, post) {
 	var buffer, data = [];
 		return json;
 	};
 })();
-Eldis.tr = function(str) {
+Eldis.tr = function(str, dict) {
 	var chan = Eldis.state.source;
+	var string = str;
 	
-	if (chan.charAt(0) != '#') {
-		return str;
-	} else {
+	if (chan.charAt(0) == '#') {
 		var channels = Data.servers[Eldis.state.server].channels;
+		var string = str;
 		
-		if (chan in channels && 'lang' in
-				channels[chan]) {
+		if (chan in channels && 'lang' in channels[chan]) {
 			var lang = channels[chan].lang;
 			
-			if (typeof(Data.langs) == 'object' &&
-					lang in Data.langs && str in
-					Data.langs[lang]) {
-				return Data.langs[lang][str];
-			} else {
-				return str;
+			if (Data.langs && lang in Data.langs && str in Data.langs[lang]) {
+				string = Data.langs[lang][str];
 			}
-		} else {
-			return str;
 		}
 	}
+	
+	return Eldis.utils.format(string, dict || {});
 };
 Eldis.trLazy = function(str) {
 	return function() {
 		return Eldis.tr(str);
-	}
+	};
 };
 Eldis.escape = function(str) {
 	return str.replace(/[\000-\010\013-\037]/g, function(chr) {
 				out[i] = func.call(arr, arr[i], i);
 			return out;
 		}
+	},
+	format: function(str, dict) { 
+		return str.replace(/(^|[^%])%([A-Za-z_$]+)/g, function(match, extra, key) {
+			if (key in dict) {
+				return extra + dict[key];
+			} else {
+				throw new Error("Eldis.tr: Couldn't find key '" + key + "' in dict when replacing '" + str + "'.");
+			}
+		});
 	}
 };
 

File src/nu/firefly/eldis3/Callback.java

 package nu.firefly.eldis3;
 
-public interface Callback<A, B> {
-	public void call(A a, B b);
+public interface Callback<A, B, C> {
+	public A call(B b, C c);
 }

File src/nu/firefly/eldis3/EldisObject.java

 
 
 public interface EldisObject {
-	public void registerTrigger(Trigger trigger, Callback<String, String> callback);
+	public void registerTrigger(Trigger trigger, Callback<Boolean, String, String> callback);
 	public void registerRaw(String command);
 	public void connectTo(String name, String address, int port, String nick, String pass);
 	public void raw(String server, String raw);

File src/nu/firefly/eldis3/IRCBot.java

 public class IRCBot {
 	private ScriptEngine engine;
 	private Invocable invocable;
-	private Map<Trigger, Callback<String, String>> commands;
+	private Map<Trigger, Callback<Boolean, String, String>> commands;
 	private Set<String> registeredRaws;
 	
 	private String filename;
 	
 	private Map<String, IRCSocket> servers;
-	private Callback<String, String> callback;
+	private Callback<Void, String, String> callback;
 	
 	private Object jsEldis;
 	
 		
 		this.registeredRaws = new HashSet<String>();
 		this.servers = new HashMap<String, IRCSocket>();
-		this.commands = new HashMap<Trigger, Callback<String, String>>();
+		this.commands = new HashMap<Trigger, Callback<Boolean, String, String>>();
 		
 		// Calls back the JavaScript on raw input.
-		callback = new Callback<String, String>() {
+		callback = new Callback<Void, String, String>() {
 			@Override
-			public void call(String server, String raw) {
+			public Void call(String server, String raw) {
 				// PING :text
 				// :nick!user@host PRIVMSG #channel :text
 				// :nick!user@host PRIVMSG Eldflugan :text
 						System.out.println("--- '" + raw + "'");
 					}
 					
-					return;
+					return null;
 				}
 				
 				String rawCmd = args.get(1);
 						}
 					}
 					//*/
-					Callback<String, String> callback = null;
+					Callback<Boolean, String, String> callback = null;
 					Trigger trigger = null;
 					
 					for (Trigger trigger2 : commands.keySet()) {
 					}
 				} else if (registeredRaws.contains(rawCmd)) {
 					try {
-						invocable.invokeMethod(jsEldis, "setProperties",
-								server, "---", "---");
-						invocable.invokeMethod(jsEldis, "internalCallNumeric",
-								rawCmd, args.toArray(new String[] {}));
+						invocable.invokeMethod(jsEldis, "setProperties", server, "---", "---");
+						invocable.invokeMethod(jsEldis, "internalCallNumeric", rawCmd, args.toArray(new String[] {}));
 					} catch (NoSuchMethodException ex) {
 						ex.printStackTrace();
 					} catch (ScriptException ex) {
 				} else {
 					System.out.println("Unhandled: '" + rawCmd + "'");
 				}
+				
+				return null;
 			}
 		};
 		
 				registeredRaws.add(command);
 			}
 			@Override
-			public void registerTrigger(Trigger trigger, Callback<String, String> callback) {
+			public void registerTrigger(Trigger trigger, Callback<Boolean, String, String> callback) {
 				commands.put(trigger, callback);
 			}
 			
 					return new SuffixTrigger((String[])args[0]);
 				}
 				
-				throw new IllegalArgumentException("Invalid trigger type.");
+				throw new IllegalArgumentException("Invalid trigger type: '" + typeL + "'");
 			}
 			
 			@Override

File src/nu/firefly/eldis3/IRCSocket.java

 	private String nickname;
 	
 	public IRCSocket(final String serverName, String addr, int port, String nick,
-			final Callback<String, String> callback) throws IOException {
+			final Callback<Void, String, String> callback) throws IOException {
 		super(addr, port);
 		
 		this.in = new BufferedReader(new InputStreamReader(getInputStream()));

File src/nu/firefly/eldis3/triggers/PrefixTrigger.java

 		for (String prefix : this.prefixes) {
 			if (msg.startsWith(prefix)) return prefix;
 		}
-		throw new IllegalStateException();
+		throw new IllegalStateException("PrefixTrigger isn't associated with any prefixes; couldn't resolve identity.");
 	}
 }

File src/nu/firefly/eldis3/triggers/SuffixTrigger.java

 	@Override
 	public String getIdentity(String msg) {
 		for (String suffix : this.suffixes) {
-			if (msg.startsWith(suffix)) return suffix;
+			if (msg.endsWith(suffix)) return suffix;
 		}
-		throw new IllegalStateException();
+		throw new IllegalStateException("SuffixTrigger isn't associated with any suffixes; couldn't resolve identity.");
 	}
 }