sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/server/extension/iq_disco.bak

73 lines
1.5 KiB
Plaintext

package extension
import (
"encoding/xml"
"dev.sum7.eu/genofire/yaja/database"
"dev.sum7.eu/genofire/yaja/messages"
"dev.sum7.eu/genofire/yaja/model"
"dev.sum7.eu/genofire/yaja/server/utils"
)
type IQDisco struct {
IQExtension
Database *database.State
}
func (ex *IQDisco) Spaces() []string { return []string{"http://jabber.org/protocol/disco#items"} }
func (ex *IQDisco) Get(msg *messages.IQClient, client *utils.Client) bool {
log := client.Log.WithField("extension", "disco-item").WithField("id", msg.ID)
// query encode
type query struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items 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 item struct {
XMLName xml.Name `xml:"item"`
JID string `xml:"jid,attr"`
}
if acc := ex.Database.GetAccount(client.JID); acc != nil {
for jid, _ := range acc.Bookmarks {
itemByte, err := xml.Marshal(&item{
JID: jid,
})
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
}
// reply
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
}