Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
42wim committed Nov 11, 2018
1 parent 58744c1 commit 087d235
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.PHONY: all build plugin
config=matterbridge.toml
all: build plugin

plugin:
ifeq ("$(wildcard $(config))","")
$(error you need to create a matterbridge.toml file, look at matterbridge.toml.sample and README.md)
endif
tar zcf plugin.tar.gz plugin.exe plugin.yaml
@echo " "
@echo "finished, upload plugin.tar.gz to mattermost"
@ls -al plugin.tar.gz

build:
@echo "building plugin.exe"
@echo "-------------------"
CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags '-s' -o plugin.exe

clean:
rm plugin.tar.gz plugin.exe
92 changes: 92 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"
"time"

"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/gateway"
"github.com/42wim/mattermost-server/model"
"github.com/42wim/mattermost-server/plugin"
)

type Plugin struct {
plugin.MattermostPlugin
r *gateway.Router
userid string
teamid string
channels []string
}

func (p *Plugin) OnActivate() error {
fmt.Println("reading config")
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
cfg := config.NewConfig(exPath + "/" + "matterbridge.toml")
fmt.Println("matterbridge config read")
r, err := gateway.NewRouter(cfg)
if err != nil {
log.Fatalf("Starting gateway failed: %s", err)
}
p.r = r
go func() {
err = r.Start()
if err != nil {
log.Fatalf("Starting gateway failed: %s", err)
}
fmt.Printf("Gateway(s) started succesfully. Now relaying messages")
select {}
}()
go func() {
// wait until activation is done, otherwise API doesn't seem to work?
time.Sleep(time.Second)
user, err := p.API.GetUserByUsername(cfg.Mattermost["plugin"].Login)
if err != nil {
fmt.Println("username", err.Error())
}
p.userid = user.Id
fmt.Println("found userid", p.userid)
team, err := p.API.GetTeamByName(cfg.Mattermost["plugin"].Team)
if err != nil {
fmt.Println("team", err.Error())
}
p.teamid = team.Id
fmt.Println("found teamid", p.teamid)
}()
go func() {
for msg := range p.r.MattermostPlugin {
fmt.Printf("Got message %#v\n", msg)
channel, _ := p.API.GetChannelByName(p.teamid, msg.Channel, false)
props := make(map[string]interface{})
props["matterbridge_"+p.userid] = true
post := &model.Post{UserId: p.userid, ChannelId: channel.Id, Message: msg.Username + msg.Text, Props: props}
fmt.Printf("Posting %#v\n", post)
p.API.CreatePost(post)
}
}()
return nil
}

func (p *Plugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) {
if post.Props != nil {
if _, ok := post.Props["matterbridge_"+p.userid].(bool); ok {
fmt.Println("sent by matterbridge, ignoring")
return
}
}
ch, _ := p.API.GetChannel(post.ChannelId)
u, _ := p.API.GetUser(post.UserId)
msg := config.Message{Username: u.Nickname, UserID: post.UserId, Channel: ch.Name, Text: post.Message, ID: post.Id, Account: "mattermost.plugin", Protocol: "mattermost", Gateway: "plugin"}
fmt.Printf("sending message %#v", msg)
p.r.Message <- msg
}

func main() {
plugin.ClientMain(&Plugin{})
}
23 changes: 23 additions & 0 deletions matterbridge.toml.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[mattermost.plugin]
team="yourteam" # CHANGE THIS
login="youruser" # CHANGE THIS
server="plugin" #DO NOT CHANGE THIS
password="plugin" #DO NOT CHANGE THIS

[slack.test]
Token="xoxp-yourslacktoken"
RemoteNickFormat="[{PROTOCOL}/{BRIDGE}] <{NICK}> "
PrefixMessagesWithNick=false

[[gateway]]
name="plugin" #DO NOT CHANGE THIS
enable=true

[[gateway.inout]]
account = "discord.test"
channel = "general"

[[gateway.inout]]
account = "mattermost.plugin" # DO NOT CHANGE THIS
channel = "town-square"

6 changes: 6 additions & 0 deletions plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id: matterbridge
backend:
executable: plugin.exe
name: matterbridge
description: matterbridge
version: '0.0.1'

0 comments on commit 087d235

Please sign in to comment.