Created
February 1, 2013 14:33
-
-
Save LambdaSix/4691633 to your computer and use it in GitHub Desktop.
Simple IRC bot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio> | |
#include <stdlib> | |
#include <stdarg> | |
#include <winsock.h> | |
#include <errno.h> | |
#define WIN32_LEAN_AND_MEAN | |
#define _SERVER_PORT 6667 | |
#define _SERVER_HOST "irc.freenode.org" | |
#define _SECURE_LOGIN | |
#if defined(_SECURE_LOGIN) | |
#define _SERVER_USER | |
#define _SERVER_PASS | |
#endif | |
sockaddr_in host; | |
char sendbuf; | |
char recvbuf[5000]; | |
int recv_data; | |
char * hostname = _SERVER_HOST; | |
SOCKET ConnectSocket; | |
WSADATA wsaData; | |
hostent * Hostentity; | |
struct User | |
{ | |
char * name; | |
char * nick; | |
char * mail; | |
char * idnt; | |
int inchan; | |
}; | |
struct DLLHandle | |
{ | |
char * Filename; | |
HMODULE h; | |
unsigned long RetVal; | |
}; | |
User user; | |
DLLHandle Handle; | |
//////////////////////////////////////////////////////////////////////////////// | |
// filluserdetails( void ) | |
//////////////////////////////////////////////////////////////////////////////// | |
// Populates the User struct with data. | |
//////////////////////////////////////////////////////////////////////////////// | |
void filluserdetails( void ) | |
{ | |
user.name = "Ted2.0"; | |
user.nick = "Ted"; | |
user.mail = "ted@ted.com"; | |
user.idnt = "Ted"; | |
} | |
void forkDLL( void ) | |
{ | |
Handle.Filename = "TParseEN.dll"; | |
Handle.h = LoadLibrary( TEXT(Handle.Filename) ); | |
if (Handle.h == NULL) { | |
Handle.RetVal = GetLastError(); | |
} | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
// resolve( char * h ) | |
//////////////////////////////////////////////////////////////////////////////// | |
// Resolves a hostname (h) into an IP suitible for s_addr. | |
//////////////////////////////////////////////////////////////////////////////// | |
long resolve( char * h ) | |
{ | |
struct in_addr ip; | |
struct hostent * he; | |
if ( ( ip.s_addr = inet_addr( h ) ) == -1 ) | |
{ | |
if ( !( he = gethostbyname( h ) ) ) | |
return ( -1 ); | |
else | |
memcpy( & ip.s_addr, he->h_addr, 4 ); | |
} | |
return ( ip.s_addr ); | |
} | |
int main( int argc, char * argv[] ) | |
{ | |
//-------------------- | |
// Initialize Winsock | |
int iResult = WSAStartup( MAKEWORD( 2, 2 ), & wsaData ); | |
if ( iResult != NO_ERROR ) | |
{ | |
printf( "Error at WSAStartup()\n" ); | |
} | |
//--------------------- | |
// Create a SOCKET for connecting to server | |
ConnectSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); | |
if ( ConnectSocket == INVALID_SOCKET ) | |
{ | |
printf( | |
"%s/line(%i): Error code: %i; In socket()\n", | |
__FILE__, | |
__LINE__, | |
WSAGetLastError() | |
); | |
WSACleanup(); | |
exit( 0 ); | |
} | |
//---------------------- | |
// The sockaddr_in structure specifies the address family, | |
// IP address, and port of the server to be connected to. | |
host.sin_family = AF_INET; | |
if ( !( host.sin_addr.s_addr = resolve( hostname ) ) ) | |
{ | |
printf( | |
"%s/line(%i): Error code: %i\n", | |
__FILE__, | |
__LINE__, | |
WSAGetLastError() | |
); | |
} | |
host.sin_port = htons( _SERVER_PORT ); | |
//---------------------- | |
// Connect to server. | |
if ( connect( ConnectSocket, ( SOCKADDR * ) & host, sizeof( host ) ) == SOCKET_ERROR ) | |
{ | |
printf( | |
"Main.cpp/line(%i): Failed to connect to Server: %s.\n", | |
__LINE__, | |
host.sin_addr.s_addr | |
); | |
printf( "%s/line(%i): Error code: %i\n", | |
__FILE__, | |
__LINE__, | |
WSAGetLastError() | |
); | |
WSACleanup(); | |
exit( 1 ); | |
} | |
filluserdetails(); | |
sprintf( & sendbuf, | |
"USER %s %s %s :%s\n\r", | |
user.nick, | |
user.mail, | |
user.idnt, | |
user.idnt | |
); | |
send( ConnectSocket, & sendbuf, strlen( & sendbuf ), 0 ); | |
printf( "%i:Send(): %s\n", __LINE__, &sendbuf ); | |
sprintf( & sendbuf, "NICK :%s\n\r", user.nick ); | |
send( ConnectSocket, & sendbuf, strlen( & sendbuf ), 0 ); | |
printf( "%i:Send(): %s\n", __LINE__, &sendbuf ); | |
////////////////////////////////////////////////////////////////////////////// | |
// Load the english parser DLL into memory and fork off here. | |
// Just maintain the while loop for basic communications | |
// like PING, console printing and clearing the recvbuf | |
// | |
// Should this moved inside the loop? Since the DLL needs access | |
// to the recvbuf for parsing. Or at least a constantly updated one. | |
////////////////////////////////////////////////////////////////////////////// | |
forkDLL(); | |
while ( 1 ) | |
{ | |
recv_data = recv( ConnectSocket, recvbuf, 5000, 0 ); | |
if ( recv_data == SOCKET_ERROR ) | |
{ | |
printf( | |
"%s/line(%i): Error code: (%i) [Socket disconnected]\n", | |
__FILE__, | |
__LINE__, | |
WSAGetLastError() | |
); | |
printf( "Terminating process.\n" ); | |
exit( 0 ); | |
} | |
if ( recvbuf[0] == 'P' || recvbuf[2] == 'N' ) | |
{ | |
recvbuf[1] = 'O'; | |
strncpy(&sendbuf, recvbuf, 50); | |
printf( "%i:Send(): %s\n", __LINE__, &sendbuf ); | |
send( ConnectSocket, & sendbuf, strlen(& sendbuf ), 0 ); | |
if (!user.inchan) | |
{ | |
sprintf( & sendbuf, "JOIN :#csharp\n\r" ); | |
send( ConnectSocket, & sendbuf, strlen( & sendbuf ), 0 ); | |
printf( "%i:Send(): %s\n", __LINE__, &sendbuf ); | |
user.inchan = 1; | |
} | |
} | |
// Hardcoded commands | |
if ( strstr(recvbuf, "!Info") ) { | |
char heapVersion; | |
sprintf(heapVersion, | |
"PRIVMSG #csharp :Ted version %i running on %s. Currently using" | |
, _BuildNumber, GetOSInfo(), | |
send( ConnectSocket, "PRIVMSG #csharp", ,0 | |
} | |
printf( "Recv(): %s\n", recvbuf ); | |
memset(recvbuf, 0, 5000); | |
} | |
WSACleanup(); | |
exit( 1 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment