|
| 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 | +} |
0 commit comments