/* My Simple IRC implemantation */ /* bugy coded by awan */ #include #include #include #include #include #include #include #define size 45 #define buf_size 4096 using namespace std; class client { public: void prisijungti(char *c, int port); char * ns(char *addr); void die(char *msg); void quit(char *msg); void user(char *nick, char *usr, char *name); int parse(int msg); int rasti(char *c); int fill(char *c, char *d); private: int sock; }; void client::prisijungti(char *c, int port) { struct sockaddr_in ircsrv; unsigned int srvlen; if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) die("failed to create socket"); memset(&ircsrv, 0, sizeof(ircsrv)); ircsrv.sin_family = AF_INET; ircsrv.sin_addr.s_addr = inet_addr(ns(c)); ircsrv.sin_port = htons(port); if (connect(sock, (struct sockaddr *) &ircsrv, sizeof(ircsrv)) < 0) die("failed connect to server"); } char * client::ns(char *addr) { struct hostent *host; struct in_addr h_addr; if ((host = gethostbyname(addr)) == NULL) die("Can't get hostname"); h_addr.s_addr = *( (unsigned long *) host-> h_addr_list[0] ); return (inet_ntoa(h_addr)); } void client::die(char *msg) { perror(msg); exit(1); } void client::quit(char *msg) { int len=0; char *sent = (char *) malloc(sizeof(char) * size); strcpy(sent,"QUIT :"); strcat(sent,msg); strcat(sent, "\n"); len = strlen(sent); if (send(sock, sent, len,0) != len) die("send error"); } void client::user(char *nick, char *usr, char *name) { int len=0; char *cmd = (char *) malloc(sizeof(char) * size); char *cmd1 = (char *) malloc(sizeof(char) * size); strcpy(cmd, "NICK "); strcat(cmd,nick );strcat(cmd, " \r\n"); strcpy(cmd1, "USER "); strcat(cmd1,usr); strcat(cmd1," \"test\" \"test\" :"); strcat(cmd1,name); strcat(cmd1, "\r\n"); len = strlen(cmd); if (send(sock, cmd, len,0) != len) die("send error"); len = strlen(cmd1); if (send(sock, cmd1, len,0) != len) die("send error"); } int client::fill(char *c, char *d) { int i=0; int j=0; while (c[i] != ':' && c[i] != '\0') i++; if (c[i] == ':') { i++; while (c[i] != '\0') { d[j] = c[i]; i++; j++; } d[j] = '\0'; return 1; } return 0; } int client::parse(int msg) { char *buffer = (char *) malloc(sizeof(char) * buf_size); char *p, *sent = (char *) malloc(sizeof(char) * size); int bytes=0; int tmp=0; int j=0, len=0; for (;;) { bytes = 0; if ((bytes = recv(sock, buffer, 4095, 0)) < 1) die("failed recieve bytes form server"); if (bytes > 0) { if (msg == 1) for (j=0; j < bytes; j++) printf("%c", buffer[j]); // searching for ping message tmp = rasti(buffer); if (tmp) { if (fill(buffer, p)) { strcpy(sent, "PONG :"); strcat(sent, p); strcat(sent,"\n"); len = strlen(sent); if (send(sock, sent, len, 0) != len) die("send error"); for (j=0; j < 100; j++) sent[j] = ' '; for (j=0; j < size; j++) p[j] = ' '; } } for (j=0; j< 4095; j++) buffer[j] = ' '; } } } int client::rasti(char *c) { char * p = (char *) malloc(sizeof(char) * 4); int i=0; int j=0; int t=0; strcpy(p, "PING"); while (c[i] != '\0') { t = 0; while (c[i] == p[j]) { t++; j++; i++; } if (t== 4) { free(p); return t; } j = 0; i++; } return 0; }