From aed3f34c433d8408ff96fc368ac8b5891c7f9c0b Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 14 Sep 2015 10:22:55 -0700 Subject: [PATCH] Add optional modifiers to factoid variables Adlib list variables can now accept trailing modifier keywords prefixed with a colon. These can be chained together to combine their effects. :uc - uppercases the expansion :lc - lowercases the expansion :ucfirst - uppercases the first letter in the expansion :title - lowercases the expansion and then uppercases the first letter (effectively an alias for :lc:ucfirst) Examples: echo $colors:uc RED echo $colors:ucfirst Blue --- PBot/Factoids.pm | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/PBot/Factoids.pm b/PBot/Factoids.pm index 99ecca38..69b5d7a0 100644 --- a/PBot/Factoids.pm +++ b/PBot/Factoids.pm @@ -8,6 +8,9 @@ package PBot::Factoids; use warnings; use strict; +use feature 'switch'; +no if $] >= 5.018, warnings => "experimental::smartmatch"; + use HTML::Entities; use Time::HiRes qw(gettimeofday); use Carp (); @@ -348,11 +351,18 @@ sub find_factoid { sub expand_factoid_vars { my ($self, $from, $action) = @_; - while ($action =~ /(?find_factoid($from, $v, undef, 0, 1); next if not @factoids; + my ($var_chan, $var) = ($factoids[0]->[0], $factoids[0]->[1]); if ($self->{factoids}->hash->{$var_chan}->{$var}->{type} eq 'text') { @@ -364,7 +374,27 @@ sub expand_factoid_vars { } my $line = int(rand($#mylist + 1)); $mylist[$line] =~ s/"//g; - $action =~ s/\$$var/$mylist[$line]/; + + + foreach my $mod (split /:/, $modifier) { + given ($mod) { + when ('uc') { + $mylist[$line] = uc $mylist[$line]; + } + when ('lc') { + $mylist[$line] = lc $mylist[$line]; + } + when ('ucfirst') { + $mylist[$line] = ucfirst $mylist[$line]; + } + when ('title') { + $mylist[$line] = lc $mylist[$line]; + $mylist[$line] = ucfirst $mylist[$line]; + } + } + } + + $action =~ s/\$$var$modifier/$mylist[$line]/; } } @@ -641,6 +671,8 @@ sub interpreter { return "/msg $nick $ref_from$keyword is currently disabled."; } + $action = $self->expand_factoid_vars($from, $action); + $action =~ s/\$nick/$nick/g; $action =~ s/\$channel/$from/g; $action =~ s/\$randomnick/my $random = $self->{pbot}->{nicklist}->random_nick($from); $random ? $random : $nick/ge;