Advertisement
cgrunwald

Untitled

Jul 15th, 2010
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @author Juntalis
  3.  */
  4.  
  5. function Interface()
  6. {
  7.     return {
  8.         Title : "",
  9.         Body : "",
  10.         Height : 0,
  11.         onLoad : function() {},
  12.         onBuild : function() {}
  13.     }
  14. }
  15.  
  16. function Interfaces(str)
  17. {
  18.     var ret = new Interface();
  19.     switch(str) {
  20.         /*
  21.          * UserLogin
  22.          * The interface used to log in.
  23.          */
  24.         case "UserLogin":
  25.             ret.Title = "User Login";
  26.             ret.Body = "<form id=\"FrmLogin\">\n" +
  27.             "\t\t\t\t\t<strong>Username</strong>:&nbsp;&nbsp;" +
  28.             "<input id=\"UName\" name=\"UserName\" type=\"text\" class=\"buffer\" maxlength=\"35\">\n" +
  29.             "\t\t\t\t\t<strong>Password</strong>:&nbsp;&nbsp;" +
  30.             "<input id=\"UPass\" name=\"UserPass\" type=\"password\" class=\"buffer\">\n" +
  31.             "\t\t\t\t\t<p>" +
  32.             "\n\t\t\t\t\t\t<input type=\"checkbox\" id=\"RUser\" class=\"chkbox\" name=\"RememberUser\" />" +
  33.             "\n\t\t\t\t\t\t<strong>Remember me.</strong><br />\n" +
  34.             "\t\t\t\t\t\t<input type=\"checkbox\" id=\"ALogin\" class=\"chkbox\" name=\"AutoLogin\" disabled=\"true\" />\n" +
  35.             "\t\t\t\t\t\t<strong>Automatically log me in.</strong>\n" +
  36.             "\t\t\t\t\t</p>\n" +
  37.             "\t\t\t\t\t<button id=\"cmdSubmit\" disabled=\"true\">Login</button>\n" +
  38.             "\t\t\t\t</form>";
  39.             ret.Height = 220;
  40.             ret.onLoad = function() {
  41.                 /* Checkbox Manipulation */
  42.                 $("#RUser").change(function() {
  43.                     if ($("#RUser:checked").length) {
  44.                         $("#ALogin").removeAttr("disabled");
  45.                     } else {
  46.                         $("#ALogin").removeAttr("checked").attr("disabled", "true");
  47.                     }
  48.                 });
  49.                
  50.                 /* Button Animation and Manipulation. */
  51.                 $("button").mousedown(function() {
  52.                     if(!$(this).hasClass("enabled")) return;
  53.                     $(this).css("background", "url('img/darkbar-c.png') repeat-x").css("color", "#ccc");
  54.                 });
  55.                 $("button").mouseup(function() {
  56.                     if(!$(this).hasClass("enabled")) return;
  57.                     $(this).css("background", "url('img/darkbar-h.png') repeat-x").css("color", "#fff");
  58.                     ValidateData();
  59.                 });
  60.  
  61.                 $("#FrmLogin").submit(function() {
  62.                     return false;
  63.                 });
  64.                
  65.                 /* Textbox Manipulation */
  66.                 $("#FrmLogin > input").keyup(function() {
  67.                     if ($("#FrmLogin > input[value=]").length == 0) {
  68.                         $("#cmdSubmit")
  69.                             .removeAttr("disabled")
  70.                             .addClass("enabled")
  71.                             .attr("class","enabled");
  72.                     } else {
  73.                         $("#cmdSubmit")
  74.                             .removeClass("enabled")
  75.                             .attr("disabled", "true")
  76.                             .removeAttr("class");
  77.                     }
  78.                 });
  79.                
  80.                 // This validates the form data, then
  81.                 // transitions the screen to the lobby
  82.                 // screen if it's ready.
  83.                 function ValidateData() {
  84.                     // Lock these until we're done.
  85.                     var blAutoDisabled = true;
  86.                     $("#UName").attr("disabled", "true");
  87.                     $("#UPass").attr("disabled", "true");
  88.                     $("#cmdSubmit").attr("disabled", "true");
  89.                     $("#RUser").attr("disabled", "true");
  90.                     if(!$("#ALogin").attr("disabled")) {
  91.                         blAutoDisabled = false;
  92.                         $("#ALogin").attr("disabled", "true");
  93.                     }
  94.                    
  95.                    
  96.                     // Get values of the text inputs.
  97.                     var strUName = $("#UName").val();
  98.                     var strUPass = $("#UPass").val();
  99.                    
  100.                     // Begin validity checking.
  101.                     var blValid = true;
  102.                     var strInvalidWhy = "";
  103.                    
  104.                     // Check for blank fields.
  105.                     if ($("input:text[value=]").length != 0) {
  106.                         blValid = false;
  107.                         strInvalidWhy = "You must fill out both fields!";
  108.                     }
  109.                    
  110.                     // Check for invalid chars.
  111.                     if (strUName.match(/[^A-Z\d[\]-]/gi)) {
  112.                         blValid = false;
  113.                         strInvalidWhy = "Your name can only consist of letters, numbers, \"-\", \"[\", or \"]\"!";
  114.                     }
  115.                    
  116.                    
  117.                     // Check to make sure we have valid data.
  118.                     if (!blValid) {
  119.                         alert(strInvalidWhy);
  120.                         $("#UName").removeAttr("disabled");
  121.                         $("#UPass").removeAttr("disabled");
  122.                         $("#cmdSubmit").removeAttr("disabled");
  123.                         $("#RUser").removeAttr("disabled");
  124.                         if (!blAutoDisabled) $("#ALogin").removeAttr("disabled");
  125.                         return false;
  126.                     }
  127.                    
  128.                    
  129.                     // Save settings.
  130.                     if ($("#RUser:checked").length > 0) {
  131.                         Settings.User.Remember = "true";
  132.                         App.SettingsManager.SetUser(strUName, strUPass);
  133.                         if ($("#ALogin:checked").length > 0) {
  134.                             Settings.User.AutoLogin = "true";
  135.                         } else {
  136.                             Settings.User.AutoLogin = "false";
  137.                         }
  138.                     } else {
  139.                         Settings.User.Remember = "false";
  140.                         Settings.User.AutoLogin = "false";
  141.                         App.SettingsManager.SetUser("", "");
  142.                     }
  143.                    
  144.                     // Finally, save our new settings.
  145.                     App.SettingsManager.SaveSettings();
  146.                    
  147.                     // Create the bot and begin transitioning user interfaces.
  148.                     Bot.Name = strUName;
  149.                     Bot.Password = md5(strUPass);
  150.                    
  151.                     // Load the lobby interface.
  152.                     UI.Manager.LoadInterface(Interfaces("Scripting"));
  153.                 }
  154.             };
  155.             ret.onBuild = function() {
  156.                 // Begin checking settings.
  157.                 if (Settings.User.Remember == "true") {
  158.                     $("#UName").val(Settings.User.Name);
  159.                     $("#UPass").val(Settings.User.Password);
  160.                     $("#RUser").attr("checked", "true");
  161.                     $("#ALogin").removeAttr("disabled");
  162.                     $("#cmdSubmit")
  163.                         .removeAttr("disabled")
  164.                         .addClass("enabled");
  165.                     if (Settings.User.AutoLogin == "true") {
  166.                         $("#ALogin")
  167.                             .attr("checked", "true")
  168.                             .attr("class","enabled");
  169.                     }
  170.                 }
  171.             };
  172.             return ret;
  173.             break;
  174.            
  175.         /*
  176.          * Hub
  177.          * The interface used after the user has logged in.
  178.          */
  179.         case "Hub":
  180.             ret.Title = "Central Hub";
  181.             ret.Body = "<p>Welcome, <span id=\"HubUsrName\"><strong>fag</strong></span></p>\n";
  182.             ret.Height = 253;
  183.             ret.onLoad = function()
  184.             {
  185.                 $("<div />")
  186.                     .addClass("smoothwhite")
  187.                     .attr("id", "HdHub")
  188.                     .css("font-size", "1.25em")
  189.                     .css("padding", "5px")
  190.                     .html("<strong>Toribash Servers</strong> - Loading...")
  191.                     .animate(
  192.                     {
  193.                         opacity: "1.0",
  194.                         height: "20px"
  195.                     }, 400)
  196.                     .appendTo("#CtMain");
  197.                    
  198.                 $("<div />")
  199.                     .addClass("smoothwhite")
  200.                     .attr("id", "CtHub")
  201.                     .css("word-wrap", "break-word")
  202.                     .css("overflow", "auto")
  203.                     .css("text-align", "center")
  204.                     .appendTo("#CtMain")
  205.                     .animate(
  206.                         {
  207.                             opacity : "1.0",
  208.                             height: "194px"
  209.                         },
  210.                         400,
  211.                         function() {
  212.                             $(this).html("<br /><br /><br /><br /><br /><p><img src=\"img/ani-loading.gif\"></p>");
  213.                             var lb = new Lobby();
  214.                             lb.Populate(
  215.                                 function() {
  216.                                     var objServers = lb.Servers();
  217.                                     if (objServers.length <= 0) return;
  218.                                     var arrBinds = new Array();
  219.                                     var htmServers = "<ul class=\"accordion\">\n";
  220.                                     for(i=0; i < objServers.length; i++ )
  221.                                     {
  222.                                         //$("#HdHub").html("<strong>Toribash Servers</strong> - " + i + " Total");
  223.                                         htmServers += "\t<li>\n";
  224.                                         htmServers += "\t\t<a href=\"#\">" + objServers[i].Name + " <span style=\"float:right;display:block;\">Players: " + objServers[i].Players + "/" + objServers[i].MaxPlayers + "</span></a>\n";
  225.                                         htmServers += "\t\t<div>\n";
  226.                                         htmServers += "\t\t\t<strong>Description</strong>: " + objServers[i].Description;
  227.                                         htmServers += "<span style=\"float:right;display:block;\"><a href=\"#\" id=\"srv_" + objServers[i].Name + "\">Connect</a></span><br />\n";
  228.                                         htmServers += "\t\t\t<strong>Host/Port</strong>: " +  objServers[i].IP + ":" + objServers[i].Port + "<br />\n";
  229.                                         htmServers += "\t\t\t<strong><u>Players</u></strong><br />\n";
  230.                                         $.each(objServers[i].Clients,function(index,value){ htmServers += "\t\t\t<span style=\"margin-left:1.5em;\">" + value + "</span><br />\n"});
  231.                                         htmServers += "\t\t</div>\n";
  232.                                         htmServers += "\t</li>\n";
  233.                                         var tmpSrv = { ID : "#srv_" + objServers[i].Name, Name : objServers[i].Name, Host : objServers[i].IP, Port : objServers[i].Port };
  234.                                         arrBinds.push(tmpSrv);
  235.                                     }
  236.                                     htmServers += "</ul>\n";
  237.                                     $("#HdHub").html("<strong>Toribash Servers</strong> - " + objServers.length + " Total");
  238.                                     $("#CtHub")
  239.                                         .html("")
  240.                                         .css("text-align", "left")
  241.                                         .append(htmServers);
  242.                                     $('ul.accordion').accordion();
  243.                                     $.each(arrBinds, function(iIndex, objVal) {
  244.                                         $(objVal.ID).click(function() {
  245.                                             Bot.Server.Name = objVal.Name;
  246.                                             Bot.Server.Host = objVal.Host;
  247.                                             Bot.Server.Port = objVal.Port;
  248.                                             UI.RemoveLink($.data(document.body, "_hub_href_login"));
  249.                                             UI.Manager.LoadInterface(Interfaces("ChatBox"));
  250.                                         });
  251.                                     });
  252.                                     $("#HdHub").prepend("<input type=\"text\" class=\"buffer\" id=\"TxtJoin\" style=\"float:right;display:block;width:200px\">");
  253.                                     if(Settings.App.RecentServer)
  254.                                         $("#TxtJoin").css("color","#444").val(Settings.App.RecentServer);
  255.                                     else
  256.                                         $("#TxtJoin").css("color","#aaa").val("Enter server name..");
  257.                                    
  258.                                     $("#TxtJoin")
  259.                                         .css("margin","0")
  260.                                         .css("padding","2px")
  261.                                         .css("font-size","1em")
  262.                                         .css("height","1em")
  263.                                         .keyup(function(e) {
  264.                                             $(this).val($(this).val().replace(" ",""));
  265.                                             if (e.keyCode == 13) {
  266.                                                 if ($(this).val()!="") {
  267.                                                     var sInput = $(this).val();
  268.                                                     $(this).val("");
  269.                                                     lb.Server(sInput,
  270.                                                         function(sname,shost,iport)
  271.                                                         {
  272.                                                             Settings.App.RecentServer = sname.replace('"','').replace("'","");
  273.                                                             Bot.Server.Name = sname;
  274.                                                             Bot.Server.Host = shost;
  275.                                                             Bot.Server.Port = iport;
  276.                                                             UI.RemoveLink($.data(document.body, "_hub_href_login"));
  277.                                                             UI.Manager.LoadInterface(Interfaces("ChatBox"));
  278.                                                         }
  279.                                                     );
  280.                                                 }
  281.                                             }
  282.                                         })
  283.                                         .focusin(function(e){ if($(this).val()=="Enter server name..") $(this).val("").css("color","#444"); })
  284.                                         .focusout(function(e){ if($(this).val()=="") $(this).val("Enter server name..").css("color","#aaa"); });
  285.                                 }
  286.                             );
  287.                         }
  288.                     );
  289.                    
  290.             };
  291.             ret.onBuild = function()
  292.             {
  293.                 var hrefLogin = UI.AddLink("#", "Return to Login");
  294.                 $("#" + hrefLogin).click(
  295.                     function() {
  296.                         UI.RemoveLink($(this).attr("id"));
  297.                         UI.Manager.LoadInterface(Interfaces("UserLogin"));     
  298.                     }
  299.                 );
  300.                 $("#HubUsrName").html("<strong>" + Bot.Name + "</strong>");
  301.                 $.data(document.body, "_hub_href_login", hrefLogin);
  302.             };
  303.             return ret;
  304.             break;
  305.            
  306.         /*
  307.          * ChatBox
  308.          * The interface used when the bot is in-game.
  309.          */
  310.         case "ChatBox":
  311.             ret.Title = "Connecting...";
  312.             ret.Body = "\t<div id=\"LstUsr\" class=\"buffer\" style=\"width:199px;height: 196px; float: right; margin-bottom: 0px; margin-top: 0px;\">" +
  313.             "\t\t<strong>Loading..</strong>" +
  314.             "</div>\n" +
  315.             "\t<div id=\"TxtIn\" class=\"buffer\" style=\"margin-bottom: 0px; width:480px\"></div>\n" +
  316.             "\t<input id=\"TxtOut\" type=\"text\" class=\"buffer\" maxlength=\"255\">\n";
  317.             ret.Height = 253;
  318.             ret.onLoad = function() {
  319.                 var sRecvBuffer = "";
  320.                 function PingTimer() {
  321.                     Bot.Send("PING");
  322.                     Misc.StartTimer(PingTimer, 5000);
  323.                 }
  324.                
  325.                 Bot.SckClient.handleConnection(
  326.                     function(event) {
  327.                         $("#TxtOut").removeAttr("disabled");
  328.                         UI.Manager.ChangeTitle(Bot.Server.Name + " - Connected");
  329.                         Misc.StartTimer(PingTimer, 5000);
  330.                         if (Bot.Scripted.Connect != null)
  331.                             Bot.Scripted.Connect();
  332.                            
  333.                     }
  334.                 );
  335.                
  336.                 Bot.SckClient.handleReceives(
  337.                     function(event) {
  338.                         sRecvBuffer += Bot.SckClient.recv();
  339.                         if(!sRecvBuffer) return;
  340.                        
  341.                         var data = "";
  342.                         if (sRecvBuffer.charAt(sRecvBuffer.length - 1) != '\n')
  343.                         {
  344.                             data = sRecvBuffer.substr(0, sRecvBuffer.lastIndexOf("\n"));
  345.                             sRecvBuffer = sRecvBuffer.substr(data.length - sRecvBuffer.length + 1);
  346.                         } else {
  347.                             data = sRecvBuffer.substr(0, sRecvBuffer.length - 1);
  348.                             sRecvBuffer = "";
  349.                         }
  350.                        
  351.                         // Check data
  352.                         if ((!data)||(data.length<=0))
  353.                             return;
  354.                        
  355.                         // Break up data
  356.                         var arrRecvs = data.split("\n");
  357.                        
  358.                         $.each(arrRecvs, function(index,value){
  359.                             if(value.match(/^TORIBASH 30$/)) {
  360.                                 Bot.Send("mlogin " + Bot.Name + " " + Bot.Password);
  361.                                 Bot.Send("SPEC");
  362.                             } else if(value.match(/^SAY\ (\d*?);(.*?)$/)) {
  363.                                 var rgXp = /^SAY (\d*?);(.*?)$/;
  364.                                 var sParam = rgXp.exec(value);
  365.                                 if (sParam != null)
  366.                                     _evSAY(sParam[1],sParam[2]);
  367.                             //} else if(value.match()) {
  368.                                
  369.                             //} else if(value.match()) {
  370.                                
  371.                             }
  372.                         });
  373.                     }
  374.                 );
  375.                
  376.                 // Called on a SAY event.
  377.                 function _evSAY(id, data)
  378.                 {
  379.                     if(id == "0") {
  380.                         ChatBox.WriteLine("[b]Server[/b]: " + data);
  381.                         if(Bot.Scripted.ServerMessage != null)
  382.                             Bot.Scripted.ServerMessage(data);
  383.                     } else {
  384.                         var usr = new User();
  385.                         var msg = "";
  386.                         usr.ID = parseInt(id);
  387.                        
  388.                         var rgXp = /^([A-Z\[\]\d_-]*?):\ (.*?)$/i;                     
  389.                         var sParam = rgXp.exec(data);
  390.                         if (sParam != null) {
  391.                             usr.Name = sParam[1];
  392.                             msg = sParam[2];
  393.                         } else {
  394.                             rgXp = /^\^(?:\d{1,2})<\^(?:\d{1,2})([A-Z\[\]\d_\-]*?)\^(?:\d{1,2})>\ (.*?)$/i;
  395.                             sParam = rgXp.exec(data);
  396.                             if (sParam != null) {
  397.                                 usr.Name = sParam[1];
  398.                                 msg = sParam[2];
  399.                             }
  400.                         }
  401.  
  402.                         ChatBox.WriteLine("[b]" + usr.Name + "[/b]: " + msg);
  403.                         if(Bot.Scripted.UserSay != null) Bot.Scripted.UserSay(usr, msg);
  404.                     }
  405.                 }
  406.                
  407.                 // The text input callback.
  408.                 $("#TxtOut").keyup(
  409.                     function(e) {
  410.                         if (e.keyCode == 13) {
  411.                             if ($(this).val()!="") {
  412.                                 var sInput = $(this).val();
  413.                                 $(this).val("");
  414.                                 Bot.Say(sInput);
  415.                             }
  416.                         }
  417.                     }
  418.                 );
  419.                
  420.                 // Connect
  421.                 $("#TxtIn").append("<strong>Connecting to " + Bot.Server.Host + ":" + Bot.Server.Port + "</strong>");
  422.                 Bot.Connect(Bot.Server.Host, Bot.Server.Port);
  423.             };
  424.             ret.onBuild = function() {
  425.                 $("#TxtIn").animate({height: "200px"},400, function(){$(this).css("overflow", "auto").css("word-wrap", "break-word");});
  426.                 $("#TxtOut").width("698px").css("margin-bottom", "2px").css("margin-top","5px").attr("disabled", "true");
  427.                 $.data(document.body, "_chatbox_href_hub", UI.AddLink("#", "Return to Hub"));
  428.                 $("#" + $.data(document.body, "_chatbox_href_hub")).click(
  429.                     function() {
  430.                         UI.RemoveLink($(this).attr("id"));
  431.                         Bot.Disconnect();
  432.                         UI.Manager.LoadInterface(Interfaces("Hub"));       
  433.                     }
  434.                 );
  435.             };
  436.             break;
  437.            
  438.         /*
  439.          * Scripting
  440.          * The interface used when to configure the script for this bot.
  441.          */
  442.         case "Scripting":
  443.             ret.Title = "Bot Scripting";
  444.             ret.Body = "<div id=\"CtDesc\" style=\"font-size:1.25em;\">"+
  445.             "\t<p>\n"+
  446.             "\t\t<strong>TB-&gt;HTML</strong> gives your the ability to create your own bots to run atop this framework.\n"+
  447.             "\t</p>\n"+
  448.             "\t<input id=\"TxtFilePath\" type=\"text\" style=\"border:1px solid #ccc; width:702px; display:block; padding:4px;background-color:#fff;color:#444;margin-right:0px;font-size:0.8em;\" readonly=\"true\">"+
  449.             "\t<div id=\"TxtSrc\" class=\"buffer\" style=\"padding: 0; opacity: 0.0; margin-top: 5px; margin-bottom: 5px; height: 155px;\"></div>\n"+
  450.             "\t<button style=\"float: right; font-weight: bold; font-size: 0.8em; width: 175px;\" class=\"enabled\" id=\"CmdNext\">Continue to Lobby</button>\n"+
  451.             "\t<button style=\"float: left; font-weight: bold; font-size: 0.8em; width: 140px;\" class=\"enabled\" id=\"CmdLoad\">Select File</button>\n"+
  452.             "\t<button style=\"margin-left: 5px; float: left; font-weight: bold; font-size: 0.8em; width: 160px; opacity: 0.0; display: none;\" class=\"enabled\" id=\"CmdRestart\">Initialize Script</button>\n"+
  453.             "\t<button style=\"margin-left: 5px; float: left; font-weight: bold; font-size: 0.8em; width: 140px; opacity: 0.0; display: none;\" class=\"enabled\" id=\"CmdDeselect\">Deselect File</button>\n"+
  454.             "</div>";
  455.             ret.Height = 250;
  456.             ret.onLoad = function () {
  457.                 /* Settings loading */
  458.                 var fhScript; var sBotScript = "";
  459.                 if (typeof(_UserBotInit) != "undefined") {
  460.                     fhScript = air.File.applicationDirectory.resolvePath("user").resolvePath("User.Bot.js");
  461.                     var evParams = new air.Event("hideRestart");
  462.                     AnimateInterface(evParams);
  463.                 } else {
  464.                     if (Settings.App.RecentBot) {
  465.                         fhScript = new air.File();
  466.                         fhScript.nativePath = Settings.App.RecentBot;
  467.                     } else fhScript = air.File.userDirectory;
  468.                     $("#TxtFilePath").val("No script loaded..");
  469.                 }
  470.                 /* Button Animation and Manipulation. */
  471.                 $("button").mousedown(function() {
  472.                     if(!$(this).hasClass("enabled")) return;
  473.                     $(this).css("background", "url('img/darkbar-c.png') repeat-x").css("color", "#ccc");
  474.                 });
  475.                 $("button").mouseup(function() {
  476.                     if(!$(this).hasClass("enabled")) return;
  477.                     $(this).css("background", "url('img/darkbar-h.png') repeat-x").css("color", "#fff");
  478.                 });
  479.                 $("button").hover(function() {
  480.                     if(!$(this).hasClass("enabled")) return;
  481.                     $(this).css("background", "url('img/darkbar-h.png') repeat-x").css("color", "#fff");
  482.                 },function() {
  483.                     if(!$(this).hasClass("enabled")) return;
  484.                     $(this).css("background", "url('img/darkbar.png') repeat-x").css("color", "#bbb");
  485.                 });
  486.                
  487.                 /* Next Interface Button */
  488.                 $("#CmdNext").click(function(){
  489.                     UI.RemoveLink($.data(document.body, "_script_href_login"));
  490.                     UI.Manager.LoadInterface(Interfaces("Hub"));
  491.                 });
  492.                
  493.                
  494.                 /* Load file button */
  495.                 $("#CmdLoad").click(function(){
  496.                     if($(this).attr("disabled")) return;
  497.                     var jsBotFilter = new air.FileFilter("JavaScript Toribash Bot (*.jstb)", "*.jstb");    
  498.                     fhScript.browseForOpen("Open", [jsBotFilter]);
  499.                     fhScript.addEventListener(air.Event.SELECT, AnimateInterface);
  500.                 });
  501.                
  502.                 /* Animates the loading of a script file. */
  503.                 function AnimateInterface(event) {
  504.                     if(fhScript.nativePath) {
  505.                         if (fhScript.exists) {
  506.                             $("#TxtFilePath").val(fhScript.nativePath);
  507.                             $("button").each(function(i, el) {$(this).attr("disabled", "true");});
  508.                             $("#CmdDeselect")
  509.                                 .css("display", "block")
  510.                                 .animate({opacity: 1.0}, 400);
  511.                             if (event.type != "hideRestart") {
  512.                                 $("#CmdRestart").css("display", "block").animate({opacity: 1.0}, 400);
  513.                             }
  514.                             $("#TxtSrc")
  515.                                 .html("<br /><br /><p style=\"text-align:center;\"><img src=\"img/ani-loading.gif\"></p>")
  516.                                 .animate({opacity: 1.0}, 400,
  517.                                     function() {
  518.                                         LoadScriptFile(true,null);
  519.                                     }
  520.                                 );
  521.                         } else {
  522.                             if (Settings.App.RecentBot) {
  523.                                 fhScript = new air.File();
  524.                                 fhScript.nativePath = Settings.App.RecentBot;
  525.                             } else fhScript = air.File.userDirectory;
  526.                             $("#TxtFilePath").val("No script loaded..");
  527.                         }
  528.                     }
  529.                 }
  530.                
  531.                 /* Load Script File */
  532.                 function LoadScriptFile(blAutoDeselect, cbAfter) {
  533.                     stFile = new air.FileStream();
  534.                     try
  535.                     {
  536.                         $("button").each(function(i, el) {$(this).removeAttr("disabled");});
  537.                         stFile.open(fhScript, air.FileMode.READ);
  538.                         var strBuffer = stFile.readUTFBytes(stFile.bytesAvailable);
  539.                         stFile.close();
  540.                         sBotScript = strBuffer;
  541.                     }
  542.                     catch(error)
  543.                     {
  544.                         alert("Error while reading script file!\nError: " + error);
  545.                         if (blAutoDeselect) $("#CmdDeselect").click();
  546.                     }
  547.                    
  548.                     // Animate transition.
  549.                     $("#TxtSrc").children().animate({opacity:0.0},200,
  550.                         function(){
  551.                             $("#TxtSrc")
  552.                                 .css("padding","0px")
  553.                                 .html("<pre class=\"sh_javascript_dom\" style=\"margin:0;padding:3px;\">" + Text.EscapeHtml(strBuffer) + "</pre>");
  554.                             sh_highlightDocument();
  555.                             $(this).animate({opacity:0.0},200,
  556.                                 function(){
  557.                                     $("button").each(function(i,el){$(this).removeAttr("disabled");});
  558.                                     if(Settings.App.RecentBot)
  559.                                         fhScript.nativePath = Settings.App.RecentBot;
  560.                                     else
  561.                                         Settings.App.RecentBot = fhScript.nativePath;
  562.                                     if (cbAfter&&sBotScript) cbAfter();
  563.                                 }
  564.                             );
  565.                         }
  566.                     );
  567.                 }
  568.                
  569.                 /* Try writing a new .js file and exiting the program. */
  570.                 $("#CmdRestart").click(function(){
  571.                     $("button").each(function(i, el) {$(this).attr("disabled", "true");});
  572.                     LoadScriptFile(false,function(){
  573.                         // Create file objects.
  574.                         var stFile = new air.FileStream();
  575.                         var appBotJs =  fhScript.parent.resolvePath("User.Bot.js");
  576.                         var appInstallDir = air.File.applicationDirectory.resolvePath("user");
  577.                        
  578.                         // Write new script file.
  579.                         try {                      
  580.                             stFile.open(appBotJs, air.FileMode.WRITE);
  581.                             stFile.writeUTFBytes("function _UserBotInit() { Bot.Scripted = " + sBotScript + "; }");
  582.                             stFile.close();        
  583.                         } catch(error) {
  584.                             alert("Error while writing user bot script!\n" + error);
  585.                             if(_DEBUG.FILE) console.log(error);
  586.                             return;
  587.                         }
  588.                        
  589.                         // If native process is supported, automate the user bot installation.
  590.                         if (air.NativeProcess.isSupported) {
  591.                             try {
  592.                                 // Create temporary shell script.
  593.                                 var osSystem = air.Capabilities.os;
  594.                                 var tmpShellScript = air.File.createTempFile();
  595.                                 var tmpShScrExec = new air.File();
  596.                                 var tmpShScrStartInfo = new air.NativeProcessStartupInfo();
  597.                                 var tmpShScrArgs = new air.Vector["<String>"]();
  598.                                 var tmpProc = new air.NativeProcess();
  599.                                 var tmpShScrContents = "";
  600.                                
  601.                                 // Create file according to operating system.
  602.                                 if (osSystem.match(/Windows/mi)) {
  603.                                     // Windows
  604.                                     tmpShellScript.nativePath = tmpShellScript.nativePath + ".bat";
  605.                                     tmpShScrContents = "@ECHO OFF\n"+
  606.                                     "MOVE /Y \"" + appBotJs.nativePath + "\" \"" + appInstallDir.nativePath + "\"";
  607.                                     tmpShScrExec.nativePath = "C:\\Windows\\system32\\cmd.exe";
  608.                                     tmpShScrArgs.push("/C");
  609.                                     tmpShScrArgs.push(tmpShellScript.nativePath);
  610.                                 } else if ((osSystem.match(/Mac OS/mi))||(osSystem.match(/Linux/mi))) {
  611.                                     // Mac OS X and Linux both use unix-style shell scripts.
  612.                                     tmpShellScript.nativePath = tmpShellScript.nativePath + ".sh";
  613.                                     tmpShScrContents = "#!/bin/bash\n"+
  614.                                     "mv -f \"" + appBotJs.nativePath + "\" \"" + appInstallDir.nativePath + "\"";
  615.                                     tmpShScrExec.nativePath = "/bin/sh";
  616.                                     tmpShScrArgs.push(tmpShellScript.nativePath);
  617.                                    
  618.                                 } else {
  619.                                     // Not sure. So instead we'll do what we would've if the
  620.                                     // system couldn't do native processes.
  621.                                     alert("Your operating system was not recognized.\n\nIt seems you will need to install the script manually.\n\n"+
  622.                                      "To do so, move the newly generated User.Bot.js which is in the same directory as your bot script to the 'user' subfolder located in the installation folder for this application.\n\n"+
  623.                                      "It should be located at:\n" + appInstallDir.nativePath);
  624.                                     appBotJs = appBotJs.parent;
  625.                                     appBotJs.openWithDefaultApplication();
  626.                                     App.Exit();
  627.                                     return;
  628.                                 }
  629.                                
  630.                                 // Use the os's line breaks.
  631.                                 tmpShScrContents = tmpShScrContents.replace(/\n/g, air.File.lineEnding);
  632.                                 stFile.open(tmpShellScript, air.FileMode.WRITE);
  633.                                 stFile.writeUTFBytes(tmpShScrContents);
  634.                                 stFile.close();
  635.                                
  636.                                 // Launch application
  637.                                 tmpShScrStartInfo.executable = tmpShScrExec;
  638.                                 tmpShScrStartInfo.arguments = tmpShScrArgs;
  639.                                 tmpProc.start(tmpShScrStartInfo);
  640.                                 tmpProc.addEventListener(air.ProgressEvent.STANDARD_OUTPUT_DATA,
  641.                                     function(event) { if(_TRACE.PROCESS) trace("Got: ", tmpProc.standardOutput.readUTFBytes(tmpProc.standardOutput.bytesAvailable)); }
  642.                                 );
  643.                                 tmpProc.addEventListener(air.ProgressEvent.STANDARD_ERROR_DATA,
  644.                                     function(event) { if(_TRACE.PROCESS) trace("ERROR -", tmpProc.standardError.readUTFBytes(tmpProc.standardError.bytesAvailable)); }
  645.                                 );
  646.                                 tmpProc.addEventListener(air.NativeProcessExitEvent.EXIT,
  647.                                     function(event) {
  648.                                          if(_TRACE.PROCESS) trace("Process exited with ", event.exitCode);
  649.                                          
  650.                                         // Finished, now we do our last acts.
  651.                                         alert("To complete the installation of your bot script, you must restart the application.\nWe will now close the application.");
  652.                                         if (tmpShellScript.exists) tmpShellScript.deleteFile();
  653.                                         App.Exit();
  654.                                     }
  655.                                 );
  656.                                 tmpProc.addEventListener(air.IOErrorEvent.STANDARD_OUTPUT_IO_ERROR,
  657.                                     function(event) { if(_TRACE.PROCESS) trace(event.toString()); }
  658.                                 );
  659.                                 tmpProc.addEventListener(air.IOErrorEvent.STANDARD_ERROR_IO_ERROR,
  660.                                     function(event) { if(_TRACE.PROCESS) trace(event.toString()); }
  661.                                 );                             
  662.                             } catch (error) {
  663.                                 if(_DEBUG.PROCESS) console.log(error);
  664.                                 if (tmpShellScript.exists) tmpShellScript.deleteFile();
  665.                                 alert("There was an error while automating the installation process. It seems you will need to install the script manually.\n\n"+
  666.                                  "To do so, move the newly generated User.Bot.js which is in the same directory as your bot script to the 'user' subfolder located in the installation folder for this application.\n\n"+
  667.                                  "It should be located at:\n" + appInstallDir.nativePath + "\n\nError Details\n" + error);
  668.                                 appBotJs = appBotJs.parent;
  669.                                 //appBotJs.openWithDefaultApplication();
  670.                                 //App.Exit();  
  671.                             }
  672.                         } else {
  673.                              alert("It seems you will need to install the script manually.\n\n"+
  674.                              "To do so, move the newly generated User.Bot.js which is in the same directory as your bot script to the 'user' subfolder located in the installation folder for this application.\n\n"+
  675.                              "It should be located at:\n" + appInstallDir.nativePath);
  676.                             appBotJs = appBotJs.parent;
  677.                             appBotJs.openWithDefaultApplication();
  678.                             App.Exit();
  679.                         }
  680.                     });
  681.                 });
  682.                
  683.                 /* Deselect a file */
  684.                 $("#CmdDeselect").click(function(){
  685.                     $("#TxtFilePath").val("No script loaded..");
  686.                     $("button").each(function(i,el){ $(this).attr("disabled","true"); });
  687.                     $("#CmdDeselect").animate({opacity:0.0},400,function(){$(this).css("display","none");});
  688.                     $("#CmdRestart").animate({opacity:0.0},400,function(){$(this).css("display","none");});
  689.                     $("#TxtSrc").animate({opacity:0.0},400,
  690.                         function(){
  691.                             // Set Bot.Scripted to default.
  692.                             _resetBotScript();
  693.                            
  694.                             // Create file objects.
  695.                             var stFile = new air.FileStream();
  696.                             var appBotJs =  fhScript.parent.resolvePath("User.Bot.js");
  697.                             var appInstallDir = air.File.applicationDirectory.resolvePath("user");
  698.                            
  699.                             // Write new script file.
  700.                             try {                      
  701.                                 stFile.open(appBotJs, air.FileMode.WRITE);
  702.                                 stFile.writeUTFBytes("\n");
  703.                                 stFile.close();        
  704.                             } catch(error) {
  705.                                 alert("Error while writing blank file!\n" + error);
  706.                                 if(_DEBUG.FILE) console.log(error);
  707.                                 // Enable buttons
  708.                                 $("button").each(function(i,el){$(this).removeAttr("disabled");});
  709.                                 return;
  710.                             }
  711.                            
  712.                             // If native process is supported, automate the user bot installation.
  713.                             if (air.NativeProcess.isSupported) {
  714.                                 try {
  715.                                     // Create temporary shell script.
  716.                                     var osSystem = air.Capabilities.os;
  717.                                     var tmpShellScript = air.File.createTempFile();
  718.                                     var tmpShScrExec = new air.File();
  719.                                     var tmpShScrStartInfo = new air.NativeProcessStartupInfo();
  720.                                     var tmpShScrArgs = new air.Vector["<String>"]();
  721.                                     var tmpProc = new air.NativeProcess();
  722.                                     var tmpShScrContents = "";
  723.                                    
  724.                                     // Create file according to operating system.
  725.                                     if (osSystem.match(/Windows/mi)) {
  726.                                         // Windows
  727.                                         tmpShellScript.nativePath = tmpShellScript.nativePath + ".bat";
  728.                                         tmpShScrContents = "@ECHO OFF\n"+
  729.                                         "MOVE /Y \"" + appBotJs.nativePath + "\" \"" + appInstallDir.nativePath + "\"";
  730.                                         tmpShScrExec.nativePath = "C:\\Windows\\system32\\cmd.exe";
  731.                                         tmpShScrArgs.push("/C");
  732.                                         tmpShScrArgs.push(tmpShellScript.nativePath);
  733.                                     } else if ((osSystem.match(/Mac OS/mi))||(osSystem.match(/Linux/mi))) {
  734.                                         // Mac OS X and Linux both use unix-style shell scripts.
  735.                                         tmpShellScript.nativePath = tmpShellScript.nativePath + ".sh";
  736.                                         tmpShScrContents = "#!/bin/bash\n"+
  737.                                         "mv -f \"" + appBotJs.nativePath + "\" \"" + appInstallDir.nativePath + "\"";
  738.                                         tmpShScrExec.nativePath = "/bin/sh";
  739.                                         tmpShScrArgs.push(tmpShellScript.nativePath);
  740.                                        
  741.                                     } else {
  742.                                         // Not sure. So instead we'll do what we would've if the
  743.                                         // system couldn't do native processes.
  744.                                         alert("To make sure the bot doesn't load next time, please clear the contents of your user/User.Bot.js file.\n\nShould be located at:\n" + appInstallDir.nativePath);
  745.                                         if(appBotJs.exists) appBotJs.deleteFile();
  746.                                         $("button").each(function(i,el){$(this).removeAttr("disabled");});
  747.                                         return;
  748.                                     }
  749.                                    
  750.                                     // Use the os's line breaks.
  751.                                     tmpShScrContents = tmpShScrContents.replace(/\n/g, air.File.lineEnding);
  752.                                     stFile.open(tmpShellScript, air.FileMode.WRITE);
  753.                                     stFile.writeUTFBytes(tmpShScrContents);
  754.                                     stFile.close();
  755.                                    
  756.                                     // Launch application
  757.                                     tmpShScrStartInfo.executable = tmpShScrExec;
  758.                                     tmpShScrStartInfo.arguments = tmpShScrArgs;
  759.                                     tmpProc.start(tmpShScrStartInfo);
  760.                                     tmpProc.addEventListener(air.ProgressEvent.STANDARD_OUTPUT_DATA,
  761.                                         function(event) { if(_TRACE.PROCESS) trace("Got: ", tmpProc.standardOutput.readUTFBytes(tmpProc.standardOutput.bytesAvailable)); }
  762.                                     );
  763.                                     tmpProc.addEventListener(air.ProgressEvent.STANDARD_ERROR_DATA,
  764.                                         function(event) { if(_TRACE.PROCESS) trace("ERROR -", tmpProc.standardError.readUTFBytes(tmpProc.standardError.bytesAvailable)); }
  765.                                     );
  766.                                     tmpProc.addEventListener(air.NativeProcessExitEvent.EXIT,
  767.                                         function(event) {
  768.                                              if(_TRACE.PROCESS) trace("Process exited with ", event.exitCode);
  769.                                             // Finished, now we do our last acts.
  770.                                             if (tmpShellScript.exists) tmpShellScript.deleteFile();
  771.                                             // Enable buttons
  772.                                             $("button").each(function(i,el){$(this).removeAttr("disabled");});
  773.                                         }
  774.                                     );
  775.                                     tmpProc.addEventListener(air.IOErrorEvent.STANDARD_OUTPUT_IO_ERROR,
  776.                                         function(event) { if(_TRACE.PROCESS) trace(event.toString()); }
  777.                                     );
  778.                                     tmpProc.addEventListener(air.IOErrorEvent.STANDARD_ERROR_IO_ERROR,
  779.                                         function(event) { if(_TRACE.PROCESS) trace(event.toString()); }
  780.                                     );                             
  781.                                 } catch (error) {
  782.                                     if(_DEBUG.PROCESS) console.log(error);
  783.                                     if (tmpShellScript.exists) tmpShellScript.deleteFile();
  784.                                     alert("There was an error while automating the removal process. To make sure the bot doesn't load next time, please clear the contents of your user/User.Bot.js file.\n\nError Details\n" + error);
  785.                                     if(appBotJs.exists) appBotJs.deleteFile();
  786.                                     // Enable buttons
  787.                                     $("button").each(function(i,el){$(this).removeAttr("disabled");});
  788.                                 }
  789.                             } else {
  790.                                 alert("To make sure the bot doesn't load next time, please clear the contents of your user/User.Bot.js file.\n\nShould be located at:\n" + appInstallDir.nativePath);
  791.                                 if(appBotJs.exists) appBotJs.deleteFile();
  792.                                 $("button").each(function(i,el){$(this).removeAttr("disabled");});
  793.                             }
  794.                         }
  795.                     );
  796.                 });
  797.                
  798.             };
  799.             ret.onBuild = function () {
  800.                 $("#TxtSrc")
  801.                     .css("overflow","auto")
  802.                     .css("word-wrap", "break-word");
  803.                 $.data(document.body, "_script_href_login", UI.AddLink("#", "Return to Login"));
  804.                 $("#" + $.data(document.body, "_script_href_login")).click(
  805.                     function() {
  806.                         UI.RemoveLink($(this).attr("id"));
  807.                         Bot.Disconnect();
  808.                         UI.Manager.LoadInterface(Interfaces("UserLogin"));     
  809.                     }
  810.                 );
  811.             };
  812.             break;
  813.         default:
  814.             break;
  815.     }
  816.     return ret;
  817. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement