From 6adcc52ea276b3dcfe3d12971a83534f802a1681 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Sun, 9 Jun 2024 14:19:04 +0200 Subject: [PATCH] Adding null/empty checks to some controls to prevent invalid behaviour --- TelegramBotBase/Controls/Hybrid/ButtonGrid.cs | 51 ++++++++++++------- .../Controls/Hybrid/CheckedButtonList.cs | 20 +++++++- .../Controls/Hybrid/TaggedButtonGrid.cs | 19 ++++++- TelegramBotBase/Controls/Inline/Label.cs | 8 ++- .../Controls/Inline/MultiToggleButton.cs | 24 ++++++++- .../Controls/Inline/ToggleButton.cs | 26 +++++++++- TelegramBotBase/Localizations/English.cs | 1 + TelegramBotBase/Localizations/German.cs | 1 + TelegramBotBase/Localizations/Persian.cs | 1 + TelegramBotBase/Localizations/Russian.cs | 1 + 10 files changed, 130 insertions(+), 22 deletions(-) diff --git a/TelegramBotBase/Controls/Hybrid/ButtonGrid.cs b/TelegramBotBase/Controls/Hybrid/ButtonGrid.cs index bc06b06..7b4dea8 100644 --- a/TelegramBotBase/Controls/Hybrid/ButtonGrid.cs +++ b/TelegramBotBase/Controls/Hybrid/ButtonGrid.cs @@ -51,7 +51,24 @@ public class ButtonGrid : ControlBase DataSource = new ButtonFormDataSource(form); } - public string Title { get; set; } = Default.Language["ButtonGrid_Title"]; + string m_Title = Default.Language["ButtonGrid_Title"]; + + public string Title + { + get + { + return m_Title; + } + set + { + if (string.IsNullOrEmpty(value)) + { + throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty"); + } + + m_Title = value; + } + } public string ConfirmationText { get; set; } = ""; @@ -340,14 +357,14 @@ public class ButtonGrid : ControlBase } - //var button = HeadLayoutButtonRow?. .FirstOrDefault(a => a.Text.Trim() == result.MessageText) - // ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText); + //var button = HeadLayoutButtonRow?. .FirstOrDefault(a => a.Text.Trim() == result.MessageText) + // ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText); - // bf.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText) + // bf.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText) - //var index = bf.FindRowByButton(button); + //var index = bf.FindRowByButton(button); - check: + check: //Remove button click message if (DeleteReplyMessage) @@ -449,15 +466,15 @@ public class ButtonGrid : ControlBase } - //var bf = DataSource.ButtonForm; + //var bf = DataSource.ButtonForm; - //var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData) - // ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData) - // ?? bf.ToList().FirstOrDefault(a => a.Value == result.RawData); + //var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData) + // ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData) + // ?? bf.ToList().FirstOrDefault(a => a.Value == result.RawData); - //var index = bf.FindRowByButton(button); + //var index = bf.FindRowByButton(button); - check: + check: if (match != null) { await result.ConfirmAction(ConfirmationText ?? ""); @@ -506,13 +523,13 @@ public class ButtonGrid : ControlBase if (DataSource.RowCount > Constants.Telegram.MaxInlineKeyBoardRows && !EnablePaging) { throw new MaximumRowsReachedException - { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxInlineKeyBoardRows }; + { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxInlineKeyBoardRows }; } if (DataSource.ColumnCount > Constants.Telegram.MaxInlineKeyBoardCols) { throw new MaximumColsException - { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxInlineKeyBoardCols }; + { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxInlineKeyBoardCols }; } break; @@ -522,13 +539,13 @@ public class ButtonGrid : ControlBase if (DataSource.RowCount > Constants.Telegram.MaxReplyKeyboardRows && !EnablePaging) { throw new MaximumRowsReachedException - { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxReplyKeyboardRows }; + { Value = DataSource.RowCount, Maximum = Constants.Telegram.MaxReplyKeyboardRows }; } if (DataSource.ColumnCount > Constants.Telegram.MaxReplyKeyboardCols) { throw new MaximumColsException - { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxReplyKeyboardCols }; + { Value = DataSource.ColumnCount, Maximum = Constants.Telegram.MaxReplyKeyboardCols }; } break; @@ -693,7 +710,7 @@ public class ButtonGrid : ControlBase Updated(); } else - //Remove event handler + //Remove event handler { Device.MessageDeleted -= Device_MessageDeleted; } diff --git a/TelegramBotBase/Controls/Hybrid/CheckedButtonList.cs b/TelegramBotBase/Controls/Hybrid/CheckedButtonList.cs index ed44fc0..83cc657 100644 --- a/TelegramBotBase/Controls/Hybrid/CheckedButtonList.cs +++ b/TelegramBotBase/Controls/Hybrid/CheckedButtonList.cs @@ -13,6 +13,7 @@ using TelegramBotBase.Enums; using TelegramBotBase.Exceptions; using TelegramBotBase.Form; using TelegramBotBase.Localizations; +using static System.Net.Mime.MediaTypeNames; using static TelegramBotBase.Base.Async; namespace TelegramBotBase.Controls.Hybrid; @@ -51,7 +52,24 @@ public class CheckedButtonList : ControlBase DataSource = new ButtonFormDataSource(form); } - public string Title { get; set; } = Default.Language["ButtonGrid_Title"]; + string m_Title = Default.Language["ButtonGrid_Title"]; + + public string Title + { + get + { + return m_Title; + } + set + { + if (string.IsNullOrEmpty(value)) + { + throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty"); + } + + m_Title = value; + } + } public string ConfirmationText { get; set; } = ""; diff --git a/TelegramBotBase/Controls/Hybrid/TaggedButtonGrid.cs b/TelegramBotBase/Controls/Hybrid/TaggedButtonGrid.cs index 94266b7..d7e1ee4 100644 --- a/TelegramBotBase/Controls/Hybrid/TaggedButtonGrid.cs +++ b/TelegramBotBase/Controls/Hybrid/TaggedButtonGrid.cs @@ -67,7 +67,24 @@ public class TaggedButtonGrid : MultiView DataSource = new ButtonFormDataSource(form); } - public string Title { get; set; } = Default.Language["ButtonGrid_Title"]; + string m_Title = Default.Language["ButtonGrid_Title"]; + + public string Title + { + get + { + return m_Title; + } + set + { + if (string.IsNullOrEmpty(value)) + { + throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty"); + } + + m_Title = value; + } + } public string ConfirmationText { get; set; } diff --git a/TelegramBotBase/Controls/Inline/Label.cs b/TelegramBotBase/Controls/Inline/Label.cs index 429e505..b327f94 100644 --- a/TelegramBotBase/Controls/Inline/Label.cs +++ b/TelegramBotBase/Controls/Inline/Label.cs @@ -16,7 +16,7 @@ public class Label : ControlBase { private bool _renderNecessary = true; - private string _text = string.Empty; + private string _text = Default.Language["Label_Text"]; public String Text { @@ -29,6 +29,10 @@ public class Label : ControlBase if (_text == value) return; + if (string.IsNullOrEmpty(value)) + { + throw new ArgumentNullException($"{nameof(Text)}", $"{nameof(Text)} property must have been a value unequal to null/empty"); + } _text = value; _renderNecessary = true; @@ -36,6 +40,8 @@ public class Label : ControlBase } } + + private ParseMode _parseMode = ParseMode.Markdown; public ParseMode ParseMode diff --git a/TelegramBotBase/Controls/Inline/MultiToggleButton.cs b/TelegramBotBase/Controls/Inline/MultiToggleButton.cs index 488a5f1..61fb6a7 100644 --- a/TelegramBotBase/Controls/Inline/MultiToggleButton.cs +++ b/TelegramBotBase/Controls/Inline/MultiToggleButton.cs @@ -32,10 +32,32 @@ public class MultiToggleButton : ControlBase /// public string ChangedString { get; set; } = Default.Language["MultiToggleButton_Changed"]; + private string _title = Default.Language["MultiToggleButton_Title"]; + /// /// This holds the title of the control. /// - public string Title { get; set; } = Default.Language["MultiToggleButton_Title"]; + public String Title + { + get + { + return _title; + } + set + { + if (_title == value) + return; + + if (string.IsNullOrEmpty(value)) + { + throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} must have been a value unequal to null/empty"); + } + + _title = value; + _renderNecessary = true; + + } + } public int? MessageId { get; set; } diff --git a/TelegramBotBase/Controls/Inline/ToggleButton.cs b/TelegramBotBase/Controls/Inline/ToggleButton.cs index 4c7811e..19c7cd2 100644 --- a/TelegramBotBase/Controls/Inline/ToggleButton.cs +++ b/TelegramBotBase/Controls/Inline/ToggleButton.cs @@ -36,7 +36,31 @@ public class ToggleButton : ControlBase public string ChangedString { get; set; } = Default.Language["ToggleButton_Changed"]; - public string Title { get; set; } = Default.Language["ToggleButton_Title"]; + private string _title = Default.Language["ToggleButton_Title"]; + + public String Title + { + get + { + return _title; + } + set + { + if (_title == value) + return; + + if (string.IsNullOrEmpty(value)) + { + throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty"); + } + + _title = value; + _renderNecessary = true; + + } + } + + public int? MessageId { get; set; } diff --git a/TelegramBotBase/Localizations/English.cs b/TelegramBotBase/Localizations/English.cs index 974143b..527b427 100644 --- a/TelegramBotBase/Localizations/English.cs +++ b/TelegramBotBase/Localizations/English.cs @@ -34,5 +34,6 @@ public sealed class English : Localization Values["ToggleButton_Changed"] = "Setting changed"; Values["ButtonGrid_SearchIcon"] = "🔍"; Values["TaggedButtonGrid_TagIcon"] = "📁"; + Values["Label_Text"] = "Default label text"; } } diff --git a/TelegramBotBase/Localizations/German.cs b/TelegramBotBase/Localizations/German.cs index ffe99c4..0d0f362 100644 --- a/TelegramBotBase/Localizations/German.cs +++ b/TelegramBotBase/Localizations/German.cs @@ -34,5 +34,6 @@ public sealed class German : Localization Values["ToggleButton_Changed"] = "Einstellung geändert"; Values["ButtonGrid_SearchIcon"] = "🔍"; Values["TaggedButtonGrid_TagIcon"] = "📁"; + Values["Label_Text"] = "Standard Label Text"; } } \ No newline at end of file diff --git a/TelegramBotBase/Localizations/Persian.cs b/TelegramBotBase/Localizations/Persian.cs index 72c6b40..add3088 100644 --- a/TelegramBotBase/Localizations/Persian.cs +++ b/TelegramBotBase/Localizations/Persian.cs @@ -34,6 +34,7 @@ public sealed class Persian : Localization Values["ToggleButton_Changed"] = "تنظیمات تغییر کرد"; Values["ButtonGrid_SearchIcon"] = "🔍"; Values["TaggedButtonGrid_TagIcon"] = "📁"; + Values["Label_Text"] = "متن برچسب پیش‌فرض"; } } diff --git a/TelegramBotBase/Localizations/Russian.cs b/TelegramBotBase/Localizations/Russian.cs index 08315e6..eff54d3 100644 --- a/TelegramBotBase/Localizations/Russian.cs +++ b/TelegramBotBase/Localizations/Russian.cs @@ -34,5 +34,6 @@ public sealed class Russian : Localization Values["ToggleButton_Changed"] = "Настройки изменены"; Values["ButtonGrid_SearchIcon"] = "🔍"; Values["TaggedButtonGrid_TagIcon"] = "📁"; + Values["Label_Text"] = "Текст метки по умолчанию"; } } \ No newline at end of file