This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gg_tools.cpp
317 lines (301 loc) · 10.3 KB
/
gg_tools.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "stdafx.h"
#include "gg_main.h"
using namespace Konnekt::GG;
using Stamina::inttostr;
using Stamina::str_tr;
using Stamina::RegEx;
using Stamina::SXML;
using Stamina::chtoint;
void GG::gg_debug_lfunc(int level, const char *format, ...)
{
va_list ap;
char buff[502];
if ((gg_debug_level & level)) {
va_start(ap, format);
_vsnprintf(buff , 500 , (string(" _GG| ") + format).c_str() , ap);
buff[500]=0;
IMessage(IMC_LOG , 0,0,(int)buff,0);
// vprintf(format, ap);
va_end(ap);
}
}
int GG::check(bool conn , bool session , bool login , bool warn) {
bool err = 0;
/* TODO : Rozsadne anulowanie polaczen... */
/* if (conn && !session && gg_connect(0,0,0) && !sess) {
err = 1;
if (warn) ICMessage(IMI_ERROR , (int)"Poczekaj .. Po³¹czenie ju¿ jest zajête!");
} else
*/
if (conn && !ICMessage(IMC_CONNECTED))
{err = 1;
if (warn) ICMessage(IMI_ERROR , (int)"Aby dzia³aæ na serwerze GG musisz byæ po³¹czony z internetem!\nJe¿eli jesteœ po³¹czony sprawdŸ czy prawid³owo skonfigurowa³eœ po³¹czenie w konfiguracji!" , MB_TASKMODAL|MB_OK);
}
else if (session && !sess)
{err = 1;
if (warn) ICMessage(IMI_ERROR , (int)"Musisz byæ po³¹czony z serwerem GG!" , MB_TASKMODAL);
}
else if (login && !GETINT(CFG_GG_LOGIN))
{err=1;
if (warn) ICMessage(IMI_ERROR , (int)"Musisz ustawiæ login i has³o konta GG!\nJe¿eli nie masz konta , za³ó¿ je przyciskiem w konfiguracji.",MB_TASKMODAL|MB_OK);
}
if (err)
{Ctrl->setError(IMERROR_NORESULT);return 0;}
else
return 1;
}
void GG::getAccount(int & login , CStdString & pass) {
login = GETINT(CFG_GG_LOGIN);
pass = GETSTR(CFG_GG_PASS);
if (!login || pass.empty()) {
sDIALOG_access sda;
sda.flag = DFLAG_SAVE;
sda.title = "Has³o do konta GG";
sda.info = "Aby wykonaæ wybran¹ operacjê, musisz podaæ has³o do swojego konta GG";
sda.save = false;
sda.login = (char*) GETSTR(CFG_GG_LOGIN);
sda.pass = "";
if (!ICMessage(IMI_DLGLOGIN , (int)&sda)) return;
pass = sda.pass;
login = atoi(sda.login);
if (sda.save) {
SETINT(CFG_GG_LOGIN , login);
SETSTR(CFG_GG_PASS , pass);
}
}
}
void GG::quickEvent(int Uid , const char * body , const char * ext , int flag) {
cMessage m;
m.net = NET_GG;
m.type = MT_QUICKEVENT;
std::string uid = inttostr(Uid);
m.fromUid = (char *)uid.c_str();
m.toUid = "";
m.body = (char *)body;
m.ext = (char *)ext;
m.flag = MF_HANDLEDBYUI;
m.action = NOACTION;
m.notify = 0;
ICMessage(IMC_NEWMESSAGE,(int)&m,0);
}
int GG::userType(int id) {
int cntStatus = GETCNTI(id , CNT_STATUS);
return (cntStatus & ST_IGNORED)?GG_USER_BLOCKED :(cntStatus & ST_HIDEMYSTATUS)? GG_USER_OFFLINE : GG_USER_NORMAL;
}
bool GG::getToken(const string & title , const string & info , string & tokenid , string & tokenval) {
gg_http * http = gg_token(0);
if (!http || http->state != GG_STATE_DONE) {
ICMessage(IMI_ERROR , (int)"Wyst¹pi³ b³¹d podczas pobierania tokenu. SprawdŸ po³¹czenie i spróbuj ponownie.");
return false;
}
struct gg_token * token = (struct gg_token*) http->data;
tokenid = token->tokenid;
ICMessage(IMC_RESTORECURDIR);
char filename [MAX_PATH];
sprintf(filename , (string((char*)ICMessage(IMC_TEMPDIR)) + "gg_token%08x.jpg").c_str() , rand());
FILE * f = fopen(filename , "wb");
if (!f || !fwrite(http->body , http->body_size , 1 , f)) {
IMDEBUG(DBG_ERROR , "! Could not create/write temp file for token fn=%s size=%d" , filename , http->body_size);
if (f)
fclose(f);
return false;
}
fclose(f);
sDIALOG_token dt;
dt.title = title.c_str();
dt.info = info.c_str();
string URL = "file://";
URL+= filename;
dt.imageURL = URL.c_str();
ICMessage(IMI_DLGTOKEN , (int)&dt);
tokenval = dt.token;
gg_token_free(http);
return !tokenval.empty();
}
CStdString GG::msgToHtml(CStdString msg , void * formats , int formats_length) {
/* Zamiast znaczników wstawia:
< - 1
> - 2
" - 3
*/
str_tr((char*)msg.c_str() , "\1\2\3" , " ");
// Przegl¹damy listê i modyfikujemy...
void * formats_end = (char*)formats + formats_length;
CStdString msg2 = "";
struct cOpened {
unsigned int bold :1;
unsigned int italic : 1;
unsigned int under : 1;
unsigned int color: 1;
void finish(CStdString & txt , int type) {
if (color) {txt+="\1/font\2"; color = 0;}
if (!(type & GG_FONT_UNDERLINE) && under) {txt+="\1/u\2"; under = 0;}
if (!(type & GG_FONT_ITALIC) && italic) {txt+="\1/i\2"; italic = 0;}
if (!(type & GG_FONT_BOLD) && bold) {txt+="\1/b\2";bold = 0;}
}
} opened;
opened.bold = opened.italic = opened.under = opened.color = 0;
size_t pos = 0;
while (formats < formats_end) {
gg_msg_richtext_format * rf = (gg_msg_richtext_format*)formats;
if (rf->position >= msg.size())
break; // b³¹d
msg2+= msg.substr(pos , rf->position - pos);
pos = rf->position;
opened.finish(msg2 , rf->font);
if ((rf->font & GG_FONT_BOLD) && !opened.bold) {
msg2+="\1b\2";
opened.bold=1;
}
if ((rf->font & GG_FONT_ITALIC) && !opened.italic) {
msg2+="\1i\2";
opened.italic=1;
}
if ((rf->font & GG_FONT_UNDERLINE) && !opened.under) {
msg2+="\1u\2";
opened.under=1;
}
formats = rf+1;
if (rf->font & GG_FONT_COLOR) {
gg_msg_richtext_color * rc = (gg_msg_richtext_color*) formats;
if (rc->red || rc->green || rc->blue) {
opened.color=1;
msg2+="\1font color=\3#";
msg2+=inttostr(rc->red , 16 , 2 , true);
msg2+=inttostr(rc->green , 16 , 2 , true);
msg2+=inttostr(rc->blue , 16 , 2 , true);
msg2+="\3\2";
} else opened.color = 0;
formats = rc+1;
}
}
msg2 += msg.substr(pos);
opened.finish(msg2 , 0);
msg = msg2;
msg2.clear();
// Koñcowy efekt "enkodujemy"
RegEx pr;
msg = pr.replace("/[^ a-z0-9\1\2\3\\!\\@\\#\\$\\%\\*\\(\\)\\-_=+\\.\\,\\;':\\\\[\\]\\{\\}\\/\\?¹æê³ñ󜟿¥ÆÊ£ÑÓŒ¯]/i" , Stamina::encodeCallback , msg);
msg = pr.replace("/\\n/" , "<br/>" , msg);
// Zamieniamy znaki kontrolne w znaczniki HTML
str_tr((char*)msg.c_str() , "\1\2\3" , "<>\"");
return msg;
};
struct FormatState {
int bold , under, italic , color;
char rgb [3];
FormatState() {bold = under = italic = color = 0;}
};
CStdString GG::htmlToMsg(CStdString msgIn , void * formats , int & length) {
int max_len = length;
length = 0;
CStdString msg;
SXML XML;
RegEx preg;
msgIn = preg.replace("#\\r|\\n#" , "" , msgIn);
msgIn = preg.replace("#<br/?>#i" , "\n" , msgIn.c_str());
XML.loadSource(msgIn);
SXML::NodeWalkInfo ni;
size_t last = 0;
void * formats_start = formats;
gg_msg_richtext * rt = (gg_msg_richtext*) formats;
formats = rt+1;
void * formats_last = formats;
memset(formats , 0 , sizeof(gg_msg_richtext_format));
/*
bleeeee<b>bold<b><font color="#FF0000">gnie¿<u>d¿ony</u></font></b>i <i>jesz</i>cze</b>koniec
*/
FormatState state;
stack <FormatState> spanStack;
while (length < max_len - 20 && XML.nodeWalk(ni)) {
XML.pos.start = XML.pos.end;
XML.pos.start_end = XML.pos.end_end;
msg += Stamina::decodeEntities(msgIn.substr(last , ni.start - last));
last = ni.end;
CStdString token = ni.path.substr(ni.path.find_last_of('/')+1);
token.MakeLower();
int oper = (ni.state == SXML::NodeWalkInfo::opened)? 1 : -1;
if (token == "b" || token == "strong") {
state.bold+=oper;
} else if (token == "i") {
state.italic+=oper;
} else if (token == "u") {
state.under+=oper;
} else if (token == "font") {
// Kolor musimy "zamkn¹æ"
if (oper > 0) state.color++;
int c = chtoint(XML.getAttrib("color").c_str() , -1);
state.rgb[0] = (c & 0xFF0000) >> 16;
state.rgb[1] = (c & 0xFF00) >> 8;
state.rgb[2] = (c & 0xFF);
} else if (token == "span") {
if (oper > 0) {
spanStack.push(state);
using Stamina::RegEx;
static RegEx::oCompiled rcWeight(new RegEx::Compiled("/font-weight:bold/i"));
//static RegEx::oCompiled rcSize = new RegEx::Compiled("/font-size:(\\d+)/i");
static RegEx::oCompiled rcColor(new RegEx::Compiled("/color:([a-z]+|#[0-9A-F]+)/i"));
static RegEx::oCompiled rcItalic(new RegEx::Compiled("/font-style:italic/i"));
static RegEx::oCompiled rcUnderline(new RegEx::Compiled("/text-decoration:underline/i"));
CStdString style = XML.getAttrib("style");
if (RegEx::doMatch(rcWeight, style)) {
state.bold += oper;
}
if (RegEx::doMatch(rcItalic, style)) {
state.italic += oper;
}
if (RegEx::doMatch(rcUnderline, style)) {
state.under += oper;
}
CStdString colorStr = RegEx::doGet(rcColor, style, 1);
if (!colorStr.empty()) {
int color = -1;
if (colorStr[0] == '#') {
color = Stamina::chtoint(colorStr , -1);
} else if (colorStr == "red") {
color = 0xFF0000;
} else if (colorStr == "blue") {
color = 0x0000FF;
} else if (colorStr == "green") {
color = 0x00FF00;
}
if (color > 0) {
state.color++;
state.rgb[0] = (color & 0xFF0000) >> 16;
state.rgb[1] = (color & 0xFF00) >> 8;
state.rgb[2] = (color & 0xFF);
}
}
} else {
if (spanStack.size()) {
state = spanStack.top();
spanStack.pop();
}
}
}
gg_msg_richtext_format * rf = (gg_msg_richtext_format*)formats_last;
// Je¿eli stoimy w tym samym miejscu, wypada po³¹czyæ si³y...
if (rf->position == msg.size())
formats = formats_last;
rf = (gg_msg_richtext_format*)formats;
rf->position = msg.size();
rf->font = (state.bold > 0?GG_FONT_BOLD:0) | (state.italic > 0?GG_FONT_ITALIC:0) | (state.under > 0?GG_FONT_UNDERLINE:0) | (state.color > 0?GG_FONT_COLOR:0);
formats_last = formats;
formats = rf+1;
if (state.color) {
gg_msg_richtext_color * rc = (gg_msg_richtext_color*) formats;
rc->red = state.rgb[0];
rc->green = state.rgb[1];
rc->blue = state.rgb[2];
formats = rc+1;
}
length = (char*)formats - (char*)formats_start;
// "Zamykamy" kolor
if (token == "font" && oper < 0)
state.color--;
}
msg += Stamina::decodeEntities(msgIn.substr(ni.end));
rt->flag = length>sizeof(gg_msg_richtext)?2:0;
rt->length = length - sizeof(gg_msg_richtext);
return msg;
};