summaryrefslogtreecommitdiff
path: root/client_network.c
blob: 226688ddfe282260662c70e71dad14cbceaaea27 (plain)
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
// Client network command handlers for HaxServ
//
// Written by: Test_User <hax@andrewyu.org>
//
// 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 <gnutls/gnutls.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#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 AndrewIRC IRC Network\r\n"));
	SENDCLIENT(STRING(":hax.irc.andrewyu.org 002 me :Your host is hax.irc.andrewyu.org, running a totally not sus IRCd\r\n"));
	SENDCLIENT(STRING(":hax.irc.andrewyu.org 003 me :This server was created 02:51:36 Apr 03 2023\r\n"));
	SENDCLIENT(STRING(":hax.irc.andrewyu.org 004 me irc.andrewyu.org InspIRCd-3 BDGHILNORSTWcdghikorswxz ABCDEFGHIJKLMNOPQRSTXYZabcdefghijklmnopqrstuvwz :BEFHIJLXYZabdefghjkloqvw\r\n"));
	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\r\n"));
	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\r\n"));
	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\r\n"));
	SENDCLIENT(STRING(":hax.irc.andrewyu.org 005 me WATCH=32 WHOX :are supported by this server\r\n"));

	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_privmsg_handler(uint64_t argc, struct string *argv) {
	if (argc < 2)
		return 1;

	SEND(STRING(":1HC000001 PRIVMSG "));
	SEND(argv[0]);
	SEND(STRING(" :"));
	SEND(argv[1]);
	SEND(STRING("\n"));

	return 0;
}

int client_raw_handler(uint64_t argc, struct string *argv) {
	if (argc < 1)
		return 1;

	SEND(argv[0]);
	SEND(STRING("\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);
	set_table_index(&client_network_commands, STRING("PRIVMSG"), &client_privmsg_handler);
	set_table_index(&client_network_commands, STRING("RAW"), &client_raw_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;
}