Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdarg.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include<process.h>
- #include<ctype.h>
- #include<string.h>
- #include<time.h>
- void senda( SOCKET sock, char* str )
- {
- send( sock, str, strlen( str ), 0 );
- }
- void sendb( SOCKET sock, char* str1, char* str2 )
- {
- char sendbuf[1000];
- sprintf( sendbuf, "%s%s", str1, str2 );
- senda( sock, sendbuf );
- }
- unsigned long resolveAddr( char* addr, int ind = -1)
- {
- hostent* host = gethostbyname( addr );
- if( host == 0 )
- {
- printf( "Failed to lookup %s.", addr );
- return 0;
- }
- if( ind > host->h_length ) return -1;
- unsigned long myhost = *((unsigned long*) host->h_addr);//_list[host->h_length - ind];
- printf( "%Host: %i.%i.%i.%i\n", myhost & 0xFF, ( myhost >> 8 ) & 0xFF, ( myhost >> 16 ) & 0xFF, ( myhost >> 24 ) & 0xFF );
- return myhost;
- }
- char* getMessage( SOCKET recvOn, char* buffer )
- {
- char tmp = 0;
- int pos = 0;
- int val = 1;
- //printf( "aloal" );
- val = recv( recvOn, &tmp, 1, 0 );
- if( val == 0 )
- {
- buffer[0] = 0;
- return (char*)1;
- }
- if( val < 0 )
- {
- buffer[0] = 0;
- return 0;
- }
- //printf( "aloal" );
- while( tmp != '\n' && val > 0 )
- {
- //printf( "aloal" );
- //if( tmp > 28 )
- buffer[pos++] = tmp;
- val = recv( recvOn, &tmp, 1, 0 );
- if( val <= 0 )
- {
- buffer[pos] = 0;
- return buffer;
- }
- }
- buffer[pos++] = 0;
- return buffer;
- }
- SOCKET connectTCP( unsigned long ip, short int port )
- {
- SOCKET ret;
- ret = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if( ret < 0 ) return 0;
- sockaddr_in clientService;
- clientService.sin_family = AF_INET;
- clientService.sin_addr.s_addr = ip;
- clientService.sin_port = htons( port );
- int result = connect( ret, (sockaddr*) &clientService, sizeof(clientService));
- if( result < 0 ) return 0;
- return ret;
- }
- void sendMsg( SOCKET tosend, const char* format, ... )
- {
- char sendBuf[1000];
- va_list strt;
- va_start( strt, format );
- vsprintf( sendBuf, format, strt );
- va_end( strt );
- sendb( tosend, sendBuf, "\r\n" );
- }
- struct param{
- SOCKET sock;
- };
- void recvloop( void* args ){
- param* p = (param*) args;
- char buf[5000];
- while( true ){
- getMessage( p->sock, buf );
- strupr( buf );
- buf[4] = 0;
- if( strcmp( buf, "PING") == 0 ){
- buf[4] = ' ';
- buf[1] = 'O';
- senda( p->sock, buf );
- }
- }
- }
- int main(){
- WSADATA globalWSAData;
- WSAStartup( MAKEWORD(2, 2), &globalWSAData );
- char get[1000];
- char nick[1000];
- char who[1000];
- char what[1000];
- char getty[1000];
- int i_min, i_max;
- printf("Nickname: ");
- gets( nick );
- printf("Server: ");
- gets(get );
- printf("Pester who: ");
- gets( who );
- printf("With what: ");
- gets( what );
- printf("Interval min (milliseconds): ");
- gets( getty );
- i_min = strtol( getty, 0, 10 );
- printf("Interval max (milliseconds): ");
- gets( getty );
- i_max = strtol( getty, 0, 10 );
- if( i_max < i_min ){
- i_max ^= i_min;
- i_min ^= i_max;
- i_max ^= i_min;
- }
- SOCKET sock = connectTCP( resolveAddr( get ), 6667 );
- sendMsg( sock, "NICK %s", nick );
- sendMsg( sock, "USER %s %s %s :%s", nick, nick, nick, nick );
- param p;
- p.sock = sock;
- _beginthread( recvloop, 1000, &p );
- srand( time(NULL) );
- while( true ){
- Sleep( rand() % abs(i_max - i_min) + i_min );
- sendMsg( sock, "NOTICE %s :%s", who, what);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement