summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/chats.cpp
blob: 6ebb94073f14478e2d1ad33a0e68148979766404 (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
/*

WhatsApp plugin for Miranda NG
Copyright © 2019-22 George Hazan

*/

#include "stdafx.h"

void WhatsAppProto::InitChat(WAUser *pUser)
{
	CMStringA jid = "";
	CMStringW wszId(Utf2T(jid.c_str())), wszNick(Utf2T(""));

	setWString(pUser->hContact, "Nick", wszNick);

	pUser->si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, wszId, wszNick);

	Chat_AddGroup(pUser->si, TranslateT("Owner"));
	Chat_AddGroup(pUser->si, TranslateT("SuperAdmin"));
	Chat_AddGroup(pUser->si, TranslateT("Admin"));
	Chat_AddGroup(pUser->si, TranslateT("Participant"));

	Chat_Control(m_szModuleName, wszId, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE);
	Chat_Control(m_szModuleName, wszId, SESSION_ONLINE);

	if (!pUser->bInited) {
		// CMStringA query(FORMAT, "[\"query\",\"GroupMetadata\",\"%s\"]", jid.c_str());
		// WSSend(query, &WhatsAppProto::OnGetChatInfo, pUser);
	}
}

void WhatsAppProto::OnGetChatInfo(const JSONNode &root, void *param)
{
	auto *pChatUser = (WAUser *)param;
	pChatUser->bInited = true;

	CMStringW wszOwner(root["owner"].as_mstring()), wszNick;

	for (auto &it : root["participants"]) {
		CMStringW jid(it["id"].as_mstring());
		CMStringA szJid(jid);

		GCEVENT gce = { m_szModuleName, 0, GC_EVENT_JOIN };
		gce.pszID.w = pChatUser->si->ptszID;
		gce.pszUID.w = jid;
		gce.bIsMe = (szJid == m_szJid);

		if (jid == wszOwner)
			gce.pszStatus.w = L"Owner";
		else if (it["isSuperAdmin"].as_bool())
			gce.pszStatus.w = L"SuperAdmin";
		else if (it["isAdmin"].as_bool())
			gce.pszStatus.w = L"Admin";
		else
			gce.pszStatus.w = L"Participant";

		if (gce.bIsMe)
			wszNick = getMStringW(DBKEY_NICK);
		else if (auto *pUser = FindUser(szJid))
			wszNick = Clist_GetContactDisplayName(pUser->hContact);
		else {
			int iPos = jid.Find('@');
			wszNick = (iPos != -1) ? jid.Left(iPos - 1) : jid;
		}

		gce.pszNick.w = wszNick;
		Chat_Event(&gce);
	}

	CMStringW wszSubject(root["subject"].as_mstring());
	if (!wszSubject.IsEmpty()) {
		time_t iSubjectTime(root["subjectTime"].as_int());
		CMStringW wszSubjectSet(root["subjectOwner"].as_mstring());

		GCEVENT gce = { m_szModuleName, 0, GC_EVENT_TOPIC };
		gce.pszID.w	 = pChatUser->si->ptszID;
		gce.pszUID.w = wszSubjectSet;
		gce.pszText.w = wszSubject;
		gce.time = iSubjectTime;
		Chat_Event(&gce);
	}
}