easybridge/main.go

184 lines
3.8 KiB
Go
Raw Normal View History

package main
import (
2020-02-16 18:30:49 +00:00
"crypto/rand"
"encoding/hex"
2020-02-17 18:02:26 +00:00
"encoding/json"
2020-02-16 18:30:49 +00:00
"flag"
2020-02-17 18:02:26 +00:00
"fmt"
_ "fmt"
2020-02-16 18:30:49 +00:00
"io/ioutil"
"os"
2020-02-17 18:02:26 +00:00
_ "strings"
2020-02-16 18:30:49 +00:00
_ "time"
2020-02-17 08:41:08 +00:00
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
2020-02-17 18:02:26 +00:00
"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
)
2020-02-16 18:30:49 +00:00
type ConfigAccount struct {
2020-02-17 18:02:26 +00:00
Protocol string `json:"protocol"`
Config map[string]string `json:"config"`
}
2020-02-16 18:30:49 +00:00
type ConfigFile struct {
2020-02-26 15:30:10 +00:00
ASBindAddr string `json:"appservice_bind_addr"`
2020-02-26 16:45:25 +00:00
WebBindAddr string `json:"web_bind_addr"`
2020-02-17 18:02:26 +00:00
Registration string `json:"registration"`
Server string `json:"homeserver_url"`
DbType string `json:"db_type"`
DbPath string `json:"db_path"`
MatrixDomain string `json:"matrix_domain"`
Accounts map[string]map[string]ConfigAccount `json:"accounts"`
}
2020-02-16 18:30:49 +00:00
var configFlag = flag.String("config", "./config.json", "Configuration file path")
2020-02-16 18:30:49 +00:00
var config *ConfigFile
var registration *mxlib.Registration
2020-02-16 18:30:49 +00:00
func readConfig() ConfigFile {
config_file := ConfigFile{
2020-02-26 15:30:10 +00:00
ASBindAddr: "0.0.0.0:8321",
2020-02-26 16:45:25 +00:00
WebBindAddr: "0.0.0.0:8281",
2020-02-16 18:30:49 +00:00
Registration: "./registration.yaml",
2020-02-17 18:02:26 +00:00
Server: "http://localhost:8008",
DbType: "sqlite3",
DbPath: "easybridge.db",
Accounts: map[string]map[string]ConfigAccount{},
}
2020-02-16 18:30:49 +00:00
_, err := os.Stat(*configFlag)
if os.IsNotExist(err) {
// Generate default config file
log.Printf("Generating default config file as %s", *configFlag)
2020-02-16 18:30:49 +00:00
bytes, err := json.MarshalIndent(&config_file, "", " ")
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(*configFlag, bytes, 0644)
if err != nil {
log.Fatal(err)
}
return config_file
}
if err != nil {
2020-02-16 18:30:49 +00:00
log.Fatal(err)
}
2020-02-16 18:30:49 +00:00
bytes, err := ioutil.ReadFile(*configFlag)
if err != nil {
2020-02-16 18:30:49 +00:00
log.Fatal(err)
}
2020-02-16 18:30:49 +00:00
err = json.Unmarshal(bytes, &config_file)
if err != nil {
2020-02-16 18:30:49 +00:00
log.Fatal(err)
}
2020-02-16 18:30:49 +00:00
return config_file
}
2020-02-16 16:53:31 +00:00
2020-02-16 18:30:49 +00:00
func readRegistration(file string) mxlib.Registration {
rnd := make([]byte, 64)
n, err := rand.Read(rnd)
if err != nil || n != 64 {
log.Fatal(err)
2020-02-16 16:53:31 +00:00
}
2020-02-16 18:30:49 +00:00
reg := mxlib.Registration{
2020-02-17 18:02:26 +00:00
Id: "Easybridge",
Url: "http://localhost:8321",
AsToken: hex.EncodeToString(rnd[:32]),
HsToken: hex.EncodeToString(rnd[32:]),
2020-02-17 14:30:01 +00:00
SenderLocalpart: "_ezbr_",
2020-02-23 19:24:50 +00:00
RateLimited: false,
2020-02-16 18:30:49 +00:00
Namespaces: mxlib.RegistrationNamespaceSet{
Users: []mxlib.RegistrationNamespace{
mxlib.RegistrationNamespace{
Exclusive: true,
2020-02-17 18:02:26 +00:00
Regex: "@_ezbr_.*",
2020-02-16 18:30:49 +00:00
},
},
Aliases: []mxlib.RegistrationNamespace{
mxlib.RegistrationNamespace{
Exclusive: true,
2020-02-17 18:02:26 +00:00
Regex: "#_ezbr_.*",
2020-02-16 18:30:49 +00:00
},
},
Rooms: []mxlib.RegistrationNamespace{},
},
}
_, err = os.Stat(file)
if os.IsNotExist(err) {
// Generate default config file
log.Printf("Generating default registration file as %s", file)
bytes, err := yaml.Marshal(&reg)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(file, bytes, 0644)
if err != nil {
log.Fatal(err)
}
return reg
2020-02-16 16:53:31 +00:00
}
if err != nil {
2020-02-16 18:30:49 +00:00
log.Fatal(err)
2020-02-16 16:53:31 +00:00
}
2020-02-16 18:30:49 +00:00
bytes, err := ioutil.ReadFile(file)
2020-02-16 16:53:31 +00:00
if err != nil {
2020-02-16 18:30:49 +00:00
log.Fatal(err)
2020-02-16 16:53:31 +00:00
}
2020-02-16 18:30:49 +00:00
err = yaml.Unmarshal(bytes, &reg)
2020-02-16 16:53:31 +00:00
if err != nil {
2020-02-16 18:30:49 +00:00
log.Fatal(err)
2020-02-16 16:53:31 +00:00
}
2020-02-16 18:30:49 +00:00
return reg
2020-02-16 16:53:31 +00:00
}
func main() {
2020-02-17 08:41:08 +00:00
log.SetLevel(log.DebugLevel)
2020-02-16 18:30:49 +00:00
flag.Parse()
config_file := readConfig()
config = &config_file
reg_file := readRegistration(config.Registration)
registration = &reg_file
2020-02-26 15:07:33 +00:00
errch, err := StartAppService()
2020-02-16 18:30:49 +00:00
if err != nil {
log.Fatal(err)
}
2020-02-26 16:45:25 +00:00
StartWeb()
2020-02-16 18:30:49 +00:00
for user, accounts := range config.Accounts {
2020-02-26 20:36:35 +00:00
mxid := fmt.Sprintf("@%s:%s", user, config.MatrixDomain)
2020-02-16 18:30:49 +00:00
for name, params := range accounts {
2020-02-26 20:36:35 +00:00
SetAccount(mxid, name, params.Protocol, params.Config)
2020-02-16 18:30:49 +00:00
}
}
err = <-errch
if err != nil {
log.Fatal(err)
}
}