4
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2025-06-28 02:09:25 +00:00

Compare commits

...

16 Commits
v0.1 ... v0.2

5 changed files with 146 additions and 22 deletions

View File

@ -5,12 +5,17 @@ Relays public channel messages between mattermost and IRC.
Work in progress. Work in progress.
Requires mattermost build from master (or 1.2.0 when released).
## binaries
Binaries can be found [here] (https://github.com/42wim/matterbridge/releases/tag/v0.1)
## building ## building
Make sure you have [Go](https://golang.org/doc/install) properly installed, including setting up your [GOPATH] (https://golang.org/doc/code.html#GOPATH) Make sure you have [Go](https://golang.org/doc/install) properly installed, including setting up your [GOPATH] (https://golang.org/doc/code.html#GOPATH)
``` ```
cd $GOPATH cd $GOPATH
go get https://github.com/42wim/matterbridge go get github.com/42wim/matterbridge
``` ```
You should now have matterbridge binary in the bin directory: You should now have matterbridge binary in the bin directory:
@ -51,6 +56,15 @@ channel="#matterbridge"
url="http://mattermost.yourdomain.com/hooks/incomingwebhookkey" url="http://mattermost.yourdomain.com/hooks/incomingwebhookkey"
#port the bridge webserver will listen on #port the bridge webserver will listen on
port=9999 port=9999
showjoinpart=true #show irc users joining and parting
#the token you get from the outgoing webhook in mattermost. If empty no token check will be done.
token=yourtokenfrommattermost
#disable certificate checking (selfsigned certificates)
#SkipTLSVerify=true
[general]
#request your API key on https://github.com/giphy/GiphyAPI. This is a public beta key
GiphyApiKey="dc6zaTOxFJmzC"
``` ```
### mattermost ### mattermost

View File

@ -18,6 +18,13 @@ type Config struct {
Mattermost struct { Mattermost struct {
URL string URL string
Port int Port int
ShowJoinPart bool
Token string
IconURL string
SkipTLSVerify bool
}
General struct {
GiphyAPIKey string
} }
} }

View File

@ -9,3 +9,10 @@ channel="#matterbridge"
[mattermost] [mattermost]
url="http://yourdomain/hooks/yourhookkey" url="http://yourdomain/hooks/yourhookkey"
port=9999 port=9999
showjoinpart=true
#token=yourtokenfrommattermost
IconURL="http://youricon.png"
#SkipTLSVerify=true
[general]
GiphyAPIKey=dc6zaTOxFJmzC

View File

@ -3,9 +3,11 @@ package main
import ( import (
"crypto/tls" "crypto/tls"
"github.com/42wim/matterbridge/matterhook" "github.com/42wim/matterbridge/matterhook"
"github.com/peterhellberg/giphy"
"github.com/thoj/go-ircevent" "github.com/thoj/go-ircevent"
"log" "log"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -18,7 +20,9 @@ type Bridge struct {
func NewBridge(name string, config *Config) *Bridge { func NewBridge(name string, config *Config) *Bridge {
b := &Bridge{} b := &Bridge{}
b.Config = config b.Config = config
b.m = matterhook.New(b.Config.Mattermost.URL, matterhook.Config{Port: b.Config.Mattermost.Port}) b.m = matterhook.New(b.Config.Mattermost.URL,
matterhook.Config{Port: b.Config.Mattermost.Port, Token: b.Config.Mattermost.Token,
InsecureSkipVerify: b.Config.Mattermost.SkipTLSVerify})
b.i = b.createIRC(name) b.i = b.createIRC(name)
go b.handleMatter() go b.handleMatter()
return b return b
@ -33,21 +37,81 @@ func (b *Bridge) createIRC(name string) *irc.Connection {
log.Println("Joining", b.Config.IRC.Channel, "as", b.Config.IRC.Nick) log.Println("Joining", b.Config.IRC.Channel, "as", b.Config.IRC.Nick)
i.Join(b.Config.IRC.Channel) i.Join(b.Config.IRC.Channel)
i.AddCallback("PRIVMSG", b.handlePrivMsg) i.AddCallback("PRIVMSG", b.handlePrivMsg)
i.AddCallback("CTCP_ACTION", b.handlePrivMsg)
if b.Config.Mattermost.ShowJoinPart {
i.AddCallback("JOIN", b.handleJoinPart)
i.AddCallback("PART", b.handleJoinPart)
}
i.AddCallback("353", b.handleOther)
return i return i
} }
func (b *Bridge) handlePrivMsg(event *irc.Event) { func (b *Bridge) handlePrivMsg(event *irc.Event) {
matterMessage := matterhook.OMessage{} msg := ""
matterMessage.Text = event.Message() if event.Code == "CTCP_ACTION" {
matterMessage.UserName = "irc-" + event.Nick msg = event.Nick + " "
b.m.Send(matterMessage) }
msg += event.Message()
b.Send("irc-"+event.Nick, msg)
}
func (b *Bridge) handleJoinPart(event *irc.Event) {
b.SendType(b.Config.IRC.Nick, "irc-"+event.Nick+" "+strings.ToLower(event.Code)+"s "+event.Message(), "join_leave")
}
func (b *Bridge) handleOther(event *irc.Event) {
switch event.Code {
case "353":
b.Send(b.Config.IRC.Nick, event.Message()+" currently on IRC")
}
}
func (b *Bridge) Send(nick string, message string) error {
return b.SendType(nick, message, "")
}
func (b *Bridge) SendType(nick string, message string, mtype string) error {
matterMessage := matterhook.OMessage{IconURL: b.Config.Mattermost.IconURL}
matterMessage.UserName = nick
matterMessage.Text = message
matterMessage.Type = mtype
err := b.m.Send(matterMessage)
if err != nil {
log.Println(err)
return err
}
return nil
} }
func (b *Bridge) handleMatter() { func (b *Bridge) handleMatter() {
for { for {
message := b.m.Receive() message := b.m.Receive()
b.i.Privmsg(b.Config.IRC.Channel, message.UserName+": "+message.Text) cmd := strings.Fields(message.Text)[0]
switch cmd {
case "!users":
log.Println("received !users from", message.UserName)
b.i.SendRaw("NAMES " + b.Config.IRC.Channel)
case "!gif":
message.Text = b.giphyRandom(strings.Fields(strings.Replace(message.Text, "!gif ", "", 1)))
b.Send(b.Config.IRC.Nick, message.Text)
} }
texts := strings.Split(message.Text, "\n")
for _, text := range texts {
b.i.Privmsg(b.Config.IRC.Channel, message.UserName+": "+text)
}
}
}
func (b *Bridge) giphyRandom(query []string) string {
g := giphy.DefaultClient
if b.Config.General.GiphyAPIKey != "" {
g.APIKey = b.Config.General.GiphyAPIKey
}
res, err := g.Random(query)
if err != nil {
return "error"
}
return res.Data.FixedHeightDownsampledURL
} }
func main() { func main() {

View File

@ -1,7 +1,9 @@
//Package matterhook provides interaction with mattermost incoming/outgoing webhooks
package matterhook package matterhook
import ( import (
"bytes" "bytes"
"crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gorilla/schema" "github.com/gorilla/schema"
@ -19,6 +21,8 @@ type OMessage struct {
IconEmoji string `json:"icon_emoji,omitempty"` IconEmoji string `json:"icon_emoji,omitempty"`
UserName string `json:"username,omitempty"` UserName string `json:"username,omitempty"`
Text string `json:"text"` Text string `json:"text"`
Attachments interface{} `json:"attachments,omitempty"`
Type string `json:"type,omitempty"`
} }
// IMessage for mattermost outgoing webhook. (received from mattermost) // IMessage for mattermost outgoing webhook. (received from mattermost)
@ -38,23 +42,34 @@ type IMessage struct {
// Client for Mattermost. // Client for Mattermost.
type Client struct { type Client struct {
url string Url string // URL for incoming webhooks on mattermost.
In chan IMessage In chan IMessage
Out chan OMessage Out chan OMessage
httpclient *http.Client
Config Config
} }
// Config for client.
type Config struct { type Config struct {
Port int Port int // Port to listen on.
Token string // Only allow this token from Mattermost. (Allow everything when empty)
InsecureSkipVerify bool // disable certificate checking
DisableServer bool // Do not start server for outgoing webhooks from Mattermost.
} }
// New Mattermost client. // New Mattermost client.
func New(url string, config Config) *Client { func New(url string, config Config) *Client {
c := &Client{url: url, In: make(chan IMessage), Out: make(chan OMessage), Config: config} c := &Client{Url: url, In: make(chan IMessage), Out: make(chan OMessage), Config: config}
if c.Port == 0 { if c.Port == 0 {
c.Port = 9999 c.Port = 9999
} }
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: config.InsecureSkipVerify},
}
c.httpclient = &http.Client{Transport: tr}
if !c.DisableServer {
go c.StartServer() go c.StartServer()
}
return c return c
} }
@ -70,6 +85,11 @@ func (c *Client) StartServer() {
// ServeHTTP implementation. // ServeHTTP implementation.
func (c *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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 := IMessage{} msg := IMessage{}
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
@ -85,6 +105,18 @@ func (c *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
if msg.Token == "" {
log.Println("no token from " + r.RemoteAddr)
http.NotFound(w, r)
return
}
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 c.In <- msg
} }
@ -104,7 +136,7 @@ func (c *Client) Send(msg OMessage) error {
if err != nil { if err != nil {
return err return err
} }
resp, err := http.Post(c.url, "application/json", bytes.NewReader(buf)) resp, err := c.httpclient.Post(c.Url, "application/json", bytes.NewReader(buf))
if err != nil { if err != nil {
return err return err
} }