"use strict";(function(){"use strict";var e=require("util");var t=0;function n(e){if(false===this instanceof n){return new n}this.instance=e;this.telnetState="start"}n.prototype.exec=function(e,t,n){var r=this;console.log(n);this.realCmd=n.cmd+"\r\n";this.callback=t;this.host=typeof n.host!=="undefined"?n.host:"127.0.0.1";this.port=typeof n.port!=="undefined"?n.port:23;this.shellPrompt=typeof n.shellPrompt!=="undefined"?n.shellPrompt:"#";this.loginPrompt=typeof n.loginPrompt!=="undefined"?n.loginPrompt:"Username:";this.passwordPrompt=typeof n.passwordPrompt!=="undefined"?n.passwordPrompt:"Password:";this.username=typeof n.username!=="undefined"?n.username:"admin";this.password=typeof n.password!=="undefined"?n.password:"admin";this.enable=typeof n.enable!=="undefined"?n.enable:true;this.enablePrompt=typeof n.enablePrompt!=="undefined"?n.enablePrompt:"Password:";this.enablePassword=typeof n.enablePassword!=="undefined"?n.enablePassword:"admin";this.moreParser=typeof n.moreParser!=="undefined"?n.moreParser:" --More-- \b\b\b\b\b\b\b\b\b\b          \b\b\b\b\b\b\b\b\b\b";this.irs=typeof n.irs!=="undefined"?n.irs:"\r\n";this.ors=typeof n.ors!=="undefined"?n.ors:"\r\n";this.pageSeparator=typeof n.pageSeparator!=="undefined"?n.pageSeparator:"--More--";this.response="";this.enableShellPrompt=">";r.instance.exec(e,function(){},r.instance)};n.prototype.parseData=function(e){var n=this;var s="";var a="";if(n.telnetState==="start"){n.telnetState="getprompt"}if(n.telnetState==="getprompt"){s=e.toString();a=s.indexOf(n.shellPrompt);if(a!==-1){n.shellPrompt=s.substring(a);n.telnetState="response";n.instance.writeToDevice(n.realCmd);n.stringData=""}else if(s.indexOf(n.loginPrompt)!==-1){n.telnetState="login";r(n,"username")}else if(s.indexOf(n.passwordPrompt)!==-1){n.telnetState="login";r(n,"password")}}else if(n.telnetState==="enable"){s=e.toString();if(s.indexOf(n.enablePrompt)!==-1){n.telnetState="login";r(n,"enablePassword")}}else if(n.telnetState==="getenprompt"){s=e.toString();if(s.indexOf(n.enableShellPrompt)!==-1){n.telnetState="login";r(n,"enable")}}else if(n.telnetState==="response"){s=e.toString();n.stringData+=s;var i=false;if(n.stringData.indexOf(n.pageSeparator)!==-1){n.stringData=n.stringData.replace(new RegExp(n.moreParser,"gm"),"");if(t==1){console.log("pageSeparator is  "+n.pageSeparator)}i=true}var o=s.trim();if(o.length>=1&&o[o.length-1]=="#"){n.cmdOutput=n.stringData.split(n.irs);if(n.containShell===true){if(n.echoLines===1){n.cmdOutput.shift()}n.cmdOutput.pop()}n.instance.doubleConnectInstance=null;n.callback(n.cmdOutput.join("\n"));n.stringData="";return}if(i){n.instance.writeToDevice(Buffer("20","hex"));return}}};function r(e,t){console.log(t);if(t==="username"){e.instance.writeToDevice(e.username+e.ors);e.telnetState="getprompt"}else if(t==="password"){e.instance.writeToDevice(e.password+e.ors);if(e.enable){e.telnetState="getenprompt"}else{e.telnetState="getprompt"}}else if(t==="enable"){e.instance.writeToDevice("en"+e.ors);e.telnetState="enable"}else if(t==="enablePassword"){e.instance.writeToDevice(e.enablePassword+e.ors);e.telnetState="getprompt"}}module.exports=n})();