SQLAccountManager.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.accountmanager;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.io.LineNumberReader;
  23. import java.security.MessageDigest;
  24. import java.security.NoSuchAlgorithmException;
  25. import java.sql.PreparedStatement;
  26. import java.sql.ResultSet;
  27. import java.sql.SQLException;
  28. import net.sf.l2j.Base64;
  29. import net.sf.l2j.Config;
  30. import net.sf.l2j.L2DatabaseFactory;
  31. import net.sf.l2j.Server;
  32. /**
  33. * This class SQL Account Manager
  34. *
  35. * @author netimperia
  36. * @version $Revision: 2.3.2.1.2.3 $ $Date: 2005/08/08 22:47:12 $
  37. */
  38. public class SQLAccountManager
  39. {
  40. private static String _uname = "";
  41. private static String _pass = "";
  42. private static String _level = "";
  43. private static String _mode = "";
  44. public static void main(String[] args) throws SQLException, IOException, NoSuchAlgorithmException
  45. {
  46. Server.serverMode = Server.MODE_LOGINSERVER;
  47. Config.load();
  48. System.out.println("Please choose an option:");
  49. System.out.println("");
  50. System.out.println("1 - Create new account or update existing one (change pass and access level).");
  51. System.out.println("2 - Change access level.");
  52. System.out.println("3 - Delete existing account.");
  53. System.out.println("4 - List accounts & access levels.");
  54. System.out.println("5 - Exit.");
  55. LineNumberReader _in = new LineNumberReader(new InputStreamReader(System.in));
  56. while (!(_mode.equals("1") || _mode.equals("2") || _mode.equals("3")
  57. || _mode.equals("4") || _mode.equals("5")) )
  58. {
  59. System.out.print("Your choice: ");
  60. _mode = _in.readLine();
  61. }
  62. if (_mode.equals("1") || _mode.equals("2") || _mode.equals("3"))
  63. {
  64. if (_mode.equals("1") || _mode.equals("2") || _mode.equals("3"))
  65. while (_uname.length() == 0)
  66. {
  67. System.out.print("Username: ");
  68. _uname = _in.readLine().toLowerCase();
  69. }
  70. if (_mode.equals("1"))
  71. while (_pass.length() == 0)
  72. {
  73. System.out.print("Password: ");
  74. _pass = _in.readLine();
  75. }
  76. if (_mode.equals("1") || _mode.equals("2"))
  77. while (_level.length() == 0)
  78. {
  79. System.out.print("Access level: ");
  80. _level = _in.readLine();
  81. }
  82. }
  83. if (_mode.equals("1")) {
  84. // Add or Update
  85. addOrUpdateAccount(_uname,_pass,_level);
  86. } else if(_mode.equals("2")) {
  87. // Change Level
  88. changeAccountLevel(_uname,_level);
  89. } else if(_mode.equals("3")) {
  90. // Delete
  91. System.out.print("Do you really want to delete this account ? Y/N : ");
  92. String yesno = _in.readLine();
  93. if (yesno.equals("Y"))
  94. {
  95. // Yes
  96. deleteAccount(_uname);
  97. }
  98. } else if(_mode.equals("4")) {
  99. // List
  100. printAccInfo();
  101. }
  102. return;
  103. }
  104. private static void printAccInfo() throws SQLException
  105. {
  106. int count = 0;
  107. java.sql.Connection con = null;
  108. con = L2DatabaseFactory.getInstance().getConnection();
  109. PreparedStatement statement = con.prepareStatement("SELECT login, access_level FROM accounts ORDER BY login ASC");
  110. ResultSet rset = statement.executeQuery();
  111. while (rset.next())
  112. {
  113. System.out.println(rset.getString("login") + " -> " + rset.getInt("access_level"));
  114. count++;
  115. }
  116. rset.close();
  117. statement.close();
  118. System.out.println("Number of accounts: " + count + ".");
  119. }
  120. private static void addOrUpdateAccount(String account,String password, String level) throws IOException, SQLException, NoSuchAlgorithmException
  121. {
  122. // Encode Password
  123. MessageDigest md = MessageDigest.getInstance("SHA");
  124. byte[] newpass;
  125. newpass = password.getBytes("UTF-8");
  126. newpass = md.digest(newpass);
  127. // Add to Base
  128. java.sql.Connection con = null;
  129. con = L2DatabaseFactory.getInstance().getConnection();
  130. PreparedStatement statement = con.prepareStatement("REPLACE accounts (login, password, access_level) VALUES (?,?,?)");
  131. statement.setString(1, account);
  132. statement.setString(2, Base64.encodeBytes(newpass));
  133. statement.setString(3, level);
  134. statement.executeUpdate();
  135. statement.close();
  136. }
  137. private static void changeAccountLevel(String account, String level) throws SQLException
  138. {
  139. java.sql.Connection con = null;
  140. con = L2DatabaseFactory.getInstance().getConnection();
  141. // Check Account Exist
  142. PreparedStatement statement = con.prepareStatement("SELECT COUNT(*) FROM accounts WHERE login=?;");
  143. statement.setString(1, account);
  144. ResultSet rset = statement.executeQuery();
  145. if(rset.next()==false) {
  146. System.out.println("False");
  147. } else if(rset.getInt(1)>0) {
  148. // Exist
  149. // Update
  150. statement = con.prepareStatement("UPDATE accounts SET access_level=? WHERE login=?;");
  151. statement.setEscapeProcessing(true);
  152. statement.setString(1, level);
  153. statement.setString(2, account);
  154. statement.executeUpdate();
  155. System.out.println("Account " + account + " has been updated.");
  156. } else {
  157. // Not Exist
  158. System.out.println("Account " + account + " does not exist.");
  159. }
  160. rset.close();
  161. // Close Connection
  162. statement.close();
  163. }
  164. private static void deleteAccount(String account) throws SQLException
  165. {
  166. java.sql.Connection con = null;
  167. con = L2DatabaseFactory.getInstance().getConnection();
  168. // Check Account Exist
  169. PreparedStatement statement = con.prepareStatement("SELECT COUNT(*) FROM accounts WHERE login=?;");
  170. statement.setString(1, account);
  171. ResultSet rset = statement.executeQuery();
  172. if(rset.next()==false) {
  173. System.out.println("False");
  174. rset.close();
  175. } else if(rset.getInt(1)>0) {
  176. rset.close();
  177. // Account exist
  178. // Get Accounts ID
  179. ResultSet rcln;
  180. statement = con.prepareStatement("SELECT obj_Id, char_name, clanid FROM characters WHERE account_name=?;");
  181. statement.setEscapeProcessing(true);
  182. statement.setString(1, account);
  183. rset = statement.executeQuery();
  184. while (rset.next())
  185. {
  186. System.out.println("Deleting character " + rset.getString("char_name") + ".");
  187. // Check If clan leader Remove Clan and remove all from it
  188. statement.close();
  189. statement = con.prepareStatement("SELECT COUNT(*) FROM clan_data WHERE leader_id=?;");
  190. statement.setString(1, rset.getString("clanid"));
  191. rcln = statement.executeQuery();
  192. rcln.next();
  193. if(rcln.getInt(1)>0) {
  194. rcln.close();
  195. // Clan Leader
  196. // Get Clan Name
  197. statement.close();
  198. statement = con.prepareStatement("SELECT clan_name FROM clan_data WHERE leader_id=?;");
  199. statement.setString(1, rset.getString("clanid"));
  200. rcln = statement.executeQuery();
  201. rcln.next();
  202. System.out.println("Deleting clan " + rcln.getString("clan_name") + ".");
  203. // Delete Clan Wars
  204. statement.close();
  205. statement = con.prepareStatement("DELETE FROM clan_wars WHERE clan1=? OR clan2=?;");
  206. statement.setEscapeProcessing(true);
  207. statement.setString(1, rcln.getString("clan_name"));
  208. statement.setString(2, rcln.getString("clan_name"));
  209. statement.executeUpdate();
  210. rcln.close();
  211. // Remove All From clan
  212. statement.close();
  213. statement = con.prepareStatement("UPDATE characters SET clanid=0 WHERE clanid=?;");
  214. statement.setString(1, rset.getString("clanid"));
  215. statement.executeUpdate();
  216. // Delete Clan
  217. statement.close();
  218. statement = con.prepareStatement("DELETE FROM clan_data WHERE clan_id=?;");
  219. statement.setString(1, rset.getString("clanid"));
  220. statement.executeUpdate();
  221. statement.close();
  222. statement = con.prepareStatement("DELETE FROM clan_privs WHERE clan_id=?;");
  223. statement.setString(1, rset.getString("clanid"));
  224. statement.executeUpdate();
  225. statement.close();
  226. statement = con.prepareStatement("DELETE FROM clan_subpledges WHERE clan_id=?;");
  227. statement.setString(1, rset.getString("clanid"));
  228. statement.executeUpdate();
  229. } else {
  230. rcln.close();
  231. }
  232. // skills
  233. statement.close();
  234. statement = con.prepareStatement("DELETE FROM character_skills WHERE char_obj_id=?;");
  235. statement.setString(1, rset.getString("obj_Id"));
  236. statement.executeUpdate();
  237. // shortcuts
  238. statement.close();
  239. statement = con.prepareStatement("DELETE FROM character_shortcuts WHERE char_obj_id=?;");
  240. statement.setString(1, rset.getString("obj_Id"));
  241. statement.executeUpdate();
  242. // items
  243. statement.close();
  244. statement = con.prepareStatement("DELETE FROM items WHERE owner_id=?;");
  245. statement.setString(1, rset.getString("obj_Id"));
  246. statement.executeUpdate();
  247. // recipebook
  248. statement.close();
  249. statement = con.prepareStatement("DELETE FROM character_recipebook WHERE char_id=?;");
  250. statement.setString(1, rset.getString("obj_Id"));
  251. statement.executeUpdate();
  252. // quests
  253. statement.close();
  254. statement = con.prepareStatement("DELETE FROM character_quests WHERE char_id=?;");
  255. statement.setString(1, rset.getString("obj_Id"));
  256. statement.executeUpdate();
  257. // macroses
  258. statement.close();
  259. statement = con.prepareStatement("DELETE FROM character_macroses WHERE char_obj_id=?;");
  260. statement.setString(1, rset.getString("obj_Id"));
  261. statement.executeUpdate();
  262. // friends
  263. statement.close();
  264. statement = con.prepareStatement("DELETE FROM character_friends WHERE char_id=?;");
  265. statement.setString(1, rset.getString("obj_Id"));
  266. statement.executeUpdate();
  267. // merchant_lease
  268. statement.close();
  269. statement = con.prepareStatement("DELETE FROM merchant_lease WHERE player_id=?;");
  270. statement.setString(1, rset.getString("obj_Id"));
  271. statement.executeUpdate();
  272. // boxaccess
  273. statement.close();
  274. statement = con.prepareStatement("DELETE FROM boxaccess WHERE charname=?;");
  275. statement.setString(1, rset.getString("char_name"));
  276. statement.executeUpdate();
  277. // characters
  278. statement.close();
  279. statement = con.prepareStatement("DELETE FROM characters WHERE obj_Id=?;");
  280. statement.setString(1, rset.getString("obj_Id"));
  281. statement.executeUpdate();
  282. }
  283. // Delete Account
  284. statement.close();
  285. statement = con.prepareStatement("DELETE FROM accounts WHERE login=?;");
  286. statement.setEscapeProcessing(true);
  287. statement.setString(1, account);
  288. statement.executeUpdate();
  289. System.out.println("Account " + account + " has been deleted.");
  290. } else {
  291. // Not Exist
  292. System.out.println("Account " + account + " does not exist.");
  293. }
  294. // Close Connection
  295. rset.close();
  296. statement.close();
  297. con.close();
  298. }
  299. }