/**
* License THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS
* CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED
* BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS
* AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
*
* BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO
* BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE
* CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED
* HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
*
*/
package l1j.server.server;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.logging.Level;
import java.util.logging.Logger;
import l1j.server.Base64;
import l1j.server.L1DatabaseFactory;
import l1j.server.server.utils.SQLUtil;
/**
*
*/
public class Account {
/** */
private String _name;
/** IP */
private String _ip;
/** */
private String _password;
/** */
private Timestamp _lastActive;
/** (GM) */
private int _accessLevel;
/** DNS */
private String _host;
/** (True ). */
private boolean _banned;
/** */
private int _characterSlot;
/** (True ). */
private boolean _isValid = false;
/** */
private int _WarePassword = 0;
/** */
private static Logger _log = Logger.getLogger(Account.class.getName());
/**
*
*/
private Account() {
}
/**
*
*
* @param rawPassword
*
* @return String
* @throws NoSuchAlgorithmException
*
* @throws UnsupportedEncodingException
*
*/
private static String encodePassword(final String rawPassword) throws NoSuchAlgorithmException, UnsupportedEncodingException {
byte[] buf = rawPassword.getBytes("UTF-8");
buf = MessageDigest.getInstance("SHA").digest(buf);
return Base64.encodeBytes(buf);
}
/**
*
*
* @param name
*
* @param rawPassword
*
* @param ip
* IP
* @param host
* dns
* @return Account
*/
public static Account create(final String name, final String rawPassword, final String ip, final String host) {
Connection con = null;
PreparedStatement pstm = null;
try {
Account account = new Account();
account._name = name;
account._password = encodePassword(rawPassword);
account._ip = ip;
account._host = host;
account._banned = false;
account._lastActive = new Timestamp(System.currentTimeMillis());
con = L1DatabaseFactory.getInstance().getConnection();
String sqlstr = "INSERT INTO accounts SET login=?,password=?,lastactive=?,access_level=?,ip=?,host=?,banned=?,character_slot=?";
pstm = con.prepareStatement(sqlstr);
pstm.setString(1, account._name);
pstm.setString(2, account._password);
pstm.setTimestamp(3, account._lastActive);
pstm.setInt(4, 0);
pstm.setString(5, account._ip);
pstm.setString(6, account._host);
pstm.setInt(7, account._banned ? 1 : 0);
pstm.setInt(8, 0);
pstm.execute();
_log.info("created new account for " + name);
return account;
}
catch (SQLException e) {
_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
catch (NoSuchAlgorithmException e) {
_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
catch (UnsupportedEncodingException e) {
_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
finally {
SQLUtil.close(pstm);
SQLUtil.close(con);
}
return null;
}
/**
*
*
* @param name
*
* @return Account
*/
public static Account load(final String name) {
Connection con = null;
PreparedStatement pstm = null;
ResultSet rs = null;
Account account = null;
try {
con = L1DatabaseFactory.getInstance().getConnection();
String sqlstr = "SELECT * FROM accounts WHERE login=? LIMIT 1";
pstm = con.prepareStatement(sqlstr);
pstm.setString(1, name);
rs = pstm.executeQuery();
if (!rs.next()) {
return null;
}
account = new Account();
account._name = rs.getString("login");
account._password = rs.getString("password");
account._lastActive = rs.getTimestamp("lastactive");
account._accessLevel = rs.getInt("access_level");
account._ip = rs.getString("ip");
account._host = rs.getString("host");
account._banned = rs.getInt("banned") == 0 ? false : true;
account._characterSlot = rs.getInt("character_slot");
account._WarePassword = rs.getInt("warepassword");
_log.fine("account exists");
}
catch (SQLException e) {
_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
finally {
SQLUtil.close(rs);
SQLUtil.close(pstm);
SQLUtil.close(con);
}
return account;
}
/**
*
*
* @param account
*
*/
public static void updateLastActive(final Account account, final String ip) {
Connection con = null;
PreparedStatement pstm = null;
Timestamp ts = new Timestamp(System.currentTimeMillis());
try {
con = L1DatabaseFactory.getInstance().getConnection();
String sqlstr = "UPDATE accounts SET lastactive=?, ip=? WHERE login = ?";
pstm = con.prepareStatement(sqlstr);
pstm.setTimestamp(1, ts);
pstm.setString(2, ip);
pstm.setString(3, account.getName());
pstm.execute();
account._lastActive = ts;
_log.fine("update lastactive for " + account.getName());
}
catch (Exception e) {
_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
finally {
SQLUtil.close(pstm);
SQLUtil.close(con);
}
}
/**
*
*
* @param account
*
*/
public static void updateCharacterSlot(final Account account) {
Connection con = null;
PreparedStatement pstm = null;
try {
con = L1DatabaseFactory.getInstance().getConnection();
String sqlstr = "UPDATE accounts SET character_slot=? WHERE login=?";
pstm = con.prepareStatement(sqlstr);
pstm.setInt(1, account.getCharacterSlot());
pstm.setString(2, account.getName());
pstm.execute();
account._characterSlot = account.getCharacterSlot();
_log.fine("update characterslot for " + account.getName());
}
catch (Exception e) {
_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
finally {
SQLUtil.close(pstm);
SQLUtil.close(con);
}
}
/**
*
*
* @return int
*/
public int countCharacters() {
int result = 0;
Connection con = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
con = L1DatabaseFactory.getInstance().getConnection();
String sqlstr = "SELECT count(*) as cnt FROM characters WHERE account_name=?";
pstm = con.prepareStatement(sqlstr);
pstm.setString(1, _name);
rs = pstm.executeQuery();
if (rs.next()) {
result = rs.getInt("cnt");
}
}
catch (SQLException e) {
_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
finally {
SQLUtil.close(rs);
SQLUtil.close(pstm);
SQLUtil.close(con);
}
return result;
}
/**
*
*
* @param login
*
*/
public static void ban(final String login) {
Connection con = null;
PreparedStatement pstm = null;
try {
con = L1DatabaseFactory.getInstance().getConnection();
String sqlstr = "UPDATE accounts SET banned=1 WHERE login=?";
pstm = con.prepareStatement(sqlstr);
pstm.setString(1, login);
pstm.execute();
}
catch (SQLException e) {
_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
finally {
SQLUtil.close(pstm);
SQLUtil.close(con);
}
}
/**
*
*
* @param rawPassword
*
* @return boolean
*/
public boolean validatePassword(final String rawPassword) {
//
if (_isValid) {
return false;
}
try {
_isValid = _password.equals(encodePassword(rawPassword));
if (_isValid) {
_password = null; //
}
return _isValid;
}
catch (Exception e) {
_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
return false;
}
/**
*
*
* @param newPassword
*
*/
public void changeWarePassword(int newPassword) {
Connection con = null;
PreparedStatement pstm = null;
try {
con = L1DatabaseFactory.getInstance().getConnection();
pstm = con.prepareStatement("UPDATE `accounts` SET `warepassword` = ? WHERE `login` = ?");
pstm.setInt(1, newPassword);
pstm.setString(2, getName());
pstm.execute();
_WarePassword = newPassword;
}
catch (SQLException e) {}
finally {
SQLUtil.close(pstm);
SQLUtil.close(con);
}
}
/**
* (True ).
*
* @return boolean
*/
public boolean isValid() {
return _isValid;
}
/**
* GM (True GM).
*
* @return boolean
*/
public boolean isGameMaster() {
return 0 < _accessLevel;
}
/**
*
*
* @return String
*/
public String getName() {
return _name;
}
/**
* IP
*
* @return String
*/
public String getIp() {
return _ip;
}
/**
*
*/
public Timestamp getLastActive() {
return _lastActive;
}
/**
*
*
* @return int
*/
public int getAccessLevel() {
return _accessLevel;
}
/**
* DNS
*
* @return String
*/
public String getHost() {
return _host;
}
/**
*
*
* @return boolean
*/
public boolean isBanned() {
return _banned;
}
/**
*
*
* @return int
*/
public int getCharacterSlot() {
return _characterSlot;
}
/**
*
*
* @param i
*
*/
public void setCharacterSlot(int i) {
_characterSlot = i;
}
/**
*
*
* @return
*/
public int getWarePassword() {
return _WarePassword;
}
}
|