-
Notifications
You must be signed in to change notification settings - Fork 1
/
bridge.go
379 lines (319 loc) · 10.4 KB
/
bridge.go
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// matrix-pulsesms - A Matrix-PulseSMS puppeting bridge.
// Copyright (C) 2021 Cam Sweeney
// Copyright (C) 2020 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package config
import (
"bytes"
"strconv"
"strings"
"text/template"
"github.com/treethought/pulsesms"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
type BridgeConfig struct {
UsernameTemplate string `yaml:"username_template"`
DisplaynameTemplate string `yaml:"displayname_template"`
CommunityTemplate string `yaml:"community_template"`
SpaceTemplate string `yaml:"space_template"`
ConnectionTimeout int `yaml:"connection_timeout"`
FetchMessageOnTimeout bool `yaml:"fetch_message_on_timeout"`
DeliveryReceipts bool `yaml:"delivery_receipts"`
MaxConnectionAttempts int `yaml:"max_connection_attempts"`
ConnectionRetryDelay int `yaml:"connection_retry_delay"`
ReportConnectionRetry bool `yaml:"report_connection_retry"`
AggressiveReconnect bool `yaml:"aggressive_reconnect"`
ChatListWait int `yaml:"chat_list_wait"`
PortalSyncWait int `yaml:"portal_sync_wait"`
UserMessageBuffer int `yaml:"user_message_buffer"`
PortalMessageBuffer int `yaml:"portal_message_buffer"`
CallNotices struct {
Start bool `yaml:"start"`
End bool `yaml:"end"`
} `yaml:"call_notices"`
InitialChatSync int `yaml:"initial_chat_sync_count"`
InitialHistoryFill int `yaml:"initial_history_fill_count"`
HistoryDisableNotifs bool `yaml:"initial_history_disable_notifications"`
RecoverChatSync int `yaml:"recovery_chat_sync_count"`
RecoverHistory bool `yaml:"recovery_history_backfill"`
ChatMetaSync bool `yaml:"chat_meta_sync"`
UserAvatarSync bool `yaml:"user_avatar_sync"`
BridgeMatrixLeave bool `yaml:"bridge_matrix_leave"`
SyncChatMaxAge int64 `yaml:"sync_max_chat_age"`
SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"`
SyncDirectChatList bool `yaml:"sync_direct_chat_list"`
DefaultBridgeReceipts bool `yaml:"default_bridge_receipts"`
DefaultBridgePresence bool `yaml:"default_bridge_presence"`
LoginSharedSecret string `yaml:"login_shared_secret"`
InviteOwnPuppetForBackfilling bool `yaml:"invite_own_puppet_for_backfilling"`
PrivateChatPortalMeta bool `yaml:"private_chat_portal_meta"`
BridgeNotices bool `yaml:"bridge_notices"`
ResendBridgeInfo bool `yaml:"resend_bridge_info"`
MuteBridging bool `yaml:"mute_bridging"`
ArchiveTag string `yaml:"archive_tag"`
PinnedTag string `yaml:"pinned_tag"`
TagOnlyOnCreate bool `yaml:"tag_only_on_create"`
MarkReadOnlyOnCreate bool `yaml:"mark_read_only_on_create"`
WhatsappThumbnail bool `yaml:"whatsapp_thumbnail"`
AllowUserInvite bool `yaml:"allow_user_invite"`
CommandPrefix string `yaml:"command_prefix"`
Encryption struct {
Allow bool `yaml:"allow"`
Default bool `yaml:"default"`
KeySharing struct {
Allow bool `yaml:"allow"`
RequireCrossSigning bool `yaml:"require_cross_signing"`
RequireVerification bool `yaml:"require_verification"`
} `yaml:"key_sharing"`
} `yaml:"encryption"`
Permissions PermissionConfig `yaml:"permissions"`
Relaybot RelaybotConfig `yaml:"relaybot"`
usernameTemplate *template.Template `yaml:"-"`
displaynameTemplate *template.Template `yaml:"-"`
communityTemplate *template.Template `yaml:"-"`
spaceTemplate *template.Template `yaml:"-"`
}
func (bc *BridgeConfig) setDefaults() {
bc.ConnectionTimeout = 20
bc.FetchMessageOnTimeout = false
bc.DeliveryReceipts = false
bc.MaxConnectionAttempts = 3
bc.ConnectionRetryDelay = -1
bc.ReportConnectionRetry = true
bc.ChatListWait = 30
bc.PortalSyncWait = 600
bc.UserMessageBuffer = 1024
bc.PortalMessageBuffer = 128
bc.CallNotices.Start = true
bc.CallNotices.End = true
bc.InitialChatSync = 10
bc.InitialHistoryFill = 20
bc.RecoverChatSync = -1
bc.RecoverHistory = true
bc.ChatMetaSync = true
bc.UserAvatarSync = true
bc.BridgeMatrixLeave = true
bc.SyncChatMaxAge = 259200
bc.SyncWithCustomPuppets = true
bc.DefaultBridgePresence = true
bc.DefaultBridgeReceipts = true
bc.LoginSharedSecret = ""
bc.InviteOwnPuppetForBackfilling = true
bc.PrivateChatPortalMeta = false
bc.BridgeNotices = true
}
type umBridgeConfig BridgeConfig
func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
err := unmarshal((*umBridgeConfig)(bc))
if err != nil {
return err
}
bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
if err != nil {
return err
}
bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)
if err != nil {
return err
}
if len(bc.CommunityTemplate) > 0 {
bc.communityTemplate, err = template.New("community").Parse(bc.CommunityTemplate)
if err != nil {
return err
}
}
if len(bc.SpaceTemplate) > 0 {
bc.spaceTemplate, err = template.New("space").Parse(bc.SpaceTemplate)
if err != nil {
return err
}
}
return nil
}
type UsernameTemplateArgs struct {
UserID id.UserID
}
func (bc BridgeConfig) FormatDisplayname(contact pulsesms.Contact) (string, int8) {
var buf bytes.Buffer
// if index := strings.IndexRune(contact.Name, '@'); index > 0 {
// contact.Name = "+" + contact.Name[:index]
// }
bc.displaynameTemplate.Execute(&buf, contact)
var quality int8
switch {
case len(contact.Notify) > 0:
quality = 3
case len(contact.Name) > 0 || len(contact.Short) > 0:
quality = 2
case len(contact.PhoneNumber) > 0:
quality = 1
default:
quality = 0
}
return contact.Name, quality
}
func (bc BridgeConfig) FormatUsername(userID string) string {
var buf bytes.Buffer
bc.usernameTemplate.Execute(&buf, userID)
return buf.String()
}
type CommunityTemplateArgs struct {
Localpart string
Server string
}
func (bc BridgeConfig) EnableCommunities() bool {
return bc.communityTemplate != nil
}
func (bc BridgeConfig) FormatCommunity(localpart, server string) string {
var buf bytes.Buffer
bc.communityTemplate.Execute(&buf, CommunityTemplateArgs{localpart, server})
return buf.String()
}
type SpaceTemplateArgs struct {
Localpart string
Server string
}
func (bc BridgeConfig) EnableSpaces() bool {
return bc.spaceTemplate != nil
}
func (bc BridgeConfig) FormatSpace(localpart, server string) string {
var buf bytes.Buffer
bc.spaceTemplate.Execute(&buf, SpaceTemplateArgs{localpart, server})
return buf.String()
}
type PermissionConfig map[string]PermissionLevel
type PermissionLevel int
const (
PermissionLevelDefault PermissionLevel = 0
PermissionLevelRelaybot PermissionLevel = 5
PermissionLevelUser PermissionLevel = 10
PermissionLevelAdmin PermissionLevel = 100
)
func (pc *PermissionConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
rawPC := make(map[string]string)
err := unmarshal(&rawPC)
if err != nil {
return err
}
if *pc == nil {
*pc = make(map[string]PermissionLevel)
}
for key, value := range rawPC {
switch strings.ToLower(value) {
case "relaybot":
(*pc)[key] = PermissionLevelRelaybot
case "user":
(*pc)[key] = PermissionLevelUser
case "admin":
(*pc)[key] = PermissionLevelAdmin
default:
val, err := strconv.Atoi(value)
if err != nil {
(*pc)[key] = PermissionLevelDefault
} else {
(*pc)[key] = PermissionLevel(val)
}
}
}
return nil
}
func (pc *PermissionConfig) MarshalYAML() (interface{}, error) {
if *pc == nil {
return nil, nil
}
rawPC := make(map[string]string)
for key, value := range *pc {
switch value {
case PermissionLevelRelaybot:
rawPC[key] = "relaybot"
case PermissionLevelUser:
rawPC[key] = "user"
case PermissionLevelAdmin:
rawPC[key] = "admin"
default:
rawPC[key] = strconv.Itoa(int(value))
}
}
return rawPC, nil
}
func (pc PermissionConfig) IsRelaybotWhitelisted(userID id.UserID) bool {
return pc.GetPermissionLevel(userID) >= PermissionLevelRelaybot
}
func (pc PermissionConfig) IsWhitelisted(userID id.UserID) bool {
return pc.GetPermissionLevel(userID) >= PermissionLevelUser
}
func (pc PermissionConfig) IsAdmin(userID id.UserID) bool {
return pc.GetPermissionLevel(userID) >= PermissionLevelAdmin
}
func (pc PermissionConfig) GetPermissionLevel(userID id.UserID) PermissionLevel {
permissions, ok := pc[string(userID)]
if ok {
return permissions
}
_, homeserver, _ := userID.Parse()
permissions, ok = pc[homeserver]
if len(homeserver) > 0 && ok {
return permissions
}
permissions, ok = pc["*"]
if ok {
return permissions
}
return PermissionLevelDefault
}
type RelaybotConfig struct {
Enabled bool `yaml:"enabled"`
ManagementRoom id.RoomID `yaml:"management"`
InviteUsers []id.UserID `yaml:"invites"`
MessageFormats map[event.MessageType]string `yaml:"message_formats"`
messageTemplates *template.Template `yaml:"-"`
}
type umRelaybotConfig RelaybotConfig
func (rc *RelaybotConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
err := unmarshal((*umRelaybotConfig)(rc))
if err != nil {
return err
}
rc.messageTemplates = template.New("messageTemplates")
for key, format := range rc.MessageFormats {
_, err := rc.messageTemplates.New(string(key)).Parse(format)
if err != nil {
return err
}
}
return nil
}
type Sender struct {
UserID id.UserID
*event.MemberEventContent
}
type formatData struct {
Sender Sender
Message string
Content *event.MessageEventContent
}
func (rc *RelaybotConfig) FormatMessage(content *event.MessageEventContent, sender id.UserID, member *event.MemberEventContent) (string, error) {
var output strings.Builder
err := rc.messageTemplates.ExecuteTemplate(&output, string(content.MsgType), formatData{
Sender: Sender{
UserID: sender,
MemberEventContent: member,
},
Content: content,
Message: content.FormattedBody,
})
return output.String(), err
}