Skip to content

Commit 087d235

Browse files
committed
Initial commit
1 parent 58744c1 commit 087d235

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.PHONY: all build plugin
2+
config=matterbridge.toml
3+
all: build plugin
4+
5+
plugin:
6+
ifeq ("$(wildcard $(config))","")
7+
$(error you need to create a matterbridge.toml file, look at matterbridge.toml.sample and README.md)
8+
endif
9+
tar zcf plugin.tar.gz plugin.exe plugin.yaml
10+
@echo " "
11+
@echo "finished, upload plugin.tar.gz to mattermost"
12+
@ls -al plugin.tar.gz
13+
14+
build:
15+
@echo "building plugin.exe"
16+
@echo "-------------------"
17+
CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags '-s' -o plugin.exe
18+
19+
clean:
20+
rm plugin.tar.gz plugin.exe

main.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
"time"
9+
10+
"github.com/42wim/matterbridge/bridge/config"
11+
"github.com/42wim/matterbridge/gateway"
12+
"github.com/42wim/mattermost-server/model"
13+
"github.com/42wim/mattermost-server/plugin"
14+
)
15+
16+
type Plugin struct {
17+
plugin.MattermostPlugin
18+
r *gateway.Router
19+
userid string
20+
teamid string
21+
channels []string
22+
}
23+
24+
func (p *Plugin) OnActivate() error {
25+
fmt.Println("reading config")
26+
ex, err := os.Executable()
27+
if err != nil {
28+
panic(err)
29+
}
30+
exPath := filepath.Dir(ex)
31+
cfg := config.NewConfig(exPath + "/" + "matterbridge.toml")
32+
fmt.Println("matterbridge config read")
33+
r, err := gateway.NewRouter(cfg)
34+
if err != nil {
35+
log.Fatalf("Starting gateway failed: %s", err)
36+
}
37+
p.r = r
38+
go func() {
39+
err = r.Start()
40+
if err != nil {
41+
log.Fatalf("Starting gateway failed: %s", err)
42+
}
43+
fmt.Printf("Gateway(s) started succesfully. Now relaying messages")
44+
select {}
45+
}()
46+
go func() {
47+
// wait until activation is done, otherwise API doesn't seem to work?
48+
time.Sleep(time.Second)
49+
user, err := p.API.GetUserByUsername(cfg.Mattermost["plugin"].Login)
50+
if err != nil {
51+
fmt.Println("username", err.Error())
52+
}
53+
p.userid = user.Id
54+
fmt.Println("found userid", p.userid)
55+
team, err := p.API.GetTeamByName(cfg.Mattermost["plugin"].Team)
56+
if err != nil {
57+
fmt.Println("team", err.Error())
58+
}
59+
p.teamid = team.Id
60+
fmt.Println("found teamid", p.teamid)
61+
}()
62+
go func() {
63+
for msg := range p.r.MattermostPlugin {
64+
fmt.Printf("Got message %#v\n", msg)
65+
channel, _ := p.API.GetChannelByName(p.teamid, msg.Channel, false)
66+
props := make(map[string]interface{})
67+
props["matterbridge_"+p.userid] = true
68+
post := &model.Post{UserId: p.userid, ChannelId: channel.Id, Message: msg.Username + msg.Text, Props: props}
69+
fmt.Printf("Posting %#v\n", post)
70+
p.API.CreatePost(post)
71+
}
72+
}()
73+
return nil
74+
}
75+
76+
func (p *Plugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) {
77+
if post.Props != nil {
78+
if _, ok := post.Props["matterbridge_"+p.userid].(bool); ok {
79+
fmt.Println("sent by matterbridge, ignoring")
80+
return
81+
}
82+
}
83+
ch, _ := p.API.GetChannel(post.ChannelId)
84+
u, _ := p.API.GetUser(post.UserId)
85+
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"}
86+
fmt.Printf("sending message %#v", msg)
87+
p.r.Message <- msg
88+
}
89+
90+
func main() {
91+
plugin.ClientMain(&Plugin{})
92+
}

matterbridge.toml.sample

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[mattermost.plugin]
2+
team="yourteam" # CHANGE THIS
3+
login="youruser" # CHANGE THIS
4+
server="plugin" #DO NOT CHANGE THIS
5+
password="plugin" #DO NOT CHANGE THIS
6+
7+
[slack.test]
8+
Token="xoxp-yourslacktoken"
9+
RemoteNickFormat="[{PROTOCOL}/{BRIDGE}] <{NICK}> "
10+
PrefixMessagesWithNick=false
11+
12+
[[gateway]]
13+
name="plugin" #DO NOT CHANGE THIS
14+
enable=true
15+
16+
[[gateway.inout]]
17+
account = "discord.test"
18+
channel = "general"
19+
20+
[[gateway.inout]]
21+
account = "mattermost.plugin" # DO NOT CHANGE THIS
22+
channel = "town-square"
23+

plugin.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
id: matterbridge
2+
backend:
3+
executable: plugin.exe
4+
name: matterbridge
5+
description: matterbridge
6+
version: '0.0.1'

0 commit comments

Comments
 (0)