6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-20 13:58:22 +00:00

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

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

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

@ -33,6 +33,12 @@ func DefaultIdentity() string {
return os.ExpandEnv("$HOME/.salty/$USER.key") 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 ... // CreateIdentity ...
func CreateIdentity(fn, user string) error { func CreateIdentity(fn, user string) error {
f, err := os.OpenFile(fn, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600) f, err := os.OpenFile(fn, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)

@ -68,6 +68,7 @@ func Lookup(addr string) (Config, error) {
res, err := Request(http.MethodGet, a.HashURI(), nil, nil) res, err := Request(http.MethodGet, a.HashURI(), nil, nil)
if err != nil { if err != nil {
// Fallback to plain user nick // 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) res, err = Request(http.MethodGet, a.URI(), nil, nil)
} }
if err != nil { if err != nil {

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