feat: domain blocker plugin
This commit is contained in:
parent
c9edb57505
commit
7dd02c0056
25 changed files with 898 additions and 63 deletions
132
internal/plugin/domainblock/domainblock.go
Normal file
132
internal/plugin/domainblock/domainblock.go
Normal file
|
@ -0,0 +1,132 @@
|
|||
package domainblock
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/model"
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/plugin"
|
||||
)
|
||||
|
||||
// DomainBlockPlugin is a plugin that blocks messages containing links from specific domains
|
||||
type DomainBlockPlugin struct {
|
||||
plugin.BasePlugin
|
||||
}
|
||||
|
||||
// Debug helper to check if RequiresConfig is working
|
||||
func (p *DomainBlockPlugin) RequiresConfig() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// New creates a new DomainBlockPlugin instance
|
||||
func New() *DomainBlockPlugin {
|
||||
return &DomainBlockPlugin{
|
||||
BasePlugin: plugin.BasePlugin{
|
||||
ID: "security.domainblock",
|
||||
Name: "Domain Blocker",
|
||||
Help: "Blocks messages containing links from configured domains",
|
||||
ConfigRequired: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// extractDomains extracts domains from a message text
|
||||
func extractDomains(text string) []string {
|
||||
// URL regex pattern
|
||||
urlPattern := regexp.MustCompile(`https?://([^\s/$.?#].[^\s]*)`)
|
||||
matches := urlPattern.FindAllStringSubmatch(text, -1)
|
||||
|
||||
domains := make([]string, 0, len(matches))
|
||||
for _, match := range matches {
|
||||
if len(match) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Try to parse the URL to extract the domain
|
||||
urlStr := match[0]
|
||||
parsedURL, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Extract the domain (host) from the URL
|
||||
domain := parsedURL.Host
|
||||
// Remove port if present
|
||||
if i := strings.IndexByte(domain, ':'); i >= 0 {
|
||||
domain = domain[:i]
|
||||
}
|
||||
|
||||
domains = append(domains, strings.ToLower(domain))
|
||||
}
|
||||
|
||||
return domains
|
||||
}
|
||||
|
||||
// OnMessage processes incoming messages
|
||||
func (p *DomainBlockPlugin) OnMessage(msg *model.Message, config map[string]interface{}) []*model.MessageAction {
|
||||
// Skip messages from bots
|
||||
if msg.FromBot {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get blocked domains from config
|
||||
blockedDomainsStr, ok := config["blocked_domains"].(string)
|
||||
if !ok || blockedDomainsStr == "" {
|
||||
return nil // No blocked domains configured
|
||||
}
|
||||
|
||||
// Split and clean blocked domains
|
||||
blockedDomains := strings.Split(blockedDomainsStr, ",")
|
||||
for i, domain := range blockedDomains {
|
||||
blockedDomains[i] = strings.ToLower(strings.TrimSpace(domain))
|
||||
}
|
||||
|
||||
// Extract domains from message
|
||||
messageDomains := extractDomains(msg.Text)
|
||||
if len(messageDomains) == 0 {
|
||||
return nil // No domains in message
|
||||
}
|
||||
|
||||
// Check if any domains in the message are blocked
|
||||
for _, msgDomain := range messageDomains {
|
||||
for _, blockedDomain := range blockedDomains {
|
||||
if blockedDomain == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasSuffix(msgDomain, blockedDomain) || msgDomain == blockedDomain {
|
||||
// Domain is blocked, create actions
|
||||
|
||||
// 1. Create a delete message action
|
||||
deleteAction := &model.MessageAction{
|
||||
Type: model.ActionDeleteMessage,
|
||||
MessageID: msg.ID,
|
||||
Chat: msg.Chat,
|
||||
Channel: msg.Channel,
|
||||
}
|
||||
|
||||
// 2. Create a notification message action
|
||||
notificationMsg := &model.Message{
|
||||
Text: fmt.Sprintf("I don't like links from %s 🙈", blockedDomain),
|
||||
Chat: msg.Chat,
|
||||
Channel: msg.Channel,
|
||||
}
|
||||
|
||||
sendAction := &model.MessageAction{
|
||||
Type: model.ActionSendMessage,
|
||||
Message: notificationMsg,
|
||||
Chat: msg.Chat,
|
||||
Channel: msg.Channel,
|
||||
}
|
||||
|
||||
return []*model.MessageAction{deleteAction, sendAction}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Plugin is registered in app.go, not using init()
|
140
internal/plugin/domainblock/domainblock_test.go
Normal file
140
internal/plugin/domainblock/domainblock_test.go
Normal file
|
@ -0,0 +1,140 @@
|
|||
package domainblock
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/model"
|
||||
)
|
||||
|
||||
func TestExtractDomains(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
text string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "No URLs",
|
||||
text: "Hello, world!",
|
||||
expected: []string{},
|
||||
},
|
||||
{
|
||||
name: "Single URL",
|
||||
text: "Check out https://example.com for more info",
|
||||
expected: []string{"example.com"},
|
||||
},
|
||||
{
|
||||
name: "Multiple URLs",
|
||||
text: "Check out https://example.com and http://test.example.org for more info",
|
||||
expected: []string{"example.com", "test.example.org"},
|
||||
},
|
||||
{
|
||||
name: "URL with path",
|
||||
text: "Check out https://example.com/path/to/resource",
|
||||
expected: []string{"example.com"},
|
||||
},
|
||||
{
|
||||
name: "URL with port",
|
||||
text: "Check out https://example.com:8080/path/to/resource",
|
||||
expected: []string{"example.com"},
|
||||
},
|
||||
{
|
||||
name: "URL with subdomain",
|
||||
text: "Check out https://sub.example.com",
|
||||
expected: []string{"sub.example.com"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
domains := extractDomains(test.text)
|
||||
|
||||
if len(domains) != len(test.expected) {
|
||||
t.Errorf("Expected %d domains, got %d", len(test.expected), len(domains))
|
||||
return
|
||||
}
|
||||
|
||||
for i, domain := range domains {
|
||||
if domain != test.expected[i] {
|
||||
t.Errorf("Expected domain %s, got %s", test.expected[i], domain)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnMessage(t *testing.T) {
|
||||
plugin := New()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
text string
|
||||
blockedDomains string
|
||||
expectBlocked bool
|
||||
}{
|
||||
{
|
||||
name: "No blocked domains",
|
||||
text: "Check out https://example.com",
|
||||
blockedDomains: "",
|
||||
expectBlocked: false,
|
||||
},
|
||||
{
|
||||
name: "No matching domain",
|
||||
text: "Check out https://example.com",
|
||||
blockedDomains: "bad.com, evil.org",
|
||||
expectBlocked: false,
|
||||
},
|
||||
{
|
||||
name: "Matching domain",
|
||||
text: "Check out https://example.com",
|
||||
blockedDomains: "example.com, evil.org",
|
||||
expectBlocked: true,
|
||||
},
|
||||
{
|
||||
name: "Matching subdomain",
|
||||
text: "Check out https://sub.example.com",
|
||||
blockedDomains: "example.com",
|
||||
expectBlocked: true,
|
||||
},
|
||||
{
|
||||
name: "Multiple domains, one matching",
|
||||
text: "Check out https://example.com and https://good.org",
|
||||
blockedDomains: "bad.com, example.com",
|
||||
expectBlocked: true,
|
||||
},
|
||||
{
|
||||
name: "Spaces in blocked domains list",
|
||||
text: "Check out https://example.com",
|
||||
blockedDomains: "bad.com, example.com , evil.org",
|
||||
expectBlocked: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
msg := &model.Message{
|
||||
Text: test.text,
|
||||
Chat: "test-chat",
|
||||
ID: "test-id",
|
||||
Channel: &model.Channel{
|
||||
ID: 1,
|
||||
},
|
||||
}
|
||||
|
||||
config := map[string]interface{}{
|
||||
"blocked_domains": test.blockedDomains,
|
||||
}
|
||||
|
||||
responses := plugin.OnMessage(msg, config)
|
||||
|
||||
if test.expectBlocked {
|
||||
if responses == nil || len(responses) == 0 {
|
||||
t.Errorf("Expected message to be blocked, but it wasn't")
|
||||
}
|
||||
} else {
|
||||
if responses != nil && len(responses) > 0 {
|
||||
t.Errorf("Expected message not to be blocked, but it was")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue