Created
February 25, 2009 00:39
-
-
Save arikui/69913 to your computer and use it in GitHub Desktop.
Command Interpreter Protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); | |
var Ci = Components.interfaces; | |
var Cc = Components.classes; | |
var Command = { | |
evaluators: { | |
"-e": function(cmd, code, args){ | |
return new Command.Evaluator(cmd.length ? eval(cmd) : "", | |
code.length? eval(code) : "", | |
args.length? eval(args) : ""); | |
}, | |
"jscript": function(cmd, code, args){ | |
return new Command.Evaluator("cscript //E:JScript //Nologo", code, args); | |
}, | |
"vbs": function(cmd, code, args){ | |
return new Command.Evaluator("cscript //E:VBScript //Nologo", code, args); | |
}, | |
__noSuchMethod__: function(id, args){ | |
var r = /^-e\s+(.+)/; | |
if(r.test(args[0])) | |
return this["-e"](r.exec(args[0])[1], args[1], args[2]); | |
return new Command.Evaluator(args[0], args[1], args[2]); | |
// return this["-e"].apply(null, ['"' + i + '"' for each(i in args)]); | |
} | |
}, | |
evaluate: function(evaluator){ | |
var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess); | |
var bat = new TempFile("eval", "bat"); | |
var script = new TempFile("eval", "code"); | |
var out = new TempFile("eval", "out"); | |
script.write(evaluator.code); | |
evaluator.code.length? bat.write([evaluator.cmd, script.file.path, evaluator.args, ">", out.file.path].join(" ")) | |
: bat.write([evaluator.cmd, evaluator.args, ">", out.file.path].join(" ")); | |
process.init(bat.file); | |
process.run(true, [], 0); | |
var res = out.read(); | |
bat.remove(false); | |
out.remove(false); | |
if(script.exists) | |
script.remove(false); | |
return res; | |
} | |
}; | |
Command.Evaluator = function(cmd, code, args){ | |
this.cmd = cmd; | |
this.code = code || ""; | |
this.args = args || ""; | |
}; | |
function Protocol(){} | |
Protocol.prototype = { | |
classDescription: "Command Interpreter Protocol", | |
classID : Components.ID("{458AA54E-3898-4237-A3B9-86BF8943D6F3}"), | |
contractID : "@mozilla.org/network/protocol;1?name=cmd", | |
QueryInterface : XPCOMUtils.generateQI([Ci.nsIProtocolHandler]), | |
scheme : "cmd", | |
defaultPort : -1, | |
protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE | |
| Ci.nsIProtocolHandler.URI_NOAUTH | |
| Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD, | |
allowPort: function(port, scheme){ | |
return false; | |
}, | |
newURI: function(spec, charset, baseURI){ | |
var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI); | |
uri.spec = spec; | |
return uri; | |
}, | |
newChannel: function(aURI){ | |
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); | |
var comps = (new RegExp("^" + this.scheme + ":([^;]+);?([^;]*);?([^;]*)$")).exec(aURI.spec); | |
var cmd = decodeURIComponent(comps[1]); | |
var code = decodeURIComponent(comps[2]); | |
var args = decodeURIComponent(comps[3]); | |
var evalu = Command.evaluators[cmd](cmd, code, args); | |
var res = Command.evaluate(evalu); | |
log(res); | |
return ios.newChannel("data:text/plain;base64," + btoa(res) + "')", null, null); | |
} | |
} | |
function NSGetModule(compMgr, fileSpec){ | |
return XPCOMUtils.generateModule([Protocol]); | |
} | |
/// util | |
function Tombloo(){ | |
if("@brasil.to/tombloo-service;1" in Cc) | |
return Cc['@brasil.to/tombloo-service;1'].getService().wrappedJSObject; | |
return null; | |
} | |
function log(msg){ | |
var win = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator).getMostRecentWindow('navigator:browser'); | |
if(win.FirebugConsole && win.FirebugContext){ | |
var console = new win.FirebugConsole(win.FirebugContext, win.content); | |
console["log"].apply(console, arguments); | |
return true; | |
} | |
if(win.Firebug && win.Firebug.Console){ | |
win.Firebug.Console.logFormatted.call(win.Firebug.Console, Array.slice(arguments), win.FirebugContext, "log"); | |
return true; | |
} | |
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage('' + msg); | |
} | |
function TempFile(name, ext){ | |
var file = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile); | |
file.append([name, (new Date()).getTime(), "tmp", ext].join(".")); | |
this.file = file; | |
} | |
TempFile.prototype = { | |
read: function(){ | |
var istream = Cc["@mozilla.org/network/file-input-stream;1"] .createInstance(Ci.nsIFileInputStream); | |
istream.init(this.file, 0x01, 0444, 0); | |
istream.QueryInterface(Ci.nsILineInputStream); | |
var line = {}, lines = [], hasmore; | |
do{ | |
hasmore = istream.readLine(line); | |
lines.push(line.value); | |
} | |
while(hasmore) | |
istream.close(); | |
return lines.join("\n"); | |
}, | |
write: function(data){ | |
if(!data.length) | |
return false; | |
var foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream); | |
foStream.init(this.file, 0x02 | 0x08 | 0x20, 0664, -1); | |
foStream.write(data, data.length); | |
foStream.close(); | |
}, | |
remove: function(){ | |
this.file.remove(false); | |
this.file = null; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment