| 1 |
// Copyright (c) 2019 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package appservice |
| 8 |
|
| 9 |
import ( |
| 10 |
"io/ioutil" |
| 11 |
"regexp" |
| 12 |
|
| 13 |
"gopkg.in/yaml.v2" |
| 14 |
) |
| 15 |
|
| 16 |
// Registration contains the data in a Matrix appservice registration. |
| 17 |
// See https://matrix.org/docs/spec/application_service/unstable.html#registration |
| 18 |
type Registration struct { |
| 19 |
ID string `yaml:"id"` |
| 20 |
URL string `yaml:"url"` |
| 21 |
AppToken string `yaml:"as_token"` |
| 22 |
ServerToken string `yaml:"hs_token"` |
| 23 |
SenderLocalpart string `yaml:"sender_localpart"` |
| 24 |
RateLimited *bool `yaml:"rate_limited,omitempty"` |
| 25 |
Namespaces Namespaces `yaml:"namespaces"` |
| 26 |
EphemeralEvents bool `yaml:"de.sorunome.msc2409.push_ephemeral,omitempty"` |
| 27 |
Protocols []string `yaml:"protocols,omitempty"` |
| 28 |
} |
| 29 |
|
| 30 |
// CreateRegistration creates a Registration with random appservice and homeserver tokens. |
| 31 |
func CreateRegistration() *Registration { |
| 32 |
return &Registration{ |
| 33 |
AppToken: RandomString(64), |
| 34 |
ServerToken: RandomString(64), |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// LoadRegistration loads a YAML file and turns it into a Registration. |
| 39 |
func LoadRegistration(path string) (*Registration, error) { |
| 40 |
data, err := ioutil.ReadFile(path) |
| 41 |
if err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
|
| 45 |
reg := &Registration{} |
| 46 |
err = yaml.Unmarshal(data, reg) |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
return reg, nil |
| 51 |
} |
| 52 |
|
| 53 |
// Save saves this Registration into a file at the given path. |
| 54 |
func (reg *Registration) Save(path string) error { |
| 55 |
data, err := yaml.Marshal(reg) |
| 56 |
if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
return ioutil.WriteFile(path, data, 0600) |
| 60 |
} |
| 61 |
|
| 62 |
// YAML returns the registration in YAML format. |
| 63 |
func (reg *Registration) YAML() (string, error) { |
| 64 |
data, err := yaml.Marshal(reg) |
| 65 |
if err != nil { |
| 66 |
return "", err |
| 67 |
} |
| 68 |
return string(data), nil |
| 69 |
} |
| 70 |
|
| 71 |
// Namespaces contains the three areas that appservices can reserve parts of. |
| 72 |
type Namespaces struct { |
| 73 |
UserIDs []Namespace `yaml:"users,omitempty"` |
| 74 |
RoomAliases []Namespace `yaml:"aliases,omitempty"` |
| 75 |
RoomIDs []Namespace `yaml:"rooms,omitempty"` |
| 76 |
} |
| 77 |
|
| 78 |
// Namespace is a reserved namespace in any area. |
| 79 |
type Namespace struct { |
| 80 |
Regex string `yaml:"regex"` |
| 81 |
Exclusive bool `yaml:"exclusive"` |
| 82 |
} |
| 83 |
|
| 84 |
// RegisterUserIDs creates an user ID namespace registration. |
| 85 |
func (nslist *Namespaces) RegisterUserIDs(regex *regexp.Regexp, exclusive bool) { |
| 86 |
nslist.UserIDs = append(nslist.UserIDs, Namespace{ |
| 87 |
Regex: regex.String(), |
| 88 |
Exclusive: exclusive, |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
// RegisterRoomAliases creates an room alias namespace registration. |
| 93 |
func (nslist *Namespaces) RegisterRoomAliases(regex *regexp.Regexp, exclusive bool) { |
| 94 |
nslist.RoomAliases = append(nslist.RoomAliases, Namespace{ |
| 95 |
Regex: regex.String(), |
| 96 |
Exclusive: exclusive, |
| 97 |
}) |
| 98 |
} |
| 99 |
|
| 100 |
// RegisterRoomIDs creates an room ID namespace registration. |
| 101 |
func (nslist *Namespaces) RegisterRoomIDs(regex *regexp.Regexp, exclusive bool) { |
| 102 |
nslist.RoomIDs = append(nslist.RoomIDs, Namespace{ |
| 103 |
Regex: regex.String(), |
| 104 |
Exclusive: exclusive, |
| 105 |
}) |
| 106 |
} |