SQLAccountManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. Connection con = null;
  162. con = L2DatabaseFactory.getInstance().getConnection();
  163. String q = "SELECT login, accessLevel FROM accounts ";
  164. if (m.equals("1"))
  165. {
  166. q = q.concat("WHERE accessLevel < 0");
  167. }
  168. else if (m.equals("2"))
  169. {
  170. q = q.concat("WHERE accessLevel > 0");
  171. }
  172. else if (m.equals("3"))
  173. {
  174. q = q.concat("WHERE accessLevel = 0");
  175. }
  176. q = q.concat(" ORDER BY login ASC");
  177. PreparedStatement statement = con.prepareStatement(q);
  178. ResultSet rset = statement.executeQuery();
  179. while (rset.next())
  180. {
  181. System.out.println(rset.getString("login") + " -> " + rset.getInt("accessLevel"));
  182. count++;
  183. }
  184. rset.close();
  185. statement.close();
  186. L2DatabaseFactory.close(con);
  187. cl.println("functListAccountDisplayed", count);
  188. }
  189. private static void addOrUpdateAccount(String account, String password, String level) throws IOException, SQLException, NoSuchAlgorithmException
  190. {
  191. // Encode Password
  192. MessageDigest md = MessageDigest.getInstance("SHA");
  193. byte[] newpass;
  194. newpass = password.getBytes("UTF-8");
  195. newpass = md.digest(newpass);
  196. // Add to Base
  197. Connection con = null;
  198. con = L2DatabaseFactory.getInstance().getConnection();
  199. PreparedStatement statement = con.prepareStatement("REPLACE accounts (login, password, accessLevel) VALUES (?,?,?)");
  200. statement.setString(1, account);
  201. statement.setString(2, Base64.encodeBytes(newpass));
  202. statement.setString(3, level);
  203. statement.executeUpdate();
  204. statement.close();
  205. L2DatabaseFactory.close(con);
  206. }
  207. private static void changeAccountLevel(String account, String level) throws SQLException
  208. {
  209. Connection con = null;
  210. con = L2DatabaseFactory.getInstance().getConnection();
  211. // Check Account Exist
  212. PreparedStatement statement = con.prepareStatement("SELECT COUNT(*) FROM accounts WHERE login=?;");
  213. statement.setString(1, account);
  214. ResultSet rset = statement.executeQuery();
  215. if (!rset.next())
  216. {
  217. cl.println("falseString");
  218. }
  219. else if (rset.getInt(1) > 0)
  220. {
  221. // Exist
  222. // Update
  223. statement = con.prepareStatement("UPDATE accounts SET accessLevel=? WHERE login=?;");
  224. statement.setEscapeProcessing(true);
  225. statement.setString(1, level);
  226. statement.setString(2, account);
  227. statement.executeUpdate();
  228. cl.println("functAccessLevelUpdated", account);
  229. }
  230. else
  231. {
  232. // Not Exist
  233. cl.println("functAccessLevelNotExist", account);
  234. }
  235. rset.close();
  236. statement.close();
  237. L2DatabaseFactory.close(con);
  238. }
  239. private static void deleteAccount(String account) throws SQLException
  240. {
  241. Connection con = null;
  242. con = L2DatabaseFactory.getInstance().getConnection();
  243. // Check Account Exist
  244. PreparedStatement statement = con.prepareStatement("SELECT COUNT(*) FROM accounts WHERE login=?;");
  245. statement.setString(1, account);
  246. ResultSet rset = statement.executeQuery();
  247. if (!rset.next())
  248. {
  249. cl.println("falseString");
  250. rset.close();
  251. }
  252. else if (rset.getInt(1) > 0)
  253. {
  254. rset.close();
  255. // Account exist
  256. // Get Accounts ID
  257. ResultSet rcln;
  258. statement = con.prepareStatement("SELECT charId, char_name, clanid FROM characters WHERE account_name=?;");
  259. statement.setEscapeProcessing(true);
  260. statement.setString(1, account);
  261. rset = statement.executeQuery();
  262. FastList<String> objIds = new FastList<>();
  263. FastList<String> charNames = new FastList<>();
  264. FastList<String> clanIds = new FastList<>();
  265. while (rset.next())
  266. {
  267. objIds.add(rset.getString("charId"));
  268. charNames.add(rset.getString("char_name"));
  269. clanIds.add(rset.getString("clanid"));
  270. }
  271. rset.close();
  272. for (int index = 0; index < objIds.size(); index++)
  273. {
  274. cl.println("functDeleteAccountChar", charNames.get(index));
  275. // Check If clan leader Remove Clan and remove all from it
  276. statement.close();
  277. statement = con.prepareStatement("SELECT COUNT(*) FROM clan_data WHERE leader_id=?;");
  278. statement.setString(1, clanIds.get(index));
  279. rcln = statement.executeQuery();
  280. rcln.next();
  281. if (rcln.getInt(1) > 0)
  282. {
  283. rcln.close();
  284. // Clan Leader
  285. // Get Clan Name
  286. statement.close();
  287. statement = con.prepareStatement("SELECT clan_name FROM clan_data WHERE leader_id=?;");
  288. statement.setString(1, clanIds.get(index));
  289. rcln = statement.executeQuery();
  290. rcln.next();
  291. String clanName = rcln.getString("clan_name");
  292. cl.println("functDeleteAccountClan", clanName);
  293. // Delete Clan Wars
  294. statement.close();
  295. statement = con.prepareStatement("DELETE FROM clan_wars WHERE clan1=? OR clan2=?;");
  296. statement.setEscapeProcessing(true);
  297. statement.setString(1, clanName);
  298. statement.setString(2, clanName);
  299. statement.executeUpdate();
  300. rcln.close();
  301. // Remove All From clan
  302. statement.close();
  303. statement = con.prepareStatement("UPDATE characters SET clanid=0 WHERE clanid=?;");
  304. statement.setString(1, clanIds.get(index));
  305. statement.executeUpdate();
  306. // Free Clan Halls
  307. statement.close();
  308. statement = con.prepareStatement("UPDATE clanhall SET ownerId=0, paidUntil=0, paid=0 WHERE ownerId=?;");
  309. statement.setString(1, clanIds.get(index));
  310. statement.executeUpdate();
  311. // Delete Clan
  312. statement.close();
  313. statement = con.prepareStatement("DELETE FROM clan_data WHERE clan_id=?;");
  314. statement.setString(1, clanIds.get(index));
  315. statement.executeUpdate();
  316. // Clan privileges
  317. statement.close();
  318. statement = con.prepareStatement("DELETE FROM clan_privs WHERE clan_id=?;");
  319. statement.setString(1, clanIds.get(index));
  320. statement.executeUpdate();
  321. // Clan subpledges
  322. statement.close();
  323. statement = con.prepareStatement("DELETE FROM clan_subpledges WHERE clan_id=?;");
  324. statement.setString(1, clanIds.get(index));
  325. statement.executeUpdate();
  326. // Clan skills
  327. statement.close();
  328. statement = con.prepareStatement("DELETE FROM clan_skills WHERE clan_id=?;");
  329. statement.setString(1, clanIds.get(index));
  330. statement.executeUpdate();
  331. }
  332. else
  333. {
  334. rcln.close();
  335. }
  336. // skills
  337. statement.close();
  338. statement = con.prepareStatement("DELETE FROM character_skills WHERE charId=?;");
  339. statement.setString(1, objIds.get(index));
  340. statement.executeUpdate();
  341. // skills save
  342. statement.close();
  343. statement = con.prepareStatement("DELETE FROM character_skills_save WHERE charId=?;");
  344. statement.setString(1, objIds.get(index));
  345. statement.executeUpdate();
  346. // subclasses
  347. statement.close();
  348. statement = con.prepareStatement("DELETE FROM character_subclasses WHERE charId=?;");
  349. statement.setString(1, objIds.get(index));
  350. statement.executeUpdate();
  351. // shortcuts
  352. statement.close();
  353. statement = con.prepareStatement("DELETE FROM character_shortcuts WHERE charId=?;");
  354. statement.setString(1, objIds.get(index));
  355. statement.executeUpdate();
  356. // items
  357. statement.close();
  358. statement = con.prepareStatement("DELETE FROM items WHERE owner_id=?;");
  359. statement.setString(1, objIds.get(index));
  360. statement.executeUpdate();
  361. // recipebook
  362. statement.close();
  363. statement = con.prepareStatement("DELETE FROM character_recipebook WHERE charId=?;");
  364. statement.setString(1, objIds.get(index));
  365. statement.executeUpdate();
  366. // quests
  367. statement.close();
  368. statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=?;");
  369. statement.setString(1, objIds.get(index));
  370. statement.executeUpdate();
  371. // macroses
  372. statement.close();
  373. statement = con.prepareStatement("DELETE FROM character_macroses WHERE charId=?;");
  374. statement.setString(1, objIds.get(index));
  375. statement.executeUpdate();
  376. // contacts
  377. statement.close();
  378. statement = con.prepareStatement("DELETE FROM character_contacts WHERE charId=? OR contactId=?;");
  379. statement.setString(1, objIds.get(index));
  380. statement.setString(2, objIds.get(index));
  381. statement.executeUpdate();
  382. // friends
  383. statement.close();
  384. statement = con.prepareStatement("DELETE FROM character_friends WHERE charId=? OR friendId=?;");
  385. statement.setString(1, objIds.get(index));
  386. statement.setString(2, objIds.get(index));
  387. statement.executeUpdate();
  388. // merchant_lease
  389. statement.close();
  390. statement = con.prepareStatement("DELETE FROM merchant_lease WHERE player_id=?;");
  391. statement.setString(1, objIds.get(index));
  392. statement.executeUpdate();
  393. // boxaccess
  394. statement.close();
  395. statement = con.prepareStatement("DELETE FROM boxaccess WHERE charname=?;");
  396. statement.setString(1, charNames.get(index));
  397. statement.executeUpdate();
  398. // hennas
  399. statement.close();
  400. statement = con.prepareStatement("DELETE FROM character_hennas WHERE charId=?;");
  401. statement.setString(1, objIds.get(index));
  402. statement.executeUpdate();
  403. // recommends
  404. statement.close();
  405. statement = con.prepareStatement("DELETE FROM character_reco_bonus WHERE charId=?;");
  406. statement.setString(1, objIds.get(index));
  407. statement.executeUpdate();
  408. // ui categories
  409. statement.close();
  410. statement = con.prepareStatement("DELETE FROM character_ui_categories WHERE charId=?;");
  411. statement.setString(1, objIds.get(index));
  412. statement.executeUpdate();
  413. // ui keys
  414. statement.close();
  415. statement = con.prepareStatement("DELETE FROM character_ui_keys WHERE charId=?;");
  416. statement.setString(1, objIds.get(index));
  417. statement.executeUpdate();
  418. // characters
  419. statement.close();
  420. statement = con.prepareStatement("DELETE FROM characters WHERE charId=?;");
  421. statement.setString(1, objIds.get(index));
  422. statement.executeUpdate();
  423. // TODO: delete pets, olympiad/noble/hero stuff
  424. }
  425. // characters
  426. statement.close();
  427. statement = con.prepareStatement("DELETE FROM account_gsdata WHERE account_name=?;");
  428. statement.setString(1, account);
  429. statement.executeUpdate();
  430. // Delete Account
  431. statement.close();
  432. statement = con.prepareStatement("DELETE FROM accounts WHERE login=?;");
  433. statement.setEscapeProcessing(true);
  434. statement.setString(1, account);
  435. statement.executeUpdate();
  436. cl.println("functDeleteAccountComplete", account);
  437. }
  438. else
  439. {
  440. // Not Exist
  441. cl.println("functDeleteAccountNotExist", account);
  442. }
  443. // Close Connection
  444. statement.close();
  445. L2DatabaseFactory.close(con);
  446. }
  447. }