diff --git a/event.go b/event.go index 30959fb..c538ecb 100644 --- a/event.go +++ b/event.go @@ -97,6 +97,16 @@ example: }, ArgsUsage: "[relay...]", Action: func(c *cli.Context) error { + // try to connect to the relays here + var relays []*nostr.Relay + if relayUrls := c.Args().Slice(); len(relayUrls) > 0 { + _, relays = connectToAllRelays(c.Context, relayUrls) + if len(relays) == 0 { + fmt.Fprintf(os.Stderr, "failed to connect to any of the given relays.\n") + os.Exit(3) + } + } + // gather the secret key first sec := c.String("sec") if c.Bool("prompt-sec") { @@ -204,12 +214,12 @@ example: } } - relays := c.Args().Slice() if len(relays) > 0 { fmt.Println(evt.String()) - for _, url := range relays { - fmt.Fprintf(os.Stderr, "publishing to %s... ", url) - if relay, err := nostr.RelayConnect(c.Context, url); err != nil { + os.Stdout.Sync() + for _, relay := range relays { + fmt.Fprintf(os.Stderr, "publishing to %s... ", relay.URL) + if relay, err := nostr.RelayConnect(c.Context, relay.URL); err != nil { fmt.Fprintf(os.Stderr, "failed to connect: %s\n", err) } else { ctx, cancel := context.WithTimeout(c.Context, 10*time.Second) diff --git a/helpers.go b/helpers.go index 8960ae1..0be6c38 100644 --- a/helpers.go +++ b/helpers.go @@ -9,6 +9,7 @@ import ( "os" "strings" + "github.com/nbd-wtf/go-nostr" "github.com/urfave/cli/v2" ) @@ -99,6 +100,21 @@ func validate32BytesHex(target string) error { return nil } +func connectToAllRelays(ctx context.Context, relayUrls []string) (*nostr.SimplePool, []*nostr.Relay) { + relays := make([]*nostr.Relay, 0, len(relayUrls)) + pool := nostr.NewSimplePool(ctx) + for _, url := range relayUrls { + fmt.Fprintf(os.Stderr, "connecting to %s... ", url) + if relay, err := pool.EnsureRelay(url); err == nil { + relays = append(relays, relay) + fmt.Fprintf(os.Stderr, "ok.\n") + } else { + fmt.Fprintf(os.Stderr, err.Error()+"\n") + } + } + return pool, relays +} + func lineProcessingError(c *cli.Context, msg string, args ...any) { c.Context = context.WithValue(c.Context, LINE_PROCESSING_ERROR, true) fmt.Fprintf(os.Stderr, msg+"\n", args...) diff --git a/req.go b/req.go index 06c0955..cbc11e9 100644 --- a/req.go +++ b/req.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "os" "strings" "github.com/nbd-wtf/go-nostr" @@ -95,6 +96,21 @@ example: }, ArgsUsage: "[relay...]", Action: func(c *cli.Context) error { + var pool *nostr.SimplePool + relayUrls := c.Args().Slice() + if len(relayUrls) > 0 { + var relays []*nostr.Relay + pool, relays = connectToAllRelays(c.Context, relayUrls) + if len(relays) == 0 { + fmt.Fprintf(os.Stderr, "failed to connect to any of the given relays.\n") + os.Exit(3) + } + relayUrls = make([]string, len(relays)) + for i, relay := range relays { + relayUrls[i] = relay.URL + } + } + for stdinFilter := range getStdinLinesOrBlank() { filter := nostr.Filter{} if stdinFilter != "" { @@ -155,14 +171,12 @@ example: filter.Limit = limit } - relays := c.Args().Slice() - if len(relays) > 0 { - pool := nostr.NewSimplePool(c.Context) + if len(relayUrls) > 0 { fn := pool.SubManyEose if c.Bool("stream") { fn = pool.SubMany } - for ie := range fn(c.Context, relays, nostr.Filters{filter}) { + for ie := range fn(c.Context, relayUrls, nostr.Filters{filter}) { fmt.Println(ie.Event) } } else {