2020-08-18 20:42:30 +00:00
# include "ChatManager.hpp"
# include "PlayerManager.hpp"
2021-03-12 19:07:26 +00:00
# include "GroupManager.hpp"
# include "CustomCommands.hpp"
2020-10-07 18:38:32 +00:00
2021-03-12 19:07:26 +00:00
# include <assert.h>
2020-10-18 21:02:51 +00:00
2021-03-12 19:07:26 +00:00
std : : vector < std : : string > ChatManager : : dump ;
2021-03-06 21:29:52 +00:00
2020-08-18 20:42:30 +00:00
void ChatManager : : init ( ) {
REGISTER_SHARD_PACKET ( P_CL2FE_REQ_SEND_FREECHAT_MESSAGE , chatHandler ) ;
REGISTER_SHARD_PACKET ( P_CL2FE_REQ_PC_AVATAR_EMOTES_CHAT , emoteHandler ) ;
2020-08-22 18:11:47 +00:00
REGISTER_SHARD_PACKET ( P_CL2FE_REQ_SEND_MENUCHAT_MESSAGE , menuChatHandler ) ;
2020-10-02 23:50:47 +00:00
2021-03-12 19:07:26 +00:00
REGISTER_SHARD_PACKET ( P_CL2FE_REQ_SEND_BUDDY_FREECHAT_MESSAGE , buddyChatHandler ) ;
REGISTER_SHARD_PACKET ( P_CL2FE_REQ_SEND_BUDDY_MENUCHAT_MESSAGE , buddyMenuChatHandler ) ;
REGISTER_SHARD_PACKET ( P_CL2FE_REQ_PC_TRADE_EMOTES_CHAT , tradeChatHandler ) ;
2020-10-02 23:50:47 +00:00
2021-03-12 19:07:26 +00:00
REGISTER_SHARD_PACKET ( P_CL2FE_REQ_SEND_ALL_GROUP_FREECHAT_MESSAGE , groupChatHandler ) ;
REGISTER_SHARD_PACKET ( P_CL2FE_REQ_SEND_ALL_GROUP_MENUCHAT_MESSAGE , groupMenuChatHandler ) ;
REGISTER_SHARD_PACKET ( P_CL2FE_GM_REQ_PC_ANNOUNCE , announcementHandler ) ;
2020-08-18 20:42:30 +00:00
}
void ChatManager : : chatHandler ( CNSocket * sock , CNPacketData * data ) {
2020-08-20 23:50:30 +00:00
if ( data - > size ! = sizeof ( sP_CL2FE_REQ_SEND_FREECHAT_MESSAGE ) )
return ; // malformed packet
2020-10-05 00:03:13 +00:00
2020-08-18 20:42:30 +00:00
sP_CL2FE_REQ_SEND_FREECHAT_MESSAGE * chat = ( sP_CL2FE_REQ_SEND_FREECHAT_MESSAGE * ) data - > buf ;
2020-10-02 23:50:47 +00:00
Player * plr = PlayerManager : : getPlayer ( sock ) ;
2020-10-31 20:31:25 +00:00
std : : string fullChat = sanitizeText ( U16toU8 ( chat - > szFreeChat ) ) ;
2020-10-03 00:20:59 +00:00
if ( fullChat . length ( ) > 1 & & fullChat [ 0 ] = = CMD_PREFIX ) { // PREFIX
2021-03-12 19:07:26 +00:00
CustomCommands : : runCmd ( fullChat , sock ) ;
2020-10-02 23:50:47 +00:00
return ;
}
2020-08-19 18:22:51 +00:00
2020-12-31 01:13:43 +00:00
if ( plr - > iSpecialState & CN_SPECIAL_STATE_FLAG__MUTE_FREECHAT )
return ;
std : : string logLine = " [FreeChat] " + PlayerManager : : getPlayerName ( plr , true ) + " : " + fullChat ;
std : : cout < < logLine < < std : : endl ;
dump . push_back ( logLine ) ;
2020-08-19 18:25:54 +00:00
// send to client
2020-08-23 00:26:18 +00:00
INITSTRUCT ( sP_FE2CL_REP_SEND_FREECHAT_MESSAGE_SUCC , resp ) ;
2020-10-31 20:31:25 +00:00
U8toU16 ( fullChat , ( char16_t * ) & resp . szFreeChat , sizeof ( resp . szFreeChat ) ) ;
2020-10-02 23:50:47 +00:00
resp . iPC_ID = plr - > iID ;
2020-08-22 23:31:09 +00:00
resp . iEmoteCode = chat - > iEmoteCode ;
2020-10-31 20:31:25 +00:00
2020-08-22 23:31:09 +00:00
sock - > sendPacket ( ( void * ) & resp , P_FE2CL_REP_SEND_FREECHAT_MESSAGE_SUCC , sizeof ( sP_FE2CL_REP_SEND_FREECHAT_MESSAGE_SUCC ) ) ;
2020-08-19 18:22:51 +00:00
2020-08-19 18:25:54 +00:00
// send to visible players
2020-09-17 22:45:43 +00:00
PlayerManager : : sendToViewable ( sock , ( void * ) & resp , P_FE2CL_REP_SEND_FREECHAT_MESSAGE_SUCC , sizeof ( sP_FE2CL_REP_SEND_FREECHAT_MESSAGE_SUCC ) ) ;
2020-08-18 20:42:30 +00:00
}
2020-09-16 19:46:15 +00:00
2020-08-22 18:11:47 +00:00
void ChatManager : : menuChatHandler ( CNSocket * sock , CNPacketData * data ) {
if ( data - > size ! = sizeof ( sP_CL2FE_REQ_SEND_MENUCHAT_MESSAGE ) )
return ; // malformed packet
2020-10-31 20:31:25 +00:00
2020-08-22 18:11:47 +00:00
sP_CL2FE_REQ_SEND_MENUCHAT_MESSAGE * chat = ( sP_CL2FE_REQ_SEND_MENUCHAT_MESSAGE * ) data - > buf ;
2020-10-31 20:31:25 +00:00
Player * plr = PlayerManager : : getPlayer ( sock ) ;
std : : string fullChat = sanitizeText ( U16toU8 ( chat - > szFreeChat ) ) ;
2020-12-31 01:13:43 +00:00
std : : string logLine = " [MenuChat] " + PlayerManager : : getPlayerName ( plr , true ) + " : " + fullChat ;
2020-10-31 20:31:25 +00:00
2020-12-31 01:13:43 +00:00
std : : cout < < logLine < < std : : endl ;
dump . push_back ( logLine ) ;
2020-08-22 18:11:47 +00:00
// send to client
2020-08-23 00:26:18 +00:00
INITSTRUCT ( sP_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC , resp ) ;
2020-10-31 20:31:25 +00:00
U8toU16 ( fullChat , ( char16_t * ) & resp . szFreeChat , sizeof ( resp . szFreeChat ) ) ;
2020-11-17 23:16:16 +00:00
resp . iPC_ID = PlayerManager : : getPlayer ( sock ) - > iID ;
2020-08-22 23:31:09 +00:00
resp . iEmoteCode = chat - > iEmoteCode ;
2020-10-31 20:31:25 +00:00
2020-08-22 23:31:09 +00:00
sock - > sendPacket ( ( void * ) & resp , P_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC , sizeof ( sP_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC ) ) ;
2020-08-18 20:42:30 +00:00
2020-08-22 18:11:47 +00:00
// send to visible players
2020-09-17 22:45:43 +00:00
PlayerManager : : sendToViewable ( sock , ( void * ) & resp , P_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC , sizeof ( sP_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC ) ) ;
2020-08-22 18:11:47 +00:00
}
2020-09-16 19:46:15 +00:00
2020-08-18 20:42:30 +00:00
void ChatManager : : emoteHandler ( CNSocket * sock , CNPacketData * data ) {
2020-08-19 00:11:31 +00:00
if ( data - > size ! = sizeof ( sP_CL2FE_REQ_PC_AVATAR_EMOTES_CHAT ) )
return ; // ignore the malformed packet
2020-08-18 20:42:30 +00:00
// you can dance with friends!!!!!!!!
sP_CL2FE_REQ_PC_AVATAR_EMOTES_CHAT * emote = ( sP_CL2FE_REQ_PC_AVATAR_EMOTES_CHAT * ) data - > buf ;
2020-10-02 23:50:47 +00:00
Player * plr = PlayerManager : : getPlayer ( sock ) ;
2020-09-14 13:53:48 +00:00
2020-08-18 20:42:30 +00:00
// send to client
2020-08-23 00:26:18 +00:00
INITSTRUCT ( sP_FE2CL_REP_PC_AVATAR_EMOTES_CHAT , resp ) ;
2020-08-22 23:31:09 +00:00
resp . iEmoteCode = emote - > iEmoteCode ;
2020-10-02 23:50:47 +00:00
resp . iID_From = plr - > iID ;
2020-08-22 23:31:09 +00:00
sock - > sendPacket ( ( void * ) & resp , P_FE2CL_REP_PC_AVATAR_EMOTES_CHAT , sizeof ( sP_FE2CL_REP_PC_AVATAR_EMOTES_CHAT ) ) ;
2020-08-18 20:42:30 +00:00
// send to visible players (players within render distance)
2020-09-17 22:45:43 +00:00
PlayerManager : : sendToViewable ( sock , ( void * ) & resp , P_FE2CL_REP_PC_AVATAR_EMOTES_CHAT , sizeof ( sP_FE2CL_REP_PC_AVATAR_EMOTES_CHAT ) ) ;
2020-08-18 20:42:30 +00:00
}
2020-10-02 23:50:47 +00:00
void ChatManager : : sendServerMessage ( CNSocket * sock , std : : string msg ) {
INITSTRUCT ( sP_FE2CL_PC_MOTD_LOGIN , motd ) ;
motd . iType = 1 ;
2020-10-04 17:52:16 +00:00
// convert string to u16 and write it to the buffer
2020-10-04 17:50:58 +00:00
U8toU16 ( msg , ( char16_t * ) motd . szSystemMsg , sizeof ( motd . szSystemMsg ) ) ;
2020-10-02 23:50:47 +00:00
// send the packet :)
sock - > sendPacket ( ( void * ) & motd , P_FE2CL_PC_MOTD_LOGIN , sizeof ( sP_FE2CL_PC_MOTD_LOGIN ) ) ;
}
2020-10-31 20:31:25 +00:00
2020-11-19 16:19:22 +00:00
void ChatManager : : announcementHandler ( CNSocket * sock , CNPacketData * data ) {
if ( data - > size ! = sizeof ( sP_CL2FE_GM_REQ_PC_ANNOUNCE ) )
return ; // ignore malformed packet
2020-11-17 23:16:16 +00:00
Player * plr = PlayerManager : : getPlayer ( sock ) ;
if ( plr - > accountLevel > 30 )
2020-11-19 16:19:22 +00:00
return ; // only players with account level less than 30 (GM) are allowed to use this command
sP_CL2FE_GM_REQ_PC_ANNOUNCE * announcement = ( sP_CL2FE_GM_REQ_PC_ANNOUNCE * ) data - > buf ;
INITSTRUCT ( sP_FE2CL_GM_REP_PC_ANNOUNCE , msg ) ;
msg . iAnnounceType = announcement - > iAnnounceType ;
msg . iDuringTime = announcement - > iDuringTime ;
memcpy ( msg . szAnnounceMsg , announcement - > szAnnounceMsg , sizeof ( msg . szAnnounceMsg ) ) ;
2020-11-17 23:16:16 +00:00
std : : map < CNSocket * , Player * > : : iterator it ;
2020-11-19 16:19:22 +00:00
switch ( announcement - > iAreaType ) {
2020-11-17 23:16:16 +00:00
case 0 : // area (all players in viewable chunks)
2020-12-01 22:32:45 +00:00
sock - > sendPacket ( ( void * ) & msg , P_FE2CL_GM_REP_PC_ANNOUNCE , sizeof ( sP_FE2CL_GM_REP_PC_ANNOUNCE ) ) ;
2020-11-17 23:16:16 +00:00
PlayerManager : : sendToViewable ( sock , ( void * ) & msg , P_FE2CL_GM_REP_PC_ANNOUNCE , sizeof ( sP_FE2CL_GM_REP_PC_ANNOUNCE ) ) ;
break ;
2020-11-19 16:19:22 +00:00
case 1 : // shard
case 2 : // world
2020-12-01 22:32:45 +00:00
break ; // not applicable to OpenFusion
2020-11-17 23:16:16 +00:00
case 3 : // global (all players)
2020-11-19 16:19:22 +00:00
for ( it = PlayerManager : : players . begin ( ) ; it ! = PlayerManager : : players . end ( ) ; it + + ) {
CNSocket * allSock = it - > first ;
allSock - > sendPacket ( ( void * ) & msg , P_FE2CL_GM_REP_PC_ANNOUNCE , sizeof ( sP_FE2CL_GM_REP_PC_ANNOUNCE ) ) ;
}
default :
break ;
}
2020-12-31 01:13:43 +00:00
std : : string logLine = " [Bcast " + std : : to_string ( announcement - > iAreaType ) + " ] " + PlayerManager : : getPlayerName ( plr , false ) + " : " + U16toU8 ( msg . szAnnounceMsg ) ;
std : : cout < < logLine < < std : : endl ;
dump . push_back ( " ** " + logLine + " ** " ) ;
2020-11-19 16:19:22 +00:00
}
2021-03-12 19:07:26 +00:00
// Buddy freechatting
void ChatManager : : buddyChatHandler ( CNSocket * sock , CNPacketData * data ) {
if ( data - > size ! = sizeof ( sP_CL2FE_REQ_SEND_BUDDY_FREECHAT_MESSAGE ) )
return ; // malformed packet
sP_CL2FE_REQ_SEND_BUDDY_FREECHAT_MESSAGE * pkt = ( sP_CL2FE_REQ_SEND_BUDDY_FREECHAT_MESSAGE * ) data - > buf ;
Player * plr = PlayerManager : : getPlayer ( sock ) ;
INITSTRUCT ( sP_FE2CL_REP_SEND_BUDDY_FREECHAT_MESSAGE_SUCC , resp ) ;
CNSocket * otherSock = PlayerManager : : getSockFromID ( pkt - > iBuddyPCUID ) ;
if ( otherSock = = nullptr )
return ; // buddy offline
Player * otherPlr = PlayerManager : : getPlayer ( otherSock ) ;
resp . iFromPCUID = plr - > PCStyle . iPC_UID ;
resp . iToPCUID = pkt - > iBuddyPCUID ;
resp . iEmoteCode = pkt - > iEmoteCode ;
std : : string fullChat = ChatManager : : sanitizeText ( U16toU8 ( pkt - > szFreeChat ) ) ;
if ( fullChat . length ( ) > 1 & & fullChat [ 0 ] = = CMD_PREFIX ) { // PREFIX
CustomCommands : : runCmd ( fullChat , sock ) ;
return ;
}
if ( plr - > iSpecialState & CN_SPECIAL_STATE_FLAG__MUTE_FREECHAT )
return ;
std : : string logLine = " [BuddyChat] " + PlayerManager : : getPlayerName ( plr ) + " (to " + PlayerManager : : getPlayerName ( otherPlr ) + " ): " + fullChat ;
std : : cout < < logLine < < std : : endl ;
ChatManager : : dump . push_back ( logLine ) ;
U8toU16 ( fullChat , ( char16_t * ) & resp . szFreeChat , sizeof ( resp . szFreeChat ) ) ;
sock - > sendPacket ( ( void * ) & resp , P_FE2CL_REP_SEND_BUDDY_FREECHAT_MESSAGE_SUCC , sizeof ( sP_FE2CL_REP_SEND_BUDDY_FREECHAT_MESSAGE_SUCC ) ) ; // confirm send to sender
otherSock - > sendPacket ( ( void * ) & resp , P_FE2CL_REP_SEND_BUDDY_FREECHAT_MESSAGE_SUCC , sizeof ( sP_FE2CL_REP_SEND_BUDDY_FREECHAT_MESSAGE_SUCC ) ) ; // broadcast send to receiver
}
// Buddy menuchat
void ChatManager : : buddyMenuChatHandler ( CNSocket * sock , CNPacketData * data ) {
if ( data - > size ! = sizeof ( sP_CL2FE_REQ_SEND_BUDDY_MENUCHAT_MESSAGE ) )
return ; // malformed packet
sP_CL2FE_REQ_SEND_BUDDY_MENUCHAT_MESSAGE * pkt = ( sP_CL2FE_REQ_SEND_BUDDY_MENUCHAT_MESSAGE * ) data - > buf ;
Player * plr = PlayerManager : : getPlayer ( sock ) ;
INITSTRUCT ( sP_FE2CL_REP_SEND_BUDDY_MENUCHAT_MESSAGE_SUCC , resp ) ;
CNSocket * otherSock = PlayerManager : : getSockFromID ( pkt - > iBuddyPCUID ) ;
if ( otherSock = = nullptr )
return ; // buddy offline
Player * otherPlr = PlayerManager : : getPlayer ( otherSock ) ;
resp . iFromPCUID = plr - > PCStyle . iPC_UID ;
resp . iToPCUID = pkt - > iBuddyPCUID ;
resp . iEmoteCode = pkt - > iEmoteCode ;
std : : string fullChat = ChatManager : : sanitizeText ( U16toU8 ( pkt - > szFreeChat ) ) ;
std : : string logLine = " [BuddyMenuChat] " + PlayerManager : : getPlayerName ( plr ) + " (to " + PlayerManager : : getPlayerName ( otherPlr ) + " ): " + fullChat ;
std : : cout < < logLine < < std : : endl ;
ChatManager : : dump . push_back ( logLine ) ;
U8toU16 ( fullChat , ( char16_t * ) & resp . szFreeChat , sizeof ( resp . szFreeChat ) ) ;
sock - > sendPacket ( ( void * ) & resp , P_FE2CL_REP_SEND_BUDDY_MENUCHAT_MESSAGE_SUCC , sizeof ( sP_FE2CL_REP_SEND_BUDDY_MENUCHAT_MESSAGE_SUCC ) ) ; // confirm send to sender
otherSock - > sendPacket ( ( void * ) & resp , P_FE2CL_REP_SEND_BUDDY_MENUCHAT_MESSAGE_SUCC , sizeof ( sP_FE2CL_REP_SEND_BUDDY_MENUCHAT_MESSAGE_SUCC ) ) ; // broadcast send to receiver
}
void ChatManager : : tradeChatHandler ( CNSocket * sock , CNPacketData * data ) {
if ( data - > size ! = sizeof ( sP_CL2FE_REQ_PC_TRADE_EMOTES_CHAT ) )
return ; // malformed packet
sP_CL2FE_REQ_PC_TRADE_EMOTES_CHAT * pacdat = ( sP_CL2FE_REQ_PC_TRADE_EMOTES_CHAT * ) data - > buf ;
CNSocket * otherSock ; // weird flip flop because we need to know who the other player is
if ( pacdat - > iID_Request = = pacdat - > iID_From )
otherSock = PlayerManager : : getSockFromID ( pacdat - > iID_To ) ;
else
otherSock = PlayerManager : : getSockFromID ( pacdat - > iID_From ) ;
if ( otherSock = = nullptr )
return ;
Player * otherPlr = PlayerManager : : getPlayer ( otherSock ) ;
INITSTRUCT ( sP_FE2CL_REP_PC_TRADE_EMOTES_CHAT , resp ) ;
Player * plr = PlayerManager : : getPlayer ( sock ) ;
resp . iID_Request = pacdat - > iID_Request ;
resp . iID_From = pacdat - > iID_From ;
resp . iID_To = pacdat - > iID_To ;
std : : string fullChat = ChatManager : : sanitizeText ( U16toU8 ( pacdat - > szFreeChat ) ) ;
U8toU16 ( fullChat , resp . szFreeChat , sizeof ( resp . szFreeChat ) ) ;
std : : string logLine = " [TradeChat] " + PlayerManager : : getPlayerName ( plr ) + " (to " + PlayerManager : : getPlayerName ( otherPlr ) + " ): " + fullChat ;
std : : cout < < logLine < < std : : endl ;
ChatManager : : dump . push_back ( logLine ) ;
resp . iEmoteCode = pacdat - > iEmoteCode ;
sock - > sendPacket ( ( void * ) & resp , P_FE2CL_REP_PC_TRADE_EMOTES_CHAT , sizeof ( sP_FE2CL_REP_PC_TRADE_EMOTES_CHAT ) ) ;
otherSock - > sendPacket ( ( void * ) & resp , P_FE2CL_REP_PC_TRADE_EMOTES_CHAT , sizeof ( sP_FE2CL_REP_PC_TRADE_EMOTES_CHAT ) ) ;
}
void ChatManager : : groupChatHandler ( CNSocket * sock , CNPacketData * data ) {
if ( data - > size ! = sizeof ( sP_CL2FE_REQ_SEND_ALL_GROUP_FREECHAT_MESSAGE ) )
return ; // malformed packet
sP_CL2FE_REQ_SEND_ALL_GROUP_FREECHAT_MESSAGE * chat = ( sP_CL2FE_REQ_SEND_ALL_GROUP_FREECHAT_MESSAGE * ) data - > buf ;
Player * plr = PlayerManager : : getPlayer ( sock ) ;
Player * otherPlr = PlayerManager : : getPlayerFromID ( plr - > iIDGroup ) ;
if ( otherPlr = = nullptr )
return ;
std : : string fullChat = ChatManager : : sanitizeText ( U16toU8 ( chat - > szFreeChat ) ) ;
if ( fullChat . length ( ) > 1 & & fullChat [ 0 ] = = CMD_PREFIX ) { // PREFIX
CustomCommands : : runCmd ( fullChat , sock ) ;
return ;
}
if ( plr - > iSpecialState & CN_SPECIAL_STATE_FLAG__MUTE_FREECHAT )
return ;
std : : string logLine = " [GroupChat] " + PlayerManager : : getPlayerName ( plr , true ) + " : " + fullChat ;
std : : cout < < logLine < < std : : endl ;
ChatManager : : dump . push_back ( logLine ) ;
// send to client
INITSTRUCT ( sP_FE2CL_REP_SEND_ALL_GROUP_FREECHAT_MESSAGE_SUCC , resp ) ;
U8toU16 ( fullChat , ( char16_t * ) & resp . szFreeChat , sizeof ( resp . szFreeChat ) ) ;
resp . iSendPCID = plr - > iID ;
resp . iEmoteCode = chat - > iEmoteCode ;
GroupManager : : sendToGroup ( otherPlr , ( void * ) & resp , P_FE2CL_REP_SEND_ALL_GROUP_FREECHAT_MESSAGE_SUCC , sizeof ( sP_FE2CL_REP_SEND_ALL_GROUP_FREECHAT_MESSAGE_SUCC ) ) ;
}
void ChatManager : : groupMenuChatHandler ( CNSocket * sock , CNPacketData * data ) {
if ( data - > size ! = sizeof ( sP_CL2FE_REQ_SEND_ALL_GROUP_MENUCHAT_MESSAGE ) )
return ; // malformed packet
sP_CL2FE_REQ_SEND_ALL_GROUP_MENUCHAT_MESSAGE * chat = ( sP_CL2FE_REQ_SEND_ALL_GROUP_MENUCHAT_MESSAGE * ) data - > buf ;
Player * plr = PlayerManager : : getPlayer ( sock ) ;
Player * otherPlr = PlayerManager : : getPlayerFromID ( plr - > iIDGroup ) ;
if ( otherPlr = = nullptr )
return ;
std : : string fullChat = ChatManager : : sanitizeText ( U16toU8 ( chat - > szFreeChat ) ) ;
std : : string logLine = " [GroupMenuChat] " + PlayerManager : : getPlayerName ( plr , true ) + " : " + fullChat ;
std : : cout < < logLine < < std : : endl ;
ChatManager : : dump . push_back ( logLine ) ;
// send to client
INITSTRUCT ( sP_FE2CL_REP_SEND_ALL_GROUP_MENUCHAT_MESSAGE_SUCC , resp ) ;
U8toU16 ( fullChat , ( char16_t * ) & resp . szFreeChat , sizeof ( resp . szFreeChat ) ) ;
resp . iSendPCID = plr - > iID ;
resp . iEmoteCode = chat - > iEmoteCode ;
GroupManager : : sendToGroup ( otherPlr , ( void * ) & resp , P_FE2CL_REP_SEND_ALL_GROUP_MENUCHAT_MESSAGE_SUCC , sizeof ( sP_FE2CL_REP_SEND_ALL_GROUP_MENUCHAT_MESSAGE_SUCC ) ) ;
}
2020-10-31 20:31:25 +00:00
// we only allow plain ascii, at least for now
2020-11-29 20:31:54 +00:00
std : : string ChatManager : : sanitizeText ( std : : string text , bool allowNewlines ) {
2020-10-31 20:31:25 +00:00
int i ;
2020-12-19 04:46:38 +00:00
const int BUFSIZE = 512 ;
char buf [ BUFSIZE ] ;
assert ( text . size ( ) < BUFSIZE ) ;
2020-10-31 20:31:25 +00:00
i = 0 ;
for ( char c : text ) {
2020-12-19 04:46:38 +00:00
if ( i > = BUFSIZE - 1 )
2020-10-31 20:31:25 +00:00
break ;
2020-11-29 20:31:54 +00:00
if ( ! allowNewlines & & c = = ' \n ' )
continue ;
2020-12-14 01:52:59 +00:00
if ( ( c > = ' ' & & c < = ' ~ ' ) | | c = = ' \n ' )
2020-10-31 20:31:25 +00:00
buf [ i + + ] = c ;
}
buf [ i ] = 0 ;
return std : : string ( buf ) ;
}