1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
package gateway
import (
"fmt"
"github.com/42wim/matterbridge/bridge/config"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)
var testconfig = []byte(`
[irc.freenode]
[mattermost.test]
[gitter.42wim]
[discord.test]
[slack.test]
[[gateway]]
name = "bridge1"
enable=true
[[gateway.inout]]
account = "irc.freenode"
channel = "#wimtesting"
[[gateway.inout]]
account="gitter.42wim"
channel="42wim/testroom"
#channel="matterbridge/Lobby"
[[gateway.inout]]
account = "discord.test"
channel = "general"
[[gateway.inout]]
account="slack.test"
channel="testing"
`)
var testconfig2 = []byte(`
[irc.freenode]
[mattermost.test]
[gitter.42wim]
[discord.test]
[slack.test]
[[gateway]]
name = "bridge1"
enable=true
[[gateway.in]]
account = "irc.freenode"
channel = "#wimtesting"
[[gateway.in]]
account="gitter.42wim"
channel="42wim/testroom"
[[gateway.inout]]
account = "discord.test"
channel = "general"
[[gateway.out]]
account="slack.test"
channel="testing"
[[gateway]]
name = "bridge2"
enable=true
[[gateway.in]]
account = "irc.freenode"
channel = "#wimtesting2"
[[gateway.out]]
account="gitter.42wim"
channel="42wim/testroom"
[[gateway.out]]
account = "discord.test"
channel = "general2"
`)
var testconfig3 = []byte(`
[irc.zzz]
[telegram.zzz]
[slack.zzz]
[[gateway]]
name="bridge"
enable=true
[[gateway.inout]]
account="irc.zzz"
channel="#main"
[[gateway.inout]]
account="telegram.zzz"
channel="-1111111111111"
[[gateway.inout]]
account="slack.zzz"
channel="irc"
[[gateway]]
name="announcements"
enable=true
[[gateway.in]]
account="telegram.zzz"
channel="-2222222222222"
[[gateway.out]]
account="irc.zzz"
channel="#main"
[[gateway.out]]
account="irc.zzz"
channel="#main-help"
[[gateway.out]]
account="telegram.zzz"
channel="--333333333333"
[[gateway.out]]
account="slack.zzz"
channel="general"
[[gateway]]
name="bridge2"
enable=true
[[gateway.inout]]
account="irc.zzz"
channel="#main-help"
[[gateway.inout]]
account="telegram.zzz"
channel="--444444444444"
[[gateway]]
name="bridge3"
enable=true
[[gateway.inout]]
account="irc.zzz"
channel="#main-telegram"
[[gateway.inout]]
account="telegram.zzz"
channel="--333333333333"
`)
func maketestRouter(input []byte) *Router {
cfg := config.NewConfigFromString(input)
r, err := NewRouter(cfg)
if err != nil {
fmt.Println(err)
}
return r
}
func TestNewRouter(t *testing.T) {
r := maketestRouter(testconfig)
assert.Equal(t, 1, len(r.Gateways))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels))
r = maketestRouter(testconfig2)
assert.Equal(t, 2, len(r.Gateways))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Bridges))
assert.Equal(t, 3, len(r.Gateways["bridge2"].Bridges))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels))
assert.Equal(t, 3, len(r.Gateways["bridge2"].Channels))
assert.Equal(t, &config.ChannelInfo{Name: "42wim/testroom", Direction: "out",
ID: "42wim/testroomgitter.42wim", Account: "gitter.42wim",
SameChannel: map[string]bool{"bridge2": false}},
r.Gateways["bridge2"].Channels["42wim/testroomgitter.42wim"])
assert.Equal(t, &config.ChannelInfo{Name: "42wim/testroom", Direction: "in",
ID: "42wim/testroomgitter.42wim", Account: "gitter.42wim",
SameChannel: map[string]bool{"bridge1": false}},
r.Gateways["bridge1"].Channels["42wim/testroomgitter.42wim"])
assert.Equal(t, &config.ChannelInfo{Name: "general", Direction: "inout",
ID: "generaldiscord.test", Account: "discord.test",
SameChannel: map[string]bool{"bridge1": false}},
r.Gateways["bridge1"].Channels["generaldiscord.test"])
}
func TestGetDestChannel(t *testing.T) {
r := maketestRouter(testconfig2)
msg := &config.Message{Text: "test", Channel: "general", Account: "discord.test", Gateway: "bridge1", Protocol: "discord", Username: "test"}
for _, br := range r.Gateways["bridge1"].Bridges {
switch br.Account {
case "discord.test":
assert.Equal(t, []config.ChannelInfo{{Name: "general", Account: "discord.test", Direction: "inout", ID: "generaldiscord.test", SameChannel: map[string]bool{"bridge1": false}, Options: config.ChannelOptions{Key: ""}}},
r.Gateways["bridge1"].getDestChannel(msg, *br))
case "slack.test":
assert.Equal(t, []config.ChannelInfo{{Name: "testing", Account: "slack.test", Direction: "out", ID: "testingslack.test", SameChannel: map[string]bool{"bridge1": false}, Options: config.ChannelOptions{Key: ""}}},
r.Gateways["bridge1"].getDestChannel(msg, *br))
case "gitter.42wim":
assert.Equal(t, []config.ChannelInfo(nil), r.Gateways["bridge1"].getDestChannel(msg, *br))
case "irc.freenode":
assert.Equal(t, []config.ChannelInfo(nil), r.Gateways["bridge1"].getDestChannel(msg, *br))
}
}
}
func TestGetDestChannelAdvanced(t *testing.T) {
r := maketestRouter(testconfig3)
var msgs []*config.Message
i := 0
for _, gw := range r.Gateways {
for _, channel := range gw.Channels {
msgs = append(msgs, &config.Message{Text: "text" + strconv.Itoa(i), Channel: channel.Name, Account: channel.Account, Gateway: gw.Name, Username: "user" + strconv.Itoa(i)})
i++
}
}
hits := make(map[string]int)
for _, gw := range r.Gateways {
for _, br := range gw.Bridges {
for _, msg := range msgs {
channels := gw.getDestChannel(msg, *br)
if gw.Name != msg.Gateway {
assert.Equal(t, []config.ChannelInfo(nil), channels)
continue
}
switch gw.Name {
case "bridge":
if (msg.Channel == "#main" || msg.Channel == "-1111111111111" || msg.Channel == "irc") && (msg.Account == "irc.zzz" || msg.Account == "telegram.zzz" || msg.Account == "slack.zzz") {
hits[gw.Name]++
switch br.Account {
case "irc.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "#main", Account: "irc.zzz", Direction: "inout", ID: "#mainirc.zzz", SameChannel: map[string]bool{"bridge": false}, Options: config.ChannelOptions{Key: ""}}}, channels)
case "telegram.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "-1111111111111", Account: "telegram.zzz", Direction: "inout", ID: "-1111111111111telegram.zzz", SameChannel: map[string]bool{"bridge": false}, Options: config.ChannelOptions{Key: ""}}}, channels)
case "slack.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "irc", Account: "slack.zzz", Direction: "inout", ID: "ircslack.zzz", SameChannel: map[string]bool{"bridge": false}, Options: config.ChannelOptions{Key: ""}}}, channels)
}
}
case "bridge2":
if (msg.Channel == "#main-help" || msg.Channel == "--444444444444") && (msg.Account == "irc.zzz" || msg.Account == "telegram.zzz") {
hits[gw.Name]++
switch br.Account {
case "irc.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "#main-help", Account: "irc.zzz", Direction: "inout", ID: "#main-helpirc.zzz", SameChannel: map[string]bool{"bridge2": false}, Options: config.ChannelOptions{Key: ""}}}, channels)
case "telegram.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "--444444444444", Account: "telegram.zzz", Direction: "inout", ID: "--444444444444telegram.zzz", SameChannel: map[string]bool{"bridge2": false}, Options: config.ChannelOptions{Key: ""}}}, channels)
}
}
case "bridge3":
if (msg.Channel == "#main-telegram" || msg.Channel == "--333333333333") && (msg.Account == "irc.zzz" || msg.Account == "telegram.zzz") {
hits[gw.Name]++
switch br.Account {
case "irc.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "#main-telegram", Account: "irc.zzz", Direction: "inout", ID: "#main-telegramirc.zzz", SameChannel: map[string]bool{"bridge3": false}, Options: config.ChannelOptions{Key: ""}}}, channels)
case "telegram.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "--333333333333", Account: "telegram.zzz", Direction: "inout", ID: "--333333333333telegram.zzz", SameChannel: map[string]bool{"bridge3": false}, Options: config.ChannelOptions{Key: ""}}}, channels)
}
}
case "announcements":
if msg.Channel != "-2222222222222" && msg.Account != "telegram" {
assert.Equal(t, []config.ChannelInfo(nil), channels)
continue
}
hits[gw.Name]++
switch br.Account {
case "irc.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "#main", Account: "irc.zzz", Direction: "out", ID: "#mainirc.zzz", SameChannel: map[string]bool{"announcements": false}, Options: config.ChannelOptions{Key: ""}}, {Name: "#main-help", Account: "irc.zzz", Direction: "out", ID: "#main-helpirc.zzz", SameChannel: map[string]bool{"announcements": false}, Options: config.ChannelOptions{Key: ""}}}, channels)
case "slack.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "general", Account: "slack.zzz", Direction: "out", ID: "generalslack.zzz", SameChannel: map[string]bool{"announcements": false}, Options: config.ChannelOptions{Key: ""}}}, channels)
case "telegram.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "--333333333333", Account: "telegram.zzz", Direction: "out", ID: "--333333333333telegram.zzz", SameChannel: map[string]bool{"announcements": false}, Options: config.ChannelOptions{Key: ""}}}, channels)
}
}
}
}
}
assert.Equal(t, map[string]int{"bridge3": 4, "bridge": 9, "announcements": 3, "bridge2": 4}, hits)
}
|