+
+/*
+* argv[0] nick|*auth|*!*@mask
+* argv[1-*] reason
+*/
+static USERLIST_CALLBACK(neonserv_cmd_addban_userlist_lookup);
+static void neonserv_cmd_addban_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *mas, char *reason);
+
+struct neonserv_cmd_addban_cache {
+ struct ClientSocket *client, *textclient;
+ struct UserNode *user;
+ char *mask;
+ char *reason;
+};
+
+static CMD_BIND(neonserv_cmd_addban) {
+ struct neonserv_cmd_addban_cache *cache = malloc(sizeof(*cache));
+ if (!cache) {
+ perror("malloc() failed");
+ return;
+ }
+ cache->client = client;
+ cache->textclient = getTextBot();
+ cache->user = user;
+ cache->mask = strdup(argv[0]);
+ if(argc > 1) {
+ cache->reason = strdup(merge_argv(argv, 1, argc));
+ } else
+ cache->reason = NULL;
+ get_userlist(chan, neonserv_cmd_addban_userlist_lookup, cache);
+}
+
+static USERLIST_CALLBACK(neonserv_cmd_addban_userlist_lookup) {
+ struct neonserv_cmd_addban_cache *cache = data;
+ neonserv_cmd_addban_async1(cache->client, cache->textclient, cache->user, chan, cache->mask, (cache->reason ? cache->reason : "Bye."));
+ free(cache->mask);
+ if(cache->reason)
+ free(cache->reason);
+ free(cache);
+}
+
+static void neonserv_cmd_addban_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *mask, char *reason) {
+ int match_count = 0;
+ char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
+ char usermask[NICKLEN+USERLEN+HOSTLEN+3];
+ struct UserNode *cuser;
+ struct ChanUser *chanuser;
+ check_mysql();
+ mask = make_banmask(mask, hostmask_buffer);
+ for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
+ cuser = chanuser->user;
+ sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
+ if(!match(mask, usermask)) {
+ if(isNetworkService(chanuser->user)) {
+ reply(textclient, user, "NS_SERVICE_IMMUNE", chanuser->user->nick);
+ return;
+ }
+ if(isUserProtected(chan, cuser, user)) {
+ reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
+ return;
+ }
+ match_count++;
+ if(match_count > 4 && (match_count * 3) > chan->usercount && !isGodMode(user)) {
+ reply(textclient, user, "NS_LAME_MASK", mask);
+ return;
+ }
+ }
+ }
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
+ int userid;
+ res = mysql_use();
+ if ((row = mysql_fetch_row(res)) != NULL)
+ userid = atoi(row[0]);
+ else
+ return;
+ //add the ban
+ printf_mysql_query("INSERT INTO `bans` (`ban_channel`, `ban_mask`, `ban_triggered`, `ban_owner`, `ban_reason`) VALUES ('%d', '%s', '%u', '%d', '%s')", chan->channel_id, escape_string(mask), (unsigned int) time(0), userid, escape_string(reason));
+ reply(getTextBot(), user, "NS_ADDBAN_DONE", mask, chan->name, match_count);
+}