attempted irc bot

This is a discussion on attempted irc bot within the C Programming forums, part of the General Programming Boards category; Code: /*********************************************************** * ircbot.c * currently having issues with sprintf() * don't think it's actually sending the defined values * ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    29

    attempted irc bot

    Code:
    /***********************************************************
    * ircbot.c
    * currently having issues with sprintf()
    * don't think it's actually sending the defined values
    * work on it after I'm not wasted
    *
    ***********************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    
    #define IRCSERVER "blah.net"
    #define PORT 6667 
    #define PASSWORD "blah"
    #define IRCNICK "blah"
    #define USERINFO "blah blah blah :blah"
    #define OnCONNECT "PRIVMSG NickServ :IDENTIFY blah"
    #define MAXDATASIZE 300
    
    char *grabpong(char input[]) {
    	char find_string[] = "PONG";
    	char *pass1, *pass2;
    
    	pass1 = strstr(input, find_string);
    	pass2 = strtok(pass1, find_string);
    	printf("%s\n", pass2);
    return(pass2);
    }
    
    int main()
    {
    
    	char pong[MAXDATASIZE];
    
    	int sockfd; // for recieve
    	int len; // for send  
    	char buf[MAXDATASIZE];
    	struct hostent *he;
    	struct sockaddr_in their_addr; // connector's address information 
    
    
    	if ((he=gethostbyname(IRCSERVER)) == NULL) {  // get the host info 
    	    perror("gethostbyname");
    	    exit(1);
    	}
    
    
    	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    	    perror("socket");
    	    exit(1);
    	}
    
    
    	their_addr.sin_family = AF_INET;    // host byte order 
    	their_addr.sin_port = htons(PORT);  // short, network byte order 
    	their_addr.sin_addr = *((struct in_addr *)he->h_addr);
    	memset(&(their_addr.sin_zero), '\0', 8);  // zero the rest of the struct 
    
    
    	if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
    	    perror("connect");
    	    exit(1);
    	}
    
    	
    	sleep(3);
    
    	len = strlen(buf);
    	sprintf(buf, "PASS %s\r\nNICK %s\r\n", PASSWORD,IRCNICK);
    	send(sockfd, buf, len, 0);	
    
    	sleep(3);
    
    	recv(sockfd, buf, MAXDATASIZE-1, 0);
    	printf("RECIEVED DATA:%s", buf);
    
    	if (strcpy(pong, buf) == 0) {
    		perror("strcpy");
    	}
    	char *PONG_DATA = grabpong(pong);
    	
    	sprintf(buf, "PONG %s\r\nUSER %s\r\n %s\r\n",PONG_DATA,USERINFO,OnCONNECT);
    
    	// buf[numbytes] = '\0';
    
    	close(sockfd);
    
    	return 0;
    }
    This is my very sloppy, unapologetic, irc-bot. Just go wild. You see an error, you show me who is boss. It's actually not even working now. Hence the post

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    	len = strlen(buf);
    	sprintf(buf, "PASS %s\r\nNICK %s\r\n", PASSWORD,IRCNICK);
    	send(sockfd, buf, len, 0);
    You can only get the string length after you've initialized it:
    Code:
    	sprintf(buf, "PASS %s\r\nNICK %s\r\n", PASSWORD,IRCNICK);
    	len = strlen(buf);
    	send(sockfd, buf, len, 0);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    > char *PONG_DATA = grabpong(pong);
    1. don't use upper case for variable names - only for #define NAMES
    2. This is C99 (or C++) code. Normal C doesn't support embedded declarations.

    > if (strcpy(pong, buf) == 0)
    This can never happen - strcpy always returns it's first parameter, so unless you passed it NULL to being with.......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design guidelines advice - IRC Bot class
    By IceDane in forum C# Programming
    Replies: 2
    Last Post: 12-05-2008, 01:21 AM
  2. Need some help with making irc bot
    By kazz in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2007, 11:47 AM
  3. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 06:34 PM
  4. Whats wrong with my IRC Bot??
    By peradox in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-23-2005, 06:16 PM

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