app-service-registration.go (5280B)
1 package registration 2 3 import ( 4 "bufio" 5 "encoding/json" 6 "github.com/MTRNord/matrix-appservice-go/utils" 7 "gopkg.in/yaml.v2" 8 "log" 9 "os" 10 "regexp" 11 ) 12 13 type RegexObject struct { 14 Exclusive bool `yaml:"exclusive,omitempty" json:"exclusive,omitempty"` 15 Regex *regexp.Regexp `yaml:"regex,omitempty" json:"regex,omitempty"` 16 } 17 18 type Namespaces struct { 19 Users []RegexObject `yaml:",flow,omitempty" json:"users,omitempty"` 20 Aliases []RegexObject `yaml:",flow,omitempty" json:"aliases,omitempty"` 21 Rooms []RegexObject `yaml:",flow,omitempty" json:"rooms,omitempty"` 22 } 23 24 // Something is the structure we work with 25 type AppServiceRegistration struct { 26 Url string `yaml:"url,omitempty" json:"url,omitempty"` 27 Id string `yaml:"id,omitempty" json:"id,omitempty"` 28 HsToken string `yaml:"hs_token,omitempty" json:"hs_token,omitempty"` 29 AsToken string `yaml:"as_token,omitempty" json:"as_token,omitempty"` 30 SenderLocalpart string `yaml:"sender_localpart,omitempty" json:"sender_localpart,omitempty"` 31 RateLimited bool `yaml:"rate_limited,omitempty" json:"rate_limited,omitempty"` 32 Namespaces `yaml:"namespaces,omitempty" json:"namespaces,omitempty"` 33 Protocols []string `yaml:"protocols,omitempty" json:"protocols,omitempty"` 34 } 35 36 // NewSomething create new instance of Something 37 func NewAppServiceRegistration(appServiceUrl string) *AppServiceRegistration { 38 AppServiceRegistrationStruct := AppServiceRegistration{ 39 Url: appServiceUrl, 40 RateLimited: true, 41 } 42 return &AppServiceRegistrationStruct 43 } 44 45 func NewFromJson(data []byte) *AppServiceRegistration { 46 AppServiceRegistrationStruct := AppServiceRegistration{} 47 json.Unmarshal(data, &AppServiceRegistrationStruct) 48 return &AppServiceRegistrationStruct 49 } 50 51 func GenerateToken() string { 52 return utils.RandomString(32) 53 } 54 55 func (a *AppServiceRegistration) SetAppServiceUrl(url string) { 56 a.Url = url 57 } 58 59 func (a *AppServiceRegistration) SetID(id string) { 60 a.Id = id 61 } 62 63 func (a *AppServiceRegistration) GetID() string { 64 return a.Id 65 } 66 67 func (a *AppServiceRegistration) SetProtocols(protocols []string) { 68 a.Protocols = protocols 69 } 70 71 func (a *AppServiceRegistration) GetProtocols() []string { 72 return a.Protocols 73 } 74 75 func (a *AppServiceRegistration) SetHomeserverToken(token string) { 76 a.HsToken = token 77 } 78 79 func (a *AppServiceRegistration) GetHomeserverToken() string { 80 return a.HsToken 81 } 82 83 func (a *AppServiceRegistration) SetAppServiceToken(token string) { 84 a.AsToken = token 85 } 86 87 func (a *AppServiceRegistration) GetAppServiceToken() string { 88 return a.AsToken 89 } 90 91 func (a *AppServiceRegistration) SetSenderLocalpart(localpart string) { 92 a.SenderLocalpart = localpart 93 } 94 95 func (a *AppServiceRegistration) GetSenderLocalpart() string { 96 return a.SenderLocalpart 97 } 98 99 func (a *AppServiceRegistration) SetRateLimited(isRateLimited bool) { 100 a.RateLimited = isRateLimited 101 } 102 103 func (a *AppServiceRegistration) AddRegexPattern(NSType string, regexString string, exclusive bool) error { 104 switch NSType { 105 case "users": 106 regex, err := regexp.Compile(regexString) 107 if err != nil { 108 return err 109 } 110 regexObjectStruct := RegexObject{Exclusive: exclusive, Regex: regex} 111 a.Namespaces.Users = append(a.Namespaces.Users, regexObjectStruct) 112 case "aliases": 113 regex, err := regexp.Compile(regexString) 114 if err != nil { 115 return err 116 } 117 regexObjectStruct := RegexObject{Exclusive: exclusive, Regex: regex} 118 a.Namespaces.Aliases = append(a.Namespaces.Aliases, regexObjectStruct) 119 case "rooms": 120 regex, err := regexp.Compile(regexString) 121 if err != nil { 122 return err 123 } 124 regexObjectStruct := RegexObject{Exclusive: exclusive, Regex: regex} 125 a.Namespaces.Rooms = append(a.Namespaces.Rooms, regexObjectStruct) 126 default: 127 log.Panicln("'NSType' must be 'users', 'rooms' or 'aliases'") 128 } 129 return nil 130 } 131 132 func (a *AppServiceRegistration) OutputAsYaml(filename string) error { 133 data, DataErr := a.getOutput(filename) 134 if DataErr != nil { 135 return DataErr 136 } 137 138 f, CreateErr := os.Create(filename) 139 if CreateErr != nil { 140 return CreateErr 141 } 142 defer f.Close() 143 144 w := bufio.NewWriter(f) 145 146 _, WriteErr := w.Write(data) 147 if WriteErr != nil { 148 return WriteErr 149 } 150 151 w.Flush() 152 153 return nil 154 } 155 156 func (a *AppServiceRegistration) isUserMatch(userId string, onlyExclusive bool) bool { 157 return a.isMatch(a.Namespaces.Users, userId, onlyExclusive) 158 } 159 160 func (a *AppServiceRegistration) isAliasMatch(alias string, onlyExclusive bool) bool { 161 return a.isMatch(a.Namespaces.Aliases, alias, onlyExclusive) 162 } 163 164 func (a *AppServiceRegistration) isRoomMatch(roomId string, onlyExclusive bool) bool { 165 return a.isMatch(a.Namespaces.Rooms, roomId, onlyExclusive) 166 } 167 168 func (a *AppServiceRegistration) getOutput(filename string) ([]byte, error) { 169 if a.Id == "" || a.HsToken == "" || a.AsToken == "" || a.Url == "" || a.SenderLocalpart == "" { 170 log.Fatalln("Missing required field(s): id, hsToken, asToken, url, senderLocalpart") 171 } 172 173 data, err := yaml.Marshal(a) 174 if err != nil { 175 return nil, err 176 } 177 178 return data, nil 179 } 180 181 func (a *AppServiceRegistration) isMatch(regexList []RegexObject, sample string, onlyExclusive bool) bool { 182 for _, regex := range regexList { 183 if regex.Regex.MatchString(sample) { 184 if onlyExclusive && !regex.Exclusive { 185 continue 186 } 187 return true 188 } 189 } 190 return false 191 }