/* Bot Net Example file (c) Christophe CALMEJANE - 1999'00 aka Ze KiLleR / SkyTech */ /* ircprint by Jonas Berlin */ #include "../makelib/botnet.h" #include #include #include #include #include #include #include #define BOT_USERNAME "nobody" char *BOT_NICKNAME = "ircprint"; char *BOT_REALNAME = "Irc print v1.1"; char *BOT_CHANNEL = "#ircprinttest"; char *BOT_SERVER = "irc.cs.hut.fi"; // #define SUBPROCESS 1 /*******************************************************************/ int lprintf(const char *str, ...) { va_list va; int i; va_start(va, str); i = vprintf(str, va); va_end(va); fflush(stdout); return i; } char *chomp(char *str) { char *pe = str + strlen(str) - 1; while(pe > str && (*pe == '\n' || *pe == '\r')) pe--; *++pe = 0; return str; } /*******************************************************************/ // produce the message we want to send #ifdef SUBPROCESS char *getMessage() { int pid; int output[2]; char *buf; char *str; int len = 0; int curlen; if(pipe(output)) { lprintf("Error initializing pipe. exiting"); exit(1); } if(!(pid = fork())) { close(0); close(1); dup2(output[1], 1); close(output[0]); close(output[1]); execl("/usr/local/src/botnet-1.6.0/example/food.pl", "/usr/local/src/botnet-1.6.0/example/food.pl", NULL); exit(1); } close(output[1]); buf = malloc(4096); str = malloc(1); *str = 0; while((curlen = read(output[0], buf, 4096)) > 0) { str = realloc(str, len + curlen + 1); memcpy(str + len, buf, curlen); len += curlen; str[len] = 0; } free(buf); waitpid(pid, 0, 0); return str; } #else char *getMessage() { char *buf; char *str; int len = 0; int curlen; buf = malloc(4096); str = malloc(1); *str = 0; while((curlen = read(0, buf, 4096)) > 0) { str = realloc(str, len + curlen + 1); memcpy(str + len, buf, curlen); len += curlen; str[len] = 0; } free(buf); return str; } #endif /*******************************************************************/ 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) { char *p; lprintf("Event Registered\n"); p = strtok(getMessage(), "\n"); while(p) { BN_SendChannelMessage(I, BOT_CHANNEL, p); sleep(1); p = strtok(0, "\n"); } BN_SendQuitMessage(I, "Dismissed.."); } 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 ProcOnNotice(BN_PInfo I,const char Who[],const char Whom[],const char Msg[]) { lprintf("Event Notice: %s -> %s %s\n",Who,Whom,Msg); } void ProcOnDisconnected(BN_PInfo I,const char Msg[]) { lprintf("Event Disconnected : %s\n",Msg); exit(1); } void ProcOnUnknown(BN_PInfo I,const char Who[],const char Command[],const char Msg[]) { lprintf("Event Unknown from %s : %s %s\n",Who,Command,Msg); } int main(int nArgs, char **Args) { BN_TInfo Info; pid_t Res; if(nArgs < 4) { fprintf(stderr, "Usage: %s nick realname channel [server]\n", Args[0]); fprintf(stderr, "The program outputs standard input to the selected channel using the\nselected nick. Server is %s by default.\n", BOT_SERVER); return 1; } BOT_NICKNAME = Args[1]; BOT_REALNAME = Args[2]; BOT_CHANNEL = Args[3]; if(nArgs > 4) { BOT_SERVER = Args[4]; } lprintf("IrcPrint v1.1 by Jonas Berlin \n%s\n",BN_GetCopyright()); memset(&Info,0,sizeof(Info)); Info.CB.OnConnected = ProcOnConnected; Info.CB.OnRegistered = ProcOnRegistered; Info.CB.OnError = ProcOnError; Info.CB.OnStatus = ProcOnStatus; Info.CB.OnNotice = ProcOnNotice; Info.CB.OnDisconnected = ProcOnDisconnected; Info.CB.OnUnknown = ProcOnUnknown; #ifdef DEBUG while(BN_Connect(&Info,BOT_SERVER,6667,0) != true) #else while(BN_Connect(&Info,BOT_SERVER,6667,PROCESS_NEW_THREAD) != true) #endif { lprintf("Disconnected.\n"); sleep(10); lprintf("Reconnecting...\n"); } do { Res = waitpid(-1,NULL,WNOHANG); sleep(5); } while(Res <= 0); return 0; }