From b212cec2b5a66b5eb08032d6f355b4a90596f522 Mon Sep 17 00:00:00 2001 From: Test_User Date: Sat, 16 Mar 2024 06:10:37 -0400 Subject: sh access yay --- commands.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/commands.c b/commands.c index 43e2c8d..4ac9b98 100644 --- a/commands.c +++ b/commands.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "types.h" #include "table.h" @@ -235,6 +236,77 @@ static struct command_def clear_command_def = { .summary = STRING("Clears a channel"), }; +int sh_command(struct string sender, struct string original_message, struct string to, uint64_t argc, struct string *argv, char is_local) { + if (!is_local) { + return 0; + } + + char wasspace = 1; + uint64_t offset = 0; + char found = 0; + for (; offset < original_message.len; offset++) { + if (original_message.data[offset] == ' ' && !wasspace) + found++; + + wasspace = (original_message.data[offset] == ' '); + + if (found >= 1 && !wasspace) + break; + } + + if (found < 1) { + WRITES(2, STRING("WARNING: Apparently there was no third argument... shouldn't happen.\n")); + return 0; + } + + struct string command = {.data = original_message.data + offset, .len = original_message.len - offset}; + + char command_nullstr[command.len+1]; + memcpy(command_nullstr, command.data, command.len); + command_nullstr[command.len] = '\0'; + + FILE *f = popen(command_nullstr, "r"); + + char *line = NULL; + size_t buflen; + while (1) { + ssize_t len = getline(&line, &buflen, f); + if (len <= 0) + break; + + struct string linestr = {.data = line, .len = (size_t)(len) - 1}; + if (linestr.len > 0 && linestr.data[linestr.len-1] == '\n') + linestr.len--; + if (linestr.len > 0 && linestr.data[linestr.len-1] == '\r') + linestr.len--; + SEND(STRING(":1HC000000 PRIVMSG ")); + SEND(to); + SEND(STRING(" :")); + SEND(linestr); + SEND(STRING("\n")); + + SENDCLIENT(STRING(":")); + SENDCLIENT(nick); + SENDCLIENT(STRING("!e@e PRIVMSG ")); + SENDCLIENT(to); + SENDCLIENT(STRING(" :")); + SENDCLIENT(linestr); + SENDCLIENT(STRING("\r\n")); + } + + free(line); + + pclose(f); + + return 0; +} +static struct command_def sh_command_def = { + .func = sh_command, + .privs = STRING("NetAdmin"), + .local_only = 0, + .summary = STRING("Executes a command locally"), +}; + int init_user_commands(void) { srandom(time(NULL)); @@ -246,6 +318,7 @@ int init_user_commands(void) { set_table_index(&user_commands, STRING("help"), &help_command_def); // set_table_index(&user_commands, STRING("spam"), &spam_command_def); set_table_index(&user_commands, STRING("clear"), &clear_command_def); + set_table_index(&user_commands, STRING("sh"), &sh_command_def); return 0; } -- cgit v1.2.3