summaryrefslogtreecommitdiff
path: root/protocols/Slack/src/slack_contacts.cpp
blob: 185d5110b887cbd95df1307f667377ab2bb6dbbb (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
#include "stdafx.h"

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

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

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

MCONTACT CSlackProto::GetContact(const char *userId)
{
	MCONTACT hContact = NULL;
	for (hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName))
	{
		ptrA contactPhone(getStringA(hContact, "UserId"));
		if (mir_strcmp(userId, contactPhone) == 0)
			break;
	}
	return hContact;
}

MCONTACT CSlackProto::AddContact(const char *userId, const char *nick, bool isTemporary)
{
	MCONTACT hContact = GetContact(userId);
	if (!hContact)
	{
		hContact = db_add_contact();
		Proto_AddToContact(hContact, m_szModuleName);

		setString(hContact, "UserId", userId);

		if (mir_strlen(nick))
			setWString(hContact, "Nick", ptrW(mir_utf8decodeW(nick)));

		DBVARIANT dbv;
		if (!getWString("TeamName", &dbv) && Clist_GroupExists(dbv.ptszVal))
		{
			db_set_ws(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;
}

void CSlackProto::OnGotUserProfile(JSONNode &root)
{
	if (!root)
	{
		debugLogA(__FUNCTION__": failed to load user profile");
		return;
	}

	bool isOk = root["ok"].as_bool();
	if (!isOk)
	{
		debugLogA(__FUNCTION__": failed to load users profile");
		return;
	}

	JSONNode profile = root["profile"].as_node();
}

void CSlackProto::OnGotUserProfile(MCONTACT hContact, JSONNode &root)
{
	if (!root)
	{
		debugLogA(__FUNCTION__": failed to read users profile");
		return;
	}

	CMStringW firstName = root["first_name"].as_mstring();
	setWString(hContact, "FirstName", firstName);

	CMStringW lastName = root["last_name"].as_mstring();
	setWString(hContact, "LastName", lastName);
}

void CSlackProto::OnGotUserList(JSONNode &root)
{
	if (!root)
	{
		debugLogA(__FUNCTION__": failed to load users list");
		return;
	}

	bool isOk = root["ok"].as_bool();
	if (!isOk)
	{
		debugLogA(__FUNCTION__": failed to load users list");
		return;
	}

	JSONNode users = root["members"].as_array();
	for (size_t i = 0; i < users.size(); i++)
	{
		JSONNode user = users[i];

		json_string userId = user["id"].as_string();
		json_string nick = user["name"].as_string();
		bool isDeleted = user["deleted"].as_bool();
		MCONTACT hContact = AddContact(userId.c_str(), nick.c_str(), isDeleted);
		if (hContact)
		{
			json_string teamId = user["team_id"].as_string();
			setString(hContact, "TeamId", teamId.c_str());

			CMStringW status = root["status"].as_mstring();
			if (!status.IsEmpty())
				setWString(hContact, "StatusMsg", status);

			JSONNode profile = root["profile"].as_node();
			OnGotUserProfile(hContact, profile);
		}
	}
}

INT_PTR CSlackProto::OnRequestAuth(WPARAM hContact, LPARAM lParam)
{
	if (!IsOnline())
	{
		return -1; // ???
	}
	return 0;
}

INT_PTR CSlackProto::OnGrantAuth(WPARAM hContact, LPARAM)
{
	if (!IsOnline())
	{
		// TODO: warn
		return 0;
	}

	return 0;
}

int CSlackProto::OnContactDeleted(MCONTACT hContact, LPARAM)
{
	if (!IsOnline())
	{
		// TODO: warn
		return 0;
	}

	return 0;
}