Add ChatFilter2, OverheadChat, EventGreeting and update several plugins

New plugins: ChatFilter2, OverheadChat, EventGreeting.

Updated: HostnameManager, Leader3 (chat and markers), NameFilter2 (plugin
and config), PlayerRankings_nohlstats (rank display modes), custom-chatcolors
(plugin and ccc.inc), Extend (sm_settimeleft).

Add shared includes the plugins build against: Break, ChatFilter2, Couch,
EventNotifier, TransmitManager, leader, ripext, unloze_playtime.
This commit is contained in:
2026-11-23 13:20:15 +00:00
parent 2dca39be8a
commit 3a52b1dfd1
25 changed files with 6209 additions and 175 deletions
+502 -46
View File
@@ -9,6 +9,7 @@
#define MAX_STACKED_MARKS 2 // combining marks allowed on one letter (Vietnamese needs 2)
#define RELOAD_FILE "data/namefilter2_reload.dat"
#define RELOAD_MAX_AGE 120 // seconds; older saves are from a previous server run
#define MAX_RENAME_RETRIES 2
enum struct SavedName
{
@@ -18,10 +19,21 @@ enum struct SavedName
char sCurrent[MAX_NAME_LENGTH];
}
// a filtered player who left; their client keeps the name we gave them
// (ChangeName runs `name` on the client), so they rejoin wearing it
enum struct PastName
{
char sOriginal[MAX_NAME_LENGTH];
char sAssigned[MAX_NAME_LENGTH];
bool bReplaced;
}
char g_sOriginalName[MAXPLAYERS + 1][MAX_NAME_LENGTH];
char g_sAssignedName[MAXPLAYERS + 1][MAX_NAME_LENGTH]; // the filtered name we gave the player
char g_sBlockEventName[MAXPLAYERS + 1][MAX_NAME_LENGTH]; // raw name whose player_changename to hide
int g_iRenameRetries[MAXPLAYERS + 1];
ArrayList g_SavedNames; // original names saved over a plugin reload
int g_iBlockNameChangeEvents[MAXPLAYERS + 1] = {0, ...};
StringMap g_PastNames; // engine auth id -> PastName, for the whole server run
bool g_bFilteredOnConnect[MAXPLAYERS + 1];
bool g_bReplacedOnConnect[MAXPLAYERS + 1];
bool g_bLastFilterReplaced; // whether the last FilterName() assigned a replacement name
@@ -31,6 +43,8 @@ ArrayList g_BannedExprs;
ArrayList g_ReplacementNames;
ArrayList g_ConfusableFrom;
ArrayList g_ConfusableTo;
StringMap g_ConfusableSet; // confusable keys, for spotting spoofed Latin words
StringMap g_Transliterate; // Cyrillic letter -> phonetic Latin
ArrayList g_WhitelistCps;
int g_iMinNameLength = 2;
int g_iMinSurvivedPct = 50;
@@ -52,6 +66,9 @@ public void OnPluginStart()
RegConsoleCmd("sm_originalname", Command_OriginalName, "Show a player's original name");
RegConsoleCmd("sm_changednames", Command_ChangedNames, "List all players whose names were changed");
RegAdminCmd("sm_namefilter2_reload", Command_Reload, ADMFLAG_CONFIG, "Reload the NameFilter2 config");
RegAdminCmd("sm_namefilter2_test", Command_Test, ADMFLAG_CONFIG, "Show what the filter makes of a name");
g_PastNames = new StringMap();
LoadConfig();
LoadSavedNames();
@@ -88,6 +105,25 @@ public void OnPluginEnd()
hFile.WriteString(sCurrent, true);
}
// userid 0 ends the client list; the past names follow
hFile.WriteInt32(0);
StringMapSnapshot snap = g_PastNames.Snapshot();
for(int i = 0; i < snap.Length; i++)
{
char sAuth[64];
snap.GetKey(i, sAuth, sizeof(sAuth));
PastName past;
g_PastNames.GetArray(sAuth, past, sizeof(past));
hFile.WriteString(sAuth, true);
hFile.WriteString(past.sOriginal, true);
hFile.WriteString(past.sAssigned, true);
hFile.WriteInt8(past.bReplaced);
}
delete snap;
delete hFile;
}
@@ -106,13 +142,25 @@ void LoadSavedNames()
if(hFile.ReadInt32(iSaved) && GetTime() - iSaved <= RELOAD_MAX_AGE)
{
SavedName saved;
while(hFile.ReadInt32(saved.iUserId)
while(hFile.ReadInt32(saved.iUserId) && saved.iUserId
&& hFile.ReadString(saved.sOriginal, sizeof(saved.sOriginal)) != -1
&& hFile.ReadString(saved.sAssigned, sizeof(saved.sAssigned)) != -1
&& hFile.ReadString(saved.sCurrent, sizeof(saved.sCurrent)) != -1)
{
g_SavedNames.PushArray(saved);
}
char sAuth[64];
PastName past;
int iReplaced;
while(hFile.ReadString(sAuth, sizeof(sAuth)) != -1
&& hFile.ReadString(past.sOriginal, sizeof(past.sOriginal)) != -1
&& hFile.ReadString(past.sAssigned, sizeof(past.sAssigned)) != -1
&& hFile.ReadInt8(iReplaced))
{
past.bReplaced = iReplaced != 0;
g_PastNames.SetArray(sAuth, past, sizeof(past));
}
}
delete hFile;
@@ -145,7 +193,8 @@ bool RestoreSavedName(int client, const char[] sName)
public void OnClientConnected(int client)
{
g_iBlockNameChangeEvents[client] = 0;
g_sBlockEventName[client][0] = '\0';
g_iRenameRetries[client] = 0;
g_bFilteredOnConnect[client] = false;
g_bReplacedOnConnect[client] = false;
g_sAssignedName[client][0] = '\0';
@@ -169,6 +218,12 @@ public void OnClientConnected(int client)
}
}
public void OnClientAuthorized(int client, const char[] auth)
{
if(!IsFakeClient(client) && IsClientInGame(client))
CheckRejoinedName(client);
}
public void OnClientPutInServer(int client)
{
if(IsFakeClient(client))
@@ -177,12 +232,17 @@ public void OnClientPutInServer(int client)
char sName[MAX_NAME_LENGTH];
GetClientName(client, sName, sizeof(sName));
// the client can re-send its raw name during signon, undoing the
// OnClientConnected rename (or bringing a new name with it)
char sRaw[MAX_NAME_LENGTH];
strcopy(sRaw, sizeof(sRaw), sName);
if(FilterName(client, sName))
{
g_bFilteredOnConnect[client] = true;
if(g_bLastFilterReplaced)
g_bReplacedOnConnect[client] = true;
g_iBlockNameChangeEvents[client] = 2;
strcopy(g_sOriginalName[client], MAX_NAME_LENGTH, sRaw);
strcopy(g_sAssignedName[client], MAX_NAME_LENGTH, sName);
SetClientName(client, sName);
}
@@ -192,21 +252,100 @@ public void OnClientPutInServer(int client)
NotifyAdminsFiltered(client, sName, g_bReplacedOnConnect[client]);
NotifyClientRenamed(client, sName, g_bReplacedOnConnect[client]);
}
else if(IsClientAuthorized(client))
CheckRejoinedName(client);
}
public void OnClientDisconnect(int client)
{
if(!IsFakeClient(client) && IsClientAuthorized(client))
{
char sAuth[64];
if(GetClientAuthId(client, AuthId_Engine, sAuth, sizeof(sAuth)))
{
if(g_sAssignedName[client][0])
{
PastName past;
strcopy(past.sOriginal, sizeof(past.sOriginal), g_sOriginalName[client]);
strcopy(past.sAssigned, sizeof(past.sAssigned), g_sAssignedName[client]);
past.bReplaced = IsReplacementName(g_sAssignedName[client]);
g_PastNames.SetArray(sAuth, past, sizeof(past));
}
else
g_PastNames.Remove(sAuth);
}
}
g_sOriginalName[client][0] = '\0';
g_sAssignedName[client][0] = '\0';
g_iBlockNameChangeEvents[client] = 0;
g_sBlockEventName[client][0] = '\0';
g_iRenameRetries[client] = 0;
g_bFilteredOnConnect[client] = false;
g_bReplacedOnConnect[client] = false;
}
// a player whose client still carries a name we gave them earlier gets the
// notices again, with the original restored when this server run saw it.
// Runs once per connection: whichever of auth and put-in-server comes last
void CheckRejoinedName(int client)
{
if(g_sAssignedName[client][0] || g_bFilteredOnConnect[client])
return;
char sName[MAX_NAME_LENGTH];
GetClientName(client, sName, sizeof(sName));
char sAuth[64];
PastName past;
if(GetClientAuthId(client, AuthId_Engine, sAuth, sizeof(sAuth))
&& g_PastNames.GetArray(sAuth, past, sizeof(past))
&& StrEqual(sName, past.sAssigned))
{
strcopy(g_sOriginalName[client], MAX_NAME_LENGTH, past.sOriginal);
strcopy(g_sAssignedName[client], MAX_NAME_LENGTH, past.sAssigned);
g_bFilteredOnConnect[client] = true;
g_bReplacedOnConnect[client] = past.bReplaced;
NotifyAdminsFiltered(client, sName, past.bReplaced);
NotifyClientRenamed(client, sName, past.bReplaced);
return;
}
if(IsReplacementName(sName))
{
NotifyAdminsUnknownOriginal(client, sName);
NotifyClientRenamed(client, sName, true);
}
}
// true if the change from the player's original name to sNewName only
// trimmed or collapsed spaces and removed invisible characters
bool IsCosmeticChange(int client, const char[] sNewName, bool bReplaced)
{
if(bReplaced)
return false;
char sTidied[MAX_NAME_LENGTH];
strcopy(sTidied, sizeof(sTidied), g_sOriginalName[client]);
StripInvisible(sTidied);
NormalizeWhitespace(sTidied);
return StrEqual(sTidied, sNewName);
}
// cosmetic changes get only the server console line
void NotifyAdminsFiltered(int client, const char[] sNewName, bool bReplaced)
{
PrintToServer("[NameFilter2] \"%s\" (#%d) was %s \"%s\"", g_sOriginalName[client], GetClientUserId(client),
bReplaced ? "given replacement name" : "renamed to", sNewName);
char sOriginal[MAX_NAME_LENGTH];
SanitizeForLog(g_sOriginalName[client], sOriginal, sizeof(sOriginal));
bool bCosmetic = IsCosmeticChange(client, sNewName, bReplaced);
PrintToServer("[NameFilter2] \"%s\" (#%d) was %s \"%s\"", sOriginal, GetClientUserId(client),
bReplaced ? "given replacement name" : bCosmetic ? "tidied to" : "renamed to", sNewName);
if(bCosmetic)
return;
for(int i = 1; i <= MaxClients; i++)
{
@@ -216,29 +355,57 @@ void NotifyAdminsFiltered(int client, const char[] sNewName, bool bReplaced)
if(!CheckCommandAccess(i, "sm_namefilter2_notify", ADMFLAG_GENERIC))
continue;
PrintToChat(i, CHAT_PREFIX ... " \x07FF6060%s\x01 (#%d) was renamed to \x0760FF60%s\x01", g_sOriginalName[client], GetClientUserId(client), sNewName);
PrintToChat(i, CHAT_PREFIX ... " \x07FF6060%s\x01 (#%d) was renamed to \x0760FF60%s\x01", sOriginal, GetClientUserId(client), sNewName);
}
}
void NotifyAdminsUnknownOriginal(int client, const char[] sName)
{
PrintToServer("[NameFilter2] \"%s\" (#%d) joined with a replacement name, original name unknown", sName, GetClientUserId(client));
for(int i = 1; i <= MaxClients; i++)
{
if(!IsClientInGame(i) || IsFakeClient(i))
continue;
if(!CheckCommandAccess(i, "sm_namefilter2_notify", ADMFLAG_GENERIC))
continue;
PrintToChat(i, CHAT_PREFIX ... " \x0760FF60%s\x01 (#%d) joined with a replacement name, original name unknown", sName, GetClientUserId(client));
}
}
void NotifyClientRenamed(int client, const char[] sNewName, bool bReplaced)
{
if(IsCosmeticChange(client, sNewName, bReplaced))
return;
if(bReplaced)
PrintToChat(client, CHAT_PREFIX ... " Your name was unable to be filtered and was replaced with \x0760FF60%s\x01.", sNewName);
else
PrintToChat(client, CHAT_PREFIX ... " Your name was filtered to \x0760FF60%s\x01.", sNewName);
}
// changes to the name we gave the player are ours and stay hidden, as is the
// event of a raw change whose SayText2 we filtered. Matching by name instead
// of counting messages means a message that never arrives cannot leave a
// stale block that lets the player's next rename skip the filter
public Action Event_ChangeName(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if(g_iBlockNameChangeEvents[client])
{
g_iBlockNameChangeEvents[client]--;
SetEventBroadcast(event, true);
return Plugin_Handled;
}
if(!client)
return Plugin_Continue;
return Plugin_Continue;
char sNewName[MAX_NAME_LENGTH];
event.GetString("newname", sNewName, sizeof(sNewName));
if(g_sBlockEventName[client][0] && StrEqual(sNewName, g_sBlockEventName[client]))
g_sBlockEventName[client][0] = '\0';
else if(!g_sAssignedName[client][0] || !StrEqual(sNewName, g_sAssignedName[client]))
return Plugin_Continue;
SetEventBroadcast(event, true);
return Plugin_Handled;
}
public Action UserMessage_SayText2(UserMsg msg_id, BfRead msg, const int[] players, int playersNum, bool reliable, bool init)
@@ -278,16 +445,10 @@ public Action UserMessage_SayText2(UserMsg msg_id, BfRead msg, const int[] playe
if(client < 1 || client > MaxClients)
return Plugin_Continue;
if(g_iBlockNameChangeEvents[client])
{
g_iBlockNameChangeEvents[client]--;
return Plugin_Handled;
}
// the name we gave them (Timer_ChangeName's re-apply or any later echo of
// it); let it broadcast but keep the original it was filtered from
// the name we gave them: SetClientName's own change, or any later echo
// of it; keep the original it was filtered from
if(g_sAssignedName[client][0] && StrEqual(sNewName, g_sAssignedName[client]))
return Plugin_Continue;
return Plugin_Handled;
strcopy(g_sOriginalName[client], MAX_NAME_LENGTH, sNewName);
g_sAssignedName[client][0] = '\0';
@@ -295,14 +456,13 @@ public Action UserMessage_SayText2(UserMsg msg_id, BfRead msg, const int[] playe
if(FilterName(client, sNewName))
{
strcopy(g_sAssignedName[client], MAX_NAME_LENGTH, sNewName);
strcopy(g_sBlockEventName[client], MAX_NAME_LENGTH, g_sOriginalName[client]);
g_iRenameRetries[client] = 0;
// CCSPlayer::ChangeName sends this SayText2 before it fires the event
// and stores the new name, so a SetClientName from in here is undone
// as soon as we return. Re-apply later instead, even when the filtered
// name equals the old one: 3 = this change's event, then the timer's
// SayText2 and event
g_iBlockNameChangeEvents[client] = 3;
// name equals the old one
DataPack pack = new DataPack();
pack.WriteCell(GetClientUserId(client));
pack.WriteString(sNewName);
@@ -321,18 +481,51 @@ public Action Timer_ChangeName(Handle timer, any data)
DataPack pack = view_as<DataPack>(data);
pack.Reset();
int client = GetClientOfUserId(pack.ReadCell());
int iUserId = pack.ReadCell();
int client = GetClientOfUserId(iUserId);
char sName[MAX_NAME_LENGTH];
pack.ReadString(sName, sizeof(sName));
bool bReplaced = view_as<bool>(pack.ReadCell());
delete pack;
if(client)
// a later rename by the player superseded this one
if(!client || !StrEqual(sName, g_sAssignedName[client]))
return Plugin_Stop;
SetClientName(client, sName);
NotifyAdminsFiltered(client, sName, bReplaced);
NotifyClientRenamed(client, sName, bReplaced);
CreateTimer(0.5, Timer_VerifyName, iUserId);
return Plugin_Stop;
}
// the rename should have taken effect inside SetClientName; if it did not,
// say so and try again, so a player never keeps a raw name unnoticed
public Action Timer_VerifyName(Handle timer, int iUserId)
{
int client = GetClientOfUserId(iUserId);
if(!client || !g_sAssignedName[client][0])
return Plugin_Stop;
char sName[MAX_NAME_LENGTH];
GetClientName(client, sName, sizeof(sName));
if(StrEqual(sName, g_sAssignedName[client]))
return Plugin_Stop;
char sSafe[MAX_NAME_LENGTH];
SanitizeForLog(sName, sSafe, sizeof(sSafe));
bool bRetry = g_iRenameRetries[client]++ < MAX_RENAME_RETRIES;
PrintToServer("[NameFilter2] rename of #%d to \"%s\" did not stick (name is \"%s\")%s", iUserId,
g_sAssignedName[client], sSafe, bRetry ? ", retrying" : "");
if(bRetry)
{
SetClientName(client, sName);
NotifyAdminsFiltered(client, sName, bReplaced);
NotifyClientRenamed(client, sName, bReplaced);
SetClientName(client, g_sAssignedName[client]);
CreateTimer(0.5, Timer_VerifyName, iUserId);
}
return Plugin_Stop;
@@ -365,13 +558,11 @@ public Action Command_OriginalName(int client, int args)
for(int i = 0; i < iCount; i++)
{
char sCurrent[MAX_NAME_LENGTH];
GetClientName(aTargets[i], sCurrent, sizeof(sCurrent));
if(StrEqual(sCurrent, g_sOriginalName[aTargets[i]]))
ReplyToCommand(client, "[SM] %N's name was not changed.", aTargets[i]);
char sLine[256];
if(DescribeNameChange(aTargets[i], sLine, sizeof(sLine)))
ReplyToCommand(client, "[SM] %s", sLine);
else
ReplyToCommand(client, "[SM] %N's original name: %s", aTargets[i], g_sOriginalName[aTargets[i]]);
ReplyToCommand(client, "[SM] %N's name was not changed.", aTargets[i]);
}
return Plugin_Handled;
@@ -380,18 +571,17 @@ public Action Command_OriginalName(int client, int args)
public Action Command_ChangedNames(int client, int args)
{
int iFound = 0;
char sCurrent[MAX_NAME_LENGTH];
char sLine[256];
for(int i = 1; i <= MaxClients; i++)
{
if(!IsClientConnected(i) || IsFakeClient(i))
continue;
GetClientName(i, sCurrent, sizeof(sCurrent));
if(StrEqual(sCurrent, g_sOriginalName[i]))
if(!DescribeNameChange(i, sLine, sizeof(sLine)))
continue;
PrintToConsole(client, "%s - original name: %s", sCurrent, g_sOriginalName[i]);
PrintToConsole(client, "%s", sLine);
iFound++;
}
@@ -403,6 +593,92 @@ public Action Command_ChangedNames(int client, int args)
return Plugin_Handled;
}
// "<current> - original name: <original>" for a player whose name we changed,
// including one who rejoined wearing a replacement name; false otherwise
bool DescribeNameChange(int client, char[] sLine, int Length)
{
char sCurrent[MAX_NAME_LENGTH];
GetClientName(client, sCurrent, sizeof(sCurrent));
char sOriginal[MAX_NAME_LENGTH];
SanitizeForLog(g_sOriginalName[client], sOriginal, sizeof(sOriginal));
if(g_sAssignedName[client][0])
{
if(StrEqual(sCurrent, g_sAssignedName[client]))
FormatEx(sLine, Length, "%s - original name: %s", sCurrent, sOriginal);
else
FormatEx(sLine, Length, "%s - original name: %s (filter did not stick, expected %s)", sCurrent, sOriginal, g_sAssignedName[client]);
return true;
}
if(!StrEqual(sCurrent, g_sOriginalName[client]))
{
FormatEx(sLine, Length, "%s - original name: %s", sCurrent, sOriginal);
return true;
}
if(IsReplacementName(sCurrent))
{
FormatEx(sLine, Length, "%s - original name: unknown (joined with a replacement name)", sCurrent);
return true;
}
return false;
}
// a configured replacement name, or AssignReplacementName's "<name> <userid>"
// fallback for when they are all taken
bool IsReplacementName(const char[] sName)
{
char sCandidate[MAX_NAME_LENGTH];
for(int i = 0; i < g_ReplacementNames.Length; i++)
{
g_ReplacementNames.GetString(i, sCandidate, sizeof(sCandidate));
int iLen = strlen(sCandidate);
if(strncmp(sName, sCandidate, iLen) != 0)
continue;
if(sName[iLen] == '\0')
return true;
if(sName[iLen] != ' ' || sName[iLen + 1] == '\0')
continue;
int k = iLen + 1;
while(IsCharNumeric(sName[k]))
k++;
if(sName[k] == '\0')
return true;
}
return false;
}
public Action Command_Test(int client, int args)
{
if(args < 1)
{
ReplyToCommand(client, "[SM] Usage: sm_namefilter2_test <name>");
return Plugin_Handled;
}
char sName[MAX_NAME_LENGTH];
GetCmdArgString(sName, sizeof(sName));
StripQuotes(sName);
char sIn[MAX_NAME_LENGTH];
SanitizeForLog(sName, sIn, sizeof(sIn));
// client 0 only steers which replacement name is picked
bool bChanged = FilterName(0, sName);
ReplyToCommand(client, "[SM] \"%s\" -> \"%s\"%s", sIn, sName,
g_bLastFilterReplaced ? " (replacement)" : (bChanged ? "" : " (unchanged)"));
return Plugin_Handled;
}
public Action Command_Reload(int client, int args)
{
LoadConfig();
@@ -433,6 +709,8 @@ void LoadConfig()
delete g_ReplacementNames;
delete g_ConfusableFrom;
delete g_ConfusableTo;
delete g_ConfusableSet;
delete g_Transliterate;
delete g_WhitelistCps;
static char sConfigFile[PLATFORM_MAX_PATH];
@@ -472,6 +750,7 @@ void LoadConfig()
g_ConfusableFrom = new ArrayList(ByteCountToCells(16));
g_ConfusableTo = new ArrayList(ByteCountToCells(16));
g_ConfusableSet = new StringMap();
if(Config.JumpToKey("confusables"))
{
if(Config.GotoFirstSubKey(false))
@@ -486,6 +765,7 @@ void LoadConfig()
{
g_ConfusableFrom.PushString(sFrom);
g_ConfusableTo.PushString(sBuffer);
g_ConfusableSet.SetValue(sFrom, true);
}
} while(Config.GotoNextKey(false));
Config.GoBack();
@@ -493,6 +773,25 @@ void LoadConfig()
Config.GoBack();
}
g_Transliterate = new StringMap();
if(Config.JumpToKey("transliterate"))
{
if(Config.GotoFirstSubKey(false))
{
do
{
char sFrom[16];
Config.GetSectionName(sFrom, sizeof(sFrom));
Config.GetString(NULL_STRING, sBuffer, sizeof(sBuffer));
if(sFrom[0])
g_Transliterate.SetString(sFrom, sBuffer);
} while(Config.GotoNextKey(false));
Config.GoBack();
}
Config.GoBack();
}
g_AdvertExprs = new ArrayList();
if(Config.JumpToKey("adverts"))
{
@@ -565,8 +864,10 @@ bool FilterName(int client, char[] sName, int Length = MAX_NAME_LENGTH)
char sBefore[MAX_NAME_LENGTH];
strcopy(sBefore, sizeof(sBefore), sName);
// applies to kept names too, so none can hide characters or pose as another
// applies to kept names too, so none can hide characters or pose as
// another ("Name " next to "Name")
StripInvisible(sName);
NormalizeWhitespace(sName);
char sClean[MAX_NAME_LENGTH];
strcopy(sClean, sizeof(sClean), sName);
@@ -576,6 +877,7 @@ bool FilterName(int client, char[] sName, int Length = MAX_NAME_LENGTH)
bool bKeepOriginal = HasTypeableRun(sClean);
bool bContentRemoved = false;
TransliterateCyrillic(sName, Length);
ApplyConfusables(sName, Length);
int iLettersBefore = CountLetters(sName);
@@ -711,6 +1013,34 @@ void StripInvisible(char[] sName)
sName[w] = '\0';
}
// copy of a name that is safe to print: invalid UTF-8 and control
// characters become '?' (journald hides whole lines containing them)
void SanitizeForLog(const char[] sIn, char[] sOut, int Length)
{
int r = 0;
int w = 0;
while(sIn[r] != '\0')
{
int rStart = r;
int cp = DecodeUTF8Char(sIn, r);
if(cp < 0 || cp < 0x20 || (cp >= 0x7F && cp <= 0x9F))
{
if(w + 1 >= Length)
break;
sOut[w++] = '?';
continue;
}
if(w + (r - rStart) >= Length)
break;
for(int k = rStart; k < r; k++)
sOut[w++] = sIn[k];
}
sOut[w] = '\0';
}
bool IsInvisibleCp(int cp)
{
return cp < 0x20 || (cp >= 0x7F && cp <= 0x9F) // C0/C1 controls, DEL
@@ -741,6 +1071,126 @@ bool IsCombiningMarkCp(int cp)
|| (cp >= 0xFE20 && cp <= 0xFE2F); // half marks
}
// spells out each word containing Cyrillic phonetically (Киллер -> Killer),
// unless it is a Latin word with only lookalike Cyrillic mixed in (a spoof
// such as "аdmin") or mixes in other scripts, which ApplyConfusables folds
// back to the Latin text it imitates. All-caps words stay all-caps (ЖУК -> ZHUK)
void TransliterateCyrillic(char[] sName, int Length)
{
char sOut[MAX_NAME_LENGTH * 2];
int w = 0;
int r = 0;
while(sName[r] != '\0')
{
if(sName[r] == ' ')
{
if(w + 1 < sizeof(sOut))
sOut[w++] = ' ';
r++;
continue;
}
int iEnd = r;
while(sName[iEnd] != '\0' && sName[iEnd] != ' ')
iEnd++;
bool bUpper;
bool bSpell = IsCyrillicWord(sName, r, iEnd, bUpper);
int wStart = w;
while(r < iEnd)
{
int rStart = r;
int cp = DecodeUTF8Char(sName, r);
char sChar[8];
for(int k = rStart; k < r && k - rStart < sizeof(sChar) - 1; k++)
sChar[k - rStart] = sName[k];
sChar[r - rStart < sizeof(sChar) ? r - rStart : sizeof(sChar) - 1] = '\0';
// in a lookalike word, Cyrillic without a confusable is still
// spelled out rather than lost when FoldToASCII strips it
bool bDummy;
char sTo[16];
if(!IsCyrillicCp(cp)
|| (!bSpell && g_ConfusableSet.GetValue(sChar, bDummy))
|| !g_Transliterate.GetString(sChar, sTo, sizeof(sTo)))
strcopy(sTo, sizeof(sTo), sChar);
for(int k = 0; sTo[k] != '\0' && w + 1 < sizeof(sOut); k++)
sOut[w++] = sTo[k];
}
if(bSpell && bUpper)
{
for(int k = wStart; k < w; k++)
sOut[k] = CharToUpper(sOut[k]);
}
}
sOut[w] = '\0';
strcopy(sName, Length, sOut);
}
// true if the word in s[iStart, iEnd) should be transliterated: it has
// Cyrillic, is not a Latin word whose Cyrillic letters are all confusables,
// and has no letters from other scripts. Real Cyrillic words never mix in
// Greek or styled letters; a word that does ("ιηƒιηιтє") is decoration that
// imitates Latin. bUpper is set when it has two or more Cyrillic letters,
// all uppercase
bool IsCyrillicWord(const char[] s, int iStart, int iEnd, bool &bUpper)
{
bool bLatin = false;
bool bOtherScript = false;
bool bAllConfusable = true;
int iCyrillic = 0;
int iLower = 0;
int i = iStart;
while(i < iEnd)
{
int iChar = i;
int cp = DecodeUTF8Char(s, i);
if((cp >= 'a' && cp <= 'z') || (cp >= 'A' && cp <= 'Z'))
{
bLatin = true;
continue;
}
if(!IsCyrillicCp(cp))
{
if(cp >= 0x80 && IsLetterCp(cp))
bOtherScript = true;
continue;
}
iCyrillic++;
// basic lowercase block, then upper/lower pairs from U+0460 on
if((cp >= 0x430 && cp <= 0x45F) || (cp >= 0x460 && cp & 1))
iLower++;
char sChar[8];
int iLen = i - iChar;
for(int k = 0; k < iLen && k < sizeof(sChar) - 1; k++)
sChar[k] = s[iChar + k];
sChar[iLen < sizeof(sChar) ? iLen : sizeof(sChar) - 1] = '\0';
bool bDummy;
if(!g_ConfusableSet.GetValue(sChar, bDummy))
bAllConfusable = false;
}
bUpper = iCyrillic >= 2 && !iLower;
return iCyrillic && !bOtherScript && !(bLatin && bAllConfusable);
}
bool IsCyrillicCp(int cp)
{
return cp >= 0x400 && cp <= 0x4FF;
}
void ApplyConfusables(char[] sName, int Length)
{
char sFrom[16];
@@ -1135,12 +1585,18 @@ void NormalizeWhitespace(char[] sName)
TrimString(sName);
}
// picked by Steam account, so a player gets the same replacement name on
// every visit (unless someone else is wearing it); the slot is the fallback
// for clients without one
void AssignReplacementName(int client, char[] sName, int Length)
{
g_bLastFilterReplaced = true;
int iCount = g_ReplacementNames.Length;
int iBase = client % iCount;
int iSeed = client > 0 ? GetSteamAccountID(client, false) : 0;
if(!iSeed)
iSeed = client;
int iBase = (iSeed & 0x7FFFFFFF) % iCount;
char sCandidate[MAX_NAME_LENGTH];
for(int i = 0; i < iCount; i++)
@@ -1170,5 +1626,5 @@ void AssignReplacementName(int client, char[] sName, int Length)
}
g_ReplacementNames.GetString(iBase, sCandidate, sizeof(sCandidate));
Format(sName, Length, "%s %d", sCandidate, GetClientUserId(client));
Format(sName, Length, "%s %d", sCandidate, client ? GetClientUserId(client) : 0);
}