Guest User

Untitled

a guest
Sep 14th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. * This program is free software: you can redistribute it and/or modify it under
  2. * the terms of the GNU General Public License as published by the Free Software
  3. * Foundation, either version 3 of the License, or (at your option) any later
  4. * version.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  9. * details.
  10. *
  11. * You should have received a copy of the GNU General Public License along with
  12. * this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package net.sf.l2j.gameserver.model.actor.instance;
  15.  
  16. import java.nio.charset.StandardCharsets;
  17. import java.security.MessageDigest;
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.util.Base64;
  21. import java.util.StringTokenizer;
  22.  
  23. import net.sf.l2j.commons.pool.ThreadPool;
  24.  
  25. import net.sf.l2j.commons.pool.ConnectionPool;
  26.  
  27. import net.sf.l2j.gameserver.model.actor.Player;
  28. import net.sf.l2j.gameserver.model.actor.template.NpcTemplate;
  29. import net.sf.l2j.gameserver.network.SystemMessageId;
  30. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  31. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  32.  
  33. /**
  34. * @author SweeTs
  35. */
  36. public class PasswordManager extends Folk
  37. {
  38. public PasswordManager(int objectId, NpcTemplate template)
  39. {
  40. super(objectId, template);
  41. }
  42.  
  43. @Override
  44. public void onBypassFeedback(Player player, String command)
  45. {
  46. if (command.startsWith("change_password"))
  47. {
  48. StringTokenizer st = new StringTokenizer(command);
  49. st.nextToken();
  50.  
  51. String newPass = "";
  52. String repeatNewPass = "";
  53.  
  54. try
  55. {
  56. if (st.hasMoreTokens())
  57. {
  58. newPass = st.nextToken();
  59. repeatNewPass = st.nextToken();
  60. }
  61. }
  62. catch (Exception e)
  63. {
  64. player.sendMessage("Please fill all the blanks before requesting for a password change.");
  65. return;
  66. }
  67.  
  68. if (!conditions(newPass, repeatNewPass, player))
  69. return;
  70.  
  71. changePassword(newPass, repeatNewPass, player);
  72. }
  73. }
  74.  
  75. private static boolean conditions(String newPass, String repeatNewPass, Player player)
  76. {
  77. if (newPass.length() < 3)
  78. {
  79. player.sendMessage("The new password is too short!");
  80. return false;
  81. }
  82. else if (newPass.length() > 45)
  83. {
  84. player.sendMessage("The new password is too long!");
  85. return false;
  86. }
  87. else if (!newPass.equals(repeatNewPass))
  88. {
  89. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.PASSWORD_ENTERED_INCORRECT2));
  90. return false;
  91. }
  92.  
  93. return true;
  94. }
  95.  
  96. @Override
  97. public void showChatWindow(Player activeChar)
  98. {
  99. final NpcHtmlMessage html = new NpcHtmlMessage(0);
  100. final StringBuilder sb = new StringBuilder();
  101.  
  102. sb.append("<html><title>Account Manager</title>");
  103. sb.append("<body><center>");
  104. sb.append("<img src=\"L2UI_CH3.herotower_deco\" width=256 height=32><br>");
  105. sb.append("New password: <edit var=\"new\" width=100 height=15><br>");
  106. sb.append("Repeat: <edit var=\"repeatnew\" width=100 height=15><br>");
  107. sb.append("<img src=\"L2UI_CH3.herotower_deco\" width=256 height=32><br><br>");
  108. sb.append("<a action=\"bypass -h npc_%objectId%_change_password $new $repeatnew\">Change password</a>");
  109. sb.append("</center></body></html>");
  110.  
  111. html.setHtml(sb.toString());
  112. html.replace("%objectId%", getObjectId());
  113. activeChar.sendPacket(html);
  114. }
  115.  
  116. private static void changePassword(String newPass, String repeatNewPass, Player activeChar)
  117. {
  118. try (Connection con = ConnectionPool.getConnection(); PreparedStatement ps = con.prepareStatement("UPDATE accounts SET password=? WHERE login=?"))
  119. {
  120. byte[] newPassword = MessageDigest.getInstance("SHA").digest(newPass.getBytes(StandardCharsets.UTF_8));
  121.  
  122. ps.setString(1, Base64.getEncoder().encodeToString(newPassword));
  123. ps.setString(2, activeChar.getAccountName());
  124. ps.executeUpdate();
  125.  
  126. activeChar.sendMessage("Congratulations! Your password has been changed. You will now be disconnected for security reasons. Please login again.");
  127. ThreadPool.schedule(() -> activeChar.logout(false), 3000);
  128. }
  129. catch (Exception e)
  130. {
  131. LOGGER.info("There was an error while updating account:" + e);
  132. }
  133. }
  134. }
Add Comment
Please, Sign In to add comment