diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 146 |
1 files changed, 99 insertions, 47 deletions
@@ -19,6 +19,7 @@ #include <unistd.h> #include <string.h> #include <errno.h> +#include <unistd.h> /* Networking libs */ #include <arpa/inet.h> @@ -26,10 +27,16 @@ #include <sys/socket.h> #include <netdb.h> + +// For trapping Ctrl+c +#include <signal.h> + #include "common.h" #define buflen 512 +static int socket_desc; + int newconn(char* addr, char* port) { int status; int socket_desc; @@ -79,71 +86,92 @@ int recv_line(int sockd, char* out) { } +char* irc_get_msg(char* str, char* buf ) { + int i = 2; + int bufi = 0; + // Place the start cursor + while(i < strlen(str)) { + if(str[i] == ':') { + i++; + printf("Found beginning of message at index %d\n", i); + break; + } + i++; + } -int doloop(int sockd, int ret) { + while(str[i] != '\r' && str[i] != '\n' && str[i] != '\0') { + buf[bufi] = str[i]; + bufi++; + i++; + } + + buf[bufi] = '\0'; + return buf; +} + + +int doloop(int sockd, int ret, char* chan) { char buf[buflen]; char resp[buflen]; + char msg[1024]; int len; while(1) { len = recv_line(sockd, buf); if(len == 0) continue; - //if(len == -1) return -1; - - printf("--- %s", buf); if(strncmp(buf, "PING", 4) == 0) { - strcpy(resp, "PONG "); - strcat(resp, &buf[5]); + sprintf(&resp[0], "PONG %s", &buf[5]); + printf("pong: %s", resp); send(sockd, resp, strlen(resp), 0); - - printf("%s", resp); - if(ret == 1) return 0; - continue; - } - - if(strstr(buf, "Looking up your hostname") != NULL) { + } else if(strstr(buf, "ircbot quit") != NULL) { + // Quit command + break; + } else if(strstr(buf, "ircbot say ") != NULL) { + // The say command + irc_get_msg(buf, msg); + sprintf(&resp[0], "PRIVMSG %s :%s\n", chan, &msg[11]); + printf("Say command received. Saying \"%s\"\n", &msg[11]); + send(sockd, resp, strlen(resp), 0); + } else if(strstr(buf, "ircbot meaning of life") != NULL) { + // The mol command + sprintf(&resp[0], "PRIVMSG %s :%d\n", chan, 42); + send(sockd, resp, strlen(resp), 0); + } else if(strstr(buf, "Looking up your hostname") != NULL) { // Receive next line recv_line(sockd, buf); printf(buf); - return 0; + break; + } else if(strstr(buf, "Nickname is already in use.") != NULL) { + // We can't proceed if nickname is already in use. + printf(buf); + return -1; } - len = 0; + + printf(buf); + //len = 0; } return 0; } -int irc_auth(int sockd, char* nick, char* user) { +int irc_auth(int sockd, char* nick, char* user, char* chan) { + int status; char nickstr[buflen]; char userstr[buflen]; char joinstr[buflen]; - char sendstr[buflen]; // Compose NICK statement - strcpy(nickstr, "NICK "); - strcat(nickstr, nick); - strcat(nickstr, "\n"); - + sprintf(nickstr, "NICK %s\n", nick); // Compose NICK statement - strcpy(userstr, "USER "); - strcat(userstr, nick); - strcat(userstr, " 0 * :"); - strcat(userstr, user); - strcat(userstr, "\n"); - + sprintf(userstr, "USER %s 0 * : %s\n", nick, user); // Compose the JOIN statement - strcpy(joinstr, "JOIN #general\n"); - - // Compose the PRIVMSG - strcpy(sendstr, "PRIVMSG #general :I'm here!\n"); - - + sprintf(joinstr, "JOIN %s\n", chan); // Wait for hostname identification - doloop(sockd, 1); + doloop(sockd, 1, chan); // Send NICK command send(sockd, nickstr, strlen(nickstr), 0); @@ -154,32 +182,52 @@ int irc_auth(int sockd, char* nick, char* user) { printf("%s\n", userstr); // Wait for ping and send pong response - doloop(sockd, 1); - - sleep(1); + printf("Waiting for initial ping\n"); + status = doloop(sockd, 1, chan); + if(status == -1) return -1; // Send JOIN command + printf("Joining channel %s\n", chan); send(sockd, joinstr, strlen(joinstr), 0); - printf(joinstr); - // Send a channel message - send(sockd, sendstr, strlen(sendstr), 0); - printf(sendstr); + printf("Entering main watcher loop\n"); + doloop(sockd, 0, chan); - //send(sockd, "QUIT\n", 6, 0); - - doloop(sockd, 0); - printf("FIN!\n"); + // send quit message + char quitstr[512]; + sprintf(quitstr, "QUIT :Blowing this popsicle stand\n"); + send(sockd, quitstr, strlen(quitstr), 0); + close(sockd); return 0; } +void quit(int signal) { + char quitstr[512]; + sprintf(quitstr, "QUIT :Force quit!\n"); + send(socket_desc, quitstr, strlen(quitstr), 0); + printf("\n\nQuitting, signal %d\n\n\n", signal); + + //doloop(socket_desc, 1); + close(socket_desc); + exit(128 + signal); +} + int main(int argc, char* argv[]) { char *server = argv[1]; char *port = argv[2]; + char *nick = argv[3]; + char *chan = argv[4]; + int status; - int socket_desc; + if(argc < 4) { + printf("\nERROR: Irc server, port, and nick required to connect\n\n"); + printf("Usage:\n ircbot irc.server.tld 6667 jcricket\n\n"); + return 1; + } + + //int socket_desc; socket_desc = newconn(server, port); @@ -188,7 +236,11 @@ int main(int argc, char* argv[]) { return 1; } - irc_auth(socket_desc, "nullspoon", "nullspoon"); + signal(SIGINT, quit); + + + status = irc_auth(socket_desc, nick, nick, chan); + if(status == -1) return 1; // Cleanup close(socket_desc); |