summaryrefslogtreecommitdiff
path: root/protocols/Teams/src/teams_server.cpp
blob: ee55e90ce2c708e5d89f6560aff12d88bd1c2a18 (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
/*
Copyright (C) 2025 Miranda NG team (https://miranda-ng.org)

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 version 2
of the License.

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 CTeamsProto::OnCapabilitiesSended(MHttpResponse *response, AsyncHttpRequest *)
{
	if (response == nullptr || response->body.IsEmpty()) {
		ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, 1001);
		SetStatus(ID_STATUS_OFFLINE);
		return;
	}

	CreateContactSubscription();
	ReceiveAvatar(0);
	RefreshContactsInfo();
	RefreshConversations();

	JSONNode root = JSONNode::parse(response->body);
	if (root)
		m_szOwnSkypeId = UrlToSkypeId(root["selfLink"].as_string().c_str()).Detach();

	GetProfileInfo(0);
}

void CTeamsProto::SendPresence()
{
	ptrA epname;

	if (!m_bUseHostnameAsPlace && m_wstrPlace && *m_wstrPlace)
		epname = mir_utf8encodeW(m_wstrPlace);
	else {
		wchar_t compName[MAX_COMPUTERNAME_LENGTH + 1];
		DWORD size = _countof(compName);
		GetComputerNameW(compName, &size);
		epname = mir_utf8encodeW(compName);
	}

	JSONNode privateInfo; privateInfo.set_name("privateInfo");
	privateInfo << CHAR_PARAM("epname", epname);

	JSONNode publicInfo; publicInfo.set_name("publicInfo");
	publicInfo << CHAR_PARAM("capabilities", "Audio|Video") << INT_PARAM("typ", 125)
		<< CHAR_PARAM("skypeNameVersion", "Miranda NG Skype") << CHAR_PARAM("nodeInfo", "xx") << CHAR_PARAM("version", g_szMirVer);

	JSONNode node;
	node << CHAR_PARAM("id", "messagingService") << CHAR_PARAM("type", "EndpointPresenceDoc")
		<< CHAR_PARAM("selfLink", "uri") << privateInfo << publicInfo;

	auto *pReq = new AsyncHttpRequest(REQUEST_PUT, HOST_DEFAULT, "/users/ME/endpoints/" + mir_urlEncode(m_szEndpoint) + "/presenceDocs/messagingService",
		&CTeamsProto::OnCapabilitiesSended);
	pReq->m_szParam = node.write().c_str();
	pReq->AddRegistration(this);
	PushRequest(pReq);
}

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

void CTeamsProto::OnStatusChanged(MHttpResponse *response, AsyncHttpRequest *)
{
	if (response == nullptr || response->resultCode != 200) {
		debugLogA(__FUNCTION__ ": failed to change status");
		ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, 1001);
		SetStatus(ID_STATUS_OFFLINE);
		return;
	}

	int oldStatus = m_iStatus;
	m_iStatus = m_iDesiredStatus;
	ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus);
}

void CTeamsProto::SetServerStatus(int iStatus)
{
	const char *pszAvailability;
	switch (iStatus) {
	case ID_STATUS_OFFLINE:
		pszAvailability = "Offline";
		break;
	case ID_STATUS_NA:
	case ID_STATUS_AWAY:
		pszAvailability = "Away";
		break;
	case ID_STATUS_DND:
		pszAvailability = "DoNotDisturb";
		break;
	case ID_STATUS_OCCUPIED:
		pszAvailability = "Busy";
		break;
	default:
		pszAvailability = "Available";
	}

	JSONNode node(JSON_NODE);
	node << CHAR_PARAM("availability", pszAvailability);

	auto *pReq = new AsyncHttpRequest(REQUEST_PUT, HOST_PRESENCE, "/me/forceavailability", &CTeamsProto::OnStatusChanged);
	pReq->m_szParam = node.write().c_str();
	PushRequest(pReq);
}

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

void CTeamsProto::CreateContactSubscription()
{
	CMStringA szUrl = m_szTrouterSurl;
	if (szUrl[szUrl.GetLength() - 1] != '/')
		szUrl += "/"; 
	szUrl += "TeamsUnifiedPresenceService";

	JSONNode listAdd(JSON_ARRAY); listAdd.set_name("subscriptionsToAdd");
	for (auto &hContact : AccContacts())
		if (!isChatRoom(hContact)) {
			JSONNode contact;
			contact << CHAR_PARAM("mri", getId(hContact));
			listAdd << contact;
		}

	JSONNode listRemove(JSON_ARRAY); listRemove.set_name("subscriptionsToRemove");

	JSONNode node;
	node << CHAR_PARAM("trouterUri", szUrl) << BOOL_PARAM("shouldPurgePreviousSubscriptions", true)
		<< listAdd << listRemove;

	auto *pReq = new AsyncHttpRequest(REQUEST_POST, HOST_PRESENCE, "/pubsub/subscriptions/" + m_szEndpoint);
	pReq->m_szParam = node.write().c_str();
	PushRequest(pReq);
}