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

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

*/

#include "stdafx.h"

void WhatsAppProto::GC_RefreshMetadata()
{
	for (auto &it : m_arUsers) {
		if (it->bIsGroupChat) {
			GC_GetAllMetadata();
			break;
		}
	}
}

void WhatsAppProto::GC_GetAllMetadata()
{
	WANodeIq iq(IQ::GET, "w:g2", "@g.us");
	auto *pRoot = iq.addChild("participating");
	*pRoot << XCHILD("participants") << XCHILD("description");
	WSSendNode(iq, &WhatsAppProto::OnIqGcGetAllMetadata);
}

void WhatsAppProto::OnIqGcGetAllMetadata(const WANode &node)
{
	if (auto *pGroup = node.getChild("groups"))
		for (auto &it : pGroup->getChildren())
			GC_ParseMetadata(it);	
}

void WhatsAppProto::GC_ParseMetadata(const WANode *pGroup)
{
	auto *pszId = pGroup->getAttr("id");
	if (pszId == nullptr)
		return;

	auto *pChatUser = AddUser(CMStringA(pszId) + "@g.us", false);
	if (pChatUser == nullptr)
		return;

	CMStringW wszId(Utf2T(pChatUser->szId));

	auto *si = pChatUser->si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, wszId, getMStringW(pChatUser->hContact, "Nick"));

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

	CMStringA szOwner(pGroup->getAttr("creator")), szNick, szRole;

	for (auto &it : pGroup->getChildren()) {
		if (it->title == "description") {
			CMStringA szDescr = it->getBody();
			if (!szDescr.IsEmpty()) {
				GCEVENT gce = { si, GC_EVENT_INFORMATION };
				gce.dwFlags = GCEF_UTF8;
				gce.pszText.a = szDescr.c_str();
				Chat_Event(&gce);
			}
		}
		else if (it->title == "member_add_mode") {
			szRole = it->getBody();
		}
		else if (it->title == "participant") {
			auto *jid = it->getAttr("jid");

			// if role isn't specified, use the default one
			auto *role = it->getAttr("type");
			if (role == nullptr)
				role = szRole;

			GCEVENT gce = { si, GC_EVENT_JOIN };
			gce.dwFlags = GCEF_UTF8;
			gce.pszUID.a = jid;
			gce.bIsMe = (jid == m_szJid);

			if (jid == szOwner)
				gce.pszStatus.a = "Owner";
			else if (!mir_strcmp(role, "superadmin"))
				gce.pszStatus.a = "SuperAdmin";
			else if (!mir_strcmp(role, "adminadd"))
				gce.pszStatus.a = "Admin";
			else
				gce.pszStatus.a = "Participant";

			if (gce.bIsMe)
				szNick = ptrA(getUStringA(DBKEY_NICK));
			else if (auto *pUser = FindUser(jid))
				szNick = T2Utf(Clist_GetContactDisplayName(pUser->hContact)).get();
			else
				szNick = WAJid(jid).user;

			gce.pszNick.a = szNick;
			Chat_Event(&gce);
		}
	}

	if (auto *pszSubject = pGroup->getAttr("subject")) {
		time_t iSubjectTime = pGroup->getAttrInt("s_t");
		auto *pszUser = pGroup->getAttr("s_o");
		if (m_szJid == pszUser)
			szNick = ptrA(getUStringA(DBKEY_NICK));
		else if (auto *pUser = FindUser(pszUser))
			szNick = T2Utf(Clist_GetContactDisplayName(pUser->hContact)).get();
		else
			szNick = WAJid(pszUser).user;

		GCEVENT gce = { si, GC_EVENT_TOPIC };
		gce.dwFlags = GCEF_UTF8;
		gce.pszUID.a = pszUser;
		gce.pszText.a = pszSubject;
		gce.time = iSubjectTime;
		Chat_Event(&gce);

		setUString(pChatUser->hContact, "Nick", pszSubject);
	}

	pChatUser->bInited = true;
	Chat_Control(si, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE);
	Chat_Control(si, SESSION_ONLINE);
}

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

int WhatsAppProto::GcEventHook(WPARAM, LPARAM lParam)
{
	GCHOOK *gch = (GCHOOK*)lParam;
	if (gch == nullptr)
		return 0;

	if (mir_strcmpi(gch->si->pszModule, m_szModuleName))
		return 0;

	auto *pUser = FindUser(T2Utf(gch->si->ptszID));
	if (pUser == nullptr)
		return 0;

	switch (gch->iType) {
	case GC_USER_MESSAGE:
		if (gch->ptszText && mir_wstrlen(gch->ptszText) > 0) {
			rtrimw(gch->ptszText);
			SendTextMessage(pUser->szId, T2Utf(gch->ptszText));
		}
		break;

	case GC_USER_PRIVMESS:
		break;

	case GC_USER_LOGMENU:
		break;

	case GC_USER_NICKLISTMENU:
		break;
	}

	return 1;
}

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

int WhatsAppProto::GcMenuHook(WPARAM, LPARAM lParam)
{
	GCMENUITEMS* gcmi = (GCMENUITEMS*)lParam;
	if (gcmi == nullptr)
		return 0;

	if (mir_strcmpi(gcmi->pszModule, m_szModuleName))
		return 0;

	auto *pUser = FindUser(T2Utf(gcmi->pszID));
	if (pUser == nullptr)
		return 0;

	if (gcmi->Type == MENU_ON_LOG) {
	}
	else if (gcmi->Type == MENU_ON_NICKLIST) {
	}
	return 0;
}