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

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

type iqRx struct {
	XMLName xml.Name `xml:"jabber:client iq"`
	Type string `xml:"type,attr"`
	Id string `xml:"id,attr"`
	Bind struct{
		Jid string `xml:"jid"`
	} `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
	Query []RosterItem `xml:"jabber:iq:roster query>item"`
}

type RosterItem struct {
	Jid string `xml:"jid,attr"`
	Subscription string `xml:"subscription,attr"`
	Name string `xml:"name,attr"`
}

func (i iqRx) handle(s *session) {
	if i.Bind.Jid != "" {
		s.jid = i.Bind.Jid
		s.sendPresence()
		s.sendRosterGet()
		return
	}

	if len(i.Query) > 0 {
		log.Println("Got roster:")
		for _, v := range i.Query {
			log.Printf("- %s\n", v.Jid)
		}
	}
}

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

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

	req := bindSet{}
	req.Id = s.resourceReq
	req.Type = "set"
	req.Bind.Resource = "limox-" + fmt.Sprintf("%08x", rand.Uint32())

	err := s.tx.Encode(req)
	if err != nil {
		log.Println("Could not encode ressource binding!")
	}
}

type rosterGet struct {
	XMLName xml.Name `xml:"jabber:client iq"`
	Type string `xml:"type,attr,omitempty"`
	Id string `xml:"id,attr,omitempty"`
	Query string `xml:"jabber:iq:roster query"`
}

func (s *session) sendRosterGet() {
	req := rosterGet{}
	req.Id = fmt.Sprintf("%016x", rand.Uint64())
	req.Type = "get"

	err := s.tx.Encode(req)
	if err != nil {
		log.Println("Could not encode ressource binding!")
	}
}