#include #include #include #include #include #include #include #include using namespace std; char *substr (const char *str, int start, int end) { char *sub = new char[strlen(str)]; start -=1 ; for(int i = 0;starth_addr); memset(&(their_addr.sin_zero), '\0', 8); if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); } strcpy(msg,"USER jay jay jay :jay jay\r\nNICK jaybot\r\n"); if (send(sockfd, msg, strlen(msg), 0) == -1) { perror("send"); } while (stay) { // get info from the server, put it in buffer if ((length=recv(sockfd, buffer, 511, 0)) == -1) { perror("recv"); exit(1); } buffer[length] = '\0'; for (int i = 0; buffer[i] != '\0'; i += 1) { if (strncmp(substr(buffer,i,i+4),"PING",4) == 0) { // reply to ping with pong, also joining #xboxrocks sprintf(msg, "PONG %s\r\njoin :#xboxrocks\r\n",substr(buffer,i+6,i+13)); if (send(sockfd, msg, strlen(msg), 0) == -1) { perror("send"); } } if (strncmp(substr(buffer,i,i+5),"die",5) == 0) { strcpy(msg, "PRIVMSG #xboxrocks :die!\r\nQUIT\r\n"); if (send(sockfd, msg, strlen(msg), 0) == -1) { perror("send"); } stay = 0; // leave our while() } } } close(sockfd); return 0; }