Initial config structs and example config, some whatsapp testing

This commit is contained in:
Tulir Asokan
2018-08-13 01:00:23 +03:00
parent c84b5dd5dc
commit ace08205d9
7 changed files with 396 additions and 2 deletions

76
config/bridge.go Normal file
View File

@ -0,0 +1,76 @@
// mautrix-whatsapp - A Matrix-Whatsapp puppeting bridge.
// Copyright (C) 2018 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 (
"bytes"
"text/template"
)
type BridgeConfig struct {
RawUsernameTemplate string `yaml:"username_template"`
RawDisplaynameTemplate string `yaml:"displayname_template"`
UsernameTemplate *template.Template `yaml:"-"`
DisplaynameTemplate *template.Template `yaml:"-"`
}
func (bc BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
err := unmarshal(bc)
if err != nil {
return err
}
bc.UsernameTemplate, err = template.New("username").Parse(bc.RawUsernameTemplate)
if err != nil {
return err
}
bc.DisplaynameTemplate, err = template.New("displayname").Parse(bc.RawDisplaynameTemplate)
return err
}
type DisplaynameTemplateArgs struct {
Displayname string
}
type UsernameTemplateArgs struct {
Receiver string
UserID string
}
func (bc BridgeConfig) FormatDisplayname(displayname string) string {
var buf bytes.Buffer
bc.DisplaynameTemplate.Execute(&buf, DisplaynameTemplateArgs{
Displayname: displayname,
})
return buf.String()
}
func (bc BridgeConfig) FormatUsername(receiver, userID string) string {
var buf bytes.Buffer
bc.UsernameTemplate.Execute(&buf, UsernameTemplateArgs{
Receiver: receiver,
UserID: userID,
})
return buf.String()
}
func (bc BridgeConfig) MarshalYAML() (interface{}, error) {
bc.RawDisplaynameTemplate = bc.FormatDisplayname("{{.Displayname}}")
bc.RawUsernameTemplate = bc.FormatUsername("{{.Receiver}}", "{{.UserID}}")
return bc, nil
}

81
config/config.go Normal file
View File

@ -0,0 +1,81 @@
// mautrix-whatsapp - A Matrix-Whatsapp puppeting bridge.
// Copyright (C) 2018 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 (
"maunium.net/go/mautrix-appservice"
"io/ioutil"
"gopkg.in/yaml.v2"
)
type Config struct {
Homeserver struct {
Address string `yaml:"address"`
Domain string `yaml:"domain"`
} `yaml:"homeserver"`
AppService struct {
Address string `yaml:"address"`
Hostname string `yaml:"hostname"`
Port uint16 `yaml:"port"`
Database string `yaml:"database"`
ID string `yaml:"id"`
Bot struct {
Username string `yaml:"username"`
Displayname string `yaml:"displayname"`
Avatar string `yaml:"avatar"`
} `yaml:"bot"`
ASToken string `yaml:"as_token"`
HSToken string `yaml:"hs_token"`
} `yaml:"appservice"`
Bridge BridgeConfig `yaml:"bridge"`
Logging appservice.LogConfig `yaml:"logging"`
}
func Load(path string) (*Config, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var config = &Config{}
err = yaml.Unmarshal(data, config)
return config, err
}
func (config *Config) Save(path string) error {
data, err := yaml.Marshal(config)
if err != nil {
return err
}
return ioutil.WriteFile(path, data, 0600)
}
func (config *Config) Appservice() (*appservice.Config, error) {
as := appservice.Create()
as.LogConfig = config.Logging
as.HomeserverDomain = config.Homeserver.Domain
as.HomeserverURL = config.Homeserver.Address
var err error
as.Registration, err = config.GetRegistration()
return as, err
}

62
config/registration.go Normal file
View File

@ -0,0 +1,62 @@
// mautrix-whatsapp - A Matrix-Whatsapp puppeting bridge.
// Copyright (C) 2018 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 (
"maunium.net/go/mautrix-appservice"
"regexp"
)
func (config *Config) NewRegistration() (*appservice.Registration, error) {
registration := appservice.CreateRegistration("mautrix-whatsapp")
err := config.copyToRegistration(registration)
if err != nil {
return nil, err
}
config.AppService.ASToken = registration.AppToken
config.AppService.HSToken = registration.ServerToken
return registration, nil
}
func (config *Config) GetRegistration() (*appservice.Registration, error) {
registration := appservice.CreateRegistration("mautrix-whatsapp")
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
registration.RateLimited = false
registration.SenderLocalpart = config.AppService.Bot.Username
userIDRegex, err := regexp.Compile(config.Bridge.FormatUsername("[0-9]+", "[0-9]+"))
if err != nil {
return err
}
registration.Namespaces.RegisterUserIDs(userIDRegex, true)
return nil
}