aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: a5b4394430bc6bb1ae8337d7c0957c301f7f63ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main

import (
	"context"
	"crypto/rand"
	"encoding/hex"
	"encoding/json"
	"flag"
	"io/ioutil"
	"os"
	"os/signal"
	"syscall"
	"time"

	log "github.com/sirupsen/logrus"
	"gopkg.in/yaml.v2"

	"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
)

type ConfigFile struct {
	LogLevel string `json:"log_level"`

	Registration string `json:"registration"`
	ASBindAddr   string `json:"appservice_bind_addr"`
	Server       string `json:"homeserver_url"`
	MatrixDomain string `json:"matrix_domain"`
	NameFormat   string `json:"name_format"`

	WebBindAddr string `json:"web_bind_addr"`
	WebURL      string `json:"web_url"`
	SessionKey  string `json:"web_session_key"`

	DbType string `json:"db_type"`
	DbPath string `json:"db_path"`

	AvatarFile string `json:"easybridge_avatar"`
}

var configFlag = flag.String("config", "./config.json", "Configuration file path")

var config *ConfigFile
var registration *mxlib.Registration

func readConfig() ConfigFile {
	defaultKey := make([]byte, 32)
	rand.Read(defaultKey)

	config_file := ConfigFile{
		LogLevel:     "info",
		ASBindAddr:   "0.0.0.0:8321",
		WebBindAddr:  "0.0.0.0:8281",
		Registration: "./registration.yaml",
		Server:       "http://localhost:8008",
		NameFormat:   "{}_ezbr_",
		DbType:       "sqlite3",
		DbPath:       "easybridge.db",
		AvatarFile:   "./easybridge.jpg",
		SessionKey:   hex.EncodeToString(defaultKey),
	}

	_, err := os.Stat(*configFlag)
	if os.IsNotExist(err) {
		// Generate default config file
		log.Printf("Generating default config file as %s", *configFlag)

		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 {
		log.Fatal(err)
	}

	bytes, err := ioutil.ReadFile(*configFlag)
	if err != nil {
		log.Fatal(err)
	}

	err = json.Unmarshal(bytes, &config_file)
	if err != nil {
		log.Fatal(err)
	}

	return config_file
}

func readRegistration(file string) mxlib.Registration {
	rnd := make([]byte, 64)
	n, err := rand.Read(rnd)
	if err != nil || n != 64 {
		log.Fatal(err)
	}

	reg := mxlib.Registration{
		Id:              "Easybridge",
		Url:             "http://localhost:8321",
		AsToken:         hex.EncodeToString(rnd[:32]),
		HsToken:         hex.EncodeToString(rnd[32:]),
		SenderLocalpart: "_ezbr_",
		RateLimited:     false,
		Namespaces: mxlib.RegistrationNamespaceSet{
			Users: []mxlib.RegistrationNamespace{
				mxlib.RegistrationNamespace{
					Exclusive: true,
					Regex:     "@.*_ezbr_",
				},
			},
			Aliases: []mxlib.RegistrationNamespace{
				mxlib.RegistrationNamespace{
					Exclusive: true,
					Regex:     "#.*_ezbr_",
				},
			},
			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
	}

	if err != nil {
		log.Fatal(err)
	}

	bytes, err := ioutil.ReadFile(file)
	if err != nil {
		log.Fatal(err)
	}

	err = yaml.Unmarshal(bytes, &reg)
	if err != nil {
		log.Fatal(err)
	}

	return reg
}

func main() {
	flag.Parse()

	// Read configuration
	config_file := readConfig()
	config = &config_file

	log_level, err := log.ParseLevel(config.LogLevel)
	if err != nil {
		log.Fatal(err)
	}
	log.SetLevel(log_level)

	reg_file := readRegistration(config.Registration)
	registration = &reg_file

	// Start appservice and web management interface
	errch := make(chan error)
	sigch := make(chan os.Signal)
	signal.Notify(sigch, os.Interrupt, syscall.SIGTERM)

	as_server, err := StartAppService(errch)
	if err != nil {
		log.Fatal(err)
	}

	web_server := StartWeb(errch)

	// Wait for an error somewhere or interrupt signal
	select {
	case err = <-errch:
		if err != nil {
			log.Error(err)
		}
	case sig := <-sigch:
		log.Warnf("Got signal %s", sig.String())
	}

	// Shut down, hopefully this is not a too bad way to do it
	log.Warn("Shuttind down")
	delay := 2 * time.Second

	ctx1, _ := context.WithTimeout(context.TODO(), delay)
	go as_server.Shutdown(ctx1)

	ctx2, _ := context.WithTimeout(context.TODO(), delay)
	go web_server.Shutdown(ctx2)

	time.Sleep(delay)
	CloseAllAcountsForShutdown()

	if err != nil {
		os.Exit(1)
	} else {
		os.Exit(0)
	}
}