added event_mode
authorpk910 <philipp@zoelle1.de>
Wed, 5 Oct 2011 09:15:21 +0000 (11:15 +0200)
committerpk910 <philipp@zoelle1.de>
Wed, 5 Oct 2011 10:56:38 +0000 (12:56 +0200)
src/UserNode.h
src/bot_NeonServ.c
src/event_neonserv_mode.c [new file with mode: 0644]

index c169024cec96e818e564ebdc6c5bf7ff888bfe7b..051a1539696ad12460cabf4c6c2e4adad485d334 100644 (file)
@@ -49,6 +49,7 @@ struct UserNode {
 };
 
 #define isNetworkService(USER) (USER->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP))
+#define isBot(USER) (USER->flags & (USERFLAG_ISBOT))
 
 void init_UserNode();
 void free_UserNode();
index 06d7826fb73d48e4ca2abeb0caecdae63a1fc548..d9305c189f6bc8a20911e025138eb6fdb06da952 100644 (file)
@@ -129,6 +129,7 @@ static const struct default_language_entry msgtab[] = {
     {"NS_BAN_DONE", "$b%d$b masks added to the %s ban list. (matching %d users)"}, /* {ARGS: 5, "#TestChan", 15} */
     {"NS_BAN_FAIL", "$b%s$b could not ban some of the nicks you provided."}, /* {ARGS: "NeonServ"} */
     {"NS_LAME_MASK", "$b%s$b is a little too general. Try making it more specific."}, /* {ARGS: "*!*@*"} */
+    {"NS_LAME_MASK_WARNING", "$k4WARNING$k: $b%s$b is very general. (matches %d users)"},
     {"NS_SET_HEADER", "Channel Settings for %s:"}, /* {ARGS: "#TestChan"} */
     {"NS_SET_ON", "on"},
     {"NS_SET_OFF", "off"},
@@ -331,7 +332,7 @@ cmd_neonserv_calc.c
 #include "event_neonserv_part.c"
 #include "event_neonserv_quit.c"
 //#include "event_neonserv_kick.c"
-//#include "event_neonserv_mode.c"
+#include "event_neonserv_mode.c"
 #include "event_neonserv_ctcp.c"
 #include "event_neonserv_notice.c"
 #include "event_neonserv_invite.c"
@@ -543,6 +544,7 @@ void init_NeonServ() {
     bind_channotice(neonserv_event_channotice);
     bind_topic(neonserv_event_topic);
     bind_invite(neonserv_event_invite);
+    bind_mode(neonserv_event_mode);
     
     set_trigger_callback(BOTID, neonserv_trigger_callback);
     
diff --git a/src/event_neonserv_mode.c b/src/event_neonserv_mode.c
new file mode 100644 (file)
index 0000000..3eec575
--- /dev/null
@@ -0,0 +1,246 @@
+/* event_neonserv_mode.c - NeonServ v5.0
+ * Copyright (C) 2011  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/>. 
+ */
+
+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 neonserv_event_mode_cache {
+    struct ClientSocket *client;
+    struct UserNode *user;
+    char *modes;
+    char **argv;
+    int argc;
+};
+
+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;
+    loadChannelSettings(chan);
+    if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
+    struct neonserv_event_mode_cache *cache = malloc(sizeof(*cache));
+    char **temp_argv = NULL;
+    if(argc) 
+        temp_argv = malloc(argc*sizeof(*temp_argv));
+    if (!cache || (argc && !temp_argv)) {
+        perror("malloc() failed");
+        return;
+    }
+    if(argc) {
+        int i;
+        for(i = 0; i < argc; i++) {
+            temp_argv[i] = strdup(argv[i]);
+        }
+    }
+    cache->client = client;
+    cache->user = user;
+    cache->modes = strdup(modes);
+    cache->argv = temp_argv;
+    cache->argc = argc;
+    get_userlist_with_invisible(chan, neonserv_event_mode_userlist_lookup, cache);
+}
+
+static USERLIST_CALLBACK(neonserv_event_mode_userlist_lookup) {
+    struct neonserv_event_mode_cache *cache = data;
+    neonserv_event_mode_async1(cache->client, cache->user, chan, cache->modes, cache->argv, cache->argc);
+    if(cache->argc) {
+        int i;
+        for(i = 0; i < cache->argc; i++) {
+            free(cache->argv[i]);
+        }
+        free(cache->argv);
+    }
+    free(cache);
+}
+
+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));
+    MYSQL_ROW row, defaults = NULL;
+    int i, arg, add = 1, skip = 0;
+    unsigned int modetype;
+    int db_canop, db_canvoice, db_canban, db_enfmodes;
+    struct ModeNode *modelock = createModeNode(NULL);
+    struct ModeBuffer *modeBuf;
+    struct UserNode *cuser;
+    struct ChanUser *chanuser;
+    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);
+    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'");
+        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]));
+    if(row[4])
+        parseModeString(modelock, row[4]);
+    else if(defaults[4])
+        parseModeString(modelock, defaults[4]);
+    int uaccess = getChannelAccess(user, chan, 0);
+    char *carg;
+    int sent_modes_locked = 0;
+    char tmp[MAXLEN];
+    arg = 0;
+    for(i = 0; i < strlen(modes); i++) {
+        switch(modes[i]) {
+            case '+':
+                add = 1;
+                break;
+            case '-':
+                add = 0;
+                break;
+            case 'o':
+            case 'v':
+                if(arg == argc) {
+                    break;
+                }
+                carg = argv[arg++];
+                if(modes[i] == 'o') {
+                    if(uaccess < db_canop) {
+                        reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
+                        db_canop = -1;
+                    }
+                    if(db_canop == -1) {
+                        modeBufferSet(modeBuf, !add, modes[i], carg);
+                        break;
+                    }
+                } else {
+                    if(uaccess < db_canvoice) {
+                        reply(textclient, user, "NS_MODE_ENFVOICE", chan->name);
+                        db_canvoice = -1;
+                    }
+                    if(db_canvoice == -1) {
+                        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);
+                        break;
+                    }
+                    if(isUserProtected(chan, cuser, user)) {
+                        reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
+                        modeBufferSet(modeBuf, !add, modes[i], carg);
+                        break;
+                    }
+                }
+                break;
+            case 'b':
+                if(arg == argc) {
+                    break;
+                }
+                carg = argv[arg++];
+                if(uaccess < db_canban) {
+                    reply(textclient, user, "NS_MODE_CANBAN", chan->name);
+                    db_canban = -1;
+                }
+                if(db_canban == -1) {
+                    modeBufferSet(modeBuf, !add, modes[i], carg);
+                    break;
+                }
+                char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
+                char usermask[NICKLEN+USERLEN+HOSTLEN+3];
+                int match_count = 0;
+                carg = make_banmask(carg, hostmask_buffer);
+                if(add) {
+                    for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
+                        cuser = chanuser->user;
+                        sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
+                        if(!match(carg, usermask)) {
+                            if(isUserProtected(chan, cuser, user)) {
+                                reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
+                                skip = 1;
+                                break;
+                            }
+                            match_count++;
+                        }
+                    }
+                    if(match_count > 4 && (match_count * 3) > chan->usercount) {
+                        reply(textclient, user, "NS_LAME_MASK_WARNING", carg, match_count);
+                    }
+                }
+                if(skip) {
+                    modeBufferSet(modeBuf, !add, modes[i], carg);
+                }
+                break;
+            default:
+                modetype = getModeType(modelock, modes[i]);
+                if(modetype == 0) {
+                    break; //unknown mode
+                }
+                
+                if(add && (modetype & CHANNEL_MODE_TYPE) != CHANNEL_MODE_TYPE_D) {
+                    if(arg == argc) {
+                        break;
+                    }
+                    carg = argv[arg++];
+                    if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
+                        char *modelock_val = getModeValue(modelock, modes[i]);
+                        if(stricmp(carg, modelock_val) && uaccess < db_enfmodes && !isGodMode(user)) {
+                            if(!sent_modes_locked) {
+                                getFullModeString(modelock, tmp);
+                                reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
+                                sent_modes_locked = 1;
+                            }
+                            modeBufferSet(modeBuf, add, modes[i], modelock_val);
+                            break;
+                        }
+                    }
+                    if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
+                        int *modelock_val = getModeValue(modelock, modes[i]);
+                        if(atoi(carg) != *modelock_val && uaccess < db_enfmodes && !isGodMode(user)) {
+                            if(!sent_modes_locked) {
+                                getFullModeString(modelock, tmp);
+                                reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
+                                sent_modes_locked = 1;
+                            }
+                            sprintf(tmp, "%d", *modelock_val);
+                            modeBufferSet(modeBuf, add, modes[i], tmp);
+                            break;
+                        }
+                    }
+                } else if(!add && (modetype & CHANNEL_MODE_TYPE) == CHANNEL_MODE_TYPE_B) {
+                    if(arg == argc) {
+                        break;
+                    }
+                    carg = argv[arg++];
+                } else
+                    carg = NULL;
+                if(isModeAffected(modelock, modes[i]) && add == !isModeSet(modelock, modes[i]) && uaccess < db_enfmodes && !isGodMode(user)) {
+                    if(!sent_modes_locked) {
+                        getFullModeString(modelock, tmp);
+                        reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
+                        sent_modes_locked = 1;
+                    }
+                    modeBufferSet(modeBuf, !add, modes[i], carg);
+                    break;
+                }
+                break;
+        }
+    }
+    freeModeBuffer(modeBuf);
+    freeModeNode(modelock);
+}