added GPL header to all files and added INSTALL AUTHORS COPYING files.
[NeonServV5.git] / src / cmd_neonserv_trim.c
1 /* cmd_neonserv_trim.c - NeonServ v5.0
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     if(!stricmp(argv[0], "users")) {
47         min_access = 1;
48         max_access = getChannelAccess(user, chan, 0) - 1;
49     } else if(!stricmp(argv[0], "bans")) {
50         if(!checkChannelAccess(user, chan, "channel_staticban", 0, 0)) {
51             if(isGodMode(user)) {
52                 event->flags |= CMDFLAG_OPLOG;
53             } else {
54                 reply(getTextBot(), user, "NS_ACCESS_DENIED");
55                 return;
56             }
57         }
58         //TODO: TRIM BANS
59         return;
60     } else {
61         char *seperator = strstr(argv[0], "-");
62         if(seperator) {
63             *seperator = '\0';
64             seperator++;
65             min_access = atoi(argv[0]);
66             max_access = atoi(seperator);
67             if(max_access < min_access) {
68                 reply(getTextBot(), user, "NS_INVALID_ACCESS_RANGE", min_access, max_access);
69                 return;
70             }
71         } else {
72             min_access = atoi(argv[0]);
73             max_access = min_access;
74         }
75         if(max_access >= getChannelAccess(user, chan, 0)) {
76             if(isGodMode(user)) {
77                 event->flags |= CMDFLAG_OPLOG;
78             } else {
79                 reply(getTextBot(), user, "NS_NO_ACCESS");
80                 return;
81             }
82         }
83     }
84     //parse duration...
85     int duration = strToTime(user, argv[1]);
86     if(duration < 30) {
87         reply(getTextBot(), user, "NS_TRIM_DURATION_TOO_SHORT", 30);
88         return;
89     }
90     struct neonserv_cmd_trim_cache *cache = malloc(sizeof(*cache));
91     if (!cache) {
92         perror("malloc() failed");
93         return;
94     }
95     cache->client = client;
96     cache->textclient = getTextBot();
97     cache->user = user;
98     cache->event = event;
99     cache->min_access = min_access;
100     cache->max_access = max_access;
101     cache->duration = duration;
102     get_userlist_with_invisible(chan, neonserv_cmd_trim_userlist_lookup, cache);
103 }
104
105 static USERLIST_CALLBACK(neonserv_cmd_trim_userlist_lookup) {
106     struct neonserv_cmd_trim_cache *cache = data;
107     //got userlist
108     neonserv_cmd_trim_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->min_access, cache->max_access, cache->duration);
109     free(cache);
110 }
111
112 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) {
113     MYSQL_RES *res;
114     MYSQL_ROW row;
115     int trim_count = 0, is_here;
116     struct ChanUser *chanuser;
117     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);
118     res = mysql_use();
119     while ((row = mysql_fetch_row(res)) != NULL) {
120         if(!strcmp(row[0], "0") || time(0) - atoi(row[0]) >= duration) {
121             //check if the user is currently in the channel
122             is_here = 0;
123             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
124                 if((chanuser->user->flags & USERFLAG_ISAUTHED) && !strcmp(chanuser->user->auth, row[1])) {
125                     is_here = 1;
126                     break;
127                 }
128             }
129             if(!is_here) {
130                 //delete the user
131                 trim_count++;
132                 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[2]);
133             }
134         }
135     }
136     char timeBuf[MAXLEN];
137     reply(getTextBot(), user, "NS_TRIM_DONE", trim_count, min_access, max_access, chan->name, timeToStr(user, duration, 3, timeBuf));
138     if(trim_count)
139         logEvent(event);
140 }