Skip to content

Commit

Permalink
Allow overriding DefaultServer & DefaultTeam
Browse files Browse the repository at this point in the history
  • Loading branch information
hloeung committed Oct 27, 2023
1 parent 04675fb commit 4c67046
Showing 1 changed file with 51 additions and 58 deletions.
109 changes: 51 additions & 58 deletions mm-go-irckit/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <login> <pass>")
u.MsgUser(toUser, "when using a personal token replace <pass> with token=<yourtoken>")
u.MsgUser(toUser, "when using a mfa token use LOGIN <login> <pass> MFAToken=<yourmfatoken>")
// server missing
case cred.Team != "":
u.MsgUser(toUser, "need LOGIN <server> <login> <pass>")
u.MsgUser(toUser, "when using a personal token replace <pass> with token=<yourtoken>")
u.MsgUser(toUser, "when using a mfa token use LOGIN <server> <login> <pass> MFAToken=<yourmfatoken>")
// team missing
case cred.Server != "":
u.MsgUser(toUser, "need LOGIN <team> <login> <pass>")
u.MsgUser(toUser, "when using a personal token replace <pass> with token=<yourtoken>")
u.MsgUser(toUser, "when using a mfa token use LOGIN <team> <login> <pass> MFAToken=<yourmfatoken>")
default:
u.MsgUser(toUser, "need LOGIN <server> <team> <login> <pass>")
u.MsgUser(toUser, "when using a personal token replace <pass> with token=<yourtoken>")
u.MsgUser(toUser, "when using a mfa token use LOGIN <server> <team> <login> <pass> MFAToken=<yourmfatoken>")
}

switch {
case cred.Team != "" && cred.Server != "":
u.MsgUser(toUser, "need LOGIN <server> <team> <login> <pass>")
u.MsgUser(toUser, "when using a personal token replace <pass> with token=<yourtoken>")
u.MsgUser(toUser, "when using a mfa token use LOGIN <server> <team> <login> <pass> MFAToken=<yourmfatoken>")
return
// server missing
case cred.Server == "":
u.MsgUser(toUser, "need LOGIN <server> <login> <pass>")
u.MsgUser(toUser, "when using a personal token replace <pass> with token=<yourtoken>")
u.MsgUser(toUser, "when using a mfa token use LOGIN <server> <login> <pass> MFAToken=<yourmfatoken>")
return
// team missing
case cred.Team == "":
u.MsgUser(toUser, "need LOGIN <team> <login> <pass>")
u.MsgUser(toUser, "when using a personal token replace <pass> with token=<yourtoken>")
u.MsgUser(toUser, "when using a mfa token use LOGIN <team> <login> <pass> MFAToken=<yourmfatoken>")
return
// login/username or password/token missing
case cred.Login == "" || cred.Pass == "":
u.MsgUser(toUser, "need LOGIN <login> <pass>")
u.MsgUser(toUser, "when using a personal token replace <pass> with token=<yourtoken>")
u.MsgUser(toUser, "when using a mfa token use LOGIN <login> <pass> MFAToken=<yourmfatoken>")
return
}

Expand Down

0 comments on commit 4c67046

Please sign in to comment.