diff options
author | xengineering <me@xengineering.eu> | 2023-07-05 21:46:12 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-07-05 22:01:04 +0200 |
commit | 989c6ee9158f67cda2d6287d096c813075d13bd2 (patch) | |
tree | 489a25bc9fc583d7348befae175a199cfae64438 | |
parent | d614346ae08e5384e1dca7306ba64fbdc9931d2e (diff) | |
download | limox-989c6ee9158f67cda2d6287d096c813075d13bd2.tar limox-989c6ee9158f67cda2d6287d096c813075d13bd2.tar.zst limox-989c6ee9158f67cda2d6287d096c813075d13bd2.zip |
Move bind logic to new xmpp/iq.go
The bind request / response works with IQ stanzas. Because of the way
encoding and decoding is done with LimoX / the Go standard library it
makes sense to group IQ-based logic in a separat file since the struct
for IQs will be there.
-rw-r--r-- | xmpp/iq.go | 51 | ||||
-rw-r--r-- | xmpp/jid.go | 50 |
2 files changed, 51 insertions, 50 deletions
diff --git a/xmpp/iq.go b/xmpp/iq.go new file mode 100644 index 0000000..1c12c8c --- /dev/null +++ b/xmpp/iq.go @@ -0,0 +1,51 @@ +package xmpp + +import ( + "encoding/xml" + "fmt" + "math/rand" + "log" +) + +type bindRequest struct { + Bind struct { + Xmlns string `xml:"xmlns,attr"` + Resource struct { + Content string `xml:",chardata"` + } `xml:"resource"` + } `xml:"bind"` +} + +func (s *session) sendBind() { + + s.resourceReq = fmt.Sprintf("%016x", rand.Uint64()) + + start := xml.StartElement{ + xml.Name{"jabber:client", "iq"}, + []xml.Attr{ + xml.Attr{xml.Name{"", "id"}, s.resourceReq}, + xml.Attr{xml.Name{"", "type"}, "set"}, + }, + } + + inner := bindRequest{} + inner.Bind.Xmlns = "urn:ietf:params:xml:ns:xmpp-bind" + inner.Bind.Resource.Content = "limox-" + fmt.Sprintf("%08x", rand.Uint32()) + + err := s.tx.EncodeElement(inner, start) + if err != nil { + log.Println("Could not encode ressource binding!") + } +} + +type iqResponse struct { + Jid string `xml:"urn:ietf:params:xml:ns:xmpp-bind bind>jid"` +} + +func handleIqResponse(s *session, i iqResponse) { + if i.Jid != "" { + s.jid = i.Jid + s.sendPresence() + return + } +} diff --git a/xmpp/jid.go b/xmpp/jid.go index 9580ad5..90c1509 100644 --- a/xmpp/jid.go +++ b/xmpp/jid.go @@ -1,12 +1,5 @@ package xmpp -import ( - "encoding/xml" - "fmt" - "log" - "math/rand" -) - // domainpart extracts the domain name from a JID / XMPP address. See // https://datatracker.ietf.org/doc/html/rfc7622#section-3.2 for details. func domainpart(jid string) string { @@ -40,46 +33,3 @@ func username(jid string) string { return "" } - -type bindRequest struct { - Bind struct { - Xmlns string `xml:"xmlns,attr"` - Resource struct { - Content string `xml:",chardata"` - } `xml:"resource"` - } `xml:"bind"` -} - -func (s *session) sendBind() { - - s.resourceReq = fmt.Sprintf("%016x", rand.Uint64()) - - start := xml.StartElement{ - xml.Name{"jabber:client", "iq"}, - []xml.Attr{ - xml.Attr{xml.Name{"", "id"}, s.resourceReq}, - xml.Attr{xml.Name{"", "type"}, "set"}, - }, - } - - inner := bindRequest{} - inner.Bind.Xmlns = "urn:ietf:params:xml:ns:xmpp-bind" - inner.Bind.Resource.Content = "limox-" + fmt.Sprintf("%08x", rand.Uint32()) - - err := s.tx.EncodeElement(inner, start) - if err != nil { - log.Println("Could not encode ressource binding!") - } -} - -type iqResponse struct { - Jid string `xml:"urn:ietf:params:xml:ns:xmpp-bind bind>jid"` -} - -func handleIqResponse(s *session, i iqResponse) { - if i.Jid != "" { - s.jid = i.Jid - s.sendPresence() - return - } -} |