spider/perl/Script.pm

123 lines
1.8 KiB
Perl
Raw Normal View History

2001-09-14 18:00:52 +00:00
#
# module to do startup script handling
#
# Copyright (c) 2001 Dirk Koopman G1TLH
#
# $Id$
#
package Script;
use strict;
use DXUtil;
use DXDebug;
use DXChannel;
use DXCommandmode;
use DXVars;
use IO::File;
my $base = "$main::root/scripts";
2005-01-12 20:41:30 +00:00
sub clean
{
my $s = shift;
$s =~ s/[^-\w\.]//g;
return $s;
}
2001-09-14 18:00:52 +00:00
sub new
{
my $pkg = shift;
2005-05-25 19:49:40 +00:00
my $script = clean(shift);
my $mybase = shift || $base;
my $fn = "$mybase/$script";
2001-09-14 18:00:52 +00:00
2005-05-25 19:49:40 +00:00
my $self = {call => $script};
my $fh = IO::File->new($fn);
if ($fh) {
$self->{fn} = $fn;
} else {
$fh = IO::File->new(lc $fn);
if ($fh) {
$self->{fn} = $fn;
} else {
return undef;
}
}
2001-09-14 18:00:52 +00:00
my @lines;
while (<$fh>) {
chomp;
push @lines, $_;
}
$fh->close;
$self->{lines} = \@lines;
2005-05-25 19:49:40 +00:00
$self->{inscript} = 1;
2005-01-12 20:41:30 +00:00
return bless $self, $pkg;
2001-09-14 18:00:52 +00:00
}
sub run
{
my $self = shift;
my $dxchan = shift;
2005-05-25 19:49:40 +00:00
my $return_output = shift;
my @out;
2001-09-14 18:00:52 +00:00
foreach my $l (@{$self->{lines}}) {
unless ($l =~ /^\s*\#/ || $l =~ /^\s*$/) {
2005-05-25 19:49:40 +00:00
$dxchan->inscript(1) if $self->{inscript};
push @out, DXCommandmode::run_cmd($dxchan, $l);
$dxchan->inscript(0) if $self->{inscript};
2003-02-25 15:20:29 +00:00
last if @out && $l =~ /^pri?v?/i;
2001-09-14 18:00:52 +00:00
}
}
2005-05-25 19:49:40 +00:00
if ($return_output) {
return @out;
} else {
if ($dxchan->can('send_ans')) {
$dxchan->send_ans(@out);
} else {
dbg($_) for @out;
}
}
return ();
}
sub inscript
{
my $self = shift;
$self->{inscript} = shift if @_;
return $self->{inscript};
2001-09-14 18:00:52 +00:00
}
2005-01-12 20:41:30 +00:00
sub store
{
my $call = clean(lc shift);
my @out;
my $ref = ref $_[0] ? shift : \@_;
my $count;
my $fn = "$base/$call";
rename $fn, "$fn.o" if -e $fn;
my $f = IO::File->new(">$fn") || return undef;
for (@$ref) {
$f->print("$_\n");
$count++;
}
$f->close;
unlink $fn unless $count;
return $count;
}
sub lines
{
my $self = shift;
return @{$self->{lines}};
}
sub erase
{
2005-05-25 19:49:40 +00:00
my $self = shift;
unlink $self->{fn};
2005-01-12 20:41:30 +00:00
}