SQLAccountManager.java 15 KB

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