Another year is about to end... So we have to update these damn copyright information :P
[NeonServV5.git] / src / IRCParser.c
index 9b91d21552ed8f6dc3f2f463c0ded662ee7d374a..b525a7d95dd3d63652ab176304de80168d8943a2 100644 (file)
@@ -1,5 +1,5 @@
-/* IRCParser.c - NeonServ v5.2
- * Copyright (C) 2011  Philipp Kreil (pk910)
+/* IRCParser.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
@@ -26,6 +26,7 @@
 #include "DBHelper.h"
 #include "BanNode.h"
 #include "ModeNode.h"
+#include "tools.h"
 
 struct irc_cmd *irc_commands = NULL;
 static struct UserNode *registering_users = NULL;
@@ -115,7 +116,7 @@ static USERLIST_CALLBACK(got_channel_userlist) {
     event_join(chanuser);
 }
 
-static IRC_CMD(raw_001) {
+static IRC_CMD(raw_002) { //fixed: ZNC fakes a 001 raw even if we're not connected!
     struct UserNode *user = getUserByNick(argv[0]);
     if(!user)
         user = addUser(argv[0]);
@@ -170,8 +171,9 @@ static IRC_CMD(raw_join) {
     if(from == NULL || argc < 1) return 0;
     struct UserNode *user = getUserByMask(from);
     struct ChanNode *chan = getChanByName(argv[0]);
-    if(!chan && !(user->flags & USERFLAG_ISBOT)) return 0;
-    if(chan && chan->chanbot != client->user) return 1; //we ignore it - but it's not a parse error
+    if(!chan && (!user || !(user->flags & USERFLAG_ISBOT))) return 0;
+    if(chan && (((!user || !isBot(user)) && chan->chanbot != client->user) || ((user && isBot(user)) && client->user != user))) return 1; //we ignore it - but it's not a parse error
+    //let Bots always add themselves! (maybe they join invisible)
     if(user == NULL) {
         user = addUserMask(from);
     }
@@ -201,11 +203,13 @@ static IRC_CMD(raw_join) {
         //request member list
         chan->chanbot = user;
         struct ChanUser *chanuser = addChanUser(chan, user); //it must be a bot
-        get_userlist(chan, got_channel_userlist, chanuser);
+        get_userlist_with_invisible(chan, got_channel_userlist, chanuser);
         putsock(client, "MODE %s", chan->name);
         putsock(client, "MODE %s +b", chan->name);
-    } else if(!isUserOnChan(user, chan) && (chan->flags & CHANFLAG_RECEIVED_USERLIST)) {
+    } else if(!isUserOnChan(user, chan) && ((chan->flags & CHANFLAG_RECEIVED_USERLIST) || isBot(user))) {
         struct ChanUser *chanuser = addChanUser(chan, user);
+        if(isModeSet(chan->modes, 'D'))
+            chanuser->flags |= CHANUSERFLAG_INVISIBLE;
         if(!noEvent) {
             if(wasRegistering)
                 user->flags |= USERFLAG_WAS_REGISTRING;
@@ -271,7 +275,7 @@ static IRC_CMD(raw_part) {
     if(user == NULL) return 0;
     struct ChanNode *chan = getChanByName(argv[0]);
     if(chan == NULL) return 0;
-    if(chan->chanbot != client->user) return 1; //we ignore it - but it's not a parse error
+    if((!isBot(user) && chan->chanbot != client->user) || (isBot(user) && client->user != user)) return 1; //we ignore it - but it's not a parse error
     int keep_channel = 1;
     if(chan->chanbot == user && (chan->flags & CHANFLAG_REJOINING)) {
         struct ClientSocket **clients = chan->rejoin_array;
@@ -312,7 +316,6 @@ 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 keep_channel = 1;
     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
@@ -323,12 +326,13 @@ static IRC_CMD(raw_quit) {
         for(chanuser = getUserChannels(user, NULL); chanuser; chanuser = next) {
             next = getUserChannels(user, chanuser);
             if(chanuser->chan->chanbot == user)
-                keep_channel = checkChannelVisibility(chanuser->chan);
+                checkChannelVisibility(chanuser->chan);
         }
         //search the user representing the bot in the world of IRC
         struct ClientSocket *bot;
         for(bot = getBots(0, NULL); bot; bot = getBots(0, bot)) {
             if(bot->user == user) {
+                bot->flags &= ~SOCKET_FLAG_READY;
                 bot->user = NULL;
                 break;
             }
@@ -377,6 +381,7 @@ void bot_disconnect(struct ClientSocket *client) {
             }
         }
         client->user = NULL;
+        client->flags &= ~SOCKET_FLAG_READY;
     }
 }
 
@@ -386,7 +391,7 @@ static IRC_CMD(raw_kick) {
     struct UserNode *target = getUserByNick(argv[1]);
     struct ChanNode *chan = getChanByName(argv[0]);
     if(chan == NULL || target == NULL) return 0;
-    if(chan->chanbot != client->user) return 1; //we ignore it - but it's not a parse error
+    if(((!isBot(target) && chan->chanbot != client->user) || (isBot(target) && client->user != target))) return 1; //we ignore it - but it's not a parse error
     int keep_channel = 1;
     if(isUserOnChan(target, chan) && (chan->flags & CHANFLAG_RECEIVED_USERLIST)) {
         if(user == NULL) {
@@ -434,6 +439,16 @@ static IRC_CMD(raw_privmsg) {
         user = createTempUser(from);
         user->flags |= USERFLAG_ISTMPUSER;
     }
+    if(!stricmp(user->nick, "*status") && !match("Disconnected from IRC.*", argv[1])) {
+        //ZNC DISCONNECT
+        bot_disconnect(client);
+        return 1;
+    }
+    if(!stricmp(user->nick, "-sBNC") && !match("* disconnected from the server.", argv[1])) {
+        //sBNC DISCONNECT
+        bot_disconnect(client);
+        return 1;
+    }
     if(argv[0][0] == '#') { //Channel message
         struct ChanNode *chan = getChanByName(argv[0]);
         if(chan && chan->chanbot == client->user) {
@@ -611,7 +626,7 @@ static IRC_CMD(raw_332) {
 
 void init_parser() {
     //all the raws we receive...
-    register_irc_function("001", raw_001);
+    register_irc_function("002", raw_002);
     register_irc_function("251", raw_251);
     register_irc_function("254", raw_254);
     register_irc_function("324", raw_324);