matrix-appservice-go

git clone git://archive.git.mtrnord.blog/MTRNord/matrix-appservice-go.git
Log | Files | Refs | README

commit e8c991ea5e96a5292e6a8e24ff7adae283d3e4d5
parent c436f57df630111c7fafac883019ff77f455eac1
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sat, 11 Nov 2017 14:22:04 +0100

Add multiple functions for the registration object and move to packages

Signed-off-by: MTRNord <mtrnord1@gmail.com>

Diffstat:
Dapp-service-registration.go | 2--
Aregistration/app-service-registration.go | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Autils/utils.go | 40++++++++++++++++++++++++++++++++++++++++
3 files changed, 161 insertions(+), 2 deletions(-)

diff --git a/app-service-registration.go b/app-service-registration.go @@ -1,2 +0,0 @@ -package matrix_appservice_go - diff --git a/registration/app-service-registration.go b/registration/app-service-registration.go @@ -0,0 +1,121 @@ +package registration + +import ( + "github.com/MTRNord/matrix-appservice-go/utils" + "log" + "regexp" +) + +type RegexObject struct { + exclusive bool + regex *regexp.Regexp +} + +type Namespaces struct { + users []RegexObject + aliases []RegexObject + rooms []RegexObject +} + +// Something is the structure we work with +type AppServiceRegistration struct { + url string + id string + hsToken string + asToken string + senderLocalpart string + rateLimited bool + Namespaces + protocols []string + cachedRegex string +} + +// NewSomething create new instance of Something +func NewAppServiceRegistration(appServiceUrl string) AppServiceRegistration { + AppServiceRegistrationStruct := AppServiceRegistration { + url: appServiceUrl, + rateLimited: true, + } + return AppServiceRegistrationStruct +} + +func GenerateToken() string { + return utils.RandomString(32) +} + +func (a *AppServiceRegistration) SetAppServiceUrl(url string) { + a.url = url +} + +func (a *AppServiceRegistration) SetID(id string) { + a.id = id +} + +func (a *AppServiceRegistration) GetID() string { + return a.id +} + +func (a *AppServiceRegistration) SetProtocols(protocols []string) { + a.protocols = protocols +} + +func (a *AppServiceRegistration) GetProtocols() []string { + return a.protocols +} + +func (a *AppServiceRegistration) SetHomeserverToken (token string) { + a.hsToken = token +} + +func (a *AppServiceRegistration) GetHomeserverToken() string { + return a.hsToken +} + +func (a *AppServiceRegistration) SetAppServiceToken(token string) { + a.asToken = token +} + +func (a *AppServiceRegistration) GetAppServiceToken() string { + return a.asToken +} + +func (a *AppServiceRegistration) SetSenderLocalpart(localpart string) { + a.senderLocalpart = localpart +} + +func (a *AppServiceRegistration) GetSenderLocalpart() string { + return a.senderLocalpart +} + +func (a *AppServiceRegistration) SetRateLimited(isRateLimited bool) { + a.rateLimited = isRateLimited +} + +func (a *AppServiceRegistration) AddRegexPattern(NSType string, regexString string, exclusive bool) error { + switch NSType { + case "users": + regex, err := regexp.Compile(regexString) + if err != nil { + return err + } + regexObjectStruct := RegexObject{exclusive: exclusive, regex: regex} + a.Namespaces.users = append(a.Namespaces.users, regexObjectStruct) + case "aliases": + regex, err := regexp.Compile(regexString) + if err != nil { + return err + } + regexObjectStruct := RegexObject{exclusive: exclusive, regex: regex} + a.Namespaces.aliases = append(a.Namespaces.aliases, regexObjectStruct) + case "rooms": + regex, err := regexp.Compile(regexString) + if err != nil { + return err + } + regexObjectStruct := RegexObject{exclusive: exclusive, regex: regex} + a.Namespaces.rooms = append(a.Namespaces.rooms, regexObjectStruct) + default: + log.Panicln("'NSType' must be 'users', 'rooms' or 'aliases'") + } + return nil +} diff --git a/utils/utils.go b/utils/utils.go @@ -0,0 +1,39 @@ +package utils + +import ( + "crypto/rand" + "log" +) + +const ( + letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 52 possibilities + letterIdxBits = 6 // 6 bits to represent 64 possibilities / indexes + letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits +) + +func RandomString(length int) string { + + result := make([]byte, length) + bufferSize := int(float64(length)*1.3) + for i, j, randomBytes := 0, 0, []byte{}; i < length; j++ { + if j%bufferSize == 0 { + randomBytes = SecureRandomBytes(bufferSize) + } + if idx := int(randomBytes[j%length] & letterIdxMask); idx < len(letterBytes) { + result[i] = letterBytes[idx] + i++ + } + } + + return string(result) +} + +// SecureRandomBytes returns the requested number of bytes using crypto/rand +func SecureRandomBytes(length int) []byte { + var randomBytes = make([]byte, length) + _, err := rand.Read(randomBytes) + if err != nil { + log.Fatal("Unable to generate random bytes") + } + return randomBytes +} +\ No newline at end of file