Advertisement
Aslai

IRC Pester

Feb 4th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. #include <winsock2.h>
  6. #include <ws2tcpip.h>
  7. #include<process.h>
  8. #include<ctype.h>
  9. #include<string.h>
  10. #include<time.h>
  11.  
  12. void senda( SOCKET sock, char* str )
  13. {
  14.     send( sock, str, strlen( str ), 0 );
  15. }
  16.  
  17. void sendb( SOCKET sock, char* str1, char* str2 )
  18. {
  19.     char sendbuf[1000];
  20.     sprintf( sendbuf, "%s%s", str1, str2 );
  21.  
  22.     senda( sock, sendbuf );
  23. }
  24.  
  25. unsigned long resolveAddr( char* addr, int ind = -1)
  26. {
  27.     hostent* host = gethostbyname( addr );
  28.     if( host == 0 )
  29.     {
  30.         printf( "Failed to lookup %s.", addr );
  31.         return 0;
  32.     }
  33.     if( ind > host->h_length ) return -1;
  34.     unsigned long     myhost = *((unsigned long*) host->h_addr);//_list[host->h_length - ind];
  35.     printf( "%Host: %i.%i.%i.%i\n", myhost & 0xFF, ( myhost >> 8 ) & 0xFF, ( myhost >> 16 ) & 0xFF, ( myhost >> 24 ) & 0xFF );
  36.     return myhost;
  37. }
  38.  
  39. char* getMessage( SOCKET recvOn, char* buffer )
  40. {
  41.     char tmp = 0;
  42.     int pos = 0;
  43.     int val = 1;
  44.     //printf( "aloal" );
  45.     val = recv( recvOn, &tmp, 1, 0 );
  46.  
  47.     if( val == 0 )
  48.     {
  49.         buffer[0] = 0;
  50.         return (char*)1;
  51.     }
  52.     if( val < 0 )
  53.     {
  54.         buffer[0] = 0;
  55.         return 0;
  56.     }
  57.  
  58.     //printf( "aloal" );
  59.     while( tmp != '\n' && val > 0 )
  60.     {
  61.         //printf( "aloal" );
  62.         //if( tmp > 28 )
  63.         buffer[pos++] = tmp;
  64.         val = recv( recvOn, &tmp, 1, 0 );
  65.         if( val <= 0 )
  66.         {
  67.             buffer[pos] = 0;
  68.             return buffer;
  69.         }
  70.     }
  71.     buffer[pos++] = 0;
  72.     return buffer;
  73. }
  74.  
  75. SOCKET connectTCP( unsigned long ip, short int port )
  76. {
  77.     SOCKET ret;
  78.     ret = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  79.     if( ret < 0 ) return 0;
  80.     sockaddr_in clientService;
  81.     clientService.sin_family = AF_INET;
  82.     clientService.sin_addr.s_addr = ip;
  83.     clientService.sin_port = htons( port );
  84.     int result = connect( ret, (sockaddr*) &clientService, sizeof(clientService));
  85.     if( result < 0 ) return 0;
  86.     return ret;
  87. }
  88.  
  89.  
  90. void sendMsg(  SOCKET tosend, const char* format, ... )
  91. {
  92.  
  93.     char sendBuf[1000];
  94.     va_list strt;
  95.     va_start( strt, format );
  96.     vsprintf( sendBuf, format, strt );
  97.     va_end( strt );
  98.     sendb( tosend, sendBuf, "\r\n" );
  99. }
  100.  
  101. struct param{
  102.     SOCKET sock;
  103. };
  104.  
  105. void recvloop( void* args ){
  106.     param* p = (param*) args;
  107.     char buf[5000];
  108.     while( true ){
  109.         getMessage( p->sock, buf );
  110.         strupr( buf );
  111.         buf[4] = 0;
  112.         if( strcmp( buf, "PING") == 0 ){
  113.             buf[4] = ' ';
  114.             buf[1] = 'O';
  115.             senda( p->sock, buf );
  116.         }
  117.     }
  118. }
  119.  
  120. int main(){
  121.     WSADATA globalWSAData;
  122.     WSAStartup( MAKEWORD(2, 2), &globalWSAData );
  123.     char get[1000];
  124.     char nick[1000];
  125.     char who[1000];
  126.     char what[1000];
  127.     char getty[1000];
  128.     int i_min, i_max;
  129.     printf("Nickname: ");
  130.     gets( nick );
  131.     printf("Server: ");
  132.     gets(get );
  133.     printf("Pester who: ");
  134.     gets( who );
  135.     printf("With what: ");
  136.     gets( what );
  137.     printf("Interval min (milliseconds): ");
  138.     gets( getty );
  139.     i_min = strtol( getty, 0, 10 );
  140.     printf("Interval max (milliseconds): ");
  141.     gets( getty );
  142.     i_max = strtol( getty, 0, 10 );
  143.     if( i_max < i_min ){
  144.         i_max ^= i_min;
  145.         i_min ^= i_max;
  146.         i_max ^= i_min;
  147.     }
  148.  
  149.     SOCKET sock = connectTCP( resolveAddr( get ), 6667 );
  150.     sendMsg( sock, "NICK %s", nick );
  151.     sendMsg( sock, "USER %s %s %s :%s", nick, nick, nick, nick );
  152.     param p;
  153.     p.sock = sock;
  154.     _beginthread( recvloop, 1000, &p );
  155.     srand( time(NULL) );
  156.     while( true ){
  157.         Sleep( rand() % abs(i_max - i_min) + i_min );
  158.         sendMsg( sock, "NOTICE %s :%s", who, what);
  159.     }
  160.  
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement