sum7
/
yaja
Archived
1
0
Fork 0

add presence status to messages + fix subscribe tester bot

This commit is contained in:
Martin/Geno 2018-02-11 14:05:01 +01:00
parent 8549f2fd32
commit 5964a8fb3c
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
4 changed files with 42 additions and 11 deletions

View File

@ -149,7 +149,7 @@ func (client *Client) connect(password string) error {
if err := client.ReadElement(&iq); err != nil {
return err
}
if &iq.Bind == nil {
if iq.Bind == nil {
return errors.New("<iq> result missing <bind>")
}
if iq.Bind.JID != nil {
@ -160,7 +160,7 @@ func (client *Client) connect(password string) error {
return errors.New(fmt.Sprintf("%v", iq.Other))
}
// set status
client.Send(&messages.PresenceClient{Show: "online", Status: "yaja client"})
client.Send(&messages.PresenceClient{Show: messages.ShowTypeXA, Status: "online"})
return err
}

View File

@ -51,19 +51,25 @@ func (client *Client) Send(p interface{}) error {
msg, ok := p.(*messages.MessageClient)
if ok {
msg.From = client.JID
if msg.ID == "" {
msg.ID = utils.CreateCookieString()
}
return client.Out.Encode(msg)
}
iq, ok := p.(*messages.IQClient)
if ok {
iq.From = client.JID
if iq.ID == "" {
iq.ID = utils.CreateCookieString()
}
return client.Out.Encode(iq)
}
pc, ok := p.(*messages.PresenceClient)
if ok {
pc.From = client.JID
if pc.ID == "" {
pc.ID = utils.CreateCookieString()
}
return client.Out.Encode(pc)
}
return client.Out.Encode(p)

View File

@ -6,6 +6,7 @@ import (
log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/yaja/messages"
"dev.sum7.eu/genofire/yaja/server/utils"
)
func (t *Tester) StartBot(status *Status) {
@ -47,11 +48,26 @@ func (t *Tester) StartBot(status *Status) {
pres := &messages.PresenceClient{}
err = status.client.In.DecodeElement(pres, element)
if err == nil {
sender := pres.From
logPres := logCTX.WithField("from", sender.Full())
if pres.Type == messages.PresenceTypeSubscribe {
logPres.Debugf("recv subscribe")
pres.Type = messages.PresenceTypeSubscribed
pres.To = pres.From
status.client.Send(pres)
logCTX.WithField("from", pres.From.Full()).Info("accept new subscribe")
pres.To = sender
pres.From = nil
status.client.Out.Encode(pres)
logPres.Debugf("accept new subscribe")
pres.Type = messages.PresenceTypeSubscribe
pres.ID = utils.CreateCookieString()
status.client.Out.Encode(pres)
logPres.Info("request also subscribe")
} else if pres.Type == messages.PresenceTypeSubscribed {
logPres.Info("recv accepted subscribe")
} else if pres.Type == messages.PresenceTypeUnsubscribe {
logPres.Info("recv remove subscribe")
} else if pres.Type == messages.PresenceTypeUnsubscribed {
logPres.Info("recv removed subscribe")
} else {
logCTX.Warnf("unsupported presence recv: %v", pres)
}

View File

@ -27,6 +27,15 @@ const (
PresenceTypeError PresenceType = "error"
)
type ShowType string
const (
ShowTypeAway ShowType = "away"
ShowTypeChat ShowType = "chat"
ShowTypeDND ShowType = "dnd"
ShowTypeXA ShowType = "xa"
)
// PresenceClient element
type PresenceClient struct {
XMLName xml.Name `xml:"jabber:client presence"`
@ -36,7 +45,7 @@ type PresenceClient struct {
Type PresenceType `xml:"type,attr,omitempty"`
Lang string `xml:"lang,attr,omitempty"`
Show string `xml:"show,omitempty"` // away, chat, dnd, xa
Show ShowType `xml:"show,omitempty"` // away, chat, dnd, xa
Status string `xml:"status,omitempty"` // sb []clientText
Priority string `xml:"priority,omitempty"`
// Caps *ClientCaps `xml:"c"`