From d9eddfdad1a2477a1ef9d7c392f4185b377784fb Mon Sep 17 00:00:00 2001 From: xuu Date: Thu, 31 Mar 2022 00:45:25 +0000 Subject: [PATCH] feat: send to outbox (#98) closes: #67 Co-authored-by: Jon Lundy Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/98 Co-authored-by: xuu Co-committed-by: xuu --- client.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 5b1b277..6b63e81 100644 --- a/client.go +++ b/client.go @@ -3,10 +3,13 @@ package saltyim import ( "bytes" "context" + "crypto/sha256" "errors" "fmt" "net/http" + "net/url" "os" + "path" "strings" "time" @@ -224,10 +227,27 @@ func (cli *Client) Env(extraenvs string) []string { return env } +func (cli *Client) Outbox() *url.URL { + ep := cli.me.Endpoint() + ep.Path = path.Join( + path.Dir(ep.Path), + fmt.Sprintf("%x", sha256.Sum256(cli.me.key.Private())), + ) + return ep +} + +func (cli *Client) OutboxAddr() *Addr { + return &Addr{ + endpoint: cli.Outbox(), + key: cli.me.key, + } +} + func (cli *Client) String() string { b := &bytes.Buffer{} fmt.Fprintln(b, "Me: ", cli.me) fmt.Fprintln(b, "Endpoint: ", cli.me.Endpoint()) + fmt.Fprintln(b, "Outbox: ", cli.Outbox()) fmt.Fprintln(b, "Key: ", cli.key) return b.String() } @@ -333,7 +353,12 @@ func (cli *Client) Send(user, msg string) error { return fmt.Errorf("error looking up user %s: %w", user, err) } - return cli.SendToAddr(addr, msg) + err = cli.SendToAddr(addr, msg) + if err != nil { + return err + } + + return cli.SendToAddr(cli.OutboxAddr(), msg) } func (cli *Client) SendToAddr(addr *Addr, msg string) error {