added some code & compiler information to cmd_netinfo
[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     check_mysql();
45     while((mask = nextmask)) {
46         nextmask = strstr(mask, ",");
47         if(nextmask) {
48             *nextmask = '\0';
49             nextmask++;
50         }
51         provided_masks++;
52         skip = 0;
53         match_count = 0;
54         mask = make_banmask(mask, hostmask_buffer);
55         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
56             cuser = chanuser->user;
57             sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
58             if(!match(mask, usermask)) {
59                 cuser->flags |= USERFLAG_SCRIPTFLAG1; //we mark the user as 'matching'
60                 if(isNetworkService(chanuser->user)) {
61                     reply(textclient, user, "NS_SERVICE_IMMUNE", chanuser->user->nick);
62                     skip = 1;
63                     break;
64                 }
65                 if(isUserProtected(chan, cuser, user)) {
66                     reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
67                     skip = 1;
68                     break;
69                 }
70                 match_count++;
71                 if(match_count > 4 && (match_count * 3) > chan->usercount && !isGodMode(user)) {
72                     skip = 1;
73                     reply(textclient, user, "NS_LAME_MASK", mask);
74                     break;
75                 }
76             }
77         }
78         if(!skip) {
79             done_masks++;
80             modeBufferBan(modeBuf, mask);
81         }
82     }
83     total_match = 0; // count all users marked as 'matching'
84     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
85         cuser = chanuser->user;
86         if(cuser->flags & USERFLAG_SCRIPTFLAG1) {
87             cuser->flags &= ~USERFLAG_SCRIPTFLAG1;
88             total_match++;
89         }
90     }
91     freeModeBuffer(modeBuf);
92     if(done_masks == provided_masks)
93         reply(getTextBot(), user, "NS_BAN_DONE", done_masks, chan->name, total_match);
94     else
95         reply(getTextBot(), user, "NS_BAN_FAIL", client->user->nick);
96 }