SQLAccountManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 com.l2jserver.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.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import javolution.util.FastList;
  26. import com.l2jserver.Base64;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.Server;
  30. /**
  31. * This class SQL Account Manager
  32. *
  33. * @author netimperia
  34. * @version $Revision: 2.3.2.1.2.3 $ $Date: 2005/08/08 22:47:12 $
  35. */
  36. public class SQLAccountManager
  37. {
  38. private static String _uname = "";
  39. private static String _pass = "";
  40. private static String _level = "";
  41. private static String _mode = "";
  42. public static void main(String[] args) throws SQLException, IOException, NoSuchAlgorithmException
  43. {
  44. Server.serverMode = Server.MODE_LOGINSERVER;
  45. Config.load();
  46. while (true)
  47. {
  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") || _mode.equals("4") || _mode.equals("5")))
  57. {
  58. System.out.print("Your choice: ");
  59. _mode = _in.readLine();
  60. }
  61. if (_mode.equals("1") || _mode.equals("2") || _mode.equals("3"))
  62. {
  63. if (_mode.equals("1") || _mode.equals("2"))
  64. {
  65. while (_uname.trim().length() == 0)
  66. {
  67. System.out.print("Username: ");
  68. _uname = _in.readLine().toLowerCase();
  69. }
  70. }
  71. else if (_mode.equals("3"))
  72. {
  73. while (_uname.trim().length() == 0)
  74. {
  75. System.out.print("Account name: ");
  76. _uname = _in.readLine().toLowerCase();
  77. }
  78. }
  79. if (_mode.equals("1"))
  80. {
  81. while (_pass.trim().length() == 0)
  82. {
  83. System.out.print("Password: ");
  84. _pass = _in.readLine();
  85. }
  86. }
  87. if (_mode.equals("1") || _mode.equals("2"))
  88. {
  89. while (_level.trim().length() == 0)
  90. {
  91. System.out.print("Access level: ");
  92. _level = _in.readLine();
  93. }
  94. }
  95. }
  96. if (_mode.equals("1"))
  97. {
  98. // Add or Update
  99. addOrUpdateAccount(_uname.trim(), _pass.trim(), _level.trim());
  100. }
  101. else if (_mode.equals("2"))
  102. {
  103. // Change Level
  104. changeAccountLevel(_uname.trim(), _level.trim());
  105. }
  106. else if (_mode.equals("3"))
  107. {
  108. // Delete
  109. System.out.print("Do you really want to delete this account ? Y/N : ");
  110. String yesno = _in.readLine();
  111. if (yesno != null && yesno.equalsIgnoreCase("Y"))
  112. deleteAccount(_uname.trim());
  113. else
  114. System.out.println("Deletion cancelled");
  115. }
  116. else if (_mode.equals("4"))
  117. {
  118. // List
  119. _mode = "";
  120. System.out.println("");
  121. System.out.println("Please choose a listing mode:");
  122. System.out.println("");
  123. System.out.println("1 - Banned accounts only (accessLevel < 0)");
  124. System.out.println("2 - GM/privileged accounts (accessLevel > 0)");
  125. System.out.println("3 - Regular accounts only (accessLevel = 0)");
  126. System.out.println("4 - List all");
  127. while (!(_mode.equals("1") || _mode.equals("2") || _mode.equals("3") || _mode.equals("4")))
  128. {
  129. System.out.print("Your choice: ");
  130. _mode = _in.readLine();
  131. }
  132. System.out.println("");
  133. printAccInfo(_mode);
  134. }
  135. else if (_mode.equals("5"))
  136. {
  137. System.exit(0);
  138. }
  139. _uname = "";
  140. _pass = "";
  141. _level = "";
  142. _mode = "";
  143. System.out.println();
  144. }
  145. }
  146. private static void printAccInfo(String m) throws SQLException
  147. {
  148. int count = 0;
  149. Connection con = null;
  150. con = L2DatabaseFactory.getInstance().getConnection();
  151. String q = "SELECT login, accessLevel FROM accounts ";
  152. if (m.equals("1"))
  153. q = q.concat("WHERE accessLevel < 0");
  154. else if (m.equals("2"))
  155. q = q.concat("WHERE accessLevel > 0");
  156. else if (m.equals("3"))
  157. q = q.concat("WHERE accessLevel = 0");
  158. q = q.concat(" ORDER BY login ASC");
  159. PreparedStatement statement = con.prepareStatement(q);
  160. ResultSet rset = statement.executeQuery();
  161. while (rset.next())
  162. {
  163. System.out.println(rset.getString("login") + " -> " + rset.getInt("accessLevel"));
  164. count++;
  165. }
  166. rset.close();
  167. statement.close();
  168. L2DatabaseFactory.close(con);
  169. System.out.println("Displayed accounts: " + count + ".");
  170. }
  171. private static void addOrUpdateAccount(String account, String password, String level) throws IOException, SQLException, NoSuchAlgorithmException
  172. {
  173. // Encode Password
  174. MessageDigest md = MessageDigest.getInstance("SHA");
  175. byte[] newpass;
  176. newpass = password.getBytes("UTF-8");
  177. newpass = md.digest(newpass);
  178. // Add to Base
  179. Connection con = null;
  180. con = L2DatabaseFactory.getInstance().getConnection();
  181. PreparedStatement statement = con.prepareStatement("REPLACE accounts (login, password, accessLevel) VALUES (?,?,?)");
  182. statement.setString(1, account);
  183. statement.setString(2, Base64.encodeBytes(newpass));
  184. statement.setString(3, level);
  185. statement.executeUpdate();
  186. statement.close();
  187. L2DatabaseFactory.close(con);
  188. }
  189. private static void changeAccountLevel(String account, String level) throws SQLException
  190. {
  191. Connection con = null;
  192. con = L2DatabaseFactory.getInstance().getConnection();
  193. // Check Account Exist
  194. PreparedStatement statement = con.prepareStatement("SELECT COUNT(*) FROM accounts WHERE login=?;");
  195. statement.setString(1, account);
  196. ResultSet rset = statement.executeQuery();
  197. if (!rset.next())
  198. {
  199. System.out.println("False");
  200. }
  201. else if (rset.getInt(1) > 0)
  202. {
  203. // Exist
  204. // Update
  205. statement = con.prepareStatement("UPDATE accounts SET accessLevel=? WHERE login=?;");
  206. statement.setEscapeProcessing(true);
  207. statement.setString(1, level);
  208. statement.setString(2, account);
  209. statement.executeUpdate();
  210. System.out.println("Account " + account + " has been updated.");
  211. }
  212. else
  213. {
  214. // Not Exist
  215. System.out.println("Account " + account + " does not exist.");
  216. }
  217. rset.close();
  218. statement.close();
  219. L2DatabaseFactory.close(con);
  220. }
  221. private static void deleteAccount(String account) throws SQLException
  222. {
  223. Connection con = null;
  224. con = L2DatabaseFactory.getInstance().getConnection();
  225. // Check Account Exist
  226. PreparedStatement statement = con.prepareStatement("SELECT COUNT(*) FROM accounts WHERE login=?;");
  227. statement.setString(1, account);
  228. ResultSet rset = statement.executeQuery();
  229. if (!rset.next())
  230. {
  231. System.out.println("False");
  232. rset.close();
  233. }
  234. else if (rset.getInt(1) > 0)
  235. {
  236. rset.close();
  237. // Account exist
  238. // Get Accounts ID
  239. ResultSet rcln;
  240. statement = con.prepareStatement("SELECT charId, char_name, clanid FROM characters WHERE account_name=?;");
  241. statement.setEscapeProcessing(true);
  242. statement.setString(1, account);
  243. rset = statement.executeQuery();
  244. FastList<String> objIds = new FastList<String>();
  245. FastList<String> charNames = new FastList<String>();
  246. FastList<String> clanIds = new FastList<String>();
  247. while (rset.next())
  248. {
  249. objIds.add(rset.getString("charId"));
  250. charNames.add(rset.getString("char_name"));
  251. clanIds.add(rset.getString("clanid"));
  252. }
  253. rset.close();
  254. for (int index = 0; index < objIds.size(); index++)
  255. {
  256. System.out.println("Deleting character " + charNames.get(index) + ".");
  257. // Check If clan leader Remove Clan and remove all from it
  258. statement.close();
  259. statement = con.prepareStatement("SELECT COUNT(*) FROM clan_data WHERE leader_id=?;");
  260. statement.setString(1, clanIds.get(index));
  261. rcln = statement.executeQuery();
  262. rcln.next();
  263. if (rcln.getInt(1) > 0)
  264. {
  265. rcln.close();
  266. // Clan Leader
  267. // Get Clan Name
  268. statement.close();
  269. statement = con.prepareStatement("SELECT clan_name FROM clan_data WHERE leader_id=?;");
  270. statement.setString(1, clanIds.get(index));
  271. rcln = statement.executeQuery();
  272. rcln.next();
  273. String clanName = rcln.getString("clan_name");
  274. System.out.println("Deleting clan " + clanName + ".");
  275. // Delete Clan Wars
  276. statement.close();
  277. statement = con.prepareStatement("DELETE FROM clan_wars WHERE clan1=? OR clan2=?;");
  278. statement.setEscapeProcessing(true);
  279. statement.setString(1, clanName);
  280. statement.setString(2, clanName);
  281. statement.executeUpdate();
  282. rcln.close();
  283. // Remove All From clan
  284. statement.close();
  285. statement = con.prepareStatement("UPDATE characters SET clanid=0 WHERE clanid=?;");
  286. statement.setString(1, clanIds.get(index));
  287. statement.executeUpdate();
  288. // Free Clan Halls
  289. statement.close();
  290. statement = con.prepareStatement("UPDATE clanhall SET ownerId=0, paidUntil=0, paid=0 WHERE ownerId=?;");
  291. statement.setString(1, clanIds.get(index));
  292. statement.executeUpdate();
  293. // Delete Clan
  294. statement.close();
  295. statement = con.prepareStatement("DELETE FROM clan_data WHERE clan_id=?;");
  296. statement.setString(1, clanIds.get(index));
  297. statement.executeUpdate();
  298. //Clan privileges
  299. statement.close();
  300. statement = con.prepareStatement("DELETE FROM clan_privs WHERE clan_id=?;");
  301. statement.setString(1, clanIds.get(index));
  302. statement.executeUpdate();
  303. //Clan subpledges
  304. statement.close();
  305. statement = con.prepareStatement("DELETE FROM clan_subpledges WHERE clan_id=?;");
  306. statement.setString(1, clanIds.get(index));
  307. statement.executeUpdate();
  308. //Clan skills
  309. statement.close();
  310. statement = con.prepareStatement("DELETE FROM clan_skills WHERE clan_id=?;");
  311. statement.setString(1, clanIds.get(index));
  312. statement.executeUpdate();
  313. }
  314. else
  315. rcln.close();
  316. // skills
  317. statement.close();
  318. statement = con.prepareStatement("DELETE FROM character_skills WHERE charId=?;");
  319. statement.setString(1, objIds.get(index));
  320. statement.executeUpdate();
  321. // skills save
  322. statement.close();
  323. statement = con.prepareStatement("DELETE FROM character_skills_save WHERE charId=?;");
  324. statement.setString(1, objIds.get(index));
  325. statement.executeUpdate();
  326. // subclasses
  327. statement.close();
  328. statement = con.prepareStatement("DELETE FROM character_subclasses WHERE charId=?;");
  329. statement.setString(1, objIds.get(index));
  330. statement.executeUpdate();
  331. // shortcuts
  332. statement.close();
  333. statement = con.prepareStatement("DELETE FROM character_shortcuts WHERE charId=?;");
  334. statement.setString(1, objIds.get(index));
  335. statement.executeUpdate();
  336. // items
  337. statement.close();
  338. statement = con.prepareStatement("DELETE FROM items WHERE owner_id=?;");
  339. statement.setString(1, objIds.get(index));
  340. statement.executeUpdate();
  341. // recipebook
  342. statement.close();
  343. statement = con.prepareStatement("DELETE FROM character_recipebook WHERE charId=?;");
  344. statement.setString(1, objIds.get(index));
  345. statement.executeUpdate();
  346. // quests
  347. statement.close();
  348. statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=?;");
  349. statement.setString(1, objIds.get(index));
  350. statement.executeUpdate();
  351. // macroses
  352. statement.close();
  353. statement = con.prepareStatement("DELETE FROM character_macroses WHERE charId=?;");
  354. statement.setString(1, objIds.get(index));
  355. statement.executeUpdate();
  356. // contacts
  357. statement.close();
  358. statement = con.prepareStatement("DELETE FROM character_contacts WHERE charId=? OR contactId=?;");
  359. statement.setString(1, objIds.get(index));
  360. statement.setString(2, objIds.get(index));
  361. statement.executeUpdate();
  362. // friends
  363. statement.close();
  364. statement = con.prepareStatement("DELETE FROM character_friends WHERE charId=? OR friendId=?;");
  365. statement.setString(1, objIds.get(index));
  366. statement.setString(2, objIds.get(index));
  367. statement.executeUpdate();
  368. // merchant_lease
  369. statement.close();
  370. statement = con.prepareStatement("DELETE FROM merchant_lease WHERE player_id=?;");
  371. statement.setString(1, objIds.get(index));
  372. statement.executeUpdate();
  373. // boxaccess
  374. statement.close();
  375. statement = con.prepareStatement("DELETE FROM boxaccess WHERE charname=?;");
  376. statement.setString(1, charNames.get(index));
  377. statement.executeUpdate();
  378. // hennas
  379. statement.close();
  380. statement = con.prepareStatement("DELETE FROM character_hennas WHERE charId=?;");
  381. statement.setString(1, objIds.get(index));
  382. statement.executeUpdate();
  383. // recommends
  384. statement.close();
  385. statement = con.prepareStatement("DELETE FROM character_reco_bonus WHERE charId=?;");
  386. statement.setString(1, objIds.get(index));
  387. statement.executeUpdate();
  388. // ui categories
  389. statement.close();
  390. statement = con.prepareStatement("DELETE FROM character_ui_categories WHERE charId=?;");
  391. statement.setString(1, objIds.get(index));
  392. statement.executeUpdate();
  393. // ui keys
  394. statement.close();
  395. statement = con.prepareStatement("DELETE FROM character_ui_keys WHERE charId=?;");
  396. statement.setString(1, objIds.get(index));
  397. statement.executeUpdate();
  398. // characters
  399. statement.close();
  400. statement = con.prepareStatement("DELETE FROM characters WHERE charId=?;");
  401. statement.setString(1, objIds.get(index));
  402. statement.executeUpdate();
  403. // TODO: delete pets, olympiad/noble/hero stuff
  404. }
  405. // characters
  406. statement.close();
  407. statement = con.prepareStatement("DELETE FROM account_gsdata WHERE account_name=?;");
  408. statement.setString(1, account);
  409. statement.executeUpdate();
  410. // Delete Account
  411. statement.close();
  412. statement = con.prepareStatement("DELETE FROM accounts WHERE login=?;");
  413. statement.setEscapeProcessing(true);
  414. statement.setString(1, account);
  415. statement.executeUpdate();
  416. System.out.println("Account " + account + " has been deleted.");
  417. }
  418. else
  419. {
  420. // Not Exist
  421. System.out.println("Account " + account + " does not exist.");
  422. }
  423. // Close Connection
  424. statement.close();
  425. L2DatabaseFactory.close(con);
  426. }
  427. }