Example #1
0
void ChatServer::processMessage(PasswordChangeRequest *msg)
{
    PasswordChangeResult *answer = new PasswordChangeResult;
    ChatClient client = m_clientList.getClient(msg->username);
    QString log;
    if(client.password() == msg->oldPassword)
    {
        client.setPassword(msg->newPassword);
        m_clientList.updateClient(client);
        answer->result = "Password changed!\nDon't forget to use new password in the next authorization.";
        log = "User " + msg->username + " changed his password";
        emit updateTable("clients");
    }
    else
    {
        answer->result = "Given old password is incorrect!";
        log = "User " + msg->username + " tried to change his password. Unfortunately, unsuccessfull.";
    }
    emit serverLog(esNotify, log);
    sendMessageToClient(msg->username, answer);
}
Example #2
0
void ChatHandler::handleGuildQuit(ChatClient &client, MessageIn &msg)
{
    MessageOut reply(CPMSG_GUILD_QUIT_RESPONSE);
    short guildId = msg.readInt16();

    Guild *guild = guildManager->findById(guildId);
    if (!guild || !guild->checkInGuild(client.characterId))
    {
        reply.writeInt8(ERRMSG_FAILURE);
        client.send(reply);
        return;
    }
    guildManager->removeGuildMember(guild, client.characterId,
                                    client.characterName, &client);

    reply.writeInt8(ERRMSG_OK);
    reply.writeInt16(guildId);
    client.send(reply);


}
Example #3
0
void ChatHandler::handlePartyRejectInvite(ChatClient &client, MessageIn &msg)
{
    MessageOut out(CPMSG_PARTY_REJECTED);

    std::string inviter = msg.readString();


    std::vector<PartyInvite>::iterator itr, itr_end;

    itr = mPartyInvitedUsers.begin();
    itr_end = mPartyInvitedUsers.end();
    bool found = false;

    while (itr != itr_end)
    {
        // Check that the player was invited
        if ((*itr).mInvited == client.characterName &&
            (*itr).mInviter == inviter)
        {
            // remove them from invited users list
            mPartyInvitedUsers.erase(itr);
            found = true;
            break;
        }

        ++itr;
    }

    if (!found)
    {
        out.writeInt8(ERRMSG_FAILURE);
    }

    // send rejection to inviter
    ChatClient *inviterClient = getClient(inviter);

    inviterClient->send(out);
}
Example #4
0
void ChatHandler::handlePartyInvite(ChatClient &client, MessageIn &msg)
{
    //TODO: Handle errors
    MessageOut out(CPMSG_PARTY_INVITED);

    out.writeString(client.characterName);

    const std::string invited = msg.readString();
    if (invited == client.characterName)
    {
        return;
    }
    if (!invited.empty())
    {
        // Get client and send it the invite
        ChatClient *c = getClient(invited);
        if (c)
        {
            ++client.numInvites;

            // TODO: Check number of invites
            // and do something if too many in a short time

            // store the invite
            PartyInvite invite;
            invite.mInvited = invited;
            invite.mInviter = client.characterName;
            if (client.party)
                invite.mPartyId = client.party->getId();
            else
                invite.mPartyId = 0;

            mPartyInvitedUsers.push_back(invite);

            c->send(out);
        }
    }
}
Example #5
0
void ChatHandler::handlePartyInvite(MessageIn &msg)
{
    std::string inviterName = msg.readString();
    std::string inviteeName = msg.readString();
    ChatClient *inviter = getClient(inviterName);
    ChatClient *invitee = getClient(inviteeName);

    if (!inviter || !invitee)
        return;

    removeExpiredPartyInvites();
    const int maxInvitesPerTimeframe = 10;
    int &num = mNumInvites[inviterName];
    if (num >= maxInvitesPerTimeframe)
    {
        MessageOut out(CPMSG_PARTY_REJECTED);
        out.writeString(inviterName);
        out.writeInt8(ERRMSG_LIMIT_REACHED);
        inviter->send(out);
        return;
    }
    ++num;

    if (invitee->party)
    {
        MessageOut out(CPMSG_PARTY_REJECTED);
        out.writeString(inviterName);
        out.writeInt8(ERRMSG_FAILURE);
        inviter->send(out);
        return;
    }

    mInvitations.push_back(PartyInvite(inviterName, inviteeName));

    MessageOut out(CPMSG_PARTY_INVITED);
    out.writeString(inviterName);
    invitee->send(out);
}
Example #6
0
//By Luiz45 - Add a "Friend" to a blackList
void ChatClient::SendBlackListAdd(sUT_FRIEND_BLACK_ADD_REQ* pData)
{
	sTU_FRIEND_BLACK_ADD_RES pBlackAdd;
	memset(&pBlackAdd, 0, sizeof(pBlackAdd));
	pBlackAdd.wOpCode = TU_FRIEND_ADD_RES;
	memcpy(pBlackAdd.wchName, pData->awchName, NTL_MAX_SIZE_CHAR_NAME_UNICODE);
	ChatClient* pChatToWhisper = pServer->GetChatManager()->GetChatClient(pData->awchName);
	if (NULL == pChatToWhisper){
		pBlackAdd.wResultCode = COMMUNITY_FRIEND_CHAR_NOT_FOUND;
	}
	else{
		if (SearchFriendDB(pChatToWhisper->GetCharSerialID()))
		{
			pBlackAdd.wResultCode = COMMUNITY_FRIEND_CHAR_ARLEADY_ADDED;
		}
		else
		{
			pServer->ServerDB->ExecuteQuery("INSERT INTO `blacklist`(`owner_id`,`target_id`) VALUES ('%u','%u');", this->GetCharSerialID(), pChatToWhisper->GetCharSerialID());
			pBlackAdd.targetID = pChatToWhisper->GetCharSerialID();
			pBlackAdd.wResultCode = GAME_SUCCESS;
		}
	}
	Send(&pBlackAdd, sizeof(pBlackAdd));
}
Example #7
0
void ChatHandler::handleWhoMessage(ChatClient &client)
{
    MessageOut reply(CPMSG_WHO_RESPONSE);

    std::map<std::string, ChatClient*>::iterator itr, itr_end;
    itr = mPlayerMap.begin();
    itr_end = mPlayerMap.end();

    while (itr != itr_end)
    {
        reply.writeString(itr->first);
        ++itr;
    }

    client.send(reply);
}
Example #8
0
void ChatHandler::handleGuildKickMember(ChatClient &client, MessageIn &msg)
{
    MessageOut reply(CPMSG_GUILD_KICK_MEMBER_RESPONSE);
    short guildId = msg.readInt16();
    std::string otherCharName = msg.readString();

    Guild *guild = guildManager->findById(guildId);

    if (!guild)
    {
        reply.writeInt8(ERRMSG_INVALID_ARGUMENT);
        client.send(reply);
        return;
    }
    ChatClient *otherClient = getClient(otherCharName);
    unsigned otherCharId;
    if (otherClient)
        otherCharId = otherClient->characterId;
    else
        otherCharId = storage->getCharacterId(otherCharName);

    if (otherCharId == 0)
    {
        reply.writeInt8(ERRMSG_INVALID_ARGUMENT);
        client.send(reply);
        return;
    }

    if (!((guild->getUserPermissions(client.characterId) & GAL_KICK) &&
            guild->checkInGuild(otherCharId) &&
            otherCharId != client.characterId))
    {
        reply.writeInt8(ERRMSG_INSUFFICIENT_RIGHTS);
        client.send(reply);
        return;
    }
    if (otherClient)
    {
        // Client is online. Inform him about that he got kicked
        MessageOut kickMsg(CPMSG_GUILD_KICK_NOTIFICATION);
        kickMsg.writeInt16(guild->getId());
        kickMsg.writeString(client.characterName);
        otherClient->send(kickMsg);
    }

    guildManager->removeGuildMember(guild, otherCharId, otherCharName,
                                    otherClient);
    reply.writeInt8(ERRMSG_OK);
    client.send(reply);
}
Example #9
0
void ChatHandler::handleQuitChannelMessage(ChatClient &client, MessageIn &msg)
{
    MessageOut reply(CPMSG_QUIT_CHANNEL_RESPONSE);

    short channelId = msg.readShort();
    ChatChannel *channel = chatChannelManager->getChannel(channelId);

    if (channelId == 0 || !channel)
    {
        reply.writeByte(ERRMSG_INVALID_ARGUMENT);
    }
    else if (!channel->removeUser(&client))
    {
        reply.writeByte(ERRMSG_FAILURE);
    }
    else
    {
        reply.writeByte(ERRMSG_OK);
        reply.writeShort(channelId);

        // Send an CPMSG_UPDATE_CHANNEL to warn other clients a user left
        // the channel.
        warnUsersAboutPlayerEventInChat(channel,
                client.characterName,
                CHAT_EVENT_LEAVING_PLAYER);

        // log transaction
        Transaction trans;
        trans.mCharacterId = client.characterId;
        trans.mAction = TRANS_CHANNEL_QUIT;
        trans.mMessage = "User left " + channel->getName();
        storage->addTransaction(trans);

        if (channel->getUserList().empty())
        {
            chatChannelManager->removeChannel(channel->getId());
        }
    }

    client.send(reply);
}
Example #10
0
void ChatHandler::handleGuildAcceptInvite(ChatClient &client,
                                          MessageIn &msg)
{
    MessageOut reply(CPMSG_GUILD_ACCEPT_RESPONSE);
    const int guildId = msg.readInt16();
    const bool accepted = msg.readInt8();

    // check guild exists and that member was invited
    // then add them as guild member
    // and remove from invite list
    Guild *guild = guildManager->findById(guildId);
    if (!(guild && guild->checkInvited(client.characterId)))
    {

        reply.writeInt8(ERRMSG_FAILURE);
    }
    else if (accepted)
    {
        // add user to guild
        guildManager->addGuildMember(guild, client.characterId);
        client.guilds.push_back(guild);
        reply.writeInt8(ERRMSG_OK);
        reply.writeString(guild->getName());
        reply.writeInt16(guild->getId());
        reply.writeInt16(guild->getUserPermissions(client.characterId));

        // have character join guild channel
        ChatChannel *channel = joinGuildChannel(guild->getName(), client);
        reply.writeInt16(channel->getId());
        sendGuildListUpdate(guild, client.characterName,
                            GUILD_EVENT_NEW_PLAYER);
    }
    else
    {
        guild->removeInvited(client.characterId);
        reply.writeInt8(ERRMSG_OK);
    }

    client.send(reply);
}
Example #11
0
void ChatHandler::handleAnnounceMessage(ChatClient &client, MessageIn &msg)
{
    std::string text = msg.readString();

    if (!stringFilter->filterContent(text))
    {
        warnPlayerAboutBadWords(client);
        return;
    }

    if (client.accountLevel == AL_ADMIN || client.accountLevel == AL_GM)
    {
        // TODO: b_lindeijer: Shouldn't announcements also have a sender?
        LOG_INFO("ANNOUNCE: " << text);
        MessageOut result(CPMSG_ANNOUNCEMENT);
        result.writeString(text);

        // We send the message to all players in the default channel as it is
        // an announcement.
        sendToEveryone(result);

        // log transaction
        Transaction trans;
        trans.mCharacterId = client.characterId;
        trans.mAction = TRANS_MSG_ANNOUNCE;
        trans.mMessage = "User announced " + text;
        storage->addTransaction(trans);
    }
    else
    {
        MessageOut result(CPMSG_ERROR);
        result.writeByte(ERRMSG_INSUFFICIENT_RIGHTS);
        client.send(result);
        LOG_INFO(client.characterName <<
            " couldn't make an announcement due to insufficient rights.");
    }

}
Example #12
0
void ChatHandler::handlePartyAcceptInvite(ChatClient &client, MessageIn &msg)
{
    MessageOut out(CPMSG_PARTY_ACCEPT_INVITE_RESPONSE);

    std::string inviter = msg.readString();

    // Check that the player was invited
    std::vector<PartyInvite>::iterator itr, itr_end;
    itr = mPartyInvitedUsers.begin();
    itr_end = mPartyInvitedUsers.end();

    bool found = false;

    while (itr != itr_end)
    {
        if ((*itr).mInvited == client.characterName &&
            (*itr).mInviter == inviter)
        {
            // make them join the party
            if (handlePartyJoin(client.characterName, inviter))
            {
                out.writeInt8(ERRMSG_OK);
                mPartyInvitedUsers.erase(itr);
                found = true;
                break;
            }
        }

        ++itr;
    }

    if (!found)
    {
        out.writeInt8(ERRMSG_FAILURE);
    }

    client.send(out);
}
Example #13
0
void ChatHandler::handleListChannelsMessage(ChatClient &client, MessageIn &)
{
    MessageOut reply(CPMSG_LIST_CHANNELS_RESPONSE);

    std::list<const ChatChannel*> channels =
        chatChannelManager->getPublicChannels();

    for (std::list<const ChatChannel*>::iterator i = channels.begin(),
            i_end = channels.end();
            i != i_end; ++i)
    {
        reply.writeString((*i)->getName());
        reply.writeInt16((*i)->getUserList().size());
    }

    client.send(reply);

    // log transaction
    Transaction trans;
    trans.mCharacterId = client.characterId;
    trans.mAction = TRANS_CHANNEL_LIST;
    storage->addTransaction(trans);
}
Example #14
0
void ChatHandler::sendGuildRejoin(ChatClient &client)
{
    // Get list of guilds and check what rights they have.
    std::vector<Guild *> guilds =
            guildManager->getGuildsForPlayer(client.characterId);

    client.guilds = guilds;

    for (std::vector<Guild *>::iterator it = guilds.begin(),
         it_end = guilds.end(); it != it_end; ++it)
    {
        Guild *guild = *it;

        const int permissions = guild->getUserPermissions(client.characterId);
        const std::string guildName = guild->getName();

        // Tell the client what guilds the character belongs to
        // and their permissions
        MessageOut msg(CPMSG_GUILD_REJOIN);
        msg.writeString(guildName);
        msg.writeInt16(guild->getId());
        msg.writeInt16(permissions);

        // get channel id of guild channel
        ChatChannel *channel = joinGuildChannel(guildName, client);

        // send the channel id for the autojoined channel
        msg.writeInt16(channel->getId());
        msg.writeString(channel->getAnnouncement());

        client.send(msg);

        sendGuildListUpdate(guild, client.characterName,
                            GUILD_EVENT_ONLINE_PLAYER);
    }
}
Example #15
0
void ChatHandler::handleGuildCreate(ChatClient &client, MessageIn &msg)
{
    MessageOut reply(CPMSG_GUILD_CREATE_RESPONSE);

    // Check if guild already exists and if so, return error
    std::string guildName = msg.readString();
    if (!guildManager->doesExist(guildName))
    {
        if ((int)client.guilds.size() >=
                Configuration::getValue("account_maxGuildsPerCharacter", 1))
        {
            reply.writeInt8(ERRMSG_LIMIT_REACHED);
        }
        else
        {
            // Guild doesnt already exist so create it
            Guild *guild = guildManager->createGuild(guildName, client.characterId);
            reply.writeInt8(ERRMSG_OK);
            reply.writeString(guildName);
            reply.writeInt16(guild->getId());
            reply.writeInt16(guild->getUserPermissions(client.characterId));

            client.guilds.push_back(guild);

            // Send autocreated channel id
            ChatChannel* channel = joinGuildChannel(guildName, client);
            reply.writeInt16(channel->getId());
        }
    }
    else
    {
        reply.writeInt8(ERRMSG_ALREADY_TAKEN);
    }

    client.send(reply);
}
Example #16
0
/**
 *  This is the main function which creates a chat client object
 *  and runs the chat.
 */
int main(int argc, char* argv[]) {
	ChatClient cc = ChatClient();
	cc.run(argc, argv);
}
//////////////////////////////
/// DRIVER
//////////////////////////////
void driver(int argc, char * argv[]) {
  
  // Argument #2 is the first command line argument, so
  // we want to make sure we have some here.
  if (argc > 1) {
  
    string type(argv[1]);
    
    // Make the string lowercase so it's not case-sensitive.
    type = str_tolower(type);
    
    unsigned int port = DEFAULT_PORT;
    
    // If they supplied a port to use.
    if (argc > 2) {
    
      string port_str(argv[2]);
      port_str = str_tolower(port_str);
      
      if (port_str != "-d") {
      
        port = str_to_int(argv[2]);
      
      }
    
    }
    
    // Create an object of the requested type and start running.
    if (type == "s" || type == "server") {
    
      ChatServer server;
      server.setup(port);
      server.run();
    
    } else if (type == "c" || type == "client") {
    
      string hostname = DEFAULT_HOST;
      
      if (argc > 3) {
      
        hostname = argv[3];
      
      }
      
      ChatClient client;
      client.setup(port, hostname);
      client.run();
    
    } else {
    
      cout << "Invalid arguments provided." << endl;
      cout << "Please supply arguments to the program in this format:" << endl;
      cout << argv[0] << " type [port [hostname]]" << endl;
      cout << "type:     Either 'S' for server or 'C' for client." << endl;
      cout << "port:     The port number to use ('-d' for default)." << endl;
      cout << "          Note: this program defaults to port " << DEFAULT_PORT
           << "." << endl;
      cout << "hostname: The hostname or IP address of the server to" << endl;
      cout << "          connect to (only used as client)." << endl;
      cout << "These are case-insensitive." << endl;
    
    }
  
  } else {
  
    cout << "No arguments provided." << endl;
    cout << "Please supply arguments to the program in this format:" << endl;
    cout << argv[0] << " type [port [hostname]]" << endl;
    cout << "type:     Either 'S' for server or 'C' for client." << endl;
    cout << "port:     The port number to use ('-d' for default)." << endl;
    cout << "          Note: this program defaults to port " << DEFAULT_PORT
         << endl;
    cout << "hostname: The hostname or IP address of the server to" << endl;
    cout << "          connect to (only used as client)." << endl;
    cout << "These are case-insensitive." << endl;
  
  }
  
}
Example #18
0
void ChatHandler::handlePartyInviteAnswer(ChatClient &client, MessageIn &msg)
{
    if (client.party)
        return;

    MessageOut outInvitee(CPMSG_PARTY_INVITE_ANSWER_RESPONSE);

    std::string inviter = msg.readString();

    // check if the invite is still valid
    bool valid = false;
    removeExpiredPartyInvites();
    const size_t size = mInvitations.size();
    for (size_t i = 0; i < size; ++i)
    {
        if (mInvitations[i].mInviter == inviter &&
            mInvitations[i].mInvitee == client.characterName)
        {
            valid = true;
        }
    }

    // the invitee did not accept the invitation
    if (!msg.readInt8())
    {
        if (!valid)
            return;

        // send rejection to inviter
        ChatClient *inviterClient = getClient(inviter);
        if (inviterClient)
        {
            MessageOut out(CPMSG_PARTY_REJECTED);
            out.writeString(inviter);
            out.writeInt8(ERRMSG_OK);
            inviterClient->send(out);
        }
        return;
    }

    // if the invitation has expired, tell the inivtee about it
    if (!valid)
    {
        outInvitee.writeInt8(ERRMSG_TIME_OUT);
        client.send(outInvitee);
        return;
    }

    // check that the inviter is still in the game
    ChatClient *c1 = getClient(inviter);
    if (!c1)
    {
        outInvitee.writeInt8(ERRMSG_FAILURE);
        client.send(outInvitee);
        return;
    }

    // if party doesnt exist, create it
    if (!c1->party)
    {
        c1->party = new Party();
        c1->party->addUser(inviter);
        // tell game server to update info
        updateInfo(c1, c1->party->getId());
    }

    outInvitee.writeInt8(ERRMSG_OK);
    Party::PartyUsers users = c1->party->getUsers();
    const unsigned usersSize = users.size();
    for (unsigned i = 0; i < usersSize; i++)
        outInvitee.writeString(users[i]);

    client.send(outInvitee);

    // add invitee to the party
    c1->party->addUser(client.characterName, inviter);
    client.party = c1->party;

    // tell game server to update info
    updateInfo(&client, client.party->getId());
}
Example #19
0
int
main(int argc, char* argv[])
{
    ChatClient app;
    return app.main(argc, argv, "config.client");
}
Example #20
0
/**
*  @brief
*    Run client
*/
void RunClient()
{
	// Create client
	ChatClient cClient;

	// Create a client connection at host address 'localhost' (our local server) at port '4000'
	ChatClientConnection *pConnection = static_cast<ChatClientConnection*>(cClient.Connect("localhost", 4000));
	if (pConnection) {
		bool bActive = true;
		String sMessage;

		// Main loop
		System::GetInstance()->GetConsole().Print("Input text to send and press 'Enter'\nPress 'ESC' to exit the client\n");
		while (bActive) {
			// Let the system some time to process other system tasks etc.
			// If this isn't done the CPU usage is nearly up to 100%!
			System::GetInstance()->Sleep(1);

			// Update client
			if (pConnection->IsConnected()) {
				// Check keys
				if (System::GetInstance()->GetConsole().IsKeyHit()) {
					// Get key and interpret it as character, not number
					const wchar_t nCharacter = static_cast<wchar_t>(System::GetInstance()->GetConsole().GetCharacter());

					// Quit
					if (nCharacter == 27)
						bActive = false;

					// Message
					else if (nCharacter == 13) {
						// New line on the console, please
						System::GetInstance()->GetConsole().Print('\n');

						// Send the message
						pConnection->Send(sMessage, sMessage.GetLength() + 1);

						// Clear the message
						sMessage = "";

					// Character
					} else {
						// Add character to the message
						sMessage += nCharacter;

						// Output the character on the console
						System::GetInstance()->GetConsole().Print(nCharacter);
					}
				}
			} else {
				// Connection broken...
				System::GetInstance()->GetConsole().Print("Error: Connection to the server broken... press any key to exit\n");
				bActive = false;

				// Wait for the 'any key'
				System::GetInstance()->GetConsole().GetCharacter();
			}
		}

		// Shutdown our client
		pConnection->Disconnect();
	} else {
		// Error!
		System::GetInstance()->GetConsole().Print("Error: Could not connect to server... press any key to exit\n");

		// Wait for the 'any key'
		System::GetInstance()->GetConsole().GetCharacter();
	}
}
Example #21
0
void ChatServer::processMessage(AuthorizationRequest *msg, QTcpSocket *socket)
//processing authorization request
//here we need to check whether user exists in table or not
//YOBA ETO YA, PSHH-PSSHHH
//and we need to form authorization answer and send it to client
{
    if (!msg)
    {
        QString log = "Error processing authprization request- message is empty";
        emit serverLog(esMinor, log);
        return;
    }
    AuthorizationAnswer *answer = new AuthorizationAnswer();
    switch (m_clientList.authorize(msg->username, msg->password, socket))
    {
    case GeneralClientList::arAllreadyAuthorized:
        {
            answer->authorizationResult = false;
            answer->denialReason = "User allready authorized";
            break;
        }
    case GeneralClientList::arWrongAuthData:
        {
            answer->authorizationResult = false;
            answer->denialReason = "Wrong username or password";
            break;
        }
    case GeneralClientList::arAuthSuccess:
        {
            answer->authorizationResult = true;
            break;
        }
    }
    sendMessageToClient(socket, answer);
    if (answer->authorizationResult)
    {
        ChatClient client = m_clientList.getClient(msg->username);
        client.setUserState("Online");
        m_clientList.updateClient(client);
        emit updateTable("clients");
        QStringList channels = m_clientList.getChannelsForClient(msg->username).keys();
        ChannelSystemMessage *informMsg = new ChannelSystemMessage();
        ChannelUserList *updateListMsg = new ChannelUserList();
        informMsg->message = msg->username + " entered chat.";
        for (int i = 0; i < channels.count(); ++i)
        {
            informMsg->channelName = channels[i];
            updateListMsg->channelName = channels[i];
            ChatChannel channel = m_clientList.getChannel(channels[i]);
            for(int k = 0; k < channel.userList.count(); k++)
            {
                //FIXME: GOVNOKOD
                updateListMsg->userList.insert(channel.userList[k], getSendableState(channel.userList[k]));
            }
            for (int j = 0; j < channel.userList.count(); j++)
            {                
                QString username = channel.userList[j];
                if (username != msg->username)
                {
                    sendMessageToClient(username, informMsg);
                    sendMessageToClient(username, updateListMsg);
                }
            }
        }
        delete updateListMsg;
        delete informMsg;
    }
    delete answer;
}
Example #22
0
void ChatHandler::handleEnterChannelMessage(ChatClient &client, MessageIn &msg)
{
    MessageOut reply(CPMSG_ENTER_CHANNEL_RESPONSE);

    std::string channelName = msg.readString();
    std::string givenPassword = msg.readString();
    ChatChannel *channel = NULL;
    if (chatChannelManager->channelExists(channelName) ||
        chatChannelManager->tryNewPublicChannel(channelName))
    {
        channel = chatChannelManager->getChannel(channelName);
    }

    if (!channel)
    {
        reply.writeByte(ERRMSG_INVALID_ARGUMENT);
    }
    else if (!channel->getPassword().empty() &&
            channel->getPassword() != givenPassword)
    {
        // Incorrect password (should probably have its own return value)
        reply.writeByte(ERRMSG_INSUFFICIENT_RIGHTS);
    }
    else if (!channel->canJoin())
    {
        reply.writeByte(ERRMSG_INVALID_ARGUMENT);
    }
    else
    {
        if (channel->addUser(&client))
        {
            reply.writeByte(ERRMSG_OK);
            // The user entered the channel, now give him the channel
            // id, the announcement string and the user list.
            reply.writeShort(channel->getId());
            reply.writeString(channelName);
            reply.writeString(channel->getAnnouncement());
            const ChatChannel::ChannelUsers &users = channel->getUserList();

            for (ChatChannel::ChannelUsers::const_iterator i = users.begin(),
                    i_end = users.end();
                    i != i_end; ++i)
            {
                reply.writeString((*i)->characterName);
                reply.writeString(channel->getUserMode((*i)));
            }
            // Send an CPMSG_UPDATE_CHANNEL to warn other clients a user went
            // in the channel.
            warnUsersAboutPlayerEventInChat(channel,
                    client.characterName,
                    CHAT_EVENT_NEW_PLAYER);

            // log transaction
            Transaction trans;
            trans.mCharacterId = client.characterId;
            trans.mAction = TRANS_CHANNEL_JOIN;
            trans.mMessage = "User joined " + channelName;
            storage->addTransaction(trans);
        }
        else
        {
            reply.writeByte(ERRMSG_FAILURE);
        }
    }

    client.send(reply);
}