Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ completes. There are no runtime dependencies or configuration needed.
Pull requests welcome. Keep it simple.

## changelog
* 12-Aug-2019: 0.2.1 hipchat support removed and fix ssh connection! [HostKeyCallback non-permissive by default](https://github.com/golang/go/issues/19767)
* 4-Sep-2015: 0.2 - Slack support added
* 11-Aug-2015: 0.1 - first public release
71 changes: 6 additions & 65 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,14 @@ import (
"os/user"
"path/filepath"
"strings"
"time"

"github.com/daneharrigan/hipchat"
"github.com/nlopes/slack"
)

const (
VERSION = "0.2"
VERSION = "0.2.1"
)

var sshUsername, idRsaPath string
var hcFlag = flag.Bool("h", false, "create HipChat bot")
var slackFlag = flag.Bool("s", false, "create Slack bot")
var currentUser *user.User

Expand All @@ -71,10 +67,9 @@ func main() {

flag.Parse()

if (!*hcFlag && !*slackFlag) || (*hcFlag && *slackFlag) ||
(*hcFlag && len(os.Args) != 4) || (*slackFlag && len(os.Args) != 3) {
usage()
}
if (*slackFlag && len(os.Args) != 3) {
usage()
}

log.SetPrefix("rtop-bot: ")
log.SetFlags(0)
Expand All @@ -99,11 +94,8 @@ func main() {
parseSshConfig(sshConfig)
}

if *hcFlag {
doHipChat(os.Args[2], os.Args[3])
} else {
doSlack(os.Args[2])
}
doSlack(os.Args[2])

}

func doSlack(apiToken string) {
Expand Down Expand Up @@ -137,57 +129,6 @@ func doSlack(apiToken string) {
}
}

func doHipChat(username, roomjid string) {
if strings.HasSuffix(username, "@chat.hipchat.com") {
username = strings.Replace(username, "@chat.hipchat.com", "", 1)
}
if !strings.HasSuffix(roomjid, "@conf.hipchat.com") {
roomjid += "@conf.hipchat.com"
}
pass, err := getpass("Password for user \"" + username + "\": ")
if err != nil {
log.Print(err)
}

client, err := hipchat.NewClient(username, pass, "bot")
if err != nil {
log.Print(err)
os.Exit(1)
}

nick, mname := getUserInfo(client, username)

client.Status("chat")
client.Join(roomjid, nick)
log.Printf("[%s] now serving room [%s]", nick, roomjid)
log.Print("hit ^C to exit")

go client.KeepAlive()
for message := range client.Messages() {
if strings.HasPrefix(message.Body, "@"+mname) {
go client.Say(roomjid, nick, process(message.Body))
}
}
}

func getUserInfo(client *hipchat.Client, id string) (string, string) {
id = id + "@chat.hipchat.com"
client.RequestUsers()
select {
case users := <-client.Users():
for _, user := range users {
if user.Id == id {
log.Printf("using username [%s] and mention name [%s]",
user.Name, user.MentionName)
return user.Name, user.MentionName
}
}
case <-time.After(10 * time.Second):
log.Print("timed out waiting for user list")
os.Exit(1)
}
return "rtop-bot", "rtop-bot"
}

func process(request string) string {

Expand Down
2 changes: 2 additions & 0 deletions sshhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func tryAgentConnect(user, addr string) (client *ssh.Client) {
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{auth},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
client, _ = ssh.Dial("tcp", addr, config)
}
Expand All @@ -177,6 +178,7 @@ func sshConnect(user, addr, keypath string) (client *ssh.Client, err error) {
config := &ssh.ClientConfig{
User: user,
Auth: auths,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
client, err = ssh.Dial("tcp", addr, config)
if err != nil {
Expand Down