From bc67e4afa9b3c9fcf0e537e04a743ad5852356e6 Mon Sep 17 00:00:00 2001 From: minima Date: Mon, 23 Jan 2006 00:02:51 +0000 Subject: [PATCH] add cmd and text xml handlers add more sophistication to the dxxml router --- perl/DXXml.pm | 45 ++++++++++++++++++++++---- perl/DXXml/Cmd.pm | 81 ++++++++++++++++++++++++++++++++++++++++++++++ perl/DXXml/Text.pm | 70 +++++++++++++++++++++++++++++++++++++++ perl/cluster.pl | 2 +- 4 files changed, 190 insertions(+), 8 deletions(-) create mode 100644 perl/DXXml/Cmd.pm create mode 100644 perl/DXXml/Text.pm diff --git a/perl/DXXml.pm b/perl/DXXml.pm index 9d0a5969..a9072d46 100644 --- a/perl/DXXml.pm +++ b/perl/DXXml.pm @@ -18,6 +18,8 @@ use DXUtil; use DXXml::Ping; use DXXml::Dx; use DXXml::IM; +use DXXml::Text; +use DXXml::Cmd; use vars qw($VERSION $BRANCH $xs $id $max_old_age $max_future_age $dupeage); $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ ); @@ -37,7 +39,13 @@ sub new { my $pkg = shift; my $class = ref $pkg || $pkg; - return bless{@_}, $class; + my $self = bless{@_}, $class; + while (@_) { + my $key = shift; + my $val = shift; + $self->{$key} = $val if defined $val; + } + return $self; } # @@ -204,7 +212,13 @@ sub route my $self = shift; my $fromdxchan = shift; my $to = shift; - my $via = $to || $self->{'-via'} || $self->{to}; + my $via = $to; + my $dxchan; + + if (my $u = $self->{u}) { + $via ||= $u if ($dxchan = DXChannel::get($u)); + } + $via ||= $self->{'-via'} || $self->{to}; unless ($via) { dbg("XML: no route specified (" . $self->toxml . ")") if isdbg('chanerr'); @@ -216,7 +230,7 @@ sub route } # always send it down the local interface if available - my $dxchan = DXChannel::get($via); + $dxchan ||= DXChannel::get($via); if ($dxchan) { dbg("route: $via -> $dxchan->{call} direct" ) if isdbg('route'); } else { @@ -249,13 +263,30 @@ sub route return; } + $self->{o} ||= $main::mycall; + $self->{id} ||= nextid(); + $self->{'-timet'} ||= $main::systime; + if ($dxchan->handle_xml) { $dxchan->send($self->toxml); + } elsif ($dxchan->isnode) { + my $ref = $self->topcxx($dxchan); + if (ref $ref) { + for (@$ref) { + $dxchan->send($_); + } + } else { + $dxchan->send($ref); + } } else { - $self->{o} ||= $main::mycall; - $self->{id} ||= nextid(); - $self->{'-timet'} ||= $main::systime; - $dxchan->send($self->topcxx); + my $ref = $self->tocmd($dxchan); + if (ref $ref) { + for (@$ref) { + $dxchan->send($_); + } + } else { + $dxchan->send($ref); + } } } diff --git a/perl/DXXml/Cmd.pm b/perl/DXXml/Cmd.pm new file mode 100644 index 00000000..821606af --- /dev/null +++ b/perl/DXXml/Cmd.pm @@ -0,0 +1,81 @@ +# +# XML (R)Cmd handler +# +# $Id$ +# +# Copyright (c) Dirk Koopman, G1TLH +# + +use strict; + +package DXXml::Cmd; + +use DXDebug; +use DXProt; +use IsoTime; +use Investigate; +use Time::HiRes qw(gettimeofday tv_interval); +use DXXml::Text; + +use vars qw($VERSION $BRANCH @ISA %pings); +$VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ ); +$BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0)); +$main::build += $VERSION; +$main::branch += $BRANCH; + +@ISA = qw(DXXml); + +sub handle_input +{ + my $self = shift; + my $dxchan = shift; + + if ($self->{to} eq $main::mycall) { + my @in; + + my $cmd = $self->{content}; + + if ($self->{o} eq $dxchan->call) { # online user or node + @in = (DXCommandmode::run_cmd($dxchan, $cmd)); + } else { + # remote command + my $ref = DXUser->get_current($self->{o}); + my $cref = Route::Node::get($self->{o}); + my $answer; + + if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS! + $self->{remotecmd} = 1; # for the benefit of any command that needs to know + my $oldpriv = $dxchan->{priv}; + $dxchan->{priv} = $ref->{priv}; # assume the user's privilege level + @in = (DXCommandmode::run_cmd($dxchan, $cmd)); + $dxchan->{priv} = $oldpriv; + delete $dxchan->{remotecmd}; + $answer = "success"; + } else { + $answer = "denied"; + } + Log('rcmd', 'in', $ref->{priv}, $self->{o}, "$self->{content}($answer)"); + } + my $rep = DXXml::Text->new(u=>$self->{u}, to=>$self->{o}, content=>join('%0A', @in)); + $rep->route($dxchan); + } else { + $self->route($dxchan); + } +} + +sub topcxx +{ + my $self = shift; + + my $ref = DXUser->get_current($self->{to}); + my $s; + + if ($ref && $ref->is_clx && $self->{u}) { + $s = DXProt::pc84(($self->{o} || $main::mycall), $self->{to}, $self->{u}, $self->{content}); + } else { + $s = DXProt::pc34(($self->{o} || $main::mycall), $self->{to}, $self->{content}); + } + return $self->{'-pcxx'} = $s; +} + +1; diff --git a/perl/DXXml/Text.pm b/perl/DXXml/Text.pm new file mode 100644 index 00000000..35c83191 --- /dev/null +++ b/perl/DXXml/Text.pm @@ -0,0 +1,70 @@ +# +# XML Text handler +# +# $Id$ +# +# Copyright (c) Dirk Koopman, G1TLH +# + +use strict; + +package DXXml::Text; + +use DXDebug; +use DXProt; +use IsoTime; +use Investigate; +use Time::HiRes qw(gettimeofday tv_interval); + +use vars qw($VERSION $BRANCH @ISA %pings); +$VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ ); +$BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0)); +$main::build += $VERSION; +$main::branch += $BRANCH; + +@ISA = qw(DXXml); + +sub handle_input +{ + my $self = shift; + my $dxchan = shift; + + # this is always routed + if ($self->{to} eq $main::mycall ) { + my $r = DXChannel::get($main::myalias); + $dxchan = $r if $r; + } + $self->route($dxchan); + +} + +sub topcxx +{ + my $self = shift; + my $dxchan = shift; + my @out; + + my $ref = DXUser->get_current($self->{to}); + while (@_) { + my $line = shift; + $line =~ s/\s*$//; + Log('rcmd', 'out', $self->{to}, $line); + if ($self->{u} && $dxchan->is_clx && $ref->is_clx) { + push @out, pc85($main::mycall, $self->{to}, $self->{u}, "$main::mycall:$line"); + } else { + push @out, pc35($main::mycall, $self->{to}, "$main::mycall:$line"); + } + } + + return $self->{'-pcxx'} = \@out; +} + +sub tocmd +{ + my $self = shift; + + my @out = split /(?:%0D)?\%0A/, $self->{content}; + return $self->{-cmd} = \@out; +} + +1; diff --git a/perl/cluster.pl b/perl/cluster.pl index 5f5b492c..8aade278 100755 --- a/perl/cluster.pl +++ b/perl/cluster.pl @@ -135,7 +135,7 @@ $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ ); $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0)); $main::build += $VERSION; $main::branch += $BRANCH; -$main::build -= 1; # fudge (put back for now) +$main::build -= 3; # fudge (put back for now) -- 2.43.0