Fix denial of service bug, empty questions handling and images handling

This commit is contained in:
Marco Realacci 2024-01-22 17:35:01 +01:00
parent 5b626f09a4
commit 2b5339ec92
2 changed files with 81 additions and 63 deletions

View file

@ -1,4 +1,5 @@
using SoUnBot.AccessControl; using System.Collections;
using SoUnBot.AccessControl;
using SoUnBot.ModuleLoader; using SoUnBot.ModuleLoader;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types; using Telegram.Bot.Types;
@ -72,6 +73,7 @@ namespace SoUnBot.Modules.OttoLinux
} }
} }
_questions.Add(cur); _questions.Add(cur);
SanitizeQuestions();
} }
private void LoadQuestionsV2() private void LoadQuestionsV2()
@ -99,6 +101,31 @@ namespace SoUnBot.Modules.OttoLinux
} }
_questions.Add(cur); _questions.Add(cur);
} }
SanitizeQuestions();
}
private void SanitizeQuestions()
{
var invalidQuestions = new List<Question>();
foreach (var qst in _questions)
{
while (qst.Quest.StartsWith("\n")) qst.Quest = qst.Quest.Substring(1);
for (int i = 0; i < qst.Answers.Count; i++)
{
while (qst.Answers[i].StartsWith("\n")) qst.Answers[i] = qst.Answers[i].Substring(1);
}
if (qst.Quest == "")
{
invalidQuestions.Add(qst);
Console.WriteLine("an empty question was found, skipping it");
}
else if(qst.Answers.Count == 0)
{
invalidQuestions.Add(qst);
Console.WriteLine($"The following question: {qst.Quest} \nhas no answers, skipping it");
}
}
_questions = _questions.Except(invalidQuestions).ToList();
} }
public Question PickRandomQuestion(long player, ITelegramBotClient botClient) public Question PickRandomQuestion(long player, ITelegramBotClient botClient)
@ -107,9 +134,11 @@ namespace SoUnBot.Modules.OttoLinux
//w.Method = "GET"; //w.Method = "GET";
//var number = int.Parse(new StreamReader(w.GetResponse().GetResponseStream()).ReadToEnd()); //var number = int.Parse(new StreamReader(w.GetResponse().GetResponseStream()).ReadToEnd());
var number = _rng.Next(1, _questions.Count - 1); var number = _rng.Next(0, _questions.Count - 1);
while (_questions[number].Quest == "") number = _rng.Next(1, _questions.Count - 1); while (_questions[number].Quest == "")
{
number = _rng.Next(0, _questions.Count - 1);
}
if (!_playedQuestions.ContainsKey(player)) _playedQuestions.Add(player, new List<int>()); if (!_playedQuestions.ContainsKey(player)) _playedQuestions.Add(player, new List<int>());
@ -185,8 +214,7 @@ namespace SoUnBot.Modules.OttoLinux
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: $"✅ Risposte indovinate {item} volte:\n{msg}", text: $"✅ Risposte indovinate {item} volte:\n{msg}");
cancellationToken: cancellationToken);
c++; c++;
} }
@ -209,8 +237,7 @@ namespace SoUnBot.Modules.OttoLinux
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: $"❌ Risposte sbagliate {item} volte:\n{msg}", text: $"❌ Risposte sbagliate {item} volte:\n{msg}");
cancellationToken: cancellationToken);
c++; c++;
} }
@ -233,8 +260,7 @@ namespace SoUnBot.Modules.OttoLinux
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: $"🟡 Risposte non date {item} volte:\n{msg}", text: $"🟡 Risposte non date {item} volte:\n{msg}");
cancellationToken: cancellationToken);
c++; c++;
} }
@ -247,15 +273,13 @@ namespace SoUnBot.Modules.OttoLinux
{ {
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: "❌ Non c'è niente da eliminare!", text: "❌ Non c'è niente da eliminare!");
cancellationToken: cancellationToken);
return; return;
} }
_playedQuestions[uid].Clear(); _playedQuestions[uid].Clear();
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: "✅ Memoria eliminata!", text: "✅ Memoria eliminata!");
cancellationToken: cancellationToken);
return; return;
} }
@ -278,8 +302,7 @@ namespace SoUnBot.Modules.OttoLinux
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: wrongMsg, text: wrongMsg
cancellationToken: cancellationToken
); );
SendRandomQuestion(uid, botClient, cancellationToken); SendRandomQuestion(uid, botClient, cancellationToken);
@ -293,12 +316,10 @@ namespace SoUnBot.Modules.OttoLinux
{ {
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: "❓ scusa, non ho capito 😭", text: "❓ scusa, non ho capito 😭");
cancellationToken: cancellationToken);
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: "⭕️ per uscire da 8linux, scrivi /leave", text: "⭕️ per uscire da 8linux, scrivi /leave");
cancellationToken: cancellationToken);
return; return;
} }
pick -= 1; pick -= 1;
@ -308,8 +329,7 @@ namespace SoUnBot.Modules.OttoLinux
{ {
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: "✅ Risposta esatta!", text: "✅ Risposta esatta!");
cancellationToken: cancellationToken);
_scores[uid].Correct += 1; _scores[uid].Correct += 1;
_questionStats[cur].Correct += 1; _questionStats[cur].Correct += 1;
@ -318,12 +338,10 @@ namespace SoUnBot.Modules.OttoLinux
{ {
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: "❌ Risposta errata!", text: "❌ Risposta errata!");
cancellationToken: cancellationToken);
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: wrongMsg, text: wrongMsg);
cancellationToken: cancellationToken);
_scores[uid].Wrong += 1; _scores[uid].Wrong += 1;
_questionStats[cur].Wrong += 1; _questionStats[cur].Wrong += 1;
@ -339,30 +357,24 @@ namespace SoUnBot.Modules.OttoLinux
try try
{ {
if (qst.Quest.StartsWith("img=")) if (qst.Quest.Length <= 40)
Console.WriteLine("Sto inviando la domanda " + qst.Quest.Substring(qst.Quest.IndexOf("\n"), 7) + {
" a " + uid); Console.WriteLine("Sto inviando la domanda " + qst.Quest + " a " + uid);
else Console.WriteLine("Sto inviando la domanda " + qst.Quest.Substring(0, 7) + " a " + uid); }
else
{
Console.WriteLine("Sto inviando la domanda " + qst.Quest.Substring(0, 40) + " a " + uid);
}
} }
catch(Exception e) catch(Exception e)
{ {
botClient.SendTextMessageAsync( botClient.SendTextMessageAsync(
chatId: _accessManager.AdminId, chatId: _accessManager.AdminId,
text: $"Question is malformed -> {qst.Quest} \n" + e.Message text: $"Question is malformed -> {qst.Quest} \n {e.Message}"
); );
return; return;
} }
while (qst.Answers.Count == 0)
{
qst = PickRandomQuestion(uid, botClient);
botClient.SendTextMessageAsync(
chatId: _accessManager.AdminId,
text: $"DOMANDA SENZA RISPOSTE -> {qst.Quest}"
);
}
if (!_questionStats.ContainsKey(qst)) _questionStats.Add(qst, new OttoScore()); if (!_questionStats.ContainsKey(qst)) _questionStats.Add(qst, new OttoScore());
if (_playingQuestions.ContainsKey(uid)) _playingQuestions[uid] = qst; if (_playingQuestions.ContainsKey(uid)) _playingQuestions[uid] = qst;
@ -484,10 +496,9 @@ namespace SoUnBot.Modules.OttoLinux
var total = stats.Correct + stats.Wrong + stats.Blank; var total = stats.Correct + stats.Wrong + stats.Blank;
Message sentMessage = await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: uid, chatId: uid,
text: stats.Correct + " corrette (" + ((float)stats.Correct / (float)total) * 100f + "%)\n" + stats.Wrong + " errate (" + ((float)stats.Wrong / (float)total) * 100f + "%)\n" + stats.Blank + " non date (" + ((float)stats.Blank / (float)total) * 100f + "%)\n", text: stats.Correct + " corrette (" + ((float)stats.Correct / (float)total) * 100f + "%)\n" + stats.Wrong + " errate (" + ((float)stats.Wrong / (float)total) * 100f + "%)\n" + stats.Blank + " non date (" + ((float)stats.Blank / (float)total) * 100f + "%)\n");
cancellationToken: cancellationToken);
} }
public string GetName() public string GetName()
@ -495,11 +506,6 @@ namespace SoUnBot.Modules.OttoLinux
return _name; return _name;
} }
public void ProcessUpdate(ITelegramBotClient botClient, global::Telegram.Bot.Types.Update update, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public List<Question> GetQuestions() public List<Question> GetQuestions()
{ {
return _questions; return _questions;

View file

@ -50,7 +50,9 @@ namespace SoUnBot.Telegram
async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{ {
long chatId; try
{
long chatId;
if (update.Type == UpdateType.CallbackQuery) if (update.Type == UpdateType.CallbackQuery)
{ {
@ -105,8 +107,7 @@ namespace SoUnBot.Telegram
await botClient.SendTextMessageAsync( await botClient.SendTextMessageAsync(
chatId: chatId, chatId: chatId,
text: "Invio annuncio in corso...", text: "Invio annuncio in corso...");
cancellationToken: cancellationToken);
new Thread(() => new Thread(() =>
{ {
Thread.CurrentThread.IsBackground = true; Thread.CurrentThread.IsBackground = true;
@ -144,21 +145,32 @@ namespace SoUnBot.Telegram
// Echo received message text // Echo received message text
Message sentMessage = await botClient.SendTextMessageAsync( Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId, chatId: chatId,
text: motd, text: motd);
cancellationToken: cancellationToken); }
catch(Exception e)
{
Console.WriteLine("Error handling the update: " + e.Message);
}
} }
Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{ {
var ErrorMessage = exception switch if (exception is ApiRequestException apiRequestException)
{ {
ApiRequestException apiRequestException await botClient.SendTextMessageAsync(_accessManager.AdminId, apiRequestException.ToString());
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}", }
_ => exception.ToString()
};
Console.WriteLine(ErrorMessage); // Restart the bot (otherwise it would become an amoeba)
return Task.CompletedTask; using var cts = new CancellationTokenSource();
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = { }
};
BotClient.StartReceiving(
HandleUpdateAsync,
HandleErrorAsync,
receiverOptions,
cancellationToken: cts.Token);
} }
private async void SendToEveryone(ITelegramBotClient botClient, long chatId, string text) private async void SendToEveryone(ITelegramBotClient botClient, long chatId, string text)
@ -172,7 +184,7 @@ namespace SoUnBot.Telegram
chatId: user, chatId: user,
text: text text: text
); );
await Task.Delay(500); await Task.Delay(100);
} }
catch catch
{ {