mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-16 11:58:24 +00:00
5e22d087ee
Co-authored-by: James Mills <prologic@shortcircuit.net.au> Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/73 Reviewed-by: xuu <xuu@noreply@mills.io>
188 lines
4.9 KiB
Go
188 lines
4.9 KiB
Go
package saltyim
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.mills.io/prologic/msgbus"
|
|
msgbus_client "git.mills.io/prologic/msgbus/client"
|
|
"github.com/keys-pub/keys"
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"go.mills.io/salty"
|
|
"go.mills.io/saltyim/internal/exec"
|
|
)
|
|
|
|
type addrCache map[string]*Addr
|
|
|
|
// PackMessage formts an outoing message in the Message Format
|
|
// <timestamp>\t(<sender>) <message>
|
|
func PackMessage(me *Addr, msg string) []byte {
|
|
return []byte(fmt.Sprint(time.Now().UTC().Format(time.RFC3339), "\t", me.Formatted(), "\t", strings.TrimSpace(msg), "\n"))
|
|
}
|
|
|
|
// Send sends the encrypted message `msg` to the Endpoint `endpoint` using a
|
|
// `POST` request and returns nil on success or an error on failure.
|
|
func Send(endpoint, msg string) error {
|
|
res, err := Request(http.MethodPost, endpoint, nil, bytes.NewBufferString(msg))
|
|
if err != nil {
|
|
return fmt.Errorf("error publishing message to %s: %w", endpoint, err)
|
|
}
|
|
defer res.Body.Close()
|
|
return nil
|
|
}
|
|
|
|
// Client is a Salty IM client that handles talking to a Salty IM Broker
|
|
// and Sedngina and Receiving messages to/from Salty IM Users.
|
|
type Client struct {
|
|
me *Addr
|
|
key *keys.EdX25519Key
|
|
cache addrCache
|
|
}
|
|
|
|
func (c *Client) String() string {
|
|
b := &bytes.Buffer{}
|
|
fmt.Fprintln(b, "Me: ", c.me)
|
|
fmt.Fprintln(b, "Endpoint: ", c.me.Endpoint())
|
|
fmt.Fprintln(b, "Key: ", c.key)
|
|
return b.String()
|
|
}
|
|
|
|
// NewClient reeturns a new Salty IM client for sending and receiving
|
|
// encrypted messages to other Salty IM users as well as decrypting
|
|
// and displaying messages of the user's own inbox.
|
|
func NewClient(me *Addr, options ...IdentityOption) (*Client, error) {
|
|
ident, err := GetIdentity(options...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error opening identity %s: %w", ident.Source(), err)
|
|
}
|
|
if me == nil || me.IsZero() {
|
|
me = ident.addr
|
|
}
|
|
|
|
if me == nil || me.IsZero() {
|
|
return nil, fmt.Errorf("unable to find your user addressn in %s", ident.Source())
|
|
}
|
|
|
|
if err := me.Refresh(); err != nil {
|
|
return nil, fmt.Errorf("error looking up user endpoint %s: %w", me.HashURI(), err)
|
|
}
|
|
|
|
log.Debugf("Using identity %s with public key %s", ident.Source(), ident.key)
|
|
log.Debugf("Salty Addr is %s", me)
|
|
log.Debugf("Endpoint is %s", me.Endpoint())
|
|
|
|
return &Client{
|
|
me: me,
|
|
key: ident.key,
|
|
cache: make(addrCache),
|
|
}, nil
|
|
}
|
|
|
|
func (cli *Client) getAddr(user string) (*Addr, error) {
|
|
addr, ok := cli.cache[user]
|
|
if ok {
|
|
return addr, nil
|
|
}
|
|
|
|
addr, err := LookupAddr(user)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error: failed to lookup user %s: %w", user, err)
|
|
}
|
|
|
|
cli.cache[user] = addr
|
|
|
|
return addr, nil
|
|
}
|
|
|
|
type Message struct {
|
|
Text string
|
|
Key *keys.EdX25519PublicKey
|
|
}
|
|
|
|
func (cli *Client) handleMessage(prehook, posthook string, msgs chan Message) msgbus.HandlerFunc {
|
|
return func(msg *msgbus.Message) error {
|
|
if prehook != "" {
|
|
out, err := exec.RunCmd(exec.DefaultRunCmdTimeout, prehook, bytes.NewBuffer(msg.Payload))
|
|
if err != nil {
|
|
log.WithError(err).Debugf("error running pre-hook %s", prehook)
|
|
}
|
|
log.Debugf("pre-hook: %q", out)
|
|
}
|
|
|
|
data, senderKey, err := salty.Decrypt(cli.key, msg.Payload)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error decrypting message")
|
|
return err
|
|
}
|
|
|
|
msgs <- Message{Text: string(data), Key: senderKey}
|
|
|
|
if posthook != "" {
|
|
out, err := exec.RunCmd(exec.DefaultRunCmdTimeout, posthook, bytes.NewBuffer(data))
|
|
if err != nil {
|
|
log.WithError(err).Debugf("error running post-hook %s", posthook)
|
|
}
|
|
log.Debugf("post-hook: %q", out)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (cli *Client) Me() *Addr { return cli.me }
|
|
func (cli *Client) Key() *keys.EdX25519PublicKey { return cli.key.PublicKey() }
|
|
|
|
// Read subscribers to this user's inbox for new messages
|
|
func (cli *Client) Read(ctx context.Context, prehook, posthook string) chan Message {
|
|
uri, inbox := SplitInbox(cli.me.Endpoint().String())
|
|
bus := msgbus_client.NewClient(uri, nil)
|
|
|
|
msgs := make(chan Message)
|
|
s := bus.Subscribe(inbox, cli.handleMessage(prehook, posthook, msgs))
|
|
s.Start()
|
|
|
|
log.Debugf("Connected to %s/%s", uri, inbox)
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
s.Stop()
|
|
close(msgs)
|
|
}()
|
|
|
|
return msgs
|
|
}
|
|
|
|
// Send sends an encrypted message to the specified user
|
|
func (cli *Client) Send(user, msg string) error {
|
|
addr, err := cli.getAddr(user)
|
|
if err != nil {
|
|
return fmt.Errorf("error looking up user %s: %w", user, err)
|
|
}
|
|
|
|
return cli.SendToAddr(addr, msg)
|
|
}
|
|
|
|
func (cli *Client) SendToAddr(addr *Addr, msg string) error {
|
|
b, err := salty.Encrypt(cli.key, PackMessage(cli.me, msg), []string{addr.key.ID().String()})
|
|
if err != nil {
|
|
return fmt.Errorf("error encrypting message to %s: %w", addr, err)
|
|
}
|
|
|
|
if err := Send(addr.Endpoint().String(), string(b)); err != nil {
|
|
return fmt.Errorf("error sending message to %s: %w", addr, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Register sends a registration requestn to a broker
|
|
func (cli *Client) Register() error {
|
|
return nil
|
|
}
|