summaryrefslogtreecommitdiff
path: root/xmpp/streams.go
blob: b9c0cb7d8cd7d5ff733f6a0d1af0332da6e5f99b (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package xmpp

import (
	"encoding/xml"
	"log"
)

func openStream(e *xml.Encoder, jid string) {
	start := xml.StartElement{
		xml.Name{"jabber:client", "stream:stream"},
		[]xml.Attr{
			xml.Attr{xml.Name{"", "from"}, jid},
			xml.Attr{xml.Name{"", "to"}, domainpart(jid)},
			xml.Attr{xml.Name{"", "version"}, "1.0"},
			xml.Attr{xml.Name{"", "xml:lang"}, "en"},
			xml.Attr{xml.Name{"", "xmlns:stream"}, "http://etherx.jabber.org/streams"},
		},
	}

	err := e.EncodeToken(start)
	if err != nil {
		log.Println("Could not encode stream start!")
	}
	err = e.Flush()
	if err != nil {
		log.Println("Could not flush after stream start!")
	}
}

func closeStream(e *xml.Encoder) {
	end := xml.EndElement{xml.Name{"jabber:client", "stream:stream"}}

	err := e.EncodeToken(end)
	if err != nil {
		log.Println("Could not encode stream end!")
	}
	err = e.Flush()
	if err != nil {
		log.Println("Could not flush after stream end!")
	}
}

func streamFeaturesHandler(s *session, e []xml.Token) {
	if hasSaslPlain(e) {
		s.sasl()
		return
	}

	if hasBind(e) {
		s.sendBind()
		return
	}

	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()
		}
	}
}