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
|
#include "common.h"
std::tstring CToxProto::GetAvatarFilePath(MCONTACT hContact)
{
TCHAR path[MAX_PATH];
mir_sntprintf(path, SIZEOF(path), _T("%s\\%S"), VARST(_T("%miranda_avatarcache%")), m_szModuleName);
DWORD dwAttributes = GetFileAttributes(path);
if (dwAttributes == 0xffffffff || (dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
CallService(MS_UTILS_CREATEDIRTREET, 0, (LPARAM)path);
}
ptrT id(getTStringA(hContact, TOX_SETTINGS_ID));
if (hContact != NULL)
{
mir_sntprintf(path, MAX_PATH, _T("%s\\%s.png"), path, id);
}
else if (id != NULL)
{
mir_sntprintf(path, MAX_PATH, _T("%s\\%s avatar.png"), path, id);
}
else
{
return _T("");
}
return path;
}
void CToxProto::SetToxAvatar(std::tstring path, bool checkHash)
{
FILE *hFile = _tfopen(path.c_str(), L"rb");
if (!hFile)
{
debugLogA(__FUNCTION__": failed to open avatar file");
return;
}
fseek(hFile, 0, SEEK_END);
size_t length = ftell(hFile);
rewind(hFile);
if (length > 1024 * 1024)
{
fclose(hFile);
debugLogA(__FUNCTION__": new avatar size is excessive");
return;
}
uint8_t *data = (uint8_t*)mir_alloc(length);
if (fread(data, sizeof(uint8_t), length, hFile) != length)
{
fclose(hFile);
debugLogA(__FUNCTION__": failed to read avatar file");
return;
}
fclose(hFile);
DBVARIANT dbv;
uint8_t hash[TOX_HASH_LENGTH];
tox_hash(hash, data, TOX_HASH_LENGTH);
if (checkHash && !db_get(NULL, m_szModuleName, TOX_SETTINGS_AVATAR_HASH, &dbv))
{
if (memcmp(hash, dbv.pbVal, TOX_HASH_LENGTH) == 0)
{
db_free(&dbv);
mir_free(data);
debugLogA(__FUNCTION__": new avatar is same with old");
return;
}
db_free(&dbv);
}
for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName))
{
if (GetContactStatus(hContact) == ID_STATUS_OFFLINE)
continue;
int32_t friendNumber = GetToxFriendNumber(hContact);
if (friendNumber == UINT32_MAX)
{
mir_free(data);
debugLogA(__FUNCTION__": failed to set new avatar");
return;
}
TOX_ERR_FILE_SEND error;
uint32_t fileNumber = tox_file_send(tox, friendNumber, TOX_FILE_KIND_AVATAR, length, NULL, hash, TOX_HASH_LENGTH, &error);
if (error != TOX_ERR_FILE_SEND_OK)
{
mir_free(data);
debugLogA(__FUNCTION__": failed to set new avatar");
return;
}
FileTransferParam *transfer = new FileTransferParam(friendNumber, fileNumber, _T("avatar"), length);
transfer->pfts.hContact = hContact;
transfer->pfts.flags |= PFTS_SENDING;
//transfer->pfts.tszWorkingDir = fileDir;
transfer->hFile = hFile;
transfers.Add(transfer);
}
mir_free(data);
if (checkHash)
{
db_set_blob(NULL, m_szModuleName, TOX_SETTINGS_AVATAR_HASH, (void*)hash, TOX_HASH_LENGTH);
}
}
INT_PTR CToxProto::GetAvatarCaps(WPARAM wParam, LPARAM lParam)
{
switch (wParam)
{
case AF_MAXSIZE:
{
POINT *size = (POINT *)lParam;
if (size)
{
size->x = 300;
size->y = 300;
}
}
break;
case AF_ENABLED:
return 1;
case AF_FORMATSUPPORTED:
return lParam == PA_FORMAT_PNG;
case AF_MAXFILESIZE:
return 1024 * 1024;
}
return 0;
}
INT_PTR CToxProto::GetAvatarInfo(WPARAM, LPARAM lParam)
{
PROTO_AVATAR_INFORMATIONW *pai = (PROTO_AVATAR_INFORMATIONW *)lParam;
ptrA id(getStringA(pai->hContact, TOX_SETTINGS_ID));
if (id != NULL)
{
std::tstring path = GetAvatarFilePath(pai->hContact);
if (IsFileExists(path))
{
_tcsncpy(pai->filename, path.c_str(), SIZEOF(pai->filename));
pai->format = PA_FORMAT_PNG;
return GAIR_SUCCESS;
}
}
return GAIR_NOAVATAR;
}
INT_PTR CToxProto::GetMyAvatar(WPARAM wParam, LPARAM lParam)
{
if (!wParam)
{
return -2;
}
std::tstring path(GetAvatarFilePath());
if (IsFileExists(path))
{
_tcsncpy((TCHAR*)wParam, path.c_str(), (int)lParam);
return 0;
}
return -1;
}
INT_PTR CToxProto::SetMyAvatar(WPARAM, LPARAM lParam)
{
TCHAR *path = (TCHAR*)lParam;
std::tstring avatarPath = GetAvatarFilePath();
if (path != NULL)
{
if (!CopyFile(path, avatarPath.c_str(), FALSE))
{
debugLogA("CToxProto::SetMyAvatar: failed to copy new avatar to avatar cache");
return -1;
}
if (IsOnline())
SetToxAvatar(avatarPath, true);
}
else
{
if (IsOnline())
{
for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName))
{
if (GetContactStatus(hContact) == ID_STATUS_OFFLINE)
continue;
int32_t friendNumber = GetToxFriendNumber(hContact);
if (friendNumber == UINT32_MAX)
{
debugLogA(__FUNCTION__": failed to unset avatar");
return -1;
}
TOX_ERR_FILE_SEND error;
if (!tox_file_send(tox, NULL, TOX_FILE_KIND_AVATAR, 0, NULL, NULL, 0, &error))
{
debugLogA(__FUNCTION__": failed to unset avatar");
return -1;
}
}
}
if (IsFileExists(avatarPath))
{
DeleteFile(avatarPath.c_str());
}
db_unset(NULL, m_szModuleName, TOX_SETTINGS_AVATAR_HASH);
}
return 0;
}
void CToxProto::OnGotFriendAvatarInfo(FileTransferParam *transfer, const uint8_t *hash)
{
MCONTACT hContact = transfer->pfts.hContact;
std::tstring path = GetAvatarFilePath();
if (transfer->pfts.totalBytes == 0)
{
delSetting(hContact, TOX_SETTINGS_AVATAR_HASH);
ProtoBroadcastAck(hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, 0, 0);
if (IsFileExists(path))
{
DeleteFile(path.c_str());
}
OnFileCancel(hContact, transfer);
}
else
{
DBVARIANT dbv;
if (!db_get(transfer->pfts.hContact, m_szModuleName, TOX_SETTINGS_AVATAR_HASH, &dbv))
{
if (memcmp(hash, dbv.pbVal, TOX_HASH_LENGTH) == 0)
{
db_free(&dbv);
OnFileCancel(hContact, transfer);
return;
}
db_free(&dbv);
}
OnFileAllow(hContact, transfer, path.c_str());
}
}
/*void CToxProto::OnGotFriendAvatarData(Tox *, int32_t number, uint8_t, uint8_t *hash, uint8_t *data, uint32_t length, void *arg)
{
CToxProto *proto = (CToxProto*)arg;
MCONTACT hContact = proto->GetContact(number);
if (hContact)
{
db_set_blob(hContact, proto->m_szModuleName, TOX_SETTINGS_AVATAR_HASH, hash, TOX_HASH_LENGTH);
std::tstring path = proto->GetAvatarFilePath(hContact);
FILE *hFile = _tfopen(path.c_str(), L"wb");
if (hFile)
{
if (fwrite(data, sizeof(uint8_t), length, hFile) == length)
{
PROTO_AVATAR_INFORMATIONW pai = { sizeof(pai) };
pai.format = PA_FORMAT_PNG;
pai.hContact = hContact;
_tcscpy(pai.filename, path.c_str());
proto->ProtoBroadcastAck(hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, (HANDLE)&pai, 0);
}
fclose(hFile);
}
}
}*/
|