public class UserLoginAction {
public UserLoginAction(InetAddress ip, LoginPacket lp) throws SQLException, UnknownHostException, IOException, RequestException {
// Check Login
ArrayList<User> alu = UserManager.searchUserAbsolute(new User(null, lp.getUsername(), lp.getPwd(), null, null, null, null));
if (alu.size() != 1) {
NetworkService.sendPacket(ip, new Packet(EnumPacket.SERVER_Error, Messages.getString("UserLoginAction.UsernamePasswordInvalid"))); // Send notice //$NON-NLS-1$
throw new RequestException(Messages.getString("UserLoginAction.ERR_WrongUsername")); //$NON-NLS-1$
}
// Get the user
User u = alu.get(0);
// Check if already logged
if (u.getOnline()) {
NetworkService.sendPacket(u.getIp(), new Packet(EnumPacket.SERVER_Error, Messages.getString("UserLoginAction.ERR_DualLoginKick"))); //$NON-NLS-1$
new KickUserAction(u);
throw new RequestException(Messages.getString("UserLoginAction.ERR_DualLogin"));
}
// Set online
u.setOnline(true);
u.setIp(ip.getHostAddress());
UserManager.updateUserById(u);
// Send updates to the client
NetworkService.sendPacket(ip, new Packet(EnumPacket.SERVER_LoginAccepted, u)); // Send
// responce
new SendContactListAction(u); // Send contacts
NetworkService.sendPacket(ip, new Packet(EnumPacket.SERVER_Notice, Messages.getString("UserLoginAction.WelcomeMsg"))); // Send notice //$NON-NLS-1$
// Update contact list to his online members
ArrayList<User> alcu = UserManager.getUserContactsByUser(u);
for (User iU : alcu) {
if (iU.getOnline() && (iU.getIp() != null)) {
new SendContactListAction(iU);
NetworkService.sendPacket(iU.getIp(), new Packet(EnumPacket.SERVER_Notice, String.format(Messages.getString("UserLoginAction.UserConnect"), u.getUsername()))); //$NON-NLS-1$
}
}
}