-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
81 lines (74 loc) · 2.75 KB
/
index.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
"use strict";
//const Msg = require("thelounge/src/models/msg");
//const log = require("thelounge/src/log");
//const Helper = require("thelounge/src/helper");
//const Utils = require("thelounge/src/command-line/utils");
//const fs = require("fs");
//const path = require('path');
const { exec } = require("child_process");
let thelounge = null;
let stop = false;
let burst = 5;
let perSec = 200;
const execCommand = {
input: function (client, target, command, args) {
if (args.length === 0) {
client.sendMessage("Usage: /exec <command|--stop|--set-rate <burst> <per-sec>>", target.chan);
return;
}
switch(args[0]) {
case "--stop":
stop = true;
return
break;
case "--set-rate":
burst = args[1];
perSec = args[2];
break;
default:
exec(args.join(' '), (error, stdout, stderr) => {
if(error) {
client.sendMessage(`error: ${error.message}`, target.chan);
return
}
if(stderr) {
client.sendMessage(`stderr: ${stderr}`, target.chan);
return;
}
// Get first burst amount of lines
let burstText = stdout.trim().split(/[\r?\n]+/, burst);
if (burstText.length < burst) {
let tempburst = burstText.length;
for(var i = 0; i < tempburst; i++) {
client.runAsUser(burstText[i], target.chan.id);
}
tempburst = burst
}
else {
for(var i = 0; i < burst; i++) {
client.runAsUser(burstText[i], target.chan.id);
}
let restText = stdout.trim().split(/[\r\n]+/);
restText = restText.splice(burst, restText.length-1);
var it = 0;
var intObj = setInterval(() => {
client.runAsUser(restText[it++], target.chan.id);
if(stop || it == restText.length) {
stop = false;
it = 0;
clearInterval(intObj);
}
}, perSec)
}
});
break;
}
},
allowDisconnected: true
};
module.exports = {
onServerStart: api => {
thelounge = api;
thelounge.Commands.add("exec", execCommand);
},
};