-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (113 loc) · 5.41 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
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
// cvmBotJS by Gunawan092w [https://github.com/gunawan092w/cvmbotJS]
const WSClient = require('websocket').client;
const toml = require('toml');
const fs = require('node:fs');
const path = require('node:path');
const rl = require('readline');
const Permissions = require('./files/utils/Permissions.js');
const {encode, decode} = require('./files/utils/Guacutils.js');
const config = toml.parse(fs.readFileSync('./config.toml', 'utf-8'));
const prefix = config.bot.prefix;
const Client = new WSClient();
// Loads all commands in commands folder
let command = {};
fs.readdirSync('./files/commands').forEach(file => {
if (file.endsWith('.js')) {
const cmdName = path.basename(file, '.js');
command[cmdName] = require(path.join(__dirname, 'files', 'commands', file));
}
});
let readline;
let permissions;
function bot() {
Client.removeAllListeners(); // Removes remaining listener
Client.on('connectFailed',function(){reconnect()});
Client.on('error',function(error){console.log("An error has accoured.){ " + error.toString())});
Client.on('close',function(){reconnect()});
readline = rl.createInterface({input: process.stdin, output: process.stdout});
Client.on('connect', client => {
function send(msg){client.sendUTF(msg)} // Added in v2.0.3 - easier rather than typing client.sendUTF
console.log(`Connected to VM!`); // Success!
send(encode(['rename',config.bot.user])); // Bot sets Username
send(encode(['list'])); // It appears this were not pushed to github (v2.0.6).
function chat(message){send(encode(['chat',message]))}; // Chat
function xss(message){send(encode(["admin","21",message]))}; // XSS
function chatsession() {
readline.question('Chat: ',(msg)=>{
if (botrole==="mod") {
if (permissions.xss){xss(msg)} else(chat(msg));
} else if (botrole==="admin"){xss(msg)} else{chat(msg)};
chatsession(); // In loop.
})
}; chatsession();
// Check if bot has been disallowed to login
let botrole;
if (config.settings.login==="false"){
console.log("Not logging in as mod / admin."); botrole="null"
} else{send(encode(['admin','2',config.bot.login]))} // If not, Bot proceeds to log in
if (config.settings.startup==="true") {if(client.connected){chat(config.settings.startupmsg)}}; // Startup Message. Enable/Disable in Config.
client.on('message', message =>{
const cmd = decode(message.utf8Data);
const action = cmd[0];
const prefix = config.bot.prefix;
if(action==="disconnect"){reconnect()}; // If Disconnect, Kill Websocket Session, Kill Chat Session and reconnects.
if(action==="nop"){send(encode(['nop']))}; // Send Heartbeat
if (action==="adduser") {
if (cmd[2]!==config.bot.user&&cmd[1]==="1") { // Bot ignored + Only filter 1 user (v2.0.4)
if (cmd[3]==='0'){console.log(`${cmd[2]} Joined!`)}; // Logs User Joins.
if (cmd[3]==='2'){console.log(`${cmd[2]} is now Administrator!`)}; // Logs User logged in as Administrator
if (cmd[3]==='3'){console.log(`${cmd[2]} is now Moderator!`)} // Logs user logged in as Moderator
};
};
if(action==="remuser"){console.log(`${cmd[2]} Left!`)}; // Logs user leaves.
if(action==="rename"&&cmd[1]==='1'){console.log(`${cmd[2]} Renamed to ${cmd[3]}`)}; // Detect if user renames
// Detects if the bot is logged in as admin / mod or fails to login.
if (action==="admin") {
if (cmd[2]==='0'){console.log("Incorrect login password!");botrole = "null"};
if (cmd[2]==='1'){console.log("Logged in as Administrator!");botrole = "admin"};
if (cmd[2]==='3'){
console.log("Logged in as Moderator!"); botrole = "mod";
permissions = new Permissions(cmd[3]); // Check Moderator Permissions
console.log(permissions); // Outputs Moderator Permissions [true/false] (as JSON)
module.exports = {permissions}; // Can be used for other admin / mod commands to check the bot's permission.
}
module.exports = {botrole}; // xsstest was not working when the bot is administrator.
}
if(action==="chat"){
if(cmd[2] !=="") {
// https://github.com/computernewb/collabvm-1.2.ts/blob/master/cvmts/src/Utilities.ts Line 26
const decodedmsg = cmd[2]
.replace(/'/g, "'") .replace(/"/g, '"')
.replace(///g, "/") .replace( /</g, "<" )
.replace( />/g, ">" ) .replace( /&/g, "&");
console.log(`${cmd[1]} says: ${decodedmsg}`); // Logs user message
} else {}; // Ignores if the message is empty.
const cmdName = cmd[2].slice(prefix.length).trim().split(' ')[0];
if(cmd[1]!==config.bot.user){ // Ignore bot messages
if (cmd[2].startsWith(prefix) && command[cmdName]){
if (command[cmdName].execute) {
command[cmdName].execute(chat, xss, cmd);
}else { chat(`It looks like ${cmdName} doesn't have 'execute' property set!`) };
};
};
};
if (action==='list') { // For vminfo command. You may remove this code if you don't want this command.
const vmname = cmd[1];
const vmdesc = cmd[2];
module.exports = {vmname, vmdesc};
};
});
});
Client.connect(config.bot.ip, 'guacamole'); // Bot connects to VM
}
console.log('Connecting to VM...');
bot();
function reconnect() {
if (readline) { readline.close(); readline = null; } // Kills the chat session
console.log("Reconnecting..."); bot(); // Calls the bot() again
}
// Catch the CTRL+C !!!!
process.on('SIGINT', () => {
console.log('\nKilling Bot Session...');
if (readline) readline.close(); process.exit(0);
});