first idea of sendxmpp
This commit is contained in:
parent
c41d068c9f
commit
6aa942dd58
17
cmd/sendxmpp/README.md
Normal file
17
cmd/sendxmpp/README.md
Normal file
|
@ -0,0 +1,17 @@
|
|||
# sendXMPP
|
||||
|
||||
sendxmpp is a tool to send messages from commandline
|
||||
|
||||
## Installation
|
||||
|
||||
To install `sendxmpp` in your Go path:
|
||||
|
||||
```
|
||||
$ go get -u gosrc.io/xmpp/cmd/sendxmpp
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
$ xmpp-check --help
|
||||
```
|
95
cmd/sendxmpp/cmd.go
Normal file
95
cmd/sendxmpp/cmd.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/bdlm/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"gosrc.io/xmpp"
|
||||
)
|
||||
|
||||
var jid = ""
|
||||
var password = ""
|
||||
|
||||
var receiverMUC = false
|
||||
var stdIn = false
|
||||
|
||||
var cmd = &cobra.Command{
|
||||
Use: "sendxmpp",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
receiver := strings.Split(args[0], ",")
|
||||
msgText := ""
|
||||
|
||||
if !stdIn && len(args) < 2 {
|
||||
log.Error("no message to send")
|
||||
return
|
||||
} else if !stdIn {
|
||||
msgText = args[1]
|
||||
}
|
||||
|
||||
var err error
|
||||
client, err := xmpp.NewClient(xmpp.Config{
|
||||
Jid: jid,
|
||||
Password: password,
|
||||
}, xmpp.NewRouter())
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("error on startup xmpp client: %s", err)
|
||||
}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
|
||||
cm := xmpp.NewStreamManager(client, func(c xmpp.Sender) {
|
||||
log.Info("client connected")
|
||||
if receiverMUC {
|
||||
for _, muc := range receiver {
|
||||
joinMUC(c, muc, "sendxmpp")
|
||||
}
|
||||
}
|
||||
|
||||
if !stdIn {
|
||||
send(c, receiver, msgText)
|
||||
wg.Done()
|
||||
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)
|
||||
}
|
||||
wg.Done()
|
||||
})
|
||||
go func() {
|
||||
err := cm.Run()
|
||||
log.Panic("closed connection:", err)
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
|
||||
leaveMUCs(client)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
cmd.Flags().StringVarP(&jid, "jid", "", "", "using jid (required)")
|
||||
viper.BindPFlag("jid", cmd.Flags().Lookup("jid"))
|
||||
// cmd.MarkFlagRequired("jid")
|
||||
|
||||
cmd.Flags().StringVarP(&password, "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)")
|
||||
}
|
6
cmd/sendxmpp/doc.go
Normal file
6
cmd/sendxmpp/doc.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
/*
|
||||
|
||||
sendxmpp is a command-line to send to send messages to users
|
||||
|
||||
*/
|
||||
package main
|
34
cmd/sendxmpp/log.go
Normal file
34
cmd/sendxmpp/log.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/bdlm/log"
|
||||
stdLogger "github.com/bdlm/std/logger"
|
||||
)
|
||||
|
||||
type hook struct{}
|
||||
|
||||
func (h *hook) Fire(entry *log.Entry) error {
|
||||
switch entry.Level {
|
||||
case log.PanicLevel:
|
||||
entry.Logger.Out = os.Stderr
|
||||
case log.FatalLevel:
|
||||
entry.Logger.Out = os.Stderr
|
||||
case log.ErrorLevel:
|
||||
entry.Logger.Out = os.Stderr
|
||||
case log.WarnLevel:
|
||||
entry.Logger.Out = os.Stdout
|
||||
case log.InfoLevel:
|
||||
entry.Logger.Out = os.Stdout
|
||||
case log.DebugLevel:
|
||||
entry.Logger.Out = os.Stdout
|
||||
default:
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *hook) Levels() []stdLogger.Level {
|
||||
return log.AllLevels
|
||||
}
|
10
cmd/sendxmpp/main.go
Normal file
10
cmd/sendxmpp/main.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/bdlm/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.AddHook(&hook{})
|
||||
cmd.Execute()
|
||||
}
|
41
cmd/sendxmpp/muc.go
Normal file
41
cmd/sendxmpp/muc.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/bdlm/log"
|
||||
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
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},
|
||||
Extensions: []stanza.PresExtension{
|
||||
stanza.MucPresence{
|
||||
History: stanza.History{MaxStanzas: stanza.NewNullableInt(0)},
|
||||
}},
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func leaveMUCs(c xmpp.Sender) {
|
||||
for _, muc := range mucsToLeave {
|
||||
if err := c.Send(stanza.Presence{Attrs: stanza.Attrs{
|
||||
To: muc,
|
||||
Type: stanza.PresenceTypeUnavailable,
|
||||
}}); err != nil {
|
||||
log.WithField("muc", muc).Errorf("error on leaving muc: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
34
cmd/sendxmpp/send.go
Normal file
34
cmd/sendxmpp/send.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/bdlm/log"
|
||||
|
||||
"gosrc.io/xmpp"
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
func send(c xmpp.Sender, receiver []string, msgText string) {
|
||||
msg := stanza.Message{
|
||||
Attrs: stanza.Attrs{Type: stanza.MessageTypeChat},
|
||||
Body: msgText,
|
||||
}
|
||||
if receiverMUC {
|
||||
msg.Type = stanza.MessageTypeGroupchat
|
||||
}
|
||||
for _, to := range receiver {
|
||||
msg.To = to
|
||||
if err := c.Send(msg); err != nil {
|
||||
log.WithFields(map[string]interface{}{
|
||||
"muc": receiverMUC,
|
||||
"to": to,
|
||||
"text": msgText,
|
||||
}).Errorf("error on send message: %s", err)
|
||||
} else {
|
||||
log.WithFields(map[string]interface{}{
|
||||
"muc": receiverMUC,
|
||||
"to": to,
|
||||
"text": msgText,
|
||||
}).Info("send message")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue