1 /************************************************************************
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 ************************************************************************/
45 /* init ban system for the given file */
46 int ban_init(char* file
)
50 ban
.mutex
= mutex_create();
55 path
= path_get("game",file
,0,NULL
,0);
60 if (!strcmp(ban
.file
,path
)) {
69 nvp_free(&ban
.data
,1);
81 /* free ban memory, reset ban struct */
87 mutex_free(ban
.mutex
);
94 nvp_free(&ban
.data
,1);
100 /* load ban data from file */
108 mutex_lock(ban
.mutex
);
110 f
= file_load(NULL
,ban
.file
);
114 while (file_readline(f
,line
,512)) {
126 nvp_set(&ban
.data
,p
,n
,NULL
);
129 mutex_unlock(ban
.mutex
);
136 /* save ban data to file */
145 mutex_lock(ban
.mutex
);
147 f
= file_create(NULL
,ban
.file
);
151 for (n
=ban
.data
; n
; n
= n
->next
) {
152 if (!n
->name
|| !n
->name
[0])
154 file_writef(f
,"%s|%s\n",n
->name
,n
->value
);
159 mutex_unlock(ban
.mutex
);
166 /* check if an ip address is banned */
167 int ban_ipbanned(char* ip
)
170 mutex_lock(ban
.mutex
);
172 n
= nvp_get(&ban
.data
,ip
);
174 mutex_unlock(ban
.mutex
);
181 /* get the discription of a ban, NULL for all */
182 int ban_description(char* ip_or_name
, char* buff
, int size
)
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
);
209 if (!strcpy(buff
+o
,b1
))
215 mutex_unlock(ban
.mutex
);
222 mutex_lock(ban
.mutex
);
224 n
= nvp_get(&ban
.data
,ip_or_name
);
229 if (!strcmp(n
->value
,ip_or_name
))
235 mutex_unlock(ban
.mutex
);
240 o
= snprintf(buff
,size
,"%s|%s",n
->name
,n
->value
);
247 /* get the name for a given ip ban */
248 char* ban_ip2name(char* ip
)
252 mutex_lock(ban
.mutex
);
254 n
= nvp_get_str(&ban
.data
,ip
);
256 mutex_unlock(ban
.mutex
);
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
)
275 mutex_lock(ban
.mutex
);
277 n
= nvp_get(&ban
.data
,ip_or_name
);
280 for (n
=ban
.data
; n
; n
=n
->next
) {
281 if (!strcmp(n
->value
,ip_or_name
))
285 mutex_unlock(ban
.mutex
);
290 ban
.data
= list_remove(&ban
.data
,n
);
292 mutex_unlock(ban
.mutex
);