+/* cmd_funcmds_bomb.c - NeonServ v5.6
+ * Copyright (C) 2011-2012 Philipp Kreil (pk910)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+static void funcmd_bomb_plant(struct funcmd_header_info *header, struct Event *event, char **argv, int argc, int kick_bomb);
+static USERAUTH_CALLBACK(funcmd_bomb_plant_nick_lookup);
+static void funcmd_bomb_plant_async1(struct funcmd_header_info *header, struct Event *event, struct UserNode *user, int wires, int kick_bomb);
+static void funcmd_bomb_plant_async2(struct funcmd_header_info *header, struct Event *event, struct UserNode *user, int wires, int kick_bomb);
+static void funcmd_bomb_defuse(struct funcmd_header_info *header, struct Event *event, char **argv, int argc);
+static void funcmd_bomb_return(struct funcmd_header_info *header, struct Event *event, char **argv, int argc);
+static TIMEQ_CALLBACK(funcmd_bomb_timeout);
+
+struct funcmd_bomb_plant_cache {
+ struct funcmd_header_info header;
+ struct Event *event;
+ int wires : 4;
+ int kick_bomb : 4;
+};
+
+struct funcmd_bomb_bomb {
+ struct funcmd_header_info header;
+ struct UserNode *owner, *target;
+ struct timeq_entry *timer;
+ char wires[FUNCMD_BOMB_WIRES_MAX];
+ int right_wire : 4;
+ int kick_bomb : 4;
+ struct funcmd_bomb_bomb *next;
+};
+
+static struct funcmd_bomb_bomb *funcmd_bomb_bombs = NULL;
+
+static int funcmd_bomb_freeuser(struct UserNode *user) {
+ struct funcmd_bomb_bomb *bomb, *next_bomb, *prev_bomb = NULL;
+ for(bomb = funcmd_bomb_bombs; bomb; bomb = next_bomb) {
+ next_bomb = bomb->next;
+ if(bomb->owner == user)
+ bomb->owner = NULL;
+ if(bomb->target == user) {
+ timeq_del(bomb->timer);
+ free(bomb);
+ if(prev_bomb)
+ prev_bomb->next = next_bomb;
+ else
+ funcmd_bomb_bombs = next_bomb;
+ } else
+ prev_bomb = bomb;
+ }
+ return 1;
+}
+
+static int funcmd_bomb_freechan(struct ChanNode *chan) {
+ struct funcmd_bomb_bomb *bomb, *next_bomb, *prev_bomb = NULL;
+ for(bomb = funcmd_bomb_bombs; bomb; bomb = next_bomb) {
+ next_bomb = bomb->next;
+ if(bomb->header.chan == chan) {
+ timeq_del(bomb->timer);
+ free(bomb);
+ if(prev_bomb)
+ prev_bomb->next = next_bomb;
+ else
+ funcmd_bomb_bombs = next_bomb;
+ } else
+ prev_bomb = bomb;
+ }
+ return 1;
+}
+
+static void funcmd_bomb_freeclient(struct ClientSocket *client) {
+ struct funcmd_bomb_bomb *bomb, *next_bomb, *prev_bomb = NULL;
+ for(bomb = funcmd_bomb_bombs; bomb; bomb = next_bomb) {
+ next_bomb = bomb->next;
+ if(bomb->header.client == client) {
+ timeq_del(bomb->timer);
+ free(bomb);
+ if(prev_bomb)
+ prev_bomb->next = next_bomb;
+ else
+ funcmd_bomb_bombs = next_bomb;
+ } else
+ prev_bomb = bomb;
+ }
+}
+
+CMD_BIND(funcmd_bomb) {
+ FUNCMD_HEADER;
+ if(argc) {
+ char *subcmd = argv[0];
+ argv++;
+ argc--;
+ if(!stricmp(subcmd, "plant"))
+ funcmd_bomb_plant(header, event, argv, argc, 0);
+ else if(!stricmp(subcmd, "kplant"))
+ funcmd_bomb_plant(header, event, argv, argc, 1);
+ else if(!stricmp(subcmd, "defuse"))
+ funcmd_bomb_defuse(header, event, argv, argc);
+ //else if(!stricmp(subcmd, "return"))
+ // funcmd_bomb_return(header, event, argv, argc);
+ else {
+ char tmp[MAXLEN];
+ sprintf(tmp, "%s %s", event->command->cmd, subcmd);
+ reply(header->client, header->user, "MODCMD_UNKNOWN", tmp);
+ }
+ } else {
+ reply(header->client, header->user, "FUN_BOMB_MENU");
+ reply(header->client, header->user, "FUN_BOMB_MENU_PLANT", event->command->cmd, FUNCMD_BOMB_WIRES_DEFAULT);
+ reply(header->client, header->user, "FUN_BOMB_MENU_KPLANT", event->command->cmd, FUNCMD_BOMB_WIRES_DEFAULT);
+ reply(header->client, header->user, "FUN_BOMB_MENU_DEFUSE", event->command->cmd);
+ //reply(header->client, header->user, "FUN_BOMB_MENU_RETURN", event->command->cmd);
+ }
+ FUNCMD_FOOTER;
+}
+
+static void funcmd_bomb_plant(struct funcmd_header_info *header, struct Event *event, char **argv, int argc, int kick_bomb) {
+ if(!argc) {
+ reply(header->client, header->user, "MODCMD_LESS_PARAM_COUNT");
+ return;
+ }
+ struct UserNode *user;
+ int wires = FUNCMD_BOMB_WIRES_DEFAULT;
+ if(!(user = getUserByNick(argv[0]))) {
+ reply(header->client, header->user, "NS_USER_UNKNOWN", argv[0]);
+ return;
+ }
+ if(isNetworkService(user)) {
+ reply(header->client, header->user, "FUN_BOMB_PLANT_SERVICE");
+ return;
+ }
+ if(user == header->user) {
+ reply(header->client, header->user, "FUN_BOMB_SELF");
+ return;
+ }
+ if(argc > 1) {
+ wires = atoi(argv[1]);
+ if(wires < FUNCMD_BOMB_WIRES_MIN || wires > FUNCMD_BOMB_WIRES_MAX) {
+ reply(header->client, header->user, "FUN_BOMB_PLANT_MAXWIRES", FUNCMD_BOMB_WIRES_MIN, FUNCMD_BOMB_WIRES_MAX);
+ return;
+ }
+ }
+ if(kick_bomb == 1) { /* protect shit... */
+ if(user->flags & USERFLAG_ISAUTHED) {
+ funcmd_bomb_plant_async1(header, event, user, wires, kick_bomb);
+ } else {
+ struct funcmd_bomb_plant_cache *cache = malloc(sizeof(*cache));
+ memcpy(&cache->header, header, sizeof(*header));
+ cache->event = event;
+ cache->wires = wires;
+ cache->kick_bomb = kick_bomb;
+ get_userauth(user, module_id, funcmd_bomb_plant_nick_lookup, cache);
+ }
+ } else
+ funcmd_bomb_plant_async2(header, event, user, wires, kick_bomb);
+}
+
+static USERAUTH_CALLBACK(funcmd_bomb_plant_nick_lookup) {
+ struct funcmd_bomb_plant_cache *cache = data;
+ if(!user) {
+ reply(cache->header.client, cache->header.user, "NS_USER_UNKNOWN", "*");
+ } else {
+ funcmd_bomb_plant_async1(&cache->header, cache->event, user, cache->wires, cache->kick_bomb);
+ }
+ free(cache);
+}
+
+static void funcmd_bomb_plant_async1(struct funcmd_header_info *header, struct Event *event, struct UserNode *user, int wires, int kick_bomb) {
+ if(kick_bomb == 1) { /* protect shit... */
+ if(isUserProtected(header->chan, user, header->user)) {
+ reply(header->client, header->user, "FUN_BOMB_PLANT_PROTECTED");
+ return;
+ }
+ }
+ funcmd_bomb_plant_async2(header, event, user, wires, kick_bomb);
+}
+
+static void funcmd_bomb_defuse_strip_color(char *buffer) {
+ int i, j;
+ j = 0;
+ for(i = 0; buffer[i]; i++) {
+ if(buffer[i] == '\003') {
+ i++;
+ if(!buffer[i]) break;
+ if(isdigit(buffer[i])) {
+ i++;
+ if(!buffer[i]) break;
+ }
+ if(isdigit(buffer[i])) {
+ i++;
+ if(!buffer[i]) break;
+ }
+ }
+ buffer[j++] = buffer[i];
+ }
+ buffer[j] = 0;
+}
+
+static int funcmd_bomb_defuse_get_wire_id(char *wire, struct UserNode *user) {
+ char *wires = get_language_string(user, "FUN_BOMB_WIRES");
+ char *cwire = wires;
+ char tmp[MAXLEN];
+ int found = 0, cindex = 0;
+ do {
+ wires = strchr(cwire, '|');
+ if(wires) {
+ *wires = '\0';
+ }
+ cindex++;
+ strcpy(tmp, cwire);
+ funcmd_bomb_defuse_strip_color(tmp);
+ if(!stricmp(wire, tmp))
+ found = cindex;
+ if(wires) {
+ *wires = '|';
+ cwire = wires + 1;
+ } else
+ cwire = NULL;
+ } while(!found && cwire);
+ return found;
+}
+
+static int funcmd_bomb_defuse_get_wire_name(int wire_id, char *buffer, char *wires, struct UserNode *user) {
+ if(!wires)
+ wires = get_language_string(user, "FUN_BOMB_WIRES");
+ char *cwire = wires;
+ int cindex = 0, chars = 0;
+ buffer[0] = 0;
+ do {
+ wires = strchr(cwire, '|');
+ if(wires) {
+ *wires = '\0';
+ }
+ cindex++;
+ if(cindex == wire_id)
+ chars = sprintf(buffer, "%s", cwire);
+ if(wires) {
+ *wires = '|';
+ cwire = wires + 1;
+ }
+ } while(!chars && cwire);
+ return chars;
+}
+
+static void funcmd_bomb_plant_async2(struct funcmd_header_info *header, struct Event *event, struct UserNode *user, int wires, int kick_bomb) {
+ //everything should be checked now... plant the bomb :)
+ struct funcmd_bomb_bomb *bomb;
+ for(bomb = funcmd_bomb_bombs; bomb; bomb = bomb->next) {
+ if(bomb->target == user) {
+ reply(header->client, header->user, "FUN_BOMB_TARGET_ACTIVE", user->nick);
+ return;
+ }
+ if(bomb->owner == header->user) {
+ reply(header->client, header->user, "FUN_BOMB_OWNER_ACTIVE");
+ return;
+ }
+ }
+ bomb = malloc(sizeof(*bomb));
+ memcpy(&bomb->header, header, sizeof(*header));
+ bomb->target = user;
+ bomb->owner = header->user;
+ bomb->kick_bomb = kick_bomb;
+ bomb->timer = timeq_add(FUNCMD_BOMB_TIME, module_id, funcmd_bomb_timeout, bomb);
+ bomb->next = funcmd_bomb_bombs;
+ funcmd_bomb_bombs = bomb;
+ int i, j, k, l;
+ int pool[FUNCMD_BOMB_WIRES_MAX+1];
+ for(i = 0; i < FUNCMD_BOMB_WIRES_MAX; i++) {
+ pool[i] = i+1;
+ }
+ pool[i] = 0;
+ for(i = 0; i < FUNCMD_BOMB_WIRES_MAX; i++) {
+ if(i < wires) {
+ j = (rand() % (FUNCMD_BOMB_WIRES_MAX - i));
+ bomb->wires[i] = pool[j];
+ l = 0;
+ for(k = 0; k < (FUNCMD_BOMB_WIRES_MAX - i); k++) {
+ if(k == j)
+ l = 1;
+ pool[k] = pool[k+l];
+ }
+ } else
+ bomb->wires[i] = 0;
+ }
+ bomb->right_wire = bomb->wires[(rand() % wires)];
+ char *wires_lang_str_a = get_language_string(header->user, "FUN_BOMB_WIRES");
+ char *wires_lang_str_b = get_language_string(user, "FUN_BOMB_WIRES");
+ if(!header->send_notice && wires_lang_str_a != wires_lang_str_b) {
+ wires_lang_str_a = get_language_string(NULL, "FUN_BOMB_WIRES");
+ header->null_language = 1;
+ bomb->header.null_language = 1;
+ }
+ char wires_str[MAXLEN];
+ j = 0;
+ for(i = 0; i < wires; i++) {
+ if(i) {
+ wires_str[j++] = ',';
+ wires_str[j++] = ' ';
+ }
+ j += funcmd_bomb_defuse_get_wire_name(bomb->wires[i], wires_str+j, wires_lang_str_a, NULL);
+ }
+ wires_str[j] = '\0';
+ if(header->send_notice) {
+ reply(header->client, header->user, (kick_bomb ? "FUN_BOMB_KPLANTED" : "FUN_BOMB_PLANTED"), header->user->nick, user->nick, FUNCMD_BOMB_TIME, wires_str, event->command->cmd);
+ j = 0;
+ for(i = 0; i < wires; i++) {
+ if(i) {
+ wires_str[j++] = ',';
+ wires_str[j++] = ' ';
+ }
+ j += funcmd_bomb_defuse_get_wire_name(bomb->wires[i], wires_str+j, wires_lang_str_b, NULL);
+ }
+ wires_str[j] = '\0';
+ reply(header->client, user, (kick_bomb ? "FUN_BOMB_KPLANTED" : "FUN_BOMB_PLANTED"), header->user->nick, user->nick, FUNCMD_BOMB_TIME, wires_str, event->command->cmd);
+ } else
+ funcmd_reply(header, (kick_bomb ? "FUN_BOMB_KPLANTED" : "FUN_BOMB_PLANTED"), REPLYTYPE_NORMAL, header->user->nick, user->nick, FUNCMD_BOMB_TIME, wires_str, event->command->cmd);
+}
+
+static void funcmd_bomb_detonate(struct funcmd_bomb_bomb *bomb) {
+ char *ptr;
+ int user_count = ((ptr = getSetting(bomb->target, bomb->header.chan, "bombs")) ? atoi(ptr) : 0);
+ int total_count = ((ptr = getSetting(bomb->target, NULL, "bombs")) ? atoi(ptr) : 0);
+ int detonated_count = ((ptr = getSetting(bomb->target, NULL, "bombs_detonated")) ? atoi(ptr) : 0);
+ user_count++;
+ total_count++;
+ detonated_count++;
+ char buf[10];
+ sprintf(buf, "%d", user_count);
+ setSetting(bomb->target, bomb->header.chan, "bombs", buf);
+ sprintf(buf, "%d", total_count);
+ setSetting(bomb->target, NULL, "bombs", buf);
+ sprintf(buf, "%d", detonated_count);
+ setSetting(bomb->target, NULL, "bombs_detonated", buf);
+ char *wires = get_language_string(NULL, "FUN_BOMB_WIRES");
+ char *cwire = wires;
+ char tmp[MAXLEN];
+ int cindex = 0;
+ tmp[0] = 0;
+ do {
+ wires = strchr(cwire, '|');
+ if(wires) {
+ *wires = '\0';
+ }
+ cindex++;
+ if(cindex == bomb->right_wire)
+ strcpy(tmp, cwire);
+ if(wires) {
+ *wires = '|';
+ cwire = wires + 1;
+ } else
+ cwire = NULL;
+ } while(!tmp[0] && cwire);
+ if(bomb->header.send_notice)
+ reply(bomb->header.client, bomb->owner, "FUN_BOMB_DETONATED", REPLYTYPE_NORMAL, bomb->target->nick, tmp, total_count, user_count);
+ funcmd_reply(&bomb->header, "FUN_BOMB_DETONATED", REPLYTYPE_NORMAL, bomb->target->nick, tmp, total_count, user_count);
+ if(bomb->kick_bomb) {
+ putsock(bomb->header.client, "KICK %s %s :[BOMB] *BOOOOOOM*", bomb->header.chan->name, bomb->target->nick);
+ }
+}
+
+static void funcmd_bomb_defuse(struct funcmd_header_info *header, struct Event *event, char **argv, int argc) {
+ if(!argc) {
+ reply(header->client, header->user, "MODCMD_LESS_PARAM_COUNT");
+ return;
+ }
+ struct funcmd_bomb_bomb *bomb, *prev_bomb = NULL;
+ for(bomb = funcmd_bomb_bombs; bomb; bomb = bomb->next) {
+ if(bomb->target == header->user)
+ break;
+ else
+ prev_bomb = bomb;
+ }
+ if(!bomb) {
+ reply(header->client, header->user, "FUN_BOMB_NOBOMB");
+ return;
+ }
+ funcmd_bomb_defuse_strip_color(argv[0]);
+ int cut_wire = funcmd_bomb_defuse_get_wire_id(argv[0], header->user);
+ if(!cut_wire)
+ cut_wire = funcmd_bomb_defuse_get_wire_id(argv[0], NULL);
+ if(!cut_wire) {
+ reply(header->client, header->user, "FUN_BOMB_UNKNOWN_WIRE", argv[0]);
+ return;
+ }
+ if(bomb->right_wire == cut_wire) {
+ char *tmp;
+ int user_count = ((tmp = getSetting(header->user, header->chan, "bombs")) ? atoi(tmp) : 0);
+ int total_count = ((tmp = getSetting(header->user, NULL, "bombs")) ? atoi(tmp) : 0);
+ int defused_count = ((tmp = getSetting(header->user, NULL, "bombs_defused")) ? atoi(tmp) : 0);
+ user_count++;
+ total_count++;
+ defused_count++;
+ char buf[10];
+ sprintf(buf, "%d", user_count);
+ setSetting(header->user, header->chan, "bombs", buf);
+ sprintf(buf, "%d", total_count);
+ setSetting(header->user, NULL, "bombs", buf);
+ sprintf(buf, "%d", defused_count);
+ setSetting(header->user, NULL, "bombs_defused", buf);
+
+ if(header->send_notice)
+ reply(header->client, bomb->owner, "FUN_BOMB_DEFUSED", REPLYTYPE_NORMAL, header->user->nick, total_count, user_count, defused_count);
+ funcmd_reply(header, "FUN_BOMB_DEFUSED", REPLYTYPE_NORMAL, header->user->nick, total_count, user_count, defused_count);
+ } else {
+ funcmd_bomb_detonate(bomb);
+ }
+ timeq_del(bomb->timer);
+ if(prev_bomb)
+ prev_bomb->next = bomb->next;
+ else
+ funcmd_bomb_bombs = bomb->next;
+ free(bomb);
+}
+
+static void funcmd_bomb_return(struct funcmd_header_info *header, struct Event *event, char **argv, int argc) {
+ if(!argc) {
+ reply(header->client, header->user, "MODCMD_LESS_PARAM_COUNT");
+ return;
+ }
+ //following ;)
+}
+
+static TIMEQ_CALLBACK(funcmd_bomb_timeout) {
+ struct funcmd_bomb_bomb *cbomb = data;
+ struct funcmd_bomb_bomb *bomb, *prev_bomb = NULL;
+ for(bomb = funcmd_bomb_bombs; bomb; bomb = bomb->next) {
+ if(cbomb == bomb) {
+ cbomb = NULL;
+ break;
+ } else
+ prev_bomb = bomb;
+ }
+ if(cbomb) return;
+ funcmd_bomb_detonate(bomb);
+ if(prev_bomb)
+ prev_bomb->next = bomb->next;
+ else
+ funcmd_bomb_bombs = bomb->next;
+ free(bomb);
+}