-
Notifications
You must be signed in to change notification settings - Fork 1
/
registration.go
77 lines (64 loc) · 2.48 KB
/
registration.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
// 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 (
"fmt"
"regexp"
"github.com/sirupsen/logrus"
"maunium.net/go/mautrix/appservice"
)
func (config *Config) NewRegistration() (*appservice.Registration, error) {
registration := appservice.CreateRegistration()
err := config.copyToRegistration(registration)
if err != nil {
return nil, err
}
config.AppService.ASToken = registration.AppToken
config.AppService.HSToken = registration.ServerToken
// Workaround for https://github.com/matrix-org/synapse/pull/5758
// registration.SenderLocalpart = appservice.RandomString(32)
registration.SenderLocalpart = config.AppService.Bot.Username
botRegex := regexp.MustCompile(fmt.Sprintf("^@%s:%s$", config.AppService.Bot.Username, config.Homeserver.Domain))
logrus.Error(botRegex.String())
registration.Namespaces.RegisterUserIDs(botRegex, true)
return registration, nil
}
func (config *Config) GetRegistration() (*appservice.Registration, error) {
registration := appservice.CreateRegistration()
err := config.copyToRegistration(registration)
if err != nil {
return nil, err
}
registration.AppToken = config.AppService.ASToken
registration.ServerToken = config.AppService.HSToken
return registration, nil
}
func (config *Config) copyToRegistration(registration *appservice.Registration) error {
registration.ID = config.AppService.ID
registration.URL = config.AppService.Address
falseVal := false
registration.RateLimited = &falseVal
registration.SenderLocalpart = config.AppService.Bot.Username
userIDRegex, err := regexp.Compile(fmt.Sprintf("^@%s:%s$",
config.Bridge.FormatUsername("[0-9]+"),
config.Homeserver.Domain))
if err != nil {
return err
}
registration.Namespaces.RegisterUserIDs(userIDRegex, true)
return nil
}