1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/*
Copyright (C) 2012-25 Miranda NG team (https://miranda-ng.org)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
void EncodeBbcodes(SESSION_INFO *si, CMStringW &wszText)
{
int idx = wszText.Find(':');
if (idx != -1) {
CMStringW wszNick(wszText.Left(idx));
for (auto &it : si->getUserList()) {
if (wszNick == it->pszNick) {
wszText.Delete(0, idx + 1);
CMStringW wszReplace(FORMAT, L"[mention=%lld]@%s[/mention]", SteamIdToAccountId(_wtoi64(it->pszUID)), it->pszNick);
wszText = wszReplace + wszText;
break;
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
struct BBCode
{
CMStringA szTag, szValue, szBody;
std::map<CMStringA, CMStringA> attrs;
};
int parseBbcode(const char *p, BBCode &ret)
{
auto *pSave = p;
while (isalpha(*p))
ret.szTag.AppendChar(*p++);
// [tag=value][/tag]
if (*p == '=') {
while (*p && *p != ']')
ret.szValue.AppendChar(*p++);
if (*p != ']')
return -1;
}
// [tag attr=value][/tag]
else if (*p == ' ') {
p++;
while (*p != ']') {
CMStringA szName, szValue;
while (*p && *p != '=')
szName.AppendChar(*p++);
if (*p++ != '=')
return -1;
if (*p == '\"') {
auto *p1 = strchr(++p, '\"');
if (p1 == nullptr)
return -1;
p++;
szValue.Append(p, p1 - p);
p = p1 + 1;
}
else {
while (*p && *p != ' ' && *p != ']')
szValue.AppendChar(*p++);
if (*p == 0)
return -1;
}
if (*p == ' ')
p++;
}
}
// [tag][/tag]
else if (*p != ']')
return -1;
p++; // skip first ]
CMStringA szClose = "[/" + ret.szTag + "]";
auto *pEnd = strstr(p, szClose);
if (pEnd == nullptr)
return -1;
if (pEnd != p)
ret.szValue.Append(p, pEnd - p);
return pEnd + szClose.GetLength() - pSave;
}
void DecodeBbcodes(SESSION_INFO *si, CMStringA &szText)
{
for (int idx = szText.Find('['); idx != -1; idx = szText.Find('[', idx + 1)) {
BBCode code;
int iLength = parseBbcode(szText.c_str() + idx + 1, code) + 1;
if (iLength == 0)
continue;
bool bPlaceFirst = false;
CMStringA szReplace;
if (code.szTag == "emoticon")
szReplace.Format(":%s:", code.szValue.c_str());
else if (code.szTag == "sticker")
szReplace.Format(":%s:", code.attrs["type"].c_str());
else if (code.szTag == "mention") {
CMStringW wszId(FORMAT, L"%lld", AccountIdToSteamId(_atoi64(code.szValue)));
if (auto *pUser = g_chatApi.UM_FindUser(si, wszId)) {
szReplace.Format("%s:", T2Utf(pUser->pszNick).get());
bPlaceFirst = true;
}
}
else if (code.szTag == "lobbyinvite") {
szReplace = TranslateU("You were invited to play a game");
}
else continue;
szText.Delete(idx, iLength);
if (!szReplace.IsEmpty()) {
if (bPlaceFirst)
szText = szReplace + szText;
else
szText.Insert(idx, szReplace);
}
}
}
|