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 { type Account struct {
JID *model.JID `json:"jid"` JID *model.JID `json:"jid"`
Password string `json:"password"` Password string `json:"password"`
Admins []*model.JID `json:"admins"`
} }

View File

@ -1,6 +1,7 @@
package tester package tester
import ( import (
"fmt"
"strings" "strings"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -19,18 +20,14 @@ func (t *Tester) StartBot(status *Status) {
element, err := status.client.Read() element, err := status.client.Read()
if err != nil { if err != nil {
logCTX.Errorf("read client %s", err) status.Disconnect(fmt.Sprintf("could not read any more data from socket: %s", err))
status.client.Close()
status.Login = false
return return
} }
errMSG := &messages.StreamError{} errMSG := &messages.StreamError{}
err = status.client.Decode(errMSG, element) err = status.client.Decode(errMSG, element)
if err == nil { if err == nil {
logCTX.Errorf("recv stream error: %s: %s", errMSG.Text, messages.XMLChildrenString(errMSG.Any)) status.Disconnect(fmt.Sprintf("recv stream error: %s: %s", errMSG.Text, messages.XMLChildrenString(errMSG.Any)))
status.client.Close()
status.Login = false
return return
} }
@ -89,9 +86,7 @@ func (t *Tester) StartBot(status *Status) {
logCTX = logCTX.WithField("from", msg.From.Full()).WithField("msg-recv", msg.Body) logCTX = logCTX.WithField("from", msg.From.Full()).WithField("msg-recv", msg.Body)
if msg.Error != nil { if msg.Error != nil {
if msg.Error.Type == "auth" { if msg.Error.Type == "auth" {
logCTX.Warnf("recv msg with error not auth") status.Disconnect("recv msg with error not auth")
status.Login = false
status.client.Close()
return 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)) 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 first := true
allAdmins := "" allAdmins := ""
isAdmin := false isAdmin := false
for _, jid := range t.Admins { for _, jid := range append(t.Admins, status.account.Admins...) {
if first { if first {
first = false first = false
allAdmins += jid.Bare() allAdmins += jid.Bare()
@ -123,8 +118,7 @@ func (t *Tester) StartBot(status *Status) {
} }
} }
if isAdmin { if isAdmin {
status.Login = false status.Disconnect(fmt.Sprintf("disconnect by admin '%s'", msg.From.Bare()))
status.client.Close()
return return
} }
status.client.Send(messages.MessageClient{Type: msg.Type, To: msg.From, Body: "not allowed, ask " + allAdmins}) 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 ( import (
"crypto/tls" "crypto/tls"
"fmt"
"time" "time"
"dev.sum7.eu/genofire/yaja/client" "dev.sum7.eu/genofire/yaja/client"
"dev.sum7.eu/genofire/yaja/messages"
"dev.sum7.eu/genofire/yaja/model" "dev.sum7.eu/genofire/yaja/model"
) )
type Status struct { type Status struct {
backupClient *client.Client
client *client.Client client *client.Client
account *Account account *Account
JID *model.JID `json:"jid"` JID *model.JID `json:"jid"`
@ -21,8 +24,9 @@ type Status struct {
IPv6 bool `json:"ipv6"` IPv6 bool `json:"ipv6"`
} }
func NewStatus(acc *Account) *Status { func NewStatus(backupClient *client.Client, acc *Account) *Status {
return &Status{ return &Status{
backupClient: backupClient,
account: acc, account: acc,
JID: acc.JID, JID: acc.JID,
Domain: acc.JID.Domain, Domain: acc.JID.Domain,
@ -30,11 +34,26 @@ func NewStatus(acc *Account) *Status {
Connections: make(map[string]bool), 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) { func (s *Status) Update(timeout time.Duration) {
if s.client == nil || !s.Login { if s.client == nil || !s.Login {
s.Login = false
s.TLSVersion = ""
return return
} }
@ -62,9 +81,7 @@ func (s *Status) Update(timeout time.Duration) {
s.IPv6 = false s.IPv6 = false
} }
if !s.IPv4 && !s.IPv6 { if !s.IPv4 && !s.IPv6 {
s.client.Close() s.Disconnect("check of ipv4 and ipv6 failed -> client should not be connected anymore")
s.Login = false
s.TLSVersion = ""
} }
if tlsstate := s.client.TLSConnectionState(); tlsstate != nil { 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 t.mainClient = mainClient
status := NewStatus(&Account{ status := NewStatus(mainClient, &Account{
JID: mainClient.JID, JID: mainClient.JID,
Password: password, Password: password,
}) })
@ -53,8 +53,7 @@ func (t *Tester) Start(mainClient *client.Client, password string) {
} }
func (t *Tester) Close() { func (t *Tester) Close() {
for _, s := range t.Status { for _, s := range t.Status {
s.Login = false s.Disconnect("yaja tester stopped")
s.client.Close()
} }
} }
@ -62,7 +61,7 @@ func (t *Tester) Connect(acc *Account) {
logCTX := log.WithField("jid", acc.JID.Full()) logCTX := log.WithField("jid", acc.JID.Full())
status, ok := t.Status[acc.JID.Bare()] status, ok := t.Status[acc.JID.Bare()]
if !ok { if !ok {
status = NewStatus(acc) status = NewStatus(t.mainClient, acc)
t.Status[acc.JID.Bare()] = status t.Status[acc.JID.Bare()] = status
} else if status.JID == nil { } else if status.JID == nil {
status.JID = acc.JID status.JID = acc.JID