added full half-op support
[NeonServV5.git] / src / event_neonserv_mode.c
index 3eec5751de0e116a0dad3c78adb673f736a53a9d..68138530b2b5b4381c5922882194cc9e4e48a5a0 100644 (file)
@@ -1,5 +1,5 @@
-/* event_neonserv_mode.c - NeonServ v5.0
- * Copyright (C) 2011  Philipp Kreil (pk910)
+/* event_neonserv_mode.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
@@ -17,6 +17,9 @@
 
 static USERLIST_CALLBACK(neonserv_event_mode_userlist_lookup);
 static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc);
+static int neonserv_cmd_mode_botwar_detect(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan);
+
+static int botwar_detect_executed;
 
 struct neonserv_event_mode_cache {
     struct ClientSocket *client;
@@ -29,7 +32,7 @@ struct neonserv_event_mode_cache {
 static void neonserv_event_mode(struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
     struct ClientSocket *client = getBotForChannel(chan);
     if(!client) return; //we can't "see" this event
-    if(user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)) return;
+    if(isNetworkService(user)) return;
     loadChannelSettings(chan);
     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
     struct neonserv_event_mode_cache *cache = malloc(sizeof(*cache));
@@ -69,32 +72,40 @@ static USERLIST_CALLBACK(neonserv_event_mode_userlist_lookup) {
 
 static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
     struct ClientSocket *textclient = ((client->flags & SOCKET_FLAG_PREFERRED) ? client : get_prefered_bot(client->botid));
+    botwar_detect_executed = 0;
     MYSQL_ROW row, defaults = NULL;
     int i, arg, add = 1, skip = 0;
     unsigned int modetype;
-    int db_canop, db_canvoice, db_canban, db_enfmodes;
+    int db_canop, db_canhalfop, db_canvoice, db_canban, db_enfmodes, db_getop, db_gethalfop, db_getvoice;
     struct ModeNode *modelock = createModeNode(NULL);
     struct ModeBuffer *modeBuf;
     struct UserNode *cuser;
     struct ChanUser *chanuser;
+    int with_halfops = get_int_field("General.have_halfop");
     modeBuf = initModeBuffer(client, chan);
-    printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
+    printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes`, `channel_getop`, `channel_getvoice`, `channel_gethalfop`, `channel_canhalfop` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
     row = mysql_fetch_row(mysql_use());
-    if(row[0] == NULL || row[1] == NULL || row[2] == NULL || row[3] == NULL) {
-        printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes` FROM `channels` WHERE `channel_name` = 'defaults'");
+    if(row[0] == NULL || row[1] == NULL || row[2] == NULL || row[3] == NULL || row[4] == NULL || row[5] == NULL || row[6] == NULL || (row[7] == NULL && with_halfops) || (row[8] == NULL && with_halfops)) {
+        printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes`, `channel_getop`, `channel_getvoice`, `channel_gethalfop`, `channel_canhalfop` FROM `channels` WHERE `channel_name` = 'defaults'");
         defaults = mysql_fetch_row(mysql_use());
     }
     db_canop = atoi((row[0] ? row[0] : defaults[0]));
     db_canvoice = atoi((row[1] ? row[1] : defaults[1]));
     db_canban = atoi((row[2] ? row[2] : defaults[2]));
     db_enfmodes = atoi((row[3] ? row[3] : defaults[3]));
+    db_getop = atoi((row[5] ? row[5] : defaults[5]));
+    db_getvoice = atoi((row[6] ? row[6] : defaults[6]));
+    db_gethalfop = (with_halfops ? atoi((row[7] ? row[7] : defaults[7])) : 0);
+    db_canhalfop = (with_halfops ? atoi((row[8] ? row[8] : defaults[8])) : 0);
     if(row[4])
         parseModeString(modelock, row[4]);
     else if(defaults[4])
         parseModeString(modelock, defaults[4]);
-    int uaccess = getChannelAccess(user, chan, 0);
+    int uaccess = getChannelAccess(user, chan);
+    int caccess;
     char *carg;
     int sent_modes_locked = 0;
+    char deop_user = 0;
     char tmp[MAXLEN];
     arg = 0;
     for(i = 0; i < strlen(modes); i++) {
@@ -106,44 +117,73 @@ static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserN
                 add = 0;
                 break;
             case 'o':
+            case 'h':
             case 'v':
                 if(arg == argc) {
                     break;
                 }
                 carg = argv[arg++];
-                if(modes[i] == 'o') {
+                cuser = searchUserByNick(carg);
+                if(!cuser) {
+                    break; //internal Bot error - this should never happen
+                }
+                caccess = getChannelAccess(cuser, chan);
+                if(modes[i] == 'o' && !add && cuser == client->user) {
+                    //someone deopped the bot???
+                    requestOp(client->user, chan);
+                }
+                if((modes[i] == 'o' || (modes[i] == 'h' && !with_halfops)) && !(add && isBot(cuser))) {
                     if(uaccess < db_canop) {
                         reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
                         db_canop = -1;
+                        if(uaccess < db_getop)
+                            deop_user = 1;
                     }
-                    if(db_canop == -1) {
-                        modeBufferSet(modeBuf, !add, modes[i], carg);
+                    if(db_canop == -1 && caccess < db_getop) {
+                        if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                            modeBufferSet(modeBuf, !add, modes[i], carg);
                         break;
                     }
-                } else {
+                } else if(modes[i] == 'h' && with_halfops && !(add && isBot(cuser))) {
+                    if(uaccess < db_canhalfop) {
+                        reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
+                        db_canhalfop = -1;
+                        if(uaccess < db_gethalfop)
+                            deop_user = 1;
+                    }
+                    if(db_canhalfop == -1 && caccess < db_gethalfop) {
+                        if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                            modeBufferSet(modeBuf, !add, modes[i], carg);
+                        break;
+                    }
+                } else if(modes[i] == 'v' && !(add && isBot(cuser))) {
                     if(uaccess < db_canvoice) {
                         reply(textclient, user, "NS_MODE_ENFVOICE", chan->name);
                         db_canvoice = -1;
+                        if(uaccess < db_getop)
+                            deop_user = 1;
                     }
-                    if(db_canvoice == -1) {
-                        modeBufferSet(modeBuf, !add, modes[i], carg);
+                    if(db_canvoice == -1 && caccess < db_getvoice) {
+                        if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                            modeBufferSet(modeBuf, !add, modes[i], carg);
                         break;
                     }
                 }
-                cuser = searchUserByNick(carg);
-                if(!cuser) {
-                    break; //internal Bot error - this should never happen
-                }
                 if(!add) {
                     //check protection
                     if(isBot(cuser)) {
-                        reply(textclient, user, "NS_SERVICE_IMMUNE", cuser->nick);
-                        modeBufferSet(modeBuf, !add, modes[i], carg);
+                        //don't send this - just try to reop
+                        //reply(textclient, user, "NS_SERVICE_IMMUNE", cuser->nick);
+                        if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                            modeBufferSet(modeBuf, !add, modes[i], carg);
                         break;
                     }
                     if(isUserProtected(chan, cuser, user)) {
                         reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
-                        modeBufferSet(modeBuf, !add, modes[i], carg);
+                        if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                            modeBufferSet(modeBuf, !add, modes[i], carg);
+                        if(uaccess < db_getop)
+                            deop_user = 1;
                         break;
                     }
                 }
@@ -158,7 +198,10 @@ static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserN
                     db_canban = -1;
                 }
                 if(db_canban == -1) {
-                    modeBufferSet(modeBuf, !add, modes[i], carg);
+                    if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                        modeBufferSet(modeBuf, !add, modes[i], carg);
+                    if(uaccess < db_getop)
+                        deop_user = 1;
                     break;
                 }
                 char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
@@ -183,7 +226,10 @@ static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserN
                     }
                 }
                 if(skip) {
-                    modeBufferSet(modeBuf, !add, modes[i], carg);
+                    if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                        modeBufferSet(modeBuf, !add, modes[i], carg);
+                    if(uaccess < db_getop)
+                        deop_user = 1;
                 }
                 break;
             default:
@@ -204,8 +250,11 @@ static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserN
                                 getFullModeString(modelock, tmp);
                                 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
                                 sent_modes_locked = 1;
+                                if(uaccess < db_getop)
+                                    deop_user = 1;
                             }
-                            modeBufferSet(modeBuf, add, modes[i], modelock_val);
+                            if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                                modeBufferSet(modeBuf, add, modes[i], modelock_val);
                             break;
                         }
                     }
@@ -216,9 +265,12 @@ static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserN
                                 getFullModeString(modelock, tmp);
                                 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
                                 sent_modes_locked = 1;
+                                if(uaccess < db_getop)
+                                    deop_user = 1;
                             }
                             sprintf(tmp, "%d", *modelock_val);
-                            modeBufferSet(modeBuf, add, modes[i], tmp);
+                            if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                                modeBufferSet(modeBuf, add, modes[i], tmp);
                             break;
                         }
                     }
@@ -234,13 +286,52 @@ static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserN
                         getFullModeString(modelock, tmp);
                         reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
                         sent_modes_locked = 1;
+                        if(uaccess < db_getop)
+                            deop_user = 1;
                     }
-                    modeBufferSet(modeBuf, !add, modes[i], carg);
+                    if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                        modeBufferSet(modeBuf, !add, modes[i], carg);
                     break;
                 }
                 break;
         }
     }
+    if(deop_user && !neonserv_cmd_mode_botwar_detect(client, user, chan)) {
+        modeBufferDeop(modeBuf, user->nick);
+    }
     freeModeBuffer(modeBuf);
     freeModeNode(modelock);
 }
+
+static int neonserv_cmd_mode_botwar_detect(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan) {
+    struct ChanUser *chanuser = getChanUser(user, chan);
+    if(!chanuser) return 0; //flying super cow?
+    if(time(0) - chanuser->changeTime > BOTWAR_DETECTION_TIME) {
+        chanuser->changeTime = time(0);
+        chanuser->chageEvents = 1;
+    } else {
+        if(!botwar_detect_executed)
+            chanuser->chageEvents++;
+        botwar_detect_executed = 1;
+        if(chanuser->chageEvents >= BOTWAR_DETECTION_EVENTS || chanuser->chageEvents < 0) {
+            //BOTWAR!
+            chanuser->changeTime = time(0);
+            if(chanuser->chageEvents > 0) {
+                char *alertchan = get_string_field("General.alertchan");
+                putsock(client, "NOTICE @%s :%s %s", chan->name, get_language_string(user, "NS_BOTWAR_DETECTED"), (alertchan ? get_language_string(user, "NS_BOTWAR_REPORTED") : ""));
+                if(alertchan) {
+                    struct ChanNode *alertchan_chan = getChanByName(alertchan);
+                    struct ClientSocket *alertclient;
+                    if(alertchan_chan && (alertclient = getBotForChannel(chan)) != NULL) {
+                        char msgBuf[MAXLEN];
+                        putsock(alertclient, "PRIVMSG %s :%s", alertchan_chan->name, build_language_string(NULL, msgBuf, "NS_BOTWAR_ALERT", chan->name, user->nick));
+                    }
+                }
+            }
+            chanuser->chageEvents = -2;
+            return 1;
+        }
+    }
+    return 0;
+}
+