go-xmpp/cmd/xmpp_component/xmpp_component.go

106 lines
2.2 KiB
Go
Raw Normal View History

2018-01-11 13:15:54 -08:00
package main
2018-01-12 10:08:47 -08:00
import (
"fmt"
2019-05-31 10:41:32 -07:00
"log"
2018-01-12 10:08:47 -08:00
"gosrc.io/xmpp"
2018-01-12 10:08:47 -08:00
)
2018-01-11 13:15:54 -08:00
func main() {
2018-01-26 03:37:27 -08:00
component := MyComponent{Name: "Test Component", Category: "gateway", Type: "service"}
component.xmpp = &xmpp.Component{Host: "service.localhost", Secret: "mypass"}
2019-05-31 10:41:32 -07:00
if err := component.xmpp.Connect("localhost:8888"); err != nil {
log.Fatal(err)
}
2018-01-12 10:08:47 -08:00
for {
2018-01-14 07:54:12 -08:00
packet, err := component.xmpp.ReadPacket()
2018-01-12 10:08:47 -08:00
if err != nil {
2018-01-13 08:46:10 -08:00
fmt.Println("read error", err)
2018-01-12 10:08:47 -08:00
return
}
2018-01-13 09:50:17 -08:00
switch p := packet.(type) {
case xmpp.IQ:
2018-01-15 03:52:28 -08:00
switch inner := p.Payload[0].(type) {
2018-01-20 09:56:07 -08:00
case *xmpp.DiscoInfo:
fmt.Println("Disco Info")
if p.Type == "get" {
2018-01-26 02:40:59 -08:00
DiscoResult(component, p.PacketAttrs, inner)
}
case *xmpp.DiscoItems:
fmt.Println("DiscoItems")
if p.Type == "get" {
DiscoItems(component, p.PacketAttrs, inner)
2018-01-15 03:28:34 -08:00
}
2018-01-13 10:27:46 -08:00
default:
2018-01-20 09:56:07 -08:00
fmt.Println("ignoring iq packet", inner)
2018-01-22 14:33:16 -08:00
xError := xmpp.Err{
2018-01-20 09:56:07 -08:00
Code: 501,
Reason: "feature-not-implemented",
Type: "cancel",
}
2018-01-22 14:33:16 -08:00
reply := p.MakeError(xError)
_ = component.xmpp.Send(&reply)
2018-01-13 10:14:26 -08:00
}
2018-02-13 13:07:15 -08:00
2018-01-23 00:08:21 -08:00
case xmpp.Message:
fmt.Println("Received message:", p.Body)
2018-02-13 13:07:15 -08:00
2018-01-23 00:08:21 -08:00
case xmpp.Presence:
fmt.Println("Received presence:", p.Type)
2018-02-13 13:07:15 -08:00
2018-01-13 09:50:17 -08:00
default:
2018-01-20 09:56:07 -08:00
fmt.Println("ignoring packet:", packet)
2018-01-13 09:50:17 -08:00
}
2018-01-12 10:08:47 -08:00
}
2018-01-11 13:15:54 -08:00
}
2018-01-14 07:54:12 -08:00
type MyComponent struct {
Name string
// Typical categories and types: https://xmpp.org/registrar/disco-categories.html
Category string
Type string
xmpp *xmpp.Component
}
2018-01-26 02:40:59 -08:00
func DiscoResult(c MyComponent, 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{
2018-01-20 09:56:07 -08:00
Name: c.Name,
Category: c.Category,
Type: c.Type,
2018-01-26 02:40:59 -08:00
}
}
payload := xmpp.DiscoInfo{
Identity: identity,
2018-01-20 09:56:07 -08:00
Features: []xmpp.Feature{
2019-06-04 23:51:21 -07:00
{Var: xmpp.NSDiscoInfo},
{Var: xmpp.NSDiscoItems},
2018-01-20 09:56:07 -08:00
},
2018-01-14 07:54:12 -08:00
}
2018-01-20 09:56:07 -08:00
iq.AddPayload(&payload)
2018-01-26 02:40:59 -08:00
_ = c.xmpp.Send(iq)
2018-01-26 02:40:59 -08:00
}
func DiscoItems(c MyComponent, attrs xmpp.PacketAttrs, items *xmpp.DiscoItems) {
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 03:37:27 -08:00
{Name: "test node", JID: "service.localhost", Node: "node1"},
2018-01-26 02:40:59 -08:00
},
}
}
iq.AddPayload(&payload)
_ = c.xmpp.Send(iq)
2018-01-14 07:54:12 -08:00
}