-
Notifications
You must be signed in to change notification settings - Fork 38
/
leash.min.js
9 lines (9 loc) · 45.5 KB
/
leash.min.js
1
2
3
4
5
6
7
8
9
/**@license
* This file is part of Leash (Browser Shell)
* Copyright (c) 2013-2023 Jakub Jankiewicz <http://jcubic.pl/me>
*
* Released under the MIT license
*
* Date: Tue, 30 May 2023 18:31:42 +0000
*/
function Uploader(e){this.token=e.terminal.token();this.leash=e}Uploader.prototype.upload_tree=function e(n,r){var i=$.Deferred();var t=this;r=r||t.leash.cwd;function a(r,t){r=r.slice();(function e(){var n=r.shift();if(n){t(n).then(e).fail(function(){i.reject()})}else{i.resolve()}})()}function o(e){a(e,function(e){return t.upload_tree(e,r+"/"+n.name)})}function s(e){t.upload(e,r).then(function(){i.resolve()}).fail(function(){i.reject()})}if(typeof Directory!="undefined"&&n instanceof Directory){n.getFilesAndDirectories().then(function(e){o(e)})}else if(typeof File!="undefined"&&n instanceof File){s(n)}else if(n.isFile){n.file(s)}else if(n.isDirectory){var c=n.createReader();c.readEntries(function(e){o(e)})}return i.promise()};Uploader.prototype.error=function(e){if(typeof e==="string"){this.leash.terminal.error(e)}else if(e.error){this.leash.terminal.error(e.error.message)}else{this.leash.terminal.error(e.message)}};Uploader.prototype.upload=function e(n,r){var t=this;var i=$.Deferred();var a=r+"/"+n.name;if(n.size>t.leash.settings.upload_max_filesize){if(!(n.slice||n.webkitSlice)){t.error("Exceeded filesize limit.");i.resolve();t.leash.animation.stop()}else{t.maybe_ask(a).then(function(){t.leash.animation.start(400);t.upload_by_chunks(n,r).then(function(){i.resolve();t.leash.animation.stop()}).fail(function(){i.reject();t.leash.animation.stop()})}).fail(function(){i.resolve();t.leash.animation.stop()})}}else{t.maybe_ask(a).then(function(){t.upload_file(n,r).then(function(){i.resolve();t.leash.animation.stop()}).fail(function(){i.reject();t.leash.animation.stop()})}).fail(function(){i.resolve();t.leash.animation.stop()})}return i.promise()};Uploader.prototype.upload_by_chunks=function e(i,a,o){var s=this;o=o||1048576;var c=$.Deferred();function f(e,n){if(i.slice){return i.slice(e,n)}else if(i.webkitSlice){return i.webkitSlice(e,n)}}var n=0;function u(e,n){if(e<i.size){var r=f(e,n);var t=new FormData;t.append("file",r,i.name);t.append("token",s.token);t.append("path",a);$.ajax({url:"lib/upload.php?append=1",type:"POST",success:function(e){if(e.error){s.error(e.error);c.reject()}else{u(n,n+o)}},error:function(e,n,r){s.error(e.statusText);c.reject()},data:t,cache:false,contentType:false,processData:false})}else{s.leash.terminal.echo('File "'+i.name+'" uploaded.');c.resolve()}}var r=a+"/"+i.name;this.leash.service.unlink(s.token,r)(function(e,n){if(e){s.error(e);c.reject()}else{u(0,o)}});return c.promise()};Uploader.prototype.maybe_ask=function e(t){var i=this;var a=$.Deferred();if(i.answer){a.resolve()}else{i.leash.service.file_exists(t)(function(e,n){if(n){var r='File "'+t+'" exis'+"ts do you want to overwrite it (Y/N/A)? ";i.leash.terminal.history().disable();i.leash.terminal.push(function(e){if(e.match(/^(y|n|a)$/i)){i.leash.terminal.pop().history().enable();if(e.match(/^a$/i)){a.resolve();i.answer=true}else if(e.match(/^y$/i)){a.resolve()}else if(e.match(/^n$/i)){a.reject()}}},{prompt:r})}else{a.resolve()}})}return a.promise()};Uploader.prototype.upload_file=function e(n,r){var t=this;var i=$.Deferred();var a=new FormData;a.append("file",n);a.append("token",t.token);a.append("path",r);$.ajax({url:"lib/upload.php",type:"POST",success:function(e){t.leash.animation.stop();if(e.error){t.error(e.error);i.reject()}else{t.leash.terminal.echo('File "'+n.name+'" '+"uploaded.");i.resolve()}},error:function(e,n,r){t.error(e.statusText);i.reject()},data:a,cache:false,contentType:false,processData:false});return i.promise()};var leash=function(){var _=$.omap({blue:"#55f",green:"#4d4",grey:"#999"},function(e,r){return function(e,n){return"[["+(n||"")+";"+r+";]"+e+"]"}});var y=["Copyright (c) 2013-2023 Jakub Jankiewicz <http://jcubic.pl/me>","","Licensed under MIT license"].map(function(e){return e===""?e:" "+e}).join("\n");function t(e,n,r){if(n>e.length){return t(r+e,n,r)}else{return e}}function n(e){var r=[];$.each(e,function(e,n){r.push(n);r.push(n.toLowerCase())});return r}function N(){var e=["ACCESSIBLE","ADD","ALL","ALTER","ANALYZE","AND","AS","ASC","ASENSITIVE","BEFORE","BETWEEN","BIGINT","BINARY","BLOB","BOTH","BY","CALL","CASCADE","CASE","CHANGE","CHAR","CHARACTER","CHECK","COLLATE","COLUMN","CONDITION","CONSTRAINT","CONTINUE","CONVERT","CREATE","CROSS","CURRENT_DATE","CURRENT_TIME","CURRENT_TIMESTAMP","CURRENT_USER","CURSOR","DATABASE","DATABASES","DAY_HOUR","DAY_MICROSECOND","DAY_MINUTE","DAY_SECOND","DEC","DECIMAL","DECLARE","DEFAULT","DELAYED","DELETE","DESC","DESCRIBE","DETERMINISTIC","DISTINCT","DISTINCTROW","DIV","DOUBLE","DROP","DUAL","EACH","ELSE","ELSEIF","ENCLOSED","ESCAPED","EXISTS","EXIT","EXPLAIN","FALSE","FETCH","FLOAT","FLOAT4","FLOAT8","FOR","FORCE","FOREIGN","FROM","FULLTEXT","GRANT","GROUP","HAVING","HIGH_PRIORITY","HOUR_MICROSECOND","HOUR_MINUTE","HOUR_SECOND","IF","IGNORE","IN","INDEX","INFILE","INNER","INOUT","INSENSITIVE","INSERT","INT","INT1","INT2","INT3","INT4","INT8","INTEGER","INTERVAL","INTO","IS","ITERATE","JOIN","KEY","KEYS","KILL","LEADING","LEAVE","LEFT","LIKE","LIMIT","LINEAR","LINES","LOAD","LOCALTIME","LOCALTIMESTAMP","LOCK","LONG","LONGBLOB","LONGTEXT","LOOP","LOW_PRIORITY","MASTER_SSL_VERIFY_SERVER_CERT","MATCH","MEDIUMBLOB","MEDIUMINT","MEDIUMTEXT","MIDDLEINT","MINUTE_MICROSECOND","MINUTE_SECOND","MOD","MODIFIES","NATURAL","NOT","NO_WRITE_TO_BINLOG","NULL","NUMERIC","ON","OPTIMIZE","OPTION","OPTIONALLY","OR","ORDER","OUT","OUTER","OUTFILE","PRECISION","PRIMARY","PROCEDURE","PURGE","RANGE","READ","READS","READ_WRITE","REAL","REFERENCES","REGEXP","RELEASE","RENAME","REPEAT","REPLACE","REQUIRE","RESTRICT","RETURN","REVOKE","RIGHT","RLIKE","SCHEMA","SCHEMAS","SECOND_MICROSECOND","SELECT","SENSITIVE","SEPARATOR","SET","SHOW","SMALLINT","SPATIAL","SPECIFIC","SQL","SQLEXCEPTION","SQLSTATE","SQLWARNING","SQL_BIG_RESULT","SQL_CALC_FOUND_ROWS","SQL_SMALL_RESULT","SSL","STARTING","STRAIGHT_JOIN","TABLE","TERMINATED","THEN","TINYBLOB","TINYINT","TINYTEXT","TO","TRAILING","TRIGGER","TRUE","UNDO","UNION","UNIQUE","UNLOCK","UNSIGNED","UPDATE","USAGE","USE","USING","UTC_DATE","UTC_TIME","UTC_TIMESTAMP","VALUES","VARBINARY","VARCHAR","VARCHARACTER","VARYING","WHEN","WHERE","WHILE","WITH","WRITE","XOR","YEAR_MONTH","ZEROFILL"];return n(e)}function S(){var e=["ABORT","ACTION","ADD","AFTER","ALL","ALTER","ANALYZE","AND","AS","ASC","ATTACH","AUTOINCREMENT","BEFORE","BEGIN","BETWEEN","BY","CASCADE","CASE","CAST","CHECK","COLLATE","COLUMN","COMMIT","CONFLICT","CONSTRAINT","CREATE","CROSS","CURRENT_DATE","CURRENT_TIME","CURRENT_TIMESTAMP","DATABASE","DEFAULT","DEFERRABLE","DEFERRED","DELETE","DESC","DETACH","DISTINCT","DROP","EACH","ELSE","END","ESCAPE","EXCEPT","EXCLUSIVE","EXISTS","EXPLAIN","FAIL","FOR","FOREIGN","FROM","FULL","GLOB","GROUP","HAVING","IF","IGNORE","IMMEDIATE","IN","INDEX","INDEXED","INITIALLY","INNER","INSERT","INSTEAD","INTERSECT","INTO","IS","ISNULL","JOIN","KEY","LEFT","LIKE","LIMIT","MATCH","NATURAL","NO","NOT","NOTNULL","NULL","OF","OFFSET","ON","OR","ORDER","OUTER","PLAN","PRAGMA","PRIMARY","QUERY","RAISE","RECURSIVE","REFERENCES","REGEXP","REINDEX","RELEASE","RENAME","REPLACE","RESTRICT","RIGHT","ROLLBACK","ROW","SAVEPOINT","SELECT","SET","TABLE","TEMP","TEMPORARY","THEN","TO","TRANSACTION","TRIGGER","UNION","UNIQUE","UPDATE","USING","VACUUM","VALUES","VIEW","VIRTUAL","WHEN","WHERE","WITH","WITHOUT"];return n(e)}function b(n,e,r){var t=new RegExp("^"+e.map($.terminal.escape_regex).join("|")+"$","i");return function(e){return e.split(/((?:\s| |\)|\()+)/).map(function(e){if(n.indexOf(e)!=-1){return"[[b;"+r+";]"+e+"]"}else if(e.match(t)){return"[[u;;]"+e+"]"}else{return e}}).join("")}}function O(t,i,n){function a(e,n){var r=$.terminal.escape_brackets("[AJAX] "+n+" server response\n"+e.responseText);t.error(r);k(t)}function o(e){if(typeof e=="string"){t.error(e)}else{if(e.message){t.echo(e.message)}if(e.error.traceback){t.echo(e.error.traceback)}}}function r(e,n,r){if(n===undefined){n=[]}C(t);$.jrpc(i,e,n,function(e){if(e.error){o(e.error)}else if(e.result){if(r===undefined||r){t.echo(e.result.replace(/\n$/,""))}}k(t)},a)}C(t);var s;$.jrpc(i,"start",[],function(e){if(e.error){o(e.error);k(t)}else if(e.result){s=e.result;t.echo("[[b;#F8B612;]Warring: each time you execute a c"+"ommand, python will execute all your previous c"+"ommands, so watch out on commands that can be e"+"xecuted only once]");$.jrpc(i,"info",[],function(e){if(e.error){o(e.error)}else{t.echo(e.result)}n({evaluate:function(e){r("evaluate",[s,e])},destroy:function(){r("destroy",[s])}});k(t)})}},a)}function L(e,n,r){if(r){r=$.terminal.escape_brackets(r)}var t=_.green(e+"@"+n);var i=_.grey(e==="root"?"# ":"$ ");return t+_.grey(":")+_.blue(r)+i}function C(e){e.pause(true).find(".prompt").hidden()}function k(e){e.resume().find(".prompt").visible()}function i(h,o,r,e){var c=$.extend({},{onDirectoryChange:$.noop},e);var n;var d;var t=$.terminal.defaults.formatters;var i=[t];var f=[];var u=[];function s(e){var r=e;$.each(A.env,function(e,n){r=r.replace("$"+e,n)});return r}function v(e,n){if(e){g(e)}else{switch($.type(n)){case"array":if(n.length){var r=Object.keys(n[0]);n=[r].concat(n.map(function(n){if(n instanceof Array){return n.map(function(e){return $.terminal.escape_brackets(String(e))})}else{return Object.keys(n).map(function(e){return $.terminal.escape_brackets(String(n[e]))})}}));A.terminal.echo(m(n,true),{formatters:false})}break;case"number":A.terminal.echo("Query OK, "+n+" row affected")}}k(A.terminal)}function g(e){if(e.error){A.terminal.error(e.error.message);A.terminal.error("in "+e.error.file+" at line "+e.error.at);A.terminal.error(e.error.line)}else{A.terminal.error(e.message)}}function m(t,e){if(!t.length){return""}for(var n=t.length-1;n>=0;n--){var r=t[n];var i=[];for(var a=0;a<r.length;a++){var o=r[a].toString().split("\n");r[a]=o.shift();i.push(o)}var s=Math.max.apply(Math,i.map(function(e){return e.length}));for(var c=s-1;c>=0;c--){t.splice(n+1,0,i.map(function(e){return e[c]||""}))}}var f=t[0].map(function(e,r){var n=t.map(function(e){if(e[r]!=undefined){var n=wcwidth(e[r]);if(e[r].match(/\t/g)){n+=e[r].match(/\t/g).length*3}return n}else{return 0}});return Math.max.apply(Math,n)});t=t.map(function(e){return"| "+e.map(function(e,n){var r=wcwidth(e);if(e.match(/\t/g)){r+=e.match(/\t/g).length*3}if(r<f[n]){e+=new Array(f[n]-r+1).join(" ")}return e}).join(" | ")+" |"});t=t.map(function(e){return e.replace(/&/g,"&")});var u="+"+f.map(function(e){return new Array(e+3).join("-")}).join("+")+"+";if(e){return u+"\n"+t[0]+"\n"+u+"\n"+t.slice(1).join("\n")+"\n"+u}else{return u+"\n"+t.join("\n")+"\n"+u}}var l=[];var a=[];function E(e,n){var r=A.terminal;var t=r.get_token();var i={columns:r.cols()};return h.shell(t,e,A.cwd,i)}function p(i){return function(e,n,r){var t=i+" "+e.args[0];C(r);E(t,A.cwd)(function(e,n){if(e){g(e)}else{A.less($.terminal.escape_brackets(n.output))}k(r)})}}function T(){var n=$.Callbacks();var r=false;this.resolve=function(){n.fire();r=true};this.add=function(e){if(r){e()}else{n.add(e)}}}function R(n){return function(e){n.add(e)}}var w=new T;var I=R(w);var A={formatters:t,version:"1.20.0",date:"Tue, 30 May 2023 18:31:42 +0000",jargon:[],dirs:{},env:{},change_directory:function(e,n){I(function(){A.cwd=e;c.onDirectoryChange(A.cwd);A.refresh_dir(n)})},banner:function(){var e="";if(!A.version.match(/\{{2}VERSION\}{2}/)){e=" v. "+A.version}var n="";if(!A.date.match(/\{{2}DATE\}{2}/)){n="build at: "+A.date}var r=[" __ _______ ______ __"," / / / __/ _ | / __/ // /"," / /__/ _// __ |_\\ \\/ _ /","/____/___/_/ |_/___/_//_/"+e];if(n){r.push(n)}r.push("");return r.join("\n")},animation:{animating:false,start:function(n){var r=["/","-","\\","|"],t=0;this.animating=true;this.prompt=A.terminal.get_prompt();var i=this;A.terminal.pause(true);(function e(){A.terminal.set_prompt(r[t++]);if(t>r.length-1){t=0}i.timer=setTimeout(e,n)})()},stop:function(){if(this.animating){clearTimeout(this.timer);A.terminal.set_prompt(this.prompt);this.animating=false}k(A.terminal)}},service:h,init:function(r){r.on("click",".jargon",function(){var e="jargon "+$(this).data("text").replace(/\s/g," ");r.exec(e).then(function(){if(r.settings().historyState){r.save_state(e)}})}).on("click",".exec",function(){var e=$(this).data("text");r.exec(e).then(function(){if(r.settings().historyState){r.save_state(e)}})}).on("click",".wiki",function(){var e=$(this).data("text").replace(/\s/g," ");var n=$.terminal.split_command("wikipedia "+e);A.commands.wikipedia(n,r.token(),r)}).on("click",".rfc",function(){var e=$(this).data("text");var n=$.terminal.split_command("rfc "+e);A.commands.rfc(n,r.token(),r)}).on("click","a",function(e){if($(this).parents().is(".exception")){return}if(!e.ctrlKey){var n=r.token();var t=$(this).attr("href").trim();if(t.match(/^mailto:/)){return}A.less(function(e,r){A.service.html(n,t,e-1)(function(e,n){if(!e){r(n.trim().split("\n"))}})});return false}});if(!A.installed){A.install(r)}else{A.config(r)}},interpreter:function(e,n){if(!f.length){n.error("Not interpeter")}else{f[f.length-1](e,n)}},make_interpreter:function(t){return function(e,n){if(!A.installed){n.error("Invalid command, you need to refresh the p"+"age")}else{if($.terminal.unclosed_strings(e)){A.shell(e,t,n)}else{var r=$.terminal.parse_command(e);if(A.commands[r.name]){A.commands[r.name](r,t,n)}else if(e!==""){A.shell(e,t,n)}}}}},install:function(c){var f={};var e=[{name:"root_password",prompt:"root password: ",mask:true},{name:"server",text:"Type your server name"},{name:"username",text:"Your normal username"},{name:"home",text:"Home directory"},{name:"guest",text:"Allow guest sessions (Y)es/(N)o",boolean:true},{name:"sudo",text:"Execute sudo for user accounts (Y)es/(N)o",boolean:true},{name:"password",mask:true}];c.echo("[[;#C78100;]You are running Leash for the first"+" time. You need to configure it]\n");c.history().disable();(function r(t,i){var a=e[t];if(a){if(a.text){c.echo("[[b;#fff;]"+a.text+"]")}c.push(function(e){if(a.mask){c.set_mask(false)}if(a.boolean){var n;if(e.match(/^Y(es)?/i)){n=true}else if(e.match(/^N(o)?/i)){n=false}if(typeof n!="undefined"){f[a.name]=n;c.pop();r(t+1,i)}}else{f[a.name]=e;c.pop();r(t+1,i)}},{prompt:a.prompt||a.name+": "});if(a.mask){c.set_mask(true)}}else{i()}})(0,function(){C(c);var o=$.terminal.ansi_colors.bold;function s(e,r){if(e.length){var t=e[0];var i=e.slice(1);var a="Test Shell '"+t+"' ";h.test_shell(null,t)(function(e,n){if(n){a+="[[[b;"+o.green+";]PASS]]"}else{a+="[[[b;"+o.red+";]FAIL]]"}c.echo(a);if(n){c.echo("Using shell "+t);f.shell=t;r()}else{s(i,r)}})}else{c.error("Not valid shell found");c.error("You will not able to use Leash ful"+"ly");f.shell=null;r()}}c.echo("Detect Shell");h.list_shells(null)(function(e,n){s(n,function(){h.configure(f,"/")(function(e){k(c);if(e){c.error(e.message);A.install(c)}else{c.echo("Your instalation is complete no"+"w you can refresh the page and "+"login")}})})})})},config:function(t){var i=t.token();if(i){C(t);h.valid_token(i)(function(e,n){if(!n){t.set_token(undefined);t.logout();k(t)}else{A.token=i;A.env.TOKEN=i;f.push(A.make_interpreter(i));h.jargon_list()(function(e,n){if(!e){A.jargon=n}});h.get_settings(i)(function(e,n){if(e){g(e);k(t);return}A.settings=d=n;A.cwd=d.home;A.home=d.home;A.server=d.server;w.resolve();c.onDirectoryChange(A.cwd);if(n.show_messages!==false){var r=n.messages||[];t.echo(r.map(function(e){return"[[;#ff0;]"+e+"]"}).join("\n"))}h.dir(i,A.cwd)(function(e,n){A.dir=n;t.set_prompt(A.prompt);setTimeout(function(){k(t)},100)});if(d.purgeOnUnload){$(window).unload(function(){t.purge()})}})}})}},refresh_dir:function(r){h.dir(A.token,A.cwd)(function(e,n){A.dir=n;if($.isFunction(r)){r()}})},shell:function(e,n,i){var r=/\|\s*less\s*$/;var a=$.Deferred();e=s(e);C(i);if(e.match(r)){e=e.replace(r,"");E(e,A.cwd)(function(e,n){if(e){g(e)}else{A.less(n.output)}k(i);a.resolve()})}else{E(e,A.cwd)(function(e,n){if(e){g(e);k(i)}else{if(n.output){var r=/\n(\x1b\[m)?$/;var t=n.output.replace(r,"").replace(/\[\[/g,"[[").replace(/\]\]/g,"]]");i.echo(t)}if(A.cwd!==n.cwd){c.onDirectoryChange(n.cwd)}A.cwd=n.cwd;A.refresh_dir(function(){k(i);a.resolve()})}})}return a.promise()},option:function(e,n){if(typeof n==="undefined"){return c[e]}else{c[e]=n}},wikipedia:function(n,i){function r(e){if(e.length==1){return e[0]}else if(e.length==2){return e.join(" and ")}else if(e.length>2){return e.slice(0,e.length-1).join(", ")+" and "+e[e.length-1]}}function t(e){return r(e.split("|").map(function(e){if(e.match(/^\s*.*\s*=/)){return""}else{return"[[bu;#fff;;wiki;]"+e+"]"}}).filter(function(e){return!!e}))}function a(e,r,n){var t=/\[\[([^\]]+)\]\]/;if(e.match()){return e.split(/(\[\[[^\]]+\]\])/).map(function(e){var n=e.match(t);if(n){return"[[bu;"+r+";;wiki]"+n[1]+"]"}else{return"[[;"+r+";]"+e+"]"}}).join("")}else{return"[[;"+r+";]"+(e||n)+"]"}}var o={main:function(e){return"Main Article: "+t(e)+"\n"},dunno:function(){return"?"},yes:function(e){return a(e,"#0f0","yes")},no:function(e){return a(e,"#f00","no")},partial:function(e){return a(e,"#ff0","partial")},"lang-ar":function(e){return"[[bu;#fff;;;Arabic_language]Arabic]: "+e},"(?:IPAc-en|IPA-en)":function(e){if(!e.match(/\|/)){return"English pronunciation: /"+e+"/"}var t={tS:"tʃ",dZ:"dʒ","J\\":"ɟ","p\\":"ɸ",B:"β",T:"θ",D:"ð",S:"ʃ",Z:"ʒ",C:"ç","j\\ (jj)":"ʝ",G:"ɣ","X\\":"ħ","?\\":"ʕ","h\\":"ɦ",F:"ɱ",J:"ɲ",N:"ŋ","4 (r)":"ɾ","r (rr)":"r","r\\":"ɹ",R:"ʀ",P:"ʋ",H:"ɥ",I:"ɪ",E:"ɛ","{":"æ",2:"ø",9:"œ",1:"i","@":"ə",6:"ɐ",3:"ɜ","}":"ʉ",8:"ɵ","&":"ɶ",M:"ɯ",7:"ɤ",V:"ʌ",A:"ɑ",U:"ʊ",O:"ɔ",Q:"ɒ",",":"ˌ","'":"ˈ",_:"ː"};var i={};var n="/"+e.split("|").map(function(n){if(!n.match(/^\s*-\s*$|^\s*en-us/)){var e=/^\s*(us|lang|pron|audio)\s*=?\s*(.*)/i;var r=n.match(e);if(r){i[r[1].toLowerCase().trim()]=r[2]||true}else{Object.keys(t).forEach(function(e){n=n.replace(e,t[e])});return n}}}).filter(Boolean).join("")+"/";n="[[bu;#fff;;wiki;Help:IPA for English]"+n+"]";if(i.pron){n="pronunciation: "+n}if(i.lang){n="English "+n}if(i.us){n="US "+n}return n},"quote box":function(e){var n=e.match(/\s*quote\s*=\s*("[^"]+"|[^|]+)/)[1];var r=/'''([^']+(?:'[^']+)*)'''/g;n=n.replace(r,function(e,n){return"][[bi;#fff;]"+n+"][[i;;]"}).replace(/''([^']+(?:'[^']+)*)''/g,"$1").replace(/\[\[([^\]]+)\]\]/g,function(e,n){n=n.split("|");if(n.length==1){return"][[bui;#fff;;wiki]"+n[0]+"][[i;;]"}else{return"][[bui;#fff;;wiki;"+n[0]+"]"+n[1]+"][[i;;]"}});var t=e.match(/\s*source\s*=\s*([^|]+)/)[1].replace(/^(—|-)/,"").trim();return"[[i;;]"+n+"]\n-- "+t},quote:function(e){e=e.replace(/^\s*\|/gm,"").split(/\|(?![^\]]+\])/);var r={};e=e.map(function(e){var n=e.match(/\s*(\w+)\s*=\s*(.*)/);if(n){if(!isNaN(+n[1])){return n[2]}else{r[n[1].toLowerCase()]=n[2];return""}}else{return e}}).join("");var n="";if(r.author){n=r.author}else if(e.match(/^ /m)){n=e.match(/^ (.*)/m)[1]}return"[[i;;]"+e.replace(/'''([^']+(?:'[^']+)*)'''/g,function(e,n){return"][[bi;#fff;]"+n+"][[i;;]"}).replace(/''([^']+(?:'[^']+)*)''/g,"$1").replace(/\[\[([^\]]+)\]\]/g,function(e,n){n=n.split("|");if(n.length==1){return"][[bui;#fff;;wiki]"+n+"][[i;;]"}else{return"][[bui;#fff;;wiki;"+n[0]+"]"+n[1]+"][[i;;]"}})+"]"+(n?"\n-- "+n:"")},"Cat main":function(e){return"The main article for this [[bu;#fff;;wiki"+";Help:category]Category] is [[bu;#fff;;wiki]"+e+"]\n"},"see also":function(e){return"See also "+t(e)+"\n"},tag:function(e){return s("<"+e+">...</"+e+">")},"(?:official website|official)":function(e){if(!e.match(/^http:/)){e="http://"+e}return"[[!;;;;"+e+"]Official Website]"},"IMDb name":function(e){if(i){var n=e.match(/id\s*=\s*([^|]+)/);var r;if(n){r=n[1]}else{r=e}var t="http://www.imdb.com/name/nm"+r;return"[[!;;;;"+t+"]"+i+"] "+"at the [[Internet Movie Database]]"}},"(?:tlx|tl)":function(e){e=e.split("|");var n="";if(e.length>1){n="|"+e.slice(1).join("|")}return s("{{")+"[[bu;#fff;;wiki;Template:"+e[0]+"]"+e[0]+"]"+n+s("}}")},"(?:as of|Asof)":function(e){e=e.split("|");var n=["January","February","March","April","May","June","July","August","September","October","November","December"];var r=[];var t={};for(var i=0;i<e.length;++i){var a=e[i].match(/(\w+)\s*=\s*(\w+)/);if(a){t[a[1].toLowerCase()]=a[2]}else{r.push(e[i])}}var o="As of ";if(t.since){o="Since "}if(t.lc=="y"){o=o.toLowerCase()}if(t.df&&t.df.toLowerCase()=="us"){return o+(r[1]?n[r[1]-1]+" ":"")+(r[2]?r[2]+", ":"")+r[0]}else{return o+(r[2]?r[2]+" ":"")+(r[1]?n[r[1]-1]+" ":"")+r[0]}}};function s(n){var r={"{":"{","}":"}","[":"[","]":"]","<":"<",">":">","'":"'"};Object.keys(r).forEach(function(e){n=n.replace(new RegExp("\\"+e,"g"),r[e])});return n}n=n.replace(/ /g," ").replace(/^\s*;\s*([^:]+):\s*/gm,function(e,n){return"\n"+n+"\n\n"}).replace(/&/g,"&").replace(/^\s*(=+)\s*([^=]+)\s*\1/gm,function(e,e,n){n=n.replace(/''([^']+)''/g,function(e,n){return"][[bi;#fff;]"+n+"][[b;#fff;]"});return"\n[[b;#fff;]"+n+"]\n"}).replace(/\[\.\.\.\]/g,"...").replace(/<code><pre>(.*?)<\/pre><\/code>/g,function(e,n){return s(n)}).replace(/\[\[(?=<nowki\s*\/>)/,function(e){return s(e)}).replace(/{{Cite([^}]+)}}(?![\s\n]*<\/ref>)/gi,function(e,n){var r=n.match(/title\s*=\s*([^|]+)/i);var t=n.match(/url\s*=\s*([^|]+)/i);if(r){if(t){return"[[!;;;;"+t[1].trim()+"]"+r[1].trim()+"]"}else{return r[1].trim()}}else{return""}}).replace(/<nowiki>([\s\S]*?)<\/nowiki>/g,function(e,n){return s(n)});var e=[/<ref[^>]*\/>/g,/<ref[^>]*>[\s\S]*?<\/ref>/g,/\[\[(file|image):[^[\]]*(?:\[\[[^[\]]*]][^[\]]*)*]]/gi,/<!--[\s\S]*?-->/g,/<gallery>[\s\S]*?<\/galery>/g];e.forEach(function(e){n=n.replace(e,"")});var c;for(var f in o){c=new RegExp("{{"+f+"\\|?(.*?)}}","gi");n=n.replace(c,function(e,n){return o[f](n)||""})}c=/{{[^{}]*(?:{(?!{)[^{}]*|}(?!})[^{}]*)*}}/g;do{var u=0;n=n.replace(c,function(e){u++;return""})}while(u);var l=/\[\[([!gbiuso]*);([^;]*)(;[^\]]*\])/i;function p(i,a){var r=/(\[\[[!gbiuso]*;[^;]*;[^\]]*\](?:[^\]]*\\\][^\]]*|[^\]]*|[^\[]*\[[^\]]*)\]?)/i;return function(e,n){return n.split(r).map(function(e){function n(e,n,r,t){return"[["+i+n+";"+(a||r)+t}if($.terminal.is_formatting(e)){return e.replace(l,n)}else{return"[["+i+";"+(a||"")+";]"+e+"]"}}).join("")}}n=n.replace(/\[\[([^\]]+)\]\]/g,function(e,r){if(e.match(l)){return e}if(e.match(/<nowiki[^>]*>/)){return $.terminal.escape_brackets(e)}r=r.replace(/^:(Category)/i,"$1").split("|");var n;if(r.length==1){n="[[bu;#fff;;wiki]"+r[0]+"]"}else{r[1]=r[1].replace(/''([^']+)''/gm,function(e,n){return"][[bui;#fff;;wiki;"+r[0]+"]"+n+"]"+"[[bu;#fff;;wiki;"+r[0]+"]"});n="[[bu;#fff;;wiki;"+r[0]+"]"+r[1]+"]"}return n}).replace(/'''([^']+(?:'[^']+)*)'''/g,p("b","#fff")).replace(/^(\n\s*)*/,"").replace(/([^[])\[(\s*(?:http|ftp)[^\[ ]+) ([^\]]+)\]/g,function(e,n,r,t){function i(e,n){return"][[!i;;;;"+r+"]"+n+"][[!;;;;"+r+"]"}t=t.replace(/'''([^']*(?:'[^']+)*)'''/g,"$1").replace(/''([^']*(?:'[^']+)*)''/g,i);return n+"[[!;;;;"+r+"]"+t+"]"}).replace(/<blockquote>([\s\S]*?)<\/blockquote>/g,p("i")).replace(/''([^']+(?:'[^']+)*)''/g,p("i")).replace(/{\|.*\n([\s\S]*?)\|}/g,function(e,n){var r=/\|\+(.*)\n/;var t;var i=n.match(r);if(i){var a=i[1].trim().replace(/\[\[([^;]+)(;[^\]]+\][^\]]+\])/g,function(e,n,r){return"][["+n+"i"+r+"[[i;;]"})}n=n.replace(/^.*\n/,"").replace(r,"").split(/\|\-.*\n/);if(n.length==1){t=false;n=n[0].replace(/[\n\s]*$/,"").split(/\n/).map(function(e){return[e]})}else{if(n[0].match(/^!|\n!/)){t=true}n=n.map(function(e){var n=/^[|!]|\n[|!]|\|\|/;if(e.match(n)){return e.split(n).map(function(e){return e.replace(/\n/g,"").trim()}).filter(function(e,n){return n!==0})}else{return[]}}).filter(function(e){return e.length})}var o="";if(a){o="\n[[i;;]"+a+"\n"}o+=m(n,t);return o}).replace(/#(REDIRECT)/i,"#$1").replace(/(^\*.*(\n|$))+/gm,function(e){return"\n"+e}).replace(/(^#.*(\n|$))+/gm,function(r){r=r.split(/^#\s*/m).slice(1);return"\n"+r.map(function(e,n){return(r.length>9&&n<9?" ":"")+(n+1)+". "+e}).join("")+"\n"}).split(/(<pre[^>]*>[\s\S]*?<\/pre>)/).map(function(e,n){var r=e.match(/<pre[^>]*>([\s\S]*?)<\/pre>/);var t=/([^\n])\n(?![\n*|+]|\s*[0-9]|:|--|\[\[bu;#fff;;wiki\]Category)/gi;if(r){return r[1]}else{return e.replace(t,"$1 ").replace(/ +/g," ")}}).join("").replace(/<[^>]+>/gm,"").replace(/\n{3,}/g,"\n\n").replace(/\*(\S)/g,"* $1");return n},less:function(e,n){var d=A.terminal;var r=d.export_view();var t,i;var v=0;var g;var E;var T;var a=0;function R(){d.clear();if(E.length-v>i-1){T=":"}else{T="[[;;;inverted](END)]"}d.set_prompt(T);var e=E.slice(v,v+i-1);var n=/^(\[\[[!gbiuso]*;[^;]*;[^\]]*\])/i;e=e.map(function(e,n){return $.terminal.substring(e,a,a+t-1)});if(e.length<i-1){while(i-1>e.length){e.push("~")}}d.echo(e.join("\n"))}function o(){d.pop().import_view(r);if($.isFunction(n)){n()}}function s(){t=d.cols();i=d.rows();if($.isFunction(e)){e(t,function(e){g=e;E=g.slice();R()})}else{g=e.split("\n");E=g.slice();R()}}s();var c=3;var f=false,u,w;function l(e,n){var r=$.terminal.escape_brackets(w),t=w.toLowerCase()==w?"i":"",i=new RegExp("^("+r+")",t),a=new RegExp(r,t),o=-1,s="",c=false,f=false;E=g.slice();if(n){o=v=0}for(var u=0;u<E.length;++u){var l=E[u];for(var p=0,m=l.length;p<m;++p){if(l[p]==="["&&l[p+1]==="["){c=true;f=false;e=p}else if(c&&l[p]==="]"){if(f){c=false;f=false}else{f=true;s=l.substring(e,p+1)}}else if(c&&f||!c){if(l.substring(p).match(i)){var h;if(c&&f){h="][[;;;inverted]$1]"+s}else{h="[[;;;inverted]$1]"}l=l.substring(0,p)+l.substring(p).replace(i,h);p+=h.length-2;if(u>v&&o===-1){o=v=u}}}}E[u]=l}R();d.set_command("");d.set_prompt(T);return o}d.push($.noop,{resize:s,mousewheel:function(e,n){if(n>0){v-=c;if(v<0){v=0}}else{v+=c;if(v-1>E.length-i){v=E.length-i+1}}R();return false},name:"less",keydown:function(e){var n=d.get_command();if(d.get_prompt()!=="/"){if(e.which==191){d.set_prompt("/")}else if(f&&$.inArray(e.which,[78,80])!=-1){if(e.which==78){if(u!=-1){u=l(u+1)}}else if(e.which==80){u=l(0,true)}}else if(e.which==81){o()}else if(e.which==39){a+=Math.round(t/2);R()}else if(e.which==37){a-=Math.round(t/2);if(a<0){a=0}R()}else{if(E.length>i){if(e.which===38){if(v>0){--v;R()}}else if(e.which===40){if(v<=E.length-i){++v;R()}}else if(e.which===34){v+=i;if(v>E.length-i+1){v=E.length-i+1}R()}else if(e.which===33){v-=i;if(v<0){v=0}R()}}}if(!e.ctrlKey&&!e.alKey){return false}}else{if(e.which===8&&n===""){d.set_prompt(T)}else if(e.which==13){if(n.length>0){f=true;v=0;w=n;u=l(0)}return false}}},prompt:T})},completion:function(n,i){var e=this.get_command();var r=this.login_name();var t;if(r=="guest"){t=A.settings.guest_commands}else{t=(A.dir.execs||[]).concat(d.executables)}function a(e){return(e.dirs||[]).map(function(e){return e+"/"})}function o(e){if(n.match(/^"/)){return e.map(function(e){return'"'+e})}else{return e.map(function(e){return e.replace(/([() ])/g,"\\$1")})}}var s=$.terminal.parse_command(e);var c=new RegExp("^\\s*"+$.terminal.escape_regex(n));var f=A.token;if(n.match(/^\$/)){E("env","/")(function(e,n){i(n.output.split("\n").map(function(e){return"$"+e.split(/=/)[0]}))})}else if(e.match(c)||e===""){var u=Object.keys(A.commands);i(u.concat(t))}else{var l=n.match(/(.*)\/([^\/]+)/);var p;if(s.name=="cd"){if(l){p=A.cwd+"/"+l[1];h.dir(f,p)(function(e,n){var r=(n.dirs||[]).map(function(e){return l[1]+"/"+e+"/"});i(r)})}else{i(a(A.dir))}}else if(s.name=="jargon"){i(A.jargon)}else{if(l){p=A.cwd+"/"+l[1];h.dir(f,p)(function(e,n){var r=a(n);var t=(n.files||[]).concat(r).map(function(e){return l[1]+"/"+e});i(t)})}else{var m=(A.dir.files||[]).concat(a(A.dir));i(m)}}}},onPush:function(e,n){var r=A.formatters.slice().concat(n.formatters||[]);$.terminal.defaults.formatters=r;i.push(r)},onPop:function(e,n){i.pop();if(i.length>0){var r=i[i.length-1];$.terminal.defaults.formatters=r}},execRead:function(r,t,e){var i=$.extend({history:true,context:null,token:false},e);var a=A.terminal;var n=a.history();if(!i.history){n.disable()}var o=[];if(i.token){o.push(a.token())}(function n(){var e=r.shift();if(e){if(typeof e==="object"){if(e.mask===true){a.set_mask(true)}prompt=e.prompt}else{prompt=e}a.read(prompt,function(e){o.push(e);a.set_mask(false);n()})}else{t.apply(i.context,o)}})()},commands:{github:function(e,n,t){var r=new optparse.OptionParser([["-u","--username USER","owner of the repo"],["-r","--repo REPO","repo to open"]]);r.banner="Usage: github [options]";var i,a,o="master";r.on("username",function(e,n){i=n});r.on("repo",function(e,n){a=n});r.parse(e.args);var s="https://api.github.com/repos";var c;var f;function u(e,n){var r=s+"/"+i+"/"+a+"/contents/"+e;$.ajax({url:r,type:"GET",success:function(e){n({dirs:e.filter(function(e){return e.type=="dir"}),files:e.filter(function(e){return e.type=="file"})})},error:function(e,n,r){t.error(n+" "+r);k(t)}})}function l(e,n){var r="https://raw.githubusercontent.com/"+i+"/"+a+"/master/"+e;$.ajax({url:r,type:"GET",success:n,error:function(e){t.error("file not found");k(t)}})}function p(e){t.echo(e.dirs.map(function(e){return _.blue(e.name)}).concat(e.files.map(function(e){return e.name})).join("\n"))}function m(e,n){C(t);l(h+e,function(e){n(e);k(t)})}if(i&&a){var h="/";f=$.Deferred();u("",function(e){c=e;f.resolve()});t.push(function(e){var n=$.terminal.parse_command(e);if(n.name=="cd"){var r=n.args[0];if(n.args[0]==".."){r=h.replace(/[^\/]+\/$/,"")}else{r=(h=="/"?"":h)+n.args[0]}C(t);f=$.Deferred();u(r,function(e){c=e;h=r;if(!h.match(/\/$/)){h+="/"}k(t);f.resolve()})}else if(n.name=="less"){m(n.args[0],A.less)}else if(n.name=="cat"){m(n.args[0],t.echo)}else if(n.name=="ls"){if(n.args==0){p(c)}else{C(t);u(n.args[0],function(e){p(e);k(t)})}}else{t.echo("unknown command "+n.name)}},{prompt:function(e){var n=_.green(i+"@"+a);var r=h;if(r!="/"){r="/"+r.replace(/\/$/,"")}e(n+_.grey(":")+_.blue(r)+"$ ")},name:"github",completion:function(e,n){var r=$.terminal.parse_command(this.get_command());var t=e.match(/(.*)\/([^\/]+)/);if(t){u(t[1],function(e){if(r.name=="cd"){n(e.dirs.map(function(e){return t[1]+"/"+e.name+"/"}))}else{n(e.files.map(function(e){return t[1]+"/"+e.name+"/"}).concat(e.dirs.map(function(e){return t[1]+"/"+e.name})))}})}else{f.then(function(){if(r.name=="cd"){n(c.dirs.map(function(e){return e.name+"/"}))}else{n(c.files.map(function(e){return e.name}).concat(c.dirs.map(function(e){return e.name+"/"})))}})}}})}else{t.echo(r)}},download:function(e,n,r){if(e.args.length==1){var t=A.cwd+"/"+e.args[0];var i=$("<iframe/>").hide();var a=+new Date;var o=$.param({filename:t,token:n,v:a});var s=setInterval(function(){if(i[0].contentDocument&&i[0].contentDocument.readyState=="complete"){i.remove();clearInterval(s)}},500);i.attr("src","lib/download.php?"+o);i.appendTo("body")}else{r.echo("usage: download {FILENAME}")}},pushd:function(e,n,r){if(e.args.length==1){var t=A.cwd;if(a.length==0){a.push(t)}A.shell("cd "+e.args[0],n,r).then(function(){a.push(A.cwd);r.echo(a.slice().reverse().join(" "))})}else{r.echo("usage: pushd {DIRECTORY}")}},popd:function(e,n,r){if(a.length>1){a.pop();var t=a[a.length-1];A.shell("cd "+t,n,r).then(function(){r.echo(a.slice().reverse().join(" "))})}else{r.echo("popd: directory stack empty");a=[]}},update:function(e,n,r){A.animation.start(400);A.service.update(n)(function(e,n){if(e){g(e)}else if(n){r.echo("[[;#ff0;]Leash updated, you can now refresh "+"the browser]")}else{r.error("No new version available")}A.animation.stop()})},rfc:function(e,n,r){var t=e.args.length?e.args[0]:null;C(r);A.service.rfc(t)(function(e,n){if(e){g(e)}else{A.less(n.replace(/^[\s\n]+|[\s\n]+$/g,""))}k(r)})},cat:function(e,n,r){if(e.command.match(/cat\s*$/)){r.push(function(e){r.echo(e)},{prompt:""})}else if(e.command.match(/cat\s*>+\s*\w+/)){var t="";var i=e.args[e.args.length-1];if(!i.match(/^\//)){i=A.cwd+"/"+i}r.push(function(e){t+=e+"\n"},{prompt:"",onExit:function(){if(e.args[0].match(/>>/)){h.append(n,i,t)(function(e,n){if(!n){r.error("Can't save file")}})}else{h.write(n,i,t)(function(e,n){if(!n){r.error("Can't save file")}})}},completion:[]})}else{A.shell(e.command,n,r)}},copyright:function(e,n,r){r.echo(y)},bzless:p("bzcat"),zless:p("zcat"),xzless:p("xzcat"),less:p("cat"),record:function(e,n,r){if(e.args[0]=="start"){r.history_state(true)}else if(e.args[0]=="stop"){r.history_state(false)}else{r.echo("usage: record [stop|start]")}},timer:function(e,n,r){function t(){r.echo("usage: timer time [command]\ntime - number [smh]")}if(e.args.length>1){var i=e.args[0];var a=i.match(/^([0-9.]+)([smh])$/);if(a){var o=e.rest.trim().replace(/^[0-9.]+[smh]?/,"");i=parseFloat(a[1]);switch(a[2]){case"h":i*=24;case"m":i*=60;case"s":i*=1e3}C(r);setTimeout(function(){A.interpreter(o,r)},i)}else{t()}}else{t()}},passwd:function(e,t,i){i.set_mask(true).history().disable();i.push(function(e){i.push(function(r){C(i);h.valid_password(t,e)(function(e,n){if(n){h.change_password(t,r)(function(e){if(!e){i.echo("Password successfully changed")}else{i.error(e.message)}i.pop().pop();k(i);i.set_mask(false).history().enable()})}else{i.error("Current password is not valid");i.pop().pop();k(i);i.set_mask(false).history().enable()}})},{prompt:"new password: ",name:"passwd_2",keydown:function(e){if(e.which==68&&e.ctrlKey){i.pop().pop().echo("new password: ");i.set_mask(false).history().enable();return false}}})},{prompt:"current password: ",name:"passwd_1"})},rpc:function(e,n,t){var r=e.args[0]||"",i;if(r===""){i=function(e,n){n(Object.keys(h))}}else{var a=$.Deferred();$.jrpc(r,"system.describe",[],function(e){a.resolve(e.procs.map(function(e){return e.name}))},function(e,n,r){t.error("[AJAX]: "+n);k(t);if(n=="Invalid JSON"){t.error(e.responseText)}a.reject()});i=function(e,n){a.then(n).fail(function(){n([])})}}t.push(function(e){var n=$.terminal.parse_command(s(e));C(t);$.jrpc(r,n.name,n.args,function(e){if(e.error){if(e.error.error){var n=e.error.error;var r=n.file.replace(d.home,"");t.error(n.message+" in "+r+" at "+n.at);t.error(n.line)}else if(e.error.message){t.error(e.error.message)}}else{t.echo(JSON.stringify(e.result))}k(t)},function(e,n,r){t.error("[AJAX]: "+n);k(t);if(n=="Invalid JSON"){t.error(e.responseText)}})},{name:"rpc",prompt:"rpc> ",completion:i})},wikipedia:function(a,e,i){function o(t){var i=$.Deferred();$.ajax({url:n,data:{action:"query",prop:"revisions",rvprop:"content",format:"json",titles:a.rest},dataType:"jsonp",success:function(e){var r=e.query.pages;var n=Object.keys(r).map(function(e){var n=r[e];if(n.revisions){return n.revisions[0]["*"]}else if(typeof n.missing!="undefined"){return"Article Not Found"}}).join("\n");n=A.wikipedia(n,a.rest);if($.isFunction(t)){t(n)}i.resolve(n)}});return i.promise()}function s(){l.pop();if(!l.length){i.option("convertLinks",true)}}if(a.args.length===0){i.echo("Display contents of wikipedia articles\n"+"usage: wikipedia [{ARTICLE} |-s {TERM}]\n\n"+"-s {SEARCH TERM}")}else{C(i);i.option("convertLinks",false);var n="https://en.wikipedia.org/w/api.php?";l.push(a.rest.replace(/^-s\s*/,""));if(a.rest.match(/^-s\s*/)){$.ajax({url:n,data:{action:"opensearch",format:"json",limit:100,search:a.rest.replace(/^-s\s*/,"")},dataType:"jsonp",success:function(r){if(r[1].length&&r[2].length){var e=r[1].map(function(e,n){return"[[bu;#fff;;wiki]"+e+"]\n"+r[2][n]}).join("\n\n");A.less(e,s);k(i)}}})}else if(a.rest.match(/^Category:/)){$.ajax({url:n,data:{action:"query",list:"categorymembers",rvprop:"content",format:"json",cmlimit:500,cmtitle:a.rest},dataType:"jsonp",success:function(e){var n=e.query.categorymembers;var r=n.map(function(e){return"[[bu;#fff;;wiki]"+e.title+"]"}).join("\n");var t=/(\[\[bu;#fff;;wiki\]Category)/;o(function(e){r=e.replace(t,r+"\n\n$1");A.less(r,s);k(i)})}})}else{o(function(t){A.less(function(e,n){var r=$.terminal.split_equal(t,e,true);n(r)},s);k(i)})}}},jargon:function(e,n,t){if(!e.args.length){var r="This is the Jargon File, a comprehensiv"+"e compendium of hacker slang illuminating man"+"y aspects of hackish tradition, folklore, and"+" humor.\n\nusage: jargon [-s] [QUERY]\n\n-s s"+"earch jargon file";t.echo(r,{keepWords:true})}else if(e.args[0]=="-s"){C(t);var i=e.rest.replace(/^-s/,"").trim();if(!i.match(/%/)){i="%"+i+"%"}A.service.jargon_search(i)(function(e,n){if(e){g(e)}else{t.echo(n.map(function(e){return"[[bu;#fff;;jargon]"+e.term+"]"}).join("\n"))}k(t)})}else{C(t);var a=e.args.join(" ").replace(/\s+/g," ");h.jargon(a)(function(e,n){if(e){g(e)}else{var r=$.map(n,function(e){var n="[[b;#fff;]"+e.term+"]";if(e.abbr){n+=" ("+e.abbr.join(", ")+")"}var r=new RegExp("((?:https?|ftps?)://\\S+)|"+"\\.(?!\\s|\\]\\s)\\)?","g");var t=e.def.replace(r,function(e,n){return n?n:e==".)"?".) ":". "});r=/\[(?![^;\]]*;[^;\]]*;[^\]]*\])[^\]]+\]/g;t=t.replace(r,function(e){return e.replace(/\]/g,"\\]")});return n+"\n"+t+"\n"}).join("\n");t.echo(r.replace(/\n$/,""),{keepWords:true})}k(t)})}},man:function(e,n,r){if(e.args.length===0){r.echo("usage: man {COMMAND}")}else{C(r);var t="MANWIDTH="+r.cols()+" "+e.command;E(t,"/")(function(e,n){A.less($.terminal.overtyping(n.output));k(r)})}},sqlite:function(e,r,t){C(t);var i;if(!e.args.length){t.error("You need to provide the file");k(t);return}if(e.args[0].match(/^\//)){i=e.args[0]}else{i=A.cwd+"/"+e.args[0]}function a(e){var n=S();t.push(function(e){if(e.match(/^\s*help\s*$/)){t.echo("show tables:\n\tSELECT name FROM sqlite_m"+'aster WHERE type = "table"\ndescribe tabl'+"e:\n\tPRAGMA table_info([TABLE NAME])")}else{C(t);A.service.sqlite_query(r,i,e)(v)}},{name:"sqlite",prompt:"sqlite> ",completion:["help"].concat(n).concat(e),formatters:[b(n,e,"white")]})}var n='SELECT name FROM sqlite_master WHERE type = "table"';A.service.sqlite_query(r,i,n)(function(e,n){if(e){t.error(e.message)}else{a(n.map(function(e){return e["name"]}))}k(t)})},mysql:function(e,u,l){var n,r,p,m;var t=new optparse.OptionParser([["-h","--host HOST","Host to connect to"],["-u","--username USER","Database user"],["-p","--password PASSWORD","Database password"]]);var i=[];for(var a=0;a<e.args.length;++a){var o=e.args[a].match(/^(-[a-z])(.*)/);if(o){i.push(o[1]);if(o[2]){i.push(o[2])}else if(o[1]=="-p"){i.push("")}}else{i.push(e.args[a])}}t.on(0,function(e){n=e});t.on("host",function(e,n){r=n});t.on("username",function(e,n){p=n});t.on("password",function(e,n){m=n});t.banner="Usage: mysql [Options] database";t.parse(i);if(!(n&&p)){l.echo(t);return}r=r||"localhost";function s(){C(l);var t;function i(e){C(l);h.mysql_query(u,t,e)(v)}function a(n){h.mysql_close(u,n)(function(){var e=c().filter(function(e){return e!=n});f(e)})}var o="[[b;#55f;]mysql]> ";function s(e,n){n=$.map(n,function(n){return Object.keys(n).map(function(e){return n[e]})});var r=N();l.push(i,{prompt:o,name:"mysql",onExit:function(){a(t)},completion:r.concat(n),formatters:[b(r,n,"white")]});k(l)}function c(){var n;try{n=JSON.parse($.Storage.get("leash_mysql"))}catch(e){n=[]}return n}function f(e){$.Storage.set("leash_mysql",JSON.stringify(e))}c().forEach(function(e){a(e)});h.mysql_connect(u,r,p,m,n)(function(e,n){if(e){g(e);k(l)}else{t=n;var r=c();r.push(t);f(r);h.mysql_query(u,t,"show tables")(s)}})}if(!m){l.history().disable();l.push(function(e){m=e;l.pop().history().enable();s()},{prompt:"password: "}).set_mask("")}else{s()}},js:function(e,n,r){r.push(function(e,n){if(e!==undefined&&e!==""){try{var r=window.eval(e);if(r!==undefined){n.echo(new String(r))}}catch(e){n.error(new String(e))}}},{prompt:"[[;#D72424;]js]> ",name:"js"})},python:function(e,n,a){if(e.args.length){A.shell(e.command,n,a);return}var r="cgi-bin/python.py?token="+n;O(a,r,function(r){var t="";var i="Type help() for interactive help,"+" or help(object) for help about object.";a.push(function(e){if(e.match(/help/)){if(e.match(/^help *$/)){a.echo(i)}else{var n=/help\((.*)\)/;r.evaluate(e.replace(n,"print $1."+"__doc__"))}}else if(e.match(/:\s*$/)){t+=e+"\n";a.set_prompt("... ")}else if(t){if(e===""){a.set_prompt(">>> ");r.evaluate(t);t=""}else{t+=e+"\n"}}else{r.evaluate(e)}},{name:"python",prompt:">>> ",completion:false,onExit:function(){r.destroy()}})})},history:function(e,n,r){r.echo(r.history().data().join("\n"))},su:function(e,n,t){var r=new optparse.OptionParser([["-u","--user USER","User or root if not specified"]]);var a="root";r.on("user",function(e,n){a=n});r.parse(e.args);var i=function(){var n=t.level();return function e(){if(t.level()==n+1){t.logout()}}}();function o(e){var n,r;if(d&&d.server){n=d.server}else{n="unknown"}if(d&&A.cwd&&d.home!="/"){var t=$.terminal.escape_regex(d.home);var i=new RegExp("^"+t);r=A.cwd.replace(i,"~")}else{r=A.cwd}a=a||$.terminal.active().login_name();e(L(a,n,r))}t.history().disable();function s(e){var r=arguments.length==2;C(t);h.login(a,e)(function(e,n){t.history().enable();if(r){t.pop().set_mask(false)}if(e){t.error(e.message)}else if(n){u.push(A.cwd);f.push(A.make_interpreter(n));t.push(A.interpreter,{prompt:o,name:"su_"+a,onExit:function(){A.cwd=u.pop();c.onDirectoryChange(A.cwd);f.pop();A.env.TOKEN=t.token(true);$(window).off("unload",i)}});t.set_token(n)}else{t.error("Wrong password")}k(t)});$(window).unload(i)}t.set_mask(true).push(s,{prompt:"password: "})},adduser:function(e,n,i){var r={prompt:"password: ",mask:true};var t=["user: ",r,"home: "];function a(e,n,r,t){h.add_user(e,n,r,t)(function(e){if(e){i.error(e.message)}else{i.echo("user "+n+" added to leash")}})}A.execRead(t,a,{history:false,token:true})},deluser:function(e,n,r){if(e.args.length!=1){r.echo("remove leash user\nusage:\ndeluser [username]")}else{var t=e.args[0];function i(e){if(e){g(e)}else{r.echo("user "+t+" removed")}}r.push(function(e){if(e.match(/^(y(es)?|n(o)?)$/i)){if(e.match(/^y/i)){h.remove_user(n,t)(i)}r.pop()}},{prompt:"Are you sure you want to delete this"+" account (Y/N)? "})}},purge:function(e,n,r){r.logout().purge()},help:function(e,n,r){function t(e){e=e.map(function(e){return"[[b;#fff;]"+e+"]"});if(e.length==0){return""}else if(e.length==1){return e[0]}return e.slice(0,-1).join(", ")+" and "+e[e.length-1]}r.echo("leash build in commands: "+t(Object.keys(A.commands)),{keepWords:true});r.echo("all other commands are exectute by the shell");if(A.settings.guest_commands.length){r.echo("guest users can only exeucte: "+t(A.settings.guest_commands))}else{r.echo("guest users can't execute any commands")}},grab:function(s,e,c){if(navigator.mediaDevices&&navigator.mediaDevices.getUserMedia){var n=navigator.mediaDevices.getUserMedia({video:true});n.then(function(e){var n=e.getVideoTracks()[0];var r=new ImageCapture(n);return r.takePhoto()}).then(function(e){var n=new optparse.OptionParser([["-u","--upload FILENAME","filename to upload to the server"],["-h","--help","Display help screen"]]);var r;n.on("upload",function(e,n){r=n});var t;n.on("help",function(){t=true});n.parse(s.args);if(t){c.echo(n)}else if(r){var i=new Uploader(A);var a=new File([e],r);i.upload(a,A.cwd).then(function(){c.echo("uploaded to "+A.cwd)})}else{var o=URL.createObjectURL(e);c.echo('<img src="'+o+'"/>',{raw:true,finialize:function(e){e.find("img").on("load",function(){URL.revokeObjectURL(this.src)})}})}}).catch(function(e){c.error("Device Media Error: "+e)})}else{c.error("Image capture API don't supported by this device")}}}};h.installed()(function(e,n){A.installed=n;if(n){var a;A.greetings=A.banner();A.prompt=function(e){var n,r;if(d&&d.server){n=d.server}else{n="unknown"}if(d&&A.cwd&&d.home!="/"){var t=$.terminal.escape_regex(d.home);var i=new RegExp("^"+t);r=A.cwd.replace(i,"~")}else{r=A.cwd}a=a||$.terminal.active().login_name();e(L(a,n,r))};A.onImport=function(e){A.cwd=e.cwd;c.onDirectoryChange(A.cwd);A.dir=e.dir;A.terminal.set_prompt(A.prompt)};A.onExport=function(){return{cwd:A.cwd,dir:A.dir}};A.set_login=function(e){a=e};A.login=function(r,e,t){D[o]=t;var i=this;i.pause();h.login(r,e)(function(e,n){D[o]=null;a=r;A.token=n;if(n&&typeof sysend!="undefined"){sysend.broadcast("leash.login",{user:r,token:n})}t(n);if(!n){i.resume()}})}}else{A.prompt="> ";A.login=false;A.greetings=null}r.resolve(A)})}var a=0;var D={};return function(n){var r=new $.Deferred;var t=a++;if(n.service){$.when(n.service).then(function(e){i(e,t,r)})}else{rpc({url:n.url||"",error:function(e){console.log(e);try{e=JSON.parse(e)}catch(e){}var n;if(e.error){e=e.error;n=e.message+" in "+e.file+"\n["+e.at+"] "+e.line}else{n=e.message||e}var r=$.terminal.active();if(r){r.error(n);r.leash().then(function(e){e.animation.stop();k(r)})}else{alert(n)}if(D[t]){D[t](null,true)}},debug:function(e,n){var r=n=="request"?"->":"<-"}})(function(e){i(e,a,r,n||{})})}return r.promise()}}();(function(l){l.fn.leash=function(i){if(!l.terminal||!l.fn.terminal){throw new Error("You need to include jQuery terminal to use leash")}if(this.data("leash")){return this.data("leash")}var a=l.extend({},{disable:[],url:"",service:null},i&&i.leash||{});var o=l.Deferred();var f=this.length;var u=[];this.each(function(r){var t=l(this);var e=leash(a);t.data("leash",e);e.then(function(s){if(a.disable.length){a.disable.forEach(function(e){delete s.commands[e]})}var e={onInit:s.init,completion:s.completion,linksNoReferrer:true,execHash:true,historyFilter:/^[^\s]/,onBeforeLogout:function(e){var n=e.token();if(n){s.service.logout(n)(function(){if(typeof sysend!="undefined"){sysend.broadcast("leash.logout")}})}},prompt:s.prompt,login:s.login,onExport:s.onExport,onImport:s.onImport,onPop:s.onPop,onPush:s.onPush,extra:{formatters:s.formatters},name:"leash",outputLimit:500,greetings:s.greetings,keypress:function(e){if(s.animation.animating){if(e.which==68&&e.ctrlKey){s.animation.stop()}return false}}};var n=l.extend(e,i||{});var c=t.terminal(s.interpreter,n);s.terminal=c;c.on("drop",function(e){e.preventDefault();var n=e.originalEvent;if(!c.token()){return}var t=new Uploader(s);var r;if(n.dataTransfer.items){r=[].slice.call(n.dataTransfer.items)}var i=n.dataTransfer.files||n.target.files;if(i){i=[].slice.call(i)}function a(){s.refresh_dir()}if(r&&r.length){if(r[0].webkitGetAsEntry){var o=[];r.forEach(function(e){var n=e.webkitGetAsEntry();if(n)o.push(n)});(function e(){var n=o.shift();if(n){t.upload_tree(n,s.cwd).then(e)}else{a()}})()}}else if(i&&i.length){(function e(){var n=i.shift();if(n){t.upload(n,s.cwd).then(e)}else{a()}})()}else if(n.dataTransfer.getFilesAndDirectories){n.dataTransfer.getFilesAndDirectories().then(function(r){(function e(){var n=r.shift();if(n){t.upload_tree(n,s.cwd).then(e)}else{a()}})()})}}).on("dragover",function(e){e.preventDefault()}).on("dragenter",function(e){e.preventDefault()});if(typeof sysend!="undefined"){sysend.on("leash.logout",function(){s.prompt(function(e){c.echo(e)});c.logout()});sysend.on("leash.login",function(e){c.autologin(e.user,e.token);s.set_login(e.user)})}u.push(s);if(f-1==r){o.resolve.apply(o,u)}})});return o.promise()}})(jQuery);