use FileHandle; use IPC::Open2; $MEGAHAL_PATH="./megahal"; %nickmap=(teknovore=>tek, j0efu=>fu, joefu=>fu, unslider=>uns, ams=>crab); # when we get a message, queue up a routine to send the megahal response to the # proper channel, then send it to the megahal process sub hook_gotpublic { my $channel=lc(shift); my $message=shift; my $thiswho=$who; # print "gotpublic $channel m $message\n"; if($message=~/^#clearq/) { undef @torespond; return; } $message=~s/(.*),\s*[Mm][Ee][Gg]\S*$/meg: $1/; if($message=~/^meg\S*[:,]\s+.+/i) { $dirtime=time; push @torespond, sub { # when addressed, respond on the channel to the addresser my $resptext=shift; my $respnick=$thiswho; # NB: closure my $respchan=$channel; # NB: closure if(defined($nickmap{lc($respnick)})) { $respnick=$nickmap{lc($respnick)}; } &msg($respchan,"$respnick: $resptext"); } } elsif($channel=~/megan/) { push @torespond, sub { # respond to all #megan messages &msg('#megan',$_[0]); } } else { push @torespond, sub { # normally respond to 3% of #penguin messages, but if the channel # has been idle for a half hour, respond to 28%, and if we # have been addressed directly in the last 30 seconds, add # another 15% $oldpengtime=$pengtime; $pengtime=time; if( (($pengtime-$oldpengtime>60*30) && rand(4)<1) || (rand(100)<1) || (($pengtime-$dirtime<30) && rand(100)<15) ) { &msg($channel,$_[0]); # NB $channel is closure } } } $message=~s/^\S+://; print TO_MEGAHAL "$message\n"; } # when we get a privmsg, queue up a response to the sender, and send the # megahal process the message sub hook_gotmsg { my $message=shift; my $thiswho=$who; push @torespond, sub { &msg($thiswho,$_[0]); }; print TO_MEGAHAL "$message\n"; } # when we get an action, see if someone high-fived us, if so then # high-five back. sub hook_gotaction { my $chan=shift; my $msg=shift; return if($chan!~/^#/); if($msg=~/high-fives meg/i) { &describe($chan,"high-fives $who"); } } # when we get a response from the megahal process, send it as appropriate sub sel_gotmegan { chomp(my $resp=shift); my $func; # print "goitmegan $resp\n"; # consider status messages to be spurious if($resp!~/^\([a-z]/) { $func=shift(@torespond); } if(!defined($func)) { &msg('#megan',"[spurious] $resp"); } else { &{$func}($resp); } } $megahalpid=open2(\*FROM_MEGAHAL,\*TO_MEGAHAL,$MEGAHAL_PATH); #select((select(TO_MEGAHAL), $| = 1)[0]); &addsel('FROM_MEGAHAL','gotmegan',1); &addhook('public','gotpublic'); &addhook('msg','gotmsg'); &addhook('action','gotaction');