summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-07-04 14:25:21 +0200
committerxengineering <me@xengineering.eu>2023-07-04 14:26:01 +0200
commit3a5e9fce02264eeed884b15781593479fda9296a (patch)
tree96fc5facb359c70c26623621f6022202f994054f
parent1d3dfa5b93000bc4109ba49ea018e72fbf4f5753 (diff)
downloadlimox-3a5e9fce02264eeed884b15781593479fda9296a.tar
limox-3a5e9fce02264eeed884b15781593479fda9296a.tar.zst
limox-3a5e9fce02264eeed884b15781593479fda9296a.zip
Rework routing completely
-rw-r--r--xmpp/router.go40
-rw-r--r--xmpp/routing.go21
-rw-r--r--xmpp/streams.go11
-rw-r--r--xmpp/xml.go2
4 files changed, 22 insertions, 52 deletions
diff --git a/xmpp/router.go b/xmpp/router.go
deleted file mode 100644
index d437b28..0000000
--- a/xmpp/router.go
+++ /dev/null
@@ -1,40 +0,0 @@
-package xmpp
-
-import (
- "encoding/xml"
-)
-
-// routingTable is a data structure which contains routing information for XML
-// elements. The xml.StartElement at the beginning of an XML element has a name
-// containing the XML namespace and a local name. Based on this compisition
-// which forms the xml.Name the appropriate handler function is defined by each
-// entry of the routingTable.
-type routingTable []struct {
- name xml.Name
- handler func(s *xml.StartElement, d *xml.Decoder, c chan<- any)
-}
-
-// getRoutingTable returns the routing table used in
-// xengineering.eu/limox/xmpp. Since Go does not allow such a datatype as a
-// constant such a function is a simple yet inefficient approach to guarantee
-// that an unmodified routing table is delivered to each user. A global
-// variable would have the problem that it could be altered during execution.
-func getRoutingTable() routingTable {
- return 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`}, iqHandler},
- }
-}
-
-// 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.
-func route(s *xml.StartElement, d *xml.Decoder, c chan<- any, t routingTable) {
- for _, v := range t {
- if v.name == (*s).Name {
- v.handler(s, d, c)
- }
- }
-}
diff --git a/xmpp/routing.go b/xmpp/routing.go
new file mode 100644
index 0000000..e8aa4ed
--- /dev/null
+++ b/xmpp/routing.go
@@ -0,0 +1,21 @@
+package xmpp
+
+import (
+ "encoding/xml"
+ "log"
+)
+
+func route(s *xml.StartElement, d *xml.Decoder, c chan<- any) {
+ switch (*s).Name {
+ case xml.Name{`http://etherx.jabber.org/streams`, `features`}:
+ data := streamFeatures{}
+ err := d.DecodeElement(&data, s)
+ if err != nil {
+ log.Printf("Could not decode stream features: %v\n", err)
+ } else {
+ log.Print(data) // TODO
+ }
+ default:
+ d.Skip()
+ }
+}
diff --git a/xmpp/streams.go b/xmpp/streams.go
index b7cde3e..3aca8a2 100644
--- a/xmpp/streams.go
+++ b/xmpp/streams.go
@@ -67,14 +67,3 @@ func iqHandler(s *session, e []xml.Token) {
type streamFeatures struct {
SaslMechanisms []string `xml:"mechanisms>mechanism"`
}
-
-func streamFeaturesHandler(s *xml.StartElement, d *xml.Decoder, c chan<- any) {
- e := streamFeatures{}
-
- err := d.DecodeElement(&e, s)
- if err != nil {
- log.Printf("Could not decode stream features: %v\n", err)
- } else {
- c <- e
- }
-}
diff --git a/xmpp/xml.go b/xmpp/xml.go
index 470a2ef..e6fccee 100644
--- a/xmpp/xml.go
+++ b/xmpp/xml.go
@@ -27,7 +27,7 @@ func runRx(ctx context.Context, chn chan<- any, conn *tls.Conn) {
if e.Name.Local == "stream" {
// new server-side stream TODO what to do with this info?
} else {
- route(&e, d, chn, getRoutingTable())
+ route(&e, d, chn)
}
case xml.EndElement:
if e.Name.Local == "stream" {