execute mysql_check only if a query fails.
[NeonServV5.git] / cmd_neonserv_ban.c
1
2 /*
3 * argv[0-*]    nick[,*auth[,*!*@mask[...]]]
4 */
5 static USERLIST_CALLBACK(neonserv_cmd_ban_userlist_lookup);
6 static void neonserv_cmd_ban_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *masks);
7
8 struct neonserv_cmd_ban_cache {
9     struct ClientSocket *client, *textclient;
10     struct UserNode *user;
11     char *masks;
12 };
13
14 static CMD_BIND(neonserv_cmd_ban) {
15     struct neonserv_cmd_ban_cache *cache = malloc(sizeof(*cache));
16     if (!cache) {
17         perror("malloc() failed");
18         return;
19     }
20     cache->client = client;
21     cache->textclient = getTextBot();
22     cache->user = user;
23     cache->masks = strdup(merge_argv_char(argv, 0, argc, ','));
24     get_userlist(chan, neonserv_cmd_ban_userlist_lookup, cache);
25 }
26
27 static USERLIST_CALLBACK(neonserv_cmd_ban_userlist_lookup) {
28     struct neonserv_cmd_ban_cache *cache = data;
29     neonserv_cmd_ban_async1(cache->client, cache->textclient, cache->user, chan, cache->masks);
30     free(cache->masks);
31     free(cache);
32 }
33
34 static void neonserv_cmd_ban_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, char *masks) {
35     int done_masks = 0, provided_masks = 0, skip, match_count, total_match;
36     char *mask, *nextmask;
37     char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
38     char usermask[NICKLEN+USERLEN+HOSTLEN+3];
39     struct UserNode *cuser;
40     struct ChanUser *chanuser;
41     struct ModeBuffer *modeBuf;
42     modeBuf = initModeBuffer(client, chan);
43     nextmask = masks;
44     while((mask = nextmask)) {
45         nextmask = strstr(mask, ",");
46         if(nextmask) {
47             *nextmask = '\0';
48             nextmask++;
49         }
50         provided_masks++;
51         skip = 0;
52         match_count = 0;
53         mask = make_banmask(mask, hostmask_buffer);
54         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
55             cuser = chanuser->user;
56             sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
57             if(!match(mask, usermask)) {
58                 cuser->flags |= USERFLAG_SCRIPTFLAG1; //we mark the user as 'matching'
59                 if(isNetworkService(chanuser->user)) {
60                     reply(textclient, user, "NS_SERVICE_IMMUNE", chanuser->user->nick);
61                     skip = 1;
62                     break;
63                 }
64                 if(isUserProtected(chan, cuser, user)) {
65                     reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
66                     skip = 1;
67                     break;
68                 }
69                 match_count++;
70                 if(match_count > 4 && (match_count * 3) > chan->usercount && !isGodMode(user)) {
71                     skip = 1;
72                     reply(textclient, user, "NS_LAME_MASK", mask);
73                     break;
74                 }
75             }
76         }
77         if(!skip) {
78             done_masks++;
79             modeBufferBan(modeBuf, mask);
80         }
81     }
82     total_match = 0; // count all users marked as 'matching'
83     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
84         cuser = chanuser->user;
85         if(cuser->flags & USERFLAG_SCRIPTFLAG1) {
86             cuser->flags &= ~USERFLAG_SCRIPTFLAG1;
87             total_match++;
88         }
89     }
90     freeModeBuffer(modeBuf);
91     if(done_masks == provided_masks)
92         reply(getTextBot(), user, "NS_BAN_DONE", done_masks, chan->name, total_match);
93     else
94         reply(getTextBot(), user, "NS_BAN_FAIL", client->user->nick);
95 }