3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-23 12:29:27 +01:00

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:
   <pragma_> echo $colors:uc
   <candide> RED
   <pragma_> echo $colors:ucfirst
   <candide> Blue
This commit is contained in:
Pragmatic Software 2015-09-14 10:22:55 -07:00
parent d6319b27c5
commit aed3f34c43

View File

@ -8,6 +8,9 @@ package PBot::Factoids;
use warnings; use warnings;
use strict; use strict;
use feature 'switch';
no if $] >= 5.018, warnings => "experimental::smartmatch";
use HTML::Entities; use HTML::Entities;
use Time::HiRes qw(gettimeofday); use Time::HiRes qw(gettimeofday);
use Carp (); use Carp ();
@ -348,11 +351,18 @@ sub find_factoid {
sub expand_factoid_vars { sub expand_factoid_vars {
my ($self, $from, $action) = @_; my ($self, $from, $action) = @_;
while ($action =~ /(?<!\\)\$([a-zA-Z0-9_\-]+)/g) { while ($action =~ /(?<!\\)\$([a-zA-Z0-9_:\-]+)/g) {
my $v = $1; my $v = $1;
next if $v =~ m/^(nick|channel|randomnick)$/; # don't override special variables next if $v =~ m/^(nick|channel|randomnick)$/; # don't override special variables
my $modifier = '';
if ($v =~ s/(:.*)$//) {
$modifier = $1;
}
my @factoids = $self->find_factoid($from, $v, undef, 0, 1); my @factoids = $self->find_factoid($from, $v, undef, 0, 1);
next if not @factoids; next if not @factoids;
my ($var_chan, $var) = ($factoids[0]->[0], $factoids[0]->[1]); my ($var_chan, $var) = ($factoids[0]->[0], $factoids[0]->[1]);
if ($self->{factoids}->hash->{$var_chan}->{$var}->{type} eq 'text') { if ($self->{factoids}->hash->{$var_chan}->{$var}->{type} eq 'text') {
@ -364,7 +374,27 @@ sub expand_factoid_vars {
} }
my $line = int(rand($#mylist + 1)); my $line = int(rand($#mylist + 1));
$mylist[$line] =~ s/"//g; $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."; return "/msg $nick $ref_from$keyword is currently disabled.";
} }
$action = $self->expand_factoid_vars($from, $action);
$action =~ s/\$nick/$nick/g; $action =~ s/\$nick/$nick/g;
$action =~ s/\$channel/$from/g; $action =~ s/\$channel/$from/g;
$action =~ s/\$randomnick/my $random = $self->{pbot}->{nicklist}->random_nick($from); $random ? $random : $nick/ge; $action =~ s/\$randomnick/my $random = $self->{pbot}->{nicklist}->random_nick($from); $random ? $random : $nick/ge;