@@ -4,13 +4,11 @@ const WSClient = require('websocket').client;
4
4
const toml = require ( 'toml' ) ;
5
5
const fs = require ( 'node:fs' ) ;
6
6
const path = require ( 'node:path' ) ;
7
- const rl = require ( 'readline' ) ;
8
7
9
8
const Permissions = require ( './files/utils/Permissions.js' ) ;
10
9
const { encode, decode} = require ( './files/utils/Guacutils.js' ) ;
11
10
12
11
const config = toml . parse ( fs . readFileSync ( './config.toml' , 'utf-8' ) ) ;
13
- const prefix = config . bot . prefix ;
14
12
const Client = new WSClient ( ) ;
15
13
16
14
// Loads all commands in commands folder
@@ -26,98 +24,96 @@ let readline;
26
24
let permissions ;
27
25
function bot ( ) {
28
26
Client . removeAllListeners ( ) ; // Removes remaining listener
29
-
30
- Client . on ( 'connectFailed' , function ( ) { reconnect ( ) } ) ;
31
- Client . on ( 'error' , function ( error ) { console . log ( "An error has accoured.){ " + error . toString ( ) ) } ) ;
32
- Client . on ( 'close' , function ( ) { reconnect ( ) } ) ;
33
-
34
- readline = rl . createInterface ( { input : process . stdin , output : process . stdout } ) ;
27
+ readline = require ( 'readline' ) . createInterface ( { input : process . stdin , output : process . stdout } ) ;
35
28
29
+ Client . on ( 'connectFailed' , reconnect ) ;
36
30
Client . on ( 'connect' , client => {
37
- function send ( msg ) { client . sendUTF ( msg ) } // Added in v2.0.3 - easier rather than typing client.sendUTF
38
-
39
- console . log ( `Connected to VM!` ) ; // Success!
40
- send ( encode ( [ 'rename' , config . bot . user ] ) ) ; // Bot sets Username
41
- send ( encode ( [ 'list' ] ) ) ; // It appears this were not pushed to github (v2.0.6).
42
-
43
- function chat ( message ) { send ( encode ( [ 'chat' , message ] ) ) } ; // Chat
44
- function xss ( message ) { send ( encode ( [ "admin" , "21" , message ] ) ) } ; // XSS
45
- function chatsession ( ) {
46
- readline . question ( 'Chat: ' , ( msg ) => {
47
- if ( botrole === "mod" ) {
48
- if ( permissions . xss ) { xss ( msg ) } else ( chat ( msg ) ) ;
49
- } else if ( botrole === "admin" ) { xss ( msg ) } else { chat ( msg ) } ;
50
- chatsession ( ) ; // In loop.
51
- } )
52
- } ; chatsession ( ) ;
31
+ client . on ( 'close' , reconnect ) ;
32
+
33
+ client . on ( 'error' , error => { console . log ( `An error has accoured. {${ error . toString ( ) } }` ) } ) ;
34
+
35
+ try {
36
+ function send ( msg ) { client . sendUTF ( msg ) } // Added in v2.0.3
37
+
38
+ console . log ( `Connected to VM!` ) ; // Connection established
39
+ send ( encode ( [ 'rename' , config . bot . user ] ) ) ; // Bot sets Username
40
+
41
+ function chat ( message ) { send ( encode ( [ 'chat' , message ] ) ) } ; // Chat
42
+ function xss ( message ) { send ( encode ( [ "admin" , "21" , message ] ) ) } ; // XSS
43
+
44
+ function chatsession ( ) {
45
+ readline . question ( 'Chat: ' , ( msg ) => {
46
+ if ( botrole === "mod" ) {
47
+ if ( permissions . xss ) { xss ( msg ) } else ( chat ( msg ) ) ;
48
+ } else if ( botrole === "admin" ) { xss ( msg ) } else { chat ( msg ) } ;
49
+ chatsession ( ) ; // In loop.
50
+ } )
51
+ } ; chatsession ( ) ;
53
52
54
- // Check if bot has been disallowed to login
55
- let botrole ;
56
- if ( config . settings . login === "false" ) {
57
- console . log ( "Not logging in as mod / admin." ) ; botrole = "null"
58
- } else { send ( encode ( [ 'admin' , '2' , config . bot . login ] ) ) } // If not, Bot proceeds to log in
59
-
60
- if ( config . settings . startup === "true" ) { if ( client . connected ) { chat ( config . settings . startupmsg ) } } ; // Startup Message. Enable/Disable in Config.
61
-
62
- client . on ( 'message' , message => {
63
- const cmd = decode ( message . utf8Data ) ;
64
- const action = cmd [ 0 ] ;
65
- const prefix = config . bot . prefix ;
66
-
67
- if ( action === "disconnect" ) { reconnect ( ) } ; // If Disconnect, Kill Websocket Session, Kill Chat Session and reconnects.
68
- if ( action === "nop" ) { send ( encode ( [ 'nop' ] ) ) } ; // Send Heartbeat
69
-
70
- if ( action === "adduser" ) {
71
- if ( cmd [ 2 ] !== config . bot . user && cmd [ 1 ] === "1" ) { // Bot ignored + Only filter 1 user (v2.0.4)
72
- if ( cmd [ 3 ] === '0' ) { console . log ( `${ cmd [ 2 ] } Joined!` ) } ; // Logs User Joins.
73
- if ( cmd [ 3 ] === '2' ) { console . log ( `${ cmd [ 2 ] } is now Administrator!` ) } ; // Logs User logged in as Administrator
74
- if ( cmd [ 3 ] === '3' ) { console . log ( `${ cmd [ 2 ] } is now Moderator!` ) } // Logs user logged in as Moderator
53
+ // Check if bot has been disallowed to login
54
+ let botrole ;
55
+ if ( config . settings . login === "false" ) {
56
+ console . log ( "Not logging in as mod / admin." ) ; botrole = "null"
57
+ } else { send ( encode ( [ 'admin' , '2' , config . bot . login ] ) ) } // If not, Bot proceeds to log in
58
+
59
+ // Startup Message. Enable/Disable in Config.
60
+ if ( config . settings . startup === "true" ) { if ( client . connected ) { chat ( `${ config . settings . startupmsg } ` ) } } ;
61
+
62
+ client . on ( 'message' , message => {
63
+ const cmd = decode ( message . utf8Data ) ;
64
+ const action = cmd [ 0 ] ;
65
+ const prefix = config . bot . prefix ;
66
+
67
+ if ( action === "nop" ) { send ( encode ( [ 'nop' ] ) ) } ; // Send Heartbeat
68
+
69
+ if ( action === "adduser" ) {
70
+ if ( cmd [ 2 ] !== config . bot . user && cmd [ 1 ] === "1" ) { // Ignore bot & Detect 1 user only. [v2.0.4]
71
+ if ( cmd [ 3 ] === '0' ) { console . log ( `${ cmd [ 2 ] } Joined!` ) } ; // Logs User Joins.
72
+ if ( cmd [ 3 ] === '2' ) { console . log ( `${ cmd [ 2 ] } is now Administrator!` ) } ; // Logs User logged in as Administrator
73
+ if ( cmd [ 3 ] === '3' ) { console . log ( `${ cmd [ 2 ] } is now Moderator!` ) } // Logs user logged in as Moderator.
74
+ } ;
75
75
} ;
76
- } ;
77
- if ( action === "remuser" ) { console . log ( `${ cmd [ 2 ] } Left!` ) } ; // Logs user leaves.
78
- if ( action === "rename" && cmd [ 1 ] === '1' ) { console . log ( `${ cmd [ 2 ] } Renamed to ${ cmd [ 3 ] } ` ) } ; // Detect if user renames
79
-
80
- // Detects if the bot is logged in as admin / mod or fails to login.
81
- if ( action === "admin" ) {
82
- if ( cmd [ 2 ] === '0' ) { console . log ( "Incorrect login password!" ) ; botrole = "null" } ;
83
- if ( cmd [ 2 ] === '1' ) { console . log ( "Logged in as Administrator!" ) ; botrole = "admin" } ;
84
- if ( cmd [ 2 ] === '3' ) {
85
- console . log ( "Logged in as Moderator!" ) ; botrole = "mod" ;
86
- permissions = new Permissions ( cmd [ 3 ] ) ; // Check Moderator Permissions
87
- console . log ( permissions ) ; // Outputs Moderator Permissions [true/false] (as JSON)
88
- module . exports = { permissions} ; // Can be used for other admin / mod commands to check the bot's permission.
76
+ if ( action === "remuser" ) { console . log ( `${ cmd [ 2 ] } Left!` ) } ; // Logs user leaves.
77
+ if ( action === "rename" && cmd [ 1 ] === '1' ) { console . log ( `${ cmd [ 2 ] } Renamed to ${ cmd [ 3 ] } ` ) } ; // Detect if user renames
78
+
79
+ // Check if the bot has logged in as admin / mod
80
+ if ( action === "admin" ) {
81
+ if ( cmd [ 2 ] === '0' ) { console . log ( "Incorrect login password!" ) ; botrole = "null" } ;
82
+ if ( cmd [ 2 ] === '1' ) { console . log ( "Logged in as Administrator!" ) ; botrole = "admin" } ;
83
+ if ( cmd [ 2 ] === '3' ) { console . log ( "Logged in as Moderator!" ) ; botrole = "mod" ;
84
+ permissions = new Permissions ( cmd [ 3 ] ) ; // Check Moderator Permissions
85
+ } module . exports = { botrole, permissions} ; // export botrole, permissions
89
86
}
90
- module . exports = { botrole} ; // xsstest was not working when the bot is administrator.
91
- }
92
-
93
- if ( action === "chat" ) {
94
- if ( cmd [ 2 ] !== "" ) {
95
- // https://github.com/computernewb/collabvm-1.2.ts/blob/master/cvmts/src/Utilities.ts Line 26
96
- const decodedmsg = cmd [ 2 ]
97
- . replace ( / & # x 2 7 ; / g, "'" ) . replace ( / & q u o t ; / g, '"' )
98
- . replace ( / & # x 2 F ; / g, "/" ) . replace ( / & l t ; / g, "<" )
99
- . replace ( / & g t ; / g, ">" ) . replace ( / & a m p ; / g, "&" ) ;
100
- console . log ( `${ cmd [ 1 ] } says: ${ decodedmsg } ` ) ; // Logs user message
101
- } else { } ; // Ignores if the message is empty.
87
+
88
+ if ( action === "chat" ) {
89
+ if ( cmd [ 2 ] !== "" ) {
90
+ // https://github.com/computernewb/collabvm-1.2.ts/blob/master/cvmts/src/Utilities.ts Line 26
91
+ // Message Decoder to make it readable
92
+ const decodedmsg = cmd [ 2 ]
93
+ . replace ( / & # x 2 7 ; / g, "'" ) . replace ( / & q u o t ; / g, '"' )
94
+ . replace ( / & # x 2 F ; / g, "/" ) . replace ( / & l t ; / g, "<" )
95
+ . replace ( / & g t ; / g, ">" ) . replace ( / & a m p ; / g, "&" ) ;
96
+ console . log ( `\n${ cmd [ 1 ] } says: ${ decodedmsg } ` ) ; // Output messages to console
97
+ } else { } ; // Ignores if the message is empty (Happens if user send empty message through XSS)
102
98
103
- const cmdName = cmd [ 2 ] . slice ( prefix . length ) . trim ( ) . split ( ' ' ) [ 0 ] ;
104
- if ( cmd [ 1 ] !== config . bot . user ) { // Ignore bot messages
105
- if ( cmd [ 2 ] . startsWith ( prefix ) && command [ cmdName ] ) {
106
- if ( command [ cmdName ] . execute ) {
107
- command [ cmdName ] . execute ( chat , xss , cmd ) ;
108
- } else { chat ( `It looks like ${ cmdName } doesn't have 'execute' property set!` ) } ;
109
- } ;
110
- } ;
111
- } ;
112
-
113
- if ( action === 'list' ) { // For vminfo command. You may remove this code if you don't want this command.
114
- const vmname = cmd [ 1 ] ;
115
- const vmdesc = cmd [ 2 ] ;
116
- module . exports = { vmname , vmdesc } ;
117
- } ;
118
- } ) ;
119
- } ) ;
120
- Client . connect ( config . bot . ip , 'guacamole' ) ; // Bot connects to VM
99
+ const cmdName = cmd [ 2 ] . slice ( prefix . length ) . trim ( ) . split ( ' ' ) [ 0 ] ;
100
+ if ( cmd [ 1 ] !== config . bot . user ) { // Ignore bot messages
101
+ if ( cmd [ 2 ] . startsWith ( prefix ) && command [ cmdName ] ) {
102
+ if ( command [ cmdName ] . execute ) {
103
+ command [ cmdName ] . execute ( chat , xss , cmd ) ;
104
+ } else { chat ( `It looks like ${ cmdName } doesn't have 'execute' property set!` ) } ;
105
+ } ;
106
+ } ;
107
+ } ;
108
+
109
+ // Debugger
110
+ if ( config . settings . debug === 'true' && cmd [ 0 ] !== 'nop' ) { // Check if debug is true in config & ignore 'nop'
111
+ console . log ( cmd ) ; //Outputs WebSocket Messages through console.
112
+ } ;
113
+
114
+ } ) ;
115
+ } catch ( error ) { chat ( `Something went wrong, check console for more details.` ) ; console . log ( error ) }
116
+ } ) ; Client . connect ( config . bot . ip , 'guacamole' ) ; // Bot connects to VM
121
117
}
122
118
123
119
console . log ( 'Connecting to VM...' ) ;
@@ -128,7 +124,7 @@ function reconnect() {
128
124
console . log ( "Reconnecting..." ) ; bot ( ) ; // Calls the bot() again
129
125
}
130
126
131
- // Catch the CTRL+C !!!!
127
+ // If process recieved "SIGINT" (or CTRL+C), Kill session
132
128
process . on ( 'SIGINT' , ( ) => {
133
129
console . log ( '\nKilling Bot Session...' ) ;
134
130
if ( readline ) readline . close ( ) ; process . exit ( 0 ) ;
0 commit comments