Пример #1
0
inline void CIRC :: ExtractPackets( )
{
	string Token, PreviousToken, Recv = *( m_Socket->GetBytes( ) );
        uint32_t Time = GetTime( );
        unsigned int i;

        /* loop through whole recv buffer */

        for( i = 0; i < Recv.size( ); ++i )
        {
            // add chars to token

            if( Recv[i] != ' ' && Recv[i] != CR && Recv[i] != LF )
            {
                Token += Recv[i];
            }
            else if( Recv[i] == ' ' || Recv[i] == LF )
            {
                // end of token, examine

                if( Token == "PRIVMSG" )
                {
                    // parse the PreviousToken as it holds the user info and then the Token for the message itself

                    string Nickname, Hostname, Message, Command, Payload;
                    bool IsCommand = true;

                    unsigned int j = 1;

                    // get nickname

                    for( ; PreviousToken[j] != '!'; ++j )
                        Nickname += PreviousToken[j];

                    // skip username

                    for( j += 2; PreviousToken[j] != '@'; ++j );

                    // get hostname

                    for( ++j; j < PreviousToken.size( ); ++j )
                        Hostname += PreviousToken[j];

                    // skip channel

                    for( i += 3; Recv[i] != ':'; ++i );

                    // process message

                    for( ++i; Recv[i] != CR; ++i )
                    {
                        Message += Recv[i];

                        if( Recv[i] == ' ' && IsCommand )
                        {
                            IsCommand = false;
                            continue;
                        }

                        if( Message.size( ) != 1 )
                        {
                            if( IsCommand )
                                Command += tolower( Recv[i] );
                            else
                                Payload += Recv[i];
                        }
                    }

                    // move position after the \n

                    i += 2;

                    if( Message.empty( ) )
                    {
                        PreviousToken = Token;
                        Token.clear( );
                        continue;
                    }

                    if( Message[0] != SOH )
                    {
                            for( vector<CBNET *> :: iterator i = m_Aura->m_BNETs.begin( ); i != m_Aura->m_BNETs.end( ); ++i )
                            {
                                    if( Message[0] == (*i)->GetCommandTrigger( ) )
                                    {
                                        CIncomingChatEvent event = CIncomingChatEvent( CBNETProtocol :: EID_IRC, Nickname, Message );
					(*i)->ProcessChatEvent( &event );
					break;
                                    }
                            }

                            if( Message[0] == m_CommandTrigger[0] )
                            {
                                bool Root = Hostname.substr( 0, 6 ) == "Aurani" || Hostname.substr( 0, 8 ) == "h4x0rz88";

                                if( Command == "nick" && Root )
				{
                                        SendIRC( "NICK :" + Payload );
                                        m_Nickname = Payload;
                                        m_OriginalNick= false;
                                }
                                else if( Command == "dcclist" )
                                {
                                        string on, off;

                                        for( vector<CDCC *> :: iterator i = m_DCC.begin( ); i != m_DCC.end( ); ++i )
                                        {
                                                if( (*i)->m_Socket->GetConnected( ) )
                                                        on += (*i)->m_Nickname + "[" + UTIL_ToString( (*i)->m_Port ) +"] ";
                                                else
                                                        off += (*i)->m_Nickname + "[" + UTIL_ToString( (*i)->m_Port ) +"] ";
                                        }

                                        SendMessageIRC( "ON: " + on, string( ) );
                                        SendMessageIRC( "OFF: " + off, string( ) );
                                }
                                else if( Command == "bnetoff" )
                                {
                                        if( Payload.empty( ) )
                                        {
                                                for( vector<CBNET *> :: iterator i = m_Aura->m_BNETs.begin( ); i != m_Aura->m_BNETs.end( ); ++i )
                                                {
                                                        (*i)->Deactivate( );
                                                        SendMessageIRC( "[BNET: " + (*i)->GetServerAlias( ) + "] deactivated.", string( ) );
                                                }
                                        }
                                        else
                                        {
                                                for( vector<CBNET *> :: iterator i = m_Aura->m_BNETs.begin( ); i != m_Aura->m_BNETs.end( ); ++i )
                                                {
                                                        if( (*i)->GetServerAlias( ) == Payload )
                                                        {
                                                                (*i)->Deactivate( );
                                                                SendMessageIRC( "[BNET: " + (*i)->GetServerAlias( ) + "] deactivated.", string( ) );
                                                                break;
                                                        }
                                                }
                                        }
                                }
                                else if( Command == "bneton" )
                                {
                                        if( Payload.empty( ) )
                                        {
                                                for( vector<CBNET *> :: iterator i = m_Aura->m_BNETs.begin( ); i != m_Aura->m_BNETs.end( ); ++i )
                                                {
                                                        (*i)->Activate( );
                                                        SendMessageIRC( "[BNET: " + (*i)->GetServerAlias( ) + "] activated.", string( ) );
                                                }
                                        }
                                        else
                                        {
                                                for( vector<CBNET *> :: iterator i = m_Aura->m_BNETs.begin( ); i != m_Aura->m_BNETs.end( ); ++i )
                                                {
                                                        if( (*i)->GetServerAlias( ) == Payload )
                                                        {
                                                                (*i)->Activate( );
                                                                SendMessageIRC( "[BNET: " + (*i)->GetServerAlias( ) + "] activated.", string( ) );
                                                                break;
                                                        }
                                                }
                                        }
                                 }
                          }
                    }
                    else if( Payload.size( ) > 12 && Payload.substr( 0, 4 ) == "CHAT" )
                    {
                            // CHAT chat 3162588924 1025

                            string strIP, strPort;
                            bool IsPort = false;

                            for( unsigned int j = 10; j < ( Payload.size( ) - 1 ); ++j )
                            {
                                if( !IsPort && Payload[j] == ' ' )
                                {
                                    IsPort = true;
                                    continue;
                                }

                                if( !IsPort )
                                    strIP += Payload[j];
                                else
                                    strPort += Payload[j];
                            }

                            unsigned int Port = UTIL_ToUInt16( strPort );

                            if( Port < 1024 || 1026 < Port )
                                Port = 1024;

                            bool Local = false;

                            for( vector<string> :: iterator i = m_Locals.begin( ); i != m_Locals.end( ); ++i )
                            {
                                    if( Nickname == (*i) )
                                    {
                                            strIP = "127.0.0.1";
                                            Local = true;
                                            break;
                                    }
                            }

                            if( !Local )
                            {
                                    unsigned long IP = UTIL_ToUInt32( strIP ), divider = 16777216UL;
                                    strIP = "";

                                    for( int i = 0; i <= 3; ++i )
                                    {
                                            stringstream ss;
                                            ss << (unsigned long) IP / divider;

                                            IP %= divider;
                                            divider /= 256;
                                            strIP += ss.str( );

                                            if( i != 3 )
                                                    strIP += '.';
                                    }
                            }

                            bool Existing = false;

                            for( vector<CDCC *> :: iterator i = m_DCC.begin( ); i != m_DCC.end( ); ++i )
                            {
                                    if( (*i)->m_Nickname == Nickname )
                                    {
                                            (*i)->Connect( strIP, Port );
                                            Existing = true;
                                            break;
                                    }
                            }

                            if( !Existing )
                                m_DCC.push_back( new CDCC( this, strIP, Port, Nickname ) );
                    }

                    // remember last packet time

                    m_LastPacketTime = Time;
                }
                else if( Token == "391" )
                {
                    // move position after the \n (next packet)

                    for( ++i; Recv[i] != CR; ++i );

                    i += 2;

                    // remember last packet time

                    m_LastPacketTime = Time;
                }
                 else if( Token == "PING" )
                {
                    string Packet;

                    // PING :blabla

                    // skip until :

                    for( ++i; Recv[i] != ':'; ++i );

                    for( ++i; Recv[i] != CR; ++i )
                        Packet += Recv[i];

                    SendIRC( "PONG :" + Packet );

                    // move position after the \n

                    i += 2;

                    // remember last packet time

                    m_LastPacketTime = Time;
                }
                else if( Token == "NOTICE" )
                {
                    // move position after the \n

                    for( ++i; Recv[i] != CR; ++i );
                    
                    i += 2;

                    // remember last packet time

                    m_LastPacketTime = Time;
                }
                else if( Token == "221" )
                {
                    // Q auth if the server is QuakeNet

                    if( m_Server.find( "quakenet.org" ) != string :: npos && !m_Password.empty( ) )
                    {
                            SendMessageIRC( "AUTH " + m_Username + " " + m_Password, "*****@*****.**" );
                            SendIRC( "MODE " + m_Nickname + " +x" );
                    }

                    // join channels

                    for( vector<string> :: iterator j = m_Channels.begin( ); j != m_Channels.end( ); ++j )
                    {
                            SendIRC( "JOIN " + (*j) );
                    }

                    // move position after the \n

                    for( ++i; Recv[i] != CR; ++i );

                    i += 2;

                    // remember last packet time

                    m_LastPacketTime = Time;
                }
                else if( Token == "433" )
                {
                    // nick taken, append _

                    m_OriginalNick = false;
                    m_Nickname += '_';

                    SendIRC( "NICK " + m_Nickname );

                    // move position after the \n (next packet)

                    for( ++i; Recv[i] != CR; ++i );

                    i += 2;

                    // remember last packet time

                    m_LastPacketTime = Time;
                }
                else if( Token == "353" )
                {
                    // move position after the \n (next packet)

                    for( ++i; Recv[i] != CR; ++i );

                    i += 2;

                    // remember last packet time

                    m_LastPacketTime = Time;
                }
                else if( Token == "KICK" )
                {
                    string Channel, Victim;
                    bool Space = false;

                    // get channel

                    for( ++i; Recv[i] != ' '; ++i )
                            Channel += Recv[i];

                    // get the victim

                    for( ++i ; i < Recv.size( ); ++i )
                    {
                        if( Recv[i] == ' ' )
                            Space = true;
                        else if( Recv[i] == CR )
                            break;
                        else if( Space && Recv[i] != ':' )
                            Victim += Recv[i];
                    }

                    // we're the victim here! rejoin

                    if( Victim == m_Nickname )
                    {
                        SendIRC( "JOIN " + Channel );
                    }

                    // move position after the \n

                    i += 2;

                    // remember last packet time

                    m_LastPacketTime = Time;
                }

                // empty the token

                PreviousToken = Token;
                Token.clear( );
            }
        }

        m_Socket->ClearRecvBuffer( );
}
Пример #2
0
int main( )
{
	// seed the RNG
	
	srand( (unsigned int) time( NULL ) );
	
	// check if the log folder exists
	
	if( !UTIL_FileExists( "log" ) )
	{
#ifdef WIN32
		CreateDirectoryA( "log", NULL );
#else
		system( "mkdir log" );
		system( "chmod 777 log" );
#endif
	}
	
	// check if the cfg folder exists
	
	if( !UTIL_FileExists( "cfg" ) )
	{
#ifdef WIN32
		CreateDirectoryA( "cfg", NULL );
#else
		system( "mkdir cfg" );
		system( "chmod 777 cfg" );
#endif
	}
	
	// read config file

	CConfig CFG;
	CFG.Read( CFGFile );
	gLog = CFG.GetInt( "bot_log", 1 ) == 0 ? false : true;
	
	// catch SIGABRT and SIGINT

	signal( SIGABRT, SignalCatcher );
	signal( SIGINT, SignalCatcher );

#ifndef WIN32
	// disable SIGPIPE since some systems like OS X don't define MSG_NOSIGNAL

	signal( SIGPIPE, SIG_IGN );
#endif

	// initialize curses

	gCurses = true;
	initscr( );
#ifdef WIN32
	resize_term( 28, 97 );
#endif
	clear( );
	noecho( );
	cbreak( );
	gMainWindow = newwin( LINES - 3, COLS - 17, 0, 0 );
	gBottomBorder = newwin( 1, COLS, LINES - 3, 0 );
	gRightBorder = newwin( LINES - 3, 1, 0, COLS - 17 );
	gInputWindow = newwin( 2, COLS, LINES - 2, 0 );
	gChannelWindow = newwin( LINES - 3, 16, 0, COLS - 16 );
	mvwhline( gBottomBorder, 0, 0, 0, COLS );
	mvwvline( gRightBorder, 0, 0, 0, LINES );
	wrefresh( gBottomBorder );
	wrefresh( gRightBorder );
	scrollok( gMainWindow, TRUE );
	keypad( gInputWindow, TRUE );
	scrollok( gInputWindow, TRUE );
	CONSOLE_Draw( );
	nodelay( gInputWindow, TRUE );	
	
	// print something for logging purposes

	CONSOLE_Print( "[CCBOT] starting up" );

#ifdef WIN32

	// increase process priority

	CONSOLE_Print( "[CCBOT] setting process priority to \"high\"" );
	SetPriorityClass( GetCurrentProcess( ), HIGH_PRIORITY_CLASS );

	// initialize winsock

	CONSOLE_Print( "[CCBOT] starting winsock" );
	WSADATA wsadata;

	if( WSAStartup( MAKEWORD( 2, 2 ), &wsadata ) != 0 )
	{
		CONSOLE_Print( "[CCBOT] error starting winsock" );
		return 1;
	}
#endif

	// initialize ccbot

	gCCBot = new CCCBot( &CFG );

	while( true )
	{
		// block for 50ms on all sockets - if you intend to perform any timed actions more frequently you should change this
		// that said it's likely we'll loop more often than this due to there being data waiting on one of the sockets but there aren't any guarantees

		if( gCCBot->Update( ) )
			break;

		bool Quit = false;
		int c = wgetch( gInputWindow );
		
		while( c != ERR )
		{ 
			if( c == 8 || c == 127 || c == KEY_BACKSPACE || c == KEY_DC )
			{
				// Backspace, Delete

				if( !gInputBuffer.empty( ) )
					gInputBuffer.erase( gInputBuffer.size( ) - 1, 1 );
			}
			else if( c == 9 )
			{
				// Tab
			}
#ifdef WIN32
			else if( c == 10 || c == 13 || c == PADENTER )
#else
			else if( c == 10 || c == 13 )
#endif
			{
				// CR, LF
				// process input buffer now

				string Command = gInputBuffer;

				if( Command[0] == gCCBot->m_BNETs[0]->GetCommandTrigger( ) )
				{
					CONSOLE_Print( "[CONSOLE] " + Command );
					
					CIncomingChatEvent event = CIncomingChatEvent( CBNETProtocol :: CONSOLE_INPUT, 0, 0, gCCBot->m_BNETs[0]->GetRootAdmin( ), Command );
					gCCBot->m_BNETs[0]->ProcessChatEvent( &event );
				}
				else
					gCCBot->m_BNETs[0]->QueueChatCommand( Command, false );				

				gInputBuffer.clear( );
			}
#ifdef WIN32
			else if( c == 22 )
			{
				// Paste

				char *clipboard = NULL;
				long length = 0;

				if( PDC_getclipboard( &clipboard, &length ) == PDC_CLIP_SUCCESS )
				{
					gInputBuffer += string( clipboard, length );
					PDC_freeclipboard( clipboard );
				}
			}
#endif
			else if( c == 27 )
			{
				// Escape button

				gInputBuffer.clear( );
			}
			else if( c >= 32 && c <= 255 )
			{
				// Printable characters

				gInputBuffer.push_back( c );
			}
#ifdef WIN32
			else if( c == PADSLASH )
				gInputBuffer.push_back( '/' );
			else if( c == PADSTAR )
				gInputBuffer.push_back( '*' );
			else if( c == PADMINUS )
				gInputBuffer.push_back( '-' );
			else if( c == PADPLUS )
				gInputBuffer.push_back( '+' );
#endif
			else if( c == KEY_RESIZE )
				CONSOLE_Resize( );

			// clamp input buffer size

			if( gInputBuffer.size( ) > 200 )
				gInputBuffer.erase( 200 );

			c = wgetch( gInputWindow );
			gInputWindowChanged = true;
		}

		CONSOLE_Draw( );

		if( Quit )
			break;
	}

	// shutdown ghost

	CONSOLE_Print( "[CCBOT] shutting down" );
	delete gCCBot;
	gCCBot = NULL;

#ifdef WIN32
	// shutdown winsock

	CONSOLE_Print( "[CCBOT] shutting down winsock" );
	WSACleanup( );
#endif

	// shutdown curses

	endwin( );
	
	if( gRestart )
	{
#ifdef WIN32
	_spawnl( _P_OVERLAY, "ccbot.exe", "ccbot.exe", NULL );
#else		
	execl( "ccbot++", "ccbot++", NULL );				
#endif
	}

	return 0;
}