summaryrefslogtreecommitdiff
path: root/xmpp/router.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-07-04 22:10:55 +0200
committerxengineering <me@xengineering.eu>2023-07-04 22:15:17 +0200
commit48811e7d2487ebc3db49b8af7e20f57db4ac28f4 (patch)
treea317f8dc44ac9828ae5806e1fa1dee7547118619 /xmpp/router.go
parent5570ccd1d6b50042acbf2fad3793afa6dde79ca2 (diff)
parentd9fe0a4360770b1e4b6b4fb3686c3275ad1b6e6e (diff)
downloadlimox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar
limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar.zst
limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.zip
Merge branch 'element-handling'
This moves away from the concept to parse each individual XML token from the token and group them as a []xml.Token slice for further processing. While this is still possible, receiving aswell as sending has switched to define structs with XML tags which can be marshalled and unmarshalled with the xml.Encoder.EncodeElement() and xml.Decoder.DecodeElement() functions. Further documentation can be found at https://pkg.go.dev/encoding/xml#Unmarshal and https://pkg.go.dev/encoding/xml#Marshal. This merge reduces with its changes the number of code lines in the XMPP implementation by around 41 percent!
Diffstat (limited to 'xmpp/router.go')
-rw-r--r--xmpp/router.go62
1 files changed, 0 insertions, 62 deletions
diff --git a/xmpp/router.go b/xmpp/router.go
deleted file mode 100644
index 1e21c9b..0000000
--- a/xmpp/router.go
+++ /dev/null
@@ -1,62 +0,0 @@
-package xmpp
-
-import (
- "encoding/xml"
- "log"
-)
-
-// 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(*session, []xml.Token)
-}
-
-// 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 *session, e []xml.Token, t routingTable) {
- var name xml.Name
-
- // TODO a stronger definition of an XML element (as here
- // https://www.w3schools.com/xml/xml_elements.asp) would define that the
- // first Token of an element is a StartElement token. This would make this
- // code easier.
- escape := false
- for _, token := range e {
- switch s := token.(type) {
- case xml.StartElement:
- name = s.Name
- escape = true
- }
- if escape {
- break
- }
- }
-
- for _, r := range t {
- if name == r.name {
- r.handler(s, e)
- return
- }
- }
-
- log.Println("Could not route XML element")
-}