char* irc_get_nick(char *buf, char *raw) { char *data; char *seperateFrmCol; char *nick; if(buf == NULL) return buf; data = (char*)callocm(MAX_LEN, sizeof(char)); if(data == NULL) return NULL; xstrcpy(data, raw, MAX_LEN); seperateFrmCol = xstrtok(data, ":", NULL); if(seperateFrmCol == NULL) { freem(data); return NULL; } strcpy(data, seperateFrmCol); if(strchr(data, '!') == NULL) nick = xstrtok(data, " ", NULL); else nick = xstrtok(data, "!", NULL); if(nick == NULL) { freem(data); return NULL; } xstrcpy(buf, nick, MAX_NICKLEN); freem(data); return buf; }
void free_xion_memory(void) { unsigned int i = 0; struct handle_list *temp_hlist, *temp_hlist2; if(bot.admin_array != NULL) { for(i = 0;i < bot.admin_lines;i++) freem(bot.admin_array[i]); freem(bot.admin_array); } if(bot.admin_array_current != NULL) { for(i = 0;i < bot.admin_current_count;i++) freem(bot.admin_array_current[i]); freem(bot.admin_array_current); } queue_free_data(); user_free_data(); chan_free_data(); botcmd_free_data(); relay_free_data(); /* These need to be the last things freed. */ event_free_data(); temp_hlist = handle_first; while(temp_hlist != NULL) { temp_hlist2 = temp_hlist->next; freem(temp_hlist); if(temp_hlist2 == NULL) break; temp_hlist = temp_hlist2; } return ; }
char* irc_get_cmdtype(char *buf, char *raw) { char *data; char *type; int pos = 0; if(buf == NULL) return buf; data = (char*)callocm(MAX_LEN, sizeof(char)); if(data == NULL) return NULL; xstrcpy(data, raw, MAX_LEN); xstrtok(data, " ", &pos); type = xstrtok(data, " ", &pos); if(type == NULL) { freem(data); return NULL; } strcpy(buf, type); freem(data); return buf; }
char* irc_get_target(char *buf, char *raw) { char *data; char *target; int pos = 0; if(buf == NULL) return buf; data = (char*)callocm(MAX_LEN, sizeof(char)); if(data == NULL) return NULL; xstrcpy(data, raw, MAX_LEN); xstrtok(data, " ", &pos); xstrtok(data, " ", &pos); target = xstrtok(data, " ", &pos); if(target == NULL) { freem(data); return NULL; } if(*target == ':') target++; strcpy(buf, target); freem(data); return buf; }
char* irc_get_mask(char *buf, char *raw) { char *data; char *seperateFrmSp; char *mask; if(buf == NULL) return buf; data = (char*)callocm(MAX_LEN, sizeof(char)); if(data == NULL) return NULL; xstrcpy(data, raw, MAX_LEN); seperateFrmSp = xstrtok(data, " ", NULL); if(seperateFrmSp == NULL) { freem(data); return NULL; } xstrcpy(data, seperateFrmSp, MAX_LEN); mask = xstrtok(data, ":", NULL); if(mask == NULL) { freem(data); return NULL; } strcpy(buf, mask); freem(data); return buf; }
void _free_2d_array(void ***array, unsigned long elements) { unsigned long i; for(i = 0;i < elements;i++) freem(array[0][i]); freem(array[0]); return ; }
void multi_free(void *first, ...) { va_list ap; void *ptr; va_start(ap, first); while((ptr = va_arg(ap, void*)) != NULL) freem(ptr); va_end(ap); freem(first); return ; }
unsigned int irc_strip_ctrlcodes(char **bufp, const char *text) { char *buf; unsigned int text_len; *bufp = NULL; if(blankstr(text)) return 0; text_len = strlen(text)+1; buf = (char*)callocm(text_len, sizeof(char*)); if(buf == NULL) return 0; xstrcpy(buf, text, text_len); if(blankstr(strrtok(buf, ctrlcodes))) { freem(buf); return 0; } *bufp = buf; return strlen(buf); }
unsigned int irc_char_type(char c, unsigned int type) { /* Types 0 = ALPHA_CHARS 1 = NUMERIC_CHARS 2 = ACCESS_CHARS 3 = SPECIAL_CHARS */ char *match; unsigned int i = 0; switch(type) { case IS_TYPE_ALPHA: match = (char*)callocm(strlen(ALPHA_CHARS)+1, sizeof(char)); if(match == NULL) return 0; strcpy(match, ALPHA_CHARS); break; case IS_TYPE_NUMERIC: match = (char*)callocm(strlen(NUMERIC_CHARS)+1, sizeof(char)); if(match == NULL) return 0; strcpy(match, NUMERIC_CHARS); break; case IS_TYPE_ACCESS: match = (char*)callocm(strlen(ACCESS_CHARS)+1, sizeof(char)); if(match == NULL) return 0; strcpy(match, ACCESS_CHARS); break; case IS_TYPE_SPECIAL: match = (char*)callocm(strlen(SPECIAL_CHARS)+1, sizeof(char)); if(match == NULL) return 0; strcpy(match, SPECIAL_CHARS); break; } for(i = 0;i < strlen(match);i++) { if(c == match[i]) { freem(match); return 1; } } freem(match); return 0; }
char* config_line(char *buf, unsigned long line) { char *fbuf, *l; FILE *fptr; unsigned long fsize, i; int pos = 0; #if defined(PLATFORM_WINDOWS) fptr = fopen(bot.config, "r+t"); #else fptr = fopen(bot.config, "r+"); #endif if(!fptr) { make_error("Couldn't open config file."); return NULL; } fseek(fptr, 0, SEEK_END); fsize = ftell(fptr); if((fbuf = (char*)callocm((fsize+1), sizeof(char))) == NULL) { return NULL; } rewind(fptr); fread(fbuf, sizeof(char), fsize, fptr); fclose(fptr); if(line == 0) line++; if((l = xstrtok(fbuf, "\n", &pos)) == NULL) { freem(fbuf); return NULL; } for(i = 1;l != NULL;i++) { if(i == line) { xstrcpy(buf, l, CONF_MAX_ITEMLEN); freem(fbuf); return buf; } l = xstrtok(fbuf, "\n", &pos); } freem(fbuf); return NULL; }
char* irc_subconftok(char *buf, unsigned char citem, char *subname, unsigned int tok) { unsigned long i = 0, lcount = 0; char **lines = NULL; char *conftok = (char*)callocm(CONF_MAX_ITEMLEN, sizeof(char)); lcount = get_confitem(&lines, citem); for(i = 0;i < lcount;i++) { if(istrcmp(get_itemtok(conftok, lines[i], 2), subname)) { get_itemtok(conftok, lines[i], tok); strncpy(buf, conftok, CONF_MAX_ITEMLEN); freem(conftok); free_2d_array(lines, lcount); return buf; } } freem(conftok); free_2d_array(lines, lcount); return NULL; }
unsigned long get_confitemcount(unsigned char citem) { char *item = (char*)callocm(CONF_MAX_ITEMLEN, sizeof(char)); unsigned long x = 0, y = 0; for(x = 0;config_line(item, x) != NULL;x++) { if(item[0] == citem) y++; } freem(item); return y; }
char* get_itemtok(char *buf, char* item, unsigned int tok) { char *retval; char *temp = (char*)callocm(CONF_MAX_ITEMLEN, sizeof(char)); char *temp2 = (char*)callocm(CONF_MAX_ITEMLEN, sizeof(char)); unsigned int i = 0, x = 0; int pos = 0; xstrcpy(temp, item, (CONF_MAX_ITEMLEN-1)); strcat(temp, ":"); for(i = 0;i < strlen(temp);i++) { if(temp[i] == ':') { x++; if(x == 2) { sprintf(temp2,":;%s",temp+i); memmove(temp+(i-1),temp2,strlen(temp2)); x = 0; } } else { x = 0; } } if(tok == 0) tok++; retval = xstrtok(temp, ":", &pos); for(i = 1;retval != NULL;i++) { if(i == tok) { xstrcpy(buf, retval, CONF_MAX_ITEMLEN); freem(temp); freem(temp2); return buf; } retval = xstrtok(temp, ":", &pos); } freem(temp); freem(temp2); return NULL; }
char* irc_get_host(char *buf, char *raw) { char *data; char *seperateFrmSp; char *host; int pos = 0; if(buf == NULL) return buf; data = (char*)callocm(MAX_LEN, sizeof(char)); if(data == NULL) return NULL; xstrcpy(data, raw, MAX_LEN); xstrtok(data, " ", &pos); seperateFrmSp = xstrtok(data, " ", &pos); if(seperateFrmSp == NULL) { freem(data); return NULL; } xstrcpy(data, seperateFrmSp, MAX_LEN); pos = 0; xstrtok(data, "@", &pos); host = xstrtok(data, "@", &pos); if(host == NULL) { freem(data); return NULL; } strcpy(buf, host); freem(data); return buf; }
unsigned int user_del_userchan(struct chanlist *hChanList) { struct userNode *user; if(hChanList == NULL) return 0; user = hChanList->user; if(user == NULL) return 0; LL_DELNODE(hChanList, user->chanlist_first, user->chanlist_last) user->chan_count--; freem(hChanList); return 1; }
char* irc_get_servname(char *buf, char *raw) { char *servname; if(buf == NULL) return buf; if(blankstr(raw)) return NULL; servname = (char*)callocm(256, sizeof(char)); xstrcpy(servname, raw+1, 256); strcpy(buf, xstrtok(servname, " ", NULL)); freem(servname); return buf; }
void user_free_chanlist(struct userNode *user) { struct chanlist *temp_list, *temp_list2; temp_list = user->chanlist_last; while(temp_list != NULL) { temp_list2 = temp_list->prev; freem(temp_list); if(temp_list2 == NULL) break; temp_list2->next = NULL; temp_list = temp_list2; } user->chanlist_first = NULL; user->chanlist_last = NULL; user->chan_count = 0; return ; }
unsigned int user_deluser(struct userNode *user) { struct chanlist *hChanList; if(user == NULL) return 0; /* Remove the user from each channel's user list then recycle user's own channel list. */ for(hChanList = user->chanlist_first;hChanList != NULL;hChanList = hChanList->next) chan_del_chanuser(hChanList->chanuser_info); user_free_chanlist(user); LL_DELNODE(user, user_first, user_last) user_count--; freem(user); return 1; }
void user_free_data(void) { struct userNode *temp_user, *temp_user2; temp_user = user_last; while(temp_user != NULL) { temp_user2 = temp_user->prev; user_free_chanlist(temp_user); freem(temp_user); if(temp_user2 == NULL) break; temp_user2->next = NULL; temp_user = temp_user2; } user_first = NULL; user_last = NULL; user_count = 0; return ; }
char* irc_get_netname(char *buf, char *raw) { char *netname, *str; if(buf == NULL) return buf; if(strstr(raw, "NETWORK=") == NULL) return NULL; netname = (char*)callocm(33, sizeof(char)); str = strstr(raw, "NETWORK="); if(blankstr(str)) { *buf = 0; return buf; } xstrcpy(netname, str+8, 32); strcpy(buf, xstrtok(netname, " ", NULL)); freem(netname); return buf; }
char* mklower(char *buf, const char *str) { char *tlwr; unsigned int i = 0; if((buf == NULL) || (str == NULL)) return buf; if((tlwr = (char*)callocm(strlen(str)+1, sizeof(char))) == NULL) return NULL; strncpy(tlwr, str, strlen(str)+1); for(i = 0;i < (strlen(tlwr)+1);i++) tlwr[i] = tolower(str[i]); strcpy(buf, tlwr); freem(tlwr); return buf; }
unsigned long get_confitem(char ***buf, unsigned char citem) { char *item = (char*)callocm(CONF_MAX_ITEMLEN, sizeof(char)); unsigned long i = 0, n = 0, count = 0, itemlen = 0; count = get_confitemcount(citem); if(!count) return 0; *buf = (char**)mallocm(count*sizeof(char*)); for(i = 0;config_line(item, i) != NULL;i++) { if(item[0] == citem) { itemlen = strlen(item)+1; if(itemlen > CONF_MAX_ITEMLEN) itemlen = CONF_MAX_ITEMLEN; buf[0][n] = (char*)callocm(itemlen, sizeof(char)); xstrcpy(buf[0][n++], item, CONF_MAX_ITEMLEN); } } freem(item); return n; }
struct dccNode* dcc_connect(struct userNode *user, char *address, unsigned short port) { struct sockaddr_in addr; struct dccNode *dcc; unsigned long longip; #ifdef PLATFORM_WINDOWS WSADATA wsa; #endif dcc = (struct dccNode*)malloc(sizeof(struct dccNode)); if(dcc == NULL) { make_error("Failed to allocate space for dcc structure."); return NULL; } LL_ADDNODE(dcc, dcc_first, dcc_last); stats.dcc_count++; dcc->user = user; #ifdef PLATFORM_WINDOWS if(WSAStartup(0x0202, &wsa)) { WSACleanup(); make_error("Failed to initiat use of WS2_32.DLL for dcc connection."); return NULL; } if(wsa.wVersion != 0x0202) { WSACleanup(); make_error("Old version of winsock detected, update this pos."); return NULL; } #endif if((dcc->sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { #ifdef PLATFORM_WINDOWS WSACleanup(); #endif freem(dcc); make_error("Failed to create dcc socket."); return NULL; } addr.sin_family = AF_INET; addr.sin_port = htons(port); if((longip = atol(address))) addr.sin_addr.s_addr = htonl(longip); else addr.sin_addr.s_addr = inet_addr(address); memset(&addr.sin_zero, 0, 8); xstrcpy(dcc->ip, inet_ntoa(addr.sin_addr), 256); if(connect(dcc->sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == -1) { #ifdef PLATFORM_WINDOWS WSACleanup(); #endif freem(dcc); make_error("Failed to connect to specified dcc ip or port."); return NULL; } return dcc; }
void invoke_prog(char* prog, char* args) { size_t l=strlen(prog); char* buf=allocmem(l+strlen(args)+2); strcpy(buf,prog); buf[l]=' '; strcpy(&buf[l+1],args); system(buf); freem(buf); }
/* This function should not be called more than once */ unsigned int init(void) { char **xlines, **clines; char *conftok = (char*)callocm(CONF_MAX_ITEMLEN, sizeof(char)); unsigned int i = 0, xcount = 0, ccount = 0, acount = 0; /* Don't get this confused with the word "account" */ extern q_maxbytes, q_maxqueue; /* For making sure when these variables are tested, they are set to NULL first. */ init_handle(&q_first); init_handle(&q_last); init_handle(&event_first); init_handle(&event_last); init_handle(&chan_first); init_handle(&chan_last); init_handle(&user_first); init_handle(&user_last); init_handle(&me); init_handle(&bc_first); init_handle(&bc_last); init_handle(&dcc_first); init_handle(&dcc_last); eventloop_running = 0; bot.cid = 0; bot.connected = 0; xstrcpy(bot.config, XION_CONFIG, 260); bot.admin_current_count = 0; bot.current_try = 1; q_first = NULL; /*X:Line*/ xcount = get_confitem(&xlines, 'X'); if(!xcount) { make_error("No X:lines in configuration file."); freem(conftok); return 0; } /*nickname*/ xstrcpy(bot.nick, get_itemtok(conftok, xlines[0], 2), 32); /*altnick*/ xstrcpy(bot.altnick, get_itemtok(conftok, xlines[0], 3), 32); /*username*/ xstrcpy(bot.username, get_itemtok(conftok, xlines[0], 4), 12); /*fullname*/ xstrcpy(bot.info, get_itemtok(conftok, xlines[0], 5), 256); xstrcpy(bot.info, conf_replace_alias(conftok), 256); /*C:Line*/ ccount = get_confitem(&clines, 'C'); if(!ccount) { make_error("No C:lines in configuration file."); freem(conftok); free_2d_array(xlines, xcount); return 0; } /*servaddr*/ xstrcpy(bot.servaddr, get_itemtok(conftok, clines[0], 2), 256); /*servport*/ bot.servport = atoi(get_itemtok(conftok, clines[0], 3)); /*servpass*/ xstrcpy(bot.servpass, get_itemtok(conftok, clines[0], 4), 256); /*S:Lines*/ /*maxretry*/ irc_subconftok(conftok, 'S', "maxretry", 3); bot.maxretry = atoi(conftok); /*pingtimeout*/ irc_subconftok(conftok, 'S', "pingtimeout", 3); bot.ping_timeout = atoi(conftok); /*freshlog*/ irc_subconftok(conftok, 'S', "freshlog", 3); bot.fresh_log = (atoi(conftok) ? 1 : 0); /*antiflood*/ irc_subconftok(conftok, 'S', "antiflood", 5); bot.floodcheck = (atoi(conftok) ? 1 : 0); irc_subconftok(conftok, 'S', "antiflood", 3); q_maxbytes = (unsigned int)atoi(conftok); irc_subconftok(conftok, 'S', "antiflood", 4); q_maxqueue = (unsigned int)atoi(conftok); /*ctrigger*/ irc_subconftok(conftok, 'S', "ctrigger", 3); conf_replace_alias(conftok); bot.ctrigger = conftok[0]; /*ptrigger*/ irc_subconftok(conftok, 'S', "ptrigger", 3); conf_replace_alias(conftok); bot.ptrigger = conftok[0]; /*A:Lines*/ if(bot.admin_array != NULL) free_2d_array(bot.admin_array, bot.admin_lines); acount = get_confitem(&bot.admin_array, 'A'); bot.admin_lines = acount; if(acount) { for(i = 0;i < bot.admin_lines;i++) { get_itemtok(conftok, bot.admin_array[i], 3); if((istrcmp(conftok, ";")) || (istrcmp(conftok, "*"))) adm_loginuser(get_itemtok(conftok, bot.admin_array[i], 2), get_itemtok(conftok, bot.admin_array[i], 4), atoi(get_itemtok(conftok, bot.admin_array[i], 5))); } } /* URL Module: mod-weburlcache.c */ urlmod_init(); freem(conftok); free_2d_array(xlines, xcount); free_2d_array(clines, ccount); event_call(EVENT_INIT, 0); return 1; }
unsigned int make_argument_array(char ***bufp, const char *str) { unsigned int x = 0, y = 0, z = 0, count = 0, length; char *temp, *backptr; char **argv; if(blankstr(str)) return 0; length = strlen(str); while(str[x++] == ' ') z++; if(z >= length) return 0; for(;x < length;x++) { if((str[x] == ' ') && (str[x+1] != ' ') && (str[x+1] != 0)) count++; } count++; argv = (char**)mallocm(count*sizeof(char*)); if(argv == NULL) { make_warning("Couldn't allocate space needed for make_argument_array()"); return 0; } temp = (char*)callocm(length+1, sizeof(char)); if(temp == NULL) { freem(argv); return 0; } backptr = temp; xstrcpy(temp, str, length+1); temp += z; for(x = z, count = 0;str[x];count++) { if(x > z) x++; temp += y; for(y = 0;str[x];x++, y++) { if(str[x] == ' ') { if((str[x+1] == ' ') || (str[x+1] == 0)) temp[y] = 0; else break; } } temp[y] = 0; argv[count] = (char*)callocm(strlen(temp)+1, sizeof(char)); if(argv[count] == NULL) { make_warning("Couldn't allocate space needed for make_argument_array()"); freem(argv); freem(temp); return 0; } xstrcpy(argv[count], temp, y+1); y++; } *bufp = argv; freem(backptr); return count; }
unsigned int rehashconfig(void) { char **xlines; char *conftok = (char*)callocm(CONF_MAX_ITEMLEN, sizeof(char)); unsigned int i = 0, xcount = 0, acount = 0; /* Don't get this confused with the word "account" */ extern q_maxbytes; /*X:Line*/ xcount = get_confitem(&xlines, 'X'); if(!xcount) { make_error("No X:lines in configuration file."); freem(conftok); return 0; } /*nickname*/ xstrcpy(bot.nick, get_itemtok(conftok, xlines[0], 2), 32); if(!istrcmp(bot.current_nick, bot.nick)) irc_nick(bot.nick); /*altnick*/ xstrcpy(bot.altnick, get_itemtok(conftok, xlines[0], 3), 32); /*S:Lines*/ /*maxretry*/ irc_subconftok(conftok, 'S', "maxretry", 3); bot.maxretry = atoi(conftok); /*pingtimeout*/ irc_subconftok(conftok, 'S', "pingtimeout", 3); bot.ping_timeout = atoi(conftok); /*antiflood*/ irc_subconftok(conftok, 'S', "antiflood", 5); bot.floodcheck = (atoi(conftok) ? 1 : 0); irc_subconftok(conftok, 'S', "antiflood", 3); q_maxbytes = (unsigned int)atoi(conftok); /*ctrigger*/ irc_subconftok(conftok, 'S', "ctrigger", 3); conf_replace_alias(conftok); bot.ctrigger = conftok[0]; /*ptrigger*/ irc_subconftok(conftok, 'S', "ptrigger", 3); conf_replace_alias(conftok); bot.ptrigger = conftok[0]; /*A:Lines*/ if(bot.admin_array != NULL) free_2d_array(bot.admin_array, bot.admin_lines); acount = get_confitem(&bot.admin_array, 'A'); bot.admin_lines = acount; if(acount) { for(i = 0;i < bot.admin_lines;i++) { get_itemtok(conftok, bot.admin_array[i], 3); if((istrcmp(conftok, ";")) || (istrcmp(conftok, "*"))) adm_loginuser(get_itemtok(conftok, bot.admin_array[i], 2), get_itemtok(conftok, bot.admin_array[i], 4), atoi(get_itemtok(conftok, bot.admin_array[i], 5))); } } freem(conftok); free_2d_array(xlines, xcount); event_call(EVENT_REHASH, 0); return 1; }