-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxmpp-shell-exec.js
125 lines (103 loc) · 3.48 KB
/
xmpp-shell-exec.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
// Extremely experimental!!
// Please becareful. You should see a lot of errors in your terminal.
// Has colors in CLI so you can read XML streams with ease
const { client, xml } = require("@xmpp/client");
const { exec } = require("child_process");
const xmpp = client({
service: "",
domain: "",
resource: "shellbot",
username: "",
password: "",
});
xmpp.on("online", async (jid) => {
// Timestamps when bot goes online
// This is good to see and log when bot goes online
const date = new Date();
console.log('\x1b[1m\x1b[36m','Bot Started on ',date,'\x1b[0m')
// sends the date to itself
// put back after done to: username, so it send to itself
const message = xml(
"message",
{ type: "chat", to: jid },
xml("body", {}, 'Bot started on ', date),
);
await xmpp.send(message);
});
console.log('\x1b[33m%s\x1b[0m', '--- XMPP Connection ---')
xmpp.start().catch(console.error), () => {
};
// This displays all the body messages
xmpp.on("stanza", (stanza) => {
bodymessage = stanza.getChildText('body')
if (bodymessage)
{
console.log('\n','\x1b[1m\x1b[36m','Message:',bodymessage,'\x1b[0m','\n')
}
});
xmpp.on("offline", () => {
console.log("bot offline");
});
// Displays Presence
xmpp.on("stanza", (presence) => {
if (presence)
{
console.log(presence.toString());
console.log('\x1b[33m%s\x1b[0m', '--- PRESENCE ---');
}
});
// Commands start here
xmpp.on("stanza", (stanza) => {
// reads message and outputs into terminal
console.log('\x1b[33m%s\x1b[0m', '--- XML Stream From User ---');
// The const that makes this all happen
// The most important CONST
const text = stanza.getChildText('body');
if(text) {
const split = text.split(/(?<=^\S+)\s/)
const possiblyBash = split[0]
const command = split[1]
// full string
console.log(split, possiblyBash, command)
console.log(possiblyBash === 'bash')
if(possiblyBash === 'bash') {
exec(command, async (error, stdout, stderr) => {
if (error) {
// Sends the command output to user if command not found
console.log(`error: ${error.message}`);
const { to, from } = stanza.attrs;
stanza.attrs.from = to;
stanza.attrs.to = from;
const message = xml(
"message",
{ type: "chat", to: from },
xml("body", {}, `${error}`),
);
xmpp.send(message);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`${stdout}`);
// Sends the command output to user
const { to, from } = stanza.attrs;
stanza.attrs.from = to;
stanza.attrs.to = from;
const message = xml(
"message",
{ type: "chat", to: from },
xml("body", {}, `${stdout}`) ,
);
xmpp.send(message);
});
// else just console log the received message
console.log('\x1b[1m\x1b[32m','--- Heres a command ---','\x1b[0m');
}
}
});
xmpp.on("online", (address) => {
console.log('\x1b[1m\x1b[36m','Online as',address.toString(),'\x1b[0m','\n');
xmpp.send(xml("presence")).catch(console.error);
});