2019-07-16 22:31:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/bdlm/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"gosrc.io/xmpp"
|
|
|
|
)
|
|
|
|
|
2019-07-17 21:55:49 +00:00
|
|
|
var configFile = ""
|
|
|
|
|
2019-07-27 23:50:10 +00:00
|
|
|
// FIXME: Remove global variables
|
|
|
|
var isMUCRecipient = false
|
2019-07-16 22:31:33 +00:00
|
|
|
|
|
|
|
var cmd = &cobra.Command{
|
2019-07-27 23:35:15 +00:00
|
|
|
Use: "sendxmpp <recipient,> [message]",
|
2019-07-17 21:55:49 +00:00
|
|
|
Example: `sendxmpp to@chat.sum7.eu "Hello World!"`,
|
2019-07-26 06:53:33 +00:00
|
|
|
Args: cobra.ExactArgs(2),
|
2019-07-16 22:31:33 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
receiver := strings.Split(args[0], ",")
|
2019-07-26 06:53:33 +00:00
|
|
|
msgText := args[1]
|
2019-07-16 22:31:33 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
client, err := xmpp.NewClient(xmpp.Config{
|
2019-07-17 21:55:49 +00:00
|
|
|
Jid: viper.GetString("jid"),
|
2019-07-17 23:10:03 +00:00
|
|
|
Address: viper.GetString("addr"),
|
2019-07-17 21:55:49 +00:00
|
|
|
Password: viper.GetString("password"),
|
2019-07-16 22:31:33 +00:00
|
|
|
}, xmpp.NewRouter())
|
|
|
|
|
|
|
|
if err != nil {
|
2019-07-27 23:35:15 +00:00
|
|
|
log.Errorf("error when starting xmpp client: %s", err)
|
2019-07-17 21:55:49 +00:00
|
|
|
return
|
2019-07-16 22:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
cm := xmpp.NewStreamManager(client, func(c xmpp.Sender) {
|
2019-07-17 21:55:49 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2019-07-16 22:31:33 +00:00
|
|
|
log.Info("client connected")
|
2019-07-17 21:55:49 +00:00
|
|
|
|
2019-07-27 23:50:10 +00:00
|
|
|
if isMUCRecipient {
|
2019-07-16 22:31:33 +00:00
|
|
|
for _, muc := range receiver {
|
|
|
|
joinMUC(c, muc, "sendxmpp")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 06:53:33 +00:00
|
|
|
if msgText != "-" {
|
2019-07-16 22:31:33 +00:00
|
|
|
send(c, receiver, msgText)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
send(c, receiver, scanner.Text())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
log.Errorf("error on reading stdin: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go func() {
|
|
|
|
err := cm.Run()
|
|
|
|
log.Panic("closed connection:", err)
|
2019-07-17 21:55:49 +00:00
|
|
|
wg.Done()
|
2019-07-16 22:31:33 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
leaveMUCs(client)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2019-07-17 21:55:49 +00:00
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
cmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is ~/.config/fluxxmpp.yml)")
|
|
|
|
|
|
|
|
cmd.Flags().StringP("jid", "", "", "using jid (required)")
|
2019-07-16 22:31:33 +00:00
|
|
|
viper.BindPFlag("jid", cmd.Flags().Lookup("jid"))
|
|
|
|
|
2019-07-17 21:55:49 +00:00
|
|
|
cmd.Flags().StringP("password", "", "", "using password for your jid (required)")
|
2019-07-16 22:31:33 +00:00
|
|
|
viper.BindPFlag("password", cmd.Flags().Lookup("password"))
|
|
|
|
|
2019-07-17 23:10:03 +00:00
|
|
|
cmd.Flags().StringP("addr", "", "", "host[:port]")
|
|
|
|
viper.BindPFlag("addr", cmd.Flags().Lookup("addr"))
|
|
|
|
|
2019-07-27 23:50:10 +00:00
|
|
|
cmd.Flags().BoolVarP(&isMUCRecipient, "muc", "m", false, "recipient is a muc (join it before sending messages)")
|
2019-07-16 22:31:33 +00:00
|
|
|
}
|
2019-07-17 21:55:49 +00:00
|
|
|
|
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
|
|
func initConfig() {
|
|
|
|
if configFile != "" {
|
|
|
|
viper.SetConfigFile(configFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.SetConfigName("fluxxmpp")
|
|
|
|
viper.AddConfigPath("/etc/")
|
|
|
|
viper.AddConfigPath("$HOME/.config")
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
|
|
|
|
viper.SetEnvPrefix("FLUXXMPP")
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
2019-07-27 23:35:15 +00:00
|
|
|
log.Warnf("no configuration found (somebody could read your password from process argument list): %s", err)
|
2019-07-17 21:55:49 +00:00
|
|
|
}
|
|
|
|
}
|