Whats wrong with my IRC Bot??

This is a discussion on Whats wrong with my IRC Bot?? within the Networking/Device Communication forums, part of the General Programming Boards category; Well, here is the source code for my bot, but it doesn't seem to work. I would appreciate it if ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    23

    Exclamation Whats wrong with my IRC Bot??

    Well, here is the source code for my bot, but it doesn't seem to work. I would appreciate it if you could point out any errors you see. Thank you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    #include <signal.h>
    #include <errno.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <dlfcn.h>
    #include <pthread.h>
    
    struct bot_t {
    	int sock;
    	FILE *toserver;
    	FILE *fromserver;
    	char server_name[256];
    	struct hostent *server;
    	struct sockaddr_in addr_sevr;
    	char channel[256];
    	char nick[256]; 
    	int port;
    	char *host;
    	char *domain;
    	char *realname;
    	int state;
    };
    
    struct bot_t bot;
    
    int main(int argc, char **argv) {
    	int z;
    	char *input = NULL;
    	char *pong;
    	int length;
    	
    	puts("Enter the IRC server you want to connect to");
    	scanf("%s", bot.server_name);
    	bot.server = gethostbyname(bot.server_name);
    	if (bot.server == NULL) {
    		puts("Could not resolve host: Did you mispell it?");
    		return 1;
    	}
    	
    	puts("Enter the channel to join");
    	scanf("%s", bot.channel);
    	if (bot.channel[0] != '#' &&
    		bot.channel[0] != '&') {
    		puts("Please Enter a Valid Channel");
    		return 1;
    	}
    	
    	puts("Enter a Nickname for Perabot");
    	scanf("%s", bot.nick);
    	
    	bot.port = 6667;
    	bot.host = "RANDOMHOST";
    	bot.domain = "RANDOMDOMAIN";
    	bot.realname = "Perabot, by Peradox";
    	
    	printf("Forming Socket...\t)");
    	bot.sock = socket(PF_INET, SOCK_STREAM, 0);
    	if (bot.sock < 0) {
    		puts("Couldn't create socket");
    		return 1;
    	}
    	bot.fromserver = fdopen(bot.sock, "r");
    	bot.toserver = fdopen(dup(bot.sock), "w");
    	
    	memset(&(bot.addr_sevr), 0, sizeof(bot.addr_sevr));
            bot.addr_sevr.sin_family = AF_INET;
            bot.addr_sevr.sin_port = htons(bot.port);
            bot.addr_sevr.sin_addr.s_addr = /*inet_addr(bot.server->h_addr_list[0]) */
    				inet_addr("195.197.175.21"); /* orwell.freenode.net */
            length = sizeof(bot.addr_sevr);
    	
    	if(bot.addr_sevr.sin_addr.s_addr == INADDR_NONE) {
    		puts("Bad Address");
    		return 1;
    	}
    	
            /* connect */
    	printf("Connecting...");
    	z = connect(bot.sock, (struct sockaddr *) &(bot.addr_sevr), length);
    	if (z == -1) {
    		puts("Could not connect to server");
    		return 1;
    	}
    	
    	
    	printf("\tRegistering...");
    	/* Register User */
    	fprintf(bot.toserver, "NICK %s\r", bot.nick);
    	fprintf(bot.toserver, "USER %s %s %s :%s\r", bot.nick, bot.domain, bot.host, bot.realname);
    	fflush(bot.toserver);
    	sleep(1);
    	
    	/* Pong Reply */
    	while(input == NULL)
    		fscanf(bot.fromserver, "%s", input);
    	pong = strstr(input, "PING");
    	pong += 5;
    	fprintf(bot.toserver, "PONG %s", pong);
    	
    	printf("\tJoining Channel...");
    	/* join the channel */
    	fprintf(bot.toserver, "JOIN %s", bot.channel);
    	
    
    	printf("\tReady\n");
    	for(;;) {
                    /* just send a ping every once in a while so we don't get logged off */
    		fprintf(bot.toserver, "PING :123456789");
    		sleep(2);
    	}
    	
    	shutdown(bot.sock, SHUT_RDWR);
    	fclose(bot.toserver);
    	fclose(bot.fromserver);
    	close(bot.sock);
    	return 0;
    }
    BTW, I'm running Mac OS X 10.4.1. I get a bus error (which is about the same as a segmentation fault on the input, but I know for a fact that the rest is not working either. Thanks again. Also, source code for a simple bot (preferably something that just kinda hangs around on IRC) is greatly appreciated.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Are you following the internet relay chat protocol?

    http://www.cse.ohio-state.edu/cgi-bin/rfc/rfc1459.html

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    23
    Yes, I am. I've read the RFC.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Yes, I am
    No, you're not. All IRC messages must end in \r\n

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    23
    Thank you. A lot.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    23
    Here's the updated code, however its still not working . Any more help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    #include <signal.h>
    #include <errno.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <dlfcn.h>
    #include <pthread.h>
    
    
    struct bot_t {
    	int sock;
    	FILE *toserver;
    	FILE *fromserver;
    	char server_name[256];
    	struct hostent *server;
    	struct sockaddr_in addr_sevr;
    	char channel[256];
    	char nick[256]; 
    	int port;
    	char *mode;
    	char *unused;
    	char *realname;
    	int state;
    };
    
    
    struct bot_t bot;
    
    
    int main(int argc, char **argv) {
    	int z;
    	char input[4086];
    	char *pong = NULL;
    	int length;
    	
    	puts("Enter the IRC server you want to connect to");
    	scanf("%s", bot.server_name);
    	bot.server = gethostbyname(bot.server_name);
    	if (bot.server == NULL) {
    		puts("Could not resolve host: Did you mispell it?");
    		return 1;
    	}
    	
    	puts("Enter the channel to join");
    	scanf("%s", bot.channel);
    	if (bot.channel[0] != '#' &&
    		bot.channel[0] != '&') {
    		puts("Please Enter a Valid Channel");
    		return 1;
    	}
    	
    	puts("Enter a Nickname for Perabot");
    	scanf("%s", bot.nick);
    	
    	bot.port = 6667;
    	bot.unused = "*";
    	bot.mode = "0";
    	bot.realname = "Perabot, by Peradox";
    	
    
    	printf("Forming Socket...\n");
    	bot.sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if (bot.sock < 0) {
    		puts("Couldn't create socket");
    		return 1;
    	}
    
    	bot.fromserver = fdopen(bot.sock, "r");
    	bot.toserver = fdopen(dup(bot.sock), "w");
    	
    	memset(&(bot.addr_sevr), 0, sizeof(bot.addr_sevr));
            bot.addr_sevr.sin_family = AF_INET;
            bot.addr_sevr.sin_port = htons(bot.port);
    	/* FIXME */
            bot.addr_sevr.sin_addr.s_addr = /*inet_addr(bot.server->h_addr_list[0]) */
    				inet_addr("195.197.175.21"); /* orwell.freenode.net */
    	/* /FIXME */
           length = sizeof(bot.addr_sevr);
    	
    	if(bot.addr_sevr.sin_addr.s_addr == INADDR_NONE) {
    		puts("Bad Address");
    		return 1;
    	}
    
    	printf("Connecting...\n");
    	z = connect(bot.sock, (struct sockaddr *) &(bot.addr_sevr), length);
    	if (z == -1) {
    		puts("Could not connect to server");
    		return 1;
    	}
    	
    	printf("Registering...\n");
    	/* Register User */
    	fprintf(bot.toserver, "NICK %s\r\n", bot.nick);
    	fprintf(bot.toserver, "USER %s %s %s :%s\r\n", bot.nick, bot.mode, bot.unused, bot.realname);
    	fflush(bot.toserver);
    	sleep(1);
    	
    	/* Pong Reply */
    	while (pong == NULL) {
    		fscanf(bot.fromserver, "%s", input);
    		pong = strstr(input, "PING");
    	}
    	pong += 5;
    	fprintf(bot.toserver, "PONG %s\r\n", pong);
    	
    	printf("Joining Channel...\n");
    	/* join the channel */
    	fprintf(bot.toserver, "JOIN %s\r\n", bot.channel);
    	
    	printf("Ready...\n");
    	for(;;) {
    		fprintf(bot.toserver, "PING :123456789\r\n");
    		sleep(2);
    	}
    	
    	/* quit */
    	fprintf(bot.toserver, "QUIT :Disgracefully Discharged\r\n");
    	
    	shutdown(bot.sock, SHUT_RDWR);
    	fclose(bot.toserver);
    	fclose(bot.fromserver);
    	close(bot.sock);
    	return 0;
    }
    Thank you.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    what part isn't working?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Useful Links And Good Books
    Use ethereal to compare what a real IRC client does with what your bot does.
    When you see a difference, examine the code running at that point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    23
    Thank you salem; thats a great idea

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot Base and RicBot Google.
    By John_ in forum C++ Programming
    Replies: 3
    Last Post: 02-07-2006, 12:16 PM
  2. attempted irc bot
    By stormy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 12:47 AM
  3. help with IRC channel ban
    By MisterSako in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-05-2004, 01:23 PM
  4. IRC Bot
    By eam in forum Networking/Device Communication
    Replies: 10
    Last Post: 07-06-2004, 01:46 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21