-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetupwizard.js
213 lines (203 loc) · 6.83 KB
/
setupwizard.js
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
var _ = require('lodash');
var child_process = require('child_process');
var fs = require('fs');
var os = require('os');
var path = require('path');
var RUN_ARG = '--runwizard';
var SPAWN_ARG = '--spawned';
function isConfigCustomized() {
if (process.env.NODE_CONFIG || process.env.NODE_CONFIG_DIR) {
return true;
}
var configDir = path.join(__dirname, 'config');
return _.some(fs.readdirSync(configDir), function(filename) {
filename = filename.toLowerCase();
return (
!_.startsWith(filename, 'default') && (_.endsWith(filename, '.json') || _.endsWith(filename, '.js'))
);
});
}
function executeSelf() {
var wizardProc = child_process.spawnSync(
process.execPath, [__filename, RUN_ARG, SPAWN_ARG],
{
stdio: [process.stdin, process.stdout, process.stderr]
}
);
return (wizardProc.status === 0);
}
function runWizard() {
var inquirer = require('inquirer');
var Promise = require('bluebird');
function promptAsync(questions) {
return new Promise(function(resolve) {
return inquirer.prompt(questions, function(answers) {
return resolve(answers);
});
});
}
function abort() {
console.log(os.EOL + 'Aborting.');
process.exit(3);
}
console.log('This wizard will generate a basic config/local.json based ' +
'on your answers.' + os.EOL);
var config = {
api: {},
plugins: {},
services: {
config: {
firewall: {}
}
}
};
Promise.try(function() {
if (isConfigCustomized()) {
console.log('WARNING: Local configuration detected; ' +
'config/local.json will be overwritten.' + os.EOL);
return promptAsync([
{
type: 'confirm',
name: 'proceed',
message: 'Proceed anyway?',
default: false
}
]);
}
}).then(function(answers) {
if (answers && answers.proceed !== true) {
abort();
}
return promptAsync([
{
name: 'apiToken',
message: 'Telegram bot API token:',
validate: function(v) { return v.length > 0; }
},
{
type: 'confirm',
name: 'autoLoadPlugins',
message: 'Auto-detect available plugins?',
default: true
}
]);
}).then(function(answers) {
config.api.token = answers.apiToken;
if (!answers.autoLoadPlugins) {
config.plugins.loadAll = false;
config.plugins.register = [];
config.plugins.autoEnabled = [];
console.log('Note: you should specify which plugins to load and ' +
'enable in the plugins.register and ' +
'plugins.autoEnable arrays.');
}
console.log(os.EOL + 'The following questions will configure the ' +
'whitelist-based message firewall.');
console.log('The default settings won\'t block anything unless a user' +
' is explicitly banned.' + os.EOL);
var commonRules = [
{name: '[~] Owner', value: 'owner'},
{name: '[&] Administrator', value: 'administrator'},
{name: '[@] Operator', value: 'operator'},
{name: '[%] Half-operator', value: 'half-operator'},
{name: '[+] Trusted user', value: 'trusted'}
];
var groupRules = commonRules.concat([
{name: '[-] User', 'value': 'user'},
{name: '[b] Blacklisted', 'value': 'blacklisted'}
]);
var privateRules = commonRules.concat([
{name: '[!] Known (at least one group in common)', value: 'known'},
{name: '[-] Stranger', 'value': 'user'},
{name: '[b] Blacklisted', 'value': 'blacklisted'}
]);
return promptAsync([
{
type: 'list',
name: 'groupCommand',
message: 'Minimum user level to process command in group chat?',
choices: groupRules,
default: 'user'
},
{
type: 'list',
name: 'groupMessage',
message: 'Minimum user level to process regular message in ' +
'group chat?',
choices: groupRules,
default: 'blacklisted'
},
{
type: 'list',
name: 'privateCommand',
message: 'Minimum user level to process command in ' +
'private chat?',
choices: privateRules,
default: 'user'
},
{
type: 'list',
name: 'privateMessage',
message: 'Minimum user level to process regular private ' +
'message?',
choices: privateRules,
default: 'user'
}
]);
}).then(function(answers) {
config.services.config.firewall.rules = {
privateCommand: answers.privateCommand,
groupCommand: answers.groupCommand,
privateMessage: answers.privateMessage,
groupMessage: answers.groupMessage
};
process.stdout.write(os.EOL);
console.log('This concludes the setup wizard.' + os.EOL);
return promptAsync([
{
type: 'confirm',
name: 'writeConfig',
message: 'Write configuration file with these settings?',
default: true
}
]);
}).then(function(answers) {
if (answers.writeConfig !== true) {
abort();
}
process.stdout.write(os.EOL + 'Writing config file...');
fs.writeFileSync(
path.join(__dirname, 'config', 'local.json'),
JSON.stringify(config, null, 4) + "\n",
{
mode: 0644
}
);
process.stdout.write(' done.' + os.EOL);
if (!_.includes(process.argv, SPAWN_ARG)) {
process.exit(0);
} else {
process.stdout.write(os.EOL);
return promptAsync([
{
type: 'confirm',
name: 'startBot',
message: 'Proceed with starting the bot now?',
default: true
}
]);
}
}).then(function(answers) {
if (answers.startBot !== true) {
abort();
}
});
}
if (_.includes(process.argv, RUN_ARG)) {
runWizard();
} else {
module.exports = {
isConfigCustomized: isConfigCustomized,
exec: executeSelf
};
}