-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
83 lines (68 loc) · 1.8 KB
/
client.go
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
package main
import (
"encoding/xml"
"flag"
"log"
"xmpp"
)
var (
jid = flag.String("j", "", "JID")
password = flag.String("p", "", "Password")
)
func main() {
flag.Parse()
// Create stream and configure it as a client connection.
jid := must(xmpp.ParseJID(*jid)).(xmpp.JID)
stream := must(xmpp.NewStream(jid.Domain+":5222", &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream)
client := must(xmpp.NewClientXMPP(stream, jid, *password, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP)
log.Printf("Connection established for %s\n", client.JID)
// Announce presence.
client.Out <- xmpp.Presence{}
// Filter messages into dedicated channel and start a goroutine to log them.
_, messages := client.AddFilter(
xmpp.MatcherFunc(
func(v interface{}) bool {
_, ok := v.(*xmpp.Message)
return ok
},
),
)
go func() {
for message := range messages {
log.Printf("* message: %v\n", message)
}
}()
// Log any stanzas that are not handled elsewhere.
go func() {
for x := range client.In {
log.Printf("* recv: %v\n", x)
}
}()
// Get disco#info for home server.
info := &DiscoInfo{}
iq := xmpp.IQ{ID: xmpp.UUID4(), Type: "get", To: client.JID.Domain}
iq.PayloadEncode(info)
reply, _ := client.SendRecv(&iq)
reply.PayloadDecode(info)
log.Printf("* info: %v\n", info)
select {}
}
func must(v interface{}, err error) interface{} {
if err != nil {
log.Fatal(err)
}
return v
}
type DiscoInfo struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"`
Identity []DiscoIdentity `xml:"identity"`
Feature []DiscoFeature `xml:"feature"`
}
type DiscoIdentity struct {
Type string `xml:"type,attr"`
Name string `xml:"name,attr"`
Category string `xml:"category,attr"`
}
type DiscoFeature struct {
Var string `xml:"var,attr"`
}