mirror of
https://github.com/pragma-/pbot.git
synced 2025-01-22 10:04:36 +01:00
Add grep
Function
This commit is contained in:
parent
cfba96e8ae
commit
8f983bcbda
72
Plugins/FuncGrep.pm
Normal file
72
Plugins/FuncGrep.pm
Normal file
@ -0,0 +1,72 @@
|
||||
# File: FuncGrep.pm
|
||||
# Author: pragma-
|
||||
#
|
||||
# Purpose: Registers the grep Function
|
||||
|
||||
# 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::FuncGrep;
|
||||
use parent 'Plugins::Plugin';
|
||||
|
||||
use warnings; use strict;
|
||||
use feature 'unicode_strings';
|
||||
|
||||
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;
|
BIN
data/factoids.sqlite3
vendored
BIN
data/factoids.sqlite3
vendored
Binary file not shown.
1
data/plugin_autoload
vendored
1
data/plugin_autoload
vendored
@ -8,6 +8,7 @@ AutoRejoin
|
||||
Counter
|
||||
Date
|
||||
FuncBuiltins
|
||||
FuncGrep
|
||||
FuncSed
|
||||
GoogleSearch
|
||||
Quotegrabs
|
||||
|
Loading…
Reference in New Issue
Block a user