go-xmpp/cmd/xmpp_component/xmpp_component.go

82 lines
1.8 KiB
Go
Raw Normal View History

2018-01-11 21:15:54 +00:00
package main
2018-01-12 18:08:47 +00:00
import (
"fmt"
"fluux.io/xmpp"
)
2018-01-11 21:15:54 +00:00
const (
localUser = "admin@localhost"
)
// TODO add webserver listener to support receiving message from facebook and replying
// Message will get to define localhost user and be routed only from local user
2018-01-11 21:15:54 +00:00
func main() {
2018-01-22 22:33:16 +00:00
component := MyComponent{Name: "Facebook Gateway", Category: "gateway", Type: "facebook"}
component.xmpp = &xmpp.Component{Host: "facebook.localhost", Secret: "mypass"}
2018-01-14 15:54:12 +00:00
component.xmpp.Connect("localhost:8888")
2018-01-12 18:08:47 +00:00
for {
2018-01-14 15:54:12 +00:00
packet, err := component.xmpp.ReadPacket()
2018-01-12 18:08:47 +00:00
if err != nil {
2018-01-13 16:46:10 +00:00
fmt.Println("read error", err)
2018-01-12 18:08:47 +00:00
return
}
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) {
2018-01-20 17:56:07 +00:00
case *xmpp.DiscoInfo:
fmt.Println("Disco Info")
if p.Type == "get" {
DiscoResult(component, p.From, p.To, p.Id)
2018-01-15 11:28:34 +00:00
}
2018-01-20 17:56:07 +00:00
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)
2018-01-20 17:56:07 +00:00
component.xmpp.Send(&reply)
2018-01-13 18:14:26 +00:00
}
2018-01-23 08:08:21 +00:00
case xmpp.Message:
fmt.Println("Received message:", p.Body)
case xmpp.Presence:
fmt.Println("Received presence:", p.Type)
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
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-20 17:56:07 +00:00
func DiscoResult(c MyComponent, from, to, id string) {
iq := xmpp.NewIQ("result", to, from, id, "en")
payload := xmpp.DiscoInfo{
Identity: xmpp.Identity{
Name: c.Name,
Category: c.Category,
Type: c.Type,
},
Features: []xmpp.Feature{
{Var: "http://jabber.org/protocol/disco#info"},
{Var: "http://jabber.org/protocol/disco#item"},
},
2018-01-14 15:54:12 +00:00
}
2018-01-20 17:56:07 +00:00
iq.AddPayload(&payload)
c.xmpp.Send(iq)
2018-01-14 15:54:12 +00:00
}