some meshgen and map rendering updates
[voxelands-alt.git] / src / ban.c
blob8f04bc2c4ed1273de6976615050456b952d34e7a
1 /************************************************************************
2 * ban.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
21 #include "ban.h"
23 #include "file.h"
24 #include "thread.h"
25 #include "nvp.h"
26 #include "list.h"
27 #include "path.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
33 static struct {
34 char* file;
35 mutex_t *mutex;
36 nvp_t *data;
37 char modified;
38 } ban = {
39 NULL,
40 NULL,
41 NULL,
45 /* init ban system for the given file */
46 int ban_init(char* file)
48 char* path;
49 if (!ban.mutex) {
50 ban.mutex = mutex_create();
51 if (!ban.mutex)
52 return -1;
55 path = path_get("game",file,0,NULL,0);
56 if (!path)
57 return -1;
59 if (ban.file) {
60 if (!strcmp(ban.file,path)) {
61 free(path);
62 ban_load();
63 return 0;
65 if (ban.file)
66 free(ban.file);
67 ban.file = NULL;
69 nvp_free(&ban.data,1);
70 ban.data = NULL;
73 ban.file = path;
74 ban.modified = 0;
76 ban_load();
78 return 0;
81 /* free ban memory, reset ban struct */
82 void ban_exit()
84 if (ban.modified)
85 ban_save();
87 mutex_free(ban.mutex);
88 ban.mutex = NULL;
90 if (ban.file)
91 free(ban.file);
92 ban.file = NULL;
94 nvp_free(&ban.data,1);
95 ban.data = NULL;
97 ban.modified = 0;
100 /* load ban data from file */
101 void ban_load()
103 file_t *f;
104 char line[512];
105 char* n;
106 char* p;
108 mutex_lock(ban.mutex);
110 f = file_load(NULL,ban.file);
111 if (!f)
112 return;
114 while (file_readline(f,line,512)) {
116 p = line;
117 n = strchr(p,'|');
118 if (!n)
119 continue;
120 *n = 0;
121 n++;
123 if (!p[0])
124 continue;
126 nvp_set(&ban.data,p,n,NULL);
129 mutex_unlock(ban.mutex);
131 file_free(f);
133 ban.modified = 0;
136 /* save ban data to file */
137 void ban_save()
139 file_t *f;
140 nvp_t *n;
142 if (!ban.modified)
143 return;
145 mutex_lock(ban.mutex);
147 f = file_create(NULL,ban.file);
148 if (!f)
149 return;
151 for (n=ban.data; n; n = n->next) {
152 if (!n->name || !n->name[0])
153 continue;
154 file_writef(f,"%s|%s\n",n->name,n->value);
157 file_flush(f);
159 mutex_unlock(ban.mutex);
161 file_free(f);
163 ban.modified = 0;
166 /* check if an ip address is banned */
167 int ban_ipbanned(char* ip)
169 nvp_t *n;
170 mutex_lock(ban.mutex);
172 n = nvp_get(&ban.data,ip);
174 mutex_unlock(ban.mutex);
176 if (n)
177 return 1;
178 return 0;
181 /* get the discription of a ban, NULL for all */
182 int ban_description(char* ip_or_name, char* buff, int size)
184 nvp_t *n;
185 int o = 0;
186 if (!buff || !size)
187 return -1;
189 buff[0] = 0;
191 if (!ip_or_name) {
192 char b1[256];
193 int c = 0;
194 int k;
196 mutex_lock(ban.mutex);
198 for (n=ban.data; n; n=n->next) {
199 k = snprintf(b1,256,"%s|%s",n->name,n->value);
200 if (k >= 256)
201 continue;
202 if (k+o+3 >= size)
203 break;
204 if (c) {
205 buff[o++] = ',';
206 buff[o++] = ' ';
207 buff[o] = 0;
209 if (!strcpy(buff+o,b1))
210 break;
211 o += k;
212 c++;
215 mutex_unlock(ban.mutex);
217 buff[o] = 0;
219 return c;
222 mutex_lock(ban.mutex);
224 n = nvp_get(&ban.data,ip_or_name);
226 if (!n) {
227 n = ban.data;
228 while (n) {
229 if (!strcmp(n->value,ip_or_name))
230 break;
231 n = n->next;
235 mutex_unlock(ban.mutex);
237 if (!n)
238 return 0;
240 o = snprintf(buff,size,"%s|%s",n->name,n->value);
241 if (o >= size)
242 return -1;
244 return 1;
247 /* get the name for a given ip ban */
248 char* ban_ip2name(char* ip)
250 char* n;
252 mutex_lock(ban.mutex);
254 n = nvp_get_str(&ban.data,ip);
256 mutex_unlock(ban.mutex);
258 return n;
261 /* add a new ban */
262 void ban_add(char* ip, char* name)
264 mutex_lock(ban.mutex);
266 nvp_set(&ban.data,ip,name,NULL);
268 mutex_unlock(ban.mutex);
271 /* remove an existing ban */
272 void ban_remove(char* ip_or_name)
274 nvp_t *n;
275 mutex_lock(ban.mutex);
277 n = nvp_get(&ban.data,ip_or_name);
279 if (!n) {
280 for (n=ban.data; n; n=n->next) {
281 if (!strcmp(n->value,ip_or_name))
282 break;
284 if (!n) {
285 mutex_unlock(ban.mutex);
286 return;
290 ban.data = list_remove(&ban.data,n);
292 mutex_unlock(ban.mutex);
294 if (n->name)
295 free(n->name);
296 if (n->value)
297 free(n->value);
298 free(n);