telnetlogin.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. module.exports = class TelnetLogin {
  2. constructor(socket) {
  3. this.socket = socket;
  4. this.awaitUsername = false;
  5. this.awaitPassword = false;
  6. this.username = "";
  7. this.password = "";
  8. this.loggedIn = false;
  9. this.player = null;
  10. this.failCount = 0;
  11. this.inputBuffer = "";
  12. this.isReconnecting = false;
  13. this.reconnectLogin = null;
  14. }
  15. attach() {
  16. libs.Telnet.sendString(this.socket, config.welcomeBanner + "\r\n");
  17. libs.Telnet.sendString(this.socket, "Please log in to your account.\r\n");
  18. libs.Telnet.sendString(this.socket, "Username: ");
  19. this.awaitUsername = true;
  20. }
  21. receive(data) {
  22. if(this.serverNegotiations(data)) {
  23. return;
  24. }
  25. this.inputBuffer += data.toString();
  26. if(!(this.inputBuffer.indexOf("\r\n") != -1 || this.inputBuffer.indexOf("\n") != -1 || this.inputBuffer.indexOf("\r") != -1)) {
  27. //did not see a return char, keep building buffer
  28. return;
  29. }
  30. var input = this.inputBuffer.toString().trim().split(" ");
  31. if(!this.loggedIn) {
  32. this.handleInput(input);
  33. } else {
  34. this.receivePlayer(input);
  35. }
  36. this.inputBuffer = "";
  37. }
  38. sendMessage(message){
  39. var terminalWidthPreference = (!this.player.hasOwnProperty("terminalWidthPreference") || this.player.terminalWidthPreference == null) ? 78 : this.player.terminalWidthPreference;
  40. var hintColor = (!this.player.hasOwnProperty("hintColor") || this.player.hintColor == null) ? libs.Color.FgMagenta : libs.Color[this.player.hintColor];
  41. var simpleMessage = message.replace(/\{.*?\}/g, "");
  42. message = libs.Color.Reset + message;
  43. message = message.replace(/\{hint\}/g, hintColor);
  44. message = message.replace(/\{\/hint\}/g, libs.Color.Reset);
  45. message = message.replace(/\{mobile\}/g, libs.Color.FgYellow);
  46. message = message.replace(/\{\/mobile\}/g, libs.Color.Reset);
  47. message = message.replace(/\{item\}/g, libs.Color.FgGreen);
  48. message = message.replace(/\{\/item\}/g, libs.Color.Reset);
  49. message = message.replace(/\{error\}/g, libs.Color.FgRed);
  50. message = message.replace(/\{\/error\}/g, libs.Color.Reset);
  51. message = message.replace(/\{global\}/g, libs.Color.FgCyan);
  52. message = message.replace(/\{\/global\}/g, libs.Color.Reset);
  53. message = message.replace(/\{red\}/g, libs.Color.FgRed);
  54. message = message.replace(/\{\/red\}/g, libs.Color.Reset);
  55. message = message.replace(/\{green\}/g, libs.Color.FgGreen);
  56. message = message.replace(/\{\/green\}/g, libs.Color.Reset);
  57. message = message.replace(/\{yellow\}/g, libs.Color.FgYellow);
  58. message = message.replace(/\{\/yellow\}/g, libs.Color.Reset);
  59. message = message.replace(/\{blue\}/g, libs.Color.FgBlue);
  60. message = message.replace(/\{\/blue\}/g, libs.Color.Reset);
  61. message = message.replace(/\{magenta\}/g, libs.Color.FgMagenta);
  62. message = message.replace(/\{\/magenta\}/g, libs.Color.Reset);
  63. message = message.replace(/\{cyan\}/g, libs.Color.FgCyan);
  64. message = message.replace(/\{\/cyan\}/g, libs.Color.Reset);
  65. message = message.replace(/\{.*?\}/g, "");
  66. var buffer = [], colorBuffer = [];
  67. buffer = simpleMessage.split(" ");
  68. colorBuffer = message.split(" ");
  69. while(buffer.length > 0) {
  70. var wrapString = buffer.shift();
  71. var displayString = colorBuffer.shift();
  72. if(buffer.length > 0) {
  73. while(wrapString.length + buffer[0].length < terminalWidthPreference) {
  74. wrapString += " " + buffer.shift();
  75. displayString += " " + colorBuffer.shift();
  76. if(buffer.length == 0) {
  77. break;
  78. }
  79. }
  80. }
  81. this.socket.write(displayString + "\r\n");
  82. }
  83. }
  84. serverNegotiations(data) {
  85. if(data[0] == libs.Telnet.IAC) {
  86. //TODO: actually listen to the client and negotiate
  87. return true;
  88. }
  89. return false;
  90. }
  91. disconnect() {
  92. if(this.isReconnecting) {
  93. this.player.login = this.reconnectLogin;
  94. this.isReconnecting = false;
  95. this.socket = null;
  96. this.loggedIn = false;
  97. //this.socket = this.reconnectSocket;
  98. //this.reconnectSocket = null;
  99. libs.Output.toMobile(this.player.id, "Reconnecting to existing session...");
  100. world.mud_handleInput(this.player.roomId, this.player.id, ["look"]);
  101. return;
  102. }
  103. if(this.loggedIn) {
  104. world.mud_playerQuit(this.player);
  105. this.socket = null;
  106. this.loggedIn = false;
  107. }
  108. }
  109. handleInput(input) {
  110. if(this.awaitUsername) {
  111. this.username = input.join(" ");
  112. libs.Telnet.sendBinary(this.socket, libs.Telnet.RequestTurnOnEcho);
  113. libs.Telnet.sendString(this.socket, "Password: ");
  114. this.awaitUsername = false;
  115. this.awaitPassword = true;
  116. } else if (this.awaitPassword) {
  117. this.password = input.join(" ");
  118. this.awaitPassword = false;
  119. libs.Telnet.sendBinary(this.socket, libs.Telnet.RequestTurnOffEcho);
  120. var userData = world.mud_getUserData(this.username);
  121. if(userData != null && world.mud_isValidLogin(userData, this.password)) {
  122. libs.Telnet.sendString(this.socket, "\r\nLogging you in...\r\n");
  123. world.mud_playerLoad(this, userData);
  124. this.loggedIn = true;
  125. } else {
  126. this.loginFailed();
  127. }
  128. }
  129. }
  130. loginFailed() {
  131. this.failCount++;
  132. if(this.failCount >= 3) {
  133. this.logout();
  134. return;
  135. }
  136. libs.Telnet.sendString(this.socket, "\r\nYour login information was not correct.\r\n");
  137. libs.Telnet.sendString(this.socket, "Username: ");
  138. this.awaitUsername = true;
  139. }
  140. receivePlayer(input) {
  141. if(this.isReconnecting || !this.loggedIn) {
  142. return;
  143. }
  144. world.mud_handleInput(this.player.roomId, this.player.id, input);
  145. }
  146. setConnectionTimeout(time) {
  147. this.socket.setTimeout(time);
  148. }
  149. logout() {
  150. this.socket.end();
  151. }
  152. reconnect(newLogin) {
  153. this.socket.end();
  154. this.isReconnecting = true;
  155. this.reconnectLogin = newLogin;
  156. }
  157. };