From 0fb90abcf778f24f462878b66988ff426f78f8b5 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Wed, 17 Jul 2019 23:55:49 +0200 Subject: [PATCH] improve authentification --- cmd/sendxmpp/cmd.go | 51 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/cmd/sendxmpp/cmd.go b/cmd/sendxmpp/cmd.go index c6f39ba..14c5553 100644 --- a/cmd/sendxmpp/cmd.go +++ b/cmd/sendxmpp/cmd.go @@ -13,6 +13,8 @@ import ( "gosrc.io/xmpp" ) +var configFile = "" + var jid = "" var password = "" @@ -20,8 +22,9 @@ var receiverMUC = false var stdIn = false var cmd = &cobra.Command{ - Use: "sendxmpp", - Args: cobra.MinimumNArgs(1), + Use: "sendxmpp [message]", + Example: `sendxmpp to@chat.sum7.eu "Hello World!"`, + Args: cobra.RangeArgs(1, 2), Run: func(cmd *cobra.Command, args []string) { receiver := strings.Split(args[0], ",") msgText := "" @@ -35,19 +38,23 @@ var cmd = &cobra.Command{ var err error client, err := xmpp.NewClient(xmpp.Config{ - Jid: jid, - Password: password, + Jid: viper.GetString("jid"), + Password: viper.GetString("password"), }, xmpp.NewRouter()) if err != nil { - log.Panicf("error on startup xmpp client: %s", err) + log.Errorf("error on startup xmpp client: %s", err) + return } wg := sync.WaitGroup{} wg.Add(1) cm := xmpp.NewStreamManager(client, func(c xmpp.Sender) { + defer wg.Done() + log.Info("client connected") + if receiverMUC { for _, muc := range receiver { joinMUC(c, muc, "sendxmpp") @@ -56,7 +63,6 @@ var cmd = &cobra.Command{ if !stdIn { send(c, receiver, msgText) - wg.Done() return } @@ -68,11 +74,11 @@ var cmd = &cobra.Command{ if err := scanner.Err(); err != nil { log.Errorf("error on reading stdin: %s", err) } - wg.Done() }) go func() { err := cm.Run() log.Panic("closed connection:", err) + wg.Done() }() wg.Wait() @@ -82,14 +88,35 @@ var cmd = &cobra.Command{ } func init() { - cmd.Flags().StringVarP(&jid, "jid", "", "", "using jid (required)") - viper.BindPFlag("jid", cmd.Flags().Lookup("jid")) - // cmd.MarkFlagRequired("jid") + cobra.OnInitialize(initConfig) + cmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is ~/.config/fluxxmpp.yml)") - cmd.Flags().StringVarP(&password, "password", "", "", "using password for your jid (required)") + cmd.Flags().StringP("jid", "", "", "using jid (required)") + viper.BindPFlag("jid", cmd.Flags().Lookup("jid")) + + cmd.Flags().StringP("password", "", "", "using password for your jid (required)") viper.BindPFlag("password", cmd.Flags().Lookup("password")) - // cmd.MarkFlagRequired("password") cmd.Flags().BoolVarP(&stdIn, "stdin", "i", false, "read from stdin instatt of 2. argument") cmd.Flags().BoolVarP(&receiverMUC, "muc", "m", false, "reciever is a muc (join it before sending messages)") } + +// 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 { + log.Warnf("no configuration found (somebody could read your password from progress argument list): %s", err) + } +}