added full half-op support
[NeonServV5.git] / src / event_neonserv_mode.c
index 583f2a26df8051c35dc18cc93b6a94b397514866..68138530b2b5b4381c5922882194cc9e4e48a5a0 100644 (file)
@@ -1,5 +1,5 @@
-/* event_neonserv_mode.c - NeonServ v5.2
- * 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
@@ -76,29 +76,36 @@ static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserN
     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 || row[4] == 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++) {
@@ -110,40 +117,63 @@ 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++];
                 cuser = searchUserByNick(carg);
-                if(modes[i] == 'o' && !(add && isBot(cuser))) {
+                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 && caccess < db_getop) {
+                        if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                            modeBufferSet(modeBuf, !add, modes[i], carg);
+                        break;
                     }
-                    if(db_canop == -1) {
+                } 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') {
+                } 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) {
+                    if(db_canvoice == -1 && caccess < db_getvoice) {
                         if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
                             modeBufferSet(modeBuf, !add, modes[i], carg);
                         break;
                     }
                 }
-                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);
+                        //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;
@@ -152,6 +182,8 @@ static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserN
                         reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
                         if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
                             modeBufferSet(modeBuf, !add, modes[i], carg);
+                        if(uaccess < db_getop)
+                            deop_user = 1;
                         break;
                     }
                 }
@@ -168,6 +200,8 @@ static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserN
                 if(db_canban == -1) {
                     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];
@@ -191,8 +225,11 @@ static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserN
                         reply(textclient, user, "NS_LAME_MASK_WARNING", carg, match_count);
                     }
                 }
-                if(skip && !neonserv_cmd_mode_botwar_detect(client, user, chan)) {
-                    modeBufferSet(modeBuf, !add, modes[i], carg);
+                if(skip) {
+                    if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
+                        modeBufferSet(modeBuf, !add, modes[i], carg);
+                    if(uaccess < db_getop)
+                        deop_user = 1;
                 }
                 break;
             default:
@@ -213,6 +250,8 @@ 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;
                             }
                             if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
                                 modeBufferSet(modeBuf, add, modes[i], modelock_val);
@@ -226,6 +265,8 @@ 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);
                             if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
@@ -245,6 +286,8 @@ 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;
                     }
                     if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
                         modeBufferSet(modeBuf, !add, modes[i], carg);
@@ -253,6 +296,9 @@ static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserN
                 break;
         }
     }
+    if(deop_user && !neonserv_cmd_mode_botwar_detect(client, user, chan)) {
+        modeBufferDeop(modeBuf, user->nick);
+    }
     freeModeBuffer(modeBuf);
     freeModeNode(modelock);
 }
@@ -271,13 +317,14 @@ static int neonserv_cmd_mode_botwar_detect(struct ClientSocket *client, struct U
             //BOTWAR!
             chanuser->changeTime = time(0);
             if(chanuser->chageEvents > 0) {
-                putsock(client, "NOTICE @%s :%s %s", chan->name, get_language_string(user, "NS_BOTWAR_DETECTED"), (BOTWAR_ALERT_CHAN ? get_language_string(user, "NS_BOTWAR_REPORTED") : ""));
-                if(BOTWAR_ALERT_CHAN) {
-                    struct ChanNode *alertchan = getChanByName(BOTWAR_ALERT_CHAN);
+                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 && (alertclient = getBotForChannel(chan)) != NULL) {
+                    if(alertchan_chan && (alertclient = getBotForChannel(chan)) != NULL) {
                         char msgBuf[MAXLEN];
-                        putsock(alertclient, "PRIVMSG %s :%s", alertchan->name, build_language_string(NULL, msgBuf, "NS_BOTWAR_ALERT", chan->name, user->nick));
+                        putsock(alertclient, "PRIVMSG %s :%s", alertchan_chan->name, build_language_string(NULL, msgBuf, "NS_BOTWAR_ALERT", chan->name, user->nick));
                     }
                 }
             }