summaryrefslogtreecommitdiffstats
path: root/handlers/dial.go
blob: aa5fbd45ed5e8c14aa1d8ed78fc07d33ab751bd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package handlers

import (
	"fmt"
	"strings"
	"time"

	"github.com/ThomsonReutersEikon/nitro/src/bots"
	_ "github.com/ThomsonReutersEikon/nitro/src/bots/xmppclient" // Register "xmpp" and "xmpp-bosh" bot schemes
	_ "github.com/ThomsonReutersEikon/nitro/src/sipbot"          // "sip" scheme
	"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)
}