]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/servercommands.c
Move code to src/
[dragonblocks_alpha.git] / src / servercommands.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "server.h"
4 #include "util.h"
5
6 static bool disconnect_handler(Client *client, bool good)
7 {
8         if (good)
9                 server_disconnect_client(client, DISCO_NO_SEND, NULL);
10         return true;
11 }
12
13 static bool auth_handler(Client *client, bool good)
14 {
15         char *name = read_string(client->fd, NAME_MAX);
16
17         if (! name)
18                 return false;
19
20         if (! good) {
21                 free(name);
22                 return true;
23         }
24
25         u8 success = linked_list_put(&client->server->clients, name, client);
26
27         printf("Authentication %s: %s -> %s\n", success ? "success" : "failure", client->address, name);
28
29         if (success) {
30                 client->name = name;
31                 client->state = CS_ACTIVE;
32         } else {
33                 free(name);
34         }
35
36         pthread_mutex_lock(&client->mtx);
37         bool ret = write_u32(client->fd, CC_AUTH) && write_u8(client->fd, success);
38         pthread_mutex_unlock(&client->mtx);
39
40         return ret;
41 }
42
43 static bool getblock_handler(Client *client, bool good)
44 {
45         v3s32 pos;
46
47         if (! read_v3s32(client->fd, &pos))
48                 return false;
49
50         if (! good)
51                 return true;
52
53         MapBlock *block = map_get_block(client->server->map, pos, false);
54         if (block) {
55                 pthread_mutex_lock(&client->mtx);
56                 bool ret = write_u32(client->fd, CC_BLOCK) && map_serialize_block(client->fd, block);
57                 pthread_mutex_unlock(&client->mtx);
58
59                 return ret;
60         }
61
62         return true;
63 }
64
65 static bool setnode_handler(Client *client, bool good)
66 {
67         v3s32 pos;
68
69         if (! read_v3s32(client->fd, &pos))
70                 return false;
71
72         MapNode node;
73
74         if (! map_deserialize_node(client->fd, &node))
75                 return false;
76
77         if (good)
78                 map_set_node(client->server->map, pos, node);
79
80         return true;
81 }
82
83 static bool kick_handler(Client *client, bool good)
84 {
85         char *target_name = read_string(client->fd, NAME_MAX);
86
87         if (! target_name)
88                 return false;
89
90         if (good) {
91                 Client *target = linked_list_get(&client->server->clients, target_name);
92                 if (target)
93                         server_disconnect_client(target, 0, "kicked");
94         }
95
96         free(target_name);
97         return true;
98 }
99
100 CommandHandler command_handlers[SERVER_COMMAND_COUNT] = {
101         {0},
102         {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_ACTIVE},
103         {&auth_handler, "AUTH", CS_CREATED},
104         {&getblock_handler, "GETBLOCK", CS_ACTIVE},
105         {&setnode_handler, "SETNODE", CS_ACTIVE},
106         {&kick_handler, "KICK", CS_ACTIVE},
107 };