2019-07-16 15:31:33 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-01-09 06:33:11 -08:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-07-16 15:31:33 -07:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/bdlm/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"gosrc.io/xmpp"
|
|
|
|
)
|
|
|
|
|
2019-07-17 14:55:49 -07:00
|
|
|
var configFile = ""
|
|
|
|
|
2019-07-27 16:50:10 -07:00
|
|
|
// FIXME: Remove global variables
|
|
|
|
var isMUCRecipient = false
|
2019-07-16 15:31:33 -07:00
|
|
|
|
2019-08-06 02:00:52 -07:00
|
|
|
var cmdSend = &cobra.Command{
|
|
|
|
Use: "send <recipient,> [message]",
|
|
|
|
Short: "is a command-line tool to send to send XMPP messages to users",
|
2019-08-06 07:03:48 -07:00
|
|
|
Example: `fluuxmpp send to@chat.sum7.eu "Hello World!"`,
|
2019-07-25 23:53:33 -07:00
|
|
|
Args: cobra.ExactArgs(2),
|
2019-07-27 18:06:55 -07:00
|
|
|
Run: sendxmpp,
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendxmpp(cmd *cobra.Command, args []string) {
|
|
|
|
receiver := strings.Split(args[0], ",")
|
|
|
|
msgText := args[1]
|
|
|
|
|
|
|
|
var err error
|
|
|
|
client, err := xmpp.NewClient(xmpp.Config{
|
2019-10-10 21:41:15 -07:00
|
|
|
TransportConfiguration: xmpp.TransportConfiguration{
|
|
|
|
Address: viper.GetString("addr"),
|
|
|
|
},
|
2019-10-01 01:59:55 -07:00
|
|
|
Jid: viper.GetString("jid"),
|
|
|
|
Credential: xmpp.Password(viper.GetString("password")),
|
2020-01-31 02:48:03 -08:00
|
|
|
},
|
|
|
|
xmpp.NewRouter(),
|
|
|
|
func(err error) {
|
|
|
|
log.Println(err)
|
|
|
|
})
|
2019-07-16 15:31:33 -07:00
|
|
|
|
2019-07-27 18:06:55 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error when starting xmpp client: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2019-07-16 15:31:33 -07:00
|
|
|
|
2019-07-27 18:06:55 -07:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
2019-07-17 14:55:49 -07:00
|
|
|
|
2019-07-27 18:06:55 -07:00
|
|
|
// FIXME: Remove global variables
|
2020-01-09 06:33:11 -08:00
|
|
|
var mucsToLeave []*stanza.Jid
|
2019-07-17 14:55:49 -07:00
|
|
|
|
2019-07-27 18:06:55 -07:00
|
|
|
cm := xmpp.NewStreamManager(client, func(c xmpp.Sender) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
log.Info("client connected")
|
|
|
|
|
|
|
|
if isMUCRecipient {
|
|
|
|
for _, muc := range receiver {
|
2020-01-09 06:33:11 -08:00
|
|
|
jid, err := stanza.NewJid(muc)
|
2019-07-27 18:06:55 -07:00
|
|
|
if err != nil {
|
|
|
|
log.WithField("muc", muc).Errorf("skipping invalid muc jid: %w", err)
|
|
|
|
continue
|
2019-07-16 15:31:33 -07:00
|
|
|
}
|
2019-07-27 18:06:55 -07:00
|
|
|
jid.Resource = "sendxmpp"
|
2019-07-16 15:31:33 -07:00
|
|
|
|
2019-07-27 18:06:55 -07:00
|
|
|
if err := joinMUC(c, jid); err != nil {
|
|
|
|
log.WithField("muc", muc).Errorf("error joining muc: %w", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mucsToLeave = append(mucsToLeave, jid)
|
2019-07-16 15:31:33 -07:00
|
|
|
}
|
2019-07-27 18:06:55 -07:00
|
|
|
}
|
2019-07-16 15:31:33 -07:00
|
|
|
|
2019-07-27 18:06:55 -07:00
|
|
|
if msgText != "-" {
|
|
|
|
send(c, receiver, msgText)
|
|
|
|
return
|
|
|
|
}
|
2019-07-16 15:31:33 -07:00
|
|
|
|
2019-07-27 18:06:55 -07:00
|
|
|
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)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2019-07-16 15:31:33 -07:00
|
|
|
|
2019-07-27 18:06:55 -07:00
|
|
|
wg.Wait()
|
2019-07-16 15:31:33 -07:00
|
|
|
|
2019-07-27 18:06:55 -07:00
|
|
|
leaveMUCs(client, mucsToLeave)
|
2019-07-16 15:31:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2019-08-06 02:00:52 -07:00
|
|
|
cmdRoot.AddCommand(cmdSend)
|
2019-07-17 14:55:49 -07:00
|
|
|
|
2019-08-06 02:00:52 -07:00
|
|
|
cobra.OnInitialize(initConfigFile)
|
2019-08-06 07:03:48 -07:00
|
|
|
cmdSend.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is ~/.config/fluuxmpp.yml)")
|
2019-07-16 15:31:33 -07:00
|
|
|
|
2019-08-06 02:00:52 -07:00
|
|
|
cmdSend.Flags().StringP("jid", "", "", "using jid (required)")
|
|
|
|
viper.BindPFlag("jid", cmdSend.Flags().Lookup("jid"))
|
2019-07-16 15:31:33 -07:00
|
|
|
|
2019-08-06 02:00:52 -07:00
|
|
|
cmdSend.Flags().StringP("password", "", "", "using password for your jid (required)")
|
|
|
|
viper.BindPFlag("password", cmdSend.Flags().Lookup("password"))
|
2019-07-17 16:10:03 -07:00
|
|
|
|
2019-08-06 02:00:52 -07:00
|
|
|
cmdSend.Flags().StringP("addr", "", "", "host[:port]")
|
|
|
|
viper.BindPFlag("addr", cmdSend.Flags().Lookup("addr"))
|
|
|
|
|
|
|
|
cmdSend.Flags().BoolVarP(&isMUCRecipient, "muc", "m", false, "recipient is a muc (join it before sending messages)")
|
2019-07-16 15:31:33 -07:00
|
|
|
}
|
2019-07-17 14:55:49 -07:00
|
|
|
|
|
|
|
// initConfig reads in config file and ENV variables if set.
|
2019-08-06 02:00:52 -07:00
|
|
|
func initConfigFile() {
|
2019-07-17 14:55:49 -07:00
|
|
|
if configFile != "" {
|
|
|
|
viper.SetConfigFile(configFile)
|
|
|
|
}
|
|
|
|
|
2019-08-06 07:03:48 -07:00
|
|
|
viper.SetConfigName("fluuxmpp")
|
2019-07-17 14:55:49 -07:00
|
|
|
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 16:35:15 -07:00
|
|
|
log.Warnf("no configuration found (somebody could read your password from process argument list): %s", err)
|
2019-07-17 14:55:49 -07:00
|
|
|
}
|
|
|
|
}
|