-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.js
156 lines (134 loc) · 4.27 KB
/
config.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
var bot = module.parent.exports;
function loadConfigurationFile(filename = process.argv[2]){
return new Promise(function(ok, fail){
if(filename == undefined){
fail("No configuration file");
} else {
bot.fs.readFile(filename, {encoding: 'utf-8'}, function(err,data){
if (err){
fail(err.toString());
} else {
var config_params = data.split("\n");
var hasRoot = 0;
var last_string = "must be empty";
for(var i = 0; i < config_params.length; i++){
last_string = config_params[i];
if(config_params[i] != ""){
var param = config_params[i].split(bot.init.DELIMETER);
if(param.length < 3)
fail("Incorrect configuration file");
/* initialize menu */
if(bot.init.menu[param[1]] == undefined)
bot.init.menu[param[1]] = { 'values': [], 'parrent': null, 'actions': [] };
bot.init.menu[param[1]].values.push(param[2]);
bot.init.menu[param[1]].parrent = param[0];
/* initialize actions in menu */
if(bot.init.menu[param[2]] == undefined)
bot.init.menu[param[2]] = { 'values': [], 'parrent': param[1], 'actions': [] };
if(param.length > 3){
for(var j = 3; j < param.length; j++){
try{
bot.init.menu[param[2]].actions.push(JSON.parse(param[j]));
} catch(e) {
fail("Incorrect configuration file at \"" + param[2] + "\" actions (string number " + (i+1) + ")");
}
}
}
/* initialize buttons */
if(bot.init.buttons[param[0]] == undefined){
bot.init.buttons[param[0]] = [];
var obj = {};
obj[param[1]] = [];
bot.init.buttons[param[0]].push(obj);
}
var flagKey = -1;
for(var j = 0; j < bot.init.buttons[param[0]].length; j++){
var keys = Object.keys(bot.init.buttons[param[0]][j]);
for(var jj = 0; jj < keys.length; jj++){
if(keys[jj] == param[1]){
flagKey = j;
}
}
}
if(flagKey == -1){
var obj = {};
obj[param[1]] = [];
bot.init.buttons[param[0]].push(obj);
flagKey = bot.init.buttons[param[0]].length - 1;
}
bot.init.buttons[param[0]][flagKey][param[1]].push(param[2]);
/* check root parrent */
if(param[0] == "root"){
hasRoot = 1;
bot.init.menu_root = param[1];
}
}
}
if(!hasRoot)
fail("No root catalog in menu at parrent position");
checkEmptyStringInConfig(last_string, filename).then(() => {
ok();
}, error => {
fail(error);
});
}
});
}
});
}
module.exports.loadConfigurationFile = loadConfigurationFile;
function checkEmptyStringInConfig(str, filename){
return new Promise(function(ok, fail){
if(str != ""){
// add empty string
bot.exec("echo >> " + filename, function(error, out, err){
if(error != null)
fail("Error when add empty string to end of config file: " + error.toString());
if(err != "")
fail("Error when add empty string to end of config file: " + err.toString());
ok();
});
} else {
ok();
}
});
}
function getDataFolder(){
return new Promise(function(ok, fail){
if(process.argv[3] == undefined)
fail("No data folder");
bot.fs.exists(process.argv[3], function (exists) {
if(!exists)
fail("Data folder \""+process.argv[3]+"\" doesn't exist, create it yourself");
bot.init.DATA_FOLDER = process.argv[3];
ok();
});
});
}
module.exports.getDataFolder = getDataFolder;
function checkMode(){
return new Promise(function(ok, fail){
if(process.argv[4] != undefined){
if(process.argv[4] != "edit"){
fail("Unknown mode");
} else {
bot.init.MODE = 1;
// Check Admins
bot.init.admins = getAdmins();
}
}
ok();
});
}
module.exports.checkMode = checkMode;
function getAdmins(){
var admins = [];
if(process.argv[5] != undefined && process.argv[4] != "all"){
admins = process.argv[5].split(",");
for(var i = 0; i < admins.length; i++){
if(admins[i][0] == "@")
admins[i] = admins[i].slice(1);
}
}
return admins;
}