-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
254 lines (227 loc) · 6.34 KB
/
Copy pathindex.js
File metadata and controls
254 lines (227 loc) · 6.34 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
var bot = require("sockbot");
var botVersion = bot.version;
var botConfigHelper = require('./node_modules/sockbot/config');
var Path = require('path');
var Hapi = require('hapi');
var storage = require('node-persist');
var bcrypt = require('bcryptjs');
var Basic = require('hapi-auth-basic');
var currCommand = "";
var server, stdin, botRunning = false;
currConfig = "example.config.yml";
module.exports = {
init: function() {
stdin = process.openStdin();
stdin.setEncoding( 'utf8' );
stdin.on('data', module.exports.processInput);
storage.initSync();
//DEfault username password
if (!storage.getItem('pass')) {
storage.setItem('user',"SockMon");
storage.setItem('pass', bcrypt.hashSync('Password'));
module.exports.respond("WARNING! You have not configured a username and password. It has been set to SockMon/Password. PLEASE CHANGE THIS.");
}
this.respond("Welcome to SockMon! You are currently configured to use the default config. If you want to get started, type 'start'. To change the config file, try 'config'. To exit, type 'exit'. ");
},
respond: function(text) {
console.log(text);
},
processInput: function(data) {
data = data.toString().trim();
if (!currCommand) {
if (data.toLowerCase() === "start") {
if (!server) module.exports.startServer(module.exports.respond);
else module.exports.respond("Server already listening on port 8000!");
}
else if (data.toLowerCase() === "config" || data.toLowerCase() === "set config") {
currCommand = "config";
module.exports.respond("Enter config file: ");
}
else if (data.toLowerCase() === "stop") {
if (!server) module.exports.respond("Server is not running!");
else module.exports.stopServer(module.exports.respond);
}
else if (data.toLowerCase() === "pause") {
if (!botRunning) module.exports.respond("Bot is not running!");
else module.exports.stopBot(module.exports.respond);
}
else if (data.toLowerCase() === "resume") {
if (botRunning) module.exports.respond("Bot is already running!");
else module.exports.startBot(module.exports.respond);
}
else if (data.toLowerCase() === "exit") {
module.exports.respond("Goodbye!");
process.exit();
}
else if(data.toLowerCase() === "set") {
module.exports.respond("USAGE: Set [user|pass|config]");
}
else if(data.toLowerCase() === "set pass") {
currCommand = "pass";
module.exports.respond("Enter new password:");
}
else if(data.toLowerCase() === "set user") {
currCommand = "user";
module.exports.respond("Enter new username:");
}
else {
module.exports.respond("Unknown command: '" + data.toLowerCase() + "'")
}
}
else if (currCommand === "config") {
currConfig = data;
currCommand = "";
bot.prepare(currConfig,function(err) {
if(err) module.exports.respond("Error changing config: " + err);
else module.exports.respond("Config file accepted.");
})
}
else if (currCommand === "pass") {
storage.setItem('pass', bcrypt.hashSync(data));
currCommand = "";
module.exports.respond("Password changed.");
}
/* istanbul ignore else */
else if (currCommand === "user") {
storage.setItem('user',data);
currCommand = "";
module.exports.respond("Username changed.");
}
},
startServer: function(cb) {
module.exports.startBot(cb);
server = new Hapi.Server();
server.connection({
port: 8000
});
server.views({
engines: {
html: require('handlebars')
},
path: Path.join(__dirname, 'templates')
});
// Add the routes
server.route({
method: 'GET',
path:'/',
handler: function (request, reply) {
reply.view('index', {
version: botVersion,
username: botConfigHelper.core.username,
owner: botConfigHelper.core.owner,
forum: botConfigHelper.core.forum,
plugins: Object.keys(botConfigHelper.plugins)
});
}
});
server.register(Basic, function (err) {
//Auth for admin route
server.auth.strategy('simple', 'basic', { validateFunc: validate });
server.route({
method: 'GET',
path:'/admin/stop',
config: {
auth: 'simple',
handler: function (request, reply) {
module.exports.stopServer(reply);
}
}
});
server.route({
method: 'GET',
path:'/admin/pause',
config: {
auth: 'simple',
handler: function (request, reply) {
module.exports.stopBot(reply);
}
}
});
server.route({
method: 'GET',
path:'/admin/resume',
config: {
auth: 'simple',
handler: function (request, reply) {
module.exports.startBot(reply);
}
}
});
server.route({
method: 'GET',
path:'/admin',
config: {
auth: 'simple',
handler: function (request, reply) {
reply.view('admin', {
username: botConfigHelper.core.username,
running: botRunning
});
}
}
});
// Start the server
server.start();
/* istanbul ignore else */
if (cb) cb("Server started! You can access it at localhost:8000");
});
},
stopServer: function(cb) {
module.exports.stopBot(function(stopReply) {
/* istanbul ignore else */
if (server) {
server.stop();
server = null;
}
if (cb) cb(stopReply + ";Server stopped!");
});
},
startBot: function(cb) {
if (!cb) {cb = module.exports.respond}
bot.start(function(err){
if (err) {
cb("ERROR starting bot: " +err);
} else {
botRunning = true;
cb("bot started!");
}
});
},
stopBot: function(cb) {
if (!cb) {cb = module.exports.respond}
bot.stop(function(err){
if (err) {
cb("ERROR stopping bot: " + err);
} else {
botRunning = false;
cb("bot stopped!");
}
});
},
reset: function(cb) {
if (botRunning) {
module.exports.stopBot(function(reply) {
module.exports.reset(cb);
});
}
if (server) {
module.exports.stopServer(function(reply) {
module.exports.reset(cb);
});
}
cb();
}
}
var validate = function (request, username, password, callback) {
if (username !== storage.getItem('user')) {
return callback(null, false);
}
bcrypt.compare(password, storage.getItem('pass'), function (err, isValid) {
callback(err, isValid, {name: username});
});
};
//Autostart
/* istanbul ignore if */
if(require.main === module) {
module.exports.init();
}