/* Bot Net Example file (c) Christophe CALMEJANE - 1999'00 aka Ze KiLleR / SkyTech */ #include "../makelib/botnet.h" #include #include #include #include #include #include #include #define BOT_NICKNAME "logger" #define BOT_USERNAME "nobody" #define BOT_REALNAME "Logger Bot" #define BOT_CHANNEL "#bottest" #define LOGGERRC ".loggerrc" #define MY_PASS "botnet" #define DCC_WAITING_FOR_PASS 1 #define DCC_PASS_ACCEPTED 2 int lprintf(char *str, ...) { va_list va; int i; va_start(va, str); i = vprintf(str, va); va_end(va); fflush(stdout); return i; } int Val; typedef struct _ChannelInfo { char *channel; FILE *file; } ChannelInfo; ChannelInfo **ci = 0; int nci = 0; ChannelInfo *newChannelInfo(void) { ChannelInfo *newci; newci = (ChannelInfo*)malloc(sizeof(ChannelInfo)); return newci; } void freeChannelInfo(ChannelInfo *ci) { if(!ci) return; if(ci->channel) { free(ci->channel); ci->channel = 0; } if(ci->file) { fclose(ci->file); ci->file = 0; } free(ci); } int getChannelIdx(const char *channel) { int i; for(i=0; ichannel)) { return i; } } return -1; } ChannelInfo *getChannel(const char *channel) { int i = getChannelIdx(channel); return i == -1 ? 0 : ci[i]; } void writeConfiguration(void) { FILE *conf = fopen(LOGGERRC, "wt"); int i; if(!conf) { lprintf("ERROR saving configuration\n"); return; } for(i=0; ichannel); } fflush(conf); fclose(conf); } void addChannel(const char *channel) { ChannelInfo *newci; char logfilename[100]; if(getChannelIdx(channel) != -1) return; sprintf(logfilename, "log.%s", channel); newci = newChannelInfo(); newci->channel = strdup(channel); newci->file = fopen(logfilename, "at"); if(!ci) ci = (ChannelInfo**)malloc(1); ci = (ChannelInfo**)realloc(ci, ++nci * sizeof(ChannelInfo*)); ci[nci-1] = newci; lprintf("Started logging channel '%s' into file '%s'\n", channel, logfilename); writeConfiguration(); } void removeChannel(const char *channel) { int i = getChannelIdx(channel); if(i == -1) return; lprintf("Stopped logging for channel '%s'\n", channel); freeChannelInfo(ci[i]); memmove(&ci[i], &ci[i+1], nci - (i+1)); nci--; writeConfiguration(); } char *chomp(char *str) { char *pe = str + strlen(str) - 1; while(pe > str && (*pe == '\n' || *pe == '\r')) pe--; *++pe = 0; return str; } /*******************************************************************/ void ProcOnConnected(BN_PInfo I,const char HostName[]) { lprintf("Event Connected : (%s)\n",HostName); BN_EnableFloodProtection(I,100,1000,200); BN_Register(I,BOT_NICKNAME,BOT_USERNAME,BOT_REALNAME); } void ProcOnRegistered(BN_PInfo I) { FILE *conf; char str[256]; int i=0; lprintf("Event Registered\n"); lprintf("Reading configuration...\n"); conf = fopen(".loggerrc", "rt"); if(!conf) { lprintf("No configuration found, using defaults\n"); } else { while(fgets(str, 256, conf)) { chomp(str); BN_SendJoinMessage(I, str, NULL); i++; } fclose(conf); lprintf("Configuration read.\n"); } if(!i) { BN_SendJoinMessage(I,BOT_CHANNEL,NULL); } // BN_SendMessage(I,BN_MakeMessage(NULL,"MODE",BOT_CHANNEL),BN_LOW_PRIORITY); // BN_SendMessage(I,BN_MakeMessage(NULL,"LIST",""),BN_LOW_PRIORITY); Val++; // Modify the variable, main's Val will only be modified if same thread } void ProcOnError(BN_PInfo I,int err) { lprintf("Event Error : (%d)\n",err); } void ProcOnStatus(BN_PInfo I,const char Msg[],int Code) { lprintf("Event Status : %s\n",Msg); if(strstr(Msg, "Nickname")) { lprintf("(retrying in 1 secs...)\n"); sleep(1); BN_Register(I,BOT_NICKNAME,BOT_USERNAME,BOT_REALNAME); } } void ProcOnJoinChannel(BN_PInfo I,const char Channel[]) { lprintf("Channel %s joined.\n", Channel); addChannel(Channel); } void ProcOnChannelTalk(BN_PInfo I,const char Channel[],const char Who[],const char Msg[]) { ChannelInfo *curci = getChannel(Channel); lprintf("%ld T %s %s %s\n",time(0),Channel,Who,Msg); if(curci) { fprintf(curci->file, "%ld T %s %s %s\n",time(0),Channel,Who,Msg); fflush(curci->file); } } void ProcOnAction(BN_PInfo I,const char Channel[],const char Who[],const char Msg[]) { ChannelInfo *curci = getChannel(Channel); lprintf("%ld A %s %s %s\n",time(0),Channel,Who,Msg); if(curci) { fprintf(curci->file, "%ld A %s %s %s\n",time(0),Channel,Who,Msg); fflush(curci->file); } } void ProcOnPrivateTalk(BN_PInfo I,const char Who[],const char Whom[],const char Msg[]) { lprintf("%s -> %s %s\n",Who,Whom,Msg); if(!strcmp(Whom, BOT_NICKNAME)) { // Don't understand messages... char str[5000]; int i; lprintf("Replying to %s..\n", Who); BN_SendPrivateMessage(I, Who, "Usage: '/invite " BOT_NICKNAME "' to enable logging for the current channel and '/kick #channel " BOT_NICKNAME "' to disable logging."); strcpy(str, "Logging enabled for channel(s) "); for(i=0; ichannel); strcat(str, ", "); } strcpy(&str[strlen(str)-2], "."); BN_SendPrivateMessage(I, Who, str); } } void ProcOnNotice(BN_PInfo I,const char Who[],const char Whom[],const char Msg[]) { lprintf("Event Notice: %s -> %s %s\n",Who,Whom,Msg); } void ProcOnNick(BN_PInfo I,const char Who[],const char Msg[]) { lprintf("Nick changed: %s -> %s\n",Who,Msg); } void ProcOnJoin(BN_PInfo I,const char Channel[],const char Who[]) { lprintf("%s : %s joined\n",Channel, Who); } void ProcOnPart(BN_PInfo I,const char Channel[],const char Who[], const char Msg[]) { lprintf("%s : %s left\n",Channel, Who); } void ProcOnQuit(BN_PInfo I,const char Who[], const char Msg[]) { lprintf("%s quit: %s\n", Who, Msg); } void ProcOnMode(BN_PInfo I,const char Channel[],const char Who[],const char Msg[]) { lprintf("Mode for %s by %s : %s\n",Channel,Who,Msg); } void ProcOnModeIs(BN_PInfo I,const char Channel[],const char Msg[]) { lprintf("Mode for %s : %s\n",Channel,Msg); } void ProcOnTopic(BN_PInfo I,const char Channel[],const char Who[],const char Msg[]) { lprintf("Topic for %s has been changed by %s to %s\n",Channel,Who,Msg); } void ProcOnTopicIs(BN_PInfo I,const char Channel[],const char Msg[]) { lprintf("Topic for %s is %s\n",Channel,Msg); } void ProcOnTopicSetBy(BN_PInfo I,const char Channel[],const char Msg[],const char Time[]) { lprintf("Topic for %s was been changed to %s at %s\n",Channel, Msg, Time); } void ProcOnList(BN_PInfo I,const char *Channels[],const char *Counts[],const char *Topics[],const int Count) { int i; for(i=0;i