aboutsummaryrefslogtreecommitdiff
path: root/Plugin
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2019-03-30 14:13:09 +1300
committerDavid Phillips <david@sighup.nz>2019-03-30 14:13:09 +1300
commit9a920fb861b87e63066b068cdc105993d93f5bbf (patch)
tree88f0714f7bf3f7c2c70302dff6574ef6f70ddb89 /Plugin
parent0db3e5d40f0e1e09aca3f76882fbb2341f002eda (diff)
downloadidalius-9a920fb861b87e63066b068cdc105993d93f5bbf.tar.xz
Convert: add convert plugin
Diffstat (limited to 'Plugin')
-rwxr-xr-xPlugin/Convert.pm39
1 files changed, 39 insertions, 0 deletions
diff --git a/Plugin/Convert.pm b/Plugin/Convert.pm
new file mode 100755
index 0000000..c830628
--- /dev/null
+++ b/Plugin/Convert.pm
@@ -0,0 +1,39 @@
+package Plugin::Convert;
+
+use strict;
+use warnings;
+use IPC::Open2;
+
+sub configure {
+ my $self = shift;
+ my $cmdref = shift;
+
+ $cmdref->($self, "convert", sub { $self->convert(@_); } );
+
+ return $self;
+}
+
+sub convert {
+ my ($self, $irc, $logger, $who, $where, $ided, $rest, $no_reenter, @arguments) = @_;
+
+ my $from = (split / to /, $rest)[0];
+ my $to = (split / to /, $rest)[1];
+
+ return "Syntax: convert <from> to <to>\n" unless ($from and $to);
+
+ my ($out, $in);
+ my $pid = open2($out, $in, 'units', '-1', '--compact', '--quiet');
+
+ print $in "$from\n$to\n";
+ my $converted = <$out>;
+ chomp $converted;
+
+ close($in);
+ waitpid($pid, 0);
+ my $exit_status = $? >> 8;
+ # `units` doesn't actually seem to set this non-zero, but use it anyway
+ return "Conversion error" if $exit_status;
+
+ return "Converted: $converted\n";
+}
+1;