diff --git a/client/client.go b/client/client.go index a50b75a..f52d1c1 100644 --- a/client/client.go +++ b/client/client.go @@ -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(" result missing ") } 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 } diff --git a/client/comm.go b/client/comm.go index 5fad5f8..0f3c80f 100644 --- a/client/comm.go +++ b/client/comm.go @@ -51,19 +51,25 @@ func (client *Client) Send(p interface{}) error { msg, ok := p.(*messages.MessageClient) if ok { msg.From = client.JID - msg.ID = utils.CreateCookieString() + if msg.ID == "" { + msg.ID = utils.CreateCookieString() + } return client.Out.Encode(msg) } iq, ok := p.(*messages.IQClient) if ok { iq.From = client.JID - iq.ID = utils.CreateCookieString() + if iq.ID == "" { + iq.ID = utils.CreateCookieString() + } return client.Out.Encode(iq) } pc, ok := p.(*messages.PresenceClient) if ok { pc.From = client.JID - pc.ID = utils.CreateCookieString() + if pc.ID == "" { + pc.ID = utils.CreateCookieString() + } return client.Out.Encode(pc) } return client.Out.Encode(p) diff --git a/daemon/tester/bot.go b/daemon/tester/bot.go index d10f715..e516209 100644 --- a/daemon/tester/bot.go +++ b/daemon/tester/bot.go @@ -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) } diff --git a/messages/presence.go b/messages/presence.go index 9bc09da..c443efb 100644 --- a/messages/presence.go +++ b/messages/presence.go @@ -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,9 +45,9 @@ type PresenceClient struct { Type PresenceType `xml:"type,attr,omitempty"` Lang string `xml:"lang,attr,omitempty"` - Show string `xml:"show,omitempty"` // away, chat, dnd, xa - Status string `xml:"status,omitempty"` // sb []clientText - Priority string `xml:"priority,omitempty"` + 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"` Delay *Delay `xml:"delay"`