Skip to content

Commit

Permalink
introduce RenameAccountCommandProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
hoijui committed Jan 18, 2011
1 parent 97f2bd2 commit 3004508
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 61 deletions.
61 changes: 0 additions & 61 deletions src/main/java/com/springrts/tasserver/TASServer.java
Expand Up @@ -539,67 +539,6 @@ public boolean tryToExecCommand(String command, Client client) {
if (response.substring(0, 12).toUpperCase().equals("SERVERMSGBOX")) {
context.getClients().killClient(client);
}
} else if (commands[0].equals("RENAMEACCOUNT")) {
if (client.getAccount().getAccess().compareTo(Account.Access.NORMAL) < 0) {
return false;
}

if (commands.length != 2) {
client.sendLine("SERVERMSG Bad RENAMEACCOUNT command - too many or too few parameters");
return false;
}

if (context.getServer().isLanMode()) {
client.sendLine("SERVERMSG RENAMEACCOUNT failed: You cannot rename your account while server is running in LAN mode since you have no account!");
return false;
}

// validate new userName:
String valid = Account.isOldUsernameValid(commands[1]);
if (valid != null) {
client.sendLine(new StringBuilder("SERVERMSG RENAMEACCOUNT failed: Invalid username (reason: ").append(valid).append(")").toString());
return false;
}

Account acc = context.getAccountsService().findAccountNoCase(commands[1]);
if (acc != null && acc != client.getAccount()) {
client.sendLine("SERVERMSG RENAMEACCOUNT failed: Account with same username already exists!");
return false;
}

// make sure all mutes are accordingly adjusted to new userName:
for (int i = 0; i < context.getChannels().getChannelsSize(); i++) {
context.getChannels().getChannel(i).getMuteList().rename(client.getAccount().getName(), commands[1]);
}

final String oldName = client.getAccount().getName();
Account account_new = client.getAccount().clone();
account_new.setName(commands[1]);
account_new.setLastLogin(System.currentTimeMillis());
account_new.setLastIP(client.getIp());
final boolean mergeOk = context.getAccountsService().mergeAccountChanges(account_new, client.getAccount().getName());
if (mergeOk) {
client.setAccount(account_new);
} else {
client.sendLine("SERVERMSG Your account renaming failed.");
return false;
}

client.sendLine(new StringBuilder("SERVERMSG Your account has been renamed to <")
.append(account_new.getName())
.append(">. Reconnect with new account (you will now be automatically disconnected)!").toString());
context.getClients().killClient(client, "Quit: renaming account");
context.getAccountsService().saveAccounts(false); // let's save new accounts info to disk
context.getClients().sendToAllAdministrators(new StringBuilder("SERVERMSG [broadcast to all admins]: User <")
.append(oldName).append("> has just renamed his account to <")
.append(client.getAccount().getName()).append(">").toString());

// add server notification:
ServerNotification sn = new ServerNotification("Account renamed");
sn.addLine(new StringBuilder("User <")
.append(oldName).append("> has renamed his account to <")
.append(client.getAccount().getName()).append(">").toString());
context.getServerNotifications().addNotification(sn);
} else if (commands[0].equals("CHANGEPASSWORD")) {
if (client.getAccount().getAccess().compareTo(Account.Access.NORMAL) < 0) {
return false;
Expand Down
Expand Up @@ -68,6 +68,7 @@
import com.springrts.tasserver.commands.impl.RedirectOffCommandProcessor;
import com.springrts.tasserver.commands.impl.RegisterCommandProcessor;
import com.springrts.tasserver.commands.impl.RemoveAccountCommandProcessor;
import com.springrts.tasserver.commands.impl.RenameAccountCommandProcessor;
import com.springrts.tasserver.commands.impl.SaveAccountsServerCommandProcessor;
import com.springrts.tasserver.commands.impl.SetBotModeCommandProcessor;
import com.springrts.tasserver.commands.impl.SetChannelKeyCommandProcessor;
Expand Down Expand Up @@ -206,6 +207,7 @@ public void init() {
commandProcessorClasses.add(LoginCommandProcessor.class);
commandProcessorClasses.add(ConfirmAgreementCommandProcessor.class);
commandProcessorClasses.add(UserIdCommandProcessor.class);
commandProcessorClasses.add(RenameAccountCommandProcessor.class);

try {
load(commandProcessorClasses);
Expand Down
@@ -0,0 +1,113 @@
/*
Copyright (c) 2010 Robin Vobruba <robin.vobruba@derisk.ch>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.springrts.tasserver.commands.impl;


import com.springrts.tasserver.Account;
import com.springrts.tasserver.Client;
import com.springrts.tasserver.Misc;
import com.springrts.tasserver.ServerNotification;
import com.springrts.tasserver.commands.AbstractCommandProcessor;
import com.springrts.tasserver.commands.CommandProcessingException;
import com.springrts.tasserver.commands.InvalidNumberOfArgumentsCommandProcessingException;
import com.springrts.tasserver.commands.SupportedCommand;
import java.util.List;

/**
* Allows a user to change his username.
* @author hoijui
*/
@SupportedCommand("RENAMEACCOUNT")
public class RenameAccountCommandProcessor extends AbstractCommandProcessor {

public RenameAccountCommandProcessor() {
super(1, 1, Account.Access.NORMAL);
}

@Override
public boolean process(Client client, List<String> args)
throws CommandProcessingException
{
boolean checksOk = false;
try {
checksOk = super.process(client, args);
} catch (InvalidNumberOfArgumentsCommandProcessingException ex) {
client.sendLine("SERVERMSG Bad RENAMEACCOUNT command - too many or too few parameters");
throw ex;
}
if (!checksOk) {
return false;
}

String newUsername = Misc.makeSentence(args, 0);

if (getContext().getServer().isLanMode()) {
client.sendLine("SERVERMSG RENAMEACCOUNT failed: You cannot rename your account while server is running in LAN mode since you have no account!");
return false;
}

// validate new userName:
String valid = Account.isOldUsernameValid(newUsername);
if (valid != null) {
client.sendLine(new StringBuilder("SERVERMSG RENAMEACCOUNT failed: Invalid username (reason: ").append(valid).append(")").toString());
return false;
}

Account acc = getContext().getAccountsService().findAccountNoCase(newUsername);
if (acc != null && acc != client.getAccount()) {
client.sendLine("SERVERMSG RENAMEACCOUNT failed: Account with same username already exists!");
return false;
}

// make sure all mutes are accordingly adjusted to new userName:
for (int i = 0; i < getContext().getChannels().getChannelsSize(); i++) {
getContext().getChannels().getChannel(i).getMuteList().rename(client.getAccount().getName(), newUsername);
}

final String oldName = client.getAccount().getName();
Account account_new = client.getAccount().clone();
account_new.setName(newUsername);
account_new.setLastLogin(System.currentTimeMillis());
account_new.setLastIP(client.getIp());
final boolean mergeOk = getContext().getAccountsService().mergeAccountChanges(account_new, client.getAccount().getName());
if (mergeOk) {
client.setAccount(account_new);
} else {
client.sendLine("SERVERMSG Your account renaming failed.");
return false;
}

client.sendLine(new StringBuilder("SERVERMSG Your account has been renamed to <")
.append(account_new.getName())
.append(">. Reconnect with new account (you will now be automatically disconnected)!").toString());
getContext().getClients().killClient(client, "Quit: renaming account");
getContext().getAccountsService().saveAccounts(false); // let's save new accounts info to disk
getContext().getClients().sendToAllAdministrators(new StringBuilder("SERVERMSG [broadcast to all admins]: User <")
.append(oldName).append("> has just renamed his account to <")
.append(client.getAccount().getName()).append(">").toString());

// add server notification:
ServerNotification sn = new ServerNotification("Account renamed");
sn.addLine(new StringBuilder("User <")
.append(oldName).append("> has renamed his account to <")
.append(client.getAccount().getName()).append(">").toString());
getContext().getServerNotifications().addNotification(sn);

return true;
}
}

0 comments on commit 3004508

Please sign in to comment.