g_svcmds.c

Go to the documentation of this file.
00001 
00006 /*
00007 All original material Copyright (C) 2002-2010 UFO: Alien Invasion.
00008 
00009 Original file from Quake 2 v3.21: quake2-2.31/game/g_svcmds.c
00010 Copyright (C) 1997-2001 Id Software, Inc.
00011 
00012 This program is free software; you can redistribute it and/or
00013 modify it under the terms of the GNU General Public License
00014 as published by the Free Software Foundation; either version 2
00015 of the License, or (at your option) any later version.
00016 
00017 This program is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00020 
00021 See the GNU General Public License for more details.
00022 
00023 You should have received a copy of the GNU General Public License
00024 along with this program; if not, write to the Free Software
00025 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00026 
00027 */
00028 
00029 
00030 #include "g_local.h"
00031 
00052 typedef struct {
00053     unsigned mask;
00054     unsigned compare;
00055 } ipfilter_t;
00056 
00057 #define MAX_IPFILTERS   1024
00058 
00059 static ipfilter_t ipfilters[MAX_IPFILTERS];
00060 static int numipfilters;
00061 
00062 static qboolean StringToFilter (const char *s, ipfilter_t * f)
00063 {
00064     char num[128];
00065     int i, j;
00066     byte b[4];
00067     byte m[4];
00068 
00069     for (i = 0; i < 4; i++) {
00070         b[i] = 0;
00071         m[i] = 0;
00072     }
00073 
00074     for (i = 0; i < 4; i++) {
00075         if (*s < '0' || *s > '9') {
00076             G_ClientPrintf(NULL, PRINT_CONSOLE, "Bad filter address: %s\n", s);
00077             return qfalse;
00078         }
00079 
00080         j = 0;
00081         while (isdigit(*s)) {
00082             num[j++] = *s++;
00083         }
00084         num[j] = 0;
00085         b[i] = atoi(num);
00086         if (b[i] != 0)
00087             m[i] = 0xFF;
00088 
00089         if (!*s)
00090             break;
00091         s++;
00092     }
00093 
00094     f->mask = *(unsigned *) m;
00095     f->compare = *(unsigned *) b;
00096 
00097     return qtrue;
00098 }
00099 
00100 qboolean SV_FilterPacket (const char *from)
00101 {
00102     int i;
00103     unsigned in;
00104     byte m[4];
00105     const char *p;
00106 
00107     i = 0;
00108     p = from;
00109     while (*p && i < 4) {
00110         m[i] = 0;
00111         while (*p >= '0' && *p <= '9') {
00112             m[i] = m[i] * 10 + (*p - '0');
00113             p++;
00114         }
00115         if (!*p || *p == ':')
00116             break;
00117         i++, p++;
00118     }
00119 
00120     in = *(unsigned *) m;
00121 
00122     for (i = 0; i < numipfilters; i++)
00123         if ((in & ipfilters[i].mask) == ipfilters[i].compare)
00124             return sv_filterban->integer;
00125 
00126     return !sv_filterban->integer;
00127 }
00128 
00129 
00133 static void SVCmd_AddIP_f (void)
00134 {
00135     int i;
00136 
00137     if (gi.Cmd_Argc() < 3) {
00138         gi.DPrintf("Usage: %s <ip-mask>\n", gi.Cmd_Argv(1));
00139         return;
00140     }
00141 
00142     for (i = 0; i < numipfilters; i++)
00143         if (ipfilters[i].compare == ~0x0)
00144             break;              /* free spot */
00145     if (i == numipfilters) {
00146         if (numipfilters == MAX_IPFILTERS) {
00147             gi.DPrintf("IP filter list is full\n");
00148             return;
00149         }
00150         numipfilters++;
00151     }
00152 
00153     if (!StringToFilter(gi.Cmd_Argv(2), &ipfilters[i]))
00154         ipfilters[i].compare = ~0x0;
00155 }
00156 
00160 static void SVCmd_RemoveIP_f (void)
00161 {
00162     ipfilter_t f;
00163     int i, j;
00164 
00165     if (gi.Cmd_Argc() < 3) {
00166         gi.DPrintf("Usage: %s <ip-mask>\n", gi.Cmd_Argv(1));
00167         return;
00168     }
00169 
00170     if (!StringToFilter(gi.Cmd_Argv(2), &f))
00171         return;
00172 
00173     for (i = 0; i < numipfilters; i++)
00174         if (ipfilters[i].mask == f.mask && ipfilters[i].compare == f.compare) {
00175             for (j = i + 1; j < numipfilters; j++)
00176                 ipfilters[j - 1] = ipfilters[j];
00177             numipfilters--;
00178             gi.DPrintf("Removed.\n");
00179             return;
00180         }
00181     gi.DPrintf("Didn't find %s.\n", gi.Cmd_Argv(2));
00182 }
00183 
00187 static void SVCmd_ListIP_f (void)
00188 {
00189     int i;
00190     byte b[4];
00191 
00192     gi.DPrintf("Filter list:\n");
00193     for (i = 0; i < numipfilters; i++) {
00194         *(unsigned *) b = ipfilters[i].compare;
00195         gi.DPrintf("%3i.%3i.%3i.%3i\n", b[0], b[1], b[2], b[3]);
00196     }
00197 }
00198 
00202 static void SVCmd_WriteIP_f (void)
00203 {
00204     FILE *f;
00205     char name[MAX_OSPATH];
00206     byte b[4];
00207     int i;
00208 
00209     Com_sprintf(name, sizeof(name), "%s/listip.cfg", gi.FS_Gamedir());
00210 
00211     gi.DPrintf("Writing %s.\n", name);
00212 
00213     f = fopen(name, "wb");
00214     if (!f) {
00215         gi.DPrintf("Couldn't open %s\n", name);
00216         return;
00217     }
00218 
00219     fprintf(f, "set sv_filterban %d\n", sv_filterban->integer);
00220 
00221     for (i = 0; i < numipfilters; i++) {
00222         *(unsigned *) b = ipfilters[i].compare;
00223         fprintf(f, "sv addip %i.%i.%i.%i\n", b[0], b[1], b[2], b[3]);
00224     }
00225 
00226     fclose(f);
00227 }
00228 
00234 static void SVCmd_AI_Add_f (void)
00235 {
00236     int team;
00237 
00238     if (gi.Cmd_Argc() < 3) {
00239         gi.DPrintf("Usage: %s <teamnum>\n", gi.Cmd_Argv(1));
00240         return;
00241     }
00242     team = atoi(gi.Cmd_Argv(2));
00243     if (team > TEAM_CIVILIAN && team < MAX_TEAMS) {
00244         if (!AI_CreatePlayer(team))
00245             gi.DPrintf("Couldn't create AI player.\n");
00246     } else
00247         gi.DPrintf("Bad team number.\n");
00248 }
00249 
00250 
00256 static void SVCmd_Win_f (void)
00257 {
00258     int team;
00259 
00260     if (gi.Cmd_Argc() < 3) {
00261         gi.DPrintf("Usage: %s <teamnum>\n", gi.Cmd_Argv(1));
00262         return;
00263     }
00264     team = atoi(gi.Cmd_Argv(2));
00265     if (team > TEAM_CIVILIAN && team < MAX_TEAMS)
00266         G_MatchEndTrigger(team, 0);
00267     else
00268         gi.DPrintf("Bad team number.\n");
00269 }
00270 
00271 #ifdef DEBUG
00272 static void SVCmd_ShowAll_f (void)
00273 {
00274     edict_t *ent = NULL;
00275 
00276     /* Make everything visible to anyone who can't already see it */
00277     while ((ent = G_EdictsGetNextInUse(ent))) {
00278         G_AppearPerishEvent(~G_VisToPM(ent->visflags), 1, ent, NULL);
00279         ent->visflags |= ~ent->visflags;
00280     }
00281     gi.DPrintf("All items and creatures revealed to all sides\n");
00282 }
00283 
00284 static void SVCmd_AddItem_f (void)
00285 {
00286     const int team = TEAM_DEFAULT;
00287     edict_t *ent = G_EdictsGetNextLivingActorOfTeam(NULL, team);
00288 
00289     if (gi.Cmd_Argc() < 3) {
00290         gi.DPrintf("Usage: %s <item-id>\n", gi.Cmd_Argv(1));
00291         return;
00292     }
00293 
00294     if (!ent) {
00295         gi.DPrintf("Could not add item, no living members of team %i left\n", team);
00296         return;
00297     }
00298 
00299     G_AddItemToFloor(ent->pos, gi.Cmd_Argv(2));
00300 }
00301 
00305 static void SVCmd_ActorInvList_f (void)
00306 {
00307     player_t* player;
00308     int i;
00309 
00310     /* show inventory off all players around - include even the ai players */
00311     for (i = 0, player = game.players; i < game.sv_maxplayersperteam * 2; i++, player++) {
00312         if (!player->inuse)
00313             continue;
00314         G_InvList_f(player);
00315     }
00316 }
00317 #endif
00318 
00325 void G_ServerCommand (void)
00326 {
00327     const char *cmd;
00328 
00329     cmd = gi.Cmd_Argv(1);
00330     if (Q_strcasecmp(cmd, "addip") == 0)
00331         SVCmd_AddIP_f();
00332     else if (Q_strcasecmp(cmd, "removeip") == 0)
00333         SVCmd_RemoveIP_f();
00334     else if (Q_strcasecmp(cmd, "listip") == 0)
00335         SVCmd_ListIP_f();
00336     else if (Q_strcasecmp(cmd, "writeip") == 0)
00337         SVCmd_WriteIP_f();
00338     else if (Q_strcasecmp(cmd, "ai_add") == 0)
00339         SVCmd_AI_Add_f();
00340     else if (Q_strcasecmp(cmd, "win") == 0)
00341         SVCmd_Win_f();
00342 #ifdef DEBUG
00343     else if (Q_strcasecmp(cmd, "debug_showall") == 0)
00344         SVCmd_ShowAll_f();
00345     else if (Q_strcasecmp(cmd, "debug_additem") == 0)
00346         SVCmd_AddItem_f();
00347     else if (Q_strcasecmp(cmd, "debug_actorinvlist") == 0)
00348         SVCmd_ActorInvList_f();
00349 #endif
00350     else
00351         gi.DPrintf("Unknown server command \"%s\"\n", cmd);
00352 }

Generated by  doxygen 1.6.2