firefly avatar firefly committed e70e57e

Added scpsync: Utility thingy.

Comments (0)

Files changed (4)

js/exts/javascript.js

-(function(Local) {
-	function createEngine() {
-		if (!('manager' in Local)) Local.manager = new javax.script.ScriptEngineManager();
-		
-		var engine = Local.manager.getEngineByName('js');
-		engine.eval('delete java; delete javax; delete com; delete org; delete this.Packages');
-		engine.eval(new java.io.FileReader('js/lib/functional.js'));
-		engine.eval(new java.io.FileReader('js/lib/complex.js'));
-		engine.eval(new java.io.FileReader('js/jsCommand.js'));
-		
-		return engine;
+(function(Persistent, Local) {
+	Local.newPredefs = {};
+	if (!('predefs' in Persistent)) Persistent.predefs = {};
+	
+	function getEngine() {
+		if ('engine' in Local) {
+			return Local.engine;
+		} else {
+			if (!('manager' in Local)) Local.manager = new javax.script.ScriptEngineManager();
+			
+			var engine = Local.manager.getEngineByName('js');
+			engine.eval('delete java; delete javax; delete com; delete org; delete this.Packages');
+			engine.eval(new java.io.FileReader('js/lib/functional.js'));
+			engine.eval(new java.io.FileReader('js/lib/complex.js'));
+			engine.eval(new java.io.FileReader('js/jsCommand.js'));
+			
+			for (var name in Persistent.predefs) {
+				println("Test: " + name + " = " + Persistent.predefs[name]);
+				engine.eval(name + "=" + Persistent.predefs[name]);
+			}
+			
+			Local.engine = engine;
+			return engine;
+		}
 	}
 	
 	var cmdJavascript = {
 			var script = args.join(' ');
 			
 			if (args[0] && (args[0] == "--reset")) {
-				delete this.jsEngine;
+				delete Local.engine;
 				send("JavaScript context has been reset.");
 				return;
 			}
 			
-			//	delete this.jsEngine;
-			if ('engine' in Local) {
-				var engine = Local.engine;
-			} else {
-				var engine = createEngine();
-				Local.engine = engine;
-			}
-			
+			var engine = getEngine();
 			var func = function() {
 				try {
 					var out = '' + engine.eval(script);
 		}
 	};
 	
+	var cmdJsRegister = {
+		help: "Registers a variable in the current JavaScript context to the bot for future use.",
+		level: 1,
+		exec: function(self, args, flag, identifier) {
+			if (args.length == 0 || (args.length == 1 && flag.charAt(0) == '-')) {
+				send("Usage: " + self + " [flag] <identifier>");
+				return;
+			}
+			
+			if (flag.charAt(0) != '-') {
+				var identifier = flag;
+				var mode = "both";
+			} else {
+				var mode = ({
+					'-s': "script",
+					'--script': "script",
+					'-c': "command",
+					'--command': "command",
+					'-b': "both",
+					'--both': "both"
+				})[flag];
+			}
+			
+			if (!identifier.match(/^[A-Za-z_$][\w$]*$/)) {
+				send("Error: Must pass a valid identifier to " + self + ".");
+				return;
+			}
+			
+				/* Array → fixes primitives like number and string. */
+			var data = getEngine().eval(Eldis.utils.format('[%name]', {name: identifier}))[0];
+			/*
+			if (typeof(data) == 'object' && data !== null && data instanceof java.lang.Object) {
+				switch (data.getClass().toString()) {
+					case 'class java.lang.String':
+						data = Eldis.toRealString(data);
+						break;
+					case 'class java.lang.Double':
+						data = parseDouble(data);
+						break;
+				}
+			}
+			*/
+			
+			Local.newPredefs[identifier] = {
+				mode: mode,
+				predef: data
+			};
+			
+			send("Submitted '" + identifier + "'.");
+		}
+	};
+	
+	var cmdJsApprove = {
+		help: "Approve a javascript registration submission.",
+		level: 5,
+		exec: function(self, args, identifier) {
+			if (args.length < 1) {
+				send("Usage: " + self + " <identifier>");
+				return;
+			}
+			
+			var data = Local.newPredefs[identifier];
+			if (data.mode == 'both' || data.mode == 'script') {
+				Persistent.predefs[identifier] = data.predef;
+			}
+			
+			send("Approved '" + identifier + "'.");
+		}
+	};
+	
 	Eldis.triggers.command.register('js', cmdJavascript);
-})({});
+	Eldis.triggers.command.register('js-submit', cmdJsRegister);
+	Eldis.triggers.command.register('js-approve', cmdJsApprove);
+})(Eldis.getPersistent('javascript'), {});
 	servers: {},
 	files: {}
 };
-var Data={}, Session={};
+var Data={Persistent: {}}, Session={};
 var Global = this;
 
 	// Internal functions.
 };
 
 	// Utility functions used by commands.
+Eldis.getPersistent = function(name) {
+	if (!('Persistent' in Data)) Data.Persistent = {};
+	
+	if (name in Data.Persistent) {
+		return Data.Persistent[name];
+	} else {
+		return Data.Persistent[name] = {};
+	}
+};
 Eldis.read = function(file) {
 	var reader = new java.io.BufferedReader(new java.io.FileReader(file));
 	var out = [], buffer;
 };
 
 Eldis.ctcp = function(cmd, args, targetArg, serverArg) {
+//	var target = targetArg || Eldis.state.nick;
 	return Eldis.send('\001' + cmd.toUpperCase() + ' ' + args + '\001', targetArg, serverArg);
 };
 Eldis.ctcpReply = function(cmd, args, targetArg, serverArg) {
-	return Eldis.notice('\001' + cmd.toUpperCase() + ' ' + args + '\001', targetArg, serverArg);
+	var target = targetArg || Eldis.state.nick;
+	send("Test: " + Eldis.state.nick);
+	return Eldis.notice('\001' + cmd.toUpperCase() + ' ' + args + '\001', target, serverArg);
 };
 Eldis.action = function(msg, targetArg, serverArg) {
 	return Eldis.ctcp('ACTION', msg, targetArg, serverArg);
 };
 Eldis.raw = function(server, raw) {
 	EldisJava.raw(server, raw);
-}
+};
 
 Eldis.utils = {
 	Array: {
+#!/bin/sh
+# $ ./scpsync foo@example.com:/path/to/eldis3
+scp -r js bin stuff eldis3 version $@
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.