X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=blobdiff_plain;f=src%2FIRCParser.c;h=ff244a05043c98384c978b0dca68e8bca1a01be2;hp=f9e79e0c127c720b34a67f96db5217334d0a2738;hb=4812835346f724fcea86e78f741cc6c72c86b676;hpb=f28a6cf114ec097583236a82aab0dbf52c4852c4 diff --git a/src/IRCParser.c b/src/IRCParser.c index f9e79e0..ff244a0 100644 --- a/src/IRCParser.c +++ b/src/IRCParser.c @@ -1,4 +1,4 @@ -/* IRCParser.c - NeonServ v5.5 +/* IRCParser.c - NeonServ v5.6 * Copyright (C) 2011-2012 Philipp Kreil (pk910) * * This program is free software: you can redistribute it and/or modify @@ -28,6 +28,8 @@ #include "ModeNode.h" #include "tools.h" #include "bots.h" +#include "timeq.h" +#include "ConfigParser.h" struct irc_cmd *irc_commands = NULL; //static struct UserNode *registering_users = NULL; @@ -38,6 +40,20 @@ int statistics_network_channels = 0; static void register_irc_function(char *command, irc_cmd_t *func); static void parse_raw(struct ClientSocket *client, char *from, char *cmd, char **argv, int argc); +struct OplessRejoinUserbot { + char *nick; + char *auth; +}; + +struct OplessRejoinBot { + unsigned char is_client; + union { + struct ClientSocket *client; + struct OplessRejoinUserbot *userbot; + } bot; + struct OplessRejoinBot *next; +}; + void parse_line(struct ClientSocket *client, char *line) { int argc = 0; char *argv[MAXNUMPARAMS]; @@ -141,6 +157,153 @@ static IRC_CMD(raw_002) { //fixed: ZNC fakes a 001 raw even if we're not connect return 1; } +static int check_userbot_rejoin(struct UserNode *user) { + if(!(user->flags & USERFLAG_ISAUTHED)) return 0; + char tmp[MAXLEN]; + sprintf(tmp, "General.UserBots.%s.enabled", user->auth); + if(!get_int_field(tmp)) + return 0; + sprintf(tmp, "General.UserBots.%s.nicks", user->auth); + char *nicks = get_string_field(tmp); + if(nicks) { + char *cnick = nicks; + int matching_nick = 0; + do { + nicks = strchr(cnick, ','); + if(nicks) + *nicks = '\0'; + if(!match(cnick, user->nick)) + matching_nick = 1; + if(nicks) { + *nicks = ','; + nicks++; + } + } while((cnick = nicks) && !matching_nick); + if(!matching_nick) + return 0; + } + sprintf(tmp, "General.UserBots.%s.opless_part", user->auth); + if(!get_string_field(tmp)) + return 0; + return 1; +} + +static void free_rejoin_clients(struct ChanNode *chan, int rejoin) { + struct OplessRejoinBot *rejoin_bot, *next_rejoin_bot; + char tmp[MAXLEN]; + int sourceid; + struct ClientSocket *bot; + for(rejoin_bot = chan->rejoin_bots; rejoin_bot; rejoin_bot = next_rejoin_bot) { + next_rejoin_bot = rejoin_bot->next; + if(rejoin) { + if(rejoin_bot->is_client) { + putsock(rejoin_bot->bot.client, "JOIN %s", chan->name); + } else { + sprintf(tmp, "General.UserBots.%s.sourcebot", rejoin_bot->bot.userbot->auth); + if(get_string_field(tmp)) { + sourceid = resolve_botalias(get_string_field(tmp)); + if(sourceid == -1) + sourceid = 0; + } else + sourceid = 0; + bot = getChannelBot(NULL, sourceid); + if(!bot) + bot = getChannelBot(NULL, 0); + sprintf(tmp, "General.UserBots.%s.opless_join", rejoin_bot->bot.userbot->auth); + if(get_string_field(tmp)) + putsock(bot, get_string_field(tmp), rejoin_bot->bot.userbot->nick, chan->name); + } + } + if(!rejoin_bot->is_client) { + free(rejoin_bot->bot.userbot->nick); + free(rejoin_bot->bot.userbot->auth); + free(rejoin_bot->bot.userbot); + } + free(rejoin_bot); + } + if(chan->rejoin_timeout) + timeq_del(chan->rejoin_timeout); +} + +static TIMEQ_CALLBACK(full_rejoin_timeout) { + struct ChanNode *chan = data; + chan->rejoin_timeout = NULL; + free_rejoin_clients(chan, 1); + chan->flags &= ~CHANFLAG_REJOINING; +} + +static void check_full_rejoin(struct ChanNode *chan) { + struct ChanUser *chanuser; + char do_rejoin = 1; + int botcount = 0; + int userbots = 0; + for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) { + if((chanuser->flags & CHANUSERFLAG_OPPED) || !(isBot(chanuser->user) || check_userbot_rejoin(chanuser->user))) { + do_rejoin = 0; + break; + } + botcount++; + if(!isBot(chanuser->user)) + userbots++; + } + if(do_rejoin) { + struct OplessRejoinBot *rejoin_bot; + struct ClientSocket *bot, *chanbot = NULL; + chan->rejoin_bots = NULL; + for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) { + if(!isUserOnChan(bot->user, chan)) + continue; + if(!chanbot && ((bot->flags & SOCKET_FLAG_PREFERRED) || !getBots(SOCKET_FLAG_READY, bot))) + chanbot = bot; + else { + rejoin_bot = malloc(sizeof(*rejoin_bot)); + rejoin_bot->is_client = 1; + rejoin_bot->bot.client = bot; + rejoin_bot->next = chan->rejoin_bots; + chan->rejoin_bots = rejoin_bot; + putsock(bot, "PART %s :rejoining", chan->name); + } + } + if(userbots) { + char tmp[MAXLEN]; + int sourceid; + for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) { + if(check_userbot_rejoin(chanuser->user)) { + rejoin_bot = malloc(sizeof(*rejoin_bot)); + rejoin_bot->is_client = 0; + rejoin_bot->bot.userbot = malloc(sizeof(struct OplessRejoinUserbot)); + rejoin_bot->bot.userbot->nick = strdup(chanuser->user->nick); + rejoin_bot->bot.userbot->auth = strdup(chanuser->user->auth); + rejoin_bot->next = chan->rejoin_bots; + chan->rejoin_bots = rejoin_bot; + sprintf(tmp, "General.UserBots.%s.sourcebot", chanuser->user->auth); + if(get_string_field(tmp)) { + sourceid = resolve_botalias(get_string_field(tmp)); + if(sourceid == -1) + sourceid = 0; + } else + sourceid = 0; + bot = getChannelBot(NULL, sourceid); + if(!bot) + bot = getChannelBot(NULL, 0); + sprintf(tmp, "General.UserBots.%s.opless_part", chanuser->user->auth); + putsock(bot, get_string_field(tmp), chanuser->user->nick, chan->name); + } + } + } + + if(botcount == 1) { + //we're alone + free(chan->rejoin_bots); + putsock(chanbot, "PART %s :magic hop", chan->name); + putsock(chanbot, "JOIN %s", chan->name); + } else { + chan->flags |= CHANFLAG_REJOINING; + chan->rejoin_timeout = timeq_add(10, 0, full_rejoin_timeout, chan); + } + } +} + static IRC_CMD(raw_join) { if(from == NULL || argc < 1) return 0; SYNCHRONIZE(cache_sync); @@ -159,6 +322,7 @@ static IRC_CMD(raw_join) { chan = addChannel(argv[0]); chanuser = addChanUser(chan, user); chanuser->visCount = 1; + chan->botcount = 1; get_userlist_with_invisible(chan, 0, got_channel_userlist, chanuser); putsock(client, "MODE %s", chan->name); putsock(client, "MODE %s +b", chan->name); @@ -170,14 +334,21 @@ static IRC_CMD(raw_join) { event_registered(user, from); user->flags &= ~USERFLAG_WAS_REGISTERING; + if(user->last_who > REWHO_TIMEOUT) + user->last_who -= REWHO_TIMEOUT; + + event_join(chanuser); } else if(!(chan->flags & CHANFLAG_RECEIVED_USERLIST)) { - if(!isBot(user)) { + if(client->user != user) { //bots are allowed to add themselves DESYNCHRONIZE(cache_sync); return 1; //ignore join } - chanuser = addChanUser(chan, user); - chanuser->visCount = 1; + if(!(chanuser = getChanUser(user, chan))) { + chanuser = addChanUser(chan, user); + } + chanuser->visCount++; + chan->botcount++; if(isModeSet(chan->modes, 'D')) //if the bot joins a channel it could also be invisible chanuser->flags |= CHANUSERFLAG_INVISIBLE; @@ -187,22 +358,18 @@ static IRC_CMD(raw_join) { //join user to an existing channel chanuser = addChanUser(chan, user); chanuser->visCount = 1; - if(isBot(user)) { + if(isBot(user) && client->user == user) { if(isModeSet(chan->modes, 'D')) //if the bot joins a channel it could also be invisible chanuser->flags |= CHANUSERFLAG_INVISIBLE; increase_viscount_butone(chan, chanuser); + chan->botcount++; } event_join(chanuser); if(!(user->flags & USERFLAG_ISBOT) && (chan->flags & CHANFLAG_REJOINING)) { //ABORT AUTOMATIC REJOIN (security break) - struct ClientSocket **clients = chan->rejoin_array; - while(*clients) { - putsock(*clients, "JOIN %s", chan->name); - clients++; - } - free(chan->rejoin_array); + free_rejoin_clients(chan, 1); chan->flags &= ~CHANFLAG_REJOINING; } } else { @@ -210,8 +377,15 @@ static IRC_CMD(raw_join) { chanuser = getChanUser(user, chan); chanuser->visCount++; - if(isBot(user) && !(chanuser->flags & CHANUSERFLAG_INVISIBLE)) + if(isBot(user) && client->user == user) { increase_viscount_butone(chan, chanuser); + chan->botcount++; + } + + if(chanuser->visCount > chan->botcount) { + chanuser->visCount = chan->botcount; + //TODO: Trigger WARNING + } //if multiple bots see the user, it can't be invisible chanuser->flags &= ~CHANUSERFLAG_INVISIBLE; @@ -220,44 +394,6 @@ static IRC_CMD(raw_join) { return 1; } -static void check_full_rejoin(struct ChanNode *chan) { - struct ChanUser *chanuser; - char do_rejoin = 1; - int botcount = 0; - for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) { - if((chanuser->flags & CHANUSERFLAG_OPPED) || !(chanuser->user->flags & USERFLAG_ISBOT)) { - do_rejoin = 0; - break; - } - if((chanuser->user->flags & USERFLAG_ISBOT)) - botcount++; - } - if(do_rejoin) { - struct ClientSocket **clients = calloc(botcount, sizeof(*clients)); - struct ClientSocket *bot, *chanbot = NULL; - int i = 0; - for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) { - if(!isUserOnChan(bot->user, chan)) - continue; - if(!chanbot && ((bot->flags & SOCKET_FLAG_PREFERRED) || !getBots(SOCKET_FLAG_READY, bot))) - chanbot = bot; - else { - clients[i++] = bot; - putsock(bot, "PART %s :rejoining", chan->name); - } - } - if(botcount == 1) { - //we're alone - free(clients); - putsock(chanbot, "PART %s :magic hop", chan->name); - putsock(chanbot, "JOIN %s", chan->name); - } else { - chan->flags |= CHANFLAG_REJOINING; - chan->rejoin_array = clients; - } - } -} - static IRC_CMD(raw_part) { if(from == NULL || argc < 1) return 0; SYNCHRONIZE(cache_sync); @@ -268,25 +404,35 @@ static IRC_CMD(raw_part) { DESYNCHRONIZE(cache_sync); return 0; } - if(isBot(user) && user == client->user) + if(isBot(user) && user == client->user) { decrease_viscount_butone(chan, chanuser); + chan->botcount--; + } + if(chanuser->flags & CHANUSERFLAG_PARTING) + chanuser->old_visCount--; chanuser->visCount--; if(chanuser->visCount == 0) { delChanUser(chanuser, 0); //not free, yet! event_part(chanuser, 0, (argc > 1 ? argv[1] : NULL)); freeChanUser(chanuser); + } else if(!(chanuser->flags & CHANUSERFLAG_PARTING)) { + chanuser->flags |= CHANUSERFLAG_PARTING; + chanuser->old_visCount = chanuser->visCount; + } else if(chanuser->old_visCount == 0) { + int visCount = chanuser->visCount; + delChanUser(chanuser, 0); //not free, yet! + event_part(chanuser, 0, (argc > 1 ? argv[1] : NULL)); + freeChanUser(chanuser); + chanuser = addChanUser(chan, user); + chanuser->visCount = visCount; + event_join(chanuser); } //check if channel is still present int keep_channel = 1; if(chan->usercount == 0) { if((chan->flags & CHANFLAG_REJOINING)) { - struct ClientSocket **clients = chan->rejoin_array; - while(*clients) { - putsock(*clients, "JOIN %s", chan->name); - clients++; - } - free(chan->rejoin_array); + free_rejoin_clients(chan, 1); chan->flags &= ~CHANFLAG_REJOINING; } delChannel(chan, 1); @@ -326,8 +472,10 @@ static IRC_CMD(raw_kick) { DESYNCHRONIZE(cache_sync); return 0; } - if(isBot(target) && target == client->user) + if(isBot(target) && target == client->user) { decrease_viscount_butone(chan, chanuser); + chan->botcount--; + } chanuser->visCount--; if(chanuser->visCount == 0) { delChanUser(chanuser, 0); //not free, yet! @@ -339,12 +487,7 @@ static IRC_CMD(raw_kick) { int keep_channel = 1; if(chan->usercount == 0) { if((chan->flags & CHANFLAG_REJOINING)) { - struct ClientSocket **clients = chan->rejoin_array; - while(*clients) { - putsock(*clients, "JOIN %s", chan->name); - clients++; - } - free(chan->rejoin_array); + free_rejoin_clients(chan, 1); chan->flags &= ~CHANFLAG_REJOINING; } delChannel(chan, 1); @@ -395,13 +538,16 @@ static IRC_CMD(raw_quit) { struct ChanUser *chanuser, *next_chanuser; for(chanuser = getUserChannels(user, NULL); chanuser; chanuser = next_chanuser) { next_chanuser = getUserChannels(user, chanuser); - chanuser->visCount--; - if(chanuser->visCount <= 0) { - delChanUser(chanuser, 0); //not free, yet! - event_part(chanuser, 1, argv[0]); - if((chanuser->chan->flags & CHANFLAG_RECEIVED_USERLIST) && !(chanuser->chan->flags & CHANFLAG_REJOINING)) - check_full_rejoin(chanuser->chan); - freeChanUser(chanuser); + //decrease visCount counter only if client is in the channel + if(isUserOnChan(client->user, chanuser->chan)) { + chanuser->visCount--; + if(chanuser->visCount <= 0 && !(user->flags & USERFLAG_WAS_REGISTERING)) { + delChanUser(chanuser, 0); //not free, yet! + event_part(chanuser, 1, argv[0]); + if((chanuser->chan->flags & CHANFLAG_RECEIVED_USERLIST) && !(chanuser->chan->flags & CHANFLAG_REJOINING)) + check_full_rejoin(chanuser->chan); + freeChanUser(chanuser); + } } } @@ -433,6 +579,7 @@ void bot_disconnect(struct ClientSocket *client) { for(chanuser = getUserChannels(user, NULL); chanuser; chanuser = next_chanuser) { next_chanuser = getUserChannels(user, chanuser); decrease_viscount_butone(chanuser->chan, chanuser); + chanuser->chan->botcount--; chanuser->visCount--; if(chanuser->visCount <= 0) { delChanUser(chanuser, 0); //not free, yet!