package component import ( "encoding/xml" "github.com/bdlm/log" "gosrc.io/xmpp" "gosrc.io/xmpp/stanza" ) func (c *Config) handleDiscoInfo(s xmpp.Sender, p stanza.Packet) { iq, ok := p.(stanza.IQ) if !ok || iq.Type != "get" { return } discoInfo, ok := iq.Payload.(*stanza.DiscoInfo) if !ok { return } attrs := iq.Attrs iq = stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, To: attrs.From, From: attrs.To, Id: attrs.Id}) payload := stanza.DiscoInfo{ XMLName: xml.Name{ Space: stanza.NSDiscoInfo, Local: "query", }, Features: []stanza.Feature{ {Var: stanza.NSDiscoInfo}, {Var: stanza.NSDiscoItems}, {Var: stanza.NSMsgReceipts}, {Var: stanza.NSMsgChatMarkers}, {Var: stanza.NSMsgChatStateNotifications}, // {Var: "http://jabber.org/protocol/muc"}, }, } if discoInfo.Node == "" { payload.Identity = append(payload.Identity, stanza.Identity{ Name: c.Type, Category: "gateway", Type: "service", }) } iq.Payload = &payload log.WithFields(map[string]interface{}{ "type": c.Type, "from": attrs.From, "to": attrs.To, }).Debug("disco info") s.Send(iq) } func (c *Config) handleDiscoItems(s xmpp.Sender, p stanza.Packet) { iq, ok := p.(stanza.IQ) if !ok || iq.Type != "get" { return } discoItems, ok := iq.Payload.(*stanza.DiscoItems) if !ok { return } attrs := iq.Attrs iq = stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, To: attrs.From, From: attrs.To, Id: attrs.Id}) payload := stanza.DiscoItems{} if discoItems.Node == "" { payload.Items = []stanza.DiscoItem{ {Name: c.Type, JID: c.Host, Node: "node1"}, } } iq.Payload = &payload log.WithFields(map[string]interface{}{ "type": c.Type, "id": attrs.Id, "to": attrs.To, }).Debug("disco items") s.Send(iq) } func (c *Config) handleIQ(s xmpp.Sender, p stanza.Packet) { iq, ok := p.(stanza.IQ) if !ok || iq.Type != "get" { return } xError := stanza.Err{ Code: 501, Reason: "feature-not-implemented", Type: "cancel", } resp := iq.MakeError(xError) attrs := iq.Attrs log.WithFields(map[string]interface{}{ "type": c.Type, "to": attrs.To, "id": attrs.Id, }).Debugf("ignore: %v", iq.Payload) s.Send(resp) } func (c *Config) handleMessage(s xmpp.Sender, p stanza.Packet) { msg, ok := p.(stanza.Message) attrs := msg.Attrs if !ok { pr, ok := p.(stanza.Presence) attrs = pr.Attrs if !ok { return } } logger := log.WithFields(map[string]interface{}{ "type": c.Type, "id": attrs.Id, "to": attrs.To, }) if attrs.Type == "error" { logger.Error(msg.XMPPFormat()) return } if c.XMPPDebug { logger.Debug(msg.XMPPFormat()) } c.comp.Send(p) }