diff options
author | Brian Cully <bjc@kublai.com> | 2015-07-10 20:50:39 +0000 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2015-07-10 20:50:39 +0000 |
commit | c8c4c1a9986ebafa48b66830a4f6f20f9529ff03 (patch) | |
tree | cf651e5233daa50e681dc58193d366f9691a9769 /handlers | |
parent | 204f830396f84f2583d081221b5e253bed85bcf1 (diff) | |
download | xmppbot-c8c4c1a9986ebafa48b66830a4f6f20f9529ff03.tar.gz xmppbot-c8c4c1a9986ebafa48b66830a4f6f20f9529ff03.zip |
Update for new goctl API.
Move handlers to their own package.
Diffstat (limited to 'handlers')
-rw-r--r-- | handlers/bind.go | 28 | ||||
-rw-r--r-- | handlers/dial.go | 50 | ||||
-rw-r--r-- | handlers/handlers.go | 1 | ||||
-rw-r--r-- | handlers/login.go | 28 | ||||
-rw-r--r-- | handlers/presence.go | 28 | ||||
-rw-r--r-- | handlers/raw.go | 32 | ||||
-rw-r--r-- | handlers/stop.go | 25 |
7 files changed, 192 insertions, 0 deletions
diff --git a/handlers/bind.go b/handlers/bind.go new file mode 100644 index 0000000..cdacf06 --- /dev/null +++ b/handlers/bind.go @@ -0,0 +1,28 @@ +package handlers + +import ( + "fmt" + + "github.com/bjc/goctl" +) + +type Bind struct{} + +func (bh Bind) Name() string { + return "bind" +} + +func (bh Bind) Help() string { + return "binds XMPP resource" +} + +func (bh Bind) Run(_ *goctl.Goctl, 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" +} diff --git a/handlers/dial.go b/handlers/dial.go new file mode 100644 index 0000000..408f748 --- /dev/null +++ b/handlers/dial.go @@ -0,0 +1,50 @@ +package handlers + +import ( + "fmt" + "strings" + "time" + + _ "github.com/ThomsonReutersEikon/nitro/src/sipbot" // "sip" scheme + "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 timeout = 1 * time.Second + +var xb bots.Bot + +type Dial struct{} + +func (dh Dial) Name() string { + return "dial" +} + +func (dh Dial) Help() string { + return "scheme jid password host[:port]" +} + +func (dh Dial) Run(_ *goctl.Goctl, args []string) string { + if xb != nil { + return "ERROR: bot is already connected." + } + + if len(args) != 4 { + return "ERROR: dial requires scheme, JID, password, and host[:port]" + } + + url := fmt.Sprintf("%s://%s,%s:%s@%s", args[0], args[1], emailFromJID(args[1]), args[2], args[3]) + + var err error + xb, err = bots.Dial(url, timeout) + if err != nil { + return fmt.Sprintf("ERROR: %s.", err) + } + return "ok" +} + +func emailFromJID(jid string) string { + i := strings.Index(jid, "@") + return strings.Replace(jid[:i], ".", "@", 1) +} diff --git a/handlers/handlers.go b/handlers/handlers.go new file mode 100644 index 0000000..5ac8282 --- /dev/null +++ b/handlers/handlers.go @@ -0,0 +1 @@ +package handlers diff --git a/handlers/login.go b/handlers/login.go new file mode 100644 index 0000000..b516c08 --- /dev/null +++ b/handlers/login.go @@ -0,0 +1,28 @@ +package handlers + +import ( + "fmt" + + "github.com/bjc/goctl" +) + +type Login struct{} + +func (lh Login) Name() string { + return "login" +} + +func (lh Login) Help() string { + return "authenticates to server" +} + +func (lh Login) Run(_ *goctl.Goctl, 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" +} diff --git a/handlers/presence.go b/handlers/presence.go new file mode 100644 index 0000000..aae28d0 --- /dev/null +++ b/handlers/presence.go @@ -0,0 +1,28 @@ +package handlers + +import ( + "fmt" + + "github.com/bjc/goctl" +) + +type Presence struct{} + +func (ph Presence) Name() string { + return "presence" +} + +func (ph Presence) Help() string { + return "sends available presence broadcast" +} + +func (ph Presence) Run(_ *goctl.Goctl, args []string) string { + if xb == nil { + return "ERROR: bot is not connected." + } + + if err := xb.Online(); err != nil { + return fmt.Sprintf("ERROR: sending available presence: %s", err) + } + return "ok" +} diff --git a/handlers/raw.go b/handlers/raw.go new file mode 100644 index 0000000..3a93947 --- /dev/null +++ b/handlers/raw.go @@ -0,0 +1,32 @@ +package handlers + +import ( + "fmt" + + "github.com/ThomsonReutersEikon/open-nitro/src/bots/xmppclient" + "github.com/bjc/goctl" +) + +type Raw struct{} + +func (rh Raw) Name() string { + return "raw" +} + +func (rh Raw) Help() string { + return "send raw data" +} + +func (rh Raw) Run(_ *goctl.Goctl, args []string) string { + if xb == nil { + return "ERROR: bot is not connected." + } + + if b, ok := xb.(*xmppclient.Bot); !ok { + return "ERROR: can only send raw data on XMPP bot." + } else if err := b.Sendf(args[0]); err != nil { + return fmt.Sprintf("ERROR: sending raw data: %s", err) + } + + return "ok" +} diff --git a/handlers/stop.go b/handlers/stop.go new file mode 100644 index 0000000..8c04327 --- /dev/null +++ b/handlers/stop.go @@ -0,0 +1,25 @@ +package handlers + +import "github.com/bjc/goctl" + +type Stop struct { + C chan bool +} + +func (sh Stop) Name() string { + return "stop" +} + +func (sh Stop) Help() string { + return "stops this bot" +} + +func (sh Stop) Run(_ *goctl.Goctl, args []string) string { + if xb != nil { + xb.Shutdown() + xb = nil + } + + sh.C <- true + return "Stopping" +} |