#include #include #include #include #include #include #include #include #include #include #include #include #include #define BUFSIZE 100 //gibson.freenode.net:6667 // "Global" variables char nick[20]; char chan[20]; int sock; void printPrompt(); int connectToServer(char *server, int port); int joinChannel(char *channel); int sendMessage(char *msg); void listenForInput(); int main(int argc, char ** argv) { char buf[BUFSIZE]; char server[BUFSIZE]; int retval; int port; char *ptr; int test; if (argc < 2) { printf("Missing argument\n"); exit(1); } strcpy(nick, argv[1]); printf("Welcome to irc3190!\n\n"); // Main loop while(1) { // Get input from user printPrompt(); bzero(buf, BUFSIZE); fgets(buf, BUFSIZE, stdin); // Figure out what to do with the input if (strcmp(buf, "exit\n") == 0) { exit(0); // Connect to server } else if (strstr(buf, "/connect") == buf) { port = atoi(strchr(buf, ':') + 1); ptr = buf + strlen("/connect "); test = strchr(ptr, ':') - ptr; strncpy(server, ptr, test); server[test] = '\0'; retval = connectToServer(server, port); if (retval < 0) printf("Could not connect to server!\n"); else printf("Connected to server!\n"); // Join a channel } else if (strstr(buf, "/join") == buf) { retval = joinChannel(buf + strlen("/join ")); if (retval < 0) printf("Could not join channel!\n"); else printf("Join channel!\n"); // Check for unknown command } else if (strstr(buf, "/") == buf) { printf("Unknown command\n"); // Send normal message } else { sendMessage(buf); } } } void printPrompt() { printf(" =>"); } // Connect to a given server, this will also create a new thread that listens // for input on the created socket int connectToServer(char *server, int port) { struct sockaddr_in serveraddr; struct hostent *hostp; int test; printf("Server: %s\n", server); printf("Port: %d\n", port); if (server == NULL) { return -1; } // Retrieves info from DNS server. if ((hostp = gethostbyname(server)) != 0) { sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock < 0) { perror("Could not create socket\n"); return -1; } bzero(&serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; memcpy(&serveraddr.sin_addr, hostp->h_addr, hostp->h_length); serveraddr.sin_port = htons(port); test = connect(sock, (struct sockaddr*)&serveraddr, sizeof serveraddr); if (test != 0) { printf("Unable to connect to server: %s\n", server); return -1; } if (fork() == 0) { listenForInput(); } printf("Hello?\n"); char userMsg[100] = "USER "; strcat(userMsg, nick); strcat(userMsg, " * *: Mats\n"); char nickMsg[100] = "NICK "; strcat(nickMsg, nick); strcat(nickMsg, "\n"); write(sock, &nickMsg, strlen(nickMsg)); write(sock, &userMsg, strlen(userMsg)); printf("Connected to: %s\n", server); return 0; } else { printf("Unknown server\n"); return -1; } return -1; } // This function does nothing but listen for input on our socket void listenForInput() { int bufSize = 1000; char buf[bufSize]; int test; while(1) { bzero(buf, bufSize); test = read(sock, buf, bufSize); fprintf(stderr, "%s", buf); if (strstr(buf, "PING") == buf) { buf[1] = 'O'; printf("Send PONG!\n"); write(sock, buf, strlen(buf)); } } } // Join a channel int joinChannel(char *channel) { // channel -1 to avoid '\n' at the end strncpy(chan, channel, strlen(channel) - 1); char buf[100] = "JOIN #"; strcat(buf, channel); strcat(buf, "\n"); write(sock, buf, strlen(buf)); return 0; } int sendMessage(char *msg) { char buf[100] = "PRIVMSG #"; strcat(buf, chan); strcat(buf, " :"); strcat(buf, msg); write(sock, buf, strlen(buf)); return 0; }