From 82f67ff228a61b447f9bc0fcf6d9b7c5e17d57c2 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 21 May 2023 12:09:58 +0200 Subject: Implement XML element routing --- xmpp/routing.go | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'xmpp/routing.go') diff --git a/xmpp/routing.go b/xmpp/routing.go index c691af3..3d1f9fe 100644 --- a/xmpp/routing.go +++ b/xmpp/routing.go @@ -44,9 +44,49 @@ func (r *tokenRouter) route(t xml.Token) error { return errors.New("Stream was closed by server") case 1: // call elementRouter - r.buffer = r.buffer[:1] + routeElement(r.buffer) + r.buffer = r.buffer[:0] } + case xml.ProcInst: + log.Println("Ignoring xml.ProcInst element") + r.buffer = r.buffer[:len(r.buffer)-1] + case xml.Directive: + log.Println("Ignoring xml.Directive element") + r.buffer = r.buffer[:len(r.buffer)-1] + case xml.Comment: + log.Println("Ignoring xml.Comment element") + r.buffer = r.buffer[:len(r.buffer)-1] } return nil } + +func routeElement(b []xml.Token) { + tab := elementRoutingTable{ + {xml.Name{"http://etherx.jabber.org/streams", "features"}, streamFeatures}, + } + + switch unwrapped := b[0].(type) { + case xml.StartElement: + for _, v := range(tab) { + if unwrapped.Name == v.name { + v.handler(b) + return + } + log.Printf("Ignoring XML element '%s' from namespace '%s'", + unwrapped.Name.Local, unwrapped.Name.Space) + } + default: + log.Println("First buffer element is not xml.StartElement") + return + } +} + +type elementRoutingTable []struct { + name xml.Name + handler func(b []xml.Token) +} + +func streamFeatures(b []xml.Token) { + log.Println("Received stream features") +} -- cgit v1.2.3-70-g09d2