summaryrefslogtreecommitdiff
path: root/xmpp/jid.go
blob: fd0d7ae2e45d7c932517485860e7fdff29426e75 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package xmpp

import (
	"encoding/xml"
	"fmt"
	"log"
	"math/rand"
)

// domainpart extracts the domain name from a JID / XMPP address. See
// https://datatracker.ietf.org/doc/html/rfc7622#section-3.2 for details.
func domainpart(jid string) string {
	list := []rune(jid)

	for i, v := range list {
		if v == '/' {
			list = list[:i]
			break
		}
	}

	for i, v := range list {
		if v == '@' {
			list = list[i+1:]
			break
		}
	}

	return string(list)
}

func username(jid string) string {
	list := []rune(jid)

	for i, v := range list {
		if v == '@' {
			return string(list[:i])
		}
	}

	return ""
}

func hasBind(e []xml.Token) bool {
	bind := xml.Name{`urn:ietf:params:xml:ns:xmpp-bind`, `bind`}

	for _, v := range e {
		switch s := v.(type) {
		case xml.StartElement:
			if s.Name == bind {
				return true
			}
		}
	}

	return false
}

func (s *session) sendBind() {
	s.resourceReq = fmt.Sprintf("%016x", rand.Uint64())

	iqStart := xml.StartElement{
		xml.Name{"jabber:client", "iq"},
		[]xml.Attr{
			xml.Attr{xml.Name{"", "id"}, s.resourceReq},
			xml.Attr{xml.Name{"", "type"}, "set"},
		},
	}
	iqEnd := iqStart.End()

	bindStart := xml.StartElement{
		xml.Name{"urn:ietf:params:xml:ns:xmpp-bind", "bind"},
		[]xml.Attr{},
	}
	bindEnd := bindStart.End()

	resourceStart := xml.StartElement{
		xml.Name{"", "resource"},
		[]xml.Attr{},
	}
	resourceEnd := resourceStart.End()

	name := xml.CharData("limox-" + fmt.Sprintf("%08x", rand.Uint32()))

	tokens := [...]xml.Token{
		iqStart,
		bindStart,
		resourceStart,
		name,
		resourceEnd,
		bindEnd,
		iqEnd,
	}

	for _, v := range tokens {
		err := s.ed.encodeToken(v)
		if err != nil {
			log.Println("Could not encode ressource binding!")
			return
		}
	}
}