Esempio n. 1
0
void ChatHandler::handleGuildMemberLevelChange(ChatClient &client,
                                               MessageIn &msg)
{
    // get the guild, the user to change the permissions, and the new permission
    // check theyre valid, and then change them
    MessageOut reply(CPMSG_GUILD_PROMOTE_MEMBER_RESPONSE);
    short guildId = msg.readInt16();
    std::string user = msg.readString();
    short level = msg.readInt8();
    Guild *guild = guildManager->findById(guildId);
    CharacterData *c = storage->getCharacter(user);

    if (guild && c)
    {
        int rights = guild->getUserPermissions(c->getDatabaseID()) | level;
        if (guildManager->changeMemberLevel(&client, guild, c->getDatabaseID(),
                                            rights) == 0)
        {
            reply.writeInt8(ERRMSG_OK);
            client.send(reply);
        }
    }

    reply.writeInt8(ERRMSG_FAILURE);
    client.send(reply);
}
Esempio n. 2
0
void ChatHandler::handleGuildInvite(ChatClient &client, MessageIn &msg)
{
    MessageOut reply(CPMSG_GUILD_INVITE_RESPONSE);
    MessageOut invite(CPMSG_GUILD_INVITED);

    // send an invitation from sender to character to join guild
    int guildId = msg.readInt16();
    std::string character = msg.readString();

    // get the chat client and the guild
    ChatClient *invitedClient = getClient(character);
    Guild *guild = guildManager->findById(guildId);

    if (invitedClient && guild)
    {
        // check permissions of inviter, and that they arent inviting themself,
        // and arent someone already in the guild
        if (guild->canInvite(client.characterId) &&
            client.characterName != character &&
            guild->checkInGuild(client.characterId))
        {
            if ((int)invitedClient->guilds.size() >=
                    Configuration::getValue("account_maxGuildsPerCharacter", 1))
            {
                reply.writeInt8(ERRMSG_LIMIT_REACHED);
            }
            else if (guild->checkInGuild(invitedClient->characterId))
            {
                reply.writeInt8(ERRMSG_ALREADY_MEMBER);
            }
            else
            {
                // send the name of the inviter and the name of the guild
                // that the character has been invited to join
                std::string senderName = client.characterName;
                std::string guildName = guild->getName();
                invite.writeString(senderName);
                invite.writeString(guildName);
                invite.writeInt16(guildId);
                invitedClient->send(invite);
                reply.writeInt8(ERRMSG_OK);

                // add member to list of invited members to the guild
                guild->addInvited(invitedClient->characterId);
            }
        }
        else
        {
            reply.writeInt8(ERRMSG_FAILURE);
        }
    }
    else
    {
        reply.writeInt8(ERRMSG_FAILURE);
    }

    client.send(reply);
}
Esempio n. 3
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);
}
Esempio n. 4
0
void ChatHandler::handleGuildGetMembers(ChatClient &client, MessageIn &msg)
{
    MessageOut reply(CPMSG_GUILD_GET_MEMBERS_RESPONSE);
    short guildId = msg.readInt16();
    Guild *guild = guildManager->findById(guildId);

    // check for valid guild
    // write a list of member names that belong to the guild
    if (guild)
    {
        // make sure the requestor is in the guild
        if (guild->checkInGuild(client.characterId))
        {
            reply.writeInt8(ERRMSG_OK);
            reply.writeInt16(guildId);
            std::list<GuildMember*> memberList = guild->getMembers();
            std::list<GuildMember*>::const_iterator itr_end = memberList.end();
            for (std::list<GuildMember*>::iterator itr = memberList.begin();
                 itr != itr_end; ++itr)
            {
                CharacterData *c = storage->getCharacter((*itr)->mId, nullptr);
                std::string memberName = c->getName();
                reply.writeString(memberName);
                reply.writeInt8(mPlayerMap.find(memberName) != mPlayerMap.end());
            }
        }
    }
    else
    {
        reply.writeInt8(ERRMSG_FAILURE);
    }

    client.send(reply);
}
Esempio n. 5
0
void ChatHandler::handleListChannelUsersMessage(ChatClient &client,
                                                MessageIn &msg)
{
    MessageOut reply(CPMSG_LIST_CHANNELUSERS_RESPONSE);

    std::string channelName = msg.readString();
    ChatChannel *channel = chatChannelManager->getChannel(channelName);

    if (channel)
    {
        reply.writeString(channel->getName());

        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)));
        }

        client.send(reply);
    }

    // log transaction
    Transaction trans;
    trans.mCharacterId = client.characterId;
    trans.mAction = TRANS_CHANNEL_USERLIST;
    trans.mMessage = "";
    storage->addTransaction(trans);
}
Esempio n. 6
0
void ChatHandler::handleCommand(ChatClient &computer, const std::string &command)
{
    LOG_INFO("Chat: Received unhandled command: " << command);
    MessageOut result(CPMSG_ERROR);
    result.writeInt8(CHAT_UNHANDLED_COMMAND);
    computer.send(result);
}
Esempio n. 7
0
void ChatHandler::handleDisconnectMessage(ChatClient &client, MessageIn &msg)
{
    MessageOut reply(CPMSG_DISCONNECT_RESPONSE);
    reply.writeByte(ERRMSG_OK);
    chatChannelManager->removeUserFromAllChannels(&client);
    guildManager->disconnectPlayer(&client);
    client.send(reply);
}
Esempio n. 8
0
void ChatHandler::warnPlayerAboutBadWords(ChatClient &computer)
{
    // We could later count if the player is really often unpolite.
    MessageOut result(CPMSG_ERROR);
    result.writeInt8(CHAT_USING_BAD_WORDS); // The Channel
    computer.send(result);

    LOG_INFO(computer.characterName << " says bad words.");
}
Esempio n. 9
0
void ChatHandler::handlePartyQuit(ChatClient &client)
{
    removeUserFromParty(client);
    MessageOut out(CPMSG_PARTY_QUIT_RESPONSE);
    out.writeInt8(ERRMSG_OK);
    client.send(out);

    // tell game server to update info
    updateInfo(&client, 0);
}
Esempio n. 10
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);


}
Esempio n. 11
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);
}
Esempio n. 12
0
void ChatHandler::sendGuildInvite(const std::string &invitedName,
                                  const std::string &inviterName,
                                  const std::string &guildName)
{
    MessageOut msg(CPMSG_GUILD_INVITED);
    msg.writeString(inviterName);
    msg.writeString(guildName);

    ChatClient *client = getClient(invitedName);
    if (client)
    {
        client->send(msg);
    }
}
Esempio n. 13
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);
}
Esempio n. 14
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);
}
Esempio n. 15
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);
}
Esempio n. 16
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.");
    }

}
Esempio n. 17
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);
        }
    }
}
Esempio n. 18
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);
}
Esempio n. 19
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);
}
Esempio n. 20
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);
}
Esempio n. 21
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);
    }
}
Esempio n. 22
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);
}
Esempio n. 23
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());
}
Esempio n. 24
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);
}