Skip to content

Commit

Permalink
make case-insensitivity of regexes optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxite committed Jun 23, 2024
1 parent a9e07b1 commit 5fa0e82
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 9 deletions.
32 changes: 27 additions & 5 deletions HighlightBot/CommandSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,42 @@ public async Task ClearHighlights(HighlightCommandContext hcc) {
}

public async Task AddHighlight(HighlightCommandContext hcc, ICollection<string> 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";

Expand All @@ -139,7 +160,8 @@ public async Task AddHighlight(HighlightCommandContext hcc, ICollection<string>
added++;
user.Terms.Add(new HighlightTerm() {
Display = display,
Regex = pattern
Regex = pattern,
RegexOptions = regexOptions,
});
}
}
Expand Down
5 changes: 5 additions & 0 deletions HighlightBot/Data/HighlightDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore;

namespace HighlightBot;
Expand Down Expand Up @@ -33,5 +34,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder
.Entity<HighlightUser>()
.HasKey(nameof(HighlightUser.DiscordGuildId), nameof(HighlightUser.DiscordUserId));

modelBuilder.Entity<HighlightTerm>()
.Property(term => term.RegexOptions)
.HasDefaultValue(RegexOptions.IgnoreCase);
}
}
1 change: 1 addition & 0 deletions HighlightBot/Data/HighlightTerm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public class HighlightTerm {

public string Regex { get; set; }
public string Display { get; set; }
public RegexOptions RegexOptions { get; set; }
}
175 changes: 175 additions & 0 deletions HighlightBot/Migrations/20240623171436_RegexOptions.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions HighlightBot/Migrations/20240623171436_RegexOptions.cs
Original file line number Diff line number Diff line change
@@ -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<int>(
name: "RegexOptions",
table: "Terms",
type: "integer",
nullable: false,
defaultValue: 1);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "RegexOptions",
table: "Terms");
}
}
}
5 changes: 5 additions & 0 deletions HighlightBot/Migrations/HighlightDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.IsRequired()
.HasColumnType("text");

b.Property<int>("RegexOptions")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasDefaultValue(1);

b.Property<decimal>("user_serverid")
.HasColumnType("numeric(20,0)");

Expand Down
7 changes: 3 additions & 4 deletions HighlightBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5fa0e82

Please sign in to comment.