summaryrefslogtreecommitdiff
path: root/protocols/SkypeWeb/src/skype_contacts.cpp
blob: 5aebc68c75ec5c86bbf3107c952af8a0cf384798 (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
241
242
243
#include "common.h"

WORD CSkypeProto::GetContactStatus(MCONTACT hContact)
{
	return getWord(hContact, "Status", ID_STATUS_OFFLINE);
}

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

void CSkypeProto::SetAllContactsStatus(WORD status)
{
	for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName))
	{
		SetContactStatus(hContact, status);
	}
}

MCONTACT CSkypeProto::GetContactFromAuthEvent(MEVENT hEvent)
{
	DWORD body[3];
	DBEVENTINFO dbei = { sizeof(DBEVENTINFO) };
	dbei.cbBlob = sizeof(DWORD) * 2;
	dbei.pBlob = (PBYTE)&body;

	if (db_event_get(hEvent, &dbei))
		return INVALID_CONTACT_ID;

	if (dbei.eventType != EVENTTYPE_AUTHREQUEST)
		return INVALID_CONTACT_ID;

	if (strcmp(dbei.szModule, m_szModuleName) != 0)
		return INVALID_CONTACT_ID;

	return DbGetAuthEventContact(&dbei);
}

MCONTACT CSkypeProto::GetContact(const char *skypename)
{
	MCONTACT hContact = NULL;
	for (hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName))
	{
		ptrA cSkypename(getStringA(hContact, SKYPE_SETTINGS_ID));
		if (mir_strcmpi(skypename, cSkypename) == 0)
		{
			break;
		}
	}
	return hContact;
}

MCONTACT CSkypeProto::AddContact(const char *skypename, bool isTemporary)
{
	MCONTACT hContact = GetContact(skypename);
	if (!hContact)
	{
		hContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0);
		CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)m_szModuleName);

		setString(hContact, SKYPE_SETTINGS_ID, skypename);

		DBVARIANT dbv;
		if (!getTString(SKYPE_SETTINGS_GROUP, &dbv))
		{
			db_set_ts(hContact, "CList", "Group", dbv.ptszVal);
			db_free(&dbv);
		}

		setByte(hContact, "Auth", 1);
		setByte(hContact, "Grant", 1);

		if (isTemporary)
		{
			db_set_b(hContact, "CList", "NotOnList", 1);
		}
	}
	return hContact;
}

//[{ "username":"echo123", "firstname" : "Echo \/ Sound Test Service", "lastname" : null, "avatarUrl" : null, "mood" : null, "richMood" : null, "displayname" : null, "country" : null, "city" : null },...]
void CSkypeProto::LoadProfiles(const NETLIBHTTPREQUEST *response)
{
	if (response == NULL)
		return;

	JSONROOT root(response->pData);
	if (root == NULL)
		return;

	JSONNODE *items = json_as_array(root), *item, *node;
	for (size_t i = 0; i < json_size(items); i++)
	{
		item = json_at(items, i);
		if (item == NULL)
			break;

		node = json_get(item, "username");
		ptrA skypename(mir_t2a(ptrT(json_as_string(node))));
		MCONTACT hContact = AddContact(skypename);
		if (hContact)
		{
			node = json_get(item, "avatarUrl");
			CMStringA avatarUrl = mir_t2a(ptrT(json_as_string(node)));
			if (avatarUrl && avatarUrl != "null")
				; // TODO: load avatar

			node = json_get(item, "mood");
			CMString mood = ptrT(json_as_string(node));
			if (!mood.IsEmpty() && mood != "null")
				db_set_ts(hContact, "CList", "StatusMsg", mood);
			else
				db_unset(hContact, "CList", "StatusMsg");

			node = json_get(item, "richMood");
			ptrT richMood(json_as_string(node));

			node = json_get(item, "country");
			ptrA country(mir_t2a(ptrT(json_as_string(node))));

			node = json_get(item, "city");
			CMString city = ptrT(json_as_string(node));
			if (!city.IsEmpty() && city != "null")
				setTString(hContact, "City", city);
			else
				delSetting(hContact, "City");
		}

	}
	json_delete(items);
}

//[{"skypename":"echo123", "fullname" : "Echo \/ Sound Test Service", "authorized" : true, "blocked" : false, "display_name" : null, "pstn_number" : null, "phone1" : null, "phone1_label" : null, "phone2" : null, "phone2_label" : null, "phone3" : null, "phone3_label" : null},...]
void CSkypeProto::LoadContacts(const NETLIBHTTPREQUEST *response)
{
	if (response == NULL)
		return;

	JSONROOT root(response->pData);
	if (root == NULL)
		return;

	LIST<char> skypenames(1);

	JSONNODE *items = json_as_array(root), *item, *node;
	for (size_t i = 0; i < json_size(items); i++)
	{
		item = json_at(items, i);
		if (item == NULL)
			break;

		node = json_get(item, "skypename");
		ptrA skypename(mir_t2a(ptrT(json_as_string(node))));
		MCONTACT hContact = AddContact(skypename);
		if (hContact)
		{
			node = json_get(item, "fullname");
			CMString realname = ptrT(json_as_string(node));
			if (!realname.IsEmpty() && realname != "null")
			{
				size_t pos = realname.Find(' ', 1);
				if (mir_strcmpi(skypename, "echo123") != 0 && pos != -1)
				{
					setTString(hContact, "FirstName", realname.Mid(0, pos));
					setTString(hContact, "LastName", realname.Mid(pos + 1));
				}
				else
				{
					setTString(hContact, "FirstName", realname);
					delSetting(hContact, "LastName");
				}
			}
			else
			{
				delSetting(hContact, "FirstName");
				delSetting(hContact, "LastName");
			}

			node = json_get(item, "display_name");
			CMString nick = ptrT(json_as_string(node));
			if (!nick.IsEmpty() && nick != "null")
				setTString(hContact, "Nick", nick);
			else
				delSetting(hContact, "Nick");

			node = json_get(item, "pstn_number");
			CMString ptsnNumber = ptrT(json_as_string(node));
			if (!ptsnNumber.IsEmpty() && ptsnNumber != "null")
				setString(hContact, "PtsnNumber", ptrA(mir_u2a(ptsnNumber)));
			else
				delSetting(hContact, "PtsnNumber");

			node = json_get(item, "authorized");
			if (json_as_bool(node))
			{
				delSetting(hContact, "Auth");
				delSetting(hContact, "Grant");
			}
			else
			{
				setByte(hContact, "Grant", 1);
			}
			
			node = json_get(item, "blocked");
			setByte(hContact, "IsBlocked", json_as_bool(node));

			skypenames.insert(mir_strdup(skypename));
		}
	}
	json_delete(items);

	if (skypenames.getCount() > 0)
	{
		ptrA token(getStringA("TokenSecret"));
		PushRequest(new GetProfilesRequest(token, skypenames), &CSkypeProto::LoadProfiles);

		for (size_t i = 0; i < skypenames.getCount(); i++)
		{
			mir_free(skypenames[i]);
		}
		skypenames.destroy();
	}
}

INT_PTR CSkypeProto::OnRequestAuth(WPARAM hContact, LPARAM lParam)
{
	return 0;
}

INT_PTR CSkypeProto::OnGrantAuth(WPARAM hContact, LPARAM)
{
	return 0;
}

int CSkypeProto::OnContactDeleted(MCONTACT hContact, LPARAM)
{
	return 0;
}