summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--xmppbot.go125
-rw-r--r--xmppbot_test.go28
3 files changed, 154 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..523a67f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+xmppbot
diff --git a/xmppbot.go b/xmppbot.go
new file mode 100644
index 0000000..f371ab5
--- /dev/null
+++ b/xmppbot.go
@@ -0,0 +1,125 @@
+package main
+
+import (
+ "fmt"
+ "strings"
+ "time"
+
+ "gopkg.in/inconshreveable/log15.v2"
+
+ "github.com/ThomsonReutersEikon/open-nitro/src/bots"
+ "github.com/ThomsonReutersEikon/open-nitro/src/bots/xmppclient" // Register "xmpp" and "xmpp-bosh" bot schemes
+ "github.com/bjc/goctl"
+)
+
+const sockPath = "/tmp/xmppbot"
+const timeout = 1 * time.Second
+
+var (
+ gc goctl.Goctl
+ xb *xmppclient.Bot
+ stopChan chan bool
+)
+
+func emailFromJID(jid string) string {
+ i := strings.Index(jid, "@")
+ return strings.Replace(jid[:i], ".", "@", 1)
+}
+
+func dialHandler(args []string) string {
+ if xb != nil {
+ return "ERROR: bot is already connected."
+ }
+
+ if len(args) != 3 {
+ return "ERROR: dial requires JID, password, and host[:port]"
+ }
+
+ url := fmt.Sprintf("xmpp-bosh://%s,%s:%s@%s", args[0], emailFromJID(args[0]), args[1], args[2])
+
+ var err error
+ b, err := bots.Dial(url, timeout)
+ if err != nil {
+ return fmt.Sprintf("ERROR: %s.", err)
+ }
+
+ var ok bool
+ xb, ok = b.(*xmppclient.Bot)
+ if !ok {
+ return "ERROR: bot wasn't xmpp client."
+ }
+ return "ok"
+}
+
+func loginHandler(args []string) string {
+ if xb == nil {
+ return "ERROR: bot is not connected."
+ }
+
+ if err := xb.Login(); err != nil {
+ return fmt.Sprintf("ERROR: couldn't login: %s.", err)
+ }
+ return "ok"
+}
+
+func bindHandler(args []string) string {
+ if xb == nil {
+ return "ERROR: bot is not connected."
+ }
+
+ if err := xb.Bind(); err != nil {
+ return fmt.Sprintf("ERROR: couldn't bind resource: %s.", err)
+ }
+ return "ok"
+}
+
+func stopHandler(args []string) string {
+ if xb == nil {
+ return "ERROR: bot is not connected."
+ }
+ xb.Shutdown()
+ xb = nil
+
+ stopChan <- true
+ return "Stopping"
+}
+
+func presenceHandler(args []string) string {
+ if xb == nil {
+ return "ERROR: bot is not connected."
+ }
+ xb.Sendf(`<presence/>`)
+ return "ok"
+}
+
+func pingHandler(args []string) string {
+ if xb == nil {
+ return "ERROR: bot is not connected."
+ }
+
+ xb.Sendf(`<iq type='get' to='%s' id='ping'><ping xmlns='urn:xmpp:ping'/></iq>`, xb.JID().Domain())
+ return "ok"
+}
+
+func main() {
+ stopChan = make(chan bool, 1)
+
+ goctl.Logger.SetHandler(log15.StdoutHandler)
+
+ gc = goctl.NewGoctl(sockPath)
+ gc.AddHandlers([]*goctl.Handler{
+ {"dial", dialHandler},
+ {"login", loginHandler},
+ {"bind", bindHandler},
+ {"stop", stopHandler},
+ {"presence", presenceHandler},
+ {"ping", pingHandler},
+ })
+ if err := gc.Start(); err != nil {
+ log15.Crit("Coudln't start command listener.", "error", err)
+ return
+ }
+ defer gc.Stop()
+
+ <-stopChan
+}
diff --git a/xmppbot_test.go b/xmppbot_test.go
new file mode 100644
index 0000000..fbabd4c
--- /dev/null
+++ b/xmppbot_test.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+ "testing"
+ "time"
+)
+
+const TIMEOUT = 10 * time.Second
+
+const JID = "xmppload0.reuters.com@array12.msgtst.reuters.com"
+const EMAIL_ADDR = "xmppload0@reuters.com"
+const PASSWORD = "Welcome1"
+const HOST = "localhost:5222"
+
+func TestConnect(t *testing.T) {
+ bot := NewBot(JID, PASSWORD, HOST, TIMEOUT)
+ if err := bot.Dial(); err != nil {
+ t.Fatalf("Couldn't connect: %s.", err)
+ }
+
+ if err := bot.Login(); err != nil {
+ t.Fatalf("Couldn't login bot: %s.", err)
+ }
+
+ if err := bot.Bind(); err != nil {
+ t.Fatalf("Couldn't bind to resource: %s.", err)
+ }
+}