forked from jshiffer/go-xmpp
Move examples out of the cmd directory
They are now in _examples dir. Fix #26
This commit is contained in:
20
_examples/xmpp_component/README.md
Normal file
20
_examples/xmpp_component/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# xmpp_component
|
||||
|
||||
This component will connect to ejabberd and act as a subdomain "service" of your primary XMPP domain
|
||||
(in that case localhost).
|
||||
|
||||
To be able to connect this component, you need to add a listener to your XMPP server.
|
||||
|
||||
Here is an example ejabberd configuration for that component listener:
|
||||
|
||||
```yaml
|
||||
listen:
|
||||
...
|
||||
-
|
||||
port: 8888
|
||||
module: ejabberd_service
|
||||
password: "mypass"
|
||||
```
|
||||
|
||||
ejabberd will listen for a component (service) on port 8888 and allows it to connect using the
|
||||
secret "mypass".
|
105
_examples/xmpp_component/xmpp_component.go
Normal file
105
_examples/xmpp_component/xmpp_component.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"gosrc.io/xmpp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
component := MyComponent{Name: "Test Component", Category: "gateway", Type: "service"}
|
||||
component.xmpp = &xmpp.Component{Host: "service.localhost", Secret: "mypass"}
|
||||
if err := component.xmpp.Connect("localhost:8888"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for {
|
||||
packet, err := component.xmpp.ReadPacket()
|
||||
if err != nil {
|
||||
fmt.Println("read error", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch p := packet.(type) {
|
||||
case xmpp.IQ:
|
||||
switch inner := p.Payload[0].(type) {
|
||||
case *xmpp.DiscoInfo:
|
||||
fmt.Println("Disco Info")
|
||||
if p.Type == "get" {
|
||||
DiscoResult(component, p.PacketAttrs, inner)
|
||||
}
|
||||
case *xmpp.DiscoItems:
|
||||
fmt.Println("DiscoItems")
|
||||
if p.Type == "get" {
|
||||
DiscoItems(component, p.PacketAttrs, inner)
|
||||
}
|
||||
default:
|
||||
fmt.Println("ignoring iq packet", inner)
|
||||
xError := xmpp.Err{
|
||||
Code: 501,
|
||||
Reason: "feature-not-implemented",
|
||||
Type: "cancel",
|
||||
}
|
||||
reply := p.MakeError(xError)
|
||||
_ = component.xmpp.Send(&reply)
|
||||
}
|
||||
|
||||
case xmpp.Message:
|
||||
fmt.Println("Received message:", p.Body)
|
||||
|
||||
case xmpp.Presence:
|
||||
fmt.Println("Received presence:", p.Type)
|
||||
|
||||
default:
|
||||
fmt.Println("ignoring packet:", packet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type MyComponent struct {
|
||||
Name string
|
||||
// Typical categories and types: https://xmpp.org/registrar/disco-categories.html
|
||||
Category string
|
||||
Type string
|
||||
|
||||
xmpp *xmpp.Component
|
||||
}
|
||||
|
||||
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{
|
||||
Name: c.Name,
|
||||
Category: c.Category,
|
||||
Type: c.Type,
|
||||
}
|
||||
}
|
||||
|
||||
payload := xmpp.DiscoInfo{
|
||||
Identity: identity,
|
||||
Features: []xmpp.Feature{
|
||||
{Var: xmpp.NSDiscoInfo},
|
||||
{Var: xmpp.NSDiscoItems},
|
||||
},
|
||||
}
|
||||
iq.AddPayload(&payload)
|
||||
|
||||
_ = c.xmpp.Send(iq)
|
||||
}
|
||||
|
||||
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{
|
||||
{Name: "test node", JID: "service.localhost", Node: "node1"},
|
||||
},
|
||||
}
|
||||
}
|
||||
iq.AddPayload(&payload)
|
||||
_ = c.xmpp.Send(iq)
|
||||
}
|
Reference in New Issue
Block a user