summaryrefslogtreecommitdiff
path: root/protocols/Telegram/src/groupchat.cpp
blob: b32db4c13fd7345e4e4cad9ece52dc902ee07548 (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
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
/*
Copyright (C) 2012-23 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 CTelegramProto::InitGroupChat(TG_USER *pUser, const TD::chat *pChat, bool bUpdateMembers)
{
	if (pUser->m_si)
		return;
		
	wchar_t wszId[100];
	_i64tow(pUser->id, wszId, 10);
	SESSION_INFO *si;

	if (bUpdateMembers) {
		si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, wszId, Utf2T(pChat->title_.c_str()), pUser);
		Chat_AddGroup(si, TranslateT("Creator"));
		Chat_AddGroup(si, TranslateT("Admin"));
		Chat_AddGroup(si, TranslateT("Participant"));

		// push async query to fetch users
		SendQuery(new TD::getBasicGroupFullInfo(pUser->id), &CTelegramProto::StartGroupChat, pUser);
	}
	else {
		si = Chat_NewSession(GCW_CHANNEL, m_szModuleName, wszId, Utf2T(pChat->title_.c_str()), pUser);
		Chat_AddGroup(si, TranslateT("SuperAdmin"));
		Chat_AddGroup(si, TranslateT("Visitor"));

		ptrW wszUserId(getWStringA(DBKEY_ID)), wszNick(Contact::GetInfo(CNF_DISPLAY, 0, m_szModuleName));

		GCEVENT gce = { si, GC_EVENT_JOIN };
		gce.pszUID.w = wszUserId;
		gce.pszNick.w = wszNick;
		gce.bIsMe = true;
		gce.pszStatus.w = TranslateT("Visitor");
		Chat_Event(&gce);

		gce.bIsMe = false;
		gce.pszUID.w = L"---";
		gce.pszNick.w = TranslateT("Admin");
		gce.pszStatus.w = TranslateT("SuperAdmin");
		Chat_Event(&gce);

		Chat_Control(si, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE);
		Chat_Control(si, SESSION_ONLINE);
	}
	
	pUser->m_si = si;
}

void CTelegramProto::StartGroupChat(td::ClientManager::Response &response, void *pUserData)
{
	if (!response.object)
		return;

	if (response.object->get_id() != TD::basicGroupFullInfo::ID) {
		debugLogA("Gotten class ID %d instead of %d, exiting", response.object->get_id(), TD::basicGroupFullInfo::ID);
		return;
	}

	auto *pInfo = ((TD::basicGroupFullInfo *)response.object.get());
	auto *pUser = (TG_USER *)pUserData;

	for (auto &it : pInfo->members_) {
		auto *pMember = it.get();
		const wchar_t *pwszRole;

		switch (pMember->status_->get_id()) {
		case TD::chatMemberStatusCreator::ID:
			pwszRole = TranslateT("Creator");
			break;
		case TD::chatMemberStatusAdministrator::ID:
			pwszRole = TranslateT("Admin");
			break;
		case TD::chatMemberStatusMember::ID:
		default:
			pwszRole = TranslateT("Participant");
			break;
		}

		if (pMember->member_id_->get_id() != TD::messageSenderUser::ID)
			continue;

		int64_t memberId = ((TD::messageSenderUser *)pMember->member_id_.get())->user_id_;
		auto *pChatUser = FindUser(memberId);
		if (pChatUser == nullptr)
			continue;

		wchar_t wszUserId[100];
		_i64tow(memberId, wszUserId, 10);

		GCEVENT gce = { pUser->m_si, GC_EVENT_JOIN };
		gce.pszUID.w = wszUserId;
		gce.pszStatus.w = pwszRole;

		switch (pChatUser->hContact) {
		case 0:
			gce.bIsMe = true;
			__fallthrough;

		case INVALID_CONTACT_ID:
			gce.pszNick.w = pChatUser->wszNick.c_str();
			break;

		default:
			gce.pszNick.w = Clist_GetContactDisplayName(pChatUser->hContact);
			break;
		}

		Chat_Event(&gce);
	}

	Chat_Control(pUser->m_si, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE);
	Chat_Control(pUser->m_si, SESSION_ONLINE);
}

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

enum
{
	IDM_LEAVE = 1,
};

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

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

	auto userId = _wtoi64(gch->si->ptszID);

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

	case GC_USER_PRIVMESS:
		Chat_SendPrivateMessage(gch);
		break;

	case GC_USER_LOGMENU:
		Chat_LogMenu(gch);
		break;

	case GC_USER_NICKLISTMENU:
		break;
	}

	return 1;
}

void CTelegramProto::Chat_LogMenu(GCHOOK *gch)
{
	switch (gch->dwData) {
	case IDM_LEAVE:
		int64_t id(_atoi64(getMStringA(gch->si->hContact, DBKEY_ID)));
		if (auto *pUser = FindUser(id)) {
			pUser->m_si = nullptr;
			SendQuery(new TD::leaveChat(pUser->chatId));
		}

		Chat_Terminate(gch->si);
		Contact::Hide(gch->si->hContact);
		Contact::RemoveFromList(gch->si->hContact);
		break;
	}
}

void CTelegramProto::Chat_SendPrivateMessage(GCHOOK *gch)
{
	MCONTACT hContact;
	TD::int53 userId = _wtoi64(gch->ptszUID);
	auto *pUser = FindUser(userId);
	if (pUser == nullptr || pUser->hContact == INVALID_CONTACT_ID) {
		PROTOSEARCHRESULT psr = { sizeof(psr) };
		psr.id.w = (wchar_t *)gch->ptszUID;
		psr.firstName.w = (wchar_t *)gch->ptszNick;

		hContact = AddToList(PALF_TEMPORARY, &psr);
		if (hContact == 0)
			return;

		setWString(hContact, "Nick", gch->ptszNick);
		Contact::Hide(hContact);
		db_set_dw(hContact, "Ignore", "Mask1", 0);
	}
	else hContact = pUser->hContact;

	CallService(MS_MSG_SENDMESSAGE, hContact, 0);
}

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

static gc_item sttLogListItems[] =
{
	{ LPGENW("&Leave chat session"), IDM_LEAVE, MENU_ITEM }
};

int CTelegramProto::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(_wtoi64(gcmi->pszID));
	if (pUser == nullptr)
		return 0;

	if (gcmi->Type == MENU_ON_LOG) {
		Chat_AddMenuItems(gcmi->hMenu, _countof(sttLogListItems), sttLogListItems, &g_plugin);
	}
	else if (gcmi->Type == MENU_ON_NICKLIST) {
	}
	return 0;
}