#!/usr/local/bin/pike // PikeBot v0.1 // (c)iMil // This software is covered by the GNU GPL string w_channel="#gcu"; string w_server; string w_nick; string w_user; string w_name; string w_passwd; int w_port; string pikebot_version="Pike Bot v0.1 by iMil"; string pikebot_info="*br0000*!"; string auths=""; int quit=0; int log=0; /* generic functions *********************************************************/ int regmatch(string str, string regxp) { object reg=Regexp("("+regxp+")"); if (reg->match(str)) return 1; else return 0; } /* irc functions *************************************************************/ // connect function object irc_connect(int port, string server, string nick, string user, string realname, void|string passwd) { // init some variables w_port=port; w_server=server; w_nick=nick; w_user=user; w_name=realname; w_passwd=passwd; string rep; string mask=user+"@localhost"; string regstr = "USER "+user+" "+mask+" "+server+" :"+realname+"\n\r"; string regnick = "NICK "+nick+"\n\r"; object irc = Stdio.File(); // connect to irc server if (!irc->connect(server, port)) { werror("Can't connect to "+server+"\n"); exit(1); } werror("Connected to "+server+"!!\n"); // send pass, user and nick irc->write("PASS "+passwd+"\n\r"); irc->write(regnick+regstr); werror(regnick); werror(regstr); // move to non_blocking mode irc->set_nonblocking(); while(!rep) rep = irc->read(); irc->set_blocking(); werror(rep+"\n"); object reg=Regexp("(.*:)([0-9].*[0-9])"); array tmp=reg->split(rep); irc->write("PONG :"+tmp[1]+"\n\r"); werror("PONG :"+tmp[1]+"\n"); return irc; } /* irc actions ***************************************************************/ void join(object fd, string channel) { fd->write("JOIN "+channel+"\n\r"); } void part(object fd, string channel) { fd->write("PART "+channel+"\n\r"); } void say(object fd, string dest, string txt) { fd->write("PRIVMSG "+dest+" :"+txt+"\n\r"); } void notice(object fd, string dest, string txt) { fd->write("NOTICE "+dest+" :"+txt+"\n\r"); } void nick(object fd, string newnick) { fd->write("NICK "+newnick+"\n\r"); } void quit_irc(object fd, string reason) { fd->write("QUIT :"+reason+"\n\r"); quit=1; } /* main loop *****************************************************************/ void main_loop(object fd) { string said; while(!quit) { // trick so this loop doen't load all CPU time string saidbl=fd->read(1); fd->set_nonblocking(); said=fd->read(); fd->set_blocking(); said=saidbl+said; if (said) { werror(said); if (regmatch(said, "[cC]losing [Ll]ink")) { fd->close(); exit(1); } if(regmatch(said, ":.*JOIN :.*")) { said=replace(said,"\n",""); said=replace(said,"\r",""); object reg=Regexp("(:)(.*)(!.*JOIN :)(.*)"); array tmp=reg->split(said); werror(tmp[1]+" "+tmp[3]+"\n"); on_join(fd,tmp[1],tmp[3]); } if (regmatch(said, "PING :.*")) { object reg=Regexp("(PING :)(.*)"); array tmp=reg->split(said); fd->write("PONG :"+tmp[1]+"\n\r"); werror("PONG :"+tmp[1]+"\n\r"); } // privmsg if (regmatch(said,":.*!.*PRIVMSG .* :.*")) { string cur_channel=w_channel; int query=1; // get text object reg=Regexp("(:.* :)(.*)"); string text=reg->split(said)[1]; // who talked reg=Regexp("(:)(.*)(!.*@.*)"); string who=reg->split(said)[1]; // get channel if ! query reg=Regexp("(:.*)(PRIVMSG #.* :)(.*)"); if (reg->match(said)) { cur_channel=(reg->split(said)[1] / " ")[1]; query=0; } if (query) { query=0; if (regmatch(text, ".VERSION.")) notice(fd,who,pikebot_version); else if (regmatch(text, ".USERINFO.")) notice(fd,who,pikebot_info); else if (regmatch(text, ".CLIENTINFO.")) notice(fd,who,pikebot_info); else if (regmatch(text, ".PING [0-9].*[0-9].")) notice(fd,who,"PONG"); else query=1; } action(fd, query, cur_channel, who, text); } } // end if(said) } //end while } // // End of generic code // /*****************************************************************************/ /* hardcoded case function ***************************************************/ /*****************************************************************************/ void on_join(object fd, string who, string channel) { int i,known=0; object file=Stdio.File(); file->open("/tmp/onjoin", "r"); string tmp=file->read(); file->close(); tmp=tmp+"EOF"; array list=tmp / "\n"; for(i=0;list[i]!="EOF";i++) if (regmatch(list[i],who)) { say(fd, channel, who+"! "+list[i][(strlen(who)+1)..]); known=1; }; if (!known) say(fd, channel, who+": "+list[random(i)]); } void action(object fd, int query, string cur_channel, string who, string text) { if (query && regmatch(text, "ident")) { text=replace(text, "ident ", ""); text=replace(text, "\r", ""); text=replace(text, "\n", ""); object file=Stdio.File(); file->open("/tmp/auth", "r"); string tmp=file->read(); file->close(); tmp = tmp+"EOF"; array list=tmp / "\n"; for (int i=0;list[i]!="EOF";i++) { array person=list[i] / " "; if (person[0] == who && person[1] == text) { notice(fd,who, "you are now authentified"); auths=auths+who; } } } else if (query) say(fd, w_channel, "Bon, y'a "+who+" qui me dit : "+text); // authentified functions if (regmatch(auths,who)) { if (regmatch(text, "!log")) if (!log) { log=1; say(fd, w_channel, "crOAAaaaaaa...\n\r"); } else { log=0; say(fd, w_channel, "pacrOAAAaaa...\n\r"); } if (log) { if (cur_channel != w_channel ) say(fd,w_channel,"("+cur_channel+") "+who+": "+text+"\n\r"); } if (regmatch(text, "!quit") && regmatch(auths, who)) { say(fd, w_channel,"bon bah je trace alors.."); quit_irc(fd, "pouet"); } if (regmatch (text, "!join")) { text=replace(text, "!join ", ""); say(fd, w_channel, "hop! je join "+text+"\n\r"); join(fd, text); } if (regmatch (text, "!part")) { text=replace(text, "!part ", ""); say(fd, w_channel, "hop! je degage de "+text+"\n\r"); part(fd, text); } if (regmatch(text, "!say")) { text=replace(text, "!say ", ""); array tmp=text / " "; if (sizeof(tmp) > 1) text = tmp[1..]*" "; say(fd, tmp[0], text+"\n\r"); } if (regmatch(text, "!nick")) { text=replace(text, "!nick ", ""); nick(fd, text); } if (regmatch(text, "!squat")) { text=replace(text, "!squat ", ""); say(fd, w_channel, "ok, je squatte sur "+text+"\n\r"); text=replace(text, "\n",""); text=replace(text, "\r",""); w_channel=text; } if (regmatch(text, "!addman")) { text=replace(text, "!addman ", ""); object file=Stdio.File(); file->open("/tmp/man.db", "cwa"); array tmp=text / " "; tmp[0] = tmp[0]+"%%"; text = tmp[0..] * " "; file->write(text); file->close(); say(fd, cur_channel, "manpage ajoutée. *brOOOps*"); } } // end authentified functions // public functions if (!query) { object file=Stdio.File(); file->open("/tmp/irclog", "cwa"); string tmp=Process.popen("date +%H:%M")+" | "+who+": "+text; tmp=replace(tmp,"\n",""); tmp=replace(tmp,"\r",""); tmp=tmp+"\n"; file->write(tmp); file->close(); } if (regmatch(text, "où")) for(int i=0;i<3;i++) say(fd, cur_channel, "DANS TON CUL"); if (regmatch(text, "grr")) for(int i=0;i<3;i++) say(fd, cur_channel, "*wouah*"); if (regmatch(text, "????")) say(fd, cur_channel, "kuwa?"); if (regmatch(text, "chien")) say(fd, cur_channel, "paf! le chien"); if (regmatch(text, "!matrix")) { string matrix="@#%$*&|/?=+^~"; string damatrix; for (int i=0;i<10;i++) { damatrix=" "*40; for (int x=0;x<40;x=x+2) { damatrix[x] = matrix[random(12)]; damatrix[x+1]=' '; } say(fd, cur_channel, damatrix); } } if (regmatch(text, "wuza")||regmatch(text, "pinpin")) { int i; object file=Stdio.File(); file->open("/tmp/callme", "r"); string tmp=file->read(); file->close(); tmp=tmp+"EOF"; array list=tmp / "\n"; for(i=0;list[i]!="EOF";i++); say(fd, cur_channel, who+": "+list[random(i)]); } if (regmatch(text, "!man")) { object file=Stdio.File(); file->open("/tmp/man.db","r"); string list=file->read(); file->close(); list = list+"EOF"; array tmp=list / "\n"; for (int i=0;tmp[i]!="EOF";i++) { array match = tmp[i] / "%%"; if (regmatch(text, match[0])) say(fd,cur_channel,match[0]+":"+match[1]); } } } // end action /* main function *************************************************************/ int main() { object fd = Stdio.File(); fd=irc_connect(6667,"irc.openprojects.net", "wuza|bot","wuza","iMil's Pike Bot used by kiwi"); join(fd, w_channel); say(fd, w_channel, "m0uh!! je suis wuzzzaa used by kiwi derivé de pinpin|pike|beta, encore plus relou, pour encore plus de plaisir"); main_loop(fd); return 0; }