summaryrefslogtreecommitdiff
path: root/protocols/Facebook/src/avatars.cpp
blob: 1823c8da4dc3aec1b36af6257c2e04ec77094c25 (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
/*

Facebook plugin for Miranda NG
Copyright © 2019-20 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, see <http://www.gnu.org/licenses/>.

*/

#include "stdafx.h"

/////////////////////////////////////////////////////////////////////////////////////////

void FacebookProto::GetAvatarFilename(MCONTACT hContact, wchar_t *pwszFileName)
{
	WCHAR wszPath[MAX_PATH];
	mir_snwprintf(wszPath, MAX_PATH, L"%s\\%S", VARSW(L"%miranda_avatarcache%"), m_szModuleName);

	DWORD dwAttributes = GetFileAttributes(wszPath);
	if (dwAttributes == 0xffffffff || (dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
		CreateDirectoryTreeW(wszPath);

	CMStringW id(getMStringW(hContact, DBKEY_ID));
	mir_snwprintf(pwszFileName, MAX_PATH, L"%s\\%s.jpg", wszPath, id.c_str());
}

/////////////////////////////////////////////////////////////////////////////////////////

void __cdecl FacebookProto::AvatarsUpdate(void *)
{
	NETLIBHTTPREQUEST req = {};
	req.cbSize = sizeof(req);
	req.flags = NLHRF_NODUMP | NLHRF_SSL | NLHRF_HTTP11 | NLHRF_REDIRECT;
	req.requestType = REQUEST_GET;

	const char *szParams = m_bUseBigAvatars ? "width=200&height=200" : "width=80&height=80";

	for (auto &cc : AccContacts()) {
		if (Miranda_IsTerminated())
			break;

		if (!getByte(cc, "UpdateNeeded"))
			continue;

		delSetting(cc, "UpdateNeeded");

		CMStringA szUrl(FORMAT, "https://graph.facebook.com/%s/picture?%s", getMStringA(cc, DBKEY_ID).c_str(), szParams);
		req.szUrl = szUrl.GetBuffer();
	
		NETLIBHTTPREQUEST *pReply = Netlib_HttpTransaction(m_hNetlibUser, &req);
		if (pReply == nullptr) {
			debugLogA("Failed to retrieve avatar from url: %s", szUrl.c_str());
			continue;
		}

		PROTO_AVATAR_INFORMATION ai;
		ai.hContact = cc;
		ai.format = PA_FORMAT_UNKNOWN;
		GetAvatarFilename(cc, ai.filename);

		bool bSuccess = false;
		if (pReply->resultCode == 200 && pReply->pData && pReply->dataLength) {
			for (int i = 0; i < pReply->headersCount; i++)
				if (!mir_strcmp(pReply->headers[i].szName, "Content-Type")) {
					ai.format = ProtoGetAvatarFormatByMimeType(pReply->headers[i].szValue);
					break;
				}

			if (ai.format != PA_FORMAT_UNKNOWN) {
				FILE *fout = _wfopen(ai.filename, L"wb");
				if (fout) {
					fwrite(pReply->pData, 1, pReply->dataLength, fout);
					fclose(fout);
					bSuccess = true;
				}
				else debugLogA("Error saving avatar to file %S", ai.filename);
			}
			else debugLogA("unknown avatar mime type");
		}
		else debugLogA("Error %d reading avatar from url: %s", pReply->resultCode, szUrl.c_str());

		ProtoBroadcastAck(cc, ACKTYPE_AVATAR, bSuccess ? ACKRESULT_SUCCESS : ACKRESULT_FAILED, (HANDLE)&ai);

		Netlib_FreeHttpRequest(pReply);
	}
}

INT_PTR FacebookProto::GetAvatarInfo(WPARAM flags, LPARAM lParam)
{
	PROTO_AVATAR_INFORMATION *pai = (PROTO_AVATAR_INFORMATION *)lParam;
	GetAvatarFilename(pai->hContact, pai->filename);

	bool bFileExist = _waccess(pai->filename, 0) == 0;

	// if we still need to load an avatar
	if ((flags & GAIF_FORCE) || !bFileExist) {
		setByte(pai->hContact, "UpdateNeeded", 1);
		ForkThread(&FacebookProto::AvatarsUpdate);
		return GAIR_WAITFOR;
	}
	
	return (bFileExist) ? GAIR_SUCCESS : GAIR_NOAVATAR;
}

INT_PTR FacebookProto::GetAvatarCaps(WPARAM wParam, LPARAM lParam)
{
	int res = 0;

	switch (wParam) {
	case AF_MAXSIZE:
		((POINT *)lParam)->x = ((POINT *)lParam)->y = 128;
		break;

	case AF_FORMATSUPPORTED:
		res = lParam == PA_FORMAT_PNG || lParam == PA_FORMAT_GIF || lParam == PA_FORMAT_JPEG;
		break;

	case AF_ENABLED:
	case AF_FETCHIFPROTONOTVISIBLE:
	case AF_FETCHIFCONTACTOFFLINE:
		return 1;
	}

	return res;
}