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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
// -----------------------------------------------------------------------------
// ICQ plugin for Miranda NG
// -----------------------------------------------------------------------------
// Copyright © 2018-21 Miranda NG team
//
// 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; either version 2
// of the License, or (at your option) any later version.
//
// 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// -----------------------------------------------------------------------------
#include "stdafx.h"
void CIcqProto::InitContactCache()
{
mir_cslock l(m_csCache);
for (auto &it : AccContacts()) {
if (isChatRoom(it))
continue;
// that was previously an ICQ contact
ptrW wszUin(GetUIN(it));
if (wszUin != nullptr) {
delSetting(it, "UIN");
setWString(it, DB_KEY_ID, wszUin);
}
// that was previously a MRA contact
else {
CMStringW wszEmail(getMStringW(it, "e-mail"));
if (!wszEmail.IsEmpty()) {
delSetting(it, "e-mail");
setWString(it, DB_KEY_ID, wszEmail);
}
}
auto *pCache = new IcqCacheItem(GetUserId(it), it);
pCache->m_iProcessedMsgId = getId(it, DB_KEY_LASTMSGID);
m_arCache.insert(pCache);
}
}
IcqCacheItem* CIcqProto::FindContactByUIN(const CMStringW &wszId)
{
IcqCacheItem tmp(wszId, -1);
mir_cslock l(m_csCache);
return m_arCache.find(&tmp);
}
wchar_t* CIcqProto::GetUIN(MCONTACT hContact)
{
DBVARIANT dbv = {};
if (!db_get(hContact, m_szModuleName, "UIN", &dbv)) {
switch (dbv.type) {
case DBVT_DWORD:
wchar_t buf[40], *ret;
_itow_s(dbv.dVal, buf, 10);
return mir_wstrdup(buf);
case DBVT_ASCIIZ:
ret = mir_a2u(dbv.pszVal);
db_free(&dbv);
return ret;
case DBVT_UTF8:
ret = mir_utf8decodeW(dbv.pszVal);
db_free(&dbv);
return ret;
case DBVT_WCHAR:
return dbv.pwszVal;
}
db_free(&dbv);
}
return nullptr;
}
MCONTACT CIcqProto::CreateContact(const CMStringW &wszId, bool bTemporary)
{
auto *pCache = FindContactByUIN(wszId);
if (pCache != nullptr)
return pCache->m_hContact;
MCONTACT hContact = db_add_contact();
setWString(hContact, DB_KEY_ID, wszId);
Proto_AddToContact(hContact, m_szModuleName);
RetrieveUserInfo(hContact);
if (bTemporary)
Contact_RemoveFromList(hContact);
return hContact;
}
/////////////////////////////////////////////////////////////////////////////////////////
void CIcqProto::CalcHash(AsyncHttpRequest *pReq)
{
CMStringA hashData(FORMAT, "%s&%s&%s",
pReq->requestType == REQUEST_POST ? "POST" : "GET",
mir_urlEncode(pReq->m_szUrl).c_str(), mir_urlEncode(pReq->m_szParam).c_str());
unsigned int len;
uint8_t hashOut[MIR_SHA256_HASH_SIZE];
HMAC(EVP_sha256(), m_szSessionKey, m_szSessionKey.GetLength(), (uint8_t*)hashData.c_str(), hashData.GetLength(), hashOut, &len);
pReq << CHAR_PARAM("sig_sha256", ptrA(mir_base64_encode(hashOut, sizeof(hashOut))));
}
/////////////////////////////////////////////////////////////////////////////////////////
void CIcqProto::Json2int(MCONTACT hContact, const JSONNode &node, const char *szJson, const char *szSetting, bool bIsPartial)
{
const JSONNode &var = node[szJson];
if (var)
setDword(hContact, szSetting, var.as_int());
else if (!bIsPartial)
delSetting(hContact, szSetting);
}
void CIcqProto::Json2string(MCONTACT hContact, const JSONNode &node, const char *szJson, const char *szSetting, bool bIsPartial)
{
const JSONNode &var = node[szJson];
if (var) {
CMStringW wszStr(var.as_mstring());
if (wszStr == L"[deleted]") {
setByte(hContact, "IcqDeleted", 1);
Contact_PutOnList(hContact);
}
else setWString(hContact, szSetting, wszStr);
}
else if (!bIsPartial)
delSetting(hContact, szSetting);
}
/////////////////////////////////////////////////////////////////////////////////////////
// Avatars
void CIcqProto::GetAvatarFileName(MCONTACT hContact, wchar_t* pszDest, size_t cbLen)
{
int tPathLen = mir_snwprintf(pszDest, cbLen, L"%s\\%S", VARSW(L"%miranda_avatarcache%").get(), m_szModuleName);
CreateDirectoryTreeW(pszDest);
pszDest[tPathLen++] = '\\';
CMStringW wszFileName(getMStringW(hContact, "IconId"));
const wchar_t* szFileType = ProtoGetAvatarExtension(getByte(hContact, "AvatarType", PA_FORMAT_PNG));
mir_snwprintf(pszDest + tPathLen, MAX_PATH - tPathLen, L"%s%s", wszFileName.c_str(), szFileType);
}
INT_PTR __cdecl CIcqProto::GetAvatar(WPARAM wParam, LPARAM lParam)
{
wchar_t *buf = (wchar_t*)wParam;
int size = (int)lParam;
if (buf == nullptr || size <= 0)
return -1;
GetAvatarFileName(0, buf, size);
return 0;
}
INT_PTR __cdecl CIcqProto::GetAvatarCaps(WPARAM wParam, LPARAM lParam)
{
switch (wParam) {
case AF_MAXSIZE:
((POINT*)lParam)->x = -1;
((POINT*)lParam)->y = -1;
return 0;
case AF_FORMATSUPPORTED: // nobody
return 1;
case AF_DELAYAFTERFAIL:
return 10 * 60 * 1000;
case AF_ENABLED:
case AF_FETCHIFPROTONOTVISIBLE:
case AF_FETCHIFCONTACTOFFLINE:
return 1;
}
return 0;
}
INT_PTR __cdecl CIcqProto::GetAvatarInfo(WPARAM, LPARAM lParam)
{
PROTO_AVATAR_INFORMATION* pai = (PROTO_AVATAR_INFORMATION*)lParam;
ptrW szIconId(getWStringA(pai->hContact, "IconId"));
if (szIconId == nullptr) {
debugLogA("No avatar");
return GAIR_NOAVATAR;
}
GetAvatarFileName(pai->hContact, pai->filename, _countof(pai->filename));
pai->format = getByte(pai->hContact, "AvatarType", 0);
if (::_waccess(pai->filename, 0) == 0)
return GAIR_SUCCESS;
debugLogA("No avatar");
return GAIR_NOAVATAR;
}
INT_PTR __cdecl CIcqProto::SetAvatar(WPARAM, LPARAM lParam)
{
wchar_t* pwszFileName = (wchar_t*)lParam;
wchar_t wszOldName[MAX_PATH];
GetAvatarFileName(0, wszOldName, _countof(wszOldName));
_wremove(wszOldName);
auto *pReq = new AsyncHttpRequest(CONN_MAIN, REQUEST_POST, ICQ_API_SERVER "/expressions/upload");
pReq->m_szUrl.AppendFormat("?f=json&aimsid=%s&r=%s&type=largeBuddyIcon", mir_urlEncode(m_aimsid.c_str()).c_str(), pReq->m_reqId);
if (pwszFileName == nullptr)
delSetting("AvatarHash");
else {
int fileId = _wopen(pwszFileName, _O_RDONLY | _O_BINARY, _S_IREAD);
if (fileId < 0) {
delete pReq;
return 1;
}
unsigned dwSize = (unsigned)_filelengthi64(fileId);
char* pData = (char*)mir_alloc(dwSize);
if (pData == nullptr) {
_close(fileId);
delete pReq;
return 2;
}
_read(fileId, pData, dwSize);
_close(fileId);
pReq->pData = pData;
pReq->dataLength = dwSize;
int iAvatarType = ProtoGetBufferFormat(pData);
if (iAvatarType == PA_FORMAT_UNKNOWN) {
delete pReq;
delete pData;
return 3;
}
pReq->AddHeader("Content-Type", ProtoGetAvatarMimeType(iAvatarType));
}
Push(pReq);
return 0; // TODO
}
/////////////////////////////////////////////////////////////////////////////////////////
CMStringW CIcqProto::GetUserId(MCONTACT hContact)
{
if (isChatRoom(hContact))
return getMStringW(hContact, "ChatRoomID");
return getMStringW(hContact, DB_KEY_ID);
}
bool IsChat(const CMStringW &aimid)
{
return aimid.Right(11) == "@chat.agent";
}
int CIcqProto::StatusFromPresence(const JSONNode &presence, MCONTACT hContact)
{
CMStringW wszStatus = presence["state"].as_mstring();
int iStatus;
if (wszStatus == L"online")
iStatus = ID_STATUS_ONLINE;
if (wszStatus == L"offline")
iStatus = ID_STATUS_OFFLINE;
else if (wszStatus == L"n/a")
iStatus = ID_STATUS_NA;
else if (wszStatus == L"away")
iStatus = ID_STATUS_AWAY;
else if (wszStatus == L"occupied")
iStatus = ID_STATUS_OCCUPIED;
else if (wszStatus == L"dnd")
iStatus = ID_STATUS_DND;
else
iStatus = -1;
int iLastSeen = presence["lastseen"].as_int();
if (iLastSeen != 0)
setDword(hContact, DB_KEY_LASTSEEN, iLastSeen);
else if (getDword(hContact, DB_KEY_ONLINETS))
iStatus = ID_STATUS_ONLINE;
return iStatus;
}
/////////////////////////////////////////////////////////////////////////////////////////
__int64 CIcqProto::getId(MCONTACT hContact, const char *szSetting)
{
DBVARIANT dbv;
dbv.type = DBVT_BLOB;
if (db_get(hContact, m_szModuleName, szSetting, &dbv))
return 0;
__int64 result = (dbv.cpbVal == sizeof(__int64)) ? *(__int64*)dbv.pbVal : 0;
db_free(&dbv);
return result;
}
void CIcqProto::setId(MCONTACT hContact, const char *szSetting, __int64 iValue)
{
__int64 oldVal = getId(hContact, szSetting);
if (oldVal != iValue)
db_set_blob(hContact, m_szModuleName, szSetting, &iValue, sizeof(iValue));
}
/////////////////////////////////////////////////////////////////////////////////////////
char* time2text(time_t time)
{
if (time == 0)
return "";
tm *local = localtime(&time);
if (local) {
if (char *str = asctime(local)) {
str[24] = '\0'; // remove new line
return str;
}
}
return "<invalid>";
}
/////////////////////////////////////////////////////////////////////////////////////////
static LRESULT CALLBACK PopupDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_CONTEXTMENU:
PUDeletePopup(hWnd);
break;
case WM_COMMAND:
CIcqProto *ppro = (CIcqProto*)PUGetPluginData(hWnd);
CallProtoService(ppro->m_szModuleName, PS_GOTO_INBOX);
PUDeletePopup(hWnd);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
void CIcqProto::EmailNotification(const wchar_t *pwszText)
{
POPUPDATAW Popup = {};
Popup.lchIcon = g_plugin.getIcon(IDI_INBOX);
wcsncpy_s(Popup.lpwzText, pwszText, _TRUNCATE);
wcsncpy_s(Popup.lpwzContactName, m_tszUserName, _TRUNCATE);
Popup.iSeconds = 20;
Popup.PluginData = this;
Popup.PluginWindowProc = PopupDlgProc;
PUAddPopupW(&Popup);
if (m_bUseTrayIcon) {
char szServiceFunction[MAX_PATH];
if (m_bLaunchMailbox)
mir_snprintf(szServiceFunction, "%s%s", m_szModuleName, PS_GOTO_INBOX);
else
mir_snprintf(szServiceFunction, "%s%s", m_szModuleName, PS_DUMMY);
int i = 0;
while (CLISTEVENT *pcle = g_clistApi.pfnGetEvent(-1, i++))
if (!mir_strcmp(pcle->pszService, szServiceFunction))
return;
CLISTEVENT cle = {};
cle.hDbEvent = ICQ_FAKE_EVENT_ID;
cle.moduleName = m_szModuleName;
cle.hIcon = g_plugin.getIcon(IDI_INBOX);
cle.flags = CLEF_UNICODE | CLEF_PROTOCOLGLOBAL;
cle.pszService = szServiceFunction;
cle.szTooltip.w = pwszText;
g_clistApi.pfnAddEvent(&cle);
}
}
|