73 lines
1.4 KiB
Plaintext
73 lines
1.4 KiB
Plaintext
package extension
|
|
|
|
import (
|
|
"encoding/xml"
|
|
|
|
"dev.sum7.eu/genofire/yaja/xmpp"
|
|
"dev.sum7.eu/genofire/yaja/model"
|
|
"dev.sum7.eu/genofire/yaja/server/utils"
|
|
)
|
|
|
|
type IQExtensionDiscovery struct {
|
|
IQExtension
|
|
GetSpaces func() []string
|
|
}
|
|
|
|
func (ex *IQExtensionDiscovery) Spaces() []string { return []string{} }
|
|
|
|
func (ex *IQExtensionDiscovery) Get(msg *xmpp.IQClient, client *utils.Client) bool {
|
|
log := client.Log.WithField("extension", "roster").WithField("id", msg.ID)
|
|
|
|
// query encode
|
|
type query struct {
|
|
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"`
|
|
Body []byte `xml:",innerxml"`
|
|
}
|
|
q := &query{}
|
|
if err := xml.Unmarshal(msg.Body, q); err != nil {
|
|
return false
|
|
}
|
|
|
|
// answer query
|
|
q.Body = []byte{}
|
|
|
|
// build answer body
|
|
type feature struct {
|
|
XMLName xml.Name `xml:"feature"`
|
|
Var string `xml:"var,attr"`
|
|
}
|
|
for _, namespace := range ex.GetSpaces() {
|
|
if namespace == "" {
|
|
continue
|
|
}
|
|
itemByte, err := xml.Marshal(&feature{
|
|
Var: namespace,
|
|
})
|
|
if err != nil {
|
|
log.Warn(err)
|
|
continue
|
|
}
|
|
q.Body = append(q.Body, itemByte...)
|
|
}
|
|
|
|
// decode query
|
|
queryByte, err := xml.Marshal(q)
|
|
if err != nil {
|
|
log.Warn(err)
|
|
return false
|
|
}
|
|
|
|
// replay
|
|
client.Messages <- &xmpp.IQClient{
|
|
Type: xmpp.IQTypeResult,
|
|
To: client.JID,
|
|
From: xmpp.NewJID(client.JID.Domain),
|
|
ID: msg.ID,
|
|
Body: queryByte,
|
|
}
|
|
|
|
log.Debug("send")
|
|
|
|
return true
|
|
}
|