From 32d75fadbf193218d0be42ca91b7688f1854f6e4 Mon Sep 17 00:00:00 2001 From: Test_User Date: Sat, 6 May 2023 03:03:58 -0400 Subject: Start adding client support --- client_network.c | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 client_network.c (limited to 'client_network.c') diff --git a/client_network.c b/client_network.c new file mode 100644 index 0000000..05e0787 --- /dev/null +++ b/client_network.c @@ -0,0 +1,158 @@ +// Client network command handlers for HaxServ +// +// Written by: Test_User +// +// This is free and unencumbered software released into the public +// domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "network.h" +#include "config.h" +#include "types.h" +#include "table.h" +#include "tls.h" + +// TODO: Lock sends (everywhere) + +struct table client_network_commands = {0}; +struct string client_nick = {0}; +uint8_t client_connected; + +int client_nick_handler(uint64_t argc, struct string *argv) { + if (argc < 1) + return 1; + + void *tmp = malloc(argv[0].len); + if (!tmp) + return 1; + memcpy(tmp, argv[0].data, argv[0].len); + + free(client_nick.data); + + client_nick.data = tmp; + client_nick.len = argv[0].len; + + if (client_connected) { + SEND(STRING(":1HC000001 NICK ")); + SEND(client_nick); + SEND(STRING(" ")); + char current_time[22]; + snprintf(current_time, 22, "%ld", time(NULL)); + SEND(((struct string){current_time, strlen(current_time)})); + SEND(STRING("\n")); + } + + return 0; +} + +int client_user_handler(uint64_t argc, struct string *argv) { + if (argc < 4) + return 1; + + if (client_nick.len == 0) + return 1; + + char current_time[22]; + snprintf(current_time, 22, "%ld", time(NULL)); + SEND(STRING("UID 1HC000001 ")); + SEND(((struct string){current_time, strlen(current_time)})); + SEND(STRING(" ")); + SEND(client_nick); + SEND(STRING(" ")); + SEND(client_hostmask); + SEND(STRING(" ")); + SEND(client_hostmask); + SEND(STRING(" ")); + SEND(argv[0]); + SEND(STRING(" 192.168.1.1 ")); + SEND(((struct string){current_time, strlen(current_time)})); + SEND(STRING(" +k :")); + SEND(argv[3]); + SEND(STRING("\n")); + + SENDCLIENT(STRING(":hax.irc.andrewyu.org 001 me :Welcome to the LibreIRC IRC Network\n")); + SENDCLIENT(STRING(":hax.irc.andrewyu.org 002 me :Your host is hax.irc.andrewyu.org, running a totally not sus IRCd\n")); + SENDCLIENT(STRING(":hax.irc.andrewyu.org 003 me :This server was created 02:51:36 Apr 03 2023")); + SENDCLIENT(STRING(":hax.irc.andrewyu.org 004 me irc.andrewyu.org InspIRCd-3 BDGHILNORSTWcdghikorswxz ABCDEFGHIJKLMNOPQRSTXYZabcdefghijklmnopqrstuvwz :BEFHIJLXYZabdefghjkloqvw")); + SENDCLIENT(STRING(":hax.irc.andrewyu.org 005 me ACCEPT=100 AWAYLEN=200 BOT=B CALLERID=g CASEMAPPING=ascii CHANLIMIT=#:20 CHANMODES=IXZbegw,k,BEFHJLdfjl,ACDGKMNOPQRSTcimnprstuz CHANNELLEN=60 CHANTYPES=# ELIST=CMNTU ESILENCE=CcdiNnPpTtx EXCEPTS=e :are supported by this server")); + SENDCLIENT(STRING(":hax.irc.andrewyu.org 005 me EXTBAN=,ACNOQRSTUacjmnprswz HOSTLEN=64 INVEX=I KEYLEN=32 KICKLEN=300 LINELEN=512 MAXLIST=I:1000,X:1000,b:1000,e:1000,g:1000,w:1000 MAXTARGETS=20 MODES=20 MONITOR=30 NAMELEN=130 NAMESX NETWORK=LibreIRC :are supported by this server")); + SENDCLIENT(STRING(":hax.irc.andrewyu.org 005 me NICKLEN=30 OVERRIDE=O PREFIX=(Yqaohv)!~&@%+ REMOVE SAFELIST SECURELIST=60 SILENCE=100 STATUSMSG=!~&@%+ TOPICLEN=330 UHNAMES USERIP USERLEN=10 USERMODES=,,s,BDGHILNORSTWcdghikorwxz :are supported by this server")); + SENDCLIENT(STRING(":hax.irc.andrewyu.org 005 me WATCH=32 WHOX :are supported by this server")); + + client_connected = 1; + + return 0; +} + +int client_join_handler(uint64_t argc, struct string *argv) { + if (argc < 1) + return 1; + + SEND(STRING(":1HC FJOIN ")); + SEND(argv[0]); + SEND(STRING(" ")); + char current_time[22]; + snprintf(current_time, 22, "%ld", time(NULL)); + SEND(((struct string){current_time, strlen(current_time)})); + SEND(STRING(" + :,1HC000001\n")); + + return 0; +} + +int client_fd = -1; + +int client_listen_fd; +int initclientnetwork(void) { + client_network_commands.array = malloc(0); + + set_table_index(&client_network_commands, STRING("NICK"), &client_nick_handler); + set_table_index(&client_network_commands, STRING("USER"), &client_user_handler); + set_table_index(&client_network_commands, STRING("JOIN"), &client_join_handler); + + client_nick.data = malloc(0); + + client_listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (client_listen_fd < 0) + return 1; + + struct sockaddr_in localhost = { + .sin_family = AF_INET, + .sin_port = htons(6667), + }; + inet_pton(AF_INET, "127.0.0.1", &localhost.sin_addr); // this is indeed localhost for mine, and I have no intent to change this + bind(client_listen_fd, (struct sockaddr*)&localhost, sizeof(localhost)); + + listen(client_listen_fd, 1); + + return 0; +} -- cgit v1.2.3