Merge pull request #1467 from 12pm/nameban_exact

Add exact name ban option
This commit is contained in:
Dennis Felsing 2019-03-11 22:50:39 +01:00 committed by GitHub
commit b8c256b602
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 26 deletions

View file

@ -15,7 +15,7 @@ CNameBan *IsNameBanned(const char *pName, CNameBan *pNameBans, int NumNameBans)
{ {
CNameBan *pBan = &pNameBans[i]; CNameBan *pBan = &pNameBans[i];
int Distance = str_utf32_dist_buffer(aSkeleton, SkeletonLength, pBan->m_aSkeleton, pBan->m_SkeletonLength, aBuffer, sizeof(aBuffer) / sizeof(aBuffer[0])); int Distance = str_utf32_dist_buffer(aSkeleton, SkeletonLength, pBan->m_aSkeleton, pBan->m_SkeletonLength, aBuffer, sizeof(aBuffer) / sizeof(aBuffer[0]));
if(Distance <= pBan->m_Distance) if(Distance <= pBan->m_Distance || (pBan->m_IsSubstring == 1 && str_utf8_find_nocase(pName, pBan->m_aName)))
{ {
pResult = pBan; pResult = pBan;
} }

View file

@ -14,8 +14,8 @@ class CNameBan
{ {
public: public:
CNameBan() {} CNameBan() {}
CNameBan(const char *pName, int Distance, const char *pReason = "") : CNameBan(const char *pName, int Distance, int IsSubstring, const char *pReason = "") :
m_Distance(Distance) m_Distance(Distance), m_IsSubstring(IsSubstring)
{ {
str_copy(m_aName, pName, sizeof(m_aName)); str_copy(m_aName, pName, sizeof(m_aName));
m_SkeletonLength = str_utf8_to_skeleton(m_aName, m_aSkeleton, sizeof(m_aSkeleton) / sizeof(m_aSkeleton[0])); m_SkeletonLength = str_utf8_to_skeleton(m_aName, m_aSkeleton, sizeof(m_aSkeleton) / sizeof(m_aSkeleton[0]));
@ -26,6 +26,7 @@ public:
int m_aSkeleton[MAX_NAME_SKELETON_LENGTH]; int m_aSkeleton[MAX_NAME_SKELETON_LENGTH];
int m_SkeletonLength; int m_SkeletonLength;
int m_Distance; int m_Distance;
int m_IsSubstring;
}; };
CNameBan *IsNameBanned(const char *pName, CNameBan *pNameBans, int NumNameBans); CNameBan *IsNameBanned(const char *pName, CNameBan *pNameBans, int NumNameBans);

View file

@ -2387,38 +2387,27 @@ void CServer::ConAuthList(IConsole::IResult *pResult, void *pUser)
void CServer::ConNameBan(IConsole::IResult *pResult, void *pUser) void CServer::ConNameBan(IConsole::IResult *pResult, void *pUser)
{ {
CServer *pThis = (CServer *)pUser; CServer *pThis = (CServer *)pUser;
char aBuf[64]; char aBuf[128];
const char *pName = pResult->GetString(0); const char *pName = pResult->GetString(0);
const char *pReason = ""; const char *pReason = pResult->NumArguments() > 3 ? pResult->GetString(3) : "";
int Distance; int Distance = pResult->NumArguments() > 1 ? pResult->GetInteger(1) : str_length(pName) / 3;
int IsSubstring = pResult->NumArguments() > 2 ? pResult->GetInteger(2) : 0;
if(pResult->NumArguments() > 1)
{
Distance = pResult->GetInteger(1);
if(pResult->NumArguments() > 2)
{
pReason = pResult->GetString(2);
}
}
else
{
Distance = str_length(pName) / 3;
}
for(int i = 0; i < pThis->m_aNameBans.size(); i++) for(int i = 0; i < pThis->m_aNameBans.size(); i++)
{ {
CNameBan *pBan = &pThis->m_aNameBans[i]; CNameBan *pBan = &pThis->m_aNameBans[i];
if(str_comp(pBan->m_aName, pName) == 0) if(str_comp(pBan->m_aName, pName) == 0)
{ {
str_format(aBuf, sizeof(aBuf), "changed name='%s' distance=%d old_distance=%d reason='%s' old_reason='%s'", pName, Distance, pBan->m_Distance, pReason, pBan->m_aReason); str_format(aBuf, sizeof(aBuf), "changed name='%s' distance=%d old_distance=%d is_substring=%d old_is_substring=%d reason='%s' old_reason='%s'", pName, Distance, pBan->m_Distance, IsSubstring, pBan->m_IsSubstring, pReason, pBan->m_aReason);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf); pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf);
pBan->m_Distance = Distance; pBan->m_Distance = Distance;
pBan->m_IsSubstring = IsSubstring;
return; return;
} }
} }
pThis->m_aNameBans.add(CNameBan(pName, Distance, pReason)); pThis->m_aNameBans.add(CNameBan(pName, Distance, IsSubstring, pReason));
str_format(aBuf, sizeof(aBuf), "added name='%s' distance=%d reason='%s'", pName, Distance, pReason); str_format(aBuf, sizeof(aBuf), "added name='%s' distance=%d is_substring=%d reason='%s'", pName, Distance, IsSubstring, pReason);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf); pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf);
} }
@ -2433,7 +2422,7 @@ void CServer::ConNameUnban(IConsole::IResult *pResult, void *pUser)
if(str_comp(pBan->m_aName, pName) == 0) if(str_comp(pBan->m_aName, pName) == 0)
{ {
char aBuf[64]; char aBuf[64];
str_format(aBuf, sizeof(aBuf), "removed name='%s' distance=%d reason='%s'", pBan->m_aName, pBan->m_Distance, pBan->m_aReason); str_format(aBuf, sizeof(aBuf), "removed name='%s' distance=%d is_substring=%d reason='%s'", pBan->m_aName, pBan->m_Distance, pBan->m_IsSubstring, pBan->m_aReason);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf); pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf);
pThis->m_aNameBans.remove_index(i); pThis->m_aNameBans.remove_index(i);
} }
@ -2448,7 +2437,7 @@ void CServer::ConNameBans(IConsole::IResult *pResult, void *pUser)
{ {
CNameBan *pBan = &pThis->m_aNameBans[i]; CNameBan *pBan = &pThis->m_aNameBans[i];
char aBuf[64]; char aBuf[64];
str_format(aBuf, sizeof(aBuf), "name='%s' distance=%d reason='%s'", pBan->m_aName, pBan->m_Distance, pBan->m_aReason); str_format(aBuf, sizeof(aBuf), "name='%s' distance=%d is_substring=%d reason='%s'", pBan->m_aName, pBan->m_Distance, pBan->m_IsSubstring, pBan->m_aReason);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf); pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf);
} }
} }
@ -2843,7 +2832,7 @@ void CServer::RegisterCommands()
Console()->Register("auth_remove", "s[ident]", CFGFLAG_SERVER|CFGFLAG_NONTEEHISTORIC, ConAuthRemove, this, "Remove a rcon key"); Console()->Register("auth_remove", "s[ident]", CFGFLAG_SERVER|CFGFLAG_NONTEEHISTORIC, ConAuthRemove, this, "Remove a rcon key");
Console()->Register("auth_list", "", CFGFLAG_SERVER, ConAuthList, this, "List all rcon keys"); Console()->Register("auth_list", "", CFGFLAG_SERVER, ConAuthList, this, "List all rcon keys");
Console()->Register("name_ban", "s[name] ?i[distance] ?r[reason]", CFGFLAG_SERVER, ConNameBan, this, "Ban a certain nick name"); Console()->Register("name_ban", "s[name] ?i[distance] ?i[is_substring] ?r[reason]", CFGFLAG_SERVER, ConNameBan, this, "Ban a certain nick name");
Console()->Register("name_unban", "s[name]", CFGFLAG_SERVER, ConNameUnban, this, "Unban a certain nick name"); Console()->Register("name_unban", "s[name]", CFGFLAG_SERVER, ConNameUnban, this, "Unban a certain nick name");
Console()->Register("name_bans", "", CFGFLAG_SERVER, ConNameBans, this, "List all name bans"); Console()->Register("name_bans", "", CFGFLAG_SERVER, ConNameBans, this, "List all name bans");

View file

@ -10,7 +10,7 @@ TEST(NameBan, Empty)
TEST(NameBan, Equality) TEST(NameBan, Equality)
{ {
CNameBan Abc0("abc", 0); CNameBan Abc0("abc", 0, 0);
ASSERT_TRUE(IsNameBanned("abc", &Abc0, 1)); ASSERT_TRUE(IsNameBanned("abc", &Abc0, 1));
ASSERT_TRUE(IsNameBanned(" abc", &Abc0, 1)); ASSERT_TRUE(IsNameBanned(" abc", &Abc0, 1));
ASSERT_TRUE(IsNameBanned("abc ", &Abc0, 1)); ASSERT_TRUE(IsNameBanned("abc ", &Abc0, 1));
@ -19,3 +19,11 @@ TEST(NameBan, Equality)
ASSERT_FALSE(IsNameBanned("def", &Abc0, 1)); ASSERT_FALSE(IsNameBanned("def", &Abc0, 1));
ASSERT_FALSE(IsNameBanned("abcdef", &Abc0, 1)); ASSERT_FALSE(IsNameBanned("abcdef", &Abc0, 1));
} }
TEST(NameBan, Substring)
{
CNameBan Xyz("xyz", 0, 1);
ASSERT_TRUE(IsNameBanned("abcxyz", &Xyz, 1));
ASSERT_TRUE(IsNameBanned("abcxyzdef", &Xyz, 1));
ASSERT_FALSE(IsNameBanned("abcdef", &Xyz, 1));
}