2017-12-03 00:29:25 +00:00
|
|
|
package bsshchat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-06-09 10:47:40 +00:00
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2018-02-26 23:33:21 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2017-12-03 00:29:25 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-02-03 00:11:11 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2017-12-03 00:29:25 +00:00
|
|
|
"github.com/shazow/ssh-chat/sshd"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bsshchat struct {
|
2017-12-19 22:15:03 +00:00
|
|
|
r *bufio.Scanner
|
|
|
|
w io.WriteCloser
|
2018-03-04 22:52:14 +00:00
|
|
|
*bridge.Config
|
2017-12-03 00:29:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
|
|
|
return &Bsshchat{Config: cfg}
|
2017-12-03 00:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bsshchat) Connect() error {
|
2018-03-04 22:52:14 +00:00
|
|
|
b.Log.Infof("Connecting %s", b.GetString("Server"))
|
2018-12-26 14:09:36 +00:00
|
|
|
|
|
|
|
// connHandler will be called by 'sshd.ConnectShell()' below
|
|
|
|
// once the connection is established in order to handle it.
|
|
|
|
connErr := make(chan error, 1) // Needs to be buffered.
|
|
|
|
connSignal := make(chan struct{})
|
|
|
|
connHandler := func(r io.Reader, w io.WriteCloser) error {
|
|
|
|
b.r = bufio.NewScanner(r)
|
|
|
|
b.r.Scan()
|
|
|
|
b.w = w
|
2019-01-05 14:42:36 +00:00
|
|
|
if _, err := b.w.Write([]byte("/theme mono\r\n/quiet\r\n")); err != nil {
|
2018-12-26 14:09:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
close(connSignal) // Connection is established so we can signal the success.
|
|
|
|
return b.handleSSHChat()
|
|
|
|
}
|
|
|
|
|
2017-12-03 00:29:25 +00:00
|
|
|
go func() {
|
2018-12-26 14:09:36 +00:00
|
|
|
// As a successful connection will result in this returning after the Connection
|
|
|
|
// method has already returned point we NEED to have a buffered channel to still
|
|
|
|
// be able to write.
|
|
|
|
connErr <- sshd.ConnectShell(b.GetString("Server"), b.GetString("Nick"), connHandler)
|
2017-12-03 00:29:25 +00:00
|
|
|
}()
|
2018-12-26 14:09:36 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-connErr:
|
|
|
|
b.Log.Error("Connection failed")
|
2017-12-03 00:29:25 +00:00
|
|
|
return err
|
2018-12-26 14:09:36 +00:00
|
|
|
case <-connSignal:
|
2017-12-03 00:29:25 +00:00
|
|
|
}
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connection succeeded")
|
2017-12-03 00:29:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bsshchat) Disconnect() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bsshchat) JoinChannel(channel config.ChannelInfo) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bsshchat) Send(msg config.Message) (string, error) {
|
|
|
|
// ignore delete messages
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventMsgDelete {
|
2017-12-03 00:29:25 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("=> Receiving %#v", msg)
|
2017-12-03 00:29:25 +00:00
|
|
|
if msg.Extra != nil {
|
2018-02-03 00:11:11 +00:00
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
2018-12-08 16:04:10 +00:00
|
|
|
if _, err := b.w.Write([]byte(rmsg.Username + rmsg.Text + "\r\n")); err != nil {
|
|
|
|
b.Log.Errorf("Could not send extra message: %#v", err)
|
|
|
|
}
|
2018-02-03 00:11:11 +00:00
|
|
|
}
|
2018-12-26 14:09:36 +00:00
|
|
|
if len(msg.Extra["file"]) > 0 {
|
|
|
|
return b.handleUploadFile(&msg)
|
|
|
|
}
|
2017-12-03 00:29:25 +00:00
|
|
|
}
|
2018-12-08 16:04:10 +00:00
|
|
|
_, err := b.w.Write([]byte(msg.Username + msg.Text + "\r\n"))
|
|
|
|
return "", err
|
2017-12-03 00:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
func (b *Bsshchat) sshchatKeepAlive() chan bool {
|
|
|
|
done := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(90 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("PING")
|
2017-12-03 00:29:25 +00:00
|
|
|
err := b.xc.PingC2S("", "")
|
|
|
|
if err != nil {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("PING failed %#v", err)
|
2017-12-03 00:29:25 +00:00
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return done
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
func stripPrompt(s string) string {
|
|
|
|
pos := strings.LastIndex(s, "\033[K")
|
|
|
|
if pos < 0 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s[pos+3:]
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:43:43 +00:00
|
|
|
func (b *Bsshchat) handleSSHChat() error {
|
2017-12-03 00:29:25 +00:00
|
|
|
/*
|
|
|
|
done := b.sshchatKeepAlive()
|
|
|
|
defer close(done)
|
|
|
|
*/
|
|
|
|
wait := true
|
|
|
|
for {
|
|
|
|
if b.r.Scan() {
|
2018-06-05 22:51:42 +00:00
|
|
|
// ignore messages from ourselves
|
|
|
|
if !strings.Contains(b.r.Text(), "\033[K") {
|
|
|
|
continue
|
|
|
|
}
|
2019-01-05 14:42:36 +00:00
|
|
|
if strings.Contains(b.r.Text(), "Rate limiting is in effect") {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-02 21:08:37 +00:00
|
|
|
// skip our own messages
|
|
|
|
if !strings.HasPrefix(b.r.Text(), "["+b.GetString("Nick")+"] \x1b") {
|
|
|
|
continue
|
|
|
|
}
|
2017-12-03 00:29:25 +00:00
|
|
|
res := strings.Split(stripPrompt(b.r.Text()), ":")
|
|
|
|
if res[0] == "-> Set theme" {
|
|
|
|
wait = false
|
2019-02-23 21:51:27 +00:00
|
|
|
b.Log.Debugf("mono found, allowing")
|
2017-12-03 00:29:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !wait {
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("<= Message %#v", res)
|
2019-01-05 14:42:36 +00:00
|
|
|
rmsg := config.Message{Username: res[0], Text: strings.TrimSpace(strings.Join(res[1:], ":")), Channel: "sshchat", Account: b.Account, UserID: "nick"}
|
2017-12-03 00:29:25 +00:00
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 22:47:07 +00:00
|
|
|
|
|
|
|
func (b *Bsshchat) handleUploadFile(msg *config.Message) (string, error) {
|
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
|
|
|
if fi.Comment != "" {
|
|
|
|
msg.Text += fi.Comment + ": "
|
|
|
|
}
|
|
|
|
if fi.URL != "" {
|
|
|
|
msg.Text = fi.URL
|
|
|
|
if fi.Comment != "" {
|
|
|
|
msg.Text = fi.Comment + ": " + fi.URL
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 14:42:36 +00:00
|
|
|
if _, err := b.w.Write([]byte(msg.Username + msg.Text + "\r\n")); err != nil {
|
2018-12-12 22:47:07 +00:00
|
|
|
b.Log.Errorf("Could not send file message: %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|