commit 1fa114b626be26464e637ef6adb776e64c873d46 Author: Martin Dosch Date: Sat Aug 4 12:16:28 2018 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1138d6f --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +config.json +.vscode + +# Binaries for programs and plugins +*.exe +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 +.glide/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..004fdb4 --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +# go-sendxmpp + +## about + +A little tool to send messages to an XMPP contact or MUC inspired by (but not as powerful as) +[sendxmpp](https://sendxmpp.hostname.sk/). + +## requirements + +* [go](https://golang.org/) + +## installation + +If you have *[GOPATH](https://github.com/golang/go/wiki/SettingGOPATH)* +set just run this commands: + +```bash +$ go get salsa.debian.org/mdosch-guest/go-sendxmpp +$ go install salsa.debian.org/mdosch-guest/go-sendxmpp +``` + +You will find the binary in `$GOPATH/bin` or, if set, `$GOBIN`. + +## usage + +The account details for logging into your XMPP account and at least `contact` +or `muc` must be specified. + +If `-message` is not specified you can either pipe a programs output to +`go-sendxmpp` or write in your terminal (put ^D in a new line to finish). + +If `-port` is not set, the standard port *5222* is used. + +```bash +Usage of ./go-sendxmpp: + -contact string + Recipient of the message. (default "alice@example.com") + -message string + The message you want to send. (default "Hello World!") + -muc string + MUC to send the message to. (default "offtopic@conference.example.com") + -muc-nick string + The nickname the bot uses in the MUC. (default "go-sendxmpp") + -pass string + Password for XMPP account. (default "ChangeThis!") + -port string + XMPP server port. (default "5222") + -server string + XMPP server address. (default "example.com") + -user string + Username for XMPP account. (default "bob@example.com") +``` + +### examples + +```bash +inxi -F | ./go-sendxmpp -pass 'ChangeThis!' -port '5222' -server 'example.com' -user 'bob@example.com' -muc 'test@conference.example.com' +``` + +```bash +./go-sendxmpp -pass 'ChangeThis!' -port '5222' -server 'example.com' -user 'bob@example.com' -muc 'test@conference.example.com' -message 'Hello World!' +``` diff --git a/go-sendxmpp b/go-sendxmpp new file mode 100755 index 0000000..326e54a Binary files /dev/null and b/go-sendxmpp differ diff --git a/go-sendxmpp.go b/go-sendxmpp.go new file mode 100644 index 0000000..a9b5664 --- /dev/null +++ b/go-sendxmpp.go @@ -0,0 +1,95 @@ +package main + +import ( + "bufio" + "flag" + "io" + "log" + "os" + "time" + + "github.com/mattn/go-xmpp" +) + +func main() { + + var ( + err error + server = flag.String("server", "example.com", "XMPP server address.") + port = flag.String("port", "5222", "XMPP server port.") + user = flag.String("user", "bob@example.com", "Username for XMPP account.") + password = flag.String("pass", "ChangeThis!", "Password for XMPP account.") + contact = flag.String("contact", "alice@example.com", "Recipient of the message.") + muc = flag.String("muc", "offtopic@conference.example.com", "MUC to send the message to.") + mucNick = flag.String("muc-nick", "go-sendxmpp", "The nickname the bot uses in the MUC.") + messagePtr = flag.String("message", "Hello World!", "The message you want to send.") + ) + + flag.Parse() + + message := *messagePtr + + if *contact == "alice@example.com" && *muc == "offtopic@conference.example.com" { + log.Fatal("No target specified.") + } + + options := xmpp.Options{ + Host: *server + ":" + *port, + User: *user, + Password: *password, + NoTLS: true, + StartTLS: true, + Debug: false, + } + + // Connect to server. + client, err := options.NewClient() + if err != nil { + log.Fatal(err) + } + + if message == "Hello World!" { + + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + + if message == "Hello World!" { + message = string(scanner.Text()) + } else { + message = message + "\n" + string(scanner.Text()) + } + } + + if err := scanner.Err(); err != nil { + if err != io.EOF { + log.Fatal(err) + } + } + } + + if *muc != "offtopic@conference.example.com" { + // Join the MUC + mucStatus, err := client.JoinMUCNoHistory(*muc, *mucNick) + if err != nil { + log.Fatal(err) + } + + // Exit if Status is > 300, see https://xmpp.org/registrar/mucstatus.html + if mucStatus > 300 { + log.Fatal("Couldn't join MUC. Status:", mucStatus) + } + + _, err = client.Send(xmpp.Chat{Remote: *muc, Type: "groupchat", Text: message}) + if err != nil { + log.Fatal(err) + } + } + + if *contact != "alice@example.com" { + _, err = client.Send(xmpp.Chat{Remote: *contact, Type: "chat", Text: message}) + if err != nil { + log.Fatal(err) + } + } + time.Sleep(1 * time.Second) +}