void irc_connect(void) { struct sockaddr_in addr; struct hostent *serv; char buf[IRC_MSG_LEN]; fd = socket(AF_INET, SOCK_STREAM/*|SOCK_NONBLOCK*/, 0); printf("Connecting to %s...\n", conf.host); serv = gethostbyname(conf.host); addr.sin_family = AF_INET; memcpy(&addr.sin_addr.s_addr, serv->h_addr, serv->h_length); addr.sin_port = htons(conf.port); if(connect(fd, (struct sockaddr *) &addr, sizeof(addr))<0) { puts("Connection error"); exit(EXIT_FAILURE); } irc_read(buf); irc_read(buf); strcpy(buf, "USER "); strcat(buf, conf.name); strcat(buf, " "); strcat(buf, conf.name); strcat(buf, " "); strcat(buf, conf.name); strcat(buf, " "); strcat(buf, conf.name); strcat(buf, "\r\n"); irc_cmd(buf); strcpy(buf, "NICK "); strcat(buf, conf.name); strcat(buf, "\r\n"); irc_cmd(buf); irc_read(buf); buf[1] = 'O'; irc_cmd(buf); while(irc_read(buf)) { buf[strlen(conf.name)+1] = '\0'; if(!strcmp(buf+1, conf.name)) break; } strcpy(buf, "JOIN "); strcat(buf, conf.chan); strcat(buf, "\r\n"); irc_cmd(buf); puts("Connected!"); sprintf(buf, "Hello, I'm %s!", conf.name); irc_say(buf); while(irc_read(buf) && irc_get_type(buf)!=T_JOIN); }
void irc_say(char msg[IRC_MSG_LEN]) { char buf[IRC_MSG_LEN]; snprintf(buf, IRC_MSG_LEN, "PRIVMSG %s :%s", conf.chan, msg); irc_cmd(buf); }
void irc_quit(void) { char buf[128]; sprintf(buf, "QUIT :%s", conf.qmsg); irc_cmd(buf); exit(EXIT_SUCCESS); }
/* * This is the join mod. It forces the bot to join a channel. * You have to write * !join #foobar * into a channel or as NOTICE to the JacKBot. It will join the channel #foobar. * JOIN #foobar */ void join(struct _Nfos_ *nfos) { char channel[CHAN_NAME_MAX + 1]; get_from_message(channel, GFM_CHANNEL); irc_cmd("JOIN %s", channel); }
void irc_quit(void) { char buf[128] = "QUIT :"; strcat(buf, conf.qmsg); strcat(buf, "\r\n"); irc_cmd(buf); exit(EXIT_SUCCESS); }
static void rejoin(char **args, enum irc_type type) { char buf[IRC_CHAN_LEN+6]; (void) type; sprintf(buf, "JOIN %s", conf.chan); irc_cmd(buf); irc_reply(args[0], "Why would you kick me? ;_;", T_CHAN); }
void irc_loop(void) { char buf[IRC_MSG_LEN]; while(1) { irc_read(buf); if(strbeg(buf, "PING")) { buf[1] = 'O'; irc_cmd(buf); } else mod_handle(buf); } }
/* * This is the opposite of the invite mod. If someone sends a !part in the channel, the bot will get * :JacK_McRiDER PRIVMSG #foo :!part * This mod reads the channel (#foo) and leaves it (with a little exit message)... * Another way of make him part is * /NOTICE JacKBot !part #foo */ void part(struct _Nfos_ *nfos) { int ctr = 0; char channel[CHAN_NAME_MAX + 1]; debug_out("It's %s!\n", nfos->sender->nickname); if(is_priv()) // if it's from a channel, no private message { get_from_message(channel, GFM_CHANNEL); }else { strncpy(channel, nfos->sender->middle, CHAN_NAME_MAX); } irc_cmd("PART %s :see ya'll, %s wants me to leave!", channel, nfos->sender->nickname); }
void irc_reply(char nick[IRC_NICK_LEN], char msg[IRC_MSG_LEN], enum irc_type type) { char buf[IRC_MSG_LEN]; switch(type) { case T_MSG: snprintf(buf, IRC_MSG_LEN, "PRIVMSG %s :%s", nick, msg); goto finish; case T_CHAN: snprintf(buf, IRC_MSG_LEN, "PRIVMSG %s :%s: %s", conf.chan, nick, msg); goto finish; default: return; } finish: irc_cmd(buf); }
/* * Parse events and execute the appropriate functions. */ int fs_parse(char *buf) { struct bot_in *bot_t = pthread_getspecific(bot); struct socket_in *fs_t = pthread_getspecific(fs_s); vout(3, "FS", "->", buf); /* If a user is added to/deleted from a conference, announce it. */ if(strncmp(buf+strlen(buf)-20, "Action: add-member", 18) == 0 || strncmp(buf+strlen(buf)-20, "Action: del-member", 18) == 0) { char mesg[513], *caller, *conf; size_t len, num = fs_caller_name_re.re_nsub+1, num2 = fs_caller_num_re.re_nsub+1; regmatch_t *preg = calloc(num, sizeof(*preg)), *preg2 = calloc(num2, sizeof(*preg2)); if(regexec(&fs_conference_re, buf, num, preg, 0) != 0) return 0; len = preg[1].rm_eo-preg[1].rm_so; conf = calloc(len+1, sizeof(*conf)); strncpy(conf, buf+preg[1].rm_so, len); if(regexec(&fs_caller_num_re, buf, num2, preg2, 0) == 0) { len = preg2[2].rm_eo-preg2[2].rm_so; caller = calloc(len+3, sizeof(*caller)); /* Setup our mask. */ memcpy(caller, "XXX-XXX-XXXX", 12); /* Copy in our real digits. */ memcpy(caller, buf+preg2[2].rm_so, 3); memcpy(caller+4, buf+preg2[2].rm_so+3, 3); /* * We keep the suffix masked for privacy. You can decide to * show the last for digits of phone numbers by uncommenting the following. * memcpy(caller+8, buf+preg2[2].rm_so+6, 4); */ } else if(regexec(&fs_caller_name_re, buf, num, preg, 0) == 0) { len = preg[1].rm_eo-preg[1].rm_so; caller = calloc(len+1, sizeof(*caller)); strncpy(caller, buf+preg[1].rm_so, len); } else { caller = calloc(8, sizeof(*caller)); strncpy(caller, "Unknown", 8); } /* XXX Use MySQL to get the name of the conference and the channel to say it in. */ if(strncmp(buf+strlen(buf)-20, "Action: add-member", 18) == 0) snprintf(mesg, 512, "%s has joined the %s bridge (ext. %s).", caller, conf, conf); else snprintf(mesg, 512, "%s is leaving the %s bridge (ext. %s).", caller, conf, conf); /* * XXX Replace #telconinja with the actual channel the info should go to. * We will be using the MySQL stuff for this.. but for now, everthing to #tn */ irc_cmd(IRC_ACTION, "#telconinja", mesg); /* Do some cleanup. */ free(caller); /*free(chan); */ free(conf); free(preg); free(preg2); return 0; } if(strncmp(buf, "Content-Type: api/response", 26) == 0) { switch(bot_t->fs_last_api) { case FS_CONFLIST: irc_cmd(IRC_PRIVMSG, "#bots", strstr(buf, "\n\n")+2); break; } return 0; } /* * If we need to authenticate then do so, and also send the event request for the * conference notices. * XXX This probably only happens at the start of the connection and therefore * should appear closer to the end of this function. */ if(strcmp(buf, "Content-Type: auth/request") == 0) { /* * XXX After we get the configuration stuff done, this should be changed * to use the value in the bot's configuration file. */ sprintf(buf, "auth %s\n\nevent plain CUSTOM conference::maintenance\n\n", bot_t->fs_pass); socket_send(fs_t, buf); return 0; } return 0; }
void do_connect(void) { struct sockaddr_in sin; struct hostent *hp; printf(" Get the server ip..."); if((hp = gethostbyname(nfos->server->hostname)) == NULL) { printf("%s", hstrerror(h_errno)); exit(EXIT_FAILURE); }else { printf(" OK!\n"); strncpy(nfos->server->ip, inet_ntoa(*(struct in_addr *)hp->h_addr), 16); } printf(BREAK); sin.sin_family = PF_INET; sin.sin_port = htons((unsigned int)atoi(nfos->server->port)); printf(" Convert server ip..."); if((signed int)(sin.sin_addr.s_addr = inet_addr(nfos->server->ip)) == -1) { printf(" failed!\n ERROR: inet_addr();: conversion failed!\n"); exit(EXIT_FAILURE); }else printf(" OK!\n"); printf(BREAK); printf(" Create socket..."); if((nfos->server->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) { printf(" failed!\n ERROR: socket();\n"); exit(EXIT_FAILURE); }else printf(" OK!\n"); printf(BREAK); printf(" Connect to server..."); if(connect(nfos->server->socket, (struct sockaddr *)&sin, sizeof(sin)) == -1) { printf(" failed!\n ERROR: connect();\n"); exit(EXIT_FAILURE); }else printf(" OK!\n"); printf(BREAK); printf(" Set nickname..."); irc_cmd("NICK %s", nfos->server->nickname); // next two lines are just for debugging //recv(nfos->server->socket, buffer_, 1024, 0); //printf(buffer_); irc_cmd("USER %s 0 0 :%s - %s", nfos->server->nickname, APP_NAME, APP_WWW); // next two lines are just for debugging //recv(nfos->server->socket, buffer_, 1024, 0); //printf(buffer_); printf(" OK!\n"); printf(BREAK); return; }