xmpp-webhook

webhook to xmpp gateway (clone of https://github.com/opthomas-prime/xmpp-webhook/ with my own mods)
git clone https://git.e1e0.net/xmpp-webhook.git
Log | Files | Refs | README | LICENSE

commit 03534604724932844e41549de2bf4a69906ac7d2
parent 3a1fc29d0cda64e557c21047d5a6220ea3e37f56
Author: Thomas Maier <thomas.maier@leomedia.eu>
Date:   Mon, 25 Sep 2017 13:33:43 +0200

xmpp working

Diffstat:
Mmain.go | 45++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 40 insertions(+), 5 deletions(-)

diff --git a/main.go b/main.go @@ -7,45 +7,80 @@ import ( "github.com/emgee/go-xmpp/src/xmpp" ) +const ( + xmppBotAnswer = "im a dumb bot" +) + +// helper function for error checks func fatalOnErr(err error) { if err != nil { log.Fatal(err) } } +// starts xmpp session, sends initial presence and returns the xmpp client func xmppLogin(id string, pass string) *xmpp.XMPP { + // parse jid structure jid, err := xmpp.ParseJID(id) fatalOnErr(err) + // extract/generate address:port from jid addr, err := xmpp.HomeServerAddrs(jid) fatalOnErr(err) + // create xml stream to address stream, err := xmpp.NewStream(addr[0], nil) fatalOnErr(err) + // create client (login) client, err := xmpp.NewClientXMPP(stream, jid, pass, nil) fatalOnErr(err) + // announce presence client.Out <- xmpp.Presence{} return client } +// creates MessageBody slice suitable for xmpp.Message +func xmppBodyCreate(message string) []xmpp.MessageBody { + return []xmpp.MessageBody{ + xmpp.MessageBody{ + Value: message, + }, + } +} + +// handles incoming stanzas +func handleXMPPStanza(in <-chan interface{}, out chan<- interface{}) { + for stanza := range in { + // check if stanza is a message + message, ok := stanza.(*xmpp.Message) + if ok && len(message.Body) > 0 { + // send constant as answer + out <- xmpp.Message{ + To: message.From, + Body: xmppBodyCreate(xmppBotAnswer), + } + } + } +} + func main() { + // get xmpp credentials from ENV xi := os.Getenv("XMPP_ID") xp := os.Getenv("XMPP_PASS") + // check if xmpp credentials are supplied if len(xi) < 1 || len(xp) < 1 { log.Fatal("XMPP_ID or XMPP_PASS not set") } + // start xmpp client xc := xmppLogin(xi, xp) - go func() { - for msg := range xc.In { - log.Printf("* recv: %v\n", msg) - } - }() + // dispatch incoming stanzas to handler + go handleXMPPStanza(xc.In, xc.Out) select {} }