This repository has been archived by the owner on Aug 1, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
generator.go
216 lines (197 loc) · 5.88 KB
/
generator.go
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
// Copyright (c) 2019 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package appservice
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/fatih/color"
)
func readString(reader *bufio.Reader, message, defaultValue string) (string, error) {
color.Green(message)
if len(defaultValue) > 0 {
fmt.Printf("[%s]", defaultValue)
}
fmt.Print("> ")
val, err := reader.ReadString('\n')
if err != nil {
return "", err
}
val = strings.TrimSuffix(val, "\n")
if len(val) == 0 {
return defaultValue, nil
}
val = strings.TrimSuffix(val, "\r")
if len(val) == 0 {
return defaultValue, nil
}
return val, nil
}
const (
yes = "yes"
yesShort = "y"
)
// GenerateRegistration asks the user questions and generates a config and registration based on the answers.
func GenerateRegistration(asName, botName string, reserveRooms, reserveUsers bool) {
var boldCyan = color.New(color.FgCyan).Add(color.Bold)
var boldGreen = color.New(color.FgGreen).Add(color.Bold)
boldCyan.Println("Generating appservice config and registration.")
reader := bufio.NewReader(os.Stdin)
registration := CreateRegistration()
config := Create()
registration.RateLimited = false
name, err := readString(reader, "Enter name for appservice", asName)
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
registration.ID = name
registration.SenderLocalpart, err = readString(reader, "Enter bot username", botName)
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
asProtocol, err := readString(reader, "Enter appservice host protocol", "http")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
if asProtocol == "https" {
sslInput, err := readString(reader, "Do you want the appservice to handle SSL [yes/no]?", "yes")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
wantSSL := strings.ToLower(sslInput)
if wantSSL == yes {
config.Host.TLSCert, err = readString(reader, "Enter path to SSL certificate", "appservice.crt")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
config.Host.TLSKey, err = readString(reader, "Enter path to SSL key", "appservice.key")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
}
}
asHostname, err := readString(reader, "Enter appservice hostname", "localhost")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
asInput, err := readString(reader, "Enter appservice host port", "29313")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
asPort, convErr := strconv.Atoi(asInput)
if convErr != nil {
fmt.Println("Failed to parse port:", convErr)
return
}
registration.URL = fmt.Sprintf("%s://%s:%d", asProtocol, asHostname, asPort)
config.Host.Hostname = asHostname
config.Host.Port = uint16(asPort)
config.HomeserverURL, err = readString(reader, "Enter homeserver address", "http://localhost:8008")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
config.HomeserverDomain, err = readString(reader, "Enter homeserver domain", "example.com")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
config.LogConfig.Directory, err = readString(reader, "Enter directory for logs", "./logs")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
os.MkdirAll(config.LogConfig.Directory, 0755)
if reserveRooms || reserveUsers {
for {
namespace, err := readString(reader, "Enter namespace prefix", fmt.Sprintf("_%s_", name))
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
roomNamespaceRegex, err := regexp.Compile(fmt.Sprintf("#%s.+:%s", namespace, config.HomeserverDomain))
if err != nil {
fmt.Println(err)
continue
}
userNamespaceRegex, regexpErr := regexp.Compile(fmt.Sprintf("@%s.+:%s", namespace, config.HomeserverDomain))
if regexpErr != nil {
fmt.Println("Failed to generate regexp for the userNamespace:", err)
return
}
if reserveRooms {
registration.Namespaces.RegisterRoomAliases(roomNamespaceRegex, true)
}
if reserveUsers {
registration.Namespaces.RegisterUserIDs(userNamespaceRegex, true)
}
break
}
}
boldCyan.Println("\n==== Registration generated ====")
yamlString, yamlErr := registration.YAML()
if err != nil {
fmt.Println("Failed to return the registration Config:", yamlErr)
return
}
color.Yellow(yamlString)
okInput, readErr := readString(reader, "Does the registration look OK [yes/no]?", "yes")
if readErr != nil {
fmt.Println("Failed to read user Input:", readErr)
return
}
ok := strings.ToLower(okInput)
if ok != yesShort && ok != yes {
fmt.Println("Cancelling generation.")
return
}
path, err := readString(reader, "Where should the registration be saved?", "registration.yaml")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
err = registration.Save(path)
if err != nil {
fmt.Println("Failed to save registration:", err)
return
}
boldGreen.Println("Registration saved.")
config.RegistrationPath = path
boldCyan.Println("\n======= Config generated =======")
color.Yellow(config.YAML())
okString, err := readString(reader, "Does the config look OK [yes/no]?", "yes")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
ok = strings.ToLower(okString)
if ok != yesShort && ok != yes {
fmt.Println("Cancelling generation.")
return
}
path, err = readString(reader, "Where should the config be saved?", "config.yaml")
if err != nil {
fmt.Println("Failed to read user Input:", err)
return
}
err = config.Save(path)
if err != nil {
fmt.Println("Failed to save config:", err)
return
}
boldGreen.Println("Config saved.")
}