This repository has been archived by the owner on Aug 1, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
registration.go
104 lines (90 loc) · 2.92 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
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
// Copyright (c) 2019 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package appservice
import (
"io/ioutil"
"regexp"
"gopkg.in/yaml.v2"
)
// Registration contains the data in a Matrix appservice registration.
// See https://matrix.org/docs/spec/application_service/unstable.html#registration
type Registration struct {
ID string `yaml:"id"`
URL string `yaml:"url"`
AppToken string `yaml:"as_token"`
ServerToken string `yaml:"hs_token"`
SenderLocalpart string `yaml:"sender_localpart"`
RateLimited bool `yaml:"rate_limited"`
Namespaces Namespaces `yaml:"namespaces"`
}
// CreateRegistration creates a Registration with random appservice and homeserver tokens.
func CreateRegistration() *Registration {
return &Registration{
AppToken: RandomString(64),
ServerToken: RandomString(64),
}
}
// LoadRegistration loads a YAML file and turns it into a Registration.
func LoadRegistration(path string) (*Registration, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
reg := &Registration{}
err = yaml.Unmarshal(data, reg)
if err != nil {
return nil, err
}
return reg, nil
}
// Save saves this Registration into a file at the given path.
func (reg *Registration) Save(path string) error {
data, err := yaml.Marshal(reg)
if err != nil {
return err
}
return ioutil.WriteFile(path, data, 0600)
}
// YAML returns the registration in YAML format.
func (reg *Registration) YAML() (string, error) {
data, err := yaml.Marshal(reg)
if err != nil {
return "", err
}
return string(data), nil
}
// Namespaces contains the three areas that appservices can reserve parts of.
type Namespaces struct {
UserIDs []Namespace `yaml:"users,omitempty"`
RoomAliases []Namespace `yaml:"aliases,omitempty"`
RoomIDs []Namespace `yaml:"rooms,omitempty"`
}
// Namespace is a reserved namespace in any area.
type Namespace struct {
Regex string `yaml:"regex"`
Exclusive bool `yaml:"exclusive"`
}
// RegisterUserIDs creates an user ID namespace registration.
func (nslist *Namespaces) RegisterUserIDs(regex *regexp.Regexp, exclusive bool) {
nslist.UserIDs = append(nslist.UserIDs, Namespace{
Regex: regex.String(),
Exclusive: exclusive,
})
}
// RegisterRoomAliases creates an room alias namespace registration.
func (nslist *Namespaces) RegisterRoomAliases(regex *regexp.Regexp, exclusive bool) {
nslist.RoomAliases = append(nslist.RoomAliases, Namespace{
Regex: regex.String(),
Exclusive: exclusive,
})
}
// RegisterRoomIDs creates an room ID namespace registration.
func (nslist *Namespaces) RegisterRoomIDs(regex *regexp.Regexp, exclusive bool) {
nslist.RoomIDs = append(nslist.RoomIDs, Namespace{
Regex: regex.String(),
Exclusive: exclusive,
})
}