#include #include "bot.h" struct tree_t *nick_stats; struct tree_t *chan_stats; struct stats_t *mk_stats(hash_t hash, time_t current_time) { struct stats_t *stats; stats = malloc(sizeof(struct stats_t)); stats->hash = hash; stats->last_msg = current_time; stats->msgs = 0; return stats; } void update_stats(char *nick, char *channel, char *line) { hash_t h_n = hash(nick); hash_t h_c = hash(channel); time_t current_time = time(NULL); struct tree_t *c_node; struct stats_t *stats; fprintf(stderr,"Start update_stats\n"); /* Update Nick Stats */ c_node = tree_find(nick_stats, h_n); /* find nick in nick tree */ fprintf(stderr,"1"); if (c_node == NULL) { if (nick_stats == NULL) { nick_stats = tree_init(nick); c_node = nick_stats; fprintf(stderr,"2"); } else { c_node = tree_insert(nick_stats, nick); fprintf(stderr,"3"); } c_node->other = mk_stats(h_n,current_time); if(c_node->other == NULL) { fprintf(stderr,"c_node->other == NULL :\\\n"); } } fprintf(stderr,"4"); stats = (struct stats_t *) c_node->other; stats->msgs += 1; stats->total_time += current_time - stats->last_msg; stats->last_msg = current_time; fprintf(stderr,"5"); /* Update Channel Stats */ if ((*channel) == '#') { fprintf(stderr,"6"); /* Only is a real channel */ c_node = tree_find(chan_stats, h_c); fprintf(stderr,"7"); if (c_node == NULL) { if (chan_stats == NULL) { chan_stats = tree_init(channel); c_node = chan_stats; fprintf(stderr,"8"); } else { c_node = tree_insert(chan_stats,channel); fprintf(stderr,"9"); } c_node->other = mk_stats(h_c,current_time); } fprintf(stderr,"A"); stats = (struct stats_t *) c_node->other; stats->msgs += 1; stats->total_time += current_time - stats->last_msg; stats->last_msg = current_time; } fprintf(stderr,"\nEnd update_stats\n"); } void output_stats(int s, char *channel, char *data) { char *fword = word(data,1); char buffer[BUFSIZE]; struct tree_t *c_node; struct stats_t *stats; double avg_time; time_t current_time = time(NULL); if((*data) == '#') { /* Channel Stats */ c_node = chan_stats; } else { /* Nick Stats */ c_node = nick_stats; } c_node = tree_find(c_node,hash(fword)); if (c_node != NULL) { stats = (struct stats_t *) c_node->other; avg_time = stats->total_time / stats->msgs; sprintf(buffer,"---Stats for %s---",fword); speak(s,channel,buffer); sprintf(buffer,"Messages: %d.",stats->msgs); speak(s,channel,buffer); sprintf(buffer,"Average time between messages %.2fs.",avg_time); speak(s,channel,buffer); sprintf(buffer,"Last Message: %s.",ctime(&stats->last_msg)); speak(s,channel,buffer); } else { sprintf(buffer,"Sorry I have no stats for %s",fword); speak(s,channel,buffer); } free(fword); } void move_nick(char *nick_old, char *nick_new) {}