+sub do_entry_stuff
+{
+ my $self = shift;
+ my $line = shift;
+ my @out;
+
+ if ($self->state eq 'enterbody') {
+ my $loc = $self->{loc} || confess "local var gone missing" ;
+ if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
+ no strict 'refs';
+ push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
+ $self->func(undef);
+ $self->state('prompt');
+ } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
+ push @out, $self->msg('m10');
+ delete $loc->{lines};
+ delete $self->{loc};
+ $self->func(undef);
+ $self->state('prompt');
+ } else {
+ push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
+ # i.e. it ain't and end or abort, therefore store the line
+ }
+ } else {
+ confess "Invalid state $self->{state}";
+ }
+ return @out;
+}
+
+sub store_startup_script
+{
+ my $self = shift;
+ my $loc = $self->{loc} || confess "local var gone missing" ;
+ my @out;
+ my $call = $loc->{call} || confess "callsign gone missing";
+ confess "lines array gone missing" unless ref $loc->{lines};
+ my $r = Script::store($call, $loc->{lines});
+ if (defined $r) {
+ if ($r) {
+ push @out, $self->msg('m19', $call, $r);
+ } else {
+ push @out, $self->msg('m20', $call);
+ }
+ } else {
+ push @out, "error opening startup script $call $!";
+ }
+ return @out;
+}
+
+# Import any commands contained in any files in import_cmd directory
+#
+# If the filename has a recogisable callsign as some delimited part
+# of it, then this is the user the command will be run as.
+#
+sub import_cmd
+{
+ # are there any to do in this directory?
+ return unless -d $cmdimportdir;
+ unless (opendir(DIR, $cmdimportdir)) {
+ LogDbg('err', "can\'t open $cmdimportdir $!");
+ return;
+ }
+
+ my @names = readdir(DIR);
+ closedir(DIR);
+ my $name;
+
+ return unless @names;
+
+ foreach $name (@names) {
+ next if $name =~ /^\./;
+
+ my $s = Script->new($name, $cmdimportdir);
+ if ($s) {
+ LogDbg('DXCommand', "Run import cmd file $name");
+ my @cat = split /[^A-Za-z0-9]+/, $name;
+ my ($call) = grep {is_callsign(uc $_)} @cat;
+ $call ||= $main::mycall;
+ $call = uc $call;
+ my @out;
+
+
+ $s->inscript(0); # switch off script checks
+
+ if ($call eq $main::mycall) {
+ @out = $s->run($main::me, 1);
+ } else {
+ my $dxchan = DXChannel::get($call);
+ if ($dxchan) {
+ @out = $s->run($dxchan, 1);
+ } else {
+ my $u = DXUser::get($call);
+ if ($u) {
+ $dxchan = $main::me;
+ my $old = $dxchan->{call};
+ my $priv = $dxchan->{priv};
+ my $user = $dxchan->{user};
+ $dxchan->{call} = $call;
+ $dxchan->{priv} = $u->priv;
+ $dxchan->{user} = $u;
+ @out = $s->run($dxchan, 1);
+ $dxchan->{call} = $old;
+ $dxchan->{priv} = $priv;
+ $dxchan->{user} = $user;
+ } else {
+ LogDbg('err', "Trying to run import cmd for non-existant user $call");
+ }
+ }
+ }
+ $s->erase;
+ for (@out) {
+ LogDbg('DXCommand', "Import cmd $name/$call: $_");
+ }
+ } else {
+ LogDbg('err', "Failed to open $cmdimportdir/$name $!");
+ unlink "$cmdimportdir/$name";
+ }
+ }
+}
+
+sub print_find_reply
+{
+ my ($self, $node, $target, $flag, $ms) = @_;
+ my $sort = $flag == 2 ? "External" : "Local";
+ $self->send("$sort $target found at $node in $ms ms" );
+}
+
+# send the most relevant motd
+sub send_motd
+{
+ my $self = shift;
+ my $motd;
+
+ unless ($self->{registered}) {
+ $motd = "${main::motd}_nor_$self->{lang}";
+ $motd = "${main::motd}_nor" unless -e $motd;
+ }
+ $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
+ $motd = $main::motd unless $motd && -e $motd;
+ if ($self->conn->ax25) {
+ if ($motd) {
+ $motd = "${motd}_ax25" if -e "${motd}_ax25";
+ } else {
+ $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
+ }
+ }
+ $self->send_file($motd) if -e $motd;
+}
+
+
+# Punt off a long running command into a separate process
+#
+# This is called from commands to run some potentially long running
+# function. The process forks and then runs the function and returns
+# the result back to the cmd.
+#
+# NOTE: this merely forks the current process and then runs the cmd in that (current) context.
+# IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER
+# THE CURRENT CONTEXT!!
+#
+# call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
+sub spawn_cmd
+{
+ my $self = shift;
+ my $line = shift;
+ my $cmdref = shift;
+ my $call = $self->{call};
+ my %args = @_;
+ my @out;
+
+ my $cb = delete $args{cb};
+ my $prefix = delete $args{prefix};
+ my $progress = delete $args{progress};
+ my $args = delete $args{args} || [];
+ my $t0 = [gettimeofday];
+
+ no strict 'refs';
+
+ # just behave normally if something has set the "one-shot" _nospawn in the channel
+ return ($cmdref->(@$args)) if $self->{_nospawn};
+
+ my $fc = Mojo::IOLoop::Subprocess->new;
+# $fc->serializer(\&encode_json);
+# $fc->deserializer(\&decode_json);
+ $fc->run(
+ sub {
+ my $subpro = shift;
+ if (isdbg('spawn_cmd')) {
+ my $s = "line: $line";
+ $s .= ", args: " . join(', ', @$args) if $args && @$args;
+ }
+ my @res = $cmdref->(@$args);
+# diffms("rcmd from $call 1", $line, $t0, scalar @res) if isdbg('chan');
+ return @res;
+ },
+# $args,
+ sub {
+ my ($fc, $err, @res) = @_;
+ my $dxchan = DXChannel::get($call);
+ return unless $dxchan;
+
+ if ($err) {
+ my $s = "DXProt::spawn_cmd: call $call error $err";
+ dbg($s) if isdbg('chan');
+ $dxchan->send($s);
+ return;
+ }
+ if ($cb) {
+ # transform output if required
+ @res = $cb->($dxchan, @res);
+ }
+ if (@res) {
+ if (defined $prefix) {
+ $dxchan->send(map {"$prefix$_"} @res);
+ } else {
+ $dxchan->send(@res);
+ }
+ }
+ diffms("by $call", $line, $t0, scalar @res) if isdbg('progress');
+ });
+
+ return @out;
+}