add presence status to messages + fix subscribe tester bot
This commit is contained in:
parent
8549f2fd32
commit
5964a8fb3c
|
@ -149,7 +149,7 @@ func (client *Client) connect(password string) error {
|
||||||
if err := client.ReadElement(&iq); err != nil {
|
if err := client.ReadElement(&iq); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if &iq.Bind == nil {
|
if iq.Bind == nil {
|
||||||
return errors.New("<iq> result missing <bind>")
|
return errors.New("<iq> result missing <bind>")
|
||||||
}
|
}
|
||||||
if iq.Bind.JID != nil {
|
if iq.Bind.JID != nil {
|
||||||
|
@ -160,7 +160,7 @@ func (client *Client) connect(password string) error {
|
||||||
return errors.New(fmt.Sprintf("%v", iq.Other))
|
return errors.New(fmt.Sprintf("%v", iq.Other))
|
||||||
}
|
}
|
||||||
// set status
|
// set status
|
||||||
client.Send(&messages.PresenceClient{Show: "online", Status: "yaja client"})
|
client.Send(&messages.PresenceClient{Show: messages.ShowTypeXA, Status: "online"})
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,19 +51,25 @@ func (client *Client) Send(p interface{}) error {
|
||||||
msg, ok := p.(*messages.MessageClient)
|
msg, ok := p.(*messages.MessageClient)
|
||||||
if ok {
|
if ok {
|
||||||
msg.From = client.JID
|
msg.From = client.JID
|
||||||
msg.ID = utils.CreateCookieString()
|
if msg.ID == "" {
|
||||||
|
msg.ID = utils.CreateCookieString()
|
||||||
|
}
|
||||||
return client.Out.Encode(msg)
|
return client.Out.Encode(msg)
|
||||||
}
|
}
|
||||||
iq, ok := p.(*messages.IQClient)
|
iq, ok := p.(*messages.IQClient)
|
||||||
if ok {
|
if ok {
|
||||||
iq.From = client.JID
|
iq.From = client.JID
|
||||||
iq.ID = utils.CreateCookieString()
|
if iq.ID == "" {
|
||||||
|
iq.ID = utils.CreateCookieString()
|
||||||
|
}
|
||||||
return client.Out.Encode(iq)
|
return client.Out.Encode(iq)
|
||||||
}
|
}
|
||||||
pc, ok := p.(*messages.PresenceClient)
|
pc, ok := p.(*messages.PresenceClient)
|
||||||
if ok {
|
if ok {
|
||||||
pc.From = client.JID
|
pc.From = client.JID
|
||||||
pc.ID = utils.CreateCookieString()
|
if pc.ID == "" {
|
||||||
|
pc.ID = utils.CreateCookieString()
|
||||||
|
}
|
||||||
return client.Out.Encode(pc)
|
return client.Out.Encode(pc)
|
||||||
}
|
}
|
||||||
return client.Out.Encode(p)
|
return client.Out.Encode(p)
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"dev.sum7.eu/genofire/yaja/messages"
|
"dev.sum7.eu/genofire/yaja/messages"
|
||||||
|
"dev.sum7.eu/genofire/yaja/server/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (t *Tester) StartBot(status *Status) {
|
func (t *Tester) StartBot(status *Status) {
|
||||||
|
@ -47,11 +48,26 @@ func (t *Tester) StartBot(status *Status) {
|
||||||
pres := &messages.PresenceClient{}
|
pres := &messages.PresenceClient{}
|
||||||
err = status.client.In.DecodeElement(pres, element)
|
err = status.client.In.DecodeElement(pres, element)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
sender := pres.From
|
||||||
|
logPres := logCTX.WithField("from", sender.Full())
|
||||||
if pres.Type == messages.PresenceTypeSubscribe {
|
if pres.Type == messages.PresenceTypeSubscribe {
|
||||||
|
logPres.Debugf("recv subscribe")
|
||||||
pres.Type = messages.PresenceTypeSubscribed
|
pres.Type = messages.PresenceTypeSubscribed
|
||||||
pres.To = pres.From
|
pres.To = sender
|
||||||
status.client.Send(pres)
|
pres.From = nil
|
||||||
logCTX.WithField("from", pres.From.Full()).Info("accept new subscribe")
|
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 {
|
} else {
|
||||||
logCTX.Warnf("unsupported presence recv: %v", pres)
|
logCTX.Warnf("unsupported presence recv: %v", pres)
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,15 @@ const (
|
||||||
PresenceTypeError PresenceType = "error"
|
PresenceTypeError PresenceType = "error"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ShowType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ShowTypeAway ShowType = "away"
|
||||||
|
ShowTypeChat ShowType = "chat"
|
||||||
|
ShowTypeDND ShowType = "dnd"
|
||||||
|
ShowTypeXA ShowType = "xa"
|
||||||
|
)
|
||||||
|
|
||||||
// PresenceClient element
|
// PresenceClient element
|
||||||
type PresenceClient struct {
|
type PresenceClient struct {
|
||||||
XMLName xml.Name `xml:"jabber:client presence"`
|
XMLName xml.Name `xml:"jabber:client presence"`
|
||||||
|
@ -36,9 +45,9 @@ type PresenceClient struct {
|
||||||
Type PresenceType `xml:"type,attr,omitempty"`
|
Type PresenceType `xml:"type,attr,omitempty"`
|
||||||
Lang string `xml:"lang,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
|
Status string `xml:"status,omitempty"` // sb []clientText
|
||||||
Priority string `xml:"priority,omitempty"`
|
Priority string `xml:"priority,omitempty"`
|
||||||
// Caps *ClientCaps `xml:"c"`
|
// Caps *ClientCaps `xml:"c"`
|
||||||
Delay *Delay `xml:"delay"`
|
Delay *Delay `xml:"delay"`
|
||||||
|
|
||||||
|
|
Reference in New Issue