2
0

SQLAccountManager.java 18 KB

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