Skip to content

Commit

Permalink
2022 (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenwiltink authored Sep 2, 2022
1 parent 875ea6a commit 5efc17b
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.vscode
vendor
config.json
whitelist.txt
allowlist.txt
out
pkg_root
go-musicbot
4 changes: 2 additions & 2 deletions dist/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"irc": {
"server": "irc.freenode.net:6667",
"channel": "#banaan",
"nick": "iSlave",
"nick": "musicbot",
"realname": "swiltink",
"pass": "",
"ssl": false
Expand All @@ -28,5 +28,5 @@
"mpvpath": "C:\\Program Files (x86)\\mpv\\mpv.exe",
"mpvsocket": "\\\\.\\pipe\\mpvsocket",
"messageplugin": "irc",
"master": "terminal"
"admin": "terminal"
}
95 changes: 95 additions & 0 deletions pkg/bot/allowlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package bot

import (
"bufio"
"fmt"
"os"
"sync"
)

type AllowList struct {
path string
names map[string]struct{}
lock sync.Mutex
}

func LoadAllowList(path string) (*AllowList, error) {
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return nil, fmt.Errorf("unable to open file %s: %v", path, err)
}
defer file.Close()

list := make(map[string]struct{})
scanner := bufio.NewScanner(file)
for scanner.Scan() {
list[scanner.Text()] = struct{}{}
}

err = scanner.Err()
if err != nil {
return nil, err
}

instance := &AllowList{
path: path,
names: list,
}

return instance, nil
}

func (allowlist *AllowList) Write() error {
allowlist.lock.Lock()
defer allowlist.lock.Unlock()

return allowlist.write()
}

func (allowlist *AllowList) write() error {
file, err := os.Create(allowlist.path)
if err != nil {
return err
}
defer file.Close()

w := bufio.NewWriter(file)
for line := range allowlist.names {
_, err = fmt.Fprintln(w, line)
if err != nil {
return err
}
}
err = w.Flush()
return err
}

func (allowlist *AllowList) Add(name string) error {
allowlist.lock.Lock()
defer allowlist.lock.Unlock()

allowlist.names[name] = struct{}{}

return allowlist.write()
}

func (allowlist *AllowList) Contains(name string) bool {
_, exists := allowlist.names[name]

return exists
}

func (allowlist *AllowList) Remove(name string) error {
allowlist.lock.Lock()
defer allowlist.lock.Unlock()

_, exists := allowlist.names[name]

if !exists {
return nil
}

delete(allowlist.names, name)

return allowlist.write()
}
22 changes: 11 additions & 11 deletions pkg/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type MusicBot struct {
config *Config
commands map[string]Command
commandAliases map[string]Command
whitelist *WhiteList
allowlist *AllowList
}

func NewMusicBot(config *Config, messageProvider MessageProvider) *MusicBot {
Expand Down Expand Up @@ -65,7 +65,7 @@ func NewMusicBot(config *Config, messageProvider MessageProvider) *MusicBot {

func (bot *MusicBot) Start() {

bot.loadWhitelist()
bot.loadAllowlist()
bot.musicPlayer.Start()
bot.registerCommands()

Expand All @@ -83,19 +83,19 @@ func (bot *MusicBot) Start() {
go bot.messageLoop()
}

func (bot *MusicBot) loadWhitelist() {
whitelist, err := LoadWhiteList(bot.config.WhiteListFile)
func (bot *MusicBot) loadAllowlist() {
allowlist, err := LoadAllowList(bot.config.AllowListFile)

if err != nil {
log.Println(err)
bot.whitelist = &WhiteList{
bot.allowlist = &AllowList{
names: make(map[string]struct{}, 0),
}

return
}

bot.whitelist = whitelist
bot.allowlist = allowlist
}

func (bot *MusicBot) messageLoop() {
Expand All @@ -117,7 +117,7 @@ func (bot *MusicBot) registerCommands() {
bot.registerCommand(queueDeleteCommand)
bot.registerCommand(flushCommand)
bot.registerCommand(shuffleCommand)
bot.registerCommand(whiteListCommand)
bot.registerCommand(allowListCommand)
bot.registerCommand(volCommand)
bot.registerCommand(aboutCommand)
}
Expand Down Expand Up @@ -172,8 +172,8 @@ func (bot *MusicBot) handleMessage(message Message) {
}

func (bot *MusicBot) handleCommand(message Message) {
if !(bot.config.Master == message.Sender.Name || bot.whitelist.Contains(message.Sender.Name)) {
bot.ReplyToMessage(message, fmt.Sprintf("You're not on the whitelist %s", message.Sender.Name))
if !(bot.config.Admin == message.Sender.Name || bot.allowlist.Contains(message.Sender.Name)) {
bot.ReplyToMessage(message, fmt.Sprintf("You're not on the allowlist %s", message.Sender.Name))
return
}

Expand All @@ -186,8 +186,8 @@ func (bot *MusicBot) handleCommand(message Message) {
return
}

if command.MasterOnly && bot.config.Master != message.Sender.Name {
bot.ReplyToMessage(message, "This command is for masters only")
if command.AdminOnly && bot.config.Admin != message.Sender.Name {
bot.ReplyToMessage(message, "This command is for admins only")
return
}

Expand Down
28 changes: 14 additions & 14 deletions pkg/bot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
)

type Command struct {
Name string
Aliases []string
MasterOnly bool
Function func(bot *MusicBot, message Message)
Name string
Aliases []string
AdminOnly bool
Function func(bot *MusicBot, message Message)
}

var helpCommand = Command{
Expand Down Expand Up @@ -291,33 +291,33 @@ var shuffleCommand = Command{
},
}

var whiteListCommand = Command{
Name: "whitelist",
Aliases: []string{},
MasterOnly: true,
var allowListCommand = Command{
Name: "allowlist",
Aliases: []string{},
AdminOnly: true,
Function: func(bot *MusicBot, message Message) {
addOrRemove, name, secCmdVarErr := message.getDualCommandParameters()
if secCmdVarErr != nil {
bot.ReplyToMessage(message, "whitelist <add|remove> <name>")
bot.ReplyToMessage(message, "allowlist <add|remove> <name>")
return
}

if addOrRemove == "add" {
err := bot.whitelist.Add(name)
err := bot.allowlist.Add(name)
if err == nil {
bot.ReplyToMessage(message, fmt.Sprintf("added %s to the whitelist", name))
bot.ReplyToMessage(message, fmt.Sprintf("added %s to the allowlist", name))
} else {
bot.ReplyToMessage(message, fmt.Sprintf("error: %v", err))
}
} else if addOrRemove == "remove" {
err := bot.whitelist.Remove(name)
err := bot.allowlist.Remove(name)
if err == nil {
bot.ReplyToMessage(message, fmt.Sprintf("removed %s from the whitelist", name))
bot.ReplyToMessage(message, fmt.Sprintf("removed %s from the allowlist", name))
} else {
bot.ReplyToMessage(message, fmt.Sprintf("error: %v", err))
}
} else {
bot.ReplyToMessage(message, "whitelist <add|remove> <name>")
bot.ReplyToMessage(message, "allowlist <add|remove> <name>")
return
}
},
Expand Down
12 changes: 6 additions & 6 deletions pkg/bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

const (
DefaultConfigFileLocation = "config.json"
DefaultWhiteListFile = "whitelist.txt"
DefaultMaster = "swiltink"
DefaultAllowListFile = "allowlist.txt"
DefaultAdmin = "swiltink"
DefaultCommandPrefix = "!music"
DefaultShortCommandPrefix = "!m"
)
Expand All @@ -24,8 +24,8 @@ type SlackConfig struct {
}

type Config struct {
WhiteListFile string `json:"whitelistFile"`
Master string `json:"master"`
AllowListFile string `json:"allowlistFile"`
Admin string `json:"admin"`
Irc IRCConfig `json:"irc"`
Rocketchat RocketchatConfig `json:"rocketchat"`
Mattermost MattermostConfig `json:"mattermost"`
Expand Down Expand Up @@ -69,8 +69,8 @@ type YoutubeConfig struct {
}

func (config *Config) applyDefaults() {
config.WhiteListFile = DefaultWhiteListFile
config.Master = DefaultMaster
config.AllowListFile = DefaultAllowListFile
config.Admin = DefaultAdmin
config.CommandPrefix = DefaultCommandPrefix
config.ShortCommandPrefix = DefaultShortCommandPrefix
config.Mattermost.ConnectionTimeout = 30
Expand Down
95 changes: 0 additions & 95 deletions pkg/bot/whitelist.go

This file was deleted.

0 comments on commit 5efc17b

Please sign in to comment.