50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
using YwxAppWpfDanMu.Models;
|
|
|
|
namespace YwxAppWpfDanMu.Controls
|
|
{
|
|
public static class DanMuFilter
|
|
{
|
|
private static readonly List<string> _blockedKeywords = new List<string>();
|
|
private static readonly List<Regex> _blockedPatterns = new List<Regex>();
|
|
|
|
public static void AddBlockedKeyword(string keyword)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(keyword) && !_blockedKeywords.Contains(keyword))
|
|
{
|
|
_blockedKeywords.Add(keyword);
|
|
}
|
|
}
|
|
|
|
public static void AddBlockedPattern(string pattern)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(pattern))
|
|
{
|
|
_blockedPatterns.Add(new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase));
|
|
}
|
|
}
|
|
|
|
public static bool ShouldFilter(DanMuMessage message)
|
|
{
|
|
if (message == null || string.IsNullOrWhiteSpace(message.Content))
|
|
return true;
|
|
|
|
// 检查关键词
|
|
foreach (var keyword in _blockedKeywords)
|
|
{
|
|
if (message.Content.Contains(keyword))
|
|
return true;
|
|
}
|
|
|
|
// 检查正则表达式
|
|
foreach (var pattern in _blockedPatterns)
|
|
{
|
|
if (pattern.IsMatch(message.Content))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
} |