From 3d250f04e6a2e5f33cdbc3c060e6ff07009c95e4 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Sun, 11 Feb 2018 23:28:21 +0100 Subject: [PATCH] add account/status admin + notify by disconnect --- daemon/tester/account.go | 5 +++-- daemon/tester/bot.go | 18 ++++++------------ daemon/tester/status.go | 29 +++++++++++++++++++++++------ daemon/tester/tester.go | 7 +++---- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/daemon/tester/account.go b/daemon/tester/account.go index 6ebad80..dd7d075 100644 --- a/daemon/tester/account.go +++ b/daemon/tester/account.go @@ -3,6 +3,7 @@ package tester import "dev.sum7.eu/genofire/yaja/model" type Account struct { - JID *model.JID `json:"jid"` - Password string `json:"password"` + JID *model.JID `json:"jid"` + Password string `json:"password"` + Admins []*model.JID `json:"admins"` } diff --git a/daemon/tester/bot.go b/daemon/tester/bot.go index 9b0efef..8bb5f6d 100644 --- a/daemon/tester/bot.go +++ b/daemon/tester/bot.go @@ -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}) diff --git a/daemon/tester/status.go b/daemon/tester/status.go index da051a9..e8a1a48 100644 --- a/daemon/tester/status.go +++ b/daemon/tester/status.go @@ -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 { diff --git a/daemon/tester/tester.go b/daemon/tester/tester.go index de56364..9222e19 100644 --- a/daemon/tester/tester.go +++ b/daemon/tester/tester.go @@ -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