summaryrefslogtreecommitdiff
path: root/client_network.c
blob: 35e3055169f76c415ac00104541a38b3f5923472 (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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// 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 "commands.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);

	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;
	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;

	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(" "));
	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"));

	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;
}

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_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;
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);
	set_table_index(&client_network_commands, STRING("PING"), &client_ping_handler);

	client_nick.data = malloc(0);

	client_listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	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),
	};
	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;
}