2018-01-11 21:15:54 +00:00
|
|
|
package main
|
|
|
|
|
2018-01-12 18:08:47 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-31 17:41:32 +00:00
|
|
|
"log"
|
2018-01-12 18:08:47 +00:00
|
|
|
|
2018-12-26 17:50:01 +00:00
|
|
|
"gosrc.io/xmpp"
|
2018-01-12 18:08:47 +00:00
|
|
|
)
|
2018-01-11 21:15:54 +00:00
|
|
|
|
|
|
|
func main() {
|
2019-06-08 17:42:02 +00:00
|
|
|
opts := xmpp.ComponentOptions{
|
|
|
|
Domain: "service.localhost",
|
|
|
|
Secret: "mypass",
|
|
|
|
Address: "localhost:8888",
|
|
|
|
Name: "Test Component",
|
|
|
|
Category: "gateway",
|
|
|
|
Type: "service",
|
|
|
|
}
|
|
|
|
component, err := xmpp.NewComponent(opts)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%+v", err)
|
2019-05-31 17:41:32 +00:00
|
|
|
}
|
2018-01-12 18:08:47 +00:00
|
|
|
|
2019-06-08 17:42:02 +00:00
|
|
|
// If you pass the component to a connection manager, it will handle the reconnect policy
|
|
|
|
// for you automatically.
|
|
|
|
cm := xmpp.NewStreamManager(component, nil)
|
|
|
|
err = cm.Start()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-01-13 17:50:17 +00:00
|
|
|
|
2019-06-08 17:42:02 +00:00
|
|
|
// Iterator to receive packets coming from our XMPP connection
|
|
|
|
for packet := range component.Recv() {
|
2018-01-13 17:50:17 +00:00
|
|
|
switch p := packet.(type) {
|
|
|
|
case xmpp.IQ:
|
2018-01-15 11:52:28 +00:00
|
|
|
switch inner := p.Payload[0].(type) {
|
2019-06-09 11:02:58 +00:00
|
|
|
case *xmpp.DiscoInfo:
|
|
|
|
fmt.Println("DiscoInfo")
|
|
|
|
if p.Type == "get" {
|
|
|
|
discoResult(component, p.PacketAttrs, inner)
|
|
|
|
}
|
2018-01-26 10:40:59 +00:00
|
|
|
case *xmpp.DiscoItems:
|
|
|
|
fmt.Println("DiscoItems")
|
|
|
|
if p.Type == "get" {
|
2019-06-08 17:42:02 +00:00
|
|
|
discoItems(component, p.PacketAttrs, inner)
|
2018-01-15 11:28:34 +00:00
|
|
|
}
|
2019-06-10 10:35:48 +00:00
|
|
|
case *xmpp.Version:
|
|
|
|
fmt.Println("Version")
|
|
|
|
if p.Type == "get" {
|
|
|
|
version(component, p.PacketAttrs)
|
|
|
|
}
|
2018-01-13 18:27:46 +00:00
|
|
|
default:
|
2018-01-20 17:56:07 +00:00
|
|
|
fmt.Println("ignoring iq packet", inner)
|
2018-01-22 22:33:16 +00:00
|
|
|
xError := xmpp.Err{
|
2018-01-20 17:56:07 +00:00
|
|
|
Code: 501,
|
|
|
|
Reason: "feature-not-implemented",
|
|
|
|
Type: "cancel",
|
|
|
|
}
|
2018-01-22 22:33:16 +00:00
|
|
|
reply := p.MakeError(xError)
|
2019-06-08 17:42:02 +00:00
|
|
|
_ = component.Send(&reply)
|
2018-01-13 18:14:26 +00:00
|
|
|
}
|
2018-02-13 21:07:15 +00:00
|
|
|
|
2018-01-23 08:08:21 +00:00
|
|
|
case xmpp.Message:
|
|
|
|
fmt.Println("Received message:", p.Body)
|
2018-02-13 21:07:15 +00:00
|
|
|
|
2018-01-23 08:08:21 +00:00
|
|
|
case xmpp.Presence:
|
|
|
|
fmt.Println("Received presence:", p.Type)
|
2018-02-13 21:07:15 +00:00
|
|
|
|
2018-01-13 17:50:17 +00:00
|
|
|
default:
|
2018-01-20 17:56:07 +00:00
|
|
|
fmt.Println("ignoring packet:", packet)
|
2018-01-13 17:50:17 +00:00
|
|
|
}
|
2018-01-12 18:08:47 +00:00
|
|
|
}
|
2018-01-11 21:15:54 +00:00
|
|
|
}
|
2018-01-14 15:54:12 +00:00
|
|
|
|
2019-06-09 11:02:58 +00:00
|
|
|
func discoResult(c *xmpp.Component, attrs xmpp.PacketAttrs, info *xmpp.DiscoInfo) {
|
|
|
|
iq := xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en")
|
|
|
|
var identity xmpp.Identity
|
|
|
|
if info.Node == "" {
|
|
|
|
identity = xmpp.Identity{
|
|
|
|
Name: c.Name,
|
|
|
|
Category: c.Category,
|
|
|
|
Type: c.Type,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := xmpp.DiscoInfo{
|
|
|
|
Identity: identity,
|
|
|
|
Features: []xmpp.Feature{
|
|
|
|
{Var: xmpp.NSDiscoInfo},
|
|
|
|
{Var: xmpp.NSDiscoItems},
|
2019-06-10 10:35:48 +00:00
|
|
|
{Var: "jabber:iq:version"},
|
2019-06-09 11:02:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
iq.AddPayload(&payload)
|
|
|
|
|
|
|
|
_ = c.Send(iq)
|
|
|
|
}
|
|
|
|
|
2019-06-08 17:42:02 +00:00
|
|
|
func discoItems(c *xmpp.Component, attrs xmpp.PacketAttrs, items *xmpp.DiscoItems) {
|
2018-01-26 10:40:59 +00:00
|
|
|
iq := xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en")
|
|
|
|
|
|
|
|
var payload xmpp.DiscoItems
|
|
|
|
if items.Node == "" {
|
|
|
|
payload = xmpp.DiscoItems{
|
|
|
|
Items: []xmpp.DiscoItem{
|
2018-01-26 11:37:27 +00:00
|
|
|
{Name: "test node", JID: "service.localhost", Node: "node1"},
|
2018-01-26 10:40:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iq.AddPayload(&payload)
|
2019-06-08 17:42:02 +00:00
|
|
|
_ = c.Send(iq)
|
2018-01-14 15:54:12 +00:00
|
|
|
}
|
2019-06-10 10:35:48 +00:00
|
|
|
|
|
|
|
func version(c *xmpp.Component, attrs xmpp.PacketAttrs) {
|
|
|
|
iq := xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en")
|
|
|
|
|
|
|
|
var payload xmpp.Version
|
|
|
|
payload.Name = "Fluux XMPP Component"
|
|
|
|
payload.Version = "0.0.1"
|
|
|
|
iq.AddPayload(&payload)
|
|
|
|
_ = c.Send(iq)
|
|
|
|
}
|