feat: hax for great justice (#14)

- Dont send message on empty
- inbox flag to override $USER
- Filter messages to only chat with partner.

NOTE: might be better to parse the message in loop to handle events outside the formatting code.

NOTE NOTE: i think we might need to revisit read recipts.. maybe including user@domain?

Co-authored-by: Jon Lundy <jon@xuu.cc>
Reviewed-on: https://git.mills.io/prologic/saltyim/pulls/14
Co-authored-by: xuu <xuu@noreply@mills.io>
Co-committed-by: xuu <xuu@noreply@mills.io>
This commit is contained in:
xuu 2022-03-19 21:33:03 +00:00 committed by James Mills
parent a2ac6a9ef3
commit d1d33e45c6
5 changed files with 45 additions and 9 deletions

View File

@ -5,6 +5,7 @@ import (
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
@ -12,6 +13,7 @@ import (
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.yarn.social/lextwt"
"go.mills.io/salty"
"go.mills.io/saltyim"
@ -38,7 +40,8 @@ messages to the user via their endpoint.`,
Run: func(cmd *cobra.Command, args []string) {
uri := viper.GetString("uri")
identity := viper.GetString("identity")
chat(identity, uri, args[0])
inbox := viper.GetString("inbox")
chat(identity, uri, inbox, args[0])
},
}
@ -46,7 +49,7 @@ func init() {
rootCmd.AddCommand(chatCmd)
}
func chat(identity, uri string, user string) {
func chat(identity, uri, inbox, user string) {
key, me, err := saltyim.GetIdentity(identity)
if err != nil {
fmt.Fprintf(os.Stderr, "error opening identity: %q", identity)
@ -63,10 +66,11 @@ func chat(identity, uri string, user string) {
os.Exit(2)
}
inbox := os.Getenv("USER")
tm.Clear()
tm.Printf("Chatting with %s\n", user)
tm.Printf("Chatting as %s", me)
tm.Printf(" via %s/%s\n", uri, inbox)
tm.Printf(" with %s", user)
tm.Printf(" via %s\n", config.Endpoint)
tm.Flush()
stop := make(chan struct{})
@ -86,8 +90,21 @@ func chat(identity, uri string, user string) {
go func() {
defer wg.Done()
for msg := range saltyim.Read(key, uri, inbox, stop) {
tm.Println(formatMsg(msg))
tm.Flush()
s, err := lextwt.ParseSalty(msg)
if err != nil {
continue
}
switch s := s.(type) {
case *lextwt.SaltyEvent:
case *lextwt.SaltyText:
if s.User.String() != user {
continue
}
tm.Println(formatMsg(msg))
tm.Flush()
}
}
}()
wg.Add(1)
@ -101,6 +118,10 @@ func chat(identity, uri string, user string) {
break
}
if strings.TrimSpace(msg) == "" {
continue
}
b, err := salty.Encrypt(key, packMsg(me, msg), []string{config.Key})
if err != nil {
log.Fatalf("error encrypting message: %v", err)

View File

@ -62,6 +62,11 @@ func init() {
"Use the identity file at PATH",
)
rootCmd.PersistentFlags().StringP(
"inbox", "b", saltyim.DefaultInbox(),
"Use the inbox with broker",
)
rootCmd.PersistentFlags().StringP(
"uri", "u", "https://msgbus.mills.io",
"URI to connect to saltyim broker",
@ -74,6 +79,9 @@ func init() {
viper.BindPFlag("identity", rootCmd.PersistentFlags().Lookup("identity"))
viper.SetDefault("identity", saltyim.DefaultIdentity())
viper.BindPFlag("inbox", rootCmd.PersistentFlags().Lookup("inbox"))
viper.SetDefault("inbox", saltyim.DefaultInbox())
}
// initConfig reads in config file and ENV variables if set.

View File

@ -33,6 +33,12 @@ func DefaultIdentity() string {
return os.ExpandEnv("$HOME/.salty/$USER.key")
}
// DefaultInbox returns a default inbox file (if one exists) otherwise
// returns an empty string
func DefaultInbox() string {
return os.ExpandEnv("$USER")
}
// CreateIdentity ...
func CreateIdentity(fn, user string) error {
f, err := os.OpenFile(fn, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)

View File

@ -68,6 +68,7 @@ func Lookup(addr string) (Config, error) {
res, err := Request(http.MethodGet, a.HashURI(), nil, nil)
if err != nil {
// Fallback to plain user nick
log.WithError(err).Warn("failed to get hash uri. attempting plaintext.")
res, err = Request(http.MethodGet, a.URI(), nil, nil)
}
if err != nil {

View File

@ -75,8 +75,8 @@ check_goversion() {
gover="$(go version | grep -o -E 'go[0-9]+\.[0-9]+(\.[0-9]+)?')"
if ! go version | grep -E 'go1\.17(\.[0-9]+)?' > /dev/null; then
log2 "Go 1.17 is required, found ${gover}"
if ! go version | grep -E 'go1\.1[78](\.[0-9]+)?' > /dev/null; then
log2 "Go 1.17+ is required, found ${gover}"
return 1
fi