SQLAccountManager.java 11 KB

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