summaryrefslogtreecommitdiff
path: root/xmpp/iq.go
blob: cd96384c5f8e10fd6ef2d7998fec4125168a8457 (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
package xmpp

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

type iq struct {
	XMLName xml.Name `xml:"jabber:client iq"`
	Type string `xml:"type,attr,omitempty"`
	Id string `xml:"id,attr,omitempty"`
	Bind struct{
		Jid string `xml:"jid,omitempty"`
	} `xml:"urn:ietf:params:xml:ns:xmpp-bind bind,omitempty"`
}

type bindRequest struct {
	Bind struct {
		Xmlns    string `xml:"xmlns,attr"`
		Resource struct {
			Content string `xml:",chardata"`
		} `xml:"resource"`
	} `xml:"bind"`
}

func (s *session) sendBind() {

	s.resourceReq = fmt.Sprintf("%016x", rand.Uint64())

	start := xml.StartElement{
		xml.Name{"jabber:client", "iq"},
		[]xml.Attr{
			xml.Attr{xml.Name{"", "id"}, s.resourceReq},
			xml.Attr{xml.Name{"", "type"}, "set"},
		},
	}

	inner := bindRequest{}
	inner.Bind.Xmlns = "urn:ietf:params:xml:ns:xmpp-bind"
	inner.Bind.Resource.Content = "limox-" + fmt.Sprintf("%08x", rand.Uint32())

	err := s.tx.EncodeElement(inner, start)
	if err != nil {
		log.Println("Could not encode ressource binding!")
	}
}

func handleIq(s *session, i iq) {
	if i.Bind.Jid != "" {
		s.jid = i.Bind.Jid
		s.sendPresence()
		return
	}
}