Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit ebf8aef438886cbdc826f848fefcd392d14d8535 Author: Wim <wim@42.be> Date: Mon Feb 20 00:49:27 2017 +0000 Add matrix support diff --git a/README.md b/README.md index 2905512..afaab06 100644 --- a/README.md +++ b/README.md @@ -19 +19 @@  # matterbridge  ![matterbridge.gif](https://s15.postimg.org/qpjhp6y3f/matterbridge.gif)   -Simple bridge between mattermost, IRC, XMPP, Gitter, Slack, Discord, Telegram, Rocket.Chat and Hipchat(via xmpp) with REST API. +Simple bridge between mattermost, IRC, XMPP, Gitter, Slack, Discord, Telegram, Rocket.Chat, Hipchat(via xmpp) and Matrix with REST API.   -* Relays public channel messages between multiple mattermost, IRC, XMPP, Gitter, Slack, Discord, Telegram, Rocket.Chat and Hipchat (via xmpp). Pick and mix. +* Relays public channel messages between multiple mattermost, IRC, XMPP, Gitter, Slack, Discord, Telegram, Rocket.Chat, Hipchat (via xmpp) and Matrix. Pick and mix.  * Supports multiple channels.  * Matterbridge can also work with private groups on your mattermost.  * Allow for bridging the same bridges, which means you can eg bridge between multiple mattermosts. @@ -286 +287 @@ Accounts to one of the supported bridges  * [Telegram] (https://telegram.org)  * [Hipchat] (https://www.hipchat.com)  * [Rocket.chat] (https://rocket.chat) +* [Matrix] (https://matrix.org)    ## Docker  Create your matterbridge.toml file locally eg in ```/tmp/matterbridge.toml``` diff --git a/bridge/bridge.go b/bridge/bridge.go index 012b0ef..312792b 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -66 +67 @@ import (   "github.com/42wim/matterbridge/bridge/discord"   "github.com/42wim/matterbridge/bridge/gitter"   "github.com/42wim/matterbridge/bridge/irc" + "github.com/42wim/matterbridge/bridge/matrix"   "github.com/42wim/matterbridge/bridge/mattermost"   "github.com/42wim/matterbridge/bridge/rocketchat"   "github.com/42wim/matterbridge/bridge/slack" @@ -716 +729 @@ func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Brid   case "rocketchat":   b.Config = cfg.Rocketchat[name]   b.Bridger = brocketchat.New(cfg.Rocketchat[name], bridge.Account, c) + case "matrix": + b.Config = cfg.Matrix[name] + b.Bridger = bmatrix.New(cfg.Matrix[name], bridge.Account, c)   case "api":   b.Config = cfg.Api[name]   b.Bridger = api.New(cfg.Api[name], bridge.Account, c) diff --git a/bridge/config/config.go b/bridge/config/config.go index 4f6568a..5077abb 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -866 +867 @@ type Config struct {   Api map[string]Protocol   IRC map[string]Protocol   Mattermost map[string]Protocol + Matrix map[string]Protocol   Slack map[string]Protocol   Gitter map[string]Protocol   Xmpp map[string]Protocol diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go new file mode 100644 index 0000000..222a8fd --- /dev/null +++ b/bridge/matrix/matrix.go @@ -00 +192 @@ +package bmatrix + +import ( + "github.com/42wim/matterbridge/bridge/config" + log "github.com/Sirupsen/logrus" + matrix "github.com/matrix-org/gomatrix" +) + +type Bmatrix struct { + mc *matrix.Client + Config *config.Protocol + Remote chan config.Message + Account string + UserID string +} + +var flog *log.Entry +var protocol = "matrix" + +func init() { + flog = log.WithFields(log.Fields{"module": protocol}) +} + +func New(cfg config.Protocol, account string, c chan config.Message) *Bmatrix { + b := &Bmatrix{} + b.Config = &cfg + b.Account = account + b.Remote = c + return b +} + +func (b *Bmatrix) Connect() error { + var err error + flog.Infof("Connecting %s", b.Config.Server) + b.mc, err = matrix.NewClient(b.Config.Server, "", "") + if err != nil { + flog.Debugf("%#v", err) + return err + } + resp, err := b.mc.Login(&matrix.ReqLogin{ + Type: "m.login.password", + User: b.Config.Login, + Password: b.Config.Password, + }) + if err != nil { + flog.Debugf("%#v", err) + return err + } + b.mc.SetCredentials(resp.UserID, resp.AccessToken) + b.UserID = resp.UserID + flog.Info("Connection succeeded") + go b.handlematrix() + return nil +} + +func (b *Bmatrix) Disconnect() error { + return nil +} + +func (b *Bmatrix) JoinChannel(channel string) error { + _, err := b.mc.JoinRoom(channel, "", nil) + return err +} + +func (b *Bmatrix) Send(msg config.Message) error { + flog.Debugf("Receiving %#v", msg) + b.mc.SendText(msg.Channel, msg.Username+msg.Text) + return nil +} + +func (b *Bmatrix) handlematrix() error { + warning := "Not relaying this message, please setup a dedicated bot user" + syncer := b.mc.Syncer.(*matrix.DefaultSyncer) + syncer.OnEventType("m.room.message", func(ev *matrix.Event) { + if ev.Content["msgtype"].(string) == "m.text" && ev.Sender != b.UserID { + flog.Debugf("Sending message from %s on %s to gateway", ev.Sender, b.Account) + b.Remote <- config.Message{Username: ev.Sender, Text: ev.Content["body"].(string), Channel: ev.RoomID, Account: b.Account} + } + if ev.Sender == b.UserID && ev.Content["body"].(string) != warning { + b.mc.SendText(ev.RoomID, warning) + } + flog.Debugf("Received: %#v", ev) + }) + go func() { + for { + if err := b.mc.Sync(); err != nil { + flog.Println("Sync() returned ", err) + } + } + }() + return nil +} diff --git a/changelog.md b/changelog.md index 2431503..20ae140 100644 --- a/changelog.md +++ b/changelog.md @@ -13 +17 @@ +# v0.10.0-dev +## New features +* matrix: New protocol support added (https://matrix.org) +  # v0.9.3  ## New features  * API: rest interface to read / post messages (see API section in matterbridge.toml.sample) diff --git a/matterbridge.go b/matterbridge.go index fe2bcce..311a5ce 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -107 +107 @@ import (  )    var ( - version = "0.9.3" + version = "0.10.0-dev"   githash string  )   diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 0fa95d0..f0901ce 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -4866 +48650 @@ RemoteNickFormat="[{PROTOCOL}] <{NICK}> "  #OPTIONAL (default false)  ShowJoinPart=false   +################################################################### +#matrix section +################################################################### +[matrix] +#You can configure multiple servers "[matrix.name]" or "[matrix.name2]" +#In this example we use [matrix.neo] +#REQUIRED + +[matrix.neo] +#Url is your homeserver (eg matrix.org) +#REQUIRED +URL="https://matrix.org" + +#login/pass of your bot. +#Use a dedicated user for this and not your own! +#Messages sent from this user will not be relayed to avoid loops. +#REQUIRED +Login="yourlogin" +Password="yourpass" + +#Whether to prefix messages from other bridges to matrix with the sender's nick. +#Useful if username overrides for incoming webhooks isn't enabled on the +#matrix server. If you set PrefixMessagesWithNick to true, each message +#from bridge to matrix will by default be prefixed by the RemoteNickFormat setting. i +#OPTIONAL (default false) +PrefixMessagesWithNick=false + +#Nicks you want to ignore. +#Messages from those users will not be sent to other bridges. +#OPTIONAL +IgnoreNicks="spammer1 spammer2" + +#RemoteNickFormat defines how remote users appear on this bridge +#The string "{NICK}" (case sensitive) will be replaced by the actual nick / username. +#The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge +#The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge +#OPTIONAL (default empty) +RemoteNickFormat="[{PROTOCOL}] <{NICK}> " + +#Enable to show users joins/parts from other bridges (only from irc-bridge at the moment) +#OPTIONAL (default false) +ShowJoinPart=false + +  ###################################################################  #API  ################################################################### @@ -5646 +6087 @@ enable=true # see (https://www.linkedin.com/pulse/telegram-bots-beginners-marco-frau) #hipchat - id_channel (see https://www.hipchat.com/account/xmpp for the correct channel) #rocketchat - #channel (# is required) + #matrix - room internal ID (looks like !QJFqjsGJwmQzbuBfff:matrix.org) #REQUIRED channel="#testing"