-
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 :)
-
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);
-
> 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.......