Add initial Rocket.Chat support
parent
d81e6bf6ce
commit
fee159541f
@ -0,0 +1,82 @@
|
||||
package brocketchat
|
||||
|
||||
import (
|
||||
"github.com/42wim/matterbridge/bridge/config"
|
||||
"github.com/42wim/matterbridge/hook/rockethook"
|
||||
"github.com/42wim/matterbridge/matterhook"
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
type MMhook struct {
|
||||
mh *matterhook.Client
|
||||
rh *rockethook.Client
|
||||
}
|
||||
|
||||
type Brocketchat struct {
|
||||
MMhook
|
||||
Config *config.Protocol
|
||||
Remote chan config.Message
|
||||
name string
|
||||
Account string
|
||||
}
|
||||
|
||||
var flog *log.Entry
|
||||
var protocol = "rocketchat"
|
||||
|
||||
func init() {
|
||||
flog = log.WithFields(log.Fields{"module": protocol})
|
||||
}
|
||||
|
||||
func New(cfg config.Protocol, account string, c chan config.Message) *Brocketchat {
|
||||
b := &Brocketchat{}
|
||||
b.Config = &cfg
|
||||
b.Remote = c
|
||||
b.Account = account
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Brocketchat) Command(cmd string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (b *Brocketchat) Connect() error {
|
||||
flog.Info("Connecting webhooks")
|
||||
b.mh = matterhook.New(b.Config.URL,
|
||||
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
||||
DisableServer: true})
|
||||
b.rh = rockethook.New(b.Config.URL, rockethook.Config{BindAddress: b.Config.BindAddress})
|
||||
go b.handleRocketHook()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Brocketchat) JoinChannel(channel string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Brocketchat) Send(msg config.Message) error {
|
||||
flog.Debugf("Receiving %#v", msg)
|
||||
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
|
||||
matterMessage.Channel = msg.Channel
|
||||
matterMessage.UserName = msg.Username
|
||||
matterMessage.Type = ""
|
||||
matterMessage.Text = msg.Text
|
||||
err := b.mh.Send(matterMessage)
|
||||
if err != nil {
|
||||
flog.Info(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Brocketchat) handleRocketHook() {
|
||||
for {
|
||||
message := b.rh.Receive()
|
||||
flog.Debugf("Receiving from rockethook %#v", message)
|
||||
// do not loop
|
||||
if message.UserName == b.Config.Nick {
|
||||
continue
|
||||
}
|
||||
flog.Debugf("Sending message from %s on %s to gateway", message.UserName, b.Account)
|
||||
b.Remote <- config.Message{Text: message.Text, Username: message.UserName, Channel: message.ChannelName, Account: b.Account}
|
||||
}
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
package rockethook
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Message for rocketchat outgoing webhook.
|
||||
type Message struct {
|
||||
Token string `json:"token"`
|
||||
ChannelID string `json:"channel_id"`
|
||||
ChannelName string `json:"channel_name"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
UserID string `json:"user_id"`
|
||||
UserName string `json:"user_name"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
// Client for Rocketchat.
|
||||
type Client struct {
|
||||
In chan Message
|
||||
httpclient *http.Client
|
||||
Config
|
||||
}
|
||||
|
||||
// Config for client.
|
||||
type Config struct {
|
||||
BindAddress string // Address to listen on
|
||||
Token string // Only allow this token from Rocketchat. (Allow everything when empty)
|
||||
InsecureSkipVerify bool // disable certificate checking
|
||||
}
|
||||
|
||||
// New Rocketchat client.
|
||||
func New(url string, config Config) *Client {
|
||||
c := &Client{In: make(chan Message), Config: config}
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: config.InsecureSkipVerify},
|
||||
}
|
||||
c.httpclient = &http.Client{Transport: tr}
|
||||
_, _, err := net.SplitHostPort(c.BindAddress)
|
||||
if err != nil {
|
||||
log.Fatalf("incorrect bindaddress %s", c.BindAddress)
|
||||
}
|
||||
go c.StartServer()
|
||||
return c
|
||||
}
|
||||
|
||||
// StartServer starts a webserver listening for incoming mattermost POSTS.
|
||||
func (c *Client) StartServer() {
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/", c)
|
||||
log.Printf("Listening on http://%v...\n", c.BindAddress)
|
||||
if err := http.ListenAndServe(c.BindAddress, mux); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// ServeHTTP implementation.
|
||||
func (c *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
log.Println("invalid " + r.Method + " connection from " + r.RemoteAddr)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
msg := Message{}
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
log.Println(string(body))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
err = json.Unmarshal(body, &msg)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if msg.Token == "" {
|
||||
log.Println("no token from " + r.RemoteAddr)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
msg.ChannelName = "#" + msg.ChannelName
|
||||
if c.Token != "" {
|
||||
if msg.Token != c.Token {
|
||||
log.Println("invalid token " + msg.Token + " from " + r.RemoteAddr)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
c.In <- msg
|
||||
}
|
||||
|
||||
// Receive returns an incoming message from mattermost outgoing webhooks URL.
|
||||
func (c *Client) Receive() Message {
|
||||
for {
|
||||
select {
|
||||
case msg := <-c.In:
|
||||
return msg
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue