3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 22:09:26 +01:00
pbot/Plugins/FuncBuiltins.pm

120 lines
2.9 KiB
Perl
Raw Normal View History

2020-02-14 07:37:09 +01:00
# File: FuncBuiltins.pm
#
# Purpose: Registers the basic built-in Functions
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
package Plugins::FuncBuiltins;
use parent 'Plugins::Plugin';
2021-06-19 06:23:34 +02:00
use PBot::Imports;
2020-02-14 07:37:09 +01:00
sub initialize {
2020-02-15 23:38:32 +01:00
my ($self, %conf) = @_;
$self->{pbot}->{functions}->register(
'title',
{
desc => 'Title-cases text',
usage => 'title <text>',
subref => sub { $self->func_title(@_) }
}
);
$self->{pbot}->{functions}->register(
'ucfirst',
{
desc => 'Uppercases first character',
usage => 'ucfirst <text>',
subref => sub { $self->func_ucfirst(@_) }
}
);
$self->{pbot}->{functions}->register(
'uc',
{
desc => 'Uppercases all characters',
usage => 'uc <text>',
subref => sub { $self->func_uc(@_) }
}
);
$self->{pbot}->{functions}->register(
'lc',
{
desc => 'Lowercases all characters',
usage => 'lc <text>',
subref => sub { $self->func_lc(@_) }
}
);
$self->{pbot}->{functions}->register(
'unquote',
{
desc => 'removes unescaped surrounding quotes and strips escapes from escaped quotes',
usage => 'unquote <text>',
subref => sub { $self->func_unquote(@_) }
}
);
$self->{pbot}->{functions}->register(
'uri_escape',
{
desc => 'percent-encode unsafe URI characters',
usage => 'uri_escape <text>',
subref => sub { $self->func_uri_escape(@_) }
}
);
2020-02-14 07:37:09 +01:00
}
sub unload {
2020-02-15 23:38:32 +01:00
my $self = shift;
$self->{pbot}->{functions}->unregister('title');
$self->{pbot}->{functions}->unregister('ucfirst');
$self->{pbot}->{functions}->unregister('uc');
$self->{pbot}->{functions}->unregister('lc');
$self->{pbot}->{functions}->unregister('unquote');
$self->{pbot}->{functions}->unregister('uri_escape');
2020-02-14 07:37:09 +01:00
}
sub func_unquote {
2020-02-15 23:38:32 +01:00
my $self = shift;
my $text = "@_";
$text =~ s/^"(.*?)(?<!\\)"$/$1/ || $text =~ s/^'(.*?)(?<!\\)'$/$1/;
$text =~ s/(?<!\\)\\'/'/g;
$text =~ s/(?<!\\)\\"/"/g;
return $text;
2020-02-14 07:37:09 +01:00
}
sub func_title {
2020-02-15 23:38:32 +01:00
my $self = shift;
my $text = "@_";
$text = ucfirst lc $text;
$text =~ s/ (\w)/' ' . uc $1/ge;
return $text;
2020-02-14 07:37:09 +01:00
}
sub func_ucfirst {
2020-02-15 23:38:32 +01:00
my $self = shift;
my $text = "@_";
return ucfirst $text;
2020-02-14 07:37:09 +01:00
}
sub func_uc {
2020-02-15 23:38:32 +01:00
my $self = shift;
my $text = "@_";
return uc $text;
2020-02-14 07:37:09 +01:00
}
sub func_lc {
2020-02-15 23:38:32 +01:00
my $self = shift;
my $text = "@_";
return lc $text;
2020-02-14 07:37:09 +01:00
}
2020-02-14 08:22:00 +01:00
use URI::Escape qw/uri_escape_utf8/;
2020-02-15 23:38:32 +01:00
2020-02-14 08:22:00 +01:00
sub func_uri_escape {
2020-02-15 23:38:32 +01:00
my $self = shift;
my $text = "@_";
return uri_escape_utf8($text);
2020-02-14 08:22:00 +01:00
}
2020-02-14 07:37:09 +01:00
1;