summaryrefslogtreecommitdiff
path: root/protocols/Steam/src/steam_contacts.cpp
blob: 6038ca029c81bccdc0bae4843b38258e26b68beb (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
#include "common.h"

void CSteamProto::SetContactStatus(MCONTACT hContact, WORD status)
{
	WORD oldStatus = getWord(hContact, "Status", ID_STATUS_OFFLINE);
	if (oldStatus != status)
		setWord(hContact, "Status", status);
}

void CSteamProto::SetAllContactsStatus(WORD status)
{
	for (MCONTACT hContact = db_find_first(this->m_szModuleName); hContact; hContact = db_find_next(hContact, this->m_szModuleName))
	{
		if (this->isChatRoom(hContact))
			continue;
		//if (this->IsContactOnline(hContact))
		setWord(hContact, "Status", status);
	}
}

void CSteamProto::UpdateContact(MCONTACT hContact, const SteamWebApi::FriendApi::Summary *contact)
{
	if (hContact && !FindContact(contact->GetSteamId()))
		return;

	// set common data
	setWString(hContact, "Nick", contact->GetNickname());
	setWord(hContact, "Status", SteamToMirandaStatus(contact->GetState()));
	setString(hContact, "Homepage", contact->GetHomepage());
	setDword(hContact, "LastEventDateTS", contact->GetLastEvent());

	// set name
	ptrW realname(mir_wstrdup(contact->GetRealname()));
	const wchar_t *p = wcschr(realname, ' ');
	if (p == NULL)
		setWString(hContact, "FirstName", realname);
	else
	{
		int length = p - (wchar_t*)realname;
		realname[length] = '\0';
		setWString(hContact, "LastName", realname);
		setWString(hContact, "LastName", p + 1);
	}

	// set country
	const char *isoCode = contact->GetCountryCode();
	if (!lstrlenA(isoCode))
		this->delSetting(hContact, "Country");
	else
	{
		// todo: is should be free()?
		char *country = (char *)CallService(MS_UTILS_GETCOUNTRYBYISOCODE, (WPARAM)isoCode, 0);
		setString(hContact, "Country", country);
	}
}

void CSteamProto::UpdateContactsThread(void *arg)
{
	ptrA steamIds((char*)arg);

	ptrA token(getStringA("TokenSecret"));

	SteamWebApi::FriendApi::Summaries summarues;
	SteamWebApi::FriendApi::LoadSummaries(m_hNetlibUser, token, steamIds, &summarues);

	if (!summarues.IsSuccess())
		return;

	for (int i = 0; i < summarues.GetItemCount(); i++)
	{
		const SteamWebApi::FriendApi::Summary *contact = summarues.GetAt(i);
		
		MCONTACT hContact = this->FindContact(contact->GetSteamId());
		if (!hContact)
		{
			UpdateContact(hContact, contact);
		}
	}
}

MCONTACT CSteamProto::FindContact(const char *steamId)
{
	MCONTACT hContact = NULL;

	EnterCriticalSection(&this->contact_search_lock);

	for (hContact = db_find_first(this->m_szModuleName); hContact; hContact = db_find_next(hContact, this->m_szModuleName))
	{
		ptrA cSteamId(db_get_sa(hContact, this->m_szModuleName, "SteamID"));
		if (!lstrcmpA(cSteamId, steamId))
			break;
	}

	LeaveCriticalSection(&this->contact_search_lock);

	return hContact;
}

MCONTACT CSteamProto::AddContact(const SteamWebApi::FriendApi::Summary *contact)
{
	MCONTACT hContact = this->FindContact(contact->GetSteamId());
	if (!hContact)
	{
		// create contact
		hContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0);
		CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)this->m_szModuleName);

		// update info
		UpdateContact(hContact, contact);

		// move to default group
		DBVARIANT dbv;
		if (!getWString("DefaultGroup", &dbv))
		{
			db_set_ts(hContact, "CList", "Group", dbv.ptszVal);
			db_free(&dbv);
		}
	}

	return hContact;
}

void CSteamProto::SearchByIdThread(void* arg)
{
	ptrW steamIdW((wchar_t*)arg);
	ptrA steamId(mir_u2a(steamIdW));

	ptrA token(getStringA("TokenSecret"));

	SteamWebApi::FriendApi::Summaries summarues;
	SteamWebApi::FriendApi::LoadSummaries(m_hNetlibUser, token, steamId, &summarues);

	if (!summarues.IsSuccess())
	{
		ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_FAILED, (HANDLE)STEAM_SEARCH_BYID, 0);
		return;
	}

	if (summarues.GetItemCount() == 0)
		return;

	const SteamWebApi::FriendApi::Summary *contact = summarues.GetAt(0);

	STEAM_SEARCH_RESULT ssr = { 0 };
	ssr.hdr.cbSize = sizeof(STEAM_SEARCH_RESULT);
	ssr.hdr.flags = PSR_TCHAR;
	
	ssr.hdr.id = mir_wstrdup(steamIdW);
	ssr.hdr.nick  = mir_wstrdup(contact->GetNickname());

	const wchar_t *realname = contact->GetRealname();
	const wchar_t *p = wcschr(realname, ' ');
	if (p == NULL)
		ssr.hdr.firstName = mir_wstrdup(realname);
	else
	{
		int length = p - realname;
		ssr.hdr.firstName = (wchar_t*)mir_alloc(sizeof(wchar_t) * (length + 1));
		wmemcpy(ssr.hdr.firstName, realname, length);
		ssr.hdr.firstName[length] = '\0';
		ssr.hdr.lastName = mir_wstrdup(p + 1);
	}
	
	ssr.contact = contact;

	ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)STEAM_SEARCH_BYID, (LPARAM)&ssr);
	ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)STEAM_SEARCH_BYID, 0);
}

void CSteamProto::SearchByNameThread(void* arg)
{
	ptrW keywordsW((wchar_t*)arg);
	ptrA keywords(mir_utf8encodeW(keywordsW));

	ptrA token(getStringA("TokenSecret"));

	SteamWebApi::SearchApi::SearchResult searchResult;
	SteamWebApi::SearchApi::Search(m_hNetlibUser, token, keywords, &searchResult);

	if (!searchResult.IsSuccess())
	{
		ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_FAILED, (HANDLE)STEAM_SEARCH_BYNAME, 0);
		return;
	}
	
	CMStringA steamIds;
	for (int i = 0; i < searchResult.GetItemCount(); i++)
	{
		const SteamWebApi::SearchApi::SearchItem *item = searchResult.GetAt(i);
		if (steamIds.IsEmpty())
			steamIds.Append(item->GetSteamId());
		else
			steamIds.AppendFormat(",%s", item->GetSteamId());
	}

	SteamWebApi::FriendApi::Summaries summarues;
	SteamWebApi::FriendApi::LoadSummaries(m_hNetlibUser, token, steamIds, &summarues);

	if (!summarues.IsSuccess())
	{
		ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_FAILED, (HANDLE)STEAM_SEARCH_BYNAME, 0);
		return;
	}

	for (int i = 0; i < summarues.GetItemCount(); i++)
	{
		const SteamWebApi::FriendApi::Summary *contact = summarues.GetAt(i);

		STEAM_SEARCH_RESULT ssr = { 0 };
		ssr.hdr.cbSize = sizeof(STEAM_SEARCH_RESULT);
		ssr.hdr.flags = PSR_TCHAR;

		ssr.hdr.id = mir_a2u(contact->GetSteamId());
		ssr.hdr.nick  = mir_wstrdup(contact->GetNickname());

		const wchar_t *realname = contact->GetRealname();
		const wchar_t *p = wcschr(realname, ' ');
		if (p == NULL)
			ssr.hdr.firstName = mir_wstrdup(realname);
		else
		{
			int length = p - realname;
			ssr.hdr.firstName = (wchar_t*)mir_alloc(sizeof(wchar_t) * (length + 1));
			wmemcpy(ssr.hdr.firstName, realname, length);
			ssr.hdr.firstName[length] = '\0';
			ssr.hdr.lastName = mir_wstrdup(p + 1);
		}

		ssr.contact = contact;

		ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)STEAM_SEARCH_BYNAME, (LPARAM)&ssr);
	}

	ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)STEAM_SEARCH_BYNAME, 0);
}