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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/*
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"
CTeamsProto::CTeamsProto(const char *protoName, const wchar_t *userName) :
PROTO<CTeamsProto>(protoName, userName),
m_impl(*this),
m_requests(10),
m_PopupClasses(1),
m_OutMessages(3, PtrKeySortT),
m_bAutoHistorySync(this, "AutoSync", true),
m_bUseHostnameAsPlace(this, "UseHostName", true),
m_bUseBBCodes(this, "UseBBCodes", true),
m_wstrCListGroup(this, DBKEY_GROUP, L"Teams"),
m_wstrPlace(this, "Place", L""),
m_iMood(this, "Mood", 0),
m_wstrMoodEmoji(this, "MoodEmoji", L""),
m_wstrMoodMessage(this, "XStatusMsg", L"")
{
// create endpoint
m_szEndpoint = getMStringA("Endpoint");
if (m_szEndpoint.IsEmpty()) {
m_szEndpoint = Utils_GenerateUUID();
setString("Endpoint", m_szEndpoint);
}
// network
NETLIBUSER nlu = {};
nlu.flags = NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS | NUF_UNICODE;
nlu.szDescriptiveName.w = m_tszUserName;
nlu.szSettingsModule = m_szModuleName;
m_hNetlibUser = Netlib_RegisterUser(&nlu);
CMStringA module(FORMAT, "%s.TRouter", m_szModuleName);
CMStringW descr(FORMAT, TranslateT("%s websocket connection"), m_tszUserName);
nlu.szSettingsModule = module.GetBuffer();
nlu.flags = NUF_INCOMING | NUF_OUTGOING | NUF_UNICODE;
nlu.szDescriptiveName.w = descr.GetBuffer();
m_hTrouterNetlibUser = Netlib_RegisterUser(&nlu);
CreateProtoService(PS_GETAVATARINFO, &CTeamsProto::SvcGetAvatarInfo);
CreateProtoService(PS_GETAVATARCAPS, &CTeamsProto::SvcGetAvatarCaps);
CreateProtoService(PS_GETMYAVATAR, &CTeamsProto::SvcGetMyAvatar);
CreateProtoService(PS_SETMYAVATAR, &CTeamsProto::SvcSetMyAvatar);
CreateProtoService(PS_OFFLINEFILE, &CTeamsProto::SvcOfflineFile);
CreateProtoService(PS_MENU_REQAUTH, &CTeamsProto::OnRequestAuth);
CreateProtoService(PS_MENU_GRANTAUTH, &CTeamsProto::OnGrantAuth);
CreateProtoService(PS_MENU_LOADHISTORY, &CTeamsProto::SvcLoadHistory);
CreateProtoService(PS_EMPTY_SRV_HISTORY, &CTeamsProto::SvcEmptyHistory);
HookProtoEvent(ME_OPT_INITIALISE, &CTeamsProto::OnOptionsInit);
CreateDirectoryTreeW(GetAvatarPath());
// sounds
g_plugin.addSound("skype_inc_call", L"SkypeWeb", LPGENW("Incoming call"));
g_plugin.addSound("skype_call_canceled", L"SkypeWeb", LPGENW("Incoming call canceled"));
InitGroupChatModule();
}
CTeamsProto::~CTeamsProto()
{
UninitPopups();
}
void CTeamsProto::OnEventDeleted(MCONTACT hContact, MEVENT hDbEvent, int flags)
{
if (!hContact || !(flags & CDF_DEL_HISTORY))
return;
DB::EventInfo dbei(hDbEvent, false);
if (dbei.szId) {
auto *pReq = new AsyncHttpRequest(REQUEST_DELETE, HOST_CHATS, "/users/ME/conversations/" + mir_urlEncode(getId(hContact)) + "/messages/" + dbei.szId);
pReq->AddAuthentication(this);
pReq->AddHeader("Origin", "https://web.skype.com");
pReq->AddHeader("Referer", "https://web.skype.com/");
PushRequest(pReq);
}
}
void CTeamsProto::OnEventEdited(MCONTACT hContact, MEVENT, const DBEVENTINFO &dbei)
{
if (dbei.szId)
SendServerMsg(hContact, dbei.pBlob, _atoi64(dbei.szId));
}
void CTeamsProto::OnModulesLoaded()
{
setAllContactStatuses(ID_STATUS_OFFLINE, false);
HookProtoEvent(ME_MSG_PRECREATEEVENT, &CTeamsProto::OnPreCreateMessage);
InitPopups();
}
void CTeamsProto::OnShutdown()
{
StopQueue();
StopTrouter();
}
INT_PTR CTeamsProto::GetCaps(int type, MCONTACT)
{
switch (type) {
case PFLAGNUM_1:
return PF1_IM | PF1_AUTHREQ | PF1_CHAT | PF1_BASICSEARCH | PF1_MODEMSG | PF1_FILE | PF1_SERVERCLIST;
case PFLAGNUM_2:
case PFLAGNUM_3:
return PF2_ONLINE | PF2_SHORTAWAY | PF2_LONGAWAY | PF2_LIGHTDND | PF2_HEAVYDND;
case PFLAGNUM_4:
return PF4_NOAUTHDENYREASON | PF4_SUPPORTTYPING | PF4_AVATARS | PF4_IMSENDOFFLINE | PF4_OFFLINEFILES | PF4_SERVERMSGID | PF4_SERVERFORMATTING;
case PFLAG_UNIQUEIDTEXT:
return (INT_PTR)TranslateT("Teams ID");
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
MCONTACT CTeamsProto::AddToList(int, PROTOSEARCHRESULT *psr)
{
debugLogA(__FUNCTION__);
if (psr->id.a == nullptr)
return NULL;
MCONTACT hContact;
if (psr->flags & PSR_UNICODE)
hContact = AddContact(T2Utf(psr->id.w), T2Utf(psr->nick.w));
else
hContact = AddContact(psr->id.a, psr->nick.a);
return hContact;
}
MCONTACT CTeamsProto::AddToListByEvent(int, int, MEVENT hDbEvent)
{
debugLogA(__FUNCTION__);
DB::EventInfo dbei(hDbEvent);
if (!dbei)
return NULL;
if (mir_strcmp(dbei.szModule, m_szModuleName))
return NULL;
if (dbei.eventType != EVENTTYPE_AUTHREQUEST)
return NULL;
DB::AUTH_BLOB blob(dbei.pBlob);
return AddContact(blob.get_email(), blob.get_nick());
}
int CTeamsProto::Authorize(MEVENT hDbEvent)
{
MCONTACT hContact = GetContactFromAuthEvent(hDbEvent);
if (hContact == INVALID_CONTACT_ID)
return 1;
PushRequest(new AsyncHttpRequest(REQUEST_POST, HOST_CONTACTS, "/users/SELF/invites/" + mir_urlEncode(getId(hContact)) + "/accept"));
return 0;
}
int CTeamsProto::AuthDeny(MEVENT hDbEvent, const wchar_t *)
{
MCONTACT hContact = GetContactFromAuthEvent(hDbEvent);
if (hContact == INVALID_CONTACT_ID)
return 1;
PushRequest(new AsyncHttpRequest(REQUEST_POST, HOST_CONTACTS, "/users/SELF/invites/" + mir_urlEncode(getId(hContact)) + "/decline"));
return 0;
}
int CTeamsProto::AuthRecv(MCONTACT, DB::EventInfo &dbei)
{
return Proto_AuthRecv(m_szModuleName, dbei);
}
int CTeamsProto::AuthRequest(MCONTACT hContact, const wchar_t *szMessage)
{
if (hContact == INVALID_CONTACT_ID)
return 1;
auto *pReq = new AsyncHttpRequest(REQUEST_PUT, HOST_CONTACTS, "/users/SELF/contacts");
JSONNode node;
node << CHAR_PARAM("mri", getId(hContact));
if (mir_wstrlen(szMessage))
node << WCHAR_PARAM("greeting", szMessage);
pReq->m_szParam = node.write().c_str();
PushRequest(pReq);
return 0;
}
int CTeamsProto::GetInfo(MCONTACT hContact, int)
{
if (isChatRoom(hContact))
return 1;
GetProfileInfo(hContact);
return 0;
}
int CTeamsProto::SendMsg(MCONTACT hContact, MEVENT, const char *szMessage)
{
return SendServerMsg(hContact, szMessage);
}
int CTeamsProto::SetStatus(int iNewStatus)
{
if (iNewStatus == m_iDesiredStatus)
return 0;
debugLogA(__FUNCTION__ ": changing status from %i to %i", m_iStatus, iNewStatus);
int old_status = m_iStatus;
m_iDesiredStatus = iNewStatus;
if (iNewStatus == ID_STATUS_OFFLINE) {
m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE;
StopQueue();
StopTrouter();
ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, ID_STATUS_OFFLINE);
if (!Miranda_IsTerminated())
setAllContactStatuses(ID_STATUS_OFFLINE, false);
return 0;
}
if (m_iStatus == ID_STATUS_OFFLINE)
Login();
else
SetServerStatus(m_iDesiredStatus);
return 0;
}
int CTeamsProto::UserIsTyping(MCONTACT hContact, int iState)
{
JSONNode node;
node << INT64_PARAM("clientmessageid", getRandomId()) << CHAR_PARAM("contenttype", "Application/Message") << CHAR_PARAM("content", "")
<< CHAR_PARAM("messagetype", (iState == PROTOTYPE_SELFTYPING_ON) ? "Control/Typing" : "Control/ClearTyping");
auto *pReq = new AsyncHttpRequest(REQUEST_POST, HOST_CHATS, "/users/ME/conversations/" + mir_urlEncode(getId(hContact)) + "/messages");
pReq->m_szParam = node.write().c_str();
PushRequest(pReq);
return 0;
}
int CTeamsProto::RecvContacts(MCONTACT hContact, DB::EventInfo &dbei)
{
PROTOSEARCHRESULT **isrList = (PROTOSEARCHRESULT **)dbei.pBlob;
int nCount = dbei.cbBlob;
uint32_t cbBlob = 0;
for (int i = 0; i < nCount; i++)
cbBlob += int(/*mir_wstrlen(isrList[i]->nick.w)*/0 + 2 + mir_wstrlen(isrList[i]->id.w));
char *pBlob = (char *)mir_calloc(cbBlob);
char *pCurBlob = pBlob;
for (int i = 0; i < nCount; i++) {
pCurBlob += mir_strlen(pCurBlob) + 1;
mir_strcpy(pCurBlob, _T2A(isrList[i]->id.w));
pCurBlob += mir_strlen(pCurBlob) + 1;
}
dbei.szModule = m_szModuleName;
dbei.eventType = EVENTTYPE_CONTACTS;
dbei.cbBlob = cbBlob;
dbei.pBlob = pBlob;
db_event_add(hContact, &dbei);
mir_free(pBlob);
return 0;
}
|