Added flag '-i' which makes go-sendxmpp work with 'tail -f'.

http_upload
Martin Dosch 6 years ago
parent 4a2a409bd1
commit d5bd444cd1

@ -42,23 +42,25 @@ account details via command line options:
```plain ```plain
./go-sendxmpp --help ./go-sendxmpp --help
Usage: go-sendxmpp [-cdtx] [-f value] [--help] [-j value] [-p value] [-r value] [-u value] [parameters ...] Usage: go-sendxmpp [-cdtx] [-f value] [--help] [-j value] [-p value] [-r value] [-u value] [parameters ...]
-c, --chatroom Send message to a chatroom. -c, --chatroom Send message to a chatroom.
-d, --debug Show debugging info. -d, --debug Show debugging info.
-f, --file=value Set configuration file. (Default: ~/.sendxmpprc) -f, --file=value Set configuration file. (Default: ~/.sendxmpprc)
--help Show help. --help Show help.
-i, --interactive Interactive mode (for use with e.g. 'tail -f').
-j, --jserver=value -j, --jserver=value
XMPP server address. XMPP server address.
-m, --message=value -m, --message=value
Set file including the message. Set file including the message.
-p, --password=value -p, --password=value
Password for XMPP account. Password for XMPP account.
-r, --resource=value -r, --resource=value
Set resource. When sending to a chatroom this is used as Set resource. When sending to a chatroom this is used as
'alias'. (Default: go-sendxmpp) 'alias'. (Default: go-sendxmpp)
-t, --tls Use TLS. -t, --tls Use TLS.
-u, --username=value -u, --username=value
Username for XMPP account. Username for XMPP account.
-x, --start-tls Use StartTLS. -x, --start-tls Use StartTLS.
``` ```
### examples ### examples
@ -79,4 +81,10 @@ Send a message to two groupchats (`-c`) using a configuration file.
```bash ```bash
cat message.txt | ./go-sendxmpp -cf ./sendxmpp chat1@conference.example.com chat2@conference.example.com 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
``` ```

@ -189,6 +189,7 @@ func main() {
"When sending to a chatroom this is used as 'alias'. (Default: go-sendxmpp)") "When sending to a chatroom this is used as 'alias'. (Default: go-sendxmpp)")
flagFile := getopt.StringLong("file", 'f', "", "Set configuration file. (Default: ~/.sendxmpprc)") flagFile := getopt.StringLong("file", 'f', "", "Set configuration file. (Default: ~/.sendxmpprc)")
flagMessageFile := getopt.StringLong("message", 'm', "", "Set file including the message.") 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. // Parse command line flags.
getopt.Parse() getopt.Parse()
@ -273,21 +274,24 @@ func main() {
log.Fatal(err) 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) scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() { for scanner.Scan() {
if message == "" { if message == "" {
message = scanner.Text() message = scanner.Text()
} else { } else {
message = message + "\n" + scanner.Text() message = message + "\n" + scanner.Text()
}
} }
}
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
if err != io.EOF { if err != io.EOF {
log.Fatal(err) log.Fatal(err)
}
} }
} }
} }
@ -307,13 +311,32 @@ func main() {
if mucStatus > 300 { if mucStatus > 300 {
log.Fatal("Couldn't join MUC. Status:", mucStatus) 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. // Send the message.
_, err = client.Send(xmpp.Chat{Remote: recipient, Type: "groupchat", Text: message}) for _, recipient := range recipients {
if err != nil { _, err = client.Send(xmpp.Chat{Remote: recipient, Type: "groupchat", Text: message})
log.Fatal(err) if err != nil {
log.Fatal(err)
}
} }
}
for _, recipient := range recipients {
// After sending the message, leave the Muc // After sending the message, leave the Muc
_, err = client.LeaveMUC(recipient) _, err = client.LeaveMUC(recipient)
if err != nil { if err != nil {
@ -321,15 +344,30 @@ func main() {
} }
} }
} else { } else {
// If the chatroom flag is not set, send message to contact(s).
for _, recipient := range recipients { // Send in endless loop (for usage with e.g. "tail -f").
if *flagInteractive == true {
// If the chatroom flag is not set, send message to contact(s). for {
_, err = client.Send(xmpp.Chat{Remote: recipient, Type: "chat", Text: message}) scanner := bufio.NewScanner(os.Stdin)
if err != nil { scanner.Scan()
log.Fatal(err) 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) time.Sleep(1 * time.Second)
} }

Loading…
Cancel
Save