SQLAccountManager.java 15 KB

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