Refactor to start removing global variables

This commit is contained in:
Mickael Remond 2019-07-27 18:06:55 -07:00
parent a3867dd0b3
commit 7ab6c3a62d
No known key found for this signature in database
GPG Key ID: E6F6045D79965AA3
2 changed files with 66 additions and 64 deletions

View File

@ -22,60 +22,76 @@ var cmd = &cobra.Command{
Use: "sendxmpp <recipient,> [message]",
Example: `sendxmpp to@chat.sum7.eu "Hello World!"`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
receiver := strings.Split(args[0], ",")
msgText := args[1]
Run: sendxmpp,
}
var err error
client, err := xmpp.NewClient(xmpp.Config{
Jid: viper.GetString("jid"),
Address: viper.GetString("addr"),
Password: viper.GetString("password"),
}, xmpp.NewRouter())
func sendxmpp(cmd *cobra.Command, args []string) {
receiver := strings.Split(args[0], ",")
msgText := args[1]
if err != nil {
log.Errorf("error when starting xmpp client: %s", err)
var err error
client, err := xmpp.NewClient(xmpp.Config{
Jid: viper.GetString("jid"),
Address: viper.GetString("addr"),
Password: viper.GetString("password"),
}, xmpp.NewRouter())
if err != nil {
log.Errorf("error when starting xmpp client: %s", err)
return
}
wg := sync.WaitGroup{}
wg.Add(1)
// FIXME: Remove global variables
var mucsToLeave []*xmpp.Jid
cm := xmpp.NewStreamManager(client, func(c xmpp.Sender) {
defer wg.Done()
log.Info("client connected")
if isMUCRecipient {
for _, muc := range receiver {
jid, err := xmpp.NewJid(muc)
if err != nil {
log.WithField("muc", muc).Errorf("skipping invalid muc jid: %w", err)
continue
}
jid.Resource = "sendxmpp"
if err := joinMUC(c, jid); err != nil {
log.WithField("muc", muc).Errorf("error joining muc: %w", err)
continue
}
mucsToLeave = append(mucsToLeave, jid)
}
}
if msgText != "-" {
send(c, receiver, msgText)
return
}
wg := sync.WaitGroup{}
wg.Add(1)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
send(c, receiver, scanner.Text())
}
cm := xmpp.NewStreamManager(client, func(c xmpp.Sender) {
defer wg.Done()
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()
}()
log.Info("client connected")
wg.Wait()
if isMUCRecipient {
for _, muc := range receiver {
joinMUC(c, muc, "sendxmpp")
}
}
if msgText != "-" {
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)
wg.Done()
}()
wg.Wait()
leaveMUCs(client)
},
leaveMUCs(client, mucsToLeave)
}
func init() {

View File

@ -7,33 +7,19 @@ import (
"gosrc.io/xmpp/stanza"
)
// FIXME: Remove global variables
var mucsToLeave []string
func joinMUC(c xmpp.Sender, to, nick string) error {
toJID, err := xmpp.NewJid(to)
if err != nil {
return err
}
toJID.Resource = nick
jid := toJID.Full()
mucsToLeave = append(mucsToLeave, jid)
return c.Send(stanza.Presence{Attrs: stanza.Attrs{To: jid},
func joinMUC(c xmpp.Sender, toJID *xmpp.Jid) error {
return c.Send(stanza.Presence{Attrs: stanza.Attrs{To: toJID.Full()},
Extensions: []stanza.PresExtension{
stanza.MucPresence{
History: stanza.History{MaxStanzas: stanza.NewNullableInt(0)},
}},
})
}
func leaveMUCs(c xmpp.Sender) {
func leaveMUCs(c xmpp.Sender, mucsToLeave []*xmpp.Jid) {
for _, muc := range mucsToLeave {
if err := c.Send(stanza.Presence{Attrs: stanza.Attrs{
To: muc,
To: muc.Full(),
Type: stanza.PresenceTypeUnavailable,
}}); err != nil {
log.WithField("muc", muc).Errorf("error on leaving muc: %s", err)