2017-12-17 13:31:02 +01:00
|
|
|
package extension
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
|
2018-02-07 15:34:18 +01:00
|
|
|
"dev.sum7.eu/genofire/yaja/messages"
|
2018-02-10 13:34:42 +01:00
|
|
|
"dev.sum7.eu/genofire/yaja/model"
|
2018-02-07 15:34:18 +01:00
|
|
|
"dev.sum7.eu/genofire/yaja/server/utils"
|
2017-12-17 13:31:02 +01:00
|
|
|
)
|
|
|
|
|
2017-12-17 15:39:36 +01:00
|
|
|
type IQExtensionDiscovery struct {
|
2017-12-17 13:31:02 +01:00
|
|
|
IQExtension
|
|
|
|
GetSpaces func() []string
|
|
|
|
}
|
|
|
|
|
2017-12-17 15:39:36 +01:00
|
|
|
func (ex *IQExtensionDiscovery) Spaces() []string { return []string{} }
|
2017-12-17 13:31:02 +01:00
|
|
|
|
2018-02-07 19:32:11 +01:00
|
|
|
func (ex *IQExtensionDiscovery) Get(msg *messages.IQClient, client *utils.Client) bool {
|
2017-12-17 13:31:02 +01:00
|
|
|
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{}
|
2017-12-17 17:50:51 +01:00
|
|
|
if err := xml.Unmarshal(msg.Body, q); err != nil {
|
2017-12-17 13:31:02 +01:00
|
|
|
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
|
2018-02-07 19:32:11 +01:00
|
|
|
client.Messages <- &messages.IQClient{
|
2017-12-17 13:31:02 +01:00
|
|
|
Type: messages.IQTypeResult,
|
2018-02-10 13:34:42 +01:00
|
|
|
To: client.JID,
|
2018-02-13 20:05:18 +01:00
|
|
|
From: messages.NewJID(client.JID.Domain),
|
2017-12-17 13:31:02 +01:00
|
|
|
ID: msg.ID,
|
|
|
|
Body: queryByte,
|
2017-12-17 15:39:36 +01:00
|
|
|
}
|
2017-12-17 13:31:02 +01:00
|
|
|
|
|
|
|
log.Debug("send")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|