summaryrefslogtreecommitdiff
path: root/protocols/Skype/src/skype_avatars.cpp
blob: bf9fae91360a1e3aeb853b26ae6f364318d4bcb9 (plain)
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
#include "skype.h"

bool CSkypeProto::IsAvatarChanged(const SEBinary &avatar, HANDLE hContact)
{
	bool result = false;

	::mir_md5_byte_t digest[16];
	::mir_md5_hash((PBYTE)avatar.data(), (int)avatar.size(), digest);

	DBVARIANT dbv;
	::db_get(hContact, this->m_szModuleName, "AvatarHash", &dbv);
	if (dbv.type == DBVT_BLOB && dbv.pbVal && dbv.cpbVal == 16)
	{
		if (::memcmp(digest, dbv.pbVal, 16) == 0)
		{
			result = true;
		}
	}
	::db_free(&dbv);

	return result;
}

int CSkypeProto::DetectAvatarFormatBuffer(const char *pBuffer)
{
	if (!strncmp(pBuffer, "%PNG", 4))
		return PA_FORMAT_PNG;

	if (!strncmp(pBuffer, "GIF8", 4))
		return PA_FORMAT_GIF;

	if (!_strnicmp(pBuffer, "<?xml", 5))
		return PA_FORMAT_XML;

	if ((((DWORD *)pBuffer)[0] == 0xE0FFD8FFul) || (((DWORD *)pBuffer)[0] == 0xE1FFD8FFul))
		return PA_FORMAT_JPEG;

	if (!strncmp(pBuffer, "BM", 2))
		return PA_FORMAT_BMP;

	return PA_FORMAT_UNKNOWN;
}

int CSkypeProto::DetectAvatarFormat(const wchar_t *path)
{
	int src = _wopen(path, _O_BINARY | _O_RDONLY, 0);
	if (src == -1)
		return PA_FORMAT_UNKNOWN;

	char pBuf[32];
	_read(src, pBuf, 32);
	_close(src);

	return CSkypeProto::DetectAvatarFormatBuffer(pBuf);
}

wchar_t * CSkypeProto::GetContactAvatarFilePath(HANDLE hContact)
{
	wchar_t *path = (wchar_t*)::mir_alloc(MAX_PATH * sizeof(wchar_t));

	this->InitCustomFolders();

	if (m_hAvatarsFolder == NULL || FoldersGetCustomPathT(m_hAvatarsFolder, path, MAX_PATH, _T("")))
	{
		ptrW tmpPath( ::Utils_ReplaceVarsT(L"%miranda_avatarcache%"));
		::mir_sntprintf(path, MAX_PATH, _T("%s\\%S"), tmpPath, this->m_szModuleName);
	}

	DWORD dwAttributes = GetFileAttributes(path);
	if (dwAttributes == 0xffffffff || (dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
		CallService(MS_UTILS_CREATEDIRTREET, 0, (LPARAM)path);

	ptrW sid(::db_get_wsa(hContact, this->m_szModuleName, SKYPE_SETTINGS_SID));
	if (hContact != NULL)
		::mir_sntprintf(path, MAX_PATH, _T("%s\\%s.jpg"), path, sid);
	else if (sid != NULL)
		::mir_sntprintf(path, MAX_PATH, _T("%s\\%s avatar.jpg"), path, sid);
	else
	{
		::mir_free(path);
		return NULL;
	}

	return path;
}

INT_PTR __cdecl CSkypeProto::GetAvatarInfo(WPARAM, LPARAM lParam)
{
	PROTO_AVATAR_INFORMATIONW *pai = (PROTO_AVATAR_INFORMATIONW *)lParam;

	if (::db_get_dw(pai->hContact, this->m_szModuleName, "AvatarTS", 0))
	{
		return GAIR_NOAVATAR;
	}

	ptrW sid = ::db_get_wsa(pai->hContact, this->m_szModuleName, SKYPE_SETTINGS_SID);
	if (sid)
	{
		ptrW path( this->GetContactAvatarFilePath(pai->hContact));
		if (path && !_waccess(path, 0))
		{
			::wcsncpy(pai->filename, path, SIZEOF(pai->filename));
			pai->format = PA_FORMAT_JPEG;
			return GAIR_SUCCESS;
		}
	}

	return GAIR_NOAVATAR;
}

INT_PTR __cdecl CSkypeProto::GetAvatarCaps(WPARAM wParam, LPARAM lParam)
{
	switch (wParam)
	{
	case AF_MAXSIZE:
	{
		POINT *size = (POINT *)lParam;
		if (size)
		{
			size->x = 96;
			size->y = 96;
		}
	}
	break;
	
	case AF_PROPORTION:
		return PIP_NONE;

	case AF_FORMATSUPPORTED:
		return lParam == PA_FORMAT_JPEG;

	case AF_ENABLED:
			return 1;
	
	case AF_DONTNEEDDELAYS:
		return 1;

	case AF_MAXFILESIZE:
		// server accepts images of 32000 bytees, not bigger
		return 32000;
	
	case AF_DELAYAFTERFAIL:
		// do not request avatar again if server gave an error
		return 1;// * 60 * 60 * 1000; // one hour
	
	case AF_FETCHALWAYS:
		// avatars can be fetched all the time (server only operation)
		return 1;
	}

	return 0;
}

INT_PTR __cdecl CSkypeProto::GetMyAvatar(WPARAM wParam, LPARAM lParam)
{
	if (!wParam)
		return -2;

	mir_ptr<wchar_t> path( this->GetContactAvatarFilePath(NULL));
	if (path && CSkypeProto::FileExists(path)) 
	{
		::wcsncpy((wchar_t *)wParam, path, (int)lParam);
		return 0;
	}

	return -1;
}

INT_PTR __cdecl CSkypeProto::SetMyAvatar(WPARAM, LPARAM lParam)
{
	wchar_t *path = (wchar_t *)lParam;
	if (path)
	{
		ptrW avatarPath( this->GetContactAvatarFilePath(NULL));
		if ( !::wcscmp(path, avatarPath))
		{
			this->Log(L"New avatar path are same with old.");
			return -1;
		}

		SEBinary avatar = this->GetAvatarBinary(path);
		if (avatar.size() == 0)
		{
			this->Log(L"Failed to read avatar file.");
			return -1;
		}

		if (this->IsAvatarChanged(avatar))
		{
			this->Log(L"New avatar are same with old.");
			return -1;
		}

		if ( !::CopyFile(path, avatarPath, FALSE))
		{
			this->Log(L"Failed to copy new avatar to local storage.");
			return -1;
		}
		
		Skype::VALIDATERESULT result = Skype::NOT_VALIDATED;
		if (!this->account->SetAvatar(avatar, result))
		{
			this->Log(CSkypeProto::ValidationReasons[result]);
			return -1;
		}

		uint newTS = this->account->GetUintProp(Account::P_AVATAR_IMAGE);
		::db_set_dw(NULL, this->m_szModuleName, "AvatarTS", newTS);
		return 0;
	}

	this->account->SetBinProperty(Account::P_AVATAR_IMAGE, SEBinary());
	::db_unset(NULL, this->m_szModuleName, "AvatarTS");
	return 0;
}

SEBinary CSkypeProto::GetAvatarBinary(wchar_t *path)
{
	SEBinary avatar;

	if (CSkypeProto::FileExists(path))
	{
		int len;
		char *buffer;
		FILE* fp = ::_wfopen(path, L"rb");
		if (fp)
		{
			::fseek(fp, 0, SEEK_END);
			len = ::ftell(fp);
			::fseek(fp, 0, SEEK_SET);
			buffer = new char[len + 1];
			::fread(buffer, len, 1, fp);
			::fclose(fp);

			avatar.set(buffer, len);
		}
	}

	return avatar;
}