From fdaadab8d86f29bf4f27ba1e6dfe07269ccffaff Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 13 Nov 2017 21:48:09 +0300 Subject: IRC: code cleaning for some crazy moments related to #1010 --- protocols/IRCG/src/services.cpp | 172 ++++++++++++++++++++-------------------- protocols/IRCG/src/stdafx.h | 2 +- protocols/IRCG/src/tools.cpp | 132 ++++++++++++++---------------- 3 files changed, 146 insertions(+), 160 deletions(-) (limited to 'protocols/IRCG/src') diff --git a/protocols/IRCG/src/services.cpp b/protocols/IRCG/src/services.cpp index d7c9228f97..7c3443a98a 100644 --- a/protocols/IRCG/src/services.cpp +++ b/protocols/IRCG/src/services.cpp @@ -375,92 +375,96 @@ INT_PTR __cdecl CIrcProto::OnChangeNickMenuCommand(WPARAM, LPARAM) return 0; } -static void DoChatFormatting(wchar_t* pszText) -{ - wchar_t* p1 = pszText; - int iFG = -1; - int iRemoveChars; - wchar_t InsertThis[50]; - - while (*p1 != '\0') { - iRemoveChars = 0; - InsertThis[0] = 0; - - if (*p1 == '%') { - switch (p1[1]) { - case 'B': - case 'b': - mir_wstrcpy(InsertThis, L"\002"); - iRemoveChars = 2; - break; - case 'I': - case 'i': - mir_wstrcpy(InsertThis, L"\026"); - iRemoveChars = 2; - break; - case 'U': - case 'u': - mir_wstrcpy(InsertThis, L"\037"); - iRemoveChars = 2; - break; - case 'c': - { - mir_wstrcpy(InsertThis, L"\003"); - iRemoveChars = 2; - - wchar_t szTemp[3]; - mir_wstrncpy(szTemp, p1 + 2, 3); - iFG = _wtoi(szTemp); - } - break; - case 'C': - if (p1[2] == '%' && p1[3] == 'F') { - mir_wstrcpy(InsertThis, L"\x0399,99"); - iRemoveChars = 4; - } - else { - mir_wstrcpy(InsertThis, L"\x0399"); - iRemoveChars = 2; - } - iFG = -1; - break; - case 'f': - if (p1 - 3 >= pszText && p1[-3] == '\003') - mir_wstrcpy(InsertThis, L","); - else if (iFG >= 0) - mir_snwprintf(InsertThis, L"\x03%u,", iFG); - else - mir_wstrcpy(InsertThis, L"\x0399,"); +static int mapSrmm2irc[] = { 1, 2, 10, 6, 3, 7, 5, 14, 15, 12, 11, 13, 9, 8, 4, 0 }; - iRemoveChars = 2; - break; +static wchar_t* DoPrintColor(wchar_t *pDest, int iFG, int iBG) +{ + *pDest = 3; + if (iFG == -1) { + if (iBG == -1) + pDest[1] = 0; + else + mir_snwprintf(pDest+1, 49, L",%d", iBG); + } + else { + if (iBG == -1) + mir_snwprintf(pDest+1, 49, L"%d", iFG); + else + mir_snwprintf(pDest+1, 49, L"%d,%d", iFG, iBG); + } - case 'F': - if (iFG >= 0) - mir_snwprintf(InsertThis, L"\x03%u,99", iFG); - else - mir_wstrcpy(InsertThis, L"\x0399,99"); - iRemoveChars = 2; - break; + return pDest; +} - case '%': - mir_wstrcpy(InsertThis, L"%"); - iRemoveChars = 2; - break; +static void DoChatFormatting(CMStringW &wszText) +{ + int iFG = -1, iBG = -1; + wchar_t InsertThis[50]; - default: - iRemoveChars = 2; - break; + for (int i = 0; i < wszText.GetLength(); i++) { + if (wszText[i] != '%') + continue; + + switch (wszText[i + 1]) { + case 'B': + case 'b': + wszText.Delete(i, 2); + wszText.Insert(i, L"\002"); + break; + case 'I': + case 'i': + wszText.Delete(i, 2); + wszText.Insert(i, L"\026"); + break; + case 'U': + case 'u': + wszText.Delete(i, 2); + wszText.Insert(i, L"\037"); + break; + + case 'c': + wszText.Delete(i, 2); + iFG = _wtoi(wszText.GetString() + i); + wszText.Delete(i, (iFG < 10) ? 1 : 2); + + iFG = (iFG < 0 || iFG >= _countof(mapSrmm2irc)) ? -1 : mapSrmm2irc[iFG]; + wszText.Insert(i, DoPrintColor(InsertThis, iFG, iBG)); + break; + + case 'C': + if (wszText[i + 2] == '%' && wszText[i + 3] == 'F') { + wszText.Delete(i, 4); + iBG = -1; } - - memmove(p1 + mir_wstrlen(InsertThis), p1 + iRemoveChars, sizeof(wchar_t)*(mir_wstrlen(p1) - iRemoveChars + 1)); - memcpy(p1, InsertThis, sizeof(wchar_t)*mir_wstrlen(InsertThis)); - if (iRemoveChars || mir_wstrlen(InsertThis)) - p1 += mir_wstrlen(InsertThis); - else - p1++; + else wszText.Delete(i, 2); + iFG = -1; + wszText.Insert(i, DoPrintColor(InsertThis, iFG, iBG)); + break; + + case 'f': + wszText.Delete(i, 2); + iBG = _wtoi(wszText.GetString() + i); + wszText.Delete(i, (iBG < 10) ? 1 : 2); + + iBG = (iBG < 0 || iBG >= _countof(mapSrmm2irc)) ? -1 : mapSrmm2irc[iBG]; + wszText.Insert(i, DoPrintColor(InsertThis, iFG, iBG)); + break; + + case 'F': + wszText.Delete(i, 2); + iBG = -1; + wszText.Insert(i, DoPrintColor(InsertThis, iFG, iBG)); + break; + + case '%': + wszText.Delete(i, 1); + i++; + break; + + default: + wszText.Delete(i, 2); + break; } - else p1++; } } @@ -486,11 +490,9 @@ int __cdecl CIrcProto::GCEventHook(WPARAM, LPARAM lParam) case GC_USER_MESSAGE: if (gch && gch->ptszText && *gch->ptszText) { - wchar_t* pszText = new wchar_t[mir_wstrlen(gch->ptszText) + 1000]; - mir_wstrcpy(pszText, gch->ptszText); - DoChatFormatting(pszText); - PostIrcMessageWnd(p1, NULL, pszText); - delete[]pszText; + CMStringW wszText(gch->ptszText); + DoChatFormatting(wszText); + PostIrcMessageWnd(p1, NULL, wszText); } break; diff --git a/protocols/IRCG/src/stdafx.h b/protocols/IRCG/src/stdafx.h index ba5cfca32b..a96192f873 100644 --- a/protocols/IRCG/src/stdafx.h +++ b/protocols/IRCG/src/stdafx.h @@ -461,7 +461,7 @@ struct CIrcProto : public PROTO void SetChatTimer(UINT_PTR &nIDEvent, UINT uElapse, TIMERPROC lpTimerFunc); void ClearUserhostReasons(int type); - void DoUserhostWithReason(int type, CMStringW reason, bool bSendCommand, CMStringW userhostparams, ...); + void DoUserhostWithReason(int type, CMStringW reason, bool bSendCommand, const wchar_t *userhostparams, ...); CMStringW GetNextUserhostReason(int type); CMStringW PeekAtReasons(int type); diff --git a/protocols/IRCG/src/tools.cpp b/protocols/IRCG/src/tools.cpp index 78434b4cf8..8c71220aae 100644 --- a/protocols/IRCG/src/tools.cpp +++ b/protocols/IRCG/src/tools.cpp @@ -231,30 +231,49 @@ wchar_t* __stdcall my_strstri(const wchar_t* s1, const wchar_t* s2) return nullptr; } -wchar_t* __stdcall DoColorCodes(const wchar_t* text, bool bStrip, bool bReplacePercent) +static int mapIrc2srmm[] = { 15, 0, 1, 4, 14, 6, 3, 5, 13, 12, 2, 10, 9, 11, 7, 8 }; + +static const wchar_t* DoEnterNumber(const wchar_t *text, int &res) +{ + if (*text >= '0' && *text <= '9') { + res = text[0] - '0'; + text++; + + if (*text >= '0' && *text <= '9') { + res *= 10; + res += text[0] - '0'; + text++; + } + + res = (res >= 0 && res < _countof(mapIrc2srmm)) ? mapIrc2srmm[res] : -1; + } + else res = -1; + + return text; +} + +wchar_t* __stdcall DoColorCodes(const wchar_t *text, bool bStrip, bool bReplacePercent) { static wchar_t szTemp[4000]; szTemp[0] = '\0'; wchar_t* p = szTemp; bool bBold = false; bool bUnderline = false; bool bItalics = false; + int iFG = -1, iBG = -1; if (!text) return szTemp; while (*text != '\0') { - int iFG = -1; - int iBG = -1; - switch (*text) { - case '%': //escape + case '%': // escape *p++ = '%'; if (bReplacePercent) *p++ = '%'; text++; break; - case 2: //bold + case 2: // bold if (!bStrip) { *p++ = '%'; *p++ = bBold ? 'B' : 'b'; @@ -263,17 +282,16 @@ wchar_t* __stdcall DoColorCodes(const wchar_t* text, bool bStrip, bool bReplaceP text++; break; - case 15: //reset + case 15: // reset if (!bStrip) { *p++ = '%'; *p++ = 'r'; } - bUnderline = false; - bBold = false; + bUnderline = bItalics = bBold = false; text++; break; - case 22: //italics + case 22: // italics if (!bStrip) { *p++ = '%'; *p++ = bItalics ? 'I' : 'i'; @@ -291,77 +309,41 @@ wchar_t* __stdcall DoColorCodes(const wchar_t* text, bool bStrip, bool bReplaceP text++; break; - case 3: //colors - text++; + case 3: // colors + int iOldBG, iOldFG; + iOldBG = iBG, iOldFG = iFG; - // do this if the colors should be reset to default - if (*text <= 47 || *text >= 58 || *text == '\0') { - if (!bStrip) { - *p++ = '%'; - *p++ = 'C'; - *p++ = '%'; - *p++ = 'F'; - } - break; - } - else { // some colors should be set... need to find out who - wchar_t buf[3]; - - // fix foreground index - if (text[1] > 47 && text[1] < 58 && text[1] != '\0') - mir_wstrncpy(buf, text, 3); - else - mir_wstrncpy(buf, text, 2); - text += mir_wstrlen(buf); - iFG = _wtoi(buf); - - // fix background color - if (*text == ',' && text[1] > 47 && text[1] < 58 && text[1] != '\0') { - text++; - - if (text[1] > 47 && text[1] < 58 && text[1] != '\0') - mir_wstrncpy(buf, text, 3); - else - mir_wstrncpy(buf, text, 2); - text += mir_wstrlen(buf); - iBG = _wtoi(buf); - } - } - - if (iFG >= 0 && iFG != 99) - while (iFG > 15) - iFG -= 16; - if (iBG >= 0 && iBG != 99) - while (iBG > 15) - iBG -= 16; + text = DoEnterNumber(text + 1, iFG); + if (*text == ',') + text = DoEnterNumber(text + 1, iBG); + else + iBG = -1; // create tag for chat.dll if (!bStrip) { wchar_t buf[10]; - if (iFG >= 0 && iFG != 99) { + if (iFG != iOldFG) { *p++ = '%'; - *p++ = 'c'; - - mir_snwprintf(buf, L"%02u", iFG); - for (int i = 0; i < 2; i++) - *p++ = buf[i]; - } - else if (iFG == 99) { - *p++ = '%'; - *p++ = 'C'; + if (iFG == -1) + *p++ = 'C'; + else { + *p++ = 'c'; + mir_snwprintf(buf, L"%02u", iFG); + *p++ = buf[0]; + *p++ = buf[1]; + } } - if (iBG >= 0 && iBG != 99) { - *p++ = '%'; - *p++ = 'f'; - - mir_snwprintf(buf, L"%02u", iBG); - for (int i = 0; i < 2; i++) - *p++ = buf[i]; - } - else if (iBG == 99) { + if (iBG != iOldBG) { *p++ = '%'; - *p++ = 'F'; + if (iBG == -1) + *p++ = 'F'; + else { + *p++ = 'f'; + mir_snwprintf(buf, L"%02u", iBG); + *p++ = buf[0]; + *p++ = buf[1]; + } } } break; @@ -601,7 +583,7 @@ void CIrcProto::FindLocalIP(HNETLIBCONN hConn) // inspiration from jabber } } -void CIrcProto::DoUserhostWithReason(int type, CMStringW reason, bool bSendCommand, CMStringW userhostparams, ...) +void CIrcProto::DoUserhostWithReason(int type, CMStringW reason, bool bSendCommand, const wchar_t *userhostparams, ...) { wchar_t temp[4096]; CMStringW S = L""; @@ -616,10 +598,12 @@ void CIrcProto::DoUserhostWithReason(int type, CMStringW reason, bool bSendComma S = L"USERHOST"; break; } + S.AppendChar(' '); + S.Append(userhostparams); va_list ap; va_start(ap, userhostparams); - mir_vsnwprintf(temp, _countof(temp), (S + L" " + userhostparams).c_str(), ap); + mir_vsnwprintf(temp, _countof(temp), S.c_str(), ap); va_end(ap); // Add reason -- cgit v1.2.3