diff options
author | Robin Jarry <robin@jarry.cc> | 2023-08-20 21:11:57 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-08-26 21:41:05 +0200 |
commit | 8ac84d0c2bd035db2c9404aee6d06cbd91fd79a0 (patch) | |
tree | 38c1c994e54187de52264fef8c82b7d89c18fbcf | |
parent | c077d2bf0f5d1a3aa0f97f7942c3d6938c9c43ff (diff) | |
download | aerc-8ac84d0c2bd035db2c9404aee6d06cbd91fd79a0.tar.gz |
wizard: better url generation
Prepare for other protocols. Do not expect that there will be an
username/password.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Reviewed-by: Tristan Partin <tristan@partin.io>
Tested-by: Tim Culverhouse <tim@timculverhouse.com>
-rw-r--r-- | widgets/account-wizard.go | 84 |
1 files changed, 40 insertions, 44 deletions
diff --git a/widgets/account-wizard.go b/widgets/account-wizard.go index b4af40b2..1c2f01ef 100644 --- a/widgets/account-wizard.go +++ b/widgets/account-wizard.go @@ -561,8 +561,40 @@ func (wizard *AccountWizard) finish(tutorial bool) { wizard.aerc.RemoveTab(wizard, false) } +func splitHostPath(server string) (string, string) { + host, path, found := strings.Cut(server, "/") + if found { + path = "/" + path + } + return host, path +} + +func makeURLs(scheme, host, path, user, pass string) (url.URL, url.URL) { + var opaque string + + // If everything is unset, the rendered URL is '<scheme>:'. + // Force a '//' opaque suffix so that it is rendered as '<scheme>://'. + if scheme != "" && host == "" && path == "" && user == "" && pass == "" { + opaque = "//" + } + + uri := url.URL{Scheme: scheme, Host: host, Path: path, Opaque: opaque} + clean := uri + + switch { + case pass != "": + uri.User = url.UserPassword(user, pass) + clean.User = url.UserPassword(user, strings.Repeat("*", len(pass))) + case user != "": + uri.User = url.User(user) + clean.User = url.User(user) + } + + return uri, clean +} + func (wizard *AccountWizard) sourceUri() url.URL { - host := wizard.sourceServer.String() + host, path := splitHostPath(wizard.sourceServer.String()) user := wizard.sourceUsername.String() pass := wizard.sourcePassword.String() var scheme string @@ -576,27 +608,9 @@ func (wizard *AccountWizard) sourceUri() url.URL { scheme = "imaps" } } - var ( - userpass *url.Userinfo - userwopass *url.Userinfo - ) - if pass == "" { - userpass = url.User(user) - userwopass = userpass - } else { - userpass = url.UserPassword(user, pass) - userwopass = url.UserPassword(user, strings.Repeat("*", len(pass))) - } - uri := url.URL{ - Scheme: scheme, - Host: host, - User: userpass, - } - clean := url.URL{ - Scheme: scheme, - Host: host, - User: userwopass, - } + + uri, clean := makeURLs(scheme, host, path, user, pass) + wizard.sourceStr.Text("Connection URL: " + strings.ReplaceAll(clean.String(), "%2A", "*")) wizard.sourceUrl = uri @@ -604,7 +618,7 @@ func (wizard *AccountWizard) sourceUri() url.URL { } func (wizard *AccountWizard) outgoingUri() url.URL { - host := wizard.outgoingServer.String() + host, path := splitHostPath(wizard.outgoingServer.String()) user := wizard.outgoingUsername.String() pass := wizard.outgoingPassword.String() var scheme string @@ -618,27 +632,9 @@ func (wizard *AccountWizard) outgoingUri() url.URL { scheme = "smtps" } } - var ( - userpass *url.Userinfo - userwopass *url.Userinfo - ) - if pass == "" { - userpass = url.User(user) - userwopass = userpass - } else { - userpass = url.UserPassword(user, pass) - userwopass = url.UserPassword(user, strings.Repeat("*", len(pass))) - } - uri := url.URL{ - Scheme: scheme, - Host: host, - User: userpass, - } - clean := url.URL{ - Scheme: scheme, - Host: host, - User: userwopass, - } + + uri, clean := makeURLs(scheme, host, path, user, pass) + wizard.outgoingStr.Text("Connection URL: " + strings.ReplaceAll(clean.String(), "%2A", "*")) wizard.outgoingUrl = uri |