1 /* cmd_neonserv_trim.c - NeonServ v5.0
2 * Copyright (C) 2011 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "cmd_neonserv.h"
21 * argv[0] target (format: minaccess-maxaccess/users/bans)
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);
27 struct neonserv_cmd_trim_cache {
28 struct ClientSocket *client, *textclient;
29 struct UserNode *user;
36 CMD_BIND(neonserv_cmd_trim) {
37 if(stricmp(argv[0], "bans") && !checkChannelAccess(user, chan, "channel_candel", 0, 0)) {
39 event->flags |= CMDFLAG_OPLOG;
41 reply(getTextBot(), user, "NS_ACCESS_DENIED");
45 int min_access, max_access;
46 if(!stricmp(argv[0], "users")) {
48 max_access = getChannelAccess(user, chan, 0) - 1;
49 } else if(!stricmp(argv[0], "bans")) {
50 if(!checkChannelAccess(user, chan, "channel_staticban", 0, 0)) {
52 event->flags |= CMDFLAG_OPLOG;
54 reply(getTextBot(), user, "NS_ACCESS_DENIED");
61 char *seperator = strstr(argv[0], "-");
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);
72 min_access = atoi(argv[0]);
73 max_access = min_access;
75 if(max_access >= getChannelAccess(user, chan, 0)) {
77 event->flags |= CMDFLAG_OPLOG;
79 reply(getTextBot(), user, "NS_NO_ACCESS");
85 int duration = strToTime(user, argv[1]);
87 reply(getTextBot(), user, "NS_TRIM_DURATION_TOO_SHORT", 30);
90 struct neonserv_cmd_trim_cache *cache = malloc(sizeof(*cache));
92 perror("malloc() failed");
95 cache->client = client;
96 cache->textclient = getTextBot();
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);
105 static USERLIST_CALLBACK(neonserv_cmd_trim_userlist_lookup) {
106 struct neonserv_cmd_trim_cache *cache = data;
108 neonserv_cmd_trim_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->min_access, cache->max_access, cache->duration);
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) {
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);
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
123 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
124 if((chanuser->user->flags & USERFLAG_ISAUTHED) && !strcmp(chanuser->user->auth, row[1])) {
132 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[2]);
136 char timeBuf[MAXLEN];
137 reply(getTextBot(), user, "NS_TRIM_DONE", trim_count, min_access, max_access, chan->name, timeToStr(user, duration, 3, timeBuf));