From d5bd444cd1c58d6fca3b4c7c7dbfdfb4f516f352 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Fri, 10 Aug 2018 15:03:04 +0200 Subject: [PATCH] Added flag '-i' which makes go-sendxmpp work with 'tail -f'. --- README.md | 32 +++++++++++++-------- go-sendxmpp.go | 78 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 78 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index f817d04..6f2fc60 100644 --- a/README.md +++ b/README.md @@ -42,23 +42,25 @@ account details via command line options: ```plain ./go-sendxmpp --help Usage: go-sendxmpp [-cdtx] [-f value] [--help] [-j value] [-p value] [-r value] [-u value] [parameters ...] - -c, --chatroom Send message to a chatroom. - -d, --debug Show debugging info. - -f, --file=value Set configuration file. (Default: ~/.sendxmpprc) - --help Show help. +-c, --chatroom Send message to a chatroom. + -d, --debug Show debugging info. + -f, --file=value Set configuration file. (Default: ~/.sendxmpprc) + --help Show help. + -i, --interactive Interactive mode (for use with e.g. 'tail -f'). -j, --jserver=value - XMPP server address. + XMPP server address. -m, --message=value - Set file including the message. + Set file including the message. -p, --password=value - Password for XMPP account. + Password for XMPP account. -r, --resource=value - Set resource. When sending to a chatroom this is used as - 'alias'. (Default: go-sendxmpp) - -t, --tls Use TLS. + Set resource. When sending to a chatroom this is used as + 'alias'. (Default: go-sendxmpp) + -t, --tls Use TLS. -u, --username=value - Username for XMPP account. - -x, --start-tls Use StartTLS. + Username for XMPP account. + -x, --start-tls Use StartTLS. + ``` ### examples @@ -79,4 +81,10 @@ Send a message to two groupchats (`-c`) using a configuration file. ```bash cat message.txt | ./go-sendxmpp -cf ./sendxmpp chat1@conference.example.com chat2@conference.example.com +``` + +Send file changes to two groupchats (`-c`) using a configuration file. + +```bash +tail -f example.log | ./go-sendxmpp -cif ./sendxmpp chat1@conference.example.com chat2@conference.example.com ``` \ No newline at end of file diff --git a/go-sendxmpp.go b/go-sendxmpp.go index e846365..7c74dbf 100644 --- a/go-sendxmpp.go +++ b/go-sendxmpp.go @@ -189,6 +189,7 @@ func main() { "When sending to a chatroom this is used as 'alias'. (Default: go-sendxmpp)") flagFile := getopt.StringLong("file", 'f', "", "Set configuration file. (Default: ~/.sendxmpprc)") flagMessageFile := getopt.StringLong("message", 'm', "", "Set file including the message.") + flagInteractive := getopt.BoolLong("interactive", 'i', "Interactive mode (for use with e.g. 'tail -f').") // Parse command line flags. getopt.Parse() @@ -273,21 +274,24 @@ func main() { log.Fatal(err) } - if message == "" { + // Skip reading message if '-i' or '--interactive' is set to work with e.g. 'tail -f'. + if *flagInteractive == false { + if message == "" { - scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { - if message == "" { - message = scanner.Text() - } else { - message = message + "\n" + scanner.Text() + if message == "" { + message = scanner.Text() + } else { + message = message + "\n" + scanner.Text() + } } - } - if err := scanner.Err(); err != nil { - if err != io.EOF { - log.Fatal(err) + if err := scanner.Err(); err != nil { + if err != io.EOF { + log.Fatal(err) + } } } } @@ -307,13 +311,32 @@ func main() { if mucStatus > 300 { log.Fatal("Couldn't join MUC. Status:", mucStatus) } + } + // Send in endless loop (for usage with e.g. "tail -f"). + if *flagInteractive == true { + for { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + message = scanner.Text() + for _, recipient := range recipients { + _, err = client.Send(xmpp.Chat{Remote: recipient, Type: "groupchat", Text: message}) + if err != nil { + log.Fatal(err) + } + } + } + } else { // Send the message. - _, err = client.Send(xmpp.Chat{Remote: recipient, Type: "groupchat", Text: message}) - if err != nil { - log.Fatal(err) + for _, recipient := range recipients { + _, err = client.Send(xmpp.Chat{Remote: recipient, Type: "groupchat", Text: message}) + if err != nil { + log.Fatal(err) + } } + } + for _, recipient := range recipients { // After sending the message, leave the Muc _, err = client.LeaveMUC(recipient) if err != nil { @@ -321,15 +344,30 @@ func main() { } } } else { + // If the chatroom flag is not set, send message to contact(s). - for _, recipient := range recipients { - - // If the chatroom flag is not set, send message to contact(s). - _, err = client.Send(xmpp.Chat{Remote: recipient, Type: "chat", Text: message}) - if err != nil { - log.Fatal(err) + // Send in endless loop (for usage with e.g. "tail -f"). + if *flagInteractive == true { + for { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + message = scanner.Text() + for _, recipient := range recipients { + _, err = client.Send(xmpp.Chat{Remote: recipient, Type: "chat", Text: message}) + if err != nil { + log.Fatal(err) + } + } + } + } else { + for _, recipient := range recipients { + _, err = client.Send(xmpp.Chat{Remote: recipient, Type: "chat", Text: message}) + if err != nil { + log.Fatal(err) + } } } } + time.Sleep(1 * time.Second) }