From a2f700d141fe9aa868a1b5cee680b4793988a391 Mon Sep 17 00:00:00 2001 From: Anjandev Momi Date: Sun, 6 Feb 2022 17:25:00 -0500 Subject: [PATCH] add -l flag: listen for messages --- go-sendxmpp.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/go-sendxmpp.go b/go-sendxmpp.go index e90b7e7..da0e5b9 100644 --- a/go-sendxmpp.go +++ b/go-sendxmpp.go @@ -237,6 +237,7 @@ func main() { flagSkipVerify := getopt.BoolLong("no-tls-verify", 'n', "Skip verification of TLS certificates (not recommended).") flagRaw := getopt.BoolLong("raw", 0, "Send raw XML.") + flagListen := getopt.BoolLong("listen", 'l', "Listen for first message from user and echo.") // Parse command line flags. getopt.Parse() @@ -248,10 +249,11 @@ func main() { } // Read recipients from command line and quit if none are specified. - // For sending raw XML it's not required to specify a recipient except - // for MUCs (go-sendxmpp will join the MUC automatically). + // For listening or sending raw XML it's not required to specify a recipient except + // when sending raw messages to MUCs (go-sendxmpp will join the MUC automatically). recipients := getopt.Args() - if (len(recipients) == 0 && !*flagRaw) || (len(recipients) == 0 && *flagChatroom) { + if (len(recipients) == 0 && !*flagRaw && !*flagListen) || + (len(recipients) == 0 && *flagChatroom) { log.Fatal("No recipient specified.") } @@ -361,7 +363,8 @@ func main() { } // Skip reading message if '-i' or '--interactive' is set to work with e.g. 'tail -f'. - if !*flagInteractive { + // Also for listening mode. + if !*flagInteractive && !*flagListen { if message == "" { scanner := bufio.NewScanner(os.Stdin) @@ -418,6 +421,24 @@ func main() { return } + if *flagListen { + for { + recieved, err := client.Recv() + if err != nil { + log.Println(err) + } + + switch v := recieved.(type) { + case xmpp.Chat: + if v.Text != "" { + fmt.Println(v.Text) + } + default: + continue + } + } + } + // Send message to chatroom(s) if the flag is set. if *flagChatroom {