Skip to content

Commit

Permalink
Add allowed_discord_ids setting
Browse files Browse the repository at this point in the history
  • Loading branch information
qaisjp committed May 23, 2021
1 parent fd95473 commit 28d6dfc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ The config file is a yaml formatted file with the following fields:
| `separator` | No | `_` | Yes | used in fallback situations. If set to `-`, the **fallback name** will be like `bob-7247_d2` (where `7247` is the discord user's discriminator, and `_d2` is the suffix) |
| `irc_listener_name` | Yes | `~d` | The name of the irc listener | |
| `ignored_discord_ids` | Sometimes | | Yes | A list of Discord IDs to not relay to IRC |
| `allowed_discord_ids` | Sometimes | `null` | Yes | A list of Discord IDs to relay to IRC. `null` allows all Discord users to be relayed to IRC |
| `puppet_username` | No | username of discord account being puppeted | Yes | username to connect to irc with |
| `webirc_pass` | No | | Yes | optional, but recommended for regular (non-simple) usage. this must be obtained by the IRC sysops |
| `irc_listener_prejoin_commands` | Yes | | Yes | list of commands for the listener IRC connection to execute (right before joining channels) |
Expand Down
1 change: 1 addition & 0 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
PuppetUsername string // Username to connect to IRC with
IRCIgnores []glob.Glob
DiscordIgnores map[string]struct{} // Discord user IDs to not bridge
DiscordAllowed map[string]struct{} // Discord user IDs to only bridge
ConnectionLimit int // number of IRC connections we can spawn

IRCPuppetPrejoinCommands []string
Expand Down
9 changes: 9 additions & 0 deletions bridge/irc_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ func (m *IRCManager) HandleUser(user DiscordUser) {
if m.ircIgnoredDiscord(user.ID) {
return
}

// If we have an allowed list of users at all
if allowed := m.bridge.Config.DiscordAllowed; allowed != nil {
// Short-circuit if they aren't in the list
if _, ok := allowed[user.ID]; !ok {
return
}
}

// Does the user exist on the IRC side?
if con, ok := m.ircConnections[user.ID]; ok {
// Close the connection if they are not
Expand Down
12 changes: 8 additions & 4 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ channel_mappings:
"#bottest chanKey": 316038111811600387
"#bottest2": 318327329044561920

# Prevent MEE6 from appearing on IRC
# ignored_discord_ids:
# - 159985870458322944

suffix: "_d2"
separator: "_"
irc_listener_name: "_d2"
Expand Down Expand Up @@ -49,3 +45,11 @@ irc_puppet_prejoin_commands:

# This limits to 2 connections (a listener, and one puppet, the rest relayed in simple mode)
# connection_limit: 2

# Prevent MEE6 from appearing on IRC
# ignored_discord_ids:
# - 159985870458322944

# Only allow specific Discord users to appear on IRC
# allowed_discord_ids:
# - 159985870458322944 # Only allow Mee6!
37 changes: 26 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ func main() {
webIRCPass := viper.GetString("webirc_pass") // Password for WEBIRC
ircIgnores := viper.GetStringSlice("ignored_irc_hostmasks") // IRC hosts to not relay to Discord
rawDiscordIgnores := viper.GetStringSlice("ignored_discord_ids") // Ignore these Discord users on IRC
rawIRCFilter := viper.GetStringSlice("irc_message_filter") // Ignore lines containing matched text from IRC
rawDiscordFilter := viper.GetStringSlice("discord_message_filter") // Ignore lines containing matched text from Discord
connectionLimit := viper.GetInt("connection_limit") // Limiter on how many IRC Connections we can spawn
rawDiscordAllowed := viper.GetStringSlice("allowed_discord_ids")
rawIRCFilter := viper.GetStringSlice("irc_message_filter") // Ignore lines containing matched text from IRC
rawDiscordFilter := viper.GetStringSlice("discord_message_filter") // Ignore lines containing matched text from Discord
connectionLimit := viper.GetInt("connection_limit") // Limiter on how many IRC Connections we can spawn
//
if !*debugMode {
*debugMode = viper.GetBool("debug")
Expand Down Expand Up @@ -137,9 +138,11 @@ func main() {
ircFilter := setupFilter(rawIRCFilter)
SetLogDebug(*debugMode)

discordIgnores := make(map[string]struct{}, len(rawDiscordIgnores))
for _, nick := range rawDiscordIgnores {
discordIgnores[nick] = struct{}{}
// Check for nil, as nil means we don't use this list
var discordAllowed map[string]struct{}
if rawDiscordAllowed != nil {
log.Println("allowed_discord_ids is set, so only specific Discord users will be bridged")
discordAllowed = stringSliceToMap(rawDiscordAllowed)
}

dib, err := bridge.New(&bridge.Config{
Expand All @@ -155,7 +158,8 @@ func main() {
ConnectionLimit: connectionLimit,
IRCIgnores: matchers,
IRCFilteredMessages: ircFilter,
DiscordIgnores: discordIgnores,
DiscordIgnores: stringSliceToMap(rawDiscordIgnores),
DiscordAllowed: discordAllowed,
DiscordFilteredMessages: discordFilter,
PuppetUsername: puppetUsername,
WebIRCPass: webIRCPass,
Expand Down Expand Up @@ -224,11 +228,14 @@ func main() {
}

rawDiscordIgnores := viper.GetStringSlice("ignored_discord_ids")
discordIgnores := make(map[string]struct{}, len(rawDiscordIgnores))
for _, nick := range rawDiscordIgnores {
discordIgnores[nick] = struct{}{}
dib.Config.DiscordIgnores = stringSliceToMap(rawDiscordIgnores)

rawDiscordAllowed := viper.GetStringSlice("allowed_discord_ids")
if rawDiscordAllowed == nil {
dib.Config.DiscordAllowed = nil
} else {
dib.Config.DiscordAllowed = stringSliceToMap(rawDiscordAllowed)
}
dib.Config.DiscordIgnores = discordIgnores

chans := viper.GetStringMapString("channel_mappings")
equalChans := reflect.DeepEqual(chans, channelMappings)
Expand All @@ -255,6 +262,14 @@ func main() {
dib.Close()
}

func stringSliceToMap(list []string) map[string]struct{} {
m := make(map[string]struct{}, len(list))
for _, v := range list {
m[v] = struct{}{}
}
return m
}

func setupHostmaskMatchers(hostmasks []string) []glob.Glob {
var matchers []glob.Glob
for _, mask := range hostmasks {
Expand Down

0 comments on commit 28d6dfc

Please sign in to comment.