-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
415 lines (364 loc) · 9.92 KB
/
client.c
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include "client.h"
#include "commands.h"
#include "player.h"
#include "playerdb.h"
#include "packet.h"
#include "level.h"
#include "network.h"
#include "socket.h"
#include "mcc.h"
struct client_list_t s_clients;
/*struct client_t *client_get_by_player(struct player_t *p)
{
int i;
for (i = 0; i < s_clients.used; i++)
{
if (s_clients.items[i]->player == p) return s_clients.items[i];
}
return NULL;
}*/
void client_add_packet(struct client_t *c, struct packet_t *p)
{
/* Don't add packets for closed sockets */
if (c->close) {
free(p);
return;
}
if (p == NULL)
{
LOG("[client] client_add_packet(): Tried to queue NULL packet\n");
return;
}
pthread_mutex_lock(&c->packet_send_mutex);
*c->packet_send_end = p;
c->packet_send_end = &p->next;
if (c->packet_send_count == 0)
{
socket_flag_write(c->sock);
}
c->packet_send_count++;
pthread_mutex_unlock(&c->packet_send_mutex);
}
void client_notify(struct client_t *c, const char *message)
{
/* One char extra to hold a NUL, which minecraft doesn't need */
char buf[65];
const char *bufp = message;
const char *bufe = message + strlen(message);
char last_colour[3] = { 0, 0, 0};
while (bufp < bufe)
{
const char *last_space = NULL;
const char *bufpp = bufp;
while (bufpp - bufp < sizeof buf - (last_colour[0] == 0 ? 1 : 3))
{
if (*bufpp == '\0' || *bufpp == '\n' || bufpp - bufp >= sizeof buf - (last_colour[0] == 0 ? 2 : 4))
{
last_space = bufpp;
last_colour[1] = last_colour[2];
break;
}
else if (*bufpp == ' ') {
last_space = bufpp;
last_colour[1] = last_colour[2];
}
else if (*bufpp == '&')
{
last_colour[2] = *(bufpp + 1);
}
bufpp++;
}
/* If string starts with a colour code, don't continue the previous colour. */
if (bufp[0] == '&') last_colour[0] = 0;
memset(buf, 0, sizeof buf);
if (last_colour[0] == 'f') last_colour[0] = 0;
if (last_colour[0] != 0)
{
buf[0] = '&';
buf[1] = last_colour[0];
}
memcpy(buf + (last_colour[0] == 0 ? 0 : 2), bufp, last_space - bufp);
/* Check if string ends with a colour code, and remove. */
size_t l = (last_colour[0] == 0 ? 0 : 2) + (last_space - bufp) - 1;
for (; l > 1; l--)
{
if (buf[l] == '&' || buf[l] == ' ')
{
buf[l] = '\0';
}
else if (buf[l - 1] == '&')
{
buf[l - 1] = '\0';
}
else
{
break;
}
}
last_colour[0] = last_colour[1];
client_add_packet(c, packet_send_message(0, buf));
switch (*last_space)
{
case '\0':
case '\n':
case ' ':
bufp = last_space + 1;
break;
default:
bufp = last_space;
break;
}
}
}
void client_notify_file(struct client_t *c, const char *filename)
{
char buf[1024];
FILE *f = fopen(filename, "r");
if (f == NULL)
{
LOG("No %s found\n", filename);
return;
}
while (!feof(f))
{
memset(buf, 0, sizeof buf);
if (fgets(buf, sizeof buf, f) != NULL)
{
if (*buf != '\0') client_notify(c, buf);
}
}
fclose(f);
}
bool client_notify_by_username(const char *username, const char *message)
{
unsigned i;
for (i = 0; i < s_clients.used; i++)
{
struct client_t *c = s_clients.items[i];
if (c->player == NULL) continue;
if (strcasecmp(c->player->username, username) == 0)
{
client_notify(c, message);
return true;
}
}
return false;
}
bool client_botcheck(struct client_t *c, char *message)
{
bool bot = false;
bool botuser = false;
if (strcasestr(message, "place one brown mushroom") != NULL) bot = true;
else if (strcasestr(message, "pasting file") != NULL) bot = true;
else if (strcasestr(message, "place 2 brown shrooms") != NULL) bot = true;
else if (strcasestr(message, "place 2 brown mushroom") != NULL) bot = true;
else if (strcasestr(message, "place 2 shrooms") != NULL) bot = true;
else if (strcasestr(message, ".reset to reset") != NULL) bot = true;
else if (strcasestr(message, "!reset to reset") != NULL) bot = true;
else if (strncasecmp(message, ".paste ", 7) == 0) botuser = true;
else if (strncasecmp(message, ".say ", 5) == 0) botuser = true;
else if (strncasecmp(message, ".copy ", 6) == 0) botuser = true;
else if (strncasecmp(message, ".drawline ", 10) == 0) botuser = true;
else if (strncasecmp(message, ".cuboid", 7) == 0) botuser = true;
else if (strncasecmp(message, "!paste ", 7) == 0) botuser = true;
else if (strncasecmp(message, "!say ", 5) == 0) botuser = true;
else if (strncasecmp(message, "!copy ", 6) == 0) botuser = true;
else if (strncasecmp(message, "!drawline ", 10) == 0) botuser = true;
else if (strncasecmp(message, "!cuboid", 7) == 0) botuser = true;
if (bot)
{
LOG("%s triggered bot detection: %s\n", c->player->username, message);
playerdb_set_rank(c->player->username, RANK_BANNED, "auto-bot");
net_close(c, "Bot detected");
return true;
}
if (botuser)
{
LOG("%s triggered bot user detection: %s\n", c->player->username, message);
playerdb_set_rank(c->player->username, RANK_BANNED, "auto-botuser");
net_close(c, "Bot user detected");
return true;
}
return false;
}
/* In place trim of string. */
static char *trim(char *text)
{
char *s = text;
char *e = text + strlen(text) - 1;
while (*s == ' ') s++;
while (*e == ' ' && e > s) e--;
e[1] = '\0';
return s;
}
void client_process(struct client_t *c, char *message)
{
/* Max length of username + message is 64 + 64 */
char buf[128];
if (message[0] == '\\' || message[0] == '/')
{
char *bufp = message + 1;
char *param[10];
int params = 0;
memset(param, 0, sizeof param);
for (;;)
{
size_t l = strcspn(bufp, " ");
bool end = false;
if (bufp[l] == '\0') end = true;
bufp[l] = '\0';
param[params++] = bufp;
bufp += l + 1;
if (end || params == 10) break;
}
if (!command_process(c, params, (const char **)param))
{
client_notify(c, "Unknown command");
return;
}
}
else
{
switch (message[0])
{
case '@': // Private message
{
message = trim(message + 1);
size_t l = strcspn(message, " ");
if (l != strlen(message))
{
message[l] = '\0';
snprintf(buf, sizeof buf, "(%s:" TAG_WHITE " %s)", c->player->colourusername, message + l + 1);
if (!client_notify_by_username(message, buf))
{
client_notify(c, "User is offline");
}
else
{
client_notify(c, buf);
}
}
return;
}
case '!':
message = trim(message + 1);
if (strlen(message) == 0) {
client_notify(c, TAG_YELLOW "No message to send");
return;
} else {
snprintf(buf, sizeof buf, "! %s:" TAG_WHITE " %s", c->player->colourusername, message);
call_hook(HOOK_CHAT, buf);
net_notify_all(buf);
}
return;
// case ';':
// snprintf(buf, sizeof buf, "* %s %s", c->player->colourusername, message + 1);
// break;
case '\'':
message = trim(message + 1);
if (strlen(message) == 0) {
client_notify(c, TAG_YELLOW "No message to send");
return;
} else {
snprintf(buf, sizeof buf, "%s:" TAG_WHITE " %s", playername(c->player, 1), message);
}
break;
default:
message = trim(message);
if (strlen(message) == 0) {
client_notify(c, TAG_YELLOW "No message to send");
return;
} else {
if (HasBit(c->player->flags, FLAG_GLOBAL))
{
snprintf(buf, sizeof buf, "! %s:" TAG_WHITE " %s", c->player->colourusername, message);
call_hook(HOOK_CHAT, buf);
net_notify_all(buf);
return;
}
if (c->player->rank < RANK_BUILDER && client_botcheck(c, message)) return;
snprintf(buf, sizeof buf, "%s:" TAG_WHITE " %s", playername(c->player, 1), message);
}
break;
}
call_hook(HOOK_CHAT, buf);
if (!call_level_hook(EVENT_CHAT, c->player->level, c, message))
{
level_notify_all(c->player->level, buf);
}
}
}
void client_send_spawn(struct client_t *c, bool hiding)
{
if (c->player == NULL || c->player->level == NULL) return;
struct level_t *level = c->player->level;
unsigned i;
for (i = 0; i < MAX_CLIENTS_PER_LEVEL; i++)
{
struct client_t *c2 = level->clients[i];
if (c2 != NULL && c2 != c && !c2->sending_level)
{
client_add_packet(c2, packet_send_spawn_player(c->player->levelid, playername(c->player, c2->player->namemode), &c->player->pos));
//printf("Told %s (%d) about %s joining %s\n", level->clients[i]->player->username, i, c->player->username, level->name);
}
}
}
void client_send_despawn(struct client_t *c, bool hiding)
{
if (c->player == NULL || c->player->level == NULL) return;
struct level_t *level = c->player->level;
unsigned i;
for (i = 0; i < MAX_CLIENTS_PER_LEVEL; i++)
{
struct client_t *c2 = level->clients[i];
if (c2 != NULL && c2 != c && !c2->sending_level)
{
client_add_packet(c2, packet_send_despawn_player(c->player->levelid));
//printf("Told %s (%d) about %s leaving %s\n", level->clients[i]->player->username, i, c->player->username, level->name);
}
}
}
void client_spawn_players(struct client_t *c)
{
if (c->player == NULL || c->player->level == NULL) return;
struct level_t *l = c->player->level;
unsigned i;
for (i = 0; i < MAX_CLIENTS_PER_LEVEL; i++)
{
struct client_t *c2 = l->clients[i];
if (c2 != NULL && c2 != c && !c2->hidden && !c2->sending_level)
{
client_add_packet(c, packet_send_spawn_player(c2->player->levelid, playername(c2->player, c->player->namemode), &c2->player->pos));
}
}
}
void client_despawn_players(struct client_t *c)
{
if (c->player == NULL || c->player->level == NULL) return;
struct level_t *l = c->player->level;
unsigned i;
for (i = 0; i < MAX_CLIENTS_PER_LEVEL; i++)
{
struct client_t *c2 = l->clients[i];
if (c2 != NULL && c2 != c && !c2->hidden && !c2->sending_level)
{
client_add_packet(c, packet_send_despawn_player(c2->player->levelid));
}
}
}
pthread_mutex_t s_client_list_mutex;
bool client_inuse(struct client_t *c, bool inuse)
{
pthread_mutex_lock(&s_client_list_mutex);
if (!client_is_valid(c) || c->inuse < 0)
{
pthread_mutex_unlock(&s_client_list_mutex);
return false;
}
c->inuse += inuse ? 1 : -1;
pthread_mutex_unlock(&s_client_list_mutex);
return true;
}