-
Notifications
You must be signed in to change notification settings - Fork 1
/
foreachline.pl
60 lines (47 loc) · 1.31 KB
/
foreachline.pl
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use strict;
use Irssi;
use vars qw($VERSION %IRSSI);
$VERSION = "0.10";
%IRSSI = (
authors => "mr_flea",
contact => "mrflea\@gmail.com",
name => "foreachline.pl",
description => "Execute a command for each line in a file.",
license => "BSD",
url => "http://www.phantomflame.com/",
changed => "Thu 24 Oct 2013 08:23:49 AM UTC",
);
sub printmsg {
Irssi::print("foreachline: $_[0]", MSGLEVEL_CLIENTCRAP);
}
sub cmd_foreachline {
my ($data, $server, $witem) = @_;
my ($file, $command) = split(/\s+/, $data, 2);
if (not defined $file || not defined $command) {
printmsg("Usage: /foreachline <file> <command|message>");
return;
}
if ($command !~ /(?<!\%)\%s/) {
printmsg("Error: At least one occurrence of '%s' should be present in the command.");
return;
}
# We can handle ~/file, but not ~user/file.
if ($file =~ /^~(?!\/)/) {
printmsg("Error: Please provide an absolute file path.");
return;
}
$file =~ s/^~\//$ENV{"HOME"}\//;
if (not open(ITERFILE, "<", $file)) {
printmsg("Couldn't open $file for reading.");
return;
}
while (my $line = <ITERFILE>) {
chomp($line);
my $exec = $command;
$exec =~ s/(?<!%)%s/$line/g;
$exec =~ s/%%/%/g;
Irssi::signal_emit("send command", $exec, $server, $witem);
}
close(ITERFILE);
}
Irssi::command_bind('foreachline', 'cmd_foreachline');