adjust initBotUsers()
This commit is contained in:
11
bot/bot.go
11
bot/bot.go
@@ -37,7 +37,7 @@ func New(
|
|||||||
log *logger.Logger,
|
log *logger.Logger,
|
||||||
prefix string,
|
prefix string,
|
||||||
domain string,
|
domain string,
|
||||||
users []string,
|
envUsers []string,
|
||||||
admins []string,
|
admins []string,
|
||||||
) (*Bot, error) {
|
) (*Bot, error) {
|
||||||
b := &Bot{
|
b := &Bot{
|
||||||
@@ -50,15 +50,22 @@ func New(
|
|||||||
lp: lp,
|
lp: lp,
|
||||||
mu: map[id.RoomID]*sync.Mutex{},
|
mu: map[id.RoomID]*sync.Mutex{},
|
||||||
}
|
}
|
||||||
err := b.initBotUsers(users)
|
users, err := b.initBotUsers(envUsers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
allowedUsers, uerr := parseMXIDpatterns(users, "")
|
||||||
|
if uerr != nil {
|
||||||
|
return nil, uerr
|
||||||
|
}
|
||||||
|
b.allowedUsers = allowedUsers
|
||||||
|
|
||||||
allowedAdmins, aerr := parseMXIDpatterns(admins, "")
|
allowedAdmins, aerr := parseMXIDpatterns(admins, "")
|
||||||
if aerr != nil {
|
if aerr != nil {
|
||||||
return nil, aerr
|
return nil, aerr
|
||||||
}
|
}
|
||||||
b.allowedAdmins = allowedAdmins
|
b.allowedAdmins = allowedAdmins
|
||||||
|
|
||||||
b.commands = b.buildCommandList()
|
b.commands = b.buildCommandList()
|
||||||
|
|
||||||
return b, nil
|
return b, nil
|
||||||
|
|||||||
@@ -35,33 +35,26 @@ func (s botSettings) Users() []string {
|
|||||||
return []string{value}
|
return []string{value}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) initBotUsers(users []string) error {
|
func (b *Bot) initBotUsers(envUsers []string) ([]string, error) {
|
||||||
_, homeserver, err := b.lp.GetClient().UserID.Parse()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
config := b.getBotSettings()
|
config := b.getBotSettings()
|
||||||
oldUsers := config.Get(botOptionUsers)
|
cfgUsers := config.Users()
|
||||||
// TODO: remove after migration
|
if len(cfgUsers) > 0 {
|
||||||
if len(users) > 0 && oldUsers == "" {
|
// already migrated
|
||||||
_, err := parseMXIDpatterns(users, "@*:"+homeserver)
|
return cfgUsers, nil
|
||||||
|
}
|
||||||
|
if len(envUsers) == 0 {
|
||||||
|
_, homeserver, err := b.lp.GetClient().UserID.Parse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.Set(botOptionUsers, strings.Join(users, " "))
|
config.Set(botOptionUsers, "@*:"+homeserver)
|
||||||
|
} else {
|
||||||
|
// Initialize from environment variable
|
||||||
|
// TODO: remove this migration later and always initialize to `"@*:"+homeserver`
|
||||||
|
config.Set(botOptionUsers, strings.Join(envUsers, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
allowedUsers, uerr := parseMXIDpatterns(config.Users(), "@*:"+homeserver)
|
return config.Users(), b.setBotSettings(config)
|
||||||
if uerr != nil {
|
|
||||||
return uerr
|
|
||||||
}
|
|
||||||
b.allowedUsers = allowedUsers
|
|
||||||
|
|
||||||
if oldUsers != config.Get(botOptionUsers) {
|
|
||||||
return b.setBotSettings(config)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) getBotSettings() botSettings {
|
func (b *Bot) getBotSettings() botSettings {
|
||||||
|
|||||||
Reference in New Issue
Block a user