sum7
/
yaja
Archived
1
0
Fork 0

add account/status admin + notify by disconnect

This commit is contained in:
Martin/Geno 2018-02-11 23:28:21 +01:00
parent 940d328b09
commit 3d250f04e6
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
4 changed files with 35 additions and 24 deletions

View File

@ -5,4 +5,5 @@ import "dev.sum7.eu/genofire/yaja/model"
type Account struct {
JID *model.JID `json:"jid"`
Password string `json:"password"`
Admins []*model.JID `json:"admins"`
}

View File

@ -1,6 +1,7 @@
package tester
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
@ -19,18 +20,14 @@ func (t *Tester) StartBot(status *Status) {
element, err := status.client.Read()
if err != nil {
logCTX.Errorf("read client %s", err)
status.client.Close()
status.Login = false
status.Disconnect(fmt.Sprintf("could not read any more data from socket: %s", err))
return
}
errMSG := &messages.StreamError{}
err = status.client.Decode(errMSG, element)
if err == nil {
logCTX.Errorf("recv stream error: %s: %s", errMSG.Text, messages.XMLChildrenString(errMSG.Any))
status.client.Close()
status.Login = false
status.Disconnect(fmt.Sprintf("recv stream error: %s: %s", errMSG.Text, messages.XMLChildrenString(errMSG.Any)))
return
}
@ -89,9 +86,7 @@ func (t *Tester) StartBot(status *Status) {
logCTX = logCTX.WithField("from", msg.From.Full()).WithField("msg-recv", msg.Body)
if msg.Error != nil {
if msg.Error.Type == "auth" {
logCTX.Warnf("recv msg with error not auth")
status.Login = false
status.client.Close()
status.Disconnect("recv msg with error not auth")
return
}
logCTX.Debugf("recv msg with error %s[%s]: %s -> %s -> %s", msg.Error.Code, msg.Error.Type, msg.Error.Text, messages.XMLChildrenString(msg.Error.StanzaErrorGroup), messages.XMLChildrenString(msg.Error.Other))
@ -109,7 +104,7 @@ func (t *Tester) StartBot(status *Status) {
first := true
allAdmins := ""
isAdmin := false
for _, jid := range t.Admins {
for _, jid := range append(t.Admins, status.account.Admins...) {
if first {
first = false
allAdmins += jid.Bare()
@ -123,8 +118,7 @@ func (t *Tester) StartBot(status *Status) {
}
}
if isAdmin {
status.Login = false
status.client.Close()
status.Disconnect(fmt.Sprintf("disconnect by admin '%s'", msg.From.Bare()))
return
}
status.client.Send(messages.MessageClient{Type: msg.Type, To: msg.From, Body: "not allowed, ask " + allAdmins})

View File

@ -2,13 +2,16 @@ package tester
import (
"crypto/tls"
"fmt"
"time"
"dev.sum7.eu/genofire/yaja/client"
"dev.sum7.eu/genofire/yaja/messages"
"dev.sum7.eu/genofire/yaja/model"
)
type Status struct {
backupClient *client.Client
client *client.Client
account *Account
JID *model.JID `json:"jid"`
@ -21,8 +24,9 @@ type Status struct {
IPv6 bool `json:"ipv6"`
}
func NewStatus(acc *Account) *Status {
func NewStatus(backupClient *client.Client, acc *Account) *Status {
return &Status{
backupClient: backupClient,
account: acc,
JID: acc.JID,
Domain: acc.JID.Domain,
@ -30,11 +34,26 @@ func NewStatus(acc *Account) *Status {
Connections: make(map[string]bool),
}
}
func (s *Status) Disconnect(reason string) {
if s.Login {
msg := &messages.MessageClient{
Body: fmt.Sprintf("you recieve a notify that '%s' disconnect: %s", s.JID.Full(), reason),
}
for _, jid := range s.account.Admins {
msg.To = jid
if err := s.backupClient.Send(msg); err != nil {
s.client.Send(msg)
}
}
}
s.client.Logging.Warn(reason)
s.client.Close()
s.Login = false
s.TLSVersion = ""
}
func (s *Status) Update(timeout time.Duration) {
if s.client == nil || !s.Login {
s.Login = false
s.TLSVersion = ""
return
}
@ -62,9 +81,7 @@ func (s *Status) Update(timeout time.Duration) {
s.IPv6 = false
}
if !s.IPv4 && !s.IPv6 {
s.client.Close()
s.Login = false
s.TLSVersion = ""
s.Disconnect("check of ipv4 and ipv6 failed -> client should not be connected anymore")
}
if tlsstate := s.client.TLSConnectionState(); tlsstate != nil {

View File

@ -33,7 +33,7 @@ func (t *Tester) Start(mainClient *client.Client, password string) {
t.mainClient = mainClient
status := NewStatus(&Account{
status := NewStatus(mainClient, &Account{
JID: mainClient.JID,
Password: password,
})
@ -53,8 +53,7 @@ func (t *Tester) Start(mainClient *client.Client, password string) {
}
func (t *Tester) Close() {
for _, s := range t.Status {
s.Login = false
s.client.Close()
s.Disconnect("yaja tester stopped")
}
}
@ -62,7 +61,7 @@ func (t *Tester) Connect(acc *Account) {
logCTX := log.WithField("jid", acc.JID.Full())
status, ok := t.Status[acc.JID.Bare()]
if !ok {
status = NewStatus(acc)
status = NewStatus(t.mainClient, acc)
t.Status[acc.JID.Bare()] = status
} else if status.JID == nil {
status.JID = acc.JID