diff --git a/cmd/salty-chat/chat.go b/cmd/salty-chat/chat.go index d51b80d..582703d 100644 --- a/cmd/salty-chat/chat.go +++ b/cmd/salty-chat/chat.go @@ -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) diff --git a/cmd/salty-chat/root.go b/cmd/salty-chat/root.go index e17e130..087891f 100644 --- a/cmd/salty-chat/root.go +++ b/cmd/salty-chat/root.go @@ -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. diff --git a/identity.go b/identity.go index d758e45..e2a6d55 100644 --- a/identity.go +++ b/identity.go @@ -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) diff --git a/lookup.go b/lookup.go index b3179a6..798e1c1 100644 --- a/lookup.go +++ b/lookup.go @@ -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 { diff --git a/preflight.sh b/preflight.sh index 8e85f33..9297f13 100755 --- a/preflight.sh +++ b/preflight.sh @@ -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