improve discover
This commit is contained in:
parent
c06740b144
commit
5365caa93c
3 changed files with 19 additions and 9 deletions
|
|
@ -124,7 +124,10 @@ cli tools
|
||||||
//export cli_discoverhs
|
//export cli_discoverhs
|
||||||
func cli_discoverhs(id *C.char) *C.char {
|
func cli_discoverhs(id *C.char) *C.char {
|
||||||
mxid := C.GoString(id)
|
mxid := C.GoString(id)
|
||||||
result := mxutils.DiscoverHS(mxid)
|
result, err := mxutils.DiscoverHS(mxid)
|
||||||
|
if err != nil {
|
||||||
|
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||||
|
}
|
||||||
return C.CString(result)
|
return C.CString(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,8 @@ import (
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DiscoverHS(ids string) string {
|
// DiscoverHS try to discover the homeserver url from the given string.
|
||||||
|
func DiscoverHS(ids string) (string, error) {
|
||||||
|
|
||||||
var domainname string
|
var domainname string
|
||||||
|
|
||||||
|
|
@ -30,17 +31,17 @@ func DiscoverHS(ids string) string {
|
||||||
|
|
||||||
wk, err := mautrix.DiscoverClientAPI(context.Background(), domainname)
|
wk, err := mautrix.DiscoverClientAPI(context.Background(), domainname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Sprintf("ERR: %v", err)
|
return "", err
|
||||||
}
|
}
|
||||||
if wk == nil {
|
if wk == nil {
|
||||||
return fmt.Sprintf("No well-known. hs from input: %s", domainname)
|
return domainname, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
j, err := json.Marshal(wk)
|
j, err := json.Marshal(wk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Sprintf("ERR: %v", err)
|
return "", err
|
||||||
}
|
}
|
||||||
return string(j)
|
return string(j), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MkToken(ids string, pw string) string {
|
func MkToken(ids string, pw string) string {
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,11 @@ import json
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
|
@click.option(
|
||||||
|
"--json", "show_json", is_flag=True, help="show json as returned from server."
|
||||||
|
)
|
||||||
@click.argument("domain", metavar="string")
|
@click.argument("domain", metavar="string")
|
||||||
def discoverhs(domain):
|
def discoverhs(domain, show_json):
|
||||||
"""Attempts to discover the homeserver from the given string"""
|
"""Attempts to discover the homeserver from the given string"""
|
||||||
mxid = domain.encode(encoding="utf-8")
|
mxid = domain.encode(encoding="utf-8")
|
||||||
|
|
||||||
|
|
@ -18,5 +21,8 @@ def discoverhs(domain):
|
||||||
if result.startswith("ERR:"):
|
if result.startswith("ERR:"):
|
||||||
print(result)
|
print(result)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
if show_json:
|
||||||
|
print(result)
|
||||||
|
else:
|
||||||
result_dict = json.loads(result)
|
result_dict = json.loads(result)
|
||||||
print(result_dict["m.homeserver"]["base_url"])
|
print(result_dict["m.homeserver"]["base_url"])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue