mirror of
https://salsa.debian.org/mdosch/go-sendxmpp
synced 2024-11-17 03:25:33 +00:00
96 lines
2.2 KiB
Go
96 lines
2.2 KiB
Go
|
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)
|
||
|
}
|