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