summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-06-30 20:40:47 +0200
committerxengineering <me@xengineering.eu>2023-06-30 20:40:47 +0200
commitdee833b803494ce962c577ddf950795a2fd490ae (patch)
treef353e8c8d653d9d94ab7e45a624ac8bd27f0e32f
parente2c057571ae309cf503851ab8f63c2159f2ef4bc (diff)
downloadlimox-dee833b803494ce962c577ddf950795a2fd490ae.tar
limox-dee833b803494ce962c577ddf950795a2fd490ae.tar.zst
limox-dee833b803494ce962c577ddf950795a2fd490ae.zip
Send initial presence after resource result
This completes the connection process.
-rw-r--r--xmpp/jid.go4
-rw-r--r--xmpp/presence.go24
-rw-r--r--xmpp/router.go9
-rw-r--r--xmpp/session.go15
-rw-r--r--xmpp/stream_pair.go24
5 files changed, 60 insertions, 16 deletions
diff --git a/xmpp/jid.go b/xmpp/jid.go
index 0446d19..fd0d7ae 100644
--- a/xmpp/jid.go
+++ b/xmpp/jid.go
@@ -57,10 +57,12 @@ func hasBind(e []xml.Token) bool {
}
func (s *session) sendBind() {
+ s.resourceReq = fmt.Sprintf("%016x", rand.Uint64())
+
iqStart := xml.StartElement{
xml.Name{"jabber:client", "iq"},
[]xml.Attr{
- xml.Attr{xml.Name{"", "id"}, fmt.Sprintf("%016x", rand.Uint64())},
+ xml.Attr{xml.Name{"", "id"}, s.resourceReq},
xml.Attr{xml.Name{"", "type"}, "set"},
},
}
diff --git a/xmpp/presence.go b/xmpp/presence.go
new file mode 100644
index 0000000..b6ea3b5
--- /dev/null
+++ b/xmpp/presence.go
@@ -0,0 +1,24 @@
+package xmpp
+
+import (
+ "encoding/xml"
+ "log"
+)
+
+func (s *session) sendPresence() {
+ start := xml.StartElement{
+ xml.Name{"", "presence"},
+ []xml.Attr{},
+ }
+ end := start.End()
+
+ tokens := [...]xml.Token{start, end}
+
+ for _, v := range tokens {
+ err := s.ed.encodeToken(v)
+ if err != nil {
+ log.Println("Could not encode presence!")
+ return
+ }
+ }
+}
diff --git a/xmpp/router.go b/xmpp/router.go
index 1d9c3e5..1e21c9b 100644
--- a/xmpp/router.go
+++ b/xmpp/router.go
@@ -25,17 +25,10 @@ func getRoutingTable() routingTable {
{xml.Name{`http://etherx.jabber.org/streams`, `features`}, streamFeaturesHandler},
{xml.Name{`urn:ietf:params:xml:ns:xmpp-sasl`, `success`}, saslSuccessHandler},
{xml.Name{`urn:ietf:params:xml:ns:xmpp-sasl`, `failure`}, saslFailureHandler},
- {xml.Name{`jabber:client`, `iq`}, voidIq},
+ {xml.Name{`jabber:client`, `iq`}, iqHandler},
}
}
-// voidIq just logs that an IQ element was received. In the current state of
-// the software there is no further processing.
-// TODO process IQs for error handling and further information processing.
-func voidIq(s *session, e []xml.Token) {
- log.Println("Received IQ element")
-}
-
// route determines the correct handler function for the given XML element by a
// given routingTable. In addition it executes the determined handler function.
// If no handler function is found an error message is send via the log module.
diff --git a/xmpp/session.go b/xmpp/session.go
index bfa2582..a43e4f4 100644
--- a/xmpp/session.go
+++ b/xmpp/session.go
@@ -12,13 +12,14 @@ type SessionDisconnect struct{}
type SessionShouldDisconnect struct{}
type session struct {
- jid string
- pwd string
- in chan any
- out chan<- any
- transport *tls.Conn
- ed encoderDecoder
- rx chan xml.Token
+ jid string
+ pwd string
+ in chan any
+ out chan<- any
+ transport *tls.Conn
+ ed encoderDecoder
+ rx chan xml.Token
+ resourceReq string
}
func StartSession(out chan<- any, jid string, pwd string) (in chan<- any) {
diff --git a/xmpp/stream_pair.go b/xmpp/stream_pair.go
index 4690f57..87df86a 100644
--- a/xmpp/stream_pair.go
+++ b/xmpp/stream_pair.go
@@ -105,3 +105,27 @@ func streamFeaturesHandler(s *session, e []xml.Token) {
log.Println("Stream has no implemented features!")
}
+
+func iqHandler(s *session, e []xml.Token) {
+ isResult := false
+ idMatches := false
+
+ result := xml.Attr{xml.Name{"", "type"}, "result"}
+ id := xml.Attr{xml.Name{"", "id"}, s.resourceReq}
+
+ switch start := e[0].(type) {
+ case xml.StartElement:
+ for _, v := range start.Attr {
+ if v == result {
+ isResult = true
+ }
+ if v == id {
+ idMatches = true
+ }
+ }
+
+ if isResult && idMatches {
+ s.sendPresence()
+ }
+ }
+}