From 9b4a09677a352d9938e4505bfc2d0c8d304567ac Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Tue, 22 Oct 2019 19:36:54 +0300 Subject: Hello world XMPP echo component --- xmpp/component.go | 30 ++++++++++++++++++++++++++++++ xmpp/handlers.go | 21 +++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 xmpp/component.go create mode 100644 xmpp/handlers.go (limited to 'xmpp') 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) +} -- cgit v1.2.3