From ed13dd5216664e5554c55dd9ebe7c8ad960b2d65 Mon Sep 17 00:00:00 2001 From: mappu Date: Wed, 6 Jun 2018 19:41:08 +1200 Subject: [PATCH] move user registration to linkbot menu file --- NTFServer.go | 37 ------------------------------------- linkBotMenu.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/NTFServer.go b/NTFServer.go index d223436..103c159 100644 --- a/NTFServer.go +++ b/NTFServer.go @@ -148,43 +148,6 @@ func NewNTFServer(configFile string, verbose bool) (*NTFServer, error) { return &ret, nil } -// registerUser lodges a user mapping in the configuration file. -// This allows them to join the group chat (unbanning them if necessary). -// An actual NMDC connection will occur once the user joins for the first time. -func (this *NTFServer) registerUser(telegramUserId int64, hubUsername string) error { - if existingHubNick, ok := this.config.KnownUsers[telegramUserId]; ok { - if existingHubNick == hubUsername { - return nil - } - - return fmt.Errorf("Telegram account is already registered with hub nick '%s'", existingHubNick) - } - - for _, v := range this.config.KnownUsers { - if v == hubUsername { - return fmt.Errorf("Requested hub nick '%s' is already used by another member", hubUsername) - } - } - - this.config.KnownUsers[telegramUserId] = hubUsername - err := this.config.Save(this.configFile) - if err != nil { - return err - } - - // Unban from groupchat, if necessary - // Ignore errors because the user might not have been banned - _, err = this.bot.UnbanChatMember(telegram.ChatMemberConfig{ - ChatID: this.config.GroupChatID, - UserID: int(telegramUserId), - }) - if err != nil { - log.Printf("Couldn't unban user '%s' from groupchat because: %s (assuming OK, continuing)", hubUsername, err.Error()) - } - - return nil -} - // LaunchUpstreamWorker opens an NMDC connection. func (this *NTFServer) LaunchUpstreamWorker(telegramUserId int64, hubUsername string) error { diff --git a/linkBotMenu.go b/linkBotMenu.go index 60abe4a..8429f77 100644 --- a/linkBotMenu.go +++ b/linkBotMenu.go @@ -10,6 +10,43 @@ import ( telegram "github.com/go-telegram-bot-api/telegram-bot-api" ) +// registerUser lodges a user mapping in the configuration file. +// This allows them to join the group chat (unbanning them if necessary). +// An actual NMDC connection will occur once the user joins for the first time. +func (this *NTFServer) registerUser(telegramUserId int64, hubUsername string) error { + if existingHubNick, ok := this.config.KnownUsers[telegramUserId]; ok { + if existingHubNick == hubUsername { + return nil + } + + return fmt.Errorf("Telegram account is already registered with hub nick '%s'", existingHubNick) + } + + for _, v := range this.config.KnownUsers { + if v == hubUsername { + return fmt.Errorf("Requested hub nick '%s' is already used by another member", hubUsername) + } + } + + this.config.KnownUsers[telegramUserId] = hubUsername + err := this.config.Save(this.configFile) + if err != nil { + return err + } + + // Unban from groupchat, if necessary + // Ignore errors because the user might not have been banned + _, err = this.bot.UnbanChatMember(telegram.ChatMemberConfig{ + ChatID: this.config.GroupChatID, + UserID: int(telegramUserId), + }) + if err != nil { + log.Printf("Couldn't unban user '%s' from groupchat because: %s (assuming OK, continuing)", hubUsername, err.Error()) + } + + return nil +} + func (this *NTFServer) HandleDirectMessage(update telegram.Update) error { // Registration workflow