2
0

SQLAccountManager.java 18 KB

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