Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blacklist #56

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
21 changes: 20 additions & 1 deletion config/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"bytes"
"log"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -159,10 +160,12 @@ func (bc BridgeConfig) FormatUsername(username string) string {
type RelayConfig struct {
Enabled bool `yaml:"enabled"`
Whitelist []string `yaml:"whitelist"`
Blacklist []string `yaml:"blacklist"`
MessageFormats map[event.MessageType]string `yaml:"message_formats"`

messageTemplates *template.Template `yaml:"-"`
whitelistMap map[string]struct{} `yaml:"-"`
blacklistMap map[string]struct{} `yaml:"-"`
isAllWhitelisted bool `yaml:"-"`
}

Expand All @@ -182,6 +185,11 @@ func (rc *RelayConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
}
}

rc.blacklistMap = make(map[string]struct{}, len(rc.Blacklist))
for _, item := range rc.Blacklist {
rc.blacklistMap[item] = struct{}{}
}

rc.whitelistMap = make(map[string]struct{}, len(rc.Whitelist))
for _, item := range rc.Whitelist {
rc.whitelistMap[item] = struct{}{}
Expand All @@ -194,7 +202,7 @@ func (rc *RelayConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
}

func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool {
if !rc.Enabled {
if !rc.Enabled || rc.IsBlacklisted(userID) {
return false
} else if rc.isAllWhitelisted {
return true
Expand All @@ -207,6 +215,17 @@ func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool {
}
}

func (rc *RelayConfig) IsBlacklisted(userID id.UserID) bool {
log.Print("Im testing: " + string(userID))
if _, ok := rc.blacklistMap[string(userID)]; ok {
return true
} else {
_, homeserver, _ := userID.Parse()
_, ok = rc.blacklistMap[homeserver]
return len(homeserver) > 0 && ok
}
}

type Sender struct {
UserID string
event.MemberEventContent
Expand Down
4 changes: 4 additions & 0 deletions example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ bridge:
enabled: false
# A list of user IDs and server names who are allowed to be relayed through this bridge. Use * to allow everyone.
whitelist: []
# A list of user IDs and server names who are not allowed to be relayed through this bridge.
# Follows the schema above but cannot use * to block everyone (if that what you want just disable relay)
# Blacklist will be checked before whitelist
blacklist: []
# The formats to use when relaying messages to iMessage.
message_formats:
m.text: "{{ .Sender.Displayname }}: {{ .Message }}"
Expand Down