SQLAccountManager.java 18 KB

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