rearranged NeonServ code to be modular
[NeonServV5.git] / src / modules / NeonServ.mod / event_neonserv_kick.c
diff --git a/src/modules/NeonServ.mod/event_neonserv_kick.c b/src/modules/NeonServ.mod/event_neonserv_kick.c
new file mode 100644 (file)
index 0000000..8e32d02
--- /dev/null
@@ -0,0 +1,99 @@
+/* event_neonserv_kick.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/>. 
+ */
+
+struct neonserv_event_kick_cache {
+    struct ClientSocket *client;
+    struct UserNode *user;
+    struct UserNode *target;
+    struct ChanNode *chan;
+    int userauth_pending;
+};
+
+static USERAUTH_CALLBACK(neonserv_event_kick_nick_lookup);
+static void neonserv_event_kick_async1(struct neonserv_event_kick_cache *cache);
+static void neonserv_event_kick_async2(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, struct ChanNode *chan);
+
+static void neonserv_event_kick(struct UserNode *user, struct ChanUser *target, char *reason) {
+    struct ChanNode *chan = target->chan;
+    struct ClientSocket *client;
+    if(isBot(target->user)) {
+        client = getChannelBot(chan, 0);
+        struct ClientSocket *bot = client;
+        for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
+            if(client->user == target->user) {
+                break;
+            }
+        }
+        if(!client) return;
+        if(bot && bot != client && (isModeSet(chan->modes, 'i') || isModeSet(chan->modes, 'a') || isModeSet(chan->modes, 'l'))) {
+            struct ChanUser *chanuser = getChanUser(bot->user, chan);
+            if(chanuser && chanuser->flags & CHANUSERFLAG_OPPED)
+                putsock(bot, "INVITE %s %s", target->user->nick, chan->name);
+        }
+        char *key = "";
+        if(isModeSet(chan->modes, 'k')) {
+            key = getModeValue(chan->modes, 'k');
+        }
+        putsock(client, "JOIN %s %s", chan->name, key);
+        return;
+    }
+    client = getBotForChannel(chan);
+    if(!client) return; //we can't "see" this event
+    if(isNetworkService(user)) return; 
+    loadChannelSettings(chan);
+    if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
+    struct neonserv_event_kick_cache *cache = malloc(sizeof(*cache));
+    if (!cache) {
+        perror("malloc() failed");
+        return;
+    }
+    cache->client = client;
+    cache->user = user;
+    cache->target = target->user;
+    cache->chan = target->chan;
+    cache->userauth_pending = 0;
+    if(!(user->flags & USERFLAG_ISAUTHED)) {
+        get_userauth(user, neonserv_event_kick_nick_lookup, cache);
+        cache->userauth_pending++;
+    }
+    if(!(target->user->flags & USERFLAG_ISAUTHED)) {
+        get_userauth(target->user, neonserv_event_kick_nick_lookup, cache);
+        cache->userauth_pending++;
+    }
+    neonserv_event_kick_async1(cache);
+}
+
+static USERAUTH_CALLBACK(neonserv_event_kick_nick_lookup) {
+    struct neonserv_event_kick_cache *cache = data;
+    cache->userauth_pending--;
+    neonserv_event_kick_async1(cache);
+}
+
+static void neonserv_event_kick_async1(struct neonserv_event_kick_cache *cache) {
+    if(cache->userauth_pending == 0) {
+        neonserv_event_kick_async2(cache->client, cache->user, cache->target, cache->chan);
+        free(cache);
+    }
+}
+
+static void neonserv_event_kick_async2(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, struct ChanNode *chan) {
+    if(isUserProtected(chan, target, user)) {
+        char buf[MAXLEN];
+        putsock(client, "KICK %s %s :%s", chan->name, user->nick, build_language_string(user, buf, "NS_USER_PROTECTED", target->nick));
+        putsock(client, "INVITE %s %s", target->nick, chan->name);
+    }
+}