Skip to content

Commit

Permalink
Add deprecated rule violations
Browse files Browse the repository at this point in the history
-Add deprecated rule violation system for pre-8.7 protocols
-Chat code optimization + auto-detection chaneel ids for hardcoded channels
  • Loading branch information
SaiyansKing committed Oct 10, 2020
1 parent fd79964 commit 5640e43
Show file tree
Hide file tree
Showing 18 changed files with 736 additions and 36 deletions.
2 changes: 2 additions & 0 deletions data/XML/groups.xml
Expand Up @@ -36,6 +36,7 @@
<flag ignoreweaponcheck="1" />
<flag cannotbemuted="1" />
<flag isalwayspremium="1" />
<flag cananswerruleviolations="1" />
</flags>
</group>
<group id="3" name="god" access="1" maxdepotitems="0" maxvipentries="200" >
Expand Down Expand Up @@ -75,6 +76,7 @@
<flag ignoreweaponcheck="1" />
<flag cannotbemuted="1" />
<flag isalwayspremium="1" />
<flag cananswerruleviolations="1" />
</flags>
</group>
</groups>
21 changes: 14 additions & 7 deletions data/chatchannels/chatchannels.xml
@@ -1,10 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<channels>
<channel id="2" name="Tutor" script="tutor.lua" />
<channel id="3" name="World Chat" public="1" script="worldchat.lua" />
<channel id="4" name="English Chat" public="1" script="englishchat.lua" />
<channel id="5" name="Advertising" public="1" script="advertising.lua" />
<channel id="6" name="Advertising-Rookgaard" public="1" script="advertising-rook.lua" />
<channel id="7" name="Help" public="1" script="help.lua" />
<channel id="8" name="Gamemaster" script="gamemaster.lua" />
<!-- README:
0 - dynamic, reserved for guilds
1 - always acts as Party channel, only "name" tag available
2-10 - reserved for main channels(hardcoded)
100-10000 - reserved for private channels
-->
<channel hid="rvr" name="Rule Violations" script="rvr.lua" />
<channel id="11" name="Tutor" script="tutor.lua" />
<channel id="12" name="World Chat" public="1" script="worldchat.lua" />
<channel id="13" name="English Chat" public="1" script="englishchat.lua" />
<channel hid="advertising" name="Advertising" public="1" script="advertising.lua" />
<channel hid="advertising-rookgaard" name="Advertising-Rookgaard" public="1" script="advertising-rook.lua" />
<channel hid="help" name="Help" public="1" script="help.lua" />
<channel id="14" name="Gamemaster" script="gamemaster.lua" />
</channels>
7 changes: 7 additions & 0 deletions data/chatchannels/scripts/rvr.lua
@@ -0,0 +1,7 @@
function canJoin(player)
return player:hasFlag(PlayerFlag_CanAnswerRuleViolations)
end

function onSpeak(player, type, message)
return false
end
2 changes: 2 additions & 0 deletions src/actions.cpp
Expand Up @@ -485,6 +485,8 @@ namespace {
}

player->sendMarketEnter(player->getLastDepotId());
#else
(void)player;
#endif
return true;
}
Expand Down
26 changes: 26 additions & 0 deletions src/ban.cpp
Expand Up @@ -121,3 +121,29 @@ bool IOBan::isPlayerNamelocked(uint32_t playerId)
query << "SELECT 1 FROM `player_namelocks` WHERE `player_id` = " << playerId;
return g_database.storeQuery(query).get() != nullptr;
}

uint32_t IOBan::getAccountID(const std::string& playerName)
{
const std::string& escapedName = g_database.escapeString(playerName);
std::stringExtended query(escapedName.length() + 64);
query << "SELECT `account_id` FROM `players` WHERE `name` = " << escapedName;

DBResult_ptr result = g_database.storeQuery(query);
if (!result) {
return 0;
}
return result->getNumber<uint32_t>("account_id");
}

uint32_t IOBan::getAccountLastIP(const std::string& playerName)
{
const std::string& escapedName = g_database.escapeString(playerName);
std::stringExtended query(escapedName.length() + 64);
query << "SELECT `lastip` FROM `players` WHERE `name` = " << escapedName;

DBResult_ptr result = g_database.storeQuery(query);
if (!result) {
return 0;
}
return result->getNumber<uint32_t>("lastip");
}
3 changes: 3 additions & 0 deletions src/ban.h
Expand Up @@ -52,6 +52,9 @@ class IOBan
static bool isAccountBanned(uint32_t accountId, BanInfo& banInfo);
static bool isIpBanned(uint32_t clientIP, BanInfo& banInfo);
static bool isPlayerNamelocked(uint32_t playerId);

static uint32_t getAccountID(const std::string& playerName);
static uint32_t getAccountLastIP(const std::string& playerName);
};

#endif
102 changes: 76 additions & 26 deletions src/chat.cpp
Expand Up @@ -302,7 +302,43 @@ bool Chat::load()
}

for (auto channelNode : doc.child("channels").children()) {
uint16_t channelId = pugi::cast<uint16_t>(channelNode.attribute("id").value());
uint16_t channelId = 0;
if (pugi::xml_attribute attr = channelNode.attribute("hid")) {
std::string tmpStrValue = asLowerCaseString(attr.as_string());
if (!tfs_strcmp(tmpStrValue.c_str(), "help")) {
#if CLIENT_VERSION >= 871
channelId = 7;
#elif CLIENT_VERSION == 870
channelId = 6;
#elif CLIENT_VERSION >= 840
channelId = 9;
#elif CLIENT_VERSION >= 790
channelId = 8;
#else
channelId = 7;
#endif
} else if (!tfs_strcmp(tmpStrValue.c_str(), "advertising")) {
#if CLIENT_VERSION >= 871
channelId = 5;
#else
channelId = 4;
#endif
} else if (!tfs_strcmp(tmpStrValue.c_str(), "advertising-rookgaard")) {
#if CLIENT_VERSION >= 871
channelId = 6;
#else
channelId = 5;
#endif
} else if (!tfs_strcmp(tmpStrValue.c_str(), "rvr")) {
#if GAME_FEATURE_RULEVIOLATION > 0
channelId = 3;
#else
continue;
#endif
}
} else {
channelId = pugi::cast<uint16_t>(channelNode.attribute("id").value());
}
std::string channelName = channelNode.attribute("name").as_string();
bool isPublic = channelNode.attribute("public").as_bool();
pugi::xml_attribute scriptAttribute = channelNode.attribute("script");
Expand Down Expand Up @@ -331,7 +367,8 @@ bool Chat::load()
continue;
}

ChatChannel channel(channelId, channelName);
auto ret = normalChannels.emplace(std::piecewise_construct, std::forward_as_tuple(channelId), std::forward_as_tuple(channelId, channelName));
ChatChannel& channel = (*ret.first).second;
channel.publicChannel = isPublic;

if (scriptAttribute) {
Expand All @@ -344,9 +381,9 @@ bool Chat::load()
std::cout << "[Warning - Chat::load] Can not load script: " << scriptAttribute.as_string() << std::endl;
}
}

normalChannels[channel.id] = channel;
cachedChannels.push_back(channelId);
}
cachedChannels.shrink_to_fit();
return true;
}

Expand All @@ -360,7 +397,7 @@ ChatChannel* Chat::createChannel(const Player& player, uint16_t channelId)
case CHANNEL_GUILD: {
Guild* guild = player.getGuild();
if (guild) {
auto ret = guildChannels.emplace(std::make_pair(guild->getId(), ChatChannel(channelId, guild->getName())));
auto ret = guildChannels.emplace(std::piecewise_construct, std::forward_as_tuple(guild->getId()), std::forward_as_tuple(channelId, guild->getName()));
return &ret.first->second;
}
break;
Expand All @@ -369,7 +406,7 @@ ChatChannel* Chat::createChannel(const Player& player, uint16_t channelId)
case CHANNEL_PARTY: {
Party* party = player.getParty();
if (party) {
auto ret = partyChannels.emplace(std::make_pair(party, ChatChannel(channelId, "Party")));
auto ret = partyChannels.emplace(std::piecewise_construct, std::forward_as_tuple(party), std::forward_as_tuple(channelId, "Party"));
return &ret.first->second;
}
break;
Expand All @@ -383,7 +420,7 @@ ChatChannel* Chat::createChannel(const Player& player, uint16_t channelId)

//find a free private channel slot
for (uint16_t i = 100; i < 10000; ++i) {
auto ret = privateChannels.emplace(std::make_pair(i, PrivateChatChannel(i, player.getName() + "'s Channel")));
auto ret = privateChannels.emplace(std::piecewise_construct, std::forward_as_tuple(i), std::forward_as_tuple(i, player.getName() + "'s Channel"));
if (ret.second) { //second is a bool that indicates that a new channel has been placed in the map
auto& newChannel = (*ret.first).second;
newChannel.setOwner(player.getGUID());
Expand Down Expand Up @@ -539,6 +576,12 @@ void Chat::openChannelsByServer(Player* player)
users = nullptr;
}

#if GAME_FEATURE_RULEVIOLATION > 0
if (channel->getId() == 3) {
player->sendRuleViolationChannel(channel->getId());
continue;
}
#endif
player->sendChannel(channel->getId(), channel->getName(), users, invitedUsers);
}
}
Expand All @@ -547,7 +590,28 @@ void Chat::openChannelsByServer(Player* player)

ChannelList Chat::getChannelList(const Player& player)
{
ChannelList list;
ChannelList list, privates;
privates.reserve(8);

bool hasPrivate = false;
for (auto& it : privateChannels) {
if (PrivateChatChannel* channel = &it.second) {
uint32_t guid = player.getGUID();
if (channel->isInvited(guid)) {
privates.push_back(channel);
}

if (channel->getOwner() == guid) {
hasPrivate = true;
}
}
}

list.reserve(cachedChannels.size() + privates.size() + 3);
if (!hasPrivate && player.isPremium()) {
list.push_back(&dummyPrivate);
}

if (player.getGuild()) {
ChatChannel* channel = getChannel(player, CHANNEL_GUILD);
if (channel) {
Expand All @@ -572,29 +636,15 @@ ChannelList Chat::getChannelList(const Player& player)
}
}

for (const auto& it : normalChannels) {
ChatChannel* channel = getChannel(player, it.first);
for (uint16_t channelId : cachedChannels) {
ChatChannel* channel = getChannel(player, channelId);
if (channel) {
list.push_back(channel);
}
}

bool hasPrivate = false;
for (auto& it : privateChannels) {
if (PrivateChatChannel* channel = &it.second) {
uint32_t guid = player.getGUID();
if (channel->isInvited(guid)) {
list.push_back(channel);
}

if (channel->getOwner() == guid) {
hasPrivate = true;
}
}
}

if (!hasPrivate && player.isPremium()) {
list.push_front(&dummyPrivate);
if (!privates.empty()) {
list.insert(list.end(), privates.begin(), privates.end());
}
return list;
}
Expand Down
5 changes: 3 additions & 2 deletions src/chat.h
Expand Up @@ -34,7 +34,7 @@ class ChatChannel
public:
ChatChannel() = default;
ChatChannel(uint16_t channelId, std::string channelName):
id{channelId}, name{std::move(channelName)} {}
id(channelId), name(std::move(channelName)) {}

virtual ~ChatChannel() = default;

Expand Down Expand Up @@ -117,7 +117,7 @@ class PrivateChatChannel final : public ChatChannel
uint32_t owner = 0;
};

using ChannelList = std::list<ChatChannel*>;
using ChannelList = std::vector<ChatChannel*>;

class Chat
{
Expand Down Expand Up @@ -152,6 +152,7 @@ class Chat
}

private:
std::vector<uint16_t> cachedChannels;
std::map<uint16_t, ChatChannel> normalChannels;
std::map<uint16_t, PrivateChatChannel> privateChannels;
std::map<Party*, ChatChannel> partyChannels;
Expand Down
1 change: 1 addition & 0 deletions src/const.h
Expand Up @@ -540,6 +540,7 @@ enum PlayerFlags : uint64_t {
PlayerFlag_IgnoreWeaponCheck = static_cast<uint64_t>(1) << 35,
PlayerFlag_CannotBeMuted = static_cast<uint64_t>(1) << 36,
PlayerFlag_IsAlwaysPremium = static_cast<uint64_t>(1) << 37,
PlayerFlag_CanAnswerRuleViolations = static_cast<uint64_t>(1) << 38,
};

enum ReloadTypes_t : uint8_t {
Expand Down
4 changes: 4 additions & 0 deletions src/features.h
Expand Up @@ -22,6 +22,10 @@

#include "definitions.h"

#if CLIENT_VERSION <= 860
#define GAME_FEATURE_RULEVIOLATION 1
#endif

#if CLIENT_VERSION >= 770
#define GAME_FEATURE_XTEA 1
#define GAME_FEATURE_RSA1024 1
Expand Down

0 comments on commit 5640e43

Please sign in to comment.