2020-04-05 20:02:09 +00:00
|
|
|
// Copyright 2018 - 2020 Martin Dosch.
|
|
|
|
// Use of this source code is governed by the BSD-2-clause
|
2020-04-04 07:42:09 +00:00
|
|
|
// license that can be found in the LICENSE file.
|
2018-08-04 10:19:32 +00:00
|
|
|
|
2018-08-04 10:16:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-04-03 12:35:03 +00:00
|
|
|
"crypto/tls"
|
2018-08-10 10:14:21 +00:00
|
|
|
"errors"
|
2018-08-04 10:16:28 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
2018-08-10 10:14:21 +00:00
|
|
|
"os/user"
|
2019-02-21 17:20:54 +00:00
|
|
|
"runtime"
|
2018-08-10 10:14:21 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-02-20 20:22:31 +00:00
|
|
|
"time"
|
2018-08-04 10:16:28 +00:00
|
|
|
|
2020-01-22 18:31:31 +00:00
|
|
|
"github.com/mattn/go-xmpp" // BSD-3-Clause
|
|
|
|
"github.com/pborman/getopt/v2" // BSD-3-Clause
|
2018-08-04 10:16:28 +00:00
|
|
|
)
|
|
|
|
|
2018-08-10 10:14:21 +00:00
|
|
|
type configuration struct {
|
|
|
|
username string
|
|
|
|
jserver string
|
|
|
|
port string
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opens the config file and returns the specified values
|
|
|
|
// for username, server and port.
|
|
|
|
func parseConfig(configPath string) (configuration, error) {
|
|
|
|
|
|
|
|
var (
|
|
|
|
output configuration
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Use ~/.sendxmpprc if no config path is specified.
|
|
|
|
if configPath == "" {
|
|
|
|
// Get systems user config path.
|
|
|
|
osConfigDir := os.Getenv("$XDG_CONFIG_HOME")
|
|
|
|
if osConfigDir != "" {
|
|
|
|
configPath = osConfigDir + "/.sendxmpprc"
|
|
|
|
} else {
|
|
|
|
// Get the current user.
|
|
|
|
curUser, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return output, err
|
|
|
|
}
|
|
|
|
// Get home directory.
|
|
|
|
home := curUser.HomeDir
|
|
|
|
if home == "" {
|
2019-02-13 17:31:58 +00:00
|
|
|
return output, errors.New("no home directory found")
|
2018-08-10 10:14:21 +00:00
|
|
|
}
|
|
|
|
configPath = home + "/.sendxmpprc"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that config file is existing.
|
|
|
|
info, err := os.Stat(configPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return output, err
|
|
|
|
}
|
2019-02-21 17:20:54 +00:00
|
|
|
// Only check file permissions if we are not running on windows.
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
// Check for file permissions. Must be 600 or 400.
|
|
|
|
perm := info.Mode().Perm()
|
|
|
|
permissions := strconv.FormatInt(int64(perm), 8)
|
|
|
|
if permissions != "600" && permissions != "400" {
|
|
|
|
return output, errors.New("Wrong permissions for " + configPath + ": " +
|
2019-02-21 17:24:59 +00:00
|
|
|
permissions + " instead of 400 or 600.")
|
2019-02-21 17:20:54 +00:00
|
|
|
}
|
2018-08-10 10:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open config file.
|
|
|
|
file, err := os.Open(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return output, err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
|
|
|
|
// Read config file per line.
|
|
|
|
for scanner.Scan() {
|
2020-04-02 17:35:12 +00:00
|
|
|
if strings.HasPrefix(scanner.Text(), "#") {
|
2018-08-10 10:14:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
row := strings.Split(scanner.Text(), " ")
|
|
|
|
|
|
|
|
switch row[0] {
|
|
|
|
case "username:":
|
|
|
|
output.username = row[1]
|
|
|
|
case "jserver:":
|
|
|
|
output.jserver = row[1]
|
|
|
|
case "password:":
|
|
|
|
output.password = row[1]
|
|
|
|
case "port:":
|
|
|
|
output.port = row[1]
|
|
|
|
default:
|
|
|
|
if len(row) >= 2 {
|
2020-04-02 17:35:12 +00:00
|
|
|
if strings.Contains(scanner.Text(), ";") {
|
2018-08-10 10:14:21 +00:00
|
|
|
output.username = strings.Split(row[0], ";")[0]
|
|
|
|
output.jserver = strings.Split(row[0], ";")[1]
|
|
|
|
output.password = row[1]
|
|
|
|
} else {
|
|
|
|
output.username = strings.Split(row[0], ":")[0]
|
|
|
|
output.jserver = strings.Split(row[0], "@")[1]
|
|
|
|
output.password = row[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 17:15:46 +00:00
|
|
|
file.Close()
|
2018-08-10 10:14:21 +00:00
|
|
|
|
|
|
|
return output, err
|
|
|
|
}
|
|
|
|
|
2018-08-10 11:12:21 +00:00
|
|
|
func readMessage(messageFilePath string) (string, error) {
|
|
|
|
var (
|
|
|
|
output string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Check that message file is existing.
|
|
|
|
_, err = os.Stat(messageFilePath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return output, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open message file.
|
|
|
|
file, err := os.Open(messageFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return output, err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
|
|
|
if output == "" {
|
|
|
|
output = scanner.Text()
|
|
|
|
} else {
|
|
|
|
output = output + "\n" + scanner.Text()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:15:46 +00:00
|
|
|
file.Close()
|
|
|
|
|
2018-08-10 11:12:21 +00:00
|
|
|
return output, err
|
|
|
|
}
|
|
|
|
|
2018-08-04 10:16:28 +00:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
var (
|
2020-04-11 14:44:20 +00:00
|
|
|
err error
|
|
|
|
message, user, server, password string
|
2018-08-04 10:16:28 +00:00
|
|
|
)
|
|
|
|
|
2018-08-10 10:14:21 +00:00
|
|
|
// Define command line flags.
|
|
|
|
flagHelp := getopt.BoolLong("help", 0, "Show help.")
|
2020-04-09 12:58:03 +00:00
|
|
|
flagHttpUpload := getopt.StringLong("http-upload", 0, "", "Send a file via http-upload.")
|
2018-08-10 10:14:21 +00:00
|
|
|
flagDebug := getopt.BoolLong("debug", 'd', "Show debugging info.")
|
|
|
|
flagServer := getopt.StringLong("jserver", 'j', "", "XMPP server address.")
|
|
|
|
flagUser := getopt.StringLong("username", 'u', "", "Username for XMPP account.")
|
|
|
|
flagPassword := getopt.StringLong("password", 'p', "", "Password for XMPP account.")
|
|
|
|
flagChatroom := getopt.BoolLong("chatroom", 'c', "Send message to a chatroom.")
|
|
|
|
flagTLS := getopt.BoolLong("tls", 't', "Use TLS.")
|
|
|
|
flagStartTLS := getopt.BoolLong("start-tls", 'x', "Use StartTLS.")
|
|
|
|
flagResource := getopt.StringLong("resource", 'r', "go-sendxmpp", "Set resource. "+
|
|
|
|
"When sending to a chatroom this is used as 'alias'. (Default: go-sendxmpp)")
|
2018-08-10 11:12:21 +00:00
|
|
|
flagFile := getopt.StringLong("file", 'f', "", "Set configuration file. (Default: ~/.sendxmpprc)")
|
|
|
|
flagMessageFile := getopt.StringLong("message", 'm', "", "Set file including the message.")
|
2018-08-10 13:03:04 +00:00
|
|
|
flagInteractive := getopt.BoolLong("interactive", 'i', "Interactive mode (for use with e.g. 'tail -f').")
|
2020-04-05 19:49:17 +00:00
|
|
|
flagSkipVerify := getopt.BoolLong("no-tls-verify", 'n',
|
|
|
|
"Skip verification of TLS certificates (not recommended).")
|
2018-08-10 10:14:21 +00:00
|
|
|
|
|
|
|
// Parse command line flags.
|
|
|
|
getopt.Parse()
|
|
|
|
|
|
|
|
// If requested, show help and quit.
|
|
|
|
if *flagHelp {
|
|
|
|
getopt.Usage()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read recipients from command line and quit if none are specified.
|
|
|
|
recipients := getopt.Args()
|
|
|
|
if len(recipients) == 0 {
|
|
|
|
log.Fatal("No recipient specified.")
|
|
|
|
}
|
2018-08-04 10:16:28 +00:00
|
|
|
|
2018-08-10 10:14:21 +00:00
|
|
|
// Quit if unreasonable TLS setting is set.
|
2020-01-22 18:49:32 +00:00
|
|
|
if *flagStartTLS && *flagTLS {
|
2018-08-10 10:14:21 +00:00
|
|
|
log.Fatal("Use either TLS or StartTLS.")
|
|
|
|
}
|
|
|
|
|
2020-04-04 07:40:24 +00:00
|
|
|
// Check that all recipient JIDs are valid.
|
|
|
|
for i, recipient := range recipients {
|
|
|
|
validatedJid, err := MarshalJID(recipient)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
recipients[i] = validatedJid
|
|
|
|
}
|
|
|
|
|
2020-04-03 12:28:08 +00:00
|
|
|
// Read configuration file if user, server or password is not specified.
|
2019-02-20 21:23:29 +00:00
|
|
|
if *flagUser == "" || *flagServer == "" || *flagPassword == "" {
|
|
|
|
// Read configuration from file.
|
|
|
|
config, err := parseConfig(*flagFile)
|
|
|
|
if err != nil {
|
2020-04-03 12:28:08 +00:00
|
|
|
log.Fatal("Error parsing ", *flagFile, ": ", err)
|
2019-02-20 21:23:29 +00:00
|
|
|
}
|
|
|
|
// Set connection options according to config.
|
|
|
|
user = config.username
|
|
|
|
server = config.jserver
|
|
|
|
password = config.password
|
|
|
|
if config.port != "" {
|
|
|
|
server = server + ":" + config.port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:14:10 +00:00
|
|
|
// Overwrite user if specified via command line flag.
|
|
|
|
if *flagUser != "" {
|
|
|
|
user = *flagUser
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite server if specified via command line flag.
|
|
|
|
if *flagServer != "" {
|
|
|
|
server = *flagServer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite password if specified via command line flag.
|
|
|
|
if *flagPassword != "" {
|
|
|
|
password = *flagPassword
|
|
|
|
}
|
|
|
|
|
2020-04-09 15:01:48 +00:00
|
|
|
if (*flagHttpUpload != "") && (*flagInteractive || (*flagMessageFile != "")) {
|
|
|
|
if *flagInteractive {
|
|
|
|
log.Fatal("Interactive mode and http upload can't" +
|
|
|
|
" be used at the same time.")
|
|
|
|
}
|
|
|
|
if *flagMessageFile != "" {
|
|
|
|
log.Fatal("You can't send a message while using" +
|
|
|
|
" http upload.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 17:35:12 +00:00
|
|
|
// Use ALPN
|
2020-04-03 12:35:03 +00:00
|
|
|
var tlsConfig tls.Config
|
2020-04-05 19:50:28 +00:00
|
|
|
tlsConfig.ServerName = strings.Split(user, "@")[1]
|
2020-04-02 17:35:12 +00:00
|
|
|
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "xmpp-client")
|
2020-04-05 19:49:17 +00:00
|
|
|
tlsConfig.InsecureSkipVerify = *flagSkipVerify
|
2020-04-02 17:35:12 +00:00
|
|
|
|
2018-08-10 10:14:21 +00:00
|
|
|
// Set XMPP connection options.
|
2018-08-04 10:16:28 +00:00
|
|
|
options := xmpp.Options{
|
2020-04-02 17:35:12 +00:00
|
|
|
Host: server,
|
|
|
|
User: user,
|
|
|
|
Resource: *flagResource,
|
|
|
|
Password: password,
|
|
|
|
NoTLS: !*flagTLS,
|
|
|
|
StartTLS: *flagStartTLS,
|
|
|
|
Debug: *flagDebug,
|
|
|
|
TLSConfig: &tlsConfig,
|
2018-08-04 10:16:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 11:12:21 +00:00
|
|
|
// Read message from file.
|
|
|
|
if *flagMessageFile != "" {
|
|
|
|
message, err = readMessage(*flagMessageFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 10:16:28 +00:00
|
|
|
// Connect to server.
|
|
|
|
client, err := options.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-09 12:58:03 +00:00
|
|
|
if *flagHttpUpload != "" {
|
2020-04-09 13:47:03 +00:00
|
|
|
message = httpUpload(client, tlsConfig.ServerName,
|
2020-04-09 12:58:03 +00:00
|
|
|
*flagHttpUpload)
|
|
|
|
}
|
|
|
|
|
2018-08-10 13:03:04 +00:00
|
|
|
// Skip reading message if '-i' or '--interactive' is set to work with e.g. 'tail -f'.
|
2020-01-22 18:49:32 +00:00
|
|
|
if !*flagInteractive {
|
2018-08-10 13:03:04 +00:00
|
|
|
if message == "" {
|
2018-08-04 10:16:28 +00:00
|
|
|
|
2018-08-10 13:03:04 +00:00
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
2018-08-04 10:16:28 +00:00
|
|
|
|
2018-08-10 13:03:04 +00:00
|
|
|
if message == "" {
|
|
|
|
message = scanner.Text()
|
|
|
|
} else {
|
|
|
|
message = message + "\n" + scanner.Text()
|
|
|
|
}
|
2018-08-04 10:16:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 13:03:04 +00:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
if err != io.EOF {
|
2020-04-03 12:14:48 +00:00
|
|
|
// Close connection and quit.
|
|
|
|
_ = client.Close()
|
2018-08-10 13:03:04 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-08-04 10:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 10:14:21 +00:00
|
|
|
// Send message to chatroom(s) if the flag is set.
|
|
|
|
if *flagChatroom {
|
2018-08-04 10:16:28 +00:00
|
|
|
|
2018-08-10 10:14:21 +00:00
|
|
|
for _, recipient := range recipients {
|
2018-08-04 10:16:28 +00:00
|
|
|
|
2018-08-10 10:14:21 +00:00
|
|
|
// Join the MUC.
|
2018-08-13 18:12:06 +00:00
|
|
|
_, err := client.JoinMUCNoHistory(recipient, *flagResource)
|
2018-08-10 10:14:21 +00:00
|
|
|
if err != nil {
|
2020-04-03 12:14:48 +00:00
|
|
|
// Try to nicely close connection,
|
|
|
|
// even if there was an error joining.
|
|
|
|
_ = client.Close()
|
2018-08-10 10:14:21 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-08-10 13:03:04 +00:00
|
|
|
}
|
2018-08-10 10:14:21 +00:00
|
|
|
|
2018-08-10 13:03:04 +00:00
|
|
|
// Send in endless loop (for usage with e.g. "tail -f").
|
2020-01-22 18:49:32 +00:00
|
|
|
if *flagInteractive {
|
2018-08-10 13:03:04 +00:00
|
|
|
for {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
scanner.Scan()
|
|
|
|
message = scanner.Text()
|
|
|
|
for _, recipient := range recipients {
|
2018-08-20 11:24:48 +00:00
|
|
|
_, err = client.Send(xmpp.Chat{Remote: recipient,
|
|
|
|
Type: "groupchat", Text: message})
|
2018-08-10 13:03:04 +00:00
|
|
|
if err != nil {
|
2020-04-03 12:14:48 +00:00
|
|
|
// Try to nicely close connection,
|
|
|
|
// even if there was an error sending.
|
|
|
|
_ = client.Close()
|
2018-08-10 13:03:04 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-10 10:14:21 +00:00
|
|
|
// Send the message.
|
2018-08-10 13:03:04 +00:00
|
|
|
for _, recipient := range recipients {
|
2020-04-09 14:49:27 +00:00
|
|
|
if *flagHttpUpload != "" {
|
2020-04-11 16:59:32 +00:00
|
|
|
_, err = client.Send(xmpp.Chat{Remote: recipient,
|
|
|
|
Type: "groupchat", Ooburl: message, Text: message})
|
2020-04-09 14:49:27 +00:00
|
|
|
} else {
|
|
|
|
_, err = client.Send(xmpp.Chat{Remote: recipient,
|
|
|
|
Type: "groupchat", Text: message})
|
|
|
|
}
|
2018-08-10 13:03:04 +00:00
|
|
|
if err != nil {
|
2020-04-03 12:14:48 +00:00
|
|
|
// Try to nicely close connection,
|
|
|
|
// even if there was an error sending.
|
|
|
|
_ = client.Close()
|
2018-08-10 13:03:04 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-08-10 10:14:21 +00:00
|
|
|
}
|
2018-08-10 13:03:04 +00:00
|
|
|
}
|
2018-08-10 10:14:21 +00:00
|
|
|
|
2018-08-10 13:03:04 +00:00
|
|
|
for _, recipient := range recipients {
|
2018-08-10 10:14:21 +00:00
|
|
|
// After sending the message, leave the Muc
|
|
|
|
_, err = client.LeaveMUC(recipient)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2018-08-04 10:16:28 +00:00
|
|
|
}
|
2018-08-10 10:14:21 +00:00
|
|
|
} else {
|
2018-08-10 13:03:04 +00:00
|
|
|
// If the chatroom flag is not set, send message to contact(s).
|
2018-08-04 10:16:28 +00:00
|
|
|
|
2018-08-10 13:03:04 +00:00
|
|
|
// Send in endless loop (for usage with e.g. "tail -f").
|
2020-01-22 18:49:32 +00:00
|
|
|
if *flagInteractive {
|
2018-08-10 13:03:04 +00:00
|
|
|
for {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
scanner.Scan()
|
|
|
|
message = scanner.Text()
|
|
|
|
for _, recipient := range recipients {
|
2018-08-20 11:24:48 +00:00
|
|
|
_, err = client.Send(xmpp.Chat{Remote: recipient,
|
|
|
|
Type: "chat", Text: message})
|
2018-08-10 13:03:04 +00:00
|
|
|
if err != nil {
|
2020-04-03 12:14:48 +00:00
|
|
|
// Try to nicely close connection,
|
|
|
|
// even if there was an error sending.
|
|
|
|
_ = client.Close()
|
2018-08-10 13:03:04 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, recipient := range recipients {
|
2020-04-09 14:49:27 +00:00
|
|
|
if *flagHttpUpload != "" {
|
2020-04-11 16:59:32 +00:00
|
|
|
_, err = client.Send(xmpp.Chat{Remote: recipient, Type: "chat",
|
|
|
|
Ooburl: message, Text: message})
|
2020-04-09 14:49:27 +00:00
|
|
|
} else {
|
|
|
|
_, err = client.Send(xmpp.Chat{Remote: recipient, Type: "chat",
|
|
|
|
Text: message})
|
|
|
|
}
|
2018-08-10 13:03:04 +00:00
|
|
|
if err != nil {
|
2020-04-03 12:14:48 +00:00
|
|
|
// Try to nicely close connection,
|
|
|
|
// even if there was an error sending.
|
|
|
|
_ = client.Close()
|
2018-08-10 13:03:04 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-08-10 10:14:21 +00:00
|
|
|
}
|
2018-08-04 10:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-10 13:03:04 +00:00
|
|
|
|
2019-02-20 20:22:31 +00:00
|
|
|
// Wait for a short time as some messages are not delievered by the server
|
|
|
|
// if the connection is closed immediately after sending a message.
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
2019-02-13 17:31:14 +00:00
|
|
|
// Close XMPP connection
|
|
|
|
err = client.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-08-04 10:16:28 +00:00
|
|
|
}
|