module.exports = class TelnetLogin { constructor(socket) { this.socket = socket; this.awaitUsername = false; this.awaitPassword = false; this.username = ""; this.password = ""; this.loggedIn = false; this.player = null; this.failCount = 0; this.inputBuffer = ""; this.isReconnecting = false; this.reconnectLogin = null; } attach() { libs.Telnet.sendString(this.socket, config.welcomeBanner + "\r\n"); libs.Telnet.sendString(this.socket, "Please log in to your account.\r\n"); libs.Telnet.sendString(this.socket, "Username: "); this.awaitUsername = true; } receive(data) { if(this.serverNegotiations(data)) { return; } this.inputBuffer += data.toString(); if(!(this.inputBuffer.indexOf("\r\n") != -1 || this.inputBuffer.indexOf("\n") != -1 || this.inputBuffer.indexOf("\r") != -1)) { //did not see a return char, keep building buffer return; } var input = this.inputBuffer.toString().trim().split(" "); if(!this.loggedIn) { this.handleInput(input); } else { this.receivePlayer(input); } this.inputBuffer = ""; } sendMessage(message){ var terminalWidthPreference = (!this.player.hasOwnProperty("terminalWidthPreference") || this.player.terminalWidthPreference == null) ? 78 : this.player.terminalWidthPreference; var hintColor = (!this.player.hasOwnProperty("hintColor") || this.player.hintColor == null) ? libs.Color.FgMagenta : libs.Color[this.player.hintColor]; var simpleMessage = message.replace(/\{.*?\}/g, ""); message = libs.Color.Reset + message; message = message.replace(/\{hint\}/g, hintColor); message = message.replace(/\{\/hint\}/g, libs.Color.Reset); message = message.replace(/\{mobile\}/g, libs.Color.FgYellow); message = message.replace(/\{\/mobile\}/g, libs.Color.Reset); message = message.replace(/\{item\}/g, libs.Color.FgGreen); message = message.replace(/\{\/item\}/g, libs.Color.Reset); message = message.replace(/\{error\}/g, libs.Color.FgRed); message = message.replace(/\{\/error\}/g, libs.Color.Reset); message = message.replace(/\{global\}/g, libs.Color.FgCyan); message = message.replace(/\{\/global\}/g, libs.Color.Reset); message = message.replace(/\{red\}/g, libs.Color.FgRed); message = message.replace(/\{\/red\}/g, libs.Color.Reset); message = message.replace(/\{green\}/g, libs.Color.FgGreen); message = message.replace(/\{\/green\}/g, libs.Color.Reset); message = message.replace(/\{yellow\}/g, libs.Color.FgYellow); message = message.replace(/\{\/yellow\}/g, libs.Color.Reset); message = message.replace(/\{blue\}/g, libs.Color.FgBlue); message = message.replace(/\{\/blue\}/g, libs.Color.Reset); message = message.replace(/\{magenta\}/g, libs.Color.FgMagenta); message = message.replace(/\{\/magenta\}/g, libs.Color.Reset); message = message.replace(/\{cyan\}/g, libs.Color.FgCyan); message = message.replace(/\{\/cyan\}/g, libs.Color.Reset); message = message.replace(/\{.*?\}/g, ""); var buffer = [], colorBuffer = []; buffer = simpleMessage.split(" "); colorBuffer = message.split(" "); while(buffer.length > 0) { var wrapString = buffer.shift(); var displayString = colorBuffer.shift(); if(buffer.length > 0) { while(wrapString.length + buffer[0].length < terminalWidthPreference) { wrapString += " " + buffer.shift(); displayString += " " + colorBuffer.shift(); if(buffer.length == 0) { break; } } } this.socket.write(displayString + "\r\n"); } } serverNegotiations(data) { if(data[0] == libs.Telnet.IAC) { //TODO: actually listen to the client and negotiate return true; } return false; } disconnect() { if(this.isReconnecting) { this.player.login = this.reconnectLogin; this.isReconnecting = false; this.socket = null; this.loggedIn = false; //this.socket = this.reconnectSocket; //this.reconnectSocket = null; libs.Output.toMobile(this.player.id, "Reconnecting to existing session..."); world.mud_handleInput(this.player.roomId, this.player.id, ["look"]); return; } if(this.loggedIn) { world.mud_playerQuit(this.player); this.socket = null; this.loggedIn = false; } } handleInput(input) { if(this.awaitUsername) { this.username = input.join(" "); libs.Telnet.sendBinary(this.socket, libs.Telnet.RequestTurnOnEcho); libs.Telnet.sendString(this.socket, "Password: "); this.awaitUsername = false; this.awaitPassword = true; } else if (this.awaitPassword) { this.password = input.join(" "); this.awaitPassword = false; libs.Telnet.sendBinary(this.socket, libs.Telnet.RequestTurnOffEcho); var userData = world.mud_getUserData(this.username); if(userData != null && world.mud_isValidLogin(userData, this.password)) { libs.Telnet.sendString(this.socket, "\r\nLogging you in...\r\n"); world.mud_playerLoad(this, userData); this.loggedIn = true; } else { this.loginFailed(); } } } loginFailed() { this.failCount++; if(this.failCount >= 3) { this.logout(); return; } libs.Telnet.sendString(this.socket, "\r\nYour login information was not correct.\r\n"); libs.Telnet.sendString(this.socket, "Username: "); this.awaitUsername = true; } receivePlayer(input) { if(this.isReconnecting || !this.loggedIn) { return; } world.mud_handleInput(this.player.roomId, this.player.id, input); } setConnectionTimeout(time) { this.socket.setTimeout(time); } logout() { this.socket.end(); } reconnect(newLogin) { this.socket.end(); this.isReconnecting = true; this.reconnectLogin = newLogin; } };