From 4c670466f8235baec55e2acd1b96b132c444cbdd Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Tue, 24 Oct 2023 21:11:34 +1100 Subject: [PATCH] Allow overriding DefaultServer & DefaultTeam --- mm-go-irckit/service.go | 109 +++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 58 deletions(-) diff --git a/mm-go-irckit/service.go b/mm-go-irckit/service.go index a5ecb79c..78e11608 100644 --- a/mm-go-irckit/service.go +++ b/mm-go-irckit/service.go @@ -113,75 +113,68 @@ func login(u *User, toUser *User, args []string, service string) { } cred := bridge.Credentials{} - datalen := 4 + datalen := len(args) if len(args) > 1 && strings.Contains(args[len(args)-1], "MFAToken=") { - datalen = 5 + MFAToken := strings.Split(args[len(args)-1], "=") + cred.MFAToken = MFAToken[1] + datalen-- } - if u.v.GetString("mattermost.DefaultTeam") != "" { - cred.Team = u.v.GetString("mattermost.DefaultTeam") - datalen-- + teamOrServer := "" + creds := []*string{&cred.Server, &teamOrServer, &cred.Login, &cred.Pass} + for argsIdx, credsIdx := datalen-1, len(creds); argsIdx > 0 && credsIdx > 0; argsIdx-- { + *creds[credsIdx-1] = args[argsIdx] + creds = creds[:credsIdx-1] + credsIdx-- } - if u.v.GetString("mattermost.DefaultServer") != "" { + switch { + // All params provided (server team username password) so set the team to provided. + case cred.Server != "": + cred.Team = teamOrServer + // Missing server, let's use DefaultServer. + case u.v.GetString("mattermost.DefaultServer") != "": cred.Server = u.v.GetString("mattermost.DefaultServer") - datalen-- + cred.Team = teamOrServer + // Missing team, let's use DefaultTeam. + case u.v.GetString("mattermost.DefaultTeam") != "": + cred.Server = teamOrServer + cred.Team = u.v.GetString("mattermost.DefaultTeam") + default: + cred.Server = teamOrServer } - if len(args) >= datalen { // nolint:nestif - logger.Debugf("args_len: %d", len(args)) - logger.Debugf("team: %s", cred.Team) - logger.Debugf("server: %s", cred.Server) - if strings.Contains(args[len(args)-1], "MFAToken=") { - logger.Debug("found MFAToken") - MFAToken := strings.Split(args[len(args)-1], "=") - cred.MFAToken = MFAToken[1] - cred.Pass = args[len(args)-2] - cred.Login = args[len(args)-3] - } else { - cred.Pass = args[len(args)-1] - cred.Login = args[len(args)-2] - } - // no default server or team specified - if cred.Server == "" && cred.Team == "" { - cred.Server = args[0] - cred.Team = args[1] - } - - if cred.Team == "" { - cred.Team = args[0] - } - - if cred.Server == "" { - cred.Server = args[0] - } + logger.Debugf("args_len: %d", len(args)) + logger.Debugf("team: %s", cred.Team) + logger.Debugf("server: %s", cred.Server) + if cred.MFAToken != "" { + logger.Debug("found MFAToken") } - // incorrect arguments - if len(args) < datalen { - switch { - // no server or team - case cred.Team != "" && cred.Server != "": - u.MsgUser(toUser, "need LOGIN ") - u.MsgUser(toUser, "when using a personal token replace with token=") - u.MsgUser(toUser, "when using a mfa token use LOGIN MFAToken=") - // server missing - case cred.Team != "": - u.MsgUser(toUser, "need LOGIN ") - u.MsgUser(toUser, "when using a personal token replace with token=") - u.MsgUser(toUser, "when using a mfa token use LOGIN MFAToken=") - // team missing - case cred.Server != "": - u.MsgUser(toUser, "need LOGIN ") - u.MsgUser(toUser, "when using a personal token replace with token=") - u.MsgUser(toUser, "when using a mfa token use LOGIN MFAToken=") - default: - u.MsgUser(toUser, "need LOGIN ") - u.MsgUser(toUser, "when using a personal token replace with token=") - u.MsgUser(toUser, "when using a mfa token use LOGIN MFAToken=") - } - + switch { + case cred.Team != "" && cred.Server != "": + u.MsgUser(toUser, "need LOGIN ") + u.MsgUser(toUser, "when using a personal token replace with token=") + u.MsgUser(toUser, "when using a mfa token use LOGIN MFAToken=") + return + // server missing + case cred.Server == "": + u.MsgUser(toUser, "need LOGIN ") + u.MsgUser(toUser, "when using a personal token replace with token=") + u.MsgUser(toUser, "when using a mfa token use LOGIN MFAToken=") + return + // team missing + case cred.Team == "": + u.MsgUser(toUser, "need LOGIN ") + u.MsgUser(toUser, "when using a personal token replace with token=") + u.MsgUser(toUser, "when using a mfa token use LOGIN MFAToken=") + return + // login/username or password/token missing + case cred.Login == "" || cred.Pass == "": + u.MsgUser(toUser, "need LOGIN ") + u.MsgUser(toUser, "when using a personal token replace with token=") + u.MsgUser(toUser, "when using a mfa token use LOGIN MFAToken=") return }