summaryrefslogtreecommitdiff
path: root/xmpp/routing.go
blob: 4058dfba98967ae80938cdbf899544ff07f7a1bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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`}:
		parse(streamFeatures{}, s, d, c)
	case xml.Name{`urn:ietf:params:xml:ns:xmpp-sasl`, `success`}:
		parse(saslSuccess{}, s, d, c)
	case xml.Name{`jabber:client`, `iq`}:
		parse(iqRx{}, s, d, c)
	case xml.Name{`jabber:client`, `message`}:
		parse(message{}, s, d, c)
	default:
		d.Skip()
	}
}

func parse[T any](data T, s *xml.StartElement, d *xml.Decoder, c chan<- any) {
	err := d.DecodeElement(&data, s)
	if err != nil {
		log.Printf("Could not decode stream features: %v\n", err)
	} else {
		c <- data
	}
}

func handle(s *session, element any) {
	switch t := element.(type) {
	case streamFeatures:
		handleStreamFeatures(s, t)
	case saslSuccess:
		handleSaslSuccess(s)
	case iqRx:
		t.handle(s)
	case message:
		handleMessage(s, t)
	default:
		log.Printf("Unknown parsed element: %v", t)
	}
}