tried to reorder the program structure and build process
[NeonServV5.git] / src / cmd_neonserv_trim.c
diff --git a/src/cmd_neonserv_trim.c b/src/cmd_neonserv_trim.c
new file mode 100644 (file)
index 0000000..67501a7
--- /dev/null
@@ -0,0 +1,124 @@
+
+#include "cmd_neonserv.h"
+
+/*
+* argv[0]  target (format: minaccess-maxaccess/users/bans)
+* argv[1]  duration
+*/
+static USERLIST_CALLBACK(neonserv_cmd_trim_userlist_lookup);
+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);
+
+struct neonserv_cmd_trim_cache {
+    struct ClientSocket *client, *textclient;
+    struct UserNode *user;
+    struct Event *event;
+    int min_access;
+    int max_access;
+    int duration;
+};
+
+CMD_BIND(neonserv_cmd_trim) {
+    if(stricmp(argv[0], "bans") && !checkChannelAccess(user, chan, "channel_candel", 0, 0)) {
+        if(isGodMode(user)) {
+            event->flags |= CMDFLAG_OPLOG;
+        } else {
+            reply(getTextBot(), user, "NS_ACCESS_DENIED");
+            return;
+        }
+    }
+    int min_access, max_access;
+    if(!stricmp(argv[0], "users")) {
+        min_access = 1;
+        max_access = getChannelAccess(user, chan, 0) - 1;
+    } else if(!stricmp(argv[0], "bans")) {
+        if(!checkChannelAccess(user, chan, "channel_staticban", 0, 0)) {
+            if(isGodMode(user)) {
+                event->flags |= CMDFLAG_OPLOG;
+            } else {
+                reply(getTextBot(), user, "NS_ACCESS_DENIED");
+                return;
+            }
+        }
+        //TODO: TRIM BANS
+        return;
+    } else {
+        char *seperator = strstr(argv[0], "-");
+        if(seperator) {
+            *seperator = '\0';
+            seperator++;
+            min_access = atoi(argv[0]);
+            max_access = atoi(seperator);
+            if(max_access < min_access) {
+                reply(getTextBot(), user, "NS_INVALID_ACCESS_RANGE", min_access, max_access);
+                return;
+            }
+        } else {
+            min_access = atoi(argv[0]);
+            max_access = min_access;
+        }
+        if(max_access >= getChannelAccess(user, chan, 0)) {
+            if(isGodMode(user)) {
+                event->flags |= CMDFLAG_OPLOG;
+            } else {
+                reply(getTextBot(), user, "NS_NO_ACCESS");
+                return;
+            }
+        }
+    }
+    //parse duration...
+    int duration = strToTime(user, argv[1]);
+    if(duration < 30) {
+        reply(getTextBot(), user, "NS_TRIM_DURATION_TOO_SHORT", 30);
+        return;
+    }
+    struct neonserv_cmd_trim_cache *cache = malloc(sizeof(*cache));
+    if (!cache) {
+        perror("malloc() failed");
+        return;
+    }
+    cache->client = client;
+    cache->textclient = getTextBot();
+    cache->user = user;
+    cache->event = event;
+    cache->min_access = min_access;
+    cache->max_access = max_access;
+    cache->duration = duration;
+    get_userlist_with_invisible(chan, neonserv_cmd_trim_userlist_lookup, cache);
+}
+
+static USERLIST_CALLBACK(neonserv_cmd_trim_userlist_lookup) {
+    struct neonserv_cmd_trim_cache *cache = data;
+    //got userlist
+    neonserv_cmd_trim_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->min_access, cache->max_access, cache->duration);
+    free(cache);
+}
+
+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) {
+    MYSQL_RES *res;
+    MYSQL_ROW row;
+    int trim_count = 0, is_here;
+    struct ChanUser *chanuser;
+    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);
+    res = mysql_use();
+    while ((row = mysql_fetch_row(res)) != NULL) {
+        if(!strcmp(row[0], "0") || time(0) - atoi(row[0]) >= duration) {
+            //check if the user is currently in the channel
+            is_here = 0;
+            for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
+                if((chanuser->user->flags & USERFLAG_ISAUTHED) && !strcmp(chanuser->user->auth, row[1])) {
+                    is_here = 1;
+                    break;
+                }
+            }
+            if(!is_here) {
+                //delete the user
+                trim_count++;
+                printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[2]);
+            }
+        }
+    }
+    char timeBuf[MAXLEN];
+    reply(getTextBot(), user, "NS_TRIM_DONE", trim_count, min_access, max_access, chan->name, timeToStr(user, duration, 3, timeBuf));
+    if(trim_count)
+        logEvent(event);
+}