From: Andres S. <di...@us...> - 2005-04-09 16:53:30
|
Update of /cvsroot/tuxaator/tuxaator/Plugins/Talk In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32351/Plugins/Talk Modified Files: Plugin.pm Log Message: generalized random string generation Index: Plugin.pm =================================================================== RCS file: /cvsroot/tuxaator/tuxaator/Plugins/Talk/Plugin.pm,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** Plugin.pm 5 Apr 2005 19:52:59 -0000 1.9 --- Plugin.pm 9 Apr 2005 16:53:20 -0000 1.10 *************** *** 5,12 **** --- 5,16 ---- use Data::Dumper; + # Random-path sayings + + # Picks and returns a random element of the supplied list sub random_of (@) { $_[int rand @_] } + # Picks a random path from a {...|...} markuped string sub random_path ($) { my ($set) = @_; *************** *** 15,18 **** --- 19,44 ---- } + # Generates a random string. Stringpath specifiers are taken from + # the named file; ${foo} in these paths are replaced according to %subst. + sub make_random_string ($%) { + my ($filename, %subst) = @_; + + # Read the lines + my $fh; + open $fh, "<$filename" or die "$filename: open: $!"; + my @lines = <$fh>; + close $fh; + chomp for @lines; + + # Pick a random line and a random path from it + my $result = random_path random_of @lines; + + # Substitute + $result =~ s/(\${([\w-]+)})/exists $subst{$2} ? $subst{$2} : $1/ge; + + # Return + return $result; + } + sub new { my $class = shift; *************** *** 103,121 **** my $event = pop @_; my $msg = join " " => @_; - my $res = "1) $msg\n"; - $res .= "2) ...\n"; # plaanid.txt is in the root directory of the bot, so you can add/edit/remove plans # without needing to restart the bot ! open F,"<plaanid.txt" or die "cannot open: $!"; ! my @lines = <F>; ! close F; ! chomp for @lines; ! my $plan = random_path random_of @lines; ! $plan =~ s/\${msg}/$msg/g; ! $plan =~ s/\${user}/$event->nick/ge; ! $res .= "3) $plan\n"; # answer goes to the channel, otherwise it doesnt make too much sense $event->to(@{$event->to}[0]); ! $self->{'core'}->queue($event,$res); } --- 129,142 ---- my $event = pop @_; my $msg = join " " => @_; # plaanid.txt is in the root directory of the bot, so you can add/edit/remove plans # without needing to restart the bot ! my $finale = make_random_string('plaanid.txt', ! msg => $msg, user => $event->nick); ! my $message = "1) $msg\n". ! "2) ...\n". ! "3) $finale\n"; # answer goes to the channel, otherwise it doesnt make too much sense $event->to(@{$event->to}[0]); ! $self->{'core'}->queue($event, $message); } |