forked from botgram/shell-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·208 lines (177 loc) · 5.75 KB
/
server.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env node
// Starts the bot, handles permissions and chat context,
// interprets commands and delegates the actual command
// running to a Command instance. When started, an owner
// ID should be given.
var path = require("path");
var botgram = require("botgram");
var escapeHtml = require("escape-html");
var utils = require("./lib/utils");
var Command = require("./lib/command").Command;
var CONFIG_FILE = path.join(__dirname, "config.json");
try {
var config = require(CONFIG_FILE);
} catch (e) {
console.error("Couldn't load the configuration file, starting the wizard.\n");
require("./lib/wizard").configWizard({ configFile: CONFIG_FILE });
return;
}
var bot = botgram(config.authToken, { agent: utils.createAgent() });
var owner = config.owner;
var user = config.user;
var tokens = {};
var granted = {};
var contexts = {};
var defaultCwd = process.env.HOME || process.cwd();
bot.on("updateError", function (err) {
console.error("Error when updating:", err);
});
bot.on("synced", function () {
console.log("Bot ready.");
});
function rootHook(msg, reply, next) {
if (msg.queued) return;
var id = msg.chat.id;
var allowed = id === owner || user || granted[id];
// If this message contains a token, check it
if (!allowed && msg.command === "start" && Object.hasOwnProperty.call(tokens, msg.args())) {
var token = tokens[msg.args()];
delete tokens[msg.args()];
granted[id] = true;
allowed = true;
// Notify owner
// FIXME: reply to token message
var contents = (msg.user ? "User" : "Chat") + " <em>" + escapeHtml(msg.chat.name) + "</em>";
if (msg.chat.username) contents += " (@" + escapeHtml(msg.chat.username) + ")";
contents += " can now use the bot. To revoke, use:";
reply.to(owner).html(contents).command("revoke", id);
}
// If chat is not allowed, but user is, use its context
if (!allowed && (msg.from.id === owner || granted[msg.from.id])) {
id = msg.from.id;
allowed = true;
}
// Check that the chat is allowed
if (!allowed) {
if (msg.command === "start") reply.html("Not authorized to use this bot.");
return;
}
if (!contexts[id])
contexts[id] = {
id: id,
shell: utils.shells[0],
env: utils.getSanitizedEnv(),
cwd: defaultCwd,
size: { columns: 40, rows: 20 },
silent: true,
interactive: false,
linkPreviews: false,
};
msg.context = contexts[id];
next();
}
bot.all(rootHook);
bot.edited.all(rootHook);
// Welcome message
bot.command("start", function (msg, reply) {
reply.html("👋 Salam aleikum habibi");
});
const binDir = "/home/dietpi/commands/";
// Commands
const commands = [
{ name: "process" },
{ name: "upload" },
{ name: "list" },
{ name: "scan" },
{ name: "new", binary: `${binDir}jellyget` },
{
name: "url",
binary: "/home/dietpi/.local/bin/yt-dlp --get-url",
arg: true,
help: "Use /url <URL> to convert a URL.",
},
{
name: "add",
binary: `${binDir}whitelist`,
arg: true,
help: "Use /add <TITLE> to whitelist shows.",
},
{
name: "get",
binary: `${binDir}weeget`,
arg: true,
help: "Use /get <BOT> <PACK> to download files.",
},
{
name: "search",
binary: `${binDir}search-get`,
arg: true,
help: "Use /get <RELEASE> to search and download files.",
},
// {
// name: "free",
// binary: `df -h | grep Router | awk "{print \$4, \$5}"`x
// },
];
commands.forEach((command) => {
bot.command(command.name, (msg, reply, next) => {
// Check if arg is needed and display help
if (command.arg && !msg.args()) return reply.html(command.help);
// Check if command is already running
if (msg.context.command) return reply.text("A command is already running.");
// Construct command
let binary = command.binary || binDir + command.name;
// Run command
msg.context.command = new Command(
reply,
msg.context,
command.arg ? `${binary} '${msg.args()}'` : binary
);
// Remove context
msg.context.command.on("exit", () => (msg.context.command = null));
});
});
bot.command("token", function (msg, reply, next) {
if (msg.context.id !== owner) return;
var token = utils.generateToken();
tokens[token] = true;
reply.disablePreview().html("One-time access token generated. The following link can be used to get access to the bot:\n%s\nOr by forwarding me this:", bot.link(token));
reply.command(true, "start", token);
});
// Signal sending
bot.command("cancel", "kill", function (msg, reply, next) {
var arg = msg.args(1)[0];
if (!msg.context.command) return reply.html("No command is running.");
var group = msg.command === "cancel";
var signal = group ? "SIGINT" : "SIGTERM";
if (arg) signal = arg.trim().toUpperCase();
if (signal.substring(0, 3) !== "SIG") signal = "SIG" + signal;
try {
msg.context.command.sendSignal(signal, group);
} catch (err) {
reply.reply(msg).html("Couldn't send signal.");
}
});
// Status
bot.command("status", function (msg, reply, next) {
var content = "",
context = msg.context;
// Running command
if (!context.command) content += "No command running.\n\n";
else content += "Command running, PID " + context.command.pty.pid + ".\n\n";
// Granted chats (msg.chat.id is intentional)
if (msg.chat.id === owner) {
var grantedIds = Object.keys(granted);
if (grantedIds.length) {
content += "\nGranted chats:\n";
content += grantedIds.map(function (id) { return id.toString(); }).join("\n");
} else {
content += "\nNo chats granted. Use /grant or /token to allow another chat to use the bot.";
}
}
if (context.command) reply.reply(context.command.initialMessage.id);
reply.html(content);
});
bot.command(function (msg, reply, next) {
reply.reply(msg).text("Invalid command.");
});