6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-28 17:51:04 +00:00
prologic-saltyim/readmsgs.go

49 lines
902 B
Go
Raw Normal View History

2022-03-19 02:35:04 +00:00
package saltyim
import (
"fmt"
"os"
"git.mills.io/prologic/msgbus"
"git.mills.io/prologic/msgbus/client"
2022-03-19 14:46:21 +00:00
"github.com/gen2brain/beeep"
2022-03-19 02:35:04 +00:00
"github.com/keys-pub/keys"
2022-03-19 02:35:04 +00:00
"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)
2022-03-19 14:46:21 +00:00
// Errors are ignored silently
_ = beeep.Alert("Salty IM", "You have a new Salty Message", "")
2022-03-19 02:35:04 +00:00
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
}