tried to make NeonServ fully ZNC compatible (shared cache problems)
[NeonServV5.git] / src / IRCParser.c
index c8d61d2917a4fcb5a5db601f74d78860bc40f337..b757f5dc4d28ca18c9af2f356120ded843736a97 100644 (file)
@@ -29,6 +29,9 @@
 
 struct irc_cmd *irc_commands = NULL;
 static struct UserNode *registering_users = NULL;
+int statistics_privmsg = 0;
+int statistics_network_users = 0;
+int statistics_network_channels = 0;
 
 static void parse_line(struct ClientSocket *client, char *line);
 static void register_irc_function(char *command, irc_cmd_t *func);
@@ -113,6 +116,11 @@ static USERLIST_CALLBACK(got_channel_userlist) {
 }
 
 static IRC_CMD(raw_001) {
+    struct UserNode *user = getUserByNick(argv[0]);
+    if(!user)
+        user = addUser(argv[0]);
+    client->user = user;
+    client->user->flags |= USERFLAG_ISBOT;
     client->flags |= SOCKET_FLAG_READY;
     event_bot_ready(client);
     return 1;
@@ -144,6 +152,20 @@ static int is_firstBotSeeUser(struct ClientSocket *client, struct UserNode *user
         return 0;
 }
 
+static int is_onlyBotSeeUser(struct ClientSocket *client, struct UserNode *user) {
+    struct ClientSocket *bot;
+    struct ChanUser *chanuser;
+    for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+        if(bot == client) continue;
+        for(chanuser = getUserChannels(bot->user, NULL); chanuser; chanuser = getUserChannels(bot->user, chanuser)) {
+            if(isUserOnChan(user, chanuser->chan)) {
+                return 0;
+            }
+        }
+    }
+    return 1;
+}
+
 static IRC_CMD(raw_join) {
     if(from == NULL || argc < 1) return 0;
     struct UserNode *user = getUserByMask(from);
@@ -224,6 +246,8 @@ static IRC_CMD(raw_quit) {
     struct UserNode *user = getUserByMask(from);
     if(user == NULL) return 0;
     if(!is_firstBotSeeUser(client, user)) return 1; //we ignore it - but it's not a parse error
+    int registering = !stricmp(argv[0], "Registered");
+    if((registering && (user->flags & USERFLAG_ISBOT))) return 1; //bot is registering - just ignore it
     delUser(user, 0); //a little bit crazy, but we want to delete the user on the channel's userlists - but not the users channel list
     event_quit(user, argv[0]);
     if(user->flags & USERFLAG_ISBOT) {
@@ -243,7 +267,7 @@ static IRC_CMD(raw_quit) {
             }
         }
     }
-    if(!stricmp(argv[0], "Registered") && !(user->flags & USERFLAG_ISBOT)) {
+    if(registering && !(user->flags & USERFLAG_ISBOT)) {
         user->next = registering_users;
         user->created = time(0);
         registering_users = user;
@@ -256,16 +280,27 @@ void bot_disconnect(struct ClientSocket *client) {
     struct UserNode *user = client->user;
     struct ChanUser *chanuser, *next;
     if(user) {
-        delUser(user, 0);
-        event_quit(user, "disconnected");
-        for(chanuser = getUserChannels(user, NULL); chanuser; chanuser = next) {
-            next = getUserChannels(user, chanuser);
-            if(chanuser->chan->chanbot == user)
-                checkChannelVisibility(chanuser->chan);
-            freeChanUser(chanuser);
+        if(is_onlyBotSeeUser(client, user)) {
+            //ok  the bot-user is not seen by any other bots so we can simply free it.
+            delUser(user, 0);
+            event_quit(user, "disconnected");
+            for(chanuser = getUserChannels(user, NULL); chanuser; chanuser = next) {
+                next = getUserChannels(user, chanuser);
+                if(chanuser->chan->chanbot == user)
+                    checkChannelVisibility(chanuser->chan);
+                freeChanUser(chanuser);
+            }
+            user->channel = NULL;
+            delUser(user, 1); //now we fully free the user
+        } else {
+            //we need to transform the bot-user back to a normal user (BNC FIX)
+            user->flags &= ~USERFLAG_ISBOT;
+            for(chanuser = getUserChannels(user, NULL); chanuser; chanuser = next) {
+                next = getUserChannels(user, chanuser);
+                if(chanuser->chan->chanbot == user)
+                    checkChannelVisibility(chanuser->chan);
+            }
         }
-        user->channel = NULL;
-        delUser(user, 1); //now we fully free the user
         client->user = NULL;
     }
 }
@@ -285,11 +320,11 @@ static IRC_CMD(raw_kick) {
         struct ChanUser *chanuser = getChanUser(target, chan);
         delChanUser(chanuser, 0); //we need to free the chanuser manually!
         event_kick(user, chanuser, argv[1]);
-        freeChanUser(chanuser);
         if(chanuser->chan->chanbot == user) {
             //check if theres another bot in the channel - otherwise free it
             checkChannelVisibility(chan);
         }
+        freeChanUser(chanuser);
     }
     if(target->channel == NULL && !(target->flags & USERFLAG_ISBOT)) {
         //remove the user
@@ -323,6 +358,8 @@ static IRC_CMD(raw_privmsg) {
     if(argv[0][0] == '#') { //Channel message
         struct ChanNode *chan = getChanByName(argv[0]);
         if(chan && chan->chanbot == client->user) {
+            if(statistics_enabled)
+                statistics_privmsg++;
             if(argv[1][0] == '\001') {
                 char *cmd = &argv[1][1];
                 char *text = strstr(cmd, " ");
@@ -455,10 +492,51 @@ static IRC_CMD(raw_367) {
     return 1;
 }
 
+static IRC_CMD(raw_251) {
+    if(argc < 2) return 0;
+    char *total_user_str = argv[1];
+    char total_visible[20], total_invisible[20];
+    int i, total_visible_pos = 0, total_invisible_pos = 0;
+    while(*total_user_str) {
+        if(*total_user_str == ' ') {
+            i++;
+        } else if(i == 2) {
+            if(total_visible_pos < 20)
+                total_visible[total_visible_pos++] = *total_user_str;
+        } else if(i == 5) {
+            if(total_invisible_pos < 20)
+                total_invisible[total_invisible_pos++] = *total_user_str;
+        }
+        total_user_str++;
+    }
+    total_visible[total_visible_pos] = '\0';
+    total_invisible[total_invisible_pos] = '\0';
+    statistics_network_users = atoi(total_visible) + atoi(total_invisible);
+    return 1;
+}
+
+static IRC_CMD(raw_254) {
+    if(argc < 3) return 0;
+    statistics_network_channels = atoi(argv[1]);
+    statistics_update();
+    return 1;
+}
+
+static IRC_CMD(raw_332) {
+    //Skynet #neonserv :topic
+    struct ChanNode *chan = getChanByName(argv[1]);
+    if(!chan) return 0;
+    strcpy(chan->topic, argv[2]);
+    return 1;
+}
+
 void init_parser() {
     //all the raws we receive...
     register_irc_function("001", raw_001);
+    register_irc_function("251", raw_251);
+    register_irc_function("254", raw_254);
     register_irc_function("324", raw_324);
+    register_irc_function("332", raw_332);
     register_irc_function("367", raw_367);
     register_irc_function("INVITE", raw_invite);
     register_irc_function("NOTICE", raw_notice);