73 lines
1.4 KiB
Plaintext
73 lines
1.4 KiB
Plaintext
package extension
|
|
|
|
import (
|
|
"encoding/xml"
|
|
|
|
"dev.sum7.eu/genofire/yaja/messages"
|
|
"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 *messages.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 <- &messages.IQClient{
|
|
Type: messages.IQTypeResult,
|
|
To: client.JID,
|
|
From: model.NewJID(client.JID.Domain),
|
|
ID: msg.ID,
|
|
Body: queryByte,
|
|
}
|
|
|
|
log.Debug("send")
|
|
|
|
return true
|
|
}
|