diff options
author | bodqhrohro <bodqhrohro@gmail.com> | 2019-10-22 19:36:54 +0300 |
---|---|---|
committer | bodqhrohro <bodqhrohro@gmail.com> | 2019-10-22 19:36:54 +0300 |
commit | 9b4a09677a352d9938e4505bfc2d0c8d304567ac (patch) | |
tree | 1863f0d4c58f4019f86910f76c96f5823fd01dd4 /xmpp |
Hello world XMPP echo component
Diffstat (limited to 'xmpp')
-rw-r--r-- | xmpp/component.go | 30 | ||||
-rw-r--r-- | xmpp/handlers.go | 21 |
2 files changed, 51 insertions, 0 deletions
diff --git a/xmpp/component.go b/xmpp/component.go new file mode 100644 index 0000000..e484e99 --- /dev/null +++ b/xmpp/component.go @@ -0,0 +1,30 @@ +package xmpp + +import ( + "log" + + "dev.narayana.im/narayana/telegabber/config" + + "gosrc.io/xmpp" +) + +func NewComponent(conf config.XmppConfig) *xmpp.StreamManager { + options := xmpp.ComponentOptions{ + Address: conf.Host + ":" + conf.Port, + Domain: conf.Jid, + Secret: conf.Password, + Name: "telegabber", + } + + router := xmpp.NewRouter() + router.HandleFunc("message", HandleMessage) + + component, err := xmpp.NewComponent(options, router) + if err != nil { + log.Fatalf("%+v", err) + } + + cm := xmpp.NewStreamManager(component, nil) + + return cm +} diff --git a/xmpp/handlers.go b/xmpp/handlers.go new file mode 100644 index 0000000..38bbe7a --- /dev/null +++ b/xmpp/handlers.go @@ -0,0 +1,21 @@ +package xmpp + +import ( + "fmt" + "os" + + "gosrc.io/xmpp" + "gosrc.io/xmpp/stanza" +) + +func HandleMessage(s xmpp.Sender, p stanza.Packet) { + msg, ok := p.(stanza.Message) + if !ok { + _, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p) + return + } + + _, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From) + reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body} + _ = s.Send(reply) +} |