aboutsummaryrefslogtreecommitdiff
path: root/Plugin/Map.pm
blob: c8708e20fc8a3b7bd38300b312fce359c435b16b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package Plugin::Map;

use strict;
use warnings;

use ListParser;

my $run_command;

sub configure {
	my $self = shift;
	my $cmdref = shift;
	$run_command = shift;

	$cmdref->($self, "map", sub { $self->map(@_); } );

	return $self;
}


sub map {
	my ($self, $irc, $logger, $who, $where, $ided, $rest, @arguments) = @_;
	my ($command, $subjects_raw) = ($rest =~ /^(.+)\s+(\[.*\])$/);

	return "Syntax: map command [item1, item2, ...]" unless $command and $subjects_raw;

	my $parsed = ListParser::parse_list($subjects_raw);
	return $parsed->{error} if $parsed->{error};

	my @results = map { $run_command->("$command $_", $who, $where, $ided) } @{$parsed->{array}};
	return "[" . (join ", ", @results). "]";
}
1;