*** VERSION 5.2.0 ***
[NeonServV5.git] / src / cmd_neonserv_trim.c
1 /* cmd_neonserv_trim.c - NeonServ v5.2
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "cmd_neonserv.h"
19
20 /*
21 * argv[0]  target (format: minaccess-maxaccess/users/bans)
22 * argv[1]  duration
23 */
24 static USERLIST_CALLBACK(neonserv_cmd_trim_userlist_lookup);
25 static void neonserv_cmd_trim_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, int min_access, int max_access, int duration);
26
27 struct neonserv_cmd_trim_cache {
28     struct ClientSocket *client, *textclient;
29     struct UserNode *user;
30     struct Event *event;
31     int min_access;
32     int max_access;
33     int duration;
34 };
35
36 CMD_BIND(neonserv_cmd_trim) {
37     if(stricmp(argv[0], "bans") && !checkChannelAccess(user, chan, "channel_candel", 0, 0)) {
38         if(isGodMode(user)) {
39             event->flags |= CMDFLAG_OPLOG;
40         } else {
41             reply(getTextBot(), user, "NS_ACCESS_DENIED");
42             return;
43         }
44     }
45     int min_access, max_access;
46     int duration = strToTime(user, argv[1]);
47     if(duration < 30) {
48         reply(getTextBot(), user, "NS_TRIM_DURATION_TOO_SHORT", 30);
49         return;
50     }
51     if(!stricmp(argv[0], "users")) {
52         min_access = 1;
53         max_access = getChannelAccess(user, chan, 0) - 1;
54     } else if(!stricmp(argv[0], "bans")) {
55         if(!checkChannelAccess(user, chan, "channel_staticban", 0, 0)) {
56             if(isGodMode(user)) {
57                 event->flags |= CMDFLAG_OPLOG;
58             } else {
59                 reply(getTextBot(), user, "NS_ACCESS_DENIED");
60                 return;
61             }
62         }
63         MYSQL_RES *res;
64         MYSQL_ROW row;
65         char nameBuf[20];
66         printf_mysql_query("SELECT `ban_mask`, `ban_id`, `ban_timeout` FROM `bans` WHERE `ban_channel` = '%d' AND `ban_triggered` < %d", chan->channel_id, (int) (time(0) - duration));
67         res = mysql_use();
68         int bancount = mysql_num_rows(res);
69         struct ModeBuffer *modenode = initModeBuffer(client, chan);
70         while ((row = mysql_fetch_row(res)) != NULL) {
71             if(strcmp(row[2], "0")) {
72                 sprintf(nameBuf, "ban_%s", row[1]);
73                 timeq_del_name(nameBuf);
74             }
75             printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[1]);
76             modeBufferUnban(modenode, row[0]);
77         }
78         freeModeBuffer(modenode);
79         char timeBuf[MAXLEN];
80         reply(getTextBot(), user, "NS_TRIM_BAN_DONE", bancount, chan->name, timeToStr(user, duration, 3, timeBuf));
81         if(bancount)
82             logEvent(event);
83         return;
84     } else {
85         char *seperator = strstr(argv[0], "-");
86         if(seperator) {
87             *seperator = '\0';
88             seperator++;
89             min_access = atoi(argv[0]);
90             max_access = atoi(seperator);
91             if(max_access < min_access) {
92                 reply(getTextBot(), user, "NS_INVALID_ACCESS_RANGE", min_access, max_access);
93                 return;
94             }
95         } else {
96             min_access = atoi(argv[0]);
97             max_access = min_access;
98         }
99         if(max_access >= getChannelAccess(user, chan, 0)) {
100             if(isGodMode(user)) {
101                 event->flags |= CMDFLAG_OPLOG;
102             } else {
103                 reply(getTextBot(), user, "NS_NO_ACCESS");
104                 return;
105             }
106         }
107     }
108     struct neonserv_cmd_trim_cache *cache = malloc(sizeof(*cache));
109     if (!cache) {
110         perror("malloc() failed");
111         return;
112     }
113     cache->client = client;
114     cache->textclient = getTextBot();
115     cache->user = user;
116     cache->event = event;
117     cache->min_access = min_access;
118     cache->max_access = max_access;
119     cache->duration = duration;
120     get_userlist_with_invisible(chan, neonserv_cmd_trim_userlist_lookup, cache);
121 }
122
123 static USERLIST_CALLBACK(neonserv_cmd_trim_userlist_lookup) {
124     struct neonserv_cmd_trim_cache *cache = data;
125     //got userlist
126     neonserv_cmd_trim_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->min_access, cache->max_access, cache->duration);
127     free(cache);
128 }
129
130 static void neonserv_cmd_trim_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, int min_access, int max_access, int duration) {
131     MYSQL_RES *res;
132     MYSQL_ROW row;
133     int trim_count = 0, is_here;
134     struct ChanUser *chanuser;
135     printf_mysql_query("SELECT `chanuser_seen`, `user_user`, `chanuser_id` FROM `chanusers` LEFT JOIN `users` ON `chanuser_uid` = `user_id` WHERE `chanuser_cid` = '%d' AND `chanuser_access` >= '%d' AND `chanuser_access` <= '%d'", chan->channel_id, min_access, max_access);
136     res = mysql_use();
137     while ((row = mysql_fetch_row(res)) != NULL) {
138         if(!strcmp(row[0], "0") || time(0) - atoi(row[0]) >= duration) {
139             //check if the user is currently in the channel
140             is_here = 0;
141             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
142                 if((chanuser->user->flags & USERFLAG_ISAUTHED) && !strcmp(chanuser->user->auth, row[1])) {
143                     is_here = 1;
144                     break;
145                 }
146             }
147             if(!is_here) {
148                 //delete the user
149                 trim_count++;
150                 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[2]);
151             }
152         }
153     }
154     char timeBuf[MAXLEN];
155     reply(getTextBot(), user, "NS_TRIM_DONE", trim_count, min_access, max_access, chan->name, timeToStr(user, duration, 3, timeBuf));
156     if(trim_count)
157         logEvent(event);
158 }