summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTest_User <hax@andrewyu.org>2023-08-07 03:24:09 -0400
committerTest_User <hax@andrewyu.org>2023-08-07 03:24:09 -0400
commit4f6fdc6d4faba1c268b8714be26f683de6884c77 (patch)
tree188b89caf3a79a63f5296f08ace24de719c21aef
parent742319702b61282dbd784ea3abf3ad3d847a3b25 (diff)
downloadcoupserv-4f6fdc6d4faba1c268b8714be26f683de6884c77.tar.gz
coupserv-4f6fdc6d4faba1c268b8714be26f683de6884c77.zip
More client usability stuff
-rw-r--r--client_network.c126
-rw-r--r--commands.c2
2 files changed, 127 insertions, 1 deletions
diff --git a/client_network.c b/client_network.c
index 226688d..35e3055 100644
--- a/client_network.c
+++ b/client_network.c
@@ -36,6 +36,7 @@
#include <stdlib.h>
#include <stdio.h>
+#include "commands.h"
#include "network.h"
#include "config.h"
#include "types.h"
@@ -57,6 +58,14 @@ int client_nick_handler(uint64_t argc, struct string *argv) {
return 1;
memcpy(tmp, argv[0].data, argv[0].len);
+ if (client_connected) {
+ SENDCLIENT(STRING(":"));
+ SENDCLIENT(client_nick);
+ SENDCLIENT(STRING("!e@e NICK :"));
+ SENDCLIENT(argv[0]);
+ SENDCLIENT(STRING("\r\n"));
+ }
+
free(client_nick.data);
client_nick.data = tmp;
@@ -118,6 +127,34 @@ int client_join_handler(uint64_t argc, struct string *argv) {
if (argc < 1)
return 1;
+ SENDCLIENT(STRING(":"));
+ SENDCLIENT(client_nick);
+ SENDCLIENT(STRING("!e@e JOIN :"));
+ SENDCLIENT(argv[0]);
+ SENDCLIENT(STRING("\r\n"));
+ SENDCLIENT(STRING(":hax.irc.andrewyu.org 332 "));
+ SENDCLIENT(client_nick);
+ SENDCLIENT(STRING(" "));
+ SENDCLIENT(argv[0]);
+ SENDCLIENT(STRING(" :\r\n")); // TODO: Actual topic
+ SENDCLIENT(STRING(":hax.irc.andrewyu.org 333 "));
+ SENDCLIENT(client_nick);
+ SENDCLIENT(STRING(" "));
+ SENDCLIENT(argv[0]);
+ SENDCLIENT(STRING(" "));
+ SENDCLIENT(client_nick); // TODO: Actual channel creator
+ SENDCLIENT(STRING(" :1\r\n")); // TODO: Actual timestamp
+ SENDCLIENT(STRING(":hax.irc.andrewyu.org 353 "));
+ SENDCLIENT(client_nick);
+ SENDCLIENT(STRING(" = "));
+ SENDCLIENT(argv[0]);
+ SENDCLIENT(STRING(" :\r\n")); // TODO: NAMES list
+ SENDCLIENT(STRING(":hax.irc.andrewyu.org 366 "));
+ SENDCLIENT(client_nick);
+ SENDCLIENT(STRING(" "));
+ SENDCLIENT(argv[0]);
+ SENDCLIENT(STRING(" :End of /NAMES list.\r\n"));
+
SEND(STRING(":1HC FJOIN "));
SEND(argv[0]);
SEND(STRING(" "));
@@ -139,6 +176,80 @@ int client_privmsg_handler(uint64_t argc, struct string *argv) {
SEND(argv[1]);
SEND(STRING("\n"));
+ uint64_t offset;
+ if (argv[0].data[0] == '#') {
+ if (argv[1].len < command_prefix.len || memcmp(argv[1].data, command_prefix.data, command_prefix.len) != 0)
+ return 0;
+
+ offset = command_prefix.len;
+ } else {
+ offset = 0;
+ }
+
+ if (offset >= argv[1].len || argv[1].data[offset] == ' ')
+ return 0;
+
+ uint64_t command_argc = 0;
+ uint64_t old_offset = offset;
+ while (offset < argv[1].len) {
+ while (offset < argv[1].len && argv[1].data[offset] != ' ')
+ offset++;
+
+ command_argc++;
+
+ while (offset < argv[1].len && argv[1].data[offset] == ' ')
+ offset++;
+ }
+ offset = old_offset;
+
+ struct string command_argv[command_argc]; // argv[0] in this case is the command itself, unlike network command handlers... might change one of these two later to match
+ uint64_t i = 0;
+ while (offset < argv[1].len) {
+ command_argv[i].data = argv[1].data+offset;
+ uint64_t start = offset;
+
+ while (offset < argv[1].len && argv[1].data[offset] != ' ')
+ offset++;
+
+ command_argv[i].len = offset - start;
+
+ while (offset < argv[1].len && argv[1].data[offset] == ' ')
+ offset++;
+
+ i++;
+ }
+
+ argv[1].data += old_offset;
+ argv[1].len -= old_offset;
+ struct command_def *cmd = get_table_index(user_commands, command_argv[0]);
+ if (cmd) {
+ if (!cmd->local_only) {
+ SEND(STRING(":1HC000000 PRIVMSG "));
+ SEND(log_channel);
+ SEND(STRING(" :Local user "));
+ SEND(client_nick);
+
+ SEND(STRING(" executes `"));
+ SEND(argv[1]);
+ SEND(STRING("'\n"));
+
+ return cmd->func(STRING("1HC000001"), argv[1], argv[0], command_argc, command_argv);
+ } else {
+ // TODO: complain about remote access
+ WRITES(1, STRING("Not executing local-only command from a remote source!\n"));
+ return 0;
+ }
+ } else {
+ if (argv[0].data[0] == '#') {
+ SEND(STRING(":1HC000000 NOTICE "));
+ SEND(argv[0]);
+ SEND(STRING(" :Unknown command: " "\x03" "04"));
+ SEND(command_argv[0]);
+ SEND(STRING("\n"));
+ }
+ return 0;
+ }
+
return 0;
}
@@ -152,6 +263,17 @@ int client_raw_handler(uint64_t argc, struct string *argv) {
return 0;
}
+int client_ping_handler(uint64_t argc, struct string *argv) {
+ if (argc < 1)
+ return 1;
+
+ SENDCLIENT(STRING(":hax.irc.andrewyu.org PONG :"));
+ SENDCLIENT(argv[0]);
+ SENDCLIENT(STRING("\r\n"));
+
+ return 0;
+}
+
int client_fd = -1;
int client_listen_fd;
@@ -163,6 +285,7 @@ int initclientnetwork(void) {
set_table_index(&client_network_commands, STRING("JOIN"), &client_join_handler);
set_table_index(&client_network_commands, STRING("PRIVMSG"), &client_privmsg_handler);
set_table_index(&client_network_commands, STRING("RAW"), &client_raw_handler);
+ set_table_index(&client_network_commands, STRING("PING"), &client_ping_handler);
client_nick.data = malloc(0);
@@ -170,6 +293,9 @@ int initclientnetwork(void) {
if (client_listen_fd < 0)
return 1;
+ int one = 1;
+ setsockopt(client_listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
+
struct sockaddr_in localhost = {
.sin_family = AF_INET,
.sin_port = htons(6667),
diff --git a/commands.c b/commands.c
index 0938ee2..f7b581d 100644
--- a/commands.c
+++ b/commands.c
@@ -69,7 +69,7 @@ int raw_command(struct string sender, struct string original_message, struct str
}
static struct command_def raw_command_def = {
.func = raw_command,
- .privs = STRING("Admin"),
+ .privs = STRING("NetAdmin"),
.local_only = 0,
.summary = STRING("Sends a raw message to the server\n"),
};