Another year is about to end... So we have to update these damn copyright information :P
[NeonServV5.git] / src / cmd_neonserv_trim.c
index 67501a7107033f4ed77b84da759f77b9c6524fc4..65a0e73482f76d6a62ffbb2710dd8174670c28e2 100644 (file)
@@ -1,3 +1,19 @@
+/* cmd_neonserv_trim.c - NeonServ v5.3
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
 
 #include "cmd_neonserv.h"
 
@@ -18,7 +34,7 @@ struct neonserv_cmd_trim_cache {
 };
 
 CMD_BIND(neonserv_cmd_trim) {
-    if(stricmp(argv[0], "bans") && !checkChannelAccess(user, chan, "channel_candel", 0, 0)) {
+    if(stricmp(argv[0], "bans") && !checkChannelAccess(user, chan, "channel_candel", 0)) {
         if(isGodMode(user)) {
             event->flags |= CMDFLAG_OPLOG;
         } else {
@@ -27,11 +43,16 @@ CMD_BIND(neonserv_cmd_trim) {
         }
     }
     int min_access, max_access;
+    int duration = strToTime(user, argv[1]);
+    if(duration < 30) {
+        reply(getTextBot(), user, "NS_TRIM_DURATION_TOO_SHORT", 30);
+        return;
+    }
     if(!stricmp(argv[0], "users")) {
         min_access = 1;
-        max_access = getChannelAccess(user, chan, 0) - 1;
+        max_access = getChannelAccess(user, chan) - 1;
     } else if(!stricmp(argv[0], "bans")) {
-        if(!checkChannelAccess(user, chan, "channel_staticban", 0, 0)) {
+        if(!checkChannelAccess(user, chan, "channel_staticban", 0)) {
             if(isGodMode(user)) {
                 event->flags |= CMDFLAG_OPLOG;
             } else {
@@ -39,7 +60,26 @@ CMD_BIND(neonserv_cmd_trim) {
                 return;
             }
         }
-        //TODO: TRIM BANS
+        MYSQL_RES *res;
+        MYSQL_ROW row;
+        char nameBuf[20];
+        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));
+        res = mysql_use();
+        int bancount = mysql_num_rows(res);
+        struct ModeBuffer *modenode = initModeBuffer(client, chan);
+        while ((row = mysql_fetch_row(res)) != NULL) {
+            if(strcmp(row[2], "0")) {
+                sprintf(nameBuf, "ban_%s", row[1]);
+                timeq_del_name(nameBuf);
+            }
+            printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[1]);
+            modeBufferUnban(modenode, row[0]);
+        }
+        freeModeBuffer(modenode);
+        char timeBuf[MAXLEN];
+        reply(getTextBot(), user, "NS_TRIM_BAN_DONE", bancount, chan->name, timeToStr(user, duration, 3, timeBuf));
+        if(bancount)
+            logEvent(event);
         return;
     } else {
         char *seperator = strstr(argv[0], "-");
@@ -56,7 +96,7 @@ CMD_BIND(neonserv_cmd_trim) {
             min_access = atoi(argv[0]);
             max_access = min_access;
         }
-        if(max_access >= getChannelAccess(user, chan, 0)) {
+        if(max_access >= getChannelAccess(user, chan)) {
             if(isGodMode(user)) {
                 event->flags |= CMDFLAG_OPLOG;
             } else {
@@ -65,12 +105,6 @@ CMD_BIND(neonserv_cmd_trim) {
             }
         }
     }
-    //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");