rearranged NeonServ code to be modular
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_up.c
diff --git a/src/modules/NeonServ.mod/cmd_neonserv_up.c b/src/modules/NeonServ.mod/cmd_neonserv_up.c
new file mode 100644 (file)
index 0000000..0085b5f
--- /dev/null
@@ -0,0 +1,95 @@
+/* cmd_neonserv_up.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"
+
+/*
+* no arguments
+*/
+
+struct neonserv_cmd_up_cache {
+    struct ClientSocket *client;
+    struct ClientSocket *textclient;
+    struct UserNode *user;
+    struct Event *event;
+};
+
+static void neonserv_cmd_up_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event);
+static USERLIST_CALLBACK(neonserv_cmd_up_userlist_lookup);
+
+CMD_BIND(neonserv_cmd_up) {
+    if(isModeSet(chan->modes, 'd') || isModeSet(chan->modes, 'D')) {
+        struct neonserv_cmd_up_cache *cache = malloc(sizeof(*cache));
+        if (!cache) {
+            perror("malloc() failed");
+            return;
+        }
+        cache->client = client;
+        cache->textclient = getTextBot();
+        cache->user = user;
+        cache->event = event;
+        get_userlist_if_invisible(chan, neonserv_cmd_up_userlist_lookup, cache);
+    } else {
+        neonserv_cmd_up_async1(client, getTextBot(), user, chan, event);
+    }
+}
+
+static USERLIST_CALLBACK(neonserv_cmd_up_userlist_lookup) {
+    struct neonserv_cmd_up_cache *cache = data;
+    neonserv_cmd_up_async1(cache->client, cache->textclient, cache->user, chan, cache->event);
+    free(cache);
+}
+
+static void neonserv_cmd_up_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event) {
+    struct ChanUser *chanuser = getChanUser(user, chan);
+    if(!chanuser) {
+        reply(getTextBot(), user, "NS_NOT_ON_CHANNEL_YOU", chan->name);
+        return;
+    }
+    loadChannelSettings(chan);
+    MYSQL_RES *res, *default_res;
+    MYSQL_ROW row, default_row;
+    int chan_getop, chan_getvoice, caccess;
+    printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
+    res = mysql_use();
+    if ((row = mysql_fetch_row(res)) == NULL) return;
+    if(!row[0] || !row[1]) {
+        printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
+        default_res = mysql_use();
+        if ((default_row = mysql_fetch_row(default_res)) == NULL) return;
+        chan_getop = (row[0] ? atoi(row[0]) : atoi(default_row[0]));
+        chan_getvoice = (row[1] ? atoi(row[1]) : atoi(default_row[1]));
+    } else {
+        chan_getop = atoi(row[0]);
+        chan_getvoice = atoi(row[1]);
+    }
+    caccess = getChannelAccess(user, chan);
+    if(caccess >= chan_getop) {
+        if(!(chanuser->flags & CHANUSERFLAG_OPPED)) {
+            putsock(client, "MODE %s +o %s", chan->name, user->nick);
+            logEvent(event);
+        } else
+            reply(getTextBot(), user, "NS_UP_ALREADY_OP", chan->name);
+    } else if(caccess >= chan_getvoice) {
+        if(!(chanuser->flags & CHANUSERFLAG_VOICED)) {
+            putsock(client, "MODE %s +v %s", chan->name, user->nick);
+            logEvent(event);
+        } else
+            reply(getTextBot(), user, "NS_UP_ALREADY_VOICE", chan->name);
+    } else
+        reply(getTextBot(), user, "NS_NOT_ON_USERLIST_YOU", chan->name);
+}