diff --git a/config/bridge.go b/config/bridge.go index d4e50382..14c268d8 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -18,6 +18,7 @@ package config import ( "bytes" + "log" "strconv" "strings" "text/template" @@ -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:"-"` } @@ -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{}{} @@ -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 @@ -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 diff --git a/example-config.yaml b/example-config.yaml index f9af18aa..9d10c2ae 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -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 }}"