diff options
author | xengineering <me@xengineering.eu> | 2023-07-06 21:45:31 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-07-06 21:45:31 +0200 |
commit | 16d5f079cc51bffe9f07b356f3a42d5a09a45317 (patch) | |
tree | b370e7c5b220351b9c42f8f19fa30672d8310724 /xmpp/iq.go | |
parent | d614346ae08e5384e1dca7306ba64fbdc9931d2e (diff) | |
parent | 007f413e457ff0b733447acba61b11d6813dd41c (diff) | |
download | limox-16d5f079cc51bffe9f07b356f3a42d5a09a45317.tar limox-16d5f079cc51bffe9f07b356f3a42d5a09a45317.tar.zst limox-16d5f079cc51bffe9f07b356f3a42d5a09a45317.zip |
Merge branch 'roster-backend'
Getting the roster (contact list) is crucial to provide a MVP LimoX
since the chats should be grouped by contacts.
For the MVP it is only relevant to get the roster on connect. Everything
else can be done later.
Diffstat (limited to 'xmpp/iq.go')
-rw-r--r-- | xmpp/iq.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/xmpp/iq.go b/xmpp/iq.go new file mode 100644 index 0000000..1cb7fd5 --- /dev/null +++ b/xmpp/iq.go @@ -0,0 +1,81 @@ +package xmpp + +import ( + "encoding/xml" + "fmt" + "log" + "math/rand" +) + +type iqRx struct { + XMLName xml.Name `xml:"jabber:client iq"` + Type string `xml:"type,attr"` + Id string `xml:"id,attr"` + Bind struct { + Jid string `xml:"jid"` + } `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"` + Query []RosterItem `xml:"jabber:iq:roster query>item"` +} + +type RosterItem struct { + Jid string `xml:"jid,attr"` + Subscription string `xml:"subscription,attr"` + Name string `xml:"name,attr"` +} + +func (i iqRx) handle(s *session) { + if i.Bind.Jid != "" { + s.jid = i.Bind.Jid + s.sendPresence() + s.sendRosterGet() + return + } + + if len(i.Query) > 0 { + log.Println("Got roster:") + for _, v := range i.Query { + log.Printf("- %s\n", v.Jid) + } + } +} + +type bindSet struct { + XMLName xml.Name `xml:"jabber:client iq"` + Type string `xml:"type,attr,omitempty"` + Id string `xml:"id,attr,omitempty"` + Bind struct { + Resource string `xml:"resource,omitempty"` + } `xml:"urn:ietf:params:xml:ns:xmpp-bind bind,omitempty"` +} + +func (s *session) sendBind() { + s.resourceReq = fmt.Sprintf("%016x", rand.Uint64()) + + req := bindSet{} + req.Id = s.resourceReq + req.Type = "set" + req.Bind.Resource = "limox-" + fmt.Sprintf("%08x", rand.Uint32()) + + err := s.tx.Encode(req) + if err != nil { + log.Println("Could not encode ressource binding!") + } +} + +type rosterGet struct { + XMLName xml.Name `xml:"jabber:client iq"` + Type string `xml:"type,attr,omitempty"` + Id string `xml:"id,attr,omitempty"` + Query string `xml:"jabber:iq:roster query"` +} + +func (s *session) sendRosterGet() { + req := rosterGet{} + req.Id = fmt.Sprintf("%016x", rand.Uint64()) + req.Type = "get" + + err := s.tx.Encode(req) + if err != nil { + log.Println("Could not encode ressource binding!") + } +} |