-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandlers.cs
380 lines (346 loc) · 15.5 KB
/
Handlers.cs
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* unified/ban - Management and protection systems
© fabricators SRL, https://fabricators.ltd , https://unifiedban.solutions
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License with our addition
to Section 7 as published in unified/ban's the GitHub repository.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License and the
additional terms along with this program.
If not, see <https://docs.fabricators.ltd/docs/licenses/unifiedban>.
For more information, see Licensing FAQ:
https://docs.fabricators.ltd/docs/licenses/faq */
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Unifiedban.Next.Common;
using Unifiedban.Next.Common.Telegram;
using Unifiedban.Next.Models.Telegram;
namespace Unifiedban.Next.Terminal.Telegram;
internal partial class TelegramManager
{
private void HandleMessage(Message message)
{
var isGroup = message.Chat.Type is ChatType.Group or ChatType.Supergroup;
if (isGroup && !CacheData.Chats.ContainsKey(message.Chat.Id))
{
if (bool.Parse(CacheData.Configuration?["Telegram:V3ChatMigration"] ?? "false") &&
CacheData.V3Chats.Contains(message.Chat.Id))
{
Task.Run(() => MigrateFromV3(message));
return;
}
lock (_regInProgObject)
{
if (!_registrationInProgress.ContainsKey(message.Chat.Id))
{
_registrationInProgress.Add(message.Chat.Id, new List<Message>());
Common.Utils.WriteLine($"Received message for not known chat {message.Chat.Id} (going to register)", 2);
RegisterNewChat(message);
}
_registrationInProgress[message.Chat.Id].Add(message);
}
return;
}
lock (_regInProgObject)
{
if (_registrationInProgress.ContainsKey(message.Chat.Id))
{
_registrationInProgress[message.Chat.Id].Add(message);
return;
}
}
if (isGroup && CacheData.Chats[message.Chat.Id].Status != Enums.ChatStates.Active)
{
#if DEBUG
Common.Utils.WriteLine($"Received message from chat with status {CacheData.Chats[message.Chat.Id].Status.ToString()}", 0);
#endif
return;
}
switch (message.Type)
{
case MessageType.Text:
case MessageType.Photo:
case MessageType.Audio:
case MessageType.Video:
case MessageType.Voice:
case MessageType.Document:
case MessageType.Sticker:
Task.Run(() => HandleBaseMessage(message));
break;
case MessageType.ChatMembersAdded:
Task.Run(() => HandleMemberJoin(message));
break;
case MessageType.ChatMemberLeft:
Task.Run(() => HandleMemberLeft(message));
break;
case MessageType.ChatTitleChanged:
case MessageType.ChatPhotoChanged:
case MessageType.ChatPhotoDeleted:
case MessageType.MigratedToSupergroup:
case MessageType.MigratedFromGroup:
Task.Run(() => HandleChatDetailUpdate(message));
break;
case MessageType.GroupCreated:
case MessageType.SupergroupCreated:
case MessageType.ChannelCreated:
Task.Run(() => HandleChatCreation(message));
break;
case MessageType.Location:
case MessageType.Contact:
case MessageType.Venue:
case MessageType.Game:
case MessageType.VideoNote:
case MessageType.Invoice:
case MessageType.SuccessfulPayment:
case MessageType.WebsiteConnected:
case MessageType.WebAppData:
case MessageType.MessagePinned:
case MessageType.Poll:
case MessageType.Dice:
case MessageType.MessageAutoDeleteTimerChanged:
case MessageType.ProximityAlertTriggered:
case MessageType.VideoChatScheduled:
case MessageType.VideoChatStarted:
case MessageType.VideoChatEnded:
case MessageType.VideoChatParticipantsInvited:
return;
case MessageType.Unknown:
default:
Common.Utils.WriteLine($"Received message of Type {message.Type.ToString()} that has no handler", 2);
return;
}
}
private async void HandleUpdateMember(ChatMemberUpdated update)
{
if (!CacheData.Chats.ContainsKey(update.Chat.Id)) return;
if (update.NewChatMember.User.Id == _myId)
{
if (update.NewChatMember is ChatMemberAdministrator chatMemberAdministrator)
{
CacheData.BotPermissions[update.Chat.Id] = new UserPrivileges
{
CanManageChat = chatMemberAdministrator.CanManageChat,
CanPostMessages = chatMemberAdministrator.CanPostMessages ?? false,
CanEditMessages = chatMemberAdministrator.CanEditMessages ?? false,
CanDeleteMessages = chatMemberAdministrator.CanDeleteMessages,
CanManageVoiceChats = chatMemberAdministrator.CanManageVoiceChats,
CanRestrictMembers = chatMemberAdministrator.CanRestrictMembers,
CanPromoteMembers = chatMemberAdministrator.CanPromoteMembers,
CanChangeInfo = chatMemberAdministrator.CanChangeInfo,
CanInviteUsers = chatMemberAdministrator.CanInviteUsers,
CanPinMessages = chatMemberAdministrator.CanPinMessages ?? false
};
#if DEBUG
await BotClient!.SendTextMessageAsync(
update.Chat!,
$"Telegram told me I'm a chat admin. CanPinMessages status is {CacheData.BotPermissions[update.Chat.Id].CanPinMessages}",
cancellationToken: Cts.Token
);
#endif
}
else
{
if (update.NewChatMember is not ChatMemberLeft)
{
#if DEBUG
await BotClient!.SendTextMessageAsync(
update.Chat!,
$"Telegram told me I'm not a chat admin.",
cancellationToken: Cts.Token
);
#endif
}
}
}
else
{
if(!CacheData.UserPermissions.ContainsKey(update.Chat.Id))
CacheData.UserPermissions[update.Chat.Id] = new Dictionary<long, UserPrivileges>();
if (update.NewChatMember is ChatMemberAdministrator chatMemberAdministrator)
{
CacheData.UserPermissions[update.Chat.Id][update.NewChatMember.User.Id] = new UserPrivileges
{
ChatId = update.Chat.Id,
UserId = update.NewChatMember.User.Id,
CanManageChat = chatMemberAdministrator.CanManageChat,
CanPostMessages = chatMemberAdministrator.CanPostMessages ?? false,
CanEditMessages = chatMemberAdministrator.CanEditMessages ?? false,
CanDeleteMessages = chatMemberAdministrator.CanDeleteMessages,
CanManageVoiceChats = chatMemberAdministrator.CanManageVoiceChats,
CanRestrictMembers = chatMemberAdministrator.CanRestrictMembers,
CanPromoteMembers = chatMemberAdministrator.CanPromoteMembers,
CanChangeInfo = chatMemberAdministrator.CanChangeInfo,
CanInviteUsers = chatMemberAdministrator.CanInviteUsers,
CanPinMessages = chatMemberAdministrator.CanPinMessages ?? false
};
#if DEBUG
await BotClient!.SendTextMessageAsync(
update.Chat!,
$"Telegram told me that {update.NewChatMember.User.Id} is chat admin. CanPinMessages status is {CacheData.BotPermissions[update.Chat.Id].CanPinMessages}",
cancellationToken: Cts.Token
);
#endif
}
else if (CacheData.UserPermissions[update.Chat.Id].ContainsKey(update.NewChatMember.User.Id))
CacheData.UserPermissions[update.Chat.Id].Remove(update.NewChatMember.User.Id);
}
}
private async void HandleCallbackQuery(CallbackQuery callbackQuery)
{
_totalMessages++;
Common.Utils.WriteLine("New callbackQuery: " + callbackQuery.Message?.Text, 0);
#if DEBUG
await BotClient!.SendTextMessageAsync(
callbackQuery.Message?.Chat!,
$"New callbackQuery: {callbackQuery.Message?.Text}",
cancellationToken: Cts.Token
);
#endif
}
private async void HandleJoinRequestAsync(ChatJoinRequest chatJoinRequest)
{
if (!CacheData.Chats.ContainsKey(chatJoinRequest.Chat.Id)) return;
#if DEBUG
await BotClient!.SendTextMessageAsync(
chatJoinRequest.Chat,
$"New ChatJoinRequest: {chatJoinRequest.From.Username ?? chatJoinRequest.From.Id.ToString()}",
cancellationToken: Cts.Token
);
#endif
}
private async void HandleBaseMessage(Message message)
{
if (!CacheData.Chats.ContainsKey(message.Chat.Id)) return;
if (message.From is null)
{
Common.Utils.WriteLine("Received message with null sender (message.From)", 2);
return;
}
if(message.Text is null)
{
Common.Utils.WriteLine("Received message with null text (message.Text)", 2);
return;
}
Common.Utils.WriteLine(message.EditDate is null ? $"New msg: {message.Text}" : $"Edited msg: {message.Text}", 0);
var botPrivileges = new UserPrivileges();
if (CacheData.BotPermissions.ContainsKey(message.Chat.Id))
botPrivileges = CacheData.BotPermissions[message.Chat.Id];
var sender = message.From!.Id;
if (message.SenderChat is not null)
sender = message.SenderChat.Id;
var queueMsg = new QueueMessage<TGChat, UserPrivileges, UserPrivileges, Message>
{
UBChat = CacheData.Chats[message.Chat.Id],
Platform = Enums.Platforms.Telegram,
Category = Enums.QueueMessageCategories.Base,
BotPermissions = botPrivileges,
UserPermissions = await GetUserPrivileges(message.Chat.Id, sender),
Payload = message
};
var json = JsonConvert.SerializeObject(queueMsg, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
var body = Encoding.UTF8.GetBytes(json);
if (message.Text.StartsWith(queueMsg.UBChat.CommandPrefix))
RabbitManager.PublishMessage("telegram", "cmd", body);
else
RabbitManager.PublishMessage(CacheData.MessageBaseQueue.Exchange, CacheData.MessageBaseQueue.RoutingKey, body);
}
private async void HandleMemberJoin(Message message)
{
if (!CacheData.Chats.ContainsKey(message.Chat.Id)) return;
var conf = CacheData.Chats[message.Chat.Id]
.GetConfigParam("DeleteSystemMessages", "false");
if (conf.Value.Equals("true"))
{
await BotClient!.DeleteMessageAsync(message.Chat.Id, message.MessageId);
}
var botPrivileges = new UserPrivileges();
if (CacheData.BotPermissions.ContainsKey(message.Chat.Id))
botPrivileges = CacheData.BotPermissions[message.Chat.Id];
foreach (var newChatMember in message.NewChatMembers!)
{
if (newChatMember.Id == _myId)
{
if (CacheData.Chats[message.Chat.Id].Status == Enums.ChatStates.Disabled)
{
CacheData.Chats[message.Chat.Id].Status = Enums.ChatStates.Active;
if (_tgChatLogic.Update(CacheData.Chats[message.Chat.Id]).StatusCode != 200)
{
// TODO - send to control chat
Common.Utils.WriteLine($"Error enabling chat {message.Chat.Id} on HandleUpdateMember", 3);
}
}
if (LoadChatPermissions(message.Chat.Id))
{
BotClient!.SendTextMessageAsync(message.Chat.Id, "Welcome to IC!").Wait();
}
continue;
}
var queueMsg = new QueueMessage<TGChat, UserPrivileges, UserPrivileges, Message>
{
UBChat = CacheData.Chats[message.Chat.Id],
Platform = Enums.Platforms.Telegram,
Category = Enums.QueueMessageCategories.MemberJoin,
BotPermissions = botPrivileges,
UserPermissions = await GetUserPrivileges(message.Chat.Id, newChatMember.Id),
Payload = message
};
var json = JsonConvert.SerializeObject(queueMsg, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
var body = Encoding.UTF8.GetBytes(json);
RabbitManager.PublishMessage(CacheData.MemberJoinQueue.Exchange, CacheData.MemberJoinQueue.RoutingKey, body);
}
}
private async void HandleMemberLeft(Message message)
{
if (!CacheData.Chats.ContainsKey(message.Chat.Id)) return;
var conf = CacheData.Chats[message.Chat.Id]
.GetConfigParam("DeleteSystemMessages", "false");
if (conf.Value.Equals("true"))
{
await BotClient!.DeleteMessageAsync(message.Chat.Id, message.MessageId);
}
if (message.LeftChatMember!.Id == _myId)
{
if (CacheData.BotPermissions.ContainsKey(message.Chat.Id))
CacheData.BotPermissions.Remove(message.Chat.Id);
CacheData.Chats[message.Chat.Id].Status = Enums.ChatStates.Disabled;
if (_tgChatLogic.Update(CacheData.Chats[message.Chat.Id]).StatusCode != 200)
{
// TODO - send to control chat
Common.Utils.WriteLine($"Error disabling chat {message.Chat.Id} on HandleUpdateMember", 3);
}
}
else
{
if (!CacheData.UserPermissions.ContainsKey(message.Chat.Id)) return;
if (CacheData.UserPermissions[message.Chat.Id].ContainsKey(message.LeftChatMember!.Id))
CacheData.UserPermissions[message.Chat.Id].Remove(message.LeftChatMember!.Id);
}
}
private async void HandleChatDetailUpdate(Message message)
{
if (!CacheData.Chats.ContainsKey(message.Chat.Id)) return;
// TODO - update chat details on database
Common.Utils.WriteLine($"Received chat update for {message.Chat.Id}", -1);
}
private async void HandleChatCreation(Message message)
{
Common.Utils.WriteLine($"Received chat update to create {message.Chat.Id}");
if (!CacheData.Chats.ContainsKey(message.Chat.Id))
{
RegisterNewChat(message);
}
}
}