2020-05-02 11:34:32 +02:00
|
|
|
# File: FuncGrep.pm
|
|
|
|
#
|
|
|
|
# Purpose: Registers the grep Function
|
|
|
|
|
2023-02-21 06:31:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 2020-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
2020-05-02 11:34:32 +02:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
package PBot::Plugin::FuncGrep;
|
|
|
|
use parent 'PBot::Plugin::Base';
|
2020-05-02 11:34:32 +02:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2020-05-02 11:34:32 +02:00
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
$self->{pbot}->{functions}->register(
|
|
|
|
'grep',
|
|
|
|
{
|
|
|
|
desc => 'prints region of text that matches regex',
|
|
|
|
usage => 'grep <regex>',
|
|
|
|
subref => sub { $self->func_grep(@_) }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub unload {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{pbot}->{functions}->unregister('grep');
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func_grep {
|
|
|
|
my $self = shift @_;
|
|
|
|
my $regex = shift @_;
|
|
|
|
my $text = "@_";
|
|
|
|
|
|
|
|
my $result = eval {
|
|
|
|
my $result = '';
|
|
|
|
|
|
|
|
my $search_regex = $regex;
|
|
|
|
|
|
|
|
if ($search_regex !~ s/^\^/\\b/) {
|
|
|
|
$search_regex = "\\S*$search_regex";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($search_regex !~ s/\$$/\\b/) {
|
|
|
|
$search_regex = "$search_regex\\S*";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $matches = 0;
|
|
|
|
|
|
|
|
while ($text =~ /($search_regex)/igms) {
|
|
|
|
$result .= "$1\n";
|
|
|
|
$matches++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "grep: '$regex' not found" if not $matches;
|
|
|
|
return $result;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
$@ =~ s/ at.*$//;
|
|
|
|
$@ =~ s/marked by .* HERE in m\/\(//;
|
|
|
|
$@ =~ s/\s*\)\/$//;
|
|
|
|
return "grep: $@";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|