From 5fa0e82c661d63628cca04a793e8c8b1aaffa685 Mon Sep 17 00:00:00 2001 From: Dirk Kok Date: Sun, 23 Jun 2024 19:15:15 +0200 Subject: [PATCH] make case-insensitivity of regexes optional --- HighlightBot/CommandSession.cs | 32 +++- HighlightBot/Data/HighlightDbContext.cs | 5 + HighlightBot/Data/HighlightTerm.cs | 1 + .../20240623171436_RegexOptions.Designer.cs | 175 ++++++++++++++++++ .../Migrations/20240623171436_RegexOptions.cs | 26 +++ .../HighlightDbContextModelSnapshot.cs | 5 + HighlightBot/Program.cs | 7 +- 7 files changed, 242 insertions(+), 9 deletions(-) create mode 100644 HighlightBot/Migrations/20240623171436_RegexOptions.Designer.cs create mode 100644 HighlightBot/Migrations/20240623171436_RegexOptions.cs diff --git a/HighlightBot/CommandSession.cs b/HighlightBot/CommandSession.cs index acd99ca..33a1a1c 100644 --- a/HighlightBot/CommandSession.cs +++ b/HighlightBot/CommandSession.cs @@ -111,21 +111,42 @@ public async Task ClearHighlights(HighlightCommandContext hcc) { } public async Task AddHighlight(HighlightCommandContext hcc, ICollection terms) { + static bool IsRegex(string term, out string regex, out string suffix) { + if (term.StartsWith("/")) { + if (term.EndsWith("/")) { + suffix = ""; + regex = term[1..^1]; + return true; + } else if (term.EndsWith("/c")) { + suffix = "c"; + regex = term[1..^2]; + return true; + } + } + + suffix = ""; + regex = term; + return false; + } + HighlightUser user = await GetOrCreateUserAsync(hcc); int added = 0; foreach (string term in terms) { - string pattern; string display; - if (term.StartsWith('/') && term.EndsWith('/')) { - pattern = term[1..^1]; - + RegexOptions regexOptions = RegexOptions.IgnoreCase; + + if (IsRegex(term, out string pattern, out string suffix)) { if (!Util.IsValidRegex(pattern)) { await hcc.RespondAsync("Invalid regex 🙁"); return; } display = $"`{term}`"; + + if (suffix == "c") { + regexOptions = RegexOptions.None; + } } else { pattern = $@"\b{Regex.Escape(term.ToLower())}\b"; @@ -139,7 +160,8 @@ public async Task AddHighlight(HighlightCommandContext hcc, ICollection added++; user.Terms.Add(new HighlightTerm() { Display = display, - Regex = pattern + Regex = pattern, + RegexOptions = regexOptions, }); } } diff --git a/HighlightBot/Data/HighlightDbContext.cs b/HighlightBot/Data/HighlightDbContext.cs index cdd7c0f..740ee0b 100644 --- a/HighlightBot/Data/HighlightDbContext.cs +++ b/HighlightBot/Data/HighlightDbContext.cs @@ -1,3 +1,4 @@ +using System.Text.RegularExpressions; using Microsoft.EntityFrameworkCore; namespace HighlightBot; @@ -33,5 +34,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder .Entity() .HasKey(nameof(HighlightUser.DiscordGuildId), nameof(HighlightUser.DiscordUserId)); + + modelBuilder.Entity() + .Property(term => term.RegexOptions) + .HasDefaultValue(RegexOptions.IgnoreCase); } } diff --git a/HighlightBot/Data/HighlightTerm.cs b/HighlightBot/Data/HighlightTerm.cs index 05da3a0..0b800e1 100644 --- a/HighlightBot/Data/HighlightTerm.cs +++ b/HighlightBot/Data/HighlightTerm.cs @@ -8,4 +8,5 @@ public class HighlightTerm { public string Regex { get; set; } public string Display { get; set; } + public RegexOptions RegexOptions { get; set; } } diff --git a/HighlightBot/Migrations/20240623171436_RegexOptions.Designer.cs b/HighlightBot/Migrations/20240623171436_RegexOptions.Designer.cs new file mode 100644 index 0000000..9ea3e22 --- /dev/null +++ b/HighlightBot/Migrations/20240623171436_RegexOptions.Designer.cs @@ -0,0 +1,175 @@ +// +using System; +using HighlightBot; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace HighlightBot.Migrations +{ + [DbContext(typeof(HighlightDbContext))] + [Migration("20240623171436_RegexOptions")] + partial class RegexOptions + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("HighlightBot.HighlightTerm", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Display") + .IsRequired() + .HasColumnType("text"); + + b.Property("Regex") + .IsRequired() + .HasColumnType("text"); + + b.Property("RegexOptions") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasDefaultValue(1); + + b.Property("user_serverid") + .HasColumnType("numeric(20,0)"); + + b.Property("user_userid") + .HasColumnType("numeric(20,0)"); + + b.HasKey("Id"); + + b.HasIndex("user_serverid", "user_userid"); + + b.ToTable("Terms"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUser", b => + { + b.Property("DiscordGuildId") + .HasColumnType("numeric(20,0)"); + + b.Property("DiscordUserId") + .HasColumnType("numeric(20,0)"); + + b.Property("HighlightDelay") + .HasColumnType("interval"); + + b.Property("IgnoreBots") + .HasColumnType("boolean"); + + b.Property("IgnoreNsfw") + .HasColumnType("boolean"); + + b.Property("LastActivity") + .HasColumnType("timestamp with time zone"); + + b.Property("LastDM") + .HasColumnType("timestamp with time zone"); + + b.HasKey("DiscordGuildId", "DiscordUserId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUserIgnoredChannel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ChannelId") + .HasColumnType("numeric(20,0)"); + + b.Property("UserDiscordGuildId") + .HasColumnType("numeric(20,0)"); + + b.Property("UserDiscordUserId") + .HasColumnType("numeric(20,0)"); + + b.HasKey("Id"); + + b.HasIndex("UserDiscordGuildId", "UserDiscordUserId"); + + b.ToTable("HighlightUserIgnoredChannel"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUserIgnoredUser", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("IgnoredUserId") + .HasColumnType("numeric(20,0)"); + + b.Property("UserDiscordGuildId") + .HasColumnType("numeric(20,0)"); + + b.Property("UserDiscordUserId") + .HasColumnType("numeric(20,0)"); + + b.HasKey("Id"); + + b.HasIndex("UserDiscordGuildId", "UserDiscordUserId"); + + b.ToTable("HighlightUserIgnoredUser"); + }); + + modelBuilder.Entity("HighlightBot.HighlightTerm", b => + { + b.HasOne("HighlightBot.HighlightUser", "User") + .WithMany("Terms") + .HasForeignKey("user_serverid", "user_userid") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUserIgnoredChannel", b => + { + b.HasOne("HighlightBot.HighlightUser", "User") + .WithMany("IgnoredChannels") + .HasForeignKey("UserDiscordGuildId", "UserDiscordUserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUserIgnoredUser", b => + { + b.HasOne("HighlightBot.HighlightUser", "User") + .WithMany("IgnoredUsers") + .HasForeignKey("UserDiscordGuildId", "UserDiscordUserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("HighlightBot.HighlightUser", b => + { + b.Navigation("IgnoredChannels"); + + b.Navigation("IgnoredUsers"); + + b.Navigation("Terms"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/HighlightBot/Migrations/20240623171436_RegexOptions.cs b/HighlightBot/Migrations/20240623171436_RegexOptions.cs new file mode 100644 index 0000000..b2ec7db --- /dev/null +++ b/HighlightBot/Migrations/20240623171436_RegexOptions.cs @@ -0,0 +1,26 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace HighlightBot.Migrations +{ + public partial class RegexOptions : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "RegexOptions", + table: "Terms", + type: "integer", + nullable: false, + defaultValue: 1); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "RegexOptions", + table: "Terms"); + } + } +} diff --git a/HighlightBot/Migrations/HighlightDbContextModelSnapshot.cs b/HighlightBot/Migrations/HighlightDbContextModelSnapshot.cs index 286388a..7a0c07a 100644 --- a/HighlightBot/Migrations/HighlightDbContextModelSnapshot.cs +++ b/HighlightBot/Migrations/HighlightDbContextModelSnapshot.cs @@ -36,6 +36,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("text"); + b.Property("RegexOptions") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasDefaultValue(1); + b.Property("user_serverid") .HasColumnType("numeric(20,0)"); diff --git a/HighlightBot/Program.cs b/HighlightBot/Program.cs index 6c2efc2..ac8b6e1 100644 --- a/HighlightBot/Program.cs +++ b/HighlightBot/Program.cs @@ -176,12 +176,11 @@ private static Task OnMessageCreatedAsync(DiscordClient discord, MessageCreateEv term.User.DiscordUserId, term.User.DiscordGuildId, term.Regex, - term.Display + term.Display, + term.RegexOptions, }) .AsEnumerable() - // TODO TEMPORARY HACK! this does not scale. - // Find a way to get PCRE regexes going in the database (postgres seems to use POSIX regexes) - .Where(term => Regex.IsMatch(content, term.Regex, RegexOptions.IgnoreCase)) + .Where(term => Regex.IsMatch(content, term.Regex, term.RegexOptions)) .Select(term => new UserIdAndTerm() { DiscordUserId = term.DiscordUserId, Value = term.Display