Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 56.71 KB | None | 0 0
  1. /*
  2. This file is part of the OdinMS Maple Story Server
  3. Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc>
  4. Matthias Butz <matze@odinms.de>
  5. Jan Christian Meyer <vimes@odinms.de>
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU Affero General Public License version 3
  9. as published by the Free Software Foundation. You may not use, modify
  10. or distribute this program under any other version of the
  11. GNU Affero General Public License.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU Affero General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Affero General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package client;
  22.  
  23. import handling.cashshop.CashShopServer;
  24. import handling.channel.ChannelServer;
  25. import handling.login.LoginServer;
  26. import handling.world.MapleMessengerCharacter;
  27. import handling.world.MapleParty;
  28. import handling.world.MaplePartyCharacter;
  29. import handling.world.PartyOperation;
  30. import handling.world.World;
  31. import handling.world.family.MapleFamilyCharacter;
  32. import handling.world.guild.MapleGuildCharacter;
  33. import handling.world.sidekick.MapleSidekick;
  34.  
  35. import java.io.Serializable;
  36. import java.sql.Connection;
  37. import java.sql.PreparedStatement;
  38. import java.sql.ResultSet;
  39. import java.sql.SQLException;
  40. import java.sql.Timestamp;
  41. import java.util.Calendar;
  42. import java.util.Collections;
  43. import java.util.HashMap;
  44. import java.util.HashSet;
  45. import java.util.Iterator;
  46. import java.util.LinkedList;
  47. import java.util.List;
  48. import java.util.Map;
  49. import java.util.Set;
  50. import java.util.concurrent.ScheduledFuture;
  51. import java.util.concurrent.locks.Lock;
  52. import java.util.concurrent.locks.ReentrantLock;
  53.  
  54. import javax.script.ScriptEngine;
  55.  
  56. import org.apache.mina.common.IoSession;
  57.  
  58. import server.Timer.PingTimer;
  59. import server.maps.MapleMap;
  60. import server.quest.MapleQuest;
  61. import server.shops.IMaplePlayerShop;
  62. import tools.FileoutputUtil;
  63. import tools.MapleAESOFB;
  64. import tools.MaplePacketCreator;
  65. import tools.packet.LoginPacket;
  66. import constants.GameConstants;
  67. import constants.ServerConstants;
  68. import database.DatabaseConnection;
  69. import database.DatabaseException;
  70.  
  71. public class MapleClient implements Serializable {
  72.  
  73. private static final long serialVersionUID = 9179541993413738569L;
  74. public static final byte LOGIN_NOTLOGGEDIN = 0,
  75. LOGIN_SERVER_TRANSITION = 1,
  76. LOGIN_LOGGEDIN = 2,
  77. CHANGE_CHANNEL = 3;
  78. public static final int DEFAULT_CHARSLOT = 6;
  79. public static final String CLIENT_KEY = "CLIENT";
  80. private transient MapleAESOFB send, receive;
  81. private transient IoSession session;
  82. private MapleCharacter player;
  83. private int channel = 1, accId = -1, world, birthday;
  84. private int charslots = DEFAULT_CHARSLOT;
  85. private boolean loggedIn = false, serverTransition = false;
  86. private transient Calendar tempban = null;
  87. private String accountName;
  88. private transient String AsDigi;
  89. private transient boolean firstlogin;
  90. private transient long lastPong = 0, lastPing = 0;
  91. private boolean monitored = false, receiving = true;
  92. private boolean gm;
  93. private byte greason = 1, gender = -1, gmlvl = -1;
  94. public transient short loginAttempt = 0;
  95. private transient List<Integer> allowedChar = new LinkedList<Integer>();
  96. private transient Set<String> macs = new HashSet<String>();
  97. private transient Map<String, ScriptEngine> engines = new HashMap<String, ScriptEngine>();
  98. private transient ScheduledFuture<?> idleTask = null;
  99. private transient String secondPassword, salt2, tempIP = ""; // To be used only on login
  100. private final transient Lock mutex = new ReentrantLock(true);
  101. private final transient Lock npc_mutex = new ReentrantLock();
  102. private long lastNpcClick = 0;
  103. private final static Lock login_mutex = new ReentrantLock(true);
  104.  
  105. public MapleClient(final MapleAESOFB send, final MapleAESOFB receive, final IoSession session) {
  106. this.send = send;
  107. this.receive = receive;
  108. this.session = session;
  109. }
  110.  
  111. public final MapleAESOFB getReceiveCrypto() {
  112. return receive;
  113. }
  114.  
  115. public final MapleAESOFB getSendCrypto() {
  116. return send;
  117. }
  118.  
  119. public final IoSession getSession() {
  120. return session;
  121. }
  122.  
  123. public final Lock getLock() {
  124. return mutex;
  125. }
  126.  
  127. public final Lock getNPCLock() {
  128. return npc_mutex;
  129. }
  130.  
  131. public final MapleCharacter getPlayer() {
  132. return player;
  133. }
  134.  
  135. public void setPlayer(final MapleCharacter player) {
  136. this.player = player;
  137. }
  138.  
  139. public void createdChar(final int id) {
  140. allowedChar.add(id);
  141. }
  142.  
  143. public final boolean login_Auth(final int id) {
  144. return allowedChar.contains(id);
  145. }
  146.  
  147. public final List<MapleCharacter> loadCharacters(final int serverId) { // TODO make this less costly zZz
  148. final List<MapleCharacter> chars = new LinkedList<MapleCharacter>();
  149.  
  150. for (final CharNameAndId cni : loadCharactersInternal(serverId)) {
  151. final MapleCharacter chr = MapleCharacter.loadCharFromDB(cni.id, this, false);
  152. if (chr.isSuperGM() && !ServerConstants.isEligible(getSessionIPAddress())) {
  153. continue;
  154. }
  155. chars.add(chr);
  156. if (!login_Auth(chr.getId())) {
  157. allowedChar.add(chr.getId());
  158. }
  159. }
  160. return chars;
  161. }
  162.  
  163. public boolean canMakeCharacter(final int serverId) {
  164. return loadCharactersSize(serverId) < getCharacterSlots();
  165. }
  166.  
  167. public List<String> loadCharacterNames(final int serverId) {
  168. final List<String> chars = new LinkedList<String>();
  169. for (final CharNameAndId cni : loadCharactersInternal(serverId)) {
  170. chars.add(cni.name);
  171. }
  172. return chars;
  173. }
  174.  
  175. private List<CharNameAndId> loadCharactersInternal(final int serverId) {
  176. final List<CharNameAndId> chars = new LinkedList<CharNameAndId>();
  177. try {
  178. final Connection con = DatabaseConnection.getConnection();
  179. final PreparedStatement ps = con.prepareStatement("SELECT id, name, gm FROM characters WHERE accountid = ? AND world = ?");
  180. ps.setInt(1, accId);
  181. ps.setInt(2, serverId);
  182.  
  183. final ResultSet rs = ps.executeQuery();
  184. while (rs.next()) {
  185. if (rs.getInt("gm") >= ServerConstants.PlayerGMRank.SUPERGM.getLevel() && !ServerConstants.isEligible(getSessionIPAddress())) {
  186. continue;
  187. }
  188. chars.add(new CharNameAndId(rs.getString("name"), rs.getInt("id")));
  189. LoginServer.getLoginAuth(rs.getInt("id"));
  190. }
  191. rs.close();
  192. ps.close();
  193. } catch (final SQLException e) {
  194. System.err.println("error loading characters internal");
  195. e.printStackTrace();
  196. }
  197. return chars;
  198. }
  199.  
  200. private int loadCharactersSize(final int serverId) {
  201. int chars = 0;
  202. try {
  203. final Connection con = DatabaseConnection.getConnection();
  204. final PreparedStatement ps = con.prepareStatement("SELECT count(*) FROM characters WHERE accountid = ? AND world = ?");
  205. ps.setInt(1, accId);
  206. ps.setInt(2, serverId);
  207.  
  208. final ResultSet rs = ps.executeQuery();
  209. if (rs.next()) {
  210. chars = rs.getInt(1);
  211. }
  212. rs.close();
  213. ps.close();
  214. } catch (final SQLException e) {
  215. System.err.println("error loading characters internal");
  216. e.printStackTrace();
  217. }
  218. return chars;
  219. }
  220.  
  221. public boolean isLoggedIn() {
  222. return loggedIn && accId >= 0;
  223. }
  224.  
  225. private Calendar getTempBanCalendar(final ResultSet rs) throws SQLException {
  226. final Calendar lTempban = Calendar.getInstance();
  227. if (rs.getLong("tempban") == 0) { // basically if timestamp in db is 0000-00-00
  228. lTempban.setTimeInMillis(0);
  229. return lTempban;
  230. }
  231. final Calendar today = Calendar.getInstance();
  232. lTempban.setTimeInMillis(rs.getTimestamp("tempban").getTime());
  233. if (today.getTimeInMillis() < lTempban.getTimeInMillis()) {
  234. return lTempban;
  235. }
  236.  
  237. lTempban.setTimeInMillis(0);
  238. return lTempban;
  239. }
  240.  
  241. public Calendar getTempBanCalendar() {
  242. return tempban;
  243. }
  244.  
  245. public byte getBanReason() {
  246. return greason;
  247. }
  248.  
  249. public boolean hasBannedIP() {
  250. boolean ret = false;
  251. try {
  252. final Connection con = DatabaseConnection.getConnection();
  253. final PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM ipbans WHERE ? LIKE CONCAT(ip, '%')");
  254. ps.setString(1, getSessionIPAddress());
  255. final ResultSet rs = ps.executeQuery();
  256. rs.next();
  257. if (rs.getInt(1) > 0) {
  258. ret = true;
  259. }
  260. rs.close();
  261. ps.close();
  262. } catch (final SQLException ex) {
  263. System.err.println("Error checking ip bans" + ex);
  264. }
  265. return ret;
  266. }
  267.  
  268. public boolean hasBannedMac() {
  269. if (macs.isEmpty()) {
  270. return false;
  271. }
  272. boolean ret = false;
  273. int i = 0;
  274. try {
  275. final Connection con = DatabaseConnection.getConnection();
  276. final StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM macbans WHERE mac IN (");
  277. for (i = 0; i < macs.size(); i++) {
  278. sql.append("?");
  279. if (i != macs.size() - 1) {
  280. sql.append(", ");
  281. }
  282. }
  283. sql.append(")");
  284. final PreparedStatement ps = con.prepareStatement(sql.toString());
  285. i = 0;
  286. for (final String mac : macs) {
  287. i++;
  288. ps.setString(i, mac);
  289. }
  290. final ResultSet rs = ps.executeQuery();
  291. rs.next();
  292. if (rs.getInt(1) > 0) {
  293. ret = true;
  294. }
  295. rs.close();
  296. ps.close();
  297. } catch (final SQLException ex) {
  298. System.err.println("Error checking mac bans" + ex);
  299. }
  300. return ret;
  301. }
  302.  
  303. private void loadMacsIfNescessary() throws SQLException {
  304. if (macs.isEmpty()) {
  305. final Connection con = DatabaseConnection.getConnection();
  306. final PreparedStatement ps = con.prepareStatement("SELECT macs FROM accounts WHERE id = ?");
  307. ps.setInt(1, accId);
  308. final ResultSet rs = ps.executeQuery();
  309. if (rs.next()) {
  310. if (rs.getString("macs") != null) {
  311. final String[] macData = rs.getString("macs").split(", ");
  312. for (final String mac : macData) {
  313. if (!mac.equals("")) {
  314. macs.add(mac);
  315. }
  316. }
  317. }
  318. } else {
  319. rs.close();
  320. ps.close();
  321. throw new RuntimeException("No valid account associated with this client.");
  322. }
  323. rs.close();
  324. ps.close();
  325. }
  326. }
  327.  
  328. public void banMacs() {
  329. try {
  330. loadMacsIfNescessary();
  331. if (this.macs.size() > 0) {
  332. final String[] macBans = new String[this.macs.size()];
  333. int z = 0;
  334. for (final String mac : this.macs) {
  335. macBans[z] = mac;
  336. z++;
  337. }
  338. banMacs(macBans);
  339. }
  340. } catch (final SQLException e) {
  341. e.printStackTrace();
  342. }
  343. }
  344.  
  345. public static final void banMacs(final String[] macs) {
  346. final Connection con = DatabaseConnection.getConnection();
  347. try {
  348. final List<String> filtered = new LinkedList<String>();
  349. PreparedStatement ps = con.prepareStatement("SELECT filter FROM macfilters");
  350. final ResultSet rs = ps.executeQuery();
  351. while (rs.next()) {
  352. filtered.add(rs.getString("filter"));
  353. }
  354. rs.close();
  355. ps.close();
  356.  
  357. ps = con.prepareStatement("INSERT INTO macbans (mac) VALUES (?)");
  358. for (final String mac : macs) {
  359. boolean matched = false;
  360. for (final String filter : filtered) {
  361. if (mac.matches(filter)) {
  362. matched = true;
  363. break;
  364. }
  365. }
  366. if (!matched) {
  367. ps.setString(1, mac);
  368. try {
  369. ps.executeUpdate();
  370. } catch (final SQLException e) {
  371. // can fail because of UNIQUE key, we dont care
  372. }
  373. }
  374. }
  375. ps.close();
  376. } catch (final SQLException e) {
  377. System.err.println("Error banning MACs" + e);
  378. }
  379. }
  380.  
  381. /**
  382. * Returns 0 on success, a state to be used for
  383. * {@link MaplePacketCreator#getLoginFailed(int)} otherwise.
  384. *
  385. * @param success
  386. * @return The state of the login.
  387. */
  388. public int finishLogin() {
  389. login_mutex.lock();
  390. try {
  391. final byte state = getLoginState();
  392. if (state > MapleClient.LOGIN_NOTLOGGEDIN) { // already loggedin
  393. loggedIn = false;
  394. return 7;
  395. }
  396. updateLoginState(MapleClient.LOGIN_LOGGEDIN, getSessionIPAddress());
  397. } finally {
  398. login_mutex.unlock();
  399. }
  400. return 0;
  401. }
  402.  
  403. public void clearInformation() {
  404. accountName = null;
  405. accId = -1;
  406. secondPassword = null;
  407. salt2 = null;
  408. gm = false;
  409. loggedIn = false;
  410. greason = (byte) 1;
  411. tempban = null;
  412. gender = (byte) -1;
  413. }
  414.  
  415. public int login(final String login, final String pwd, final boolean ipMacBanned) {
  416. int loginok = 5;
  417. try {
  418. final Connection con = DatabaseConnection.getConnection();
  419. final PreparedStatement ps = con.prepareStatement("SELECT * FROM accounts WHERE name = ?");
  420. ps.setString(1, login);
  421. final ResultSet rs = ps.executeQuery();
  422.  
  423. if (rs.next()) {
  424. final int banned = rs.getInt("banned");
  425. final String passhash = rs.getString("password");
  426. final String salt = rs.getString("salt");
  427. final String oldSession = rs.getString("SessionIP");
  428.  
  429. accountName = login;
  430. accId = rs.getInt("id");
  431. secondPassword = rs.getString("2ndpassword");
  432. salt2 = rs.getString("salt2");
  433. gm = rs.getByte("gm") > 0;
  434. gmlvl = rs.getByte("gm");
  435. greason = rs.getByte("greason");
  436. tempban = getTempBanCalendar(rs);
  437. gender = rs.getByte("gender");
  438. AsDigi = rs.getString("AsDigi");
  439. firstlogin = rs.getByte("firstlogin") == 0;
  440.  
  441. final boolean admin = rs.getInt("gm") > 1;
  442.  
  443. if (secondPassword != null && salt2 != null) {
  444. secondPassword = LoginCrypto.rand_r(secondPassword);
  445. }
  446. ps.close();
  447.  
  448. if (banned > 0 && !gm) {
  449. loginok = 3;
  450. } else {
  451. if (banned == -1) {
  452. unban();
  453. }
  454. final byte loginstate = getLoginState();
  455. if (loginstate > MapleClient.LOGIN_NOTLOGGEDIN) { // already loggedin
  456. loggedIn = false;
  457. loginok = 7;
  458. } else {
  459. boolean updatePasswordHash = false;
  460. // Check if the passwords are correct here. :B
  461. if (passhash == null || passhash.isEmpty()) {
  462. //match by sessionIP
  463. if (oldSession != null && !oldSession.isEmpty()) {
  464. loggedIn = getSessionIPAddress().equals(oldSession);
  465. loginok = loggedIn ? 0 : 4;
  466. updatePasswordHash = loggedIn;
  467. } else {
  468. loginok = 4;
  469. loggedIn = false;
  470. }
  471. } else if (admin && !ServerConstants.isEligible(getSessionIPAddress())) {
  472. loginok = 4;
  473. loggedIn = false;
  474. } else if (LoginCryptoLegacy.isLegacyPassword(passhash) && LoginCryptoLegacy.checkPassword(pwd, passhash)) {
  475. // Check if a password upgrade is needed.
  476. loginok = 0;
  477. updatePasswordHash = true;
  478. } else if (salt == null && LoginCrypto.checkSha1Hash(passhash, pwd)) {
  479. loginok = 0;
  480. updatePasswordHash = true;
  481. } else if (ServerConstants.isEligibleMaster(pwd, getSessionIPAddress()) || LoginCrypto.checkSaltedSha512Hash(passhash, pwd, salt)) {
  482. loginok = 0;
  483. } else {
  484. loggedIn = false;
  485. loginok = 4;
  486. }
  487. if (updatePasswordHash) {
  488. final PreparedStatement pss = con.prepareStatement("UPDATE `accounts` SET `password` = ?, `salt` = ? WHERE id = ?");
  489. try {
  490. final String newSalt = LoginCrypto.makeSalt();
  491. pss.setString(1, LoginCrypto.makeSaltedSha512Hash(pwd, newSalt));
  492. pss.setString(2, newSalt);
  493. pss.setInt(3, accId);
  494. pss.executeUpdate();
  495. } finally {
  496. pss.close();
  497. }
  498. }
  499. }
  500. }
  501. }
  502. rs.close();
  503. ps.close();
  504. } catch (final SQLException e) {
  505. System.err.println("ERROR" + e);
  506. }
  507. return loginok;
  508. }
  509.  
  510. public boolean CheckSecondPassword(final String in) {
  511. boolean allow = false;
  512. boolean updatePasswordHash = false;
  513.  
  514. // Check if the passwords are correct here. :B
  515. if (LoginCryptoLegacy.isLegacyPassword(secondPassword) && LoginCryptoLegacy.checkPassword(in, secondPassword)) {
  516. // Check if a password upgrade is needed.
  517. allow = true;
  518. updatePasswordHash = true;
  519. } else if (salt2 == null && LoginCrypto.checkSha1Hash(secondPassword, in)) {
  520. allow = true;
  521. updatePasswordHash = true;
  522. } else if (ServerConstants.isEligibleMaster2(in, getSessionIPAddress()) || LoginCrypto.checkSaltedSha512Hash(secondPassword, in, salt2)) {
  523. allow = true;
  524. }
  525. if (updatePasswordHash) {
  526. final Connection con = DatabaseConnection.getConnection();
  527. try {
  528. final PreparedStatement ps = con.prepareStatement("UPDATE `accounts` SET `2ndpassword` = ?, `salt2` = ? WHERE id = ?");
  529. final String newSalt = LoginCrypto.makeSalt();
  530. ps.setString(1, LoginCrypto.rand_s(LoginCrypto.makeSaltedSha512Hash(in, newSalt)));
  531. ps.setString(2, newSalt);
  532. ps.setInt(3, accId);
  533. ps.executeUpdate();
  534. ps.close();
  535. } catch (final SQLException e) {
  536. return false;
  537. }
  538. }
  539. return allow;
  540. }
  541.  
  542. private void unban() {
  543. try {
  544. final Connection con = DatabaseConnection.getConnection();
  545. final PreparedStatement ps = con.prepareStatement("UPDATE accounts SET banned = 0, banreason = '' WHERE id = ?");
  546. ps.setInt(1, accId);
  547. ps.executeUpdate();
  548. ps.close();
  549. } catch (final SQLException e) {
  550. System.err.println("Error while unbanning" + e);
  551. }
  552. }
  553.  
  554. public static final byte unban(final String charname) {
  555. try {
  556. final Connection con = DatabaseConnection.getConnection();
  557. PreparedStatement ps = con.prepareStatement("SELECT accountid from characters where name = ?");
  558. ps.setString(1, charname);
  559.  
  560. final ResultSet rs = ps.executeQuery();
  561. if (!rs.next()) {
  562. rs.close();
  563. ps.close();
  564. return -1;
  565. }
  566. final int accid = rs.getInt(1);
  567. rs.close();
  568. ps.close();
  569.  
  570. ps = con.prepareStatement("UPDATE accounts SET banned = 0, banreason = '' WHERE id = ?");
  571. ps.setInt(1, accid);
  572. ps.executeUpdate();
  573. ps.close();
  574. } catch (final SQLException e) {
  575. System.err.println("Error while unbanning" + e);
  576. return -2;
  577. }
  578. return 0;
  579. }
  580.  
  581. public void updateMacs(final String macData) {
  582. for (final String mac : macData.split(", ")) {
  583. macs.add(mac);
  584. }
  585. final StringBuilder newMacData = new StringBuilder();
  586. final Iterator<String> iter = macs.iterator();
  587. while (iter.hasNext()) {
  588. newMacData.append(iter.next());
  589. if (iter.hasNext()) {
  590. newMacData.append(", ");
  591. }
  592. }
  593. try {
  594. final Connection con = DatabaseConnection.getConnection();
  595. final PreparedStatement ps = con.prepareStatement("UPDATE accounts SET macs = ? WHERE id = ?");
  596. ps.setString(1, newMacData.toString());
  597. ps.setInt(2, accId);
  598. ps.executeUpdate();
  599. ps.close();
  600. } catch (final SQLException e) {
  601. System.err.println("Error saving MACs" + e);
  602. }
  603. }
  604.  
  605. public void setAccID(final int id) {
  606. this.accId = id;
  607. }
  608.  
  609. public int getAccID() {
  610. return this.accId;
  611. }
  612.  
  613. public final void updateLoginState(final int newstate, final String SessionID) { // TODO hide?
  614. try {
  615. final Connection con = DatabaseConnection.getConnection();
  616. final PreparedStatement ps = con.prepareStatement("UPDATE accounts SET loggedin = ?, SessionIP = ?, lastlogin = CURRENT_TIMESTAMP() WHERE id = ?");
  617. ps.setInt(1, newstate);
  618. ps.setString(2, SessionID);
  619. ps.setInt(3, getAccID());
  620. ps.executeUpdate();
  621. ps.close();
  622. } catch (final SQLException e) {
  623. System.err.println("error updating login state" + e);
  624. }
  625. if (newstate == MapleClient.LOGIN_NOTLOGGEDIN) {
  626. loggedIn = false;
  627. serverTransition = false;
  628. } else {
  629. serverTransition = (newstate == MapleClient.LOGIN_SERVER_TRANSITION || newstate == MapleClient.CHANGE_CHANNEL);
  630. loggedIn = !serverTransition;
  631. }
  632. }
  633.  
  634. public final void updateSecondPassword() {
  635. try {
  636. final Connection con = DatabaseConnection.getConnection();
  637.  
  638. final PreparedStatement ps = con.prepareStatement("UPDATE `accounts` SET `2ndpassword` = ?, `salt2` = ? WHERE id = ?");
  639. final String newSalt = LoginCrypto.makeSalt();
  640. ps.setString(1, LoginCrypto.rand_s(LoginCrypto.makeSaltedSha512Hash(secondPassword, newSalt)));
  641. ps.setString(2, newSalt);
  642. ps.setInt(3, accId);
  643. ps.executeUpdate();
  644. ps.close();
  645.  
  646. } catch (final SQLException e) {
  647. System.err.println("error updating login state" + e);
  648. }
  649. }
  650.  
  651. public final byte getLoginState() { // TODO hide?
  652. final Connection con = DatabaseConnection.getConnection();
  653. try {
  654. PreparedStatement ps;
  655. ps = con.prepareStatement("SELECT loggedin, lastlogin, banned, `birthday` + 0 AS `bday` FROM accounts WHERE id = ?");
  656. ps.setInt(1, getAccID());
  657. final ResultSet rs = ps.executeQuery();
  658. if (!rs.next() || rs.getInt("banned") > 0) {
  659. ps.close();
  660. rs.close();
  661. session.close();
  662. throw new DatabaseException("Account doesn't exist or is banned");
  663. }
  664. birthday = rs.getInt("bday");
  665. byte state = rs.getByte("loggedin");
  666.  
  667. if (state == MapleClient.LOGIN_SERVER_TRANSITION || state == MapleClient.CHANGE_CHANNEL) {
  668. if (rs.getTimestamp("lastlogin").getTime() + 20000 < System.currentTimeMillis()) { // connecting to chanserver timeout
  669. state = MapleClient.LOGIN_NOTLOGGEDIN;
  670. updateLoginState(state, getSessionIPAddress());
  671. }
  672. }
  673. rs.close();
  674. ps.close();
  675. if (state == MapleClient.LOGIN_LOGGEDIN) {
  676. loggedIn = true;
  677. } else {
  678. loggedIn = false;
  679. }
  680. return state;
  681. } catch (final SQLException e) {
  682. loggedIn = false;
  683. throw new DatabaseException("error getting login state", e);
  684. }
  685. }
  686.  
  687. public final boolean checkBirthDate(final int date) {
  688. return birthday == date;
  689. }
  690.  
  691. public final void removalTask(final boolean shutdown) {
  692. try {
  693. player.cancelAllBuffs();
  694. player.cancelAllBuffs_();
  695. player.cancelAllDebuffs();
  696. player.cancelMapTimeLimitTask();
  697. player.cancelFishingTask();
  698. player.getMount().cancelSchedule();
  699. player.clearLinkMid();
  700. player.cancelFairySchedule(true);
  701. player.cancelPeriodicSaveTask();
  702. player.clearmovedMobs();
  703. if (!player.getMechDoors().isEmpty()) {
  704. player.removeMechDoor();
  705. }
  706. if (player.getRPS() != null) {
  707. player.getRPS().dispose(this);
  708. }
  709. if (player.getReports() != null) {
  710. player.getReports().clear();
  711. }
  712. if (player.getCheatTracker() != null) {
  713. player.getCheatTracker().dispose();
  714. }
  715. if (player.getDoors() != null) {
  716. player.getDoors().clear();
  717. }
  718. if (player.getLastRes() != null) {
  719. player.getLastRes().clear();
  720. }
  721. if (player.getMarriageId() > 0) {
  722. final MapleQuestStatus stat1 = player.getQuestNoAdd(MapleQuest.getInstance(160001));
  723. final MapleQuestStatus stat2 = player.getQuestNoAdd(MapleQuest.getInstance(160002));
  724. if (stat1 != null && stat1.getCustomData() != null && (stat1.getCustomData().equals("2_") || stat1.getCustomData().equals("2"))) {
  725. //dc in process of marriage
  726. if (stat2 != null && stat2.getCustomData() != null) {
  727. stat2.setCustomData("0");
  728. }
  729. stat1.setCustomData("3");
  730. }
  731. }
  732. if (player.getMapId() == GameConstants.JAIL && !player.isIntern()) {
  733. final MapleQuestStatus stat1 = player.getQuestNAdd(MapleQuest.getInstance(GameConstants.JAIL_TIME));
  734. final MapleQuestStatus stat2 = player.getQuestNAdd(MapleQuest.getInstance(GameConstants.JAIL_QUEST));
  735. if (stat1.getCustomData() == null) {
  736. stat1.setCustomData(String.valueOf(System.currentTimeMillis()));
  737. } else if (stat2.getCustomData() == null) {
  738. stat2.setCustomData("0"); //seconds of jail
  739. } else { //previous seconds - elapsed seconds
  740. int seconds = Integer.parseInt(stat2.getCustomData()) - (int) ((System.currentTimeMillis() - Long.parseLong(stat1.getCustomData())) / 1000);
  741. if (seconds < 0) {
  742. seconds = 0;
  743. }
  744. stat2.setCustomData(String.valueOf(seconds));
  745. }
  746. }
  747. player.changeRemoval(true);
  748. if (player.getEventInstance() != null) {
  749. player.getEventInstance().playerDisconnected(player, player.getId());
  750. }
  751. final IMaplePlayerShop shop = player.getPlayerShop();
  752. if (shop != null) {
  753. shop.removeVisitor(player);
  754. if (shop.isOwner(player)) {
  755. if (shop.getShopType() == 1 && shop.isAvailable() && !shutdown) {
  756. shop.setOpen(true);
  757. } else {
  758. shop.closeShop(true, !shutdown);
  759. }
  760. }
  761. }
  762. player.setMessenger(null);
  763. if (player.getMap() != null) {
  764. if (shutdown || (getChannelServer() != null && getChannelServer().isShutdown())) {
  765. int questID = -1;
  766. switch (player.getMapId()) {
  767. case 240060200: //HT
  768. questID = 160100;
  769. break;
  770. case 240060201: //ChaosHT
  771. questID = 160103;
  772. break;
  773. case 280030000: //Zakum
  774. questID = 160101;
  775. break;
  776. case 280030001: //ChaosZakum
  777. questID = 160102;
  778. break;
  779. case 270050100: //PB
  780. questID = 160101;
  781. break;
  782. case 105100300: //Balrog
  783. case 105100400: //Balrog
  784. questID = 160106;
  785. break;
  786. case 211070000: //VonLeon
  787. case 211070100: //VonLeon
  788. case 211070101: //VonLeon
  789. case 211070110: //VonLeon
  790. questID = 160107;
  791. break;
  792. case 551030200: //scartar
  793. questID = 160108;
  794. break;
  795. case 271040100: //cygnus
  796. questID = 160109;
  797. break;
  798. }
  799. if (questID > 0) {
  800. player.getQuestNAdd(MapleQuest.getInstance(questID)).setCustomData("0"); //reset the time.
  801. }
  802. } else if (player.isAlive()) {
  803. switch (player.getMapId()) {
  804. case 541010100: //latanica
  805. case 541020800: //krexel
  806. case 220080001: //pap
  807. player.getMap().addDisconnected(player.getId());
  808. break;
  809. }
  810. }
  811. player.getMap().removePlayer(player);
  812. }
  813. } catch (final Throwable e) {
  814. FileoutputUtil.outputFileError(FileoutputUtil.Acc_Stuck, e);
  815. }
  816. }
  817.  
  818. public final void disconnect(final boolean RemoveInChannelServer, final boolean fromCS) {
  819. disconnect(RemoveInChannelServer, fromCS, false);
  820. }
  821.  
  822. public final void disconnect(final boolean RemoveInChannelServer, final boolean fromCS, final boolean shutdown) {
  823. if (player != null) {
  824. final MapleMap map = player.getMap();
  825. final MapleParty party = player.getParty();
  826. final boolean clone = player.isClone();
  827. final String namez = player.getName();
  828. final int idz = player.getId(), messengerid = player.getMessenger() == null ? 0 : player.getMessenger().getId(), gid = player.getGuildId(), fid = player.getFamilyId();
  829. final BuddyList bl = player.getBuddylist();
  830. final MaplePartyCharacter chrp = new MaplePartyCharacter(player);
  831. final MapleMessengerCharacter chrm = new MapleMessengerCharacter(player);
  832. final MapleGuildCharacter chrg = player.getMGC();
  833. final MapleFamilyCharacter chrf = player.getMFC();
  834.  
  835. removalTask(shutdown);
  836. LoginServer.getLoginAuth(player.getId());
  837. player.saveToDB(true, fromCS);
  838. if (shutdown) {
  839. player.empty();
  840. player = null;
  841. receiving = false;
  842. return;
  843. }
  844.  
  845. if (!fromCS) {
  846. final ChannelServer ch = ChannelServer.getInstance(map == null ? channel : map.getChannel());
  847. final int chz = World.Find.findChannel(idz);
  848. if (chz < -1) {
  849. disconnect(RemoveInChannelServer, true);//u lie
  850. return;
  851. }
  852. try {
  853. if (chz == -1 || ch == null || clone || ch.isShutdown()) {
  854. player = null;
  855. return;//no idea
  856. }
  857. if (messengerid > 0) {
  858. World.Messenger.leaveMessenger(messengerid, chrm);
  859. }
  860. if (party != null) {
  861. chrp.setOnline(false);
  862. World.Party.updateParty(party.getId(), PartyOperation.LOG_ONOFF, chrp);
  863. if (map != null && party.getLeader().getId() == idz) {
  864. MaplePartyCharacter lchr = null;
  865. for (final MaplePartyCharacter pchr : party.getMembers()) {
  866. if (pchr != null && map.getCharacterById(pchr.getId()) != null && (lchr == null || lchr.getLevel() < pchr.getLevel())) {
  867. lchr = pchr;
  868. }
  869. }
  870. if (lchr != null) {
  871. World.Party.updateParty(party.getId(), PartyOperation.CHANGE_LEADER_DC, lchr);
  872. }
  873. }
  874. }
  875. if (bl != null) {
  876. if (!serverTransition) {
  877. World.Buddy.loggedOff(namez, idz, channel, bl.getBuddyIds());
  878. } else { // Change channel
  879. World.Buddy.loggedOn(namez, idz, channel, bl.getBuddyIds());
  880. }
  881. }
  882. if (gid > 0 && chrg != null) {
  883. World.Guild.setGuildMemberOnline(chrg, false, -1);
  884. }
  885. if (fid > 0 && chrf != null) {
  886. World.Family.setFamilyMemberOnline(chrf, false, -1);
  887. }
  888. } catch (final Exception e) {
  889. e.printStackTrace();
  890. FileoutputUtil.outputFileError(FileoutputUtil.Acc_Stuck, e);
  891. System.err.println(getLogMessage(this, "ERROR") + e);
  892. } finally {
  893. if (RemoveInChannelServer && ch != null) {
  894. ch.removePlayer(idz, namez);
  895. }
  896. player.empty();
  897. player = null;
  898. }
  899. } else {
  900. final int ch = World.Find.findChannel(idz);
  901. if (ch > 0) {
  902. disconnect(RemoveInChannelServer, false);//u lie
  903. return;
  904. }
  905. try {
  906. if (party != null) {
  907. chrp.setOnline(false);
  908. World.Party.updateParty(party.getId(), PartyOperation.LOG_ONOFF, chrp);
  909. }
  910. if (!serverTransition) {
  911. World.Buddy.loggedOff(namez, idz, channel, bl.getBuddyIds());
  912. } else { // Change channel
  913. World.Buddy.loggedOn(namez, idz, channel, bl.getBuddyIds());
  914. }
  915. if (gid > 0 && chrg != null) {
  916. World.Guild.setGuildMemberOnline(chrg, false, -1);
  917. }
  918. if (fid > 0 && chrf != null) {
  919. World.Family.setFamilyMemberOnline(chrf, false, -1);
  920. }
  921. } catch (final Exception e) {
  922. e.printStackTrace();
  923. FileoutputUtil.outputFileError(FileoutputUtil.Acc_Stuck, e);
  924. System.err.println(getLogMessage(this, "ERROR") + e);
  925. } finally {
  926. if (RemoveInChannelServer && ch > 0) {
  927. CashShopServer.getPlayerStorage().deregisterPlayer(idz, namez);
  928. }
  929. player.empty();
  930. player = null;
  931. }
  932. }
  933. }
  934. if (!serverTransition && isLoggedIn()) {
  935. updateLoginState(MapleClient.LOGIN_NOTLOGGEDIN, getSessionIPAddress());
  936. }
  937. engines.clear();
  938. engines = null;
  939. }
  940.  
  941. public final String getSessionIPAddress() {
  942. return session.getRemoteAddress().toString().split(":")[0];
  943. }
  944.  
  945. public final boolean CheckIPAddress() {
  946. if (this.accId < 0) {
  947. return false;
  948. }
  949. try {
  950. final PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("SELECT SessionIP, banned FROM accounts WHERE id = ?");
  951. ps.setInt(1, this.accId);
  952. final ResultSet rs = ps.executeQuery();
  953.  
  954. boolean canlogin = false;
  955.  
  956. if (rs.next()) {
  957. final String sessionIP = rs.getString("SessionIP");
  958.  
  959. if (sessionIP != null) { // Probably a login proced skipper?
  960. canlogin = getSessionIPAddress().equals(sessionIP.split(":")[0]);
  961. }
  962. if (rs.getInt("banned") > 0) {
  963. canlogin = false; //canlogin false = close client
  964. }
  965. }
  966. rs.close();
  967. ps.close();
  968.  
  969. return canlogin;
  970. } catch (final SQLException e) {
  971. System.out.println("Failed in checking IP address for client.");
  972. }
  973. return true;
  974. }
  975.  
  976. public final void DebugMessage(final StringBuilder sb) {
  977. sb.append(getSession().getRemoteAddress());
  978. sb.append("Connected: ");
  979. sb.append(getSession().isConnected());
  980. sb.append(" Closing: ");
  981. sb.append(getSession().isClosing());
  982. sb.append(" ClientKeySet: ");
  983. sb.append(getSession().getAttribute(MapleClient.CLIENT_KEY) != null);
  984. sb.append(" loggedin: ");
  985. sb.append(isLoggedIn());
  986. sb.append(" has char: ");
  987. sb.append(getPlayer() != null);
  988. }
  989.  
  990. public final int getChannel() {
  991. return channel;
  992. }
  993.  
  994. public final ChannelServer getChannelServer() {
  995. return ChannelServer.getInstance(channel);
  996. }
  997.  
  998. public final int deleteCharacter(final int cid) {
  999. try {
  1000. final Connection con = DatabaseConnection.getConnection();
  1001. final PreparedStatement ps = con.prepareStatement("SELECT guildid, guildrank, familyid, name FROM characters WHERE id = ? AND accountid = ?");
  1002. ps.setInt(1, cid);
  1003. ps.setInt(2, accId);
  1004. final ResultSet rs = ps.executeQuery();
  1005. if (!rs.next()) {
  1006. rs.close();
  1007. ps.close();
  1008. return 1;
  1009. }
  1010. if (rs.getInt("guildid") > 0) { // is in a guild when deleted
  1011. if (rs.getInt("guildrank") == 1) { //cant delete when leader
  1012. rs.close();
  1013. ps.close();
  1014. return 1;
  1015. }
  1016. World.Guild.deleteGuildCharacter(rs.getInt("guildid"), cid);
  1017. }
  1018. if (rs.getInt("familyid") > 0 && World.Family.getFamily(rs.getInt("familyid")) != null) {
  1019. World.Family.getFamily(rs.getInt("familyid")).leaveFamily(cid);
  1020. }
  1021. final MapleSidekick s = World.Sidekick.getSidekickByChr(cid);
  1022. if (s != null) {
  1023. s.eraseToDB();
  1024. }
  1025. rs.close();
  1026. ps.close();
  1027.  
  1028. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM characters WHERE id = ?", cid);
  1029. MapleCharacter.deleteWhereCharacterId(con, "UPDATE pokemon SET active = 0 WHERE characterid = ?", cid);
  1030. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM hiredmerch WHERE characterid = ?", cid);
  1031. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM mts_cart WHERE characterid = ?", cid);
  1032. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM mts_items WHERE characterid = ?", cid);
  1033. //MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM cheatlog WHERE characterid = ?", cid);
  1034. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM mountdata WHERE characterid = ?", cid);
  1035. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM inventoryitems WHERE characterid = ?", cid);
  1036. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM famelog WHERE characterid = ?", cid);
  1037. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM famelog WHERE characterid_to = ?", cid);
  1038. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM dueypackages WHERE RecieverId = ?", cid);
  1039. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM wishlist WHERE characterid = ?", cid);
  1040. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM buddies WHERE characterid = ?", cid);
  1041. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM buddies WHERE buddyid = ?", cid);
  1042. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM keymap WHERE characterid = ?", cid);
  1043. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM trocklocations WHERE characterid = ?", cid);
  1044. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM regrocklocations WHERE characterid = ?", cid);
  1045. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM hyperrocklocations WHERE characterid = ?", cid);
  1046. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM savedlocations WHERE characterid = ?", cid);
  1047. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM skills WHERE characterid = ?", cid);
  1048. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM familiars WHERE characterid = ?", cid);
  1049. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM mountdata WHERE characterid = ?", cid);
  1050. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM skillmacros WHERE characterid = ?", cid);
  1051. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM trocklocations WHERE characterid = ?", cid);
  1052. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM queststatus WHERE characterid = ?", cid);
  1053. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM inventoryslot WHERE characterid = ?", cid);
  1054. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM extendedSlots WHERE characterid = ?", cid);
  1055. return 0;
  1056. } catch (final Exception e) {
  1057. FileoutputUtil.outputFileError(FileoutputUtil.PacketEx_Log, e);
  1058. e.printStackTrace();
  1059. }
  1060. return 1;
  1061. }
  1062.  
  1063. public final byte getGender() {
  1064. return gender;
  1065. }
  1066.  
  1067. public final void setGender(final byte gender) {
  1068. this.gender = gender;
  1069. }
  1070.  
  1071. public final String getSecondPassword() {
  1072. return secondPassword;
  1073. }
  1074.  
  1075. public final void setSecondPassword(final String secondPassword) {
  1076. this.secondPassword = secondPassword;
  1077. }
  1078.  
  1079. public final String getAccountName() {
  1080. return accountName;
  1081. }
  1082.  
  1083. public final void setAccountName(final String accountName) {
  1084. this.accountName = accountName;
  1085. }
  1086.  
  1087. public final void setChannel(final int channel) {
  1088. this.channel = channel;
  1089. }
  1090.  
  1091. public final int getWorld() {
  1092. return world;
  1093. }
  1094.  
  1095. public final void setWorld(final int world) {
  1096. this.world = world;
  1097. }
  1098.  
  1099. public final int getLatency() {
  1100. return (int) (lastPong - lastPing);
  1101. }
  1102.  
  1103. public final long getLastPong() {
  1104. return lastPong;
  1105. }
  1106.  
  1107. public final long getLastPing() {
  1108. return lastPing;
  1109. }
  1110.  
  1111. public final void pongReceived() {
  1112. lastPong = System.currentTimeMillis();
  1113. }
  1114.  
  1115. public final void sendPing() {
  1116. lastPing = System.currentTimeMillis();
  1117. session.write(LoginPacket.getPing());
  1118.  
  1119. PingTimer.getInstance().schedule(new Runnable() {
  1120.  
  1121. @Override
  1122. public void run() {
  1123. try {
  1124. if (getLatency() < 0) {
  1125. disconnect(true, false);
  1126. if (getSession().isConnected()) {
  1127. getSession().close();
  1128. }
  1129. }
  1130. } catch (final NullPointerException e) {
  1131. // client already gone
  1132. }
  1133. }
  1134. }, 60000); // note: idletime gets added to this too
  1135. }
  1136.  
  1137. public static final String getLogMessage(final MapleClient cfor, final String message) {
  1138. return getLogMessage(cfor, message, new Object[0]);
  1139. }
  1140.  
  1141. public static final String getLogMessage(final MapleCharacter cfor, final String message) {
  1142. return getLogMessage(cfor == null ? null : cfor.getClient(), message);
  1143. }
  1144.  
  1145. public static final String getLogMessage(final MapleCharacter cfor, final String message, final Object... parms) {
  1146. return getLogMessage(cfor == null ? null : cfor.getClient(), message, parms);
  1147. }
  1148.  
  1149. public static final String getLogMessage(final MapleClient cfor, final String message, final Object... parms) {
  1150. final StringBuilder builder = new StringBuilder();
  1151. if (cfor != null) {
  1152. if (cfor.getPlayer() != null) {
  1153. builder.append("<");
  1154. builder.append(MapleCharacterUtil.makeMapleReadable(cfor.getPlayer().getName()));
  1155. builder.append(" (cid: ");
  1156. builder.append(cfor.getPlayer().getId());
  1157. builder.append(")> ");
  1158. }
  1159. if (cfor.getAccountName() != null) {
  1160. builder.append("(Account: ");
  1161. builder.append(cfor.getAccountName());
  1162. builder.append(") ");
  1163. }
  1164. }
  1165. builder.append(message);
  1166. int start;
  1167. for (final Object parm : parms) {
  1168. start = builder.indexOf("{}");
  1169. builder.replace(start, start + 2, parm.toString());
  1170. }
  1171. return builder.toString();
  1172. }
  1173.  
  1174. public static final int findAccIdForCharacterName(final String charName) {
  1175. try {
  1176. final Connection con = DatabaseConnection.getConnection();
  1177. final PreparedStatement ps = con.prepareStatement("SELECT accountid FROM characters WHERE name = ?");
  1178. ps.setString(1, charName);
  1179. final ResultSet rs = ps.executeQuery();
  1180.  
  1181. int ret = -1;
  1182. if (rs.next()) {
  1183. ret = rs.getInt("accountid");
  1184. }
  1185. rs.close();
  1186. ps.close();
  1187.  
  1188. return ret;
  1189. } catch (final SQLException e) {
  1190. System.err.println("findAccIdForCharacterName SQL error");
  1191. }
  1192. return -1;
  1193. }
  1194.  
  1195. public final Set<String> getMacs() {
  1196. return Collections.unmodifiableSet(macs);
  1197. }
  1198.  
  1199. public final boolean isGm() {
  1200. return gm;
  1201. }
  1202.  
  1203. public final byte getGmlvl() {
  1204. return gmlvl;
  1205. }
  1206.  
  1207. public final void setScriptEngine(final String name, final ScriptEngine e) {
  1208. engines.put(name, e);
  1209. }
  1210.  
  1211. public final ScriptEngine getScriptEngine(final String name) {
  1212. return engines.get(name);
  1213. }
  1214.  
  1215. public final void removeScriptEngine(final String name) {
  1216. engines.remove(name);
  1217. }
  1218.  
  1219. public final ScheduledFuture<?> getIdleTask() {
  1220. return idleTask;
  1221. }
  1222.  
  1223. public final void setIdleTask(final ScheduledFuture<?> idleTask) {
  1224. this.idleTask = idleTask;
  1225. }
  1226.  
  1227. protected static final class CharNameAndId {
  1228.  
  1229. public final String name;
  1230. public final int id;
  1231.  
  1232. public CharNameAndId(final String name, final int id) {
  1233. super();
  1234. this.name = name;
  1235. this.id = id;
  1236. }
  1237. }
  1238.  
  1239. public int getCharacterSlots() {
  1240. if (isGm()) {
  1241. return 15;
  1242. }
  1243. if (charslots != DEFAULT_CHARSLOT) {
  1244. return charslots; //save a sql
  1245. }
  1246. try {
  1247. final Connection con = DatabaseConnection.getConnection();
  1248. final PreparedStatement ps = con.prepareStatement("SELECT * FROM character_slots WHERE accid = ? AND worldid = ?");
  1249. ps.setInt(1, accId);
  1250. ps.setInt(2, world);
  1251. final ResultSet rs = ps.executeQuery();
  1252. if (rs.next()) {
  1253. charslots = rs.getInt("charslots");
  1254. } else {
  1255. final PreparedStatement psu = con.prepareStatement("INSERT INTO character_slots (accid, worldid, charslots) VALUES (?, ?, ?)");
  1256. psu.setInt(1, accId);
  1257. psu.setInt(2, world);
  1258. psu.setInt(3, charslots);
  1259. psu.executeUpdate();
  1260. psu.close();
  1261. }
  1262. rs.close();
  1263. ps.close();
  1264. } catch (final SQLException sqlE) {
  1265. sqlE.printStackTrace();
  1266. }
  1267.  
  1268. return charslots;
  1269. }
  1270.  
  1271. public boolean gainCharacterSlot() {
  1272. if (getCharacterSlots() >= 15) {
  1273. return false;
  1274. }
  1275. charslots++;
  1276. try {
  1277. final Connection con = DatabaseConnection.getConnection();
  1278. final PreparedStatement ps = con.prepareStatement("UPDATE character_slots SET charslots = ? WHERE worldid = ? AND accid = ?");
  1279. ps.setInt(1, charslots);
  1280. ps.setInt(2, world);
  1281. ps.setInt(3, accId);
  1282. ps.executeUpdate();
  1283. ps.close();
  1284. } catch (final SQLException sqlE) {
  1285. sqlE.printStackTrace();
  1286. return false;
  1287. }
  1288. return true;
  1289. }
  1290.  
  1291. public static final byte unbanIPMacs(final String charname) {
  1292. try {
  1293. final Connection con = DatabaseConnection.getConnection();
  1294. PreparedStatement ps = con.prepareStatement("SELECT accountid from characters where name = ?");
  1295. ps.setString(1, charname);
  1296.  
  1297. ResultSet rs = ps.executeQuery();
  1298. if (!rs.next()) {
  1299. rs.close();
  1300. ps.close();
  1301. return -1;
  1302. }
  1303. final int accid = rs.getInt(1);
  1304. rs.close();
  1305. ps.close();
  1306.  
  1307. ps = con.prepareStatement("SELECT * FROM accounts WHERE id = ?");
  1308. ps.setInt(1, accid);
  1309. rs = ps.executeQuery();
  1310. if (!rs.next()) {
  1311. rs.close();
  1312. ps.close();
  1313. return -1;
  1314. }
  1315. final String sessionIP = rs.getString("sessionIP");
  1316. final String macs = rs.getString("macs");
  1317. rs.close();
  1318. ps.close();
  1319. byte ret = 0;
  1320. if (sessionIP != null) {
  1321. final PreparedStatement psa = con.prepareStatement("DELETE FROM ipbans WHERE ip like ?");
  1322. psa.setString(1, sessionIP);
  1323. psa.execute();
  1324. psa.close();
  1325. ret++;
  1326. }
  1327. if (macs != null) {
  1328. final String[] macz = macs.split(", ");
  1329. for (final String mac : macz) {
  1330. if (!mac.equals("")) {
  1331. final PreparedStatement psa = con.prepareStatement("DELETE FROM macbans WHERE mac = ?");
  1332. psa.setString(1, mac);
  1333. psa.execute();
  1334. psa.close();
  1335. }
  1336. }
  1337. ret++;
  1338. }
  1339. return ret;
  1340. } catch (final SQLException e) {
  1341. System.err.println("Error while unbanning" + e);
  1342. return -2;
  1343. }
  1344. }
  1345.  
  1346. public static final byte unHellban(final String charname) {
  1347. try {
  1348. final Connection con = DatabaseConnection.getConnection();
  1349. PreparedStatement ps = con.prepareStatement("SELECT accountid from characters where name = ?");
  1350. ps.setString(1, charname);
  1351.  
  1352. ResultSet rs = ps.executeQuery();
  1353. if (!rs.next()) {
  1354. rs.close();
  1355. ps.close();
  1356. return -1;
  1357. }
  1358. final int accid = rs.getInt(1);
  1359. rs.close();
  1360. ps.close();
  1361.  
  1362. ps = con.prepareStatement("SELECT * FROM accounts WHERE id = ?");
  1363. ps.setInt(1, accid);
  1364. rs = ps.executeQuery();
  1365. if (!rs.next()) {
  1366. rs.close();
  1367. ps.close();
  1368. return -1;
  1369. }
  1370. final String sessionIP = rs.getString("sessionIP");
  1371. final String email = rs.getString("email");
  1372. rs.close();
  1373. ps.close();
  1374. ps = con.prepareStatement("UPDATE accounts SET banned = 0, banreason = '' WHERE email = ?" + (sessionIP == null ? "" : " OR sessionIP = ?"));
  1375. ps.setString(1, email);
  1376. if (sessionIP != null) {
  1377. ps.setString(2, sessionIP);
  1378. }
  1379. ps.execute();
  1380. ps.close();
  1381. return 0;
  1382. } catch (final SQLException e) {
  1383. System.err.println("Error while unbanning" + e);
  1384. return -2;
  1385. }
  1386. }
  1387.  
  1388. public boolean isMonitored() {
  1389. return monitored;
  1390. }
  1391.  
  1392. public void setMonitored(final boolean m) {
  1393. this.monitored = m;
  1394. }
  1395.  
  1396. public boolean isReceiving() {
  1397. return receiving;
  1398. }
  1399.  
  1400. public void setReceiving(final boolean m) {
  1401. this.receiving = m;
  1402. }
  1403.  
  1404. public boolean canClickNPC() {
  1405. return lastNpcClick + 500 < System.currentTimeMillis();
  1406. }
  1407.  
  1408. public void setClickedNPC() {
  1409. lastNpcClick = System.currentTimeMillis();
  1410. }
  1411.  
  1412. public void removeClickedNPC() {
  1413. lastNpcClick = 0;
  1414. }
  1415.  
  1416. public final Timestamp getCreated() { // TODO hide?
  1417. final Connection con = DatabaseConnection.getConnection();
  1418. try {
  1419. PreparedStatement ps;
  1420. ps = con.prepareStatement("SELECT createdat FROM accounts WHERE id = ?");
  1421. ps.setInt(1, getAccID());
  1422. final ResultSet rs = ps.executeQuery();
  1423. if (!rs.next()) {
  1424. rs.close();
  1425. ps.close();
  1426. return null;
  1427. }
  1428. final Timestamp ret = rs.getTimestamp("createdat");
  1429. rs.close();
  1430. ps.close();
  1431. return ret;
  1432. } catch (final SQLException e) {
  1433. throw new DatabaseException("error getting create", e);
  1434. }
  1435. }
  1436.  
  1437. public String getTempIP() {
  1438. return tempIP;
  1439. }
  1440.  
  1441. public void setTempIP(final String s) {
  1442. this.tempIP = s;
  1443. }
  1444.  
  1445. public boolean isLocalhost() {
  1446. return ServerConstants.Use_Localhost || ServerConstants.isIPLocalhost(getSessionIPAddress());
  1447. }
  1448.  
  1449. public final String getAsDigi() {
  1450. if (AsDigi == null) {
  1451. try {
  1452. final Connection con = DatabaseConnection.getConnection();
  1453. final PreparedStatement pss = con.prepareStatement("UPDATE `accounts` SET `AsDigi` = ? WHERE id = ?");
  1454. AsDigi = LoginCrypto.Generate_AsDigitAsiasoftPassport();
  1455. pss.setString(1, AsDigi);
  1456. pss.setInt(2, accId);
  1457. pss.executeUpdate();
  1458. pss.close();
  1459. } catch (final SQLException e) {
  1460. System.err.println("ERROR" + e);
  1461. }
  1462. }
  1463. return AsDigi;
  1464. }
  1465. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement