6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-25 16:28:20 +00:00
prologic-saltyim/readmsgs.go
2022-03-20 00:46:21 +10:00

49 lines
902 B
Go

package saltyim
import (
"fmt"
"os"
"git.mills.io/prologic/msgbus"
"git.mills.io/prologic/msgbus/client"
"github.com/gen2brain/beeep"
"github.com/keys-pub/keys"
"go.mills.io/salty"
)
func handleMessage(key *keys.EdX25519Key, msgs chan string) msgbus.HandlerFunc {
return func(msg *msgbus.Message) error {
data, _, err := salty.Decrypt(key, msg.Payload)
if err != nil {
fmt.Fprintf(os.Stderr, "error decrypting message")
return err
}
msgs <- string(data)
// Errors are ignored silently
_ = beeep.Alert("Salty IM", "You have a new Salty Message", "")
return nil
}
}
// Read ...
func Read(key *keys.EdX25519Key, uri, inbox string, stop chan struct{}) chan string {
client := client.NewClient(uri, nil)
msgs := make(chan string)
s := client.Subscribe(inbox, handleMessage(key, msgs))
s.Start()
go func() {
<-stop
s.Stop()
close(msgs)
}()
return msgs
}