-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patheval.js
More file actions
63 lines (51 loc) · 1.5 KB
/
eval.js
File metadata and controls
63 lines (51 loc) · 1.5 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
var fork = require('child_process').fork;
var e = require(__dirname + '/../lib/event');
var helper = require(__dirname + '/../lib/helper');
e.on('command.probe.eval', function(callback){
callback(null, {
name : 'eval',
version : '0.0.1',
description : 'Roda códigos javascript.'
});
});
e.on('command.exec.eval', function(args, nick, raw){
var str = raw.substring(6);
var terminado = false;
/*
* Tem que melhorar isso, se ouver uma string com @
* no meio dela, vai ferrar tudo. Talvez pesquisar na
* lista de usuários pra ver se o nick existe mesmo...
*/
if (str.indexOf('@') !== -1) {
nick = str.slice(str.indexOf('@') + 1);
}
/*
* Evitamos loops infinitos
*/
var timer = setTimeout(function(){
helper.say(nick + ', o comando demorou para terminar.');
try {
vm.kill();
} catch (err) {
// provavelmente a vm já saiu...
}
}, 2000);
/*
* Enviar apenas uma msg e terminar o que temos pra fazer aqui..
*/
var terminar = function(msg){
if(terminado)
return;
terminado = true;
clearTimeout(timer);
helper.say(msg);
};
var vm = fork(__dirname + '/../resource/vm-eval.js');
vm.on('message', function(msg) {
if(msg.error)
terminar(nick + ', ocorreu um erro: ' + msg.error);
if(msg.result)
terminar(nick + ', ' + msg.result);
});
vm.send(str);
});