Merge branch 'development'
[NeonServV5.git] / src / ChanNode.c
index bafc3bd055e9ca8263bc5b2529a6b726ccc7eaae..44a335f1cb822f2d7c414ebeef955781c93601c1 100644 (file)
@@ -1,5 +1,5 @@
-/* ChanNode.c - NeonServ v5.2
- * Copyright (C) 2011  Philipp Kreil (pk910)
+/* ChanNode.c - NeonServ v5.6
+ * 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
@@ -20,8 +20,9 @@
 #include "BanNode.h"
 #include "modcmd.h"
 #include "ModeNode.h"
-#include "bot_NeonSpam.h"
 #include "IRCEvents.h"
+#include "tools.h"
+#include "log.h"
 
 static struct ChanNode **chanList;
 
@@ -40,6 +41,7 @@ void init_ChanNode() {
 }
 
 void free_ChanNode() {
+    SYNCHRONIZE(cache_sync);
     //kamikaze free all channels and chanusers
     int i;
     struct ChanNode *chan, *next;
@@ -55,6 +57,7 @@ void free_ChanNode() {
         }
     }
     free(chanList);
+    DESYNCHRONIZE(cache_sync);
 }
 
 int is_valid_chan(const char *name) {
@@ -105,8 +108,9 @@ struct ChanNode* getAllChans(struct ChanNode *last) {
             cindex++;
         if(cindex >= CHANNEL_LIST_SIZE) return NULL;
         return chanList[cindex];
-    } else
+    } else {
         return last->next;
+    }
 }
 
 struct ChanNode* getChanByName(const char *name) { //case insensitive
@@ -128,7 +132,7 @@ struct ChanNode* addChannel(const char *name) {
     struct ChanNode *chan = malloc(sizeof(*chan));
     if (!chan)
     {
-        perror("malloc() failed");
+        printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
         return NULL;
     }
     strcpy(chan->name, name);
@@ -136,15 +140,17 @@ struct ChanNode* addChannel(const char *name) {
     chan->bans = NULL;
     chan->spam_settings = NULL;
     chan->usercount = 0;
-    chan->chanbot = NULL;
+    chan->botcount = 0;
     chan->topic[0] = 0;
     chan->flags = 0;
     /* mode lists */
     chan->modes = createModeNode(chan);
     chan->trigger = NULL;
     
+    SYNCHRONIZE(cache_sync);
     chan->next = chanList[chanListIndex];
     chanList[chanListIndex] = chan;
+    DESYNCHRONIZE(cache_sync);
     return chan;
 }
 
@@ -186,6 +192,7 @@ int getChanBanCount() {
 void delChannel(struct ChanNode* chan, int freeChan) {
     int chanListIndex = get_chanlist_entry(chan->name[1]);
     if(chanListIndex == -1) return;
+    SYNCHRONIZE(cache_sync);
     struct ChanNode *cchan, *last_chan = NULL;
     for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
         if(cchan == chan) {
@@ -209,6 +216,7 @@ void delChannel(struct ChanNode* chan, int freeChan) {
         freeChanNode(chan);
     else
         chan->next = NULL;
+    DESYNCHRONIZE(cache_sync);
 }
 
 void freeChanNode(struct ChanNode* chan) {
@@ -224,20 +232,17 @@ void freeChanNode(struct ChanNode* chan) {
     freeModeNode(chan->modes);
     if(chan->bans)
         removeChannelBans(chan);
-    if(chan->spam_settings)
-        freeNeonSpamSettings(chan->spam_settings);
     free(chan);
 }
 
-void checkChannelVisibility(struct ChanNode* chan) {
+int checkChannelVisibility(struct ChanNode* chan) {
     struct ChanUser *chanuser, *next;
     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
-        if(chanuser->user->flags & USERFLAG_ISBOT) {
-            chan->chanbot = chanuser->user;
-            return;
-        }
+        if(chanuser->user->flags & USERFLAG_ISBOT)
+            return 1;
     }
     //free the channel...
+    SYNCHRONIZE(cache_sync);
     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
         next = getChannelUsers(chan, chanuser);
         //remove the channel from the user's channel-list
@@ -250,4 +255,6 @@ void checkChannelVisibility(struct ChanNode* chan) {
     }
     chan->user = NULL;
     delChannel(chan, 1);
+    DESYNCHRONIZE(cache_sync);
+    return 0;
 }