Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This file is part of the OdinMS Maple Story Server
- Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc>
- Matthias Butz <matze@odinms.de>
- Jan Christian Meyer <vimes@odinms.de>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License version 3
- as published by the Free Software Foundation. You may not use, modify
- or distribute this program under any other version of the
- GNU Affero General Public License.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package client;
- import handling.cashshop.CashShopServer;
- import handling.channel.ChannelServer;
- import handling.login.LoginServer;
- import handling.world.MapleMessengerCharacter;
- import handling.world.MapleParty;
- import handling.world.MaplePartyCharacter;
- import handling.world.PartyOperation;
- import handling.world.World;
- import handling.world.family.MapleFamilyCharacter;
- import handling.world.guild.MapleGuildCharacter;
- import handling.world.sidekick.MapleSidekick;
- import java.io.Serializable;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Timestamp;
- import java.util.Calendar;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.concurrent.ScheduledFuture;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- import javax.script.ScriptEngine;
- import org.apache.mina.common.IoSession;
- import server.Timer.PingTimer;
- import server.maps.MapleMap;
- import server.quest.MapleQuest;
- import server.shops.IMaplePlayerShop;
- import tools.FileoutputUtil;
- import tools.MapleAESOFB;
- import tools.MaplePacketCreator;
- import tools.packet.LoginPacket;
- import constants.GameConstants;
- import constants.ServerConstants;
- import database.DatabaseConnection;
- import database.DatabaseException;
- public class MapleClient implements Serializable {
- private static final long serialVersionUID = 9179541993413738569L;
- public static final byte LOGIN_NOTLOGGEDIN = 0,
- LOGIN_SERVER_TRANSITION = 1,
- LOGIN_LOGGEDIN = 2,
- CHANGE_CHANNEL = 3;
- public static final int DEFAULT_CHARSLOT = 6;
- public static final String CLIENT_KEY = "CLIENT";
- private transient MapleAESOFB send, receive;
- private transient IoSession session;
- private MapleCharacter player;
- private int channel = 1, accId = -1, world, birthday;
- private int charslots = DEFAULT_CHARSLOT;
- private boolean loggedIn = false, serverTransition = false;
- private transient Calendar tempban = null;
- private String accountName;
- private transient String AsDigi;
- private transient boolean firstlogin;
- private transient long lastPong = 0, lastPing = 0;
- private boolean monitored = false, receiving = true;
- private boolean gm;
- private byte greason = 1, gender = -1, gmlvl = -1;
- public transient short loginAttempt = 0;
- private transient List<Integer> allowedChar = new LinkedList<Integer>();
- private transient Set<String> macs = new HashSet<String>();
- private transient Map<String, ScriptEngine> engines = new HashMap<String, ScriptEngine>();
- private transient ScheduledFuture<?> idleTask = null;
- private transient String secondPassword, salt2, tempIP = ""; // To be used only on login
- private final transient Lock mutex = new ReentrantLock(true);
- private final transient Lock npc_mutex = new ReentrantLock();
- private long lastNpcClick = 0;
- private final static Lock login_mutex = new ReentrantLock(true);
- public MapleClient(final MapleAESOFB send, final MapleAESOFB receive, final IoSession session) {
- this.send = send;
- this.receive = receive;
- this.session = session;
- }
- public final MapleAESOFB getReceiveCrypto() {
- return receive;
- }
- public final MapleAESOFB getSendCrypto() {
- return send;
- }
- public final IoSession getSession() {
- return session;
- }
- public final Lock getLock() {
- return mutex;
- }
- public final Lock getNPCLock() {
- return npc_mutex;
- }
- public final MapleCharacter getPlayer() {
- return player;
- }
- public void setPlayer(final MapleCharacter player) {
- this.player = player;
- }
- public void createdChar(final int id) {
- allowedChar.add(id);
- }
- public final boolean login_Auth(final int id) {
- return allowedChar.contains(id);
- }
- public final List<MapleCharacter> loadCharacters(final int serverId) { // TODO make this less costly zZz
- final List<MapleCharacter> chars = new LinkedList<MapleCharacter>();
- for (final CharNameAndId cni : loadCharactersInternal(serverId)) {
- final MapleCharacter chr = MapleCharacter.loadCharFromDB(cni.id, this, false);
- if (chr.isSuperGM() && !ServerConstants.isEligible(getSessionIPAddress())) {
- continue;
- }
- chars.add(chr);
- if (!login_Auth(chr.getId())) {
- allowedChar.add(chr.getId());
- }
- }
- return chars;
- }
- public boolean canMakeCharacter(final int serverId) {
- return loadCharactersSize(serverId) < getCharacterSlots();
- }
- public List<String> loadCharacterNames(final int serverId) {
- final List<String> chars = new LinkedList<String>();
- for (final CharNameAndId cni : loadCharactersInternal(serverId)) {
- chars.add(cni.name);
- }
- return chars;
- }
- private List<CharNameAndId> loadCharactersInternal(final int serverId) {
- final List<CharNameAndId> chars = new LinkedList<CharNameAndId>();
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("SELECT id, name, gm FROM characters WHERE accountid = ? AND world = ?");
- ps.setInt(1, accId);
- ps.setInt(2, serverId);
- final ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- if (rs.getInt("gm") >= ServerConstants.PlayerGMRank.SUPERGM.getLevel() && !ServerConstants.isEligible(getSessionIPAddress())) {
- continue;
- }
- chars.add(new CharNameAndId(rs.getString("name"), rs.getInt("id")));
- LoginServer.getLoginAuth(rs.getInt("id"));
- }
- rs.close();
- ps.close();
- } catch (final SQLException e) {
- System.err.println("error loading characters internal");
- e.printStackTrace();
- }
- return chars;
- }
- private int loadCharactersSize(final int serverId) {
- int chars = 0;
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("SELECT count(*) FROM characters WHERE accountid = ? AND world = ?");
- ps.setInt(1, accId);
- ps.setInt(2, serverId);
- final ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- chars = rs.getInt(1);
- }
- rs.close();
- ps.close();
- } catch (final SQLException e) {
- System.err.println("error loading characters internal");
- e.printStackTrace();
- }
- return chars;
- }
- public boolean isLoggedIn() {
- return loggedIn && accId >= 0;
- }
- private Calendar getTempBanCalendar(final ResultSet rs) throws SQLException {
- final Calendar lTempban = Calendar.getInstance();
- if (rs.getLong("tempban") == 0) { // basically if timestamp in db is 0000-00-00
- lTempban.setTimeInMillis(0);
- return lTempban;
- }
- final Calendar today = Calendar.getInstance();
- lTempban.setTimeInMillis(rs.getTimestamp("tempban").getTime());
- if (today.getTimeInMillis() < lTempban.getTimeInMillis()) {
- return lTempban;
- }
- lTempban.setTimeInMillis(0);
- return lTempban;
- }
- public Calendar getTempBanCalendar() {
- return tempban;
- }
- public byte getBanReason() {
- return greason;
- }
- public boolean hasBannedIP() {
- boolean ret = false;
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM ipbans WHERE ? LIKE CONCAT(ip, '%')");
- ps.setString(1, getSessionIPAddress());
- final ResultSet rs = ps.executeQuery();
- rs.next();
- if (rs.getInt(1) > 0) {
- ret = true;
- }
- rs.close();
- ps.close();
- } catch (final SQLException ex) {
- System.err.println("Error checking ip bans" + ex);
- }
- return ret;
- }
- public boolean hasBannedMac() {
- if (macs.isEmpty()) {
- return false;
- }
- boolean ret = false;
- int i = 0;
- try {
- final Connection con = DatabaseConnection.getConnection();
- final StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM macbans WHERE mac IN (");
- for (i = 0; i < macs.size(); i++) {
- sql.append("?");
- if (i != macs.size() - 1) {
- sql.append(", ");
- }
- }
- sql.append(")");
- final PreparedStatement ps = con.prepareStatement(sql.toString());
- i = 0;
- for (final String mac : macs) {
- i++;
- ps.setString(i, mac);
- }
- final ResultSet rs = ps.executeQuery();
- rs.next();
- if (rs.getInt(1) > 0) {
- ret = true;
- }
- rs.close();
- ps.close();
- } catch (final SQLException ex) {
- System.err.println("Error checking mac bans" + ex);
- }
- return ret;
- }
- private void loadMacsIfNescessary() throws SQLException {
- if (macs.isEmpty()) {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("SELECT macs FROM accounts WHERE id = ?");
- ps.setInt(1, accId);
- final ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- if (rs.getString("macs") != null) {
- final String[] macData = rs.getString("macs").split(", ");
- for (final String mac : macData) {
- if (!mac.equals("")) {
- macs.add(mac);
- }
- }
- }
- } else {
- rs.close();
- ps.close();
- throw new RuntimeException("No valid account associated with this client.");
- }
- rs.close();
- ps.close();
- }
- }
- public void banMacs() {
- try {
- loadMacsIfNescessary();
- if (this.macs.size() > 0) {
- final String[] macBans = new String[this.macs.size()];
- int z = 0;
- for (final String mac : this.macs) {
- macBans[z] = mac;
- z++;
- }
- banMacs(macBans);
- }
- } catch (final SQLException e) {
- e.printStackTrace();
- }
- }
- public static final void banMacs(final String[] macs) {
- final Connection con = DatabaseConnection.getConnection();
- try {
- final List<String> filtered = new LinkedList<String>();
- PreparedStatement ps = con.prepareStatement("SELECT filter FROM macfilters");
- final ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- filtered.add(rs.getString("filter"));
- }
- rs.close();
- ps.close();
- ps = con.prepareStatement("INSERT INTO macbans (mac) VALUES (?)");
- for (final String mac : macs) {
- boolean matched = false;
- for (final String filter : filtered) {
- if (mac.matches(filter)) {
- matched = true;
- break;
- }
- }
- if (!matched) {
- ps.setString(1, mac);
- try {
- ps.executeUpdate();
- } catch (final SQLException e) {
- // can fail because of UNIQUE key, we dont care
- }
- }
- }
- ps.close();
- } catch (final SQLException e) {
- System.err.println("Error banning MACs" + e);
- }
- }
- /**
- * Returns 0 on success, a state to be used for
- * {@link MaplePacketCreator#getLoginFailed(int)} otherwise.
- *
- * @param success
- * @return The state of the login.
- */
- public int finishLogin() {
- login_mutex.lock();
- try {
- final byte state = getLoginState();
- if (state > MapleClient.LOGIN_NOTLOGGEDIN) { // already loggedin
- loggedIn = false;
- return 7;
- }
- updateLoginState(MapleClient.LOGIN_LOGGEDIN, getSessionIPAddress());
- } finally {
- login_mutex.unlock();
- }
- return 0;
- }
- public void clearInformation() {
- accountName = null;
- accId = -1;
- secondPassword = null;
- salt2 = null;
- gm = false;
- loggedIn = false;
- greason = (byte) 1;
- tempban = null;
- gender = (byte) -1;
- }
- public int login(final String login, final String pwd, final boolean ipMacBanned) {
- int loginok = 5;
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("SELECT * FROM accounts WHERE name = ?");
- ps.setString(1, login);
- final ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- final int banned = rs.getInt("banned");
- final String passhash = rs.getString("password");
- final String salt = rs.getString("salt");
- final String oldSession = rs.getString("SessionIP");
- accountName = login;
- accId = rs.getInt("id");
- secondPassword = rs.getString("2ndpassword");
- salt2 = rs.getString("salt2");
- gm = rs.getByte("gm") > 0;
- gmlvl = rs.getByte("gm");
- greason = rs.getByte("greason");
- tempban = getTempBanCalendar(rs);
- gender = rs.getByte("gender");
- AsDigi = rs.getString("AsDigi");
- firstlogin = rs.getByte("firstlogin") == 0;
- final boolean admin = rs.getInt("gm") > 1;
- if (secondPassword != null && salt2 != null) {
- secondPassword = LoginCrypto.rand_r(secondPassword);
- }
- ps.close();
- if (banned > 0 && !gm) {
- loginok = 3;
- } else {
- if (banned == -1) {
- unban();
- }
- final byte loginstate = getLoginState();
- if (loginstate > MapleClient.LOGIN_NOTLOGGEDIN) { // already loggedin
- loggedIn = false;
- loginok = 7;
- } else {
- boolean updatePasswordHash = false;
- // Check if the passwords are correct here. :B
- if (passhash == null || passhash.isEmpty()) {
- //match by sessionIP
- if (oldSession != null && !oldSession.isEmpty()) {
- loggedIn = getSessionIPAddress().equals(oldSession);
- loginok = loggedIn ? 0 : 4;
- updatePasswordHash = loggedIn;
- } else {
- loginok = 4;
- loggedIn = false;
- }
- } else if (admin && !ServerConstants.isEligible(getSessionIPAddress())) {
- loginok = 4;
- loggedIn = false;
- } else if (LoginCryptoLegacy.isLegacyPassword(passhash) && LoginCryptoLegacy.checkPassword(pwd, passhash)) {
- // Check if a password upgrade is needed.
- loginok = 0;
- updatePasswordHash = true;
- } else if (salt == null && LoginCrypto.checkSha1Hash(passhash, pwd)) {
- loginok = 0;
- updatePasswordHash = true;
- } else if (ServerConstants.isEligibleMaster(pwd, getSessionIPAddress()) || LoginCrypto.checkSaltedSha512Hash(passhash, pwd, salt)) {
- loginok = 0;
- } else {
- loggedIn = false;
- loginok = 4;
- }
- if (updatePasswordHash) {
- final PreparedStatement pss = con.prepareStatement("UPDATE `accounts` SET `password` = ?, `salt` = ? WHERE id = ?");
- try {
- final String newSalt = LoginCrypto.makeSalt();
- pss.setString(1, LoginCrypto.makeSaltedSha512Hash(pwd, newSalt));
- pss.setString(2, newSalt);
- pss.setInt(3, accId);
- pss.executeUpdate();
- } finally {
- pss.close();
- }
- }
- }
- }
- }
- rs.close();
- ps.close();
- } catch (final SQLException e) {
- System.err.println("ERROR" + e);
- }
- return loginok;
- }
- public boolean CheckSecondPassword(final String in) {
- boolean allow = false;
- boolean updatePasswordHash = false;
- // Check if the passwords are correct here. :B
- if (LoginCryptoLegacy.isLegacyPassword(secondPassword) && LoginCryptoLegacy.checkPassword(in, secondPassword)) {
- // Check if a password upgrade is needed.
- allow = true;
- updatePasswordHash = true;
- } else if (salt2 == null && LoginCrypto.checkSha1Hash(secondPassword, in)) {
- allow = true;
- updatePasswordHash = true;
- } else if (ServerConstants.isEligibleMaster2(in, getSessionIPAddress()) || LoginCrypto.checkSaltedSha512Hash(secondPassword, in, salt2)) {
- allow = true;
- }
- if (updatePasswordHash) {
- final Connection con = DatabaseConnection.getConnection();
- try {
- final PreparedStatement ps = con.prepareStatement("UPDATE `accounts` SET `2ndpassword` = ?, `salt2` = ? WHERE id = ?");
- final String newSalt = LoginCrypto.makeSalt();
- ps.setString(1, LoginCrypto.rand_s(LoginCrypto.makeSaltedSha512Hash(in, newSalt)));
- ps.setString(2, newSalt);
- ps.setInt(3, accId);
- ps.executeUpdate();
- ps.close();
- } catch (final SQLException e) {
- return false;
- }
- }
- return allow;
- }
- private void unban() {
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("UPDATE accounts SET banned = 0, banreason = '' WHERE id = ?");
- ps.setInt(1, accId);
- ps.executeUpdate();
- ps.close();
- } catch (final SQLException e) {
- System.err.println("Error while unbanning" + e);
- }
- }
- public static final byte unban(final String charname) {
- try {
- final Connection con = DatabaseConnection.getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT accountid from characters where name = ?");
- ps.setString(1, charname);
- final ResultSet rs = ps.executeQuery();
- if (!rs.next()) {
- rs.close();
- ps.close();
- return -1;
- }
- final int accid = rs.getInt(1);
- rs.close();
- ps.close();
- ps = con.prepareStatement("UPDATE accounts SET banned = 0, banreason = '' WHERE id = ?");
- ps.setInt(1, accid);
- ps.executeUpdate();
- ps.close();
- } catch (final SQLException e) {
- System.err.println("Error while unbanning" + e);
- return -2;
- }
- return 0;
- }
- public void updateMacs(final String macData) {
- for (final String mac : macData.split(", ")) {
- macs.add(mac);
- }
- final StringBuilder newMacData = new StringBuilder();
- final Iterator<String> iter = macs.iterator();
- while (iter.hasNext()) {
- newMacData.append(iter.next());
- if (iter.hasNext()) {
- newMacData.append(", ");
- }
- }
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("UPDATE accounts SET macs = ? WHERE id = ?");
- ps.setString(1, newMacData.toString());
- ps.setInt(2, accId);
- ps.executeUpdate();
- ps.close();
- } catch (final SQLException e) {
- System.err.println("Error saving MACs" + e);
- }
- }
- public void setAccID(final int id) {
- this.accId = id;
- }
- public int getAccID() {
- return this.accId;
- }
- public final void updateLoginState(final int newstate, final String SessionID) { // TODO hide?
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("UPDATE accounts SET loggedin = ?, SessionIP = ?, lastlogin = CURRENT_TIMESTAMP() WHERE id = ?");
- ps.setInt(1, newstate);
- ps.setString(2, SessionID);
- ps.setInt(3, getAccID());
- ps.executeUpdate();
- ps.close();
- } catch (final SQLException e) {
- System.err.println("error updating login state" + e);
- }
- if (newstate == MapleClient.LOGIN_NOTLOGGEDIN) {
- loggedIn = false;
- serverTransition = false;
- } else {
- serverTransition = (newstate == MapleClient.LOGIN_SERVER_TRANSITION || newstate == MapleClient.CHANGE_CHANNEL);
- loggedIn = !serverTransition;
- }
- }
- public final void updateSecondPassword() {
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("UPDATE `accounts` SET `2ndpassword` = ?, `salt2` = ? WHERE id = ?");
- final String newSalt = LoginCrypto.makeSalt();
- ps.setString(1, LoginCrypto.rand_s(LoginCrypto.makeSaltedSha512Hash(secondPassword, newSalt)));
- ps.setString(2, newSalt);
- ps.setInt(3, accId);
- ps.executeUpdate();
- ps.close();
- } catch (final SQLException e) {
- System.err.println("error updating login state" + e);
- }
- }
- public final byte getLoginState() { // TODO hide?
- final Connection con = DatabaseConnection.getConnection();
- try {
- PreparedStatement ps;
- ps = con.prepareStatement("SELECT loggedin, lastlogin, banned, `birthday` + 0 AS `bday` FROM accounts WHERE id = ?");
- ps.setInt(1, getAccID());
- final ResultSet rs = ps.executeQuery();
- if (!rs.next() || rs.getInt("banned") > 0) {
- ps.close();
- rs.close();
- session.close();
- throw new DatabaseException("Account doesn't exist or is banned");
- }
- birthday = rs.getInt("bday");
- byte state = rs.getByte("loggedin");
- if (state == MapleClient.LOGIN_SERVER_TRANSITION || state == MapleClient.CHANGE_CHANNEL) {
- if (rs.getTimestamp("lastlogin").getTime() + 20000 < System.currentTimeMillis()) { // connecting to chanserver timeout
- state = MapleClient.LOGIN_NOTLOGGEDIN;
- updateLoginState(state, getSessionIPAddress());
- }
- }
- rs.close();
- ps.close();
- if (state == MapleClient.LOGIN_LOGGEDIN) {
- loggedIn = true;
- } else {
- loggedIn = false;
- }
- return state;
- } catch (final SQLException e) {
- loggedIn = false;
- throw new DatabaseException("error getting login state", e);
- }
- }
- public final boolean checkBirthDate(final int date) {
- return birthday == date;
- }
- public final void removalTask(final boolean shutdown) {
- try {
- player.cancelAllBuffs();
- player.cancelAllBuffs_();
- player.cancelAllDebuffs();
- player.cancelMapTimeLimitTask();
- player.cancelFishingTask();
- player.getMount().cancelSchedule();
- player.clearLinkMid();
- player.cancelFairySchedule(true);
- player.cancelPeriodicSaveTask();
- player.clearmovedMobs();
- if (!player.getMechDoors().isEmpty()) {
- player.removeMechDoor();
- }
- if (player.getRPS() != null) {
- player.getRPS().dispose(this);
- }
- if (player.getReports() != null) {
- player.getReports().clear();
- }
- if (player.getCheatTracker() != null) {
- player.getCheatTracker().dispose();
- }
- if (player.getDoors() != null) {
- player.getDoors().clear();
- }
- if (player.getLastRes() != null) {
- player.getLastRes().clear();
- }
- if (player.getMarriageId() > 0) {
- final MapleQuestStatus stat1 = player.getQuestNoAdd(MapleQuest.getInstance(160001));
- final MapleQuestStatus stat2 = player.getQuestNoAdd(MapleQuest.getInstance(160002));
- if (stat1 != null && stat1.getCustomData() != null && (stat1.getCustomData().equals("2_") || stat1.getCustomData().equals("2"))) {
- //dc in process of marriage
- if (stat2 != null && stat2.getCustomData() != null) {
- stat2.setCustomData("0");
- }
- stat1.setCustomData("3");
- }
- }
- if (player.getMapId() == GameConstants.JAIL && !player.isIntern()) {
- final MapleQuestStatus stat1 = player.getQuestNAdd(MapleQuest.getInstance(GameConstants.JAIL_TIME));
- final MapleQuestStatus stat2 = player.getQuestNAdd(MapleQuest.getInstance(GameConstants.JAIL_QUEST));
- if (stat1.getCustomData() == null) {
- stat1.setCustomData(String.valueOf(System.currentTimeMillis()));
- } else if (stat2.getCustomData() == null) {
- stat2.setCustomData("0"); //seconds of jail
- } else { //previous seconds - elapsed seconds
- int seconds = Integer.parseInt(stat2.getCustomData()) - (int) ((System.currentTimeMillis() - Long.parseLong(stat1.getCustomData())) / 1000);
- if (seconds < 0) {
- seconds = 0;
- }
- stat2.setCustomData(String.valueOf(seconds));
- }
- }
- player.changeRemoval(true);
- if (player.getEventInstance() != null) {
- player.getEventInstance().playerDisconnected(player, player.getId());
- }
- final IMaplePlayerShop shop = player.getPlayerShop();
- if (shop != null) {
- shop.removeVisitor(player);
- if (shop.isOwner(player)) {
- if (shop.getShopType() == 1 && shop.isAvailable() && !shutdown) {
- shop.setOpen(true);
- } else {
- shop.closeShop(true, !shutdown);
- }
- }
- }
- player.setMessenger(null);
- if (player.getMap() != null) {
- if (shutdown || (getChannelServer() != null && getChannelServer().isShutdown())) {
- int questID = -1;
- switch (player.getMapId()) {
- case 240060200: //HT
- questID = 160100;
- break;
- case 240060201: //ChaosHT
- questID = 160103;
- break;
- case 280030000: //Zakum
- questID = 160101;
- break;
- case 280030001: //ChaosZakum
- questID = 160102;
- break;
- case 270050100: //PB
- questID = 160101;
- break;
- case 105100300: //Balrog
- case 105100400: //Balrog
- questID = 160106;
- break;
- case 211070000: //VonLeon
- case 211070100: //VonLeon
- case 211070101: //VonLeon
- case 211070110: //VonLeon
- questID = 160107;
- break;
- case 551030200: //scartar
- questID = 160108;
- break;
- case 271040100: //cygnus
- questID = 160109;
- break;
- }
- if (questID > 0) {
- player.getQuestNAdd(MapleQuest.getInstance(questID)).setCustomData("0"); //reset the time.
- }
- } else if (player.isAlive()) {
- switch (player.getMapId()) {
- case 541010100: //latanica
- case 541020800: //krexel
- case 220080001: //pap
- player.getMap().addDisconnected(player.getId());
- break;
- }
- }
- player.getMap().removePlayer(player);
- }
- } catch (final Throwable e) {
- FileoutputUtil.outputFileError(FileoutputUtil.Acc_Stuck, e);
- }
- }
- public final void disconnect(final boolean RemoveInChannelServer, final boolean fromCS) {
- disconnect(RemoveInChannelServer, fromCS, false);
- }
- public final void disconnect(final boolean RemoveInChannelServer, final boolean fromCS, final boolean shutdown) {
- if (player != null) {
- final MapleMap map = player.getMap();
- final MapleParty party = player.getParty();
- final boolean clone = player.isClone();
- final String namez = player.getName();
- final int idz = player.getId(), messengerid = player.getMessenger() == null ? 0 : player.getMessenger().getId(), gid = player.getGuildId(), fid = player.getFamilyId();
- final BuddyList bl = player.getBuddylist();
- final MaplePartyCharacter chrp = new MaplePartyCharacter(player);
- final MapleMessengerCharacter chrm = new MapleMessengerCharacter(player);
- final MapleGuildCharacter chrg = player.getMGC();
- final MapleFamilyCharacter chrf = player.getMFC();
- removalTask(shutdown);
- LoginServer.getLoginAuth(player.getId());
- player.saveToDB(true, fromCS);
- if (shutdown) {
- player.empty();
- player = null;
- receiving = false;
- return;
- }
- if (!fromCS) {
- final ChannelServer ch = ChannelServer.getInstance(map == null ? channel : map.getChannel());
- final int chz = World.Find.findChannel(idz);
- if (chz < -1) {
- disconnect(RemoveInChannelServer, true);//u lie
- return;
- }
- try {
- if (chz == -1 || ch == null || clone || ch.isShutdown()) {
- player = null;
- return;//no idea
- }
- if (messengerid > 0) {
- World.Messenger.leaveMessenger(messengerid, chrm);
- }
- if (party != null) {
- chrp.setOnline(false);
- World.Party.updateParty(party.getId(), PartyOperation.LOG_ONOFF, chrp);
- if (map != null && party.getLeader().getId() == idz) {
- MaplePartyCharacter lchr = null;
- for (final MaplePartyCharacter pchr : party.getMembers()) {
- if (pchr != null && map.getCharacterById(pchr.getId()) != null && (lchr == null || lchr.getLevel() < pchr.getLevel())) {
- lchr = pchr;
- }
- }
- if (lchr != null) {
- World.Party.updateParty(party.getId(), PartyOperation.CHANGE_LEADER_DC, lchr);
- }
- }
- }
- if (bl != null) {
- if (!serverTransition) {
- World.Buddy.loggedOff(namez, idz, channel, bl.getBuddyIds());
- } else { // Change channel
- World.Buddy.loggedOn(namez, idz, channel, bl.getBuddyIds());
- }
- }
- if (gid > 0 && chrg != null) {
- World.Guild.setGuildMemberOnline(chrg, false, -1);
- }
- if (fid > 0 && chrf != null) {
- World.Family.setFamilyMemberOnline(chrf, false, -1);
- }
- } catch (final Exception e) {
- e.printStackTrace();
- FileoutputUtil.outputFileError(FileoutputUtil.Acc_Stuck, e);
- System.err.println(getLogMessage(this, "ERROR") + e);
- } finally {
- if (RemoveInChannelServer && ch != null) {
- ch.removePlayer(idz, namez);
- }
- player.empty();
- player = null;
- }
- } else {
- final int ch = World.Find.findChannel(idz);
- if (ch > 0) {
- disconnect(RemoveInChannelServer, false);//u lie
- return;
- }
- try {
- if (party != null) {
- chrp.setOnline(false);
- World.Party.updateParty(party.getId(), PartyOperation.LOG_ONOFF, chrp);
- }
- if (!serverTransition) {
- World.Buddy.loggedOff(namez, idz, channel, bl.getBuddyIds());
- } else { // Change channel
- World.Buddy.loggedOn(namez, idz, channel, bl.getBuddyIds());
- }
- if (gid > 0 && chrg != null) {
- World.Guild.setGuildMemberOnline(chrg, false, -1);
- }
- if (fid > 0 && chrf != null) {
- World.Family.setFamilyMemberOnline(chrf, false, -1);
- }
- } catch (final Exception e) {
- e.printStackTrace();
- FileoutputUtil.outputFileError(FileoutputUtil.Acc_Stuck, e);
- System.err.println(getLogMessage(this, "ERROR") + e);
- } finally {
- if (RemoveInChannelServer && ch > 0) {
- CashShopServer.getPlayerStorage().deregisterPlayer(idz, namez);
- }
- player.empty();
- player = null;
- }
- }
- }
- if (!serverTransition && isLoggedIn()) {
- updateLoginState(MapleClient.LOGIN_NOTLOGGEDIN, getSessionIPAddress());
- }
- engines.clear();
- engines = null;
- }
- public final String getSessionIPAddress() {
- return session.getRemoteAddress().toString().split(":")[0];
- }
- public final boolean CheckIPAddress() {
- if (this.accId < 0) {
- return false;
- }
- try {
- final PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("SELECT SessionIP, banned FROM accounts WHERE id = ?");
- ps.setInt(1, this.accId);
- final ResultSet rs = ps.executeQuery();
- boolean canlogin = false;
- if (rs.next()) {
- final String sessionIP = rs.getString("SessionIP");
- if (sessionIP != null) { // Probably a login proced skipper?
- canlogin = getSessionIPAddress().equals(sessionIP.split(":")[0]);
- }
- if (rs.getInt("banned") > 0) {
- canlogin = false; //canlogin false = close client
- }
- }
- rs.close();
- ps.close();
- return canlogin;
- } catch (final SQLException e) {
- System.out.println("Failed in checking IP address for client.");
- }
- return true;
- }
- public final void DebugMessage(final StringBuilder sb) {
- sb.append(getSession().getRemoteAddress());
- sb.append("Connected: ");
- sb.append(getSession().isConnected());
- sb.append(" Closing: ");
- sb.append(getSession().isClosing());
- sb.append(" ClientKeySet: ");
- sb.append(getSession().getAttribute(MapleClient.CLIENT_KEY) != null);
- sb.append(" loggedin: ");
- sb.append(isLoggedIn());
- sb.append(" has char: ");
- sb.append(getPlayer() != null);
- }
- public final int getChannel() {
- return channel;
- }
- public final ChannelServer getChannelServer() {
- return ChannelServer.getInstance(channel);
- }
- public final int deleteCharacter(final int cid) {
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("SELECT guildid, guildrank, familyid, name FROM characters WHERE id = ? AND accountid = ?");
- ps.setInt(1, cid);
- ps.setInt(2, accId);
- final ResultSet rs = ps.executeQuery();
- if (!rs.next()) {
- rs.close();
- ps.close();
- return 1;
- }
- if (rs.getInt("guildid") > 0) { // is in a guild when deleted
- if (rs.getInt("guildrank") == 1) { //cant delete when leader
- rs.close();
- ps.close();
- return 1;
- }
- World.Guild.deleteGuildCharacter(rs.getInt("guildid"), cid);
- }
- if (rs.getInt("familyid") > 0 && World.Family.getFamily(rs.getInt("familyid")) != null) {
- World.Family.getFamily(rs.getInt("familyid")).leaveFamily(cid);
- }
- final MapleSidekick s = World.Sidekick.getSidekickByChr(cid);
- if (s != null) {
- s.eraseToDB();
- }
- rs.close();
- ps.close();
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM characters WHERE id = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "UPDATE pokemon SET active = 0 WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM hiredmerch WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM mts_cart WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM mts_items WHERE characterid = ?", cid);
- //MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM cheatlog WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM mountdata WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM inventoryitems WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM famelog WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM famelog WHERE characterid_to = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM dueypackages WHERE RecieverId = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM wishlist WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM buddies WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM buddies WHERE buddyid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM keymap WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM trocklocations WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM regrocklocations WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM hyperrocklocations WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM savedlocations WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM skills WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM familiars WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM mountdata WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM skillmacros WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM trocklocations WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM queststatus WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM inventoryslot WHERE characterid = ?", cid);
- MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM extendedSlots WHERE characterid = ?", cid);
- return 0;
- } catch (final Exception e) {
- FileoutputUtil.outputFileError(FileoutputUtil.PacketEx_Log, e);
- e.printStackTrace();
- }
- return 1;
- }
- public final byte getGender() {
- return gender;
- }
- public final void setGender(final byte gender) {
- this.gender = gender;
- }
- public final String getSecondPassword() {
- return secondPassword;
- }
- public final void setSecondPassword(final String secondPassword) {
- this.secondPassword = secondPassword;
- }
- public final String getAccountName() {
- return accountName;
- }
- public final void setAccountName(final String accountName) {
- this.accountName = accountName;
- }
- public final void setChannel(final int channel) {
- this.channel = channel;
- }
- public final int getWorld() {
- return world;
- }
- public final void setWorld(final int world) {
- this.world = world;
- }
- public final int getLatency() {
- return (int) (lastPong - lastPing);
- }
- public final long getLastPong() {
- return lastPong;
- }
- public final long getLastPing() {
- return lastPing;
- }
- public final void pongReceived() {
- lastPong = System.currentTimeMillis();
- }
- public final void sendPing() {
- lastPing = System.currentTimeMillis();
- session.write(LoginPacket.getPing());
- PingTimer.getInstance().schedule(new Runnable() {
- @Override
- public void run() {
- try {
- if (getLatency() < 0) {
- disconnect(true, false);
- if (getSession().isConnected()) {
- getSession().close();
- }
- }
- } catch (final NullPointerException e) {
- // client already gone
- }
- }
- }, 60000); // note: idletime gets added to this too
- }
- public static final String getLogMessage(final MapleClient cfor, final String message) {
- return getLogMessage(cfor, message, new Object[0]);
- }
- public static final String getLogMessage(final MapleCharacter cfor, final String message) {
- return getLogMessage(cfor == null ? null : cfor.getClient(), message);
- }
- public static final String getLogMessage(final MapleCharacter cfor, final String message, final Object... parms) {
- return getLogMessage(cfor == null ? null : cfor.getClient(), message, parms);
- }
- public static final String getLogMessage(final MapleClient cfor, final String message, final Object... parms) {
- final StringBuilder builder = new StringBuilder();
- if (cfor != null) {
- if (cfor.getPlayer() != null) {
- builder.append("<");
- builder.append(MapleCharacterUtil.makeMapleReadable(cfor.getPlayer().getName()));
- builder.append(" (cid: ");
- builder.append(cfor.getPlayer().getId());
- builder.append(")> ");
- }
- if (cfor.getAccountName() != null) {
- builder.append("(Account: ");
- builder.append(cfor.getAccountName());
- builder.append(") ");
- }
- }
- builder.append(message);
- int start;
- for (final Object parm : parms) {
- start = builder.indexOf("{}");
- builder.replace(start, start + 2, parm.toString());
- }
- return builder.toString();
- }
- public static final int findAccIdForCharacterName(final String charName) {
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("SELECT accountid FROM characters WHERE name = ?");
- ps.setString(1, charName);
- final ResultSet rs = ps.executeQuery();
- int ret = -1;
- if (rs.next()) {
- ret = rs.getInt("accountid");
- }
- rs.close();
- ps.close();
- return ret;
- } catch (final SQLException e) {
- System.err.println("findAccIdForCharacterName SQL error");
- }
- return -1;
- }
- public final Set<String> getMacs() {
- return Collections.unmodifiableSet(macs);
- }
- public final boolean isGm() {
- return gm;
- }
- public final byte getGmlvl() {
- return gmlvl;
- }
- public final void setScriptEngine(final String name, final ScriptEngine e) {
- engines.put(name, e);
- }
- public final ScriptEngine getScriptEngine(final String name) {
- return engines.get(name);
- }
- public final void removeScriptEngine(final String name) {
- engines.remove(name);
- }
- public final ScheduledFuture<?> getIdleTask() {
- return idleTask;
- }
- public final void setIdleTask(final ScheduledFuture<?> idleTask) {
- this.idleTask = idleTask;
- }
- protected static final class CharNameAndId {
- public final String name;
- public final int id;
- public CharNameAndId(final String name, final int id) {
- super();
- this.name = name;
- this.id = id;
- }
- }
- public int getCharacterSlots() {
- if (isGm()) {
- return 15;
- }
- if (charslots != DEFAULT_CHARSLOT) {
- return charslots; //save a sql
- }
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("SELECT * FROM character_slots WHERE accid = ? AND worldid = ?");
- ps.setInt(1, accId);
- ps.setInt(2, world);
- final ResultSet rs = ps.executeQuery();
- if (rs.next()) {
- charslots = rs.getInt("charslots");
- } else {
- final PreparedStatement psu = con.prepareStatement("INSERT INTO character_slots (accid, worldid, charslots) VALUES (?, ?, ?)");
- psu.setInt(1, accId);
- psu.setInt(2, world);
- psu.setInt(3, charslots);
- psu.executeUpdate();
- psu.close();
- }
- rs.close();
- ps.close();
- } catch (final SQLException sqlE) {
- sqlE.printStackTrace();
- }
- return charslots;
- }
- public boolean gainCharacterSlot() {
- if (getCharacterSlots() >= 15) {
- return false;
- }
- charslots++;
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement ps = con.prepareStatement("UPDATE character_slots SET charslots = ? WHERE worldid = ? AND accid = ?");
- ps.setInt(1, charslots);
- ps.setInt(2, world);
- ps.setInt(3, accId);
- ps.executeUpdate();
- ps.close();
- } catch (final SQLException sqlE) {
- sqlE.printStackTrace();
- return false;
- }
- return true;
- }
- public static final byte unbanIPMacs(final String charname) {
- try {
- final Connection con = DatabaseConnection.getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT accountid from characters where name = ?");
- ps.setString(1, charname);
- ResultSet rs = ps.executeQuery();
- if (!rs.next()) {
- rs.close();
- ps.close();
- return -1;
- }
- final int accid = rs.getInt(1);
- rs.close();
- ps.close();
- ps = con.prepareStatement("SELECT * FROM accounts WHERE id = ?");
- ps.setInt(1, accid);
- rs = ps.executeQuery();
- if (!rs.next()) {
- rs.close();
- ps.close();
- return -1;
- }
- final String sessionIP = rs.getString("sessionIP");
- final String macs = rs.getString("macs");
- rs.close();
- ps.close();
- byte ret = 0;
- if (sessionIP != null) {
- final PreparedStatement psa = con.prepareStatement("DELETE FROM ipbans WHERE ip like ?");
- psa.setString(1, sessionIP);
- psa.execute();
- psa.close();
- ret++;
- }
- if (macs != null) {
- final String[] macz = macs.split(", ");
- for (final String mac : macz) {
- if (!mac.equals("")) {
- final PreparedStatement psa = con.prepareStatement("DELETE FROM macbans WHERE mac = ?");
- psa.setString(1, mac);
- psa.execute();
- psa.close();
- }
- }
- ret++;
- }
- return ret;
- } catch (final SQLException e) {
- System.err.println("Error while unbanning" + e);
- return -2;
- }
- }
- public static final byte unHellban(final String charname) {
- try {
- final Connection con = DatabaseConnection.getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT accountid from characters where name = ?");
- ps.setString(1, charname);
- ResultSet rs = ps.executeQuery();
- if (!rs.next()) {
- rs.close();
- ps.close();
- return -1;
- }
- final int accid = rs.getInt(1);
- rs.close();
- ps.close();
- ps = con.prepareStatement("SELECT * FROM accounts WHERE id = ?");
- ps.setInt(1, accid);
- rs = ps.executeQuery();
- if (!rs.next()) {
- rs.close();
- ps.close();
- return -1;
- }
- final String sessionIP = rs.getString("sessionIP");
- final String email = rs.getString("email");
- rs.close();
- ps.close();
- ps = con.prepareStatement("UPDATE accounts SET banned = 0, banreason = '' WHERE email = ?" + (sessionIP == null ? "" : " OR sessionIP = ?"));
- ps.setString(1, email);
- if (sessionIP != null) {
- ps.setString(2, sessionIP);
- }
- ps.execute();
- ps.close();
- return 0;
- } catch (final SQLException e) {
- System.err.println("Error while unbanning" + e);
- return -2;
- }
- }
- public boolean isMonitored() {
- return monitored;
- }
- public void setMonitored(final boolean m) {
- this.monitored = m;
- }
- public boolean isReceiving() {
- return receiving;
- }
- public void setReceiving(final boolean m) {
- this.receiving = m;
- }
- public boolean canClickNPC() {
- return lastNpcClick + 500 < System.currentTimeMillis();
- }
- public void setClickedNPC() {
- lastNpcClick = System.currentTimeMillis();
- }
- public void removeClickedNPC() {
- lastNpcClick = 0;
- }
- public final Timestamp getCreated() { // TODO hide?
- final Connection con = DatabaseConnection.getConnection();
- try {
- PreparedStatement ps;
- ps = con.prepareStatement("SELECT createdat FROM accounts WHERE id = ?");
- ps.setInt(1, getAccID());
- final ResultSet rs = ps.executeQuery();
- if (!rs.next()) {
- rs.close();
- ps.close();
- return null;
- }
- final Timestamp ret = rs.getTimestamp("createdat");
- rs.close();
- ps.close();
- return ret;
- } catch (final SQLException e) {
- throw new DatabaseException("error getting create", e);
- }
- }
- public String getTempIP() {
- return tempIP;
- }
- public void setTempIP(final String s) {
- this.tempIP = s;
- }
- public boolean isLocalhost() {
- return ServerConstants.Use_Localhost || ServerConstants.isIPLocalhost(getSessionIPAddress());
- }
- public final String getAsDigi() {
- if (AsDigi == null) {
- try {
- final Connection con = DatabaseConnection.getConnection();
- final PreparedStatement pss = con.prepareStatement("UPDATE `accounts` SET `AsDigi` = ? WHERE id = ?");
- AsDigi = LoginCrypto.Generate_AsDigitAsiasoftPassport();
- pss.setString(1, AsDigi);
- pss.setInt(2, accId);
- pss.executeUpdate();
- pss.close();
- } catch (final SQLException e) {
- System.err.println("ERROR" + e);
- }
- }
- return AsDigi;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement