Merge branch 'test' into mojo
[spider.git] / perl / DXProt.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the protocal mode for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 #
8 #
9
10 package DXProt;
11
12 @ISA = qw(DXChannel);
13
14 use DXUtil;
15 use DXChannel;
16 use DXUser;
17 use DXM;
18 use DXProtVars;
19 use DXCommandmode;
20 use DXLog;
21 use Spot;
22 use DXProtout;
23 use DXDebug;
24 use Filter;
25 use Local;
26 use DXDb;
27 use AnnTalk;
28 use Geomag;
29 use WCY;
30 use BadWords;
31 use DXHash;
32 use Route;
33 use Route::Node;
34 use Script;
35 use DXProtHandle;
36 use DXCIDR;
37
38 use Time::HiRes qw(gettimeofday tv_interval);
39 use DXSubprocess;
40
41 use strict;
42
43 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
44                         $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
45                         $pingint $obscount %pc19list $chatdupeage $chatimportfn
46                         $pc19_version $myprot_version
47                         %nodehops $baddx $badspotter $badnode $censorpc
48                         $allowzero $decode_dk0wcy $send_opernam @checklist
49                         $eph_pc15_restime $pc92_update_period $pc92_obs_timeout
50                         %pc92_find $pc92_find_timeout $pc92_short_update_period
51                         $next_pc92_obs_timeout $pc92_slug_changes $last_pc92_slug
52                         $pc92_extnode_update_period $pc50_interval
53                         $pc92_keepalive_period $senderverify $pc92_ad_enabled
54                         $pc92c_ipaddr_enable
55                    );
56
57 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
58 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
59
60 $last_hour = time;                              # last time I did an hourly periodic update
61 %rcmds = ();                    # outstanding rcmd requests outbound
62 %nodehops = ();                 # node specific hop control
63 %pc19list = ();                                 # list of outstanding PC19s that haven't had PC16s on them
64
65 $censorpc = 1;                                  # Do a BadWords::check on text fields and reject things
66                                                                 # loads of 'bad things'
67 $baddx = new DXHash "baddx";
68 $badspotter = new DXHash "badspotter";
69 $badnode = new DXHash "badnode";
70 $last10 = $last_pc50 = time;
71 $ann_to_talk = 1;
72 $eph_restime = 60;
73 $eph_info_restime = 18*60*60;
74 $eph_pc15_restime = 6*60;
75 $eph_pc34_restime = 30;
76 $pingint = 5*60;
77 $obscount = 2;
78 $chatdupeage = 20 * 60;
79 $chatimportfn = "$main::root/chat_import";
80 $pc19_version = 5455;                   # the visible version no for outgoing PC19s generated from pc59
81 $pc92_update_period = 4*60*60;  # the period between outgoing PC92 C updates
82 $pc92_short_update_period = 15*60; # shorten the update period after a connection or start up
83 $pc92_extnode_update_period = 1*60*60; # the update period for external nodes
84 $pc92_keepalive_period = 1*60*60;       # frequency of PC92 K (keepalive) records
85 %pc92_find = ();                                # outstanding pc92 find operations
86 $pc92_find_timeout = 30;                # maximum time to wait for a reply
87 $senderverify = 0;                              # 1 = check spotter is on node it says it is and check ip address if available
88 ;                                                               # 2 = do 1 and dump if check
89 $pc92_ad_enabled = 1;                   # send pc92 A & D records.
90 $pc92c_ipaddr_enable = 0;               # add the local ip address info to each callsign in a PC92 C
91
92 @checklist =
93 (
94  [ qw(i c c m bp bc c) ],                       # pc10
95  [ qw(i f m d t m c c h) ],             # pc11
96  [ qw(i c bm m bm bm p h) ],            # pc12
97  [ qw(i c h) ],                                 #
98  [ qw(i c h) ],                                 #
99  [ qw(i c m h) ],                                       #
100  undef ,                                                # pc16 has to be validated manually
101  [ qw(i c c h) ],                                       # pc17
102  [ qw(i m n) ],                                 # pc18
103  undef ,                                                # pc19 has to be validated manually
104  undef ,                                                # pc20 no validation
105  [ qw(i c m h) ],                                       # pc21
106  undef ,                                                # pc22 no validation
107  [ qw(i d n n n n m c c h) ],           # pc23
108  [ qw(i c p h) ],                                       # pc24
109  [ qw(i c c n n) ],                             # pc25
110  [ qw(i f m d t m c c bc) ],            # pc26
111  [ qw(i d n n n n m c c bc) ],  # pc27
112  [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
113  [ qw(i c c n m) ],                             # pc29
114  [ qw(i c c n) ],                                       # pc30
115  [ qw(i c c n) ],                                       # pc31
116  [ qw(i c c n) ],                                       # pc32
117  [ qw(i c c n) ],                                       # pc33
118  [ qw(i c c m) ],                                       # pc34
119  [ qw(i c c m) ],                                       # pc35
120  [ qw(i c c m) ],                                       # pc36
121  [ qw(i c c n m) ],                             # pc37
122  undef,                                                 # pc38 not interested
123  [ qw(i c m) ],                                 # pc39
124  [ qw(i c c m p n) ],                           # pc40
125  [ qw(i c n m h) ],                             # pc41
126  [ qw(i c c n) ],                                       # pc42
127  undef,                                                 # pc43 don't handle it
128  [ qw(i c c n m m c) ],                 # pc44
129  [ qw(i c c n m) ],                             # pc45
130  [ qw(i c c n) ],                                       # pc46
131  undef,                                                 # pc47
132  undef,                                                 # pc48
133  [ qw(i c m h) ],                                       # pc49
134  [ qw(i c n h) ],                                       # pc50
135  [ qw(i c c n) ],                                       # pc51
136  undef,
137  undef,
138  undef,
139  undef,
140  undef,
141  undef,
142  undef,
143  undef,
144  undef,                                                 # pc60
145  [ qw(i f m d t m c c a h) ],           # pc61
146  undef,
147  undef,
148  undef,
149  undef,
150  undef,
151  undef,
152  undef,
153  undef,
154  undef,                                                 # pc70
155  undef,
156  undef,
157  [ qw(i d n n n n n n m m m c c h) ],   # pc73
158  undef,
159  undef,
160  undef,
161  undef,
162  undef,
163  undef,
164  undef,                                                 # pc80
165  undef,
166  undef,
167  undef,
168  [ qw(i c c c m) ],                             # pc84
169  [ qw(i c c c m) ],                             # pc85
170  undef,
171  undef,
172  undef,
173  undef,
174  undef,                                                 # pc90
175  undef,
176  [ qw(i c f l)],                                # pc92
177  [ qw(i c f *m c *c m)],                                        # pc93
178 );
179
180 # use the entry in the check list to check the field list presented
181 # return OK if line NOT in check list (for now)
182 sub check
183 {
184         my $n = shift;
185         my $pc = shift;
186         
187         $n -= 10;
188         return 0 if $n < 0 || $n > @checklist;
189         my $ref = $checklist[$n];
190         return 0 unless ref $ref;
191
192         my $i;
193         for ($i = 1; $i < @$ref; $i++) {
194                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
195                 return 0 unless $act;
196                 next if $blank eq 'b' && $pc->[$i] =~ /^[ \*]$/;
197                 next if $blank eq '*' && $pc->[$i] =~ /^\*$/;
198                 if ($act eq 'c') {
199                         return $i unless is_callsign($pc->[$i]);
200                 } elsif ($act eq 'i') {
201                         ;                                       # do nothing
202                 } elsif ($act eq 'm') {
203                         return $i unless is_pctext($pc->[$i]);
204                 } elsif ($act eq 'p') {
205                         return $i unless is_pcflag($pc->[$i]);
206                 } elsif ($act eq 'f') {
207                         return $i unless is_freq($pc->[$i]);
208                 } elsif ($act eq 'n') {
209                         return $i unless $pc->[$i] =~ /^[\d ]+$/;
210                 } elsif ($act eq 'h') {
211                         return $i unless $pc->[$i] =~ /^H\d\d?$/;
212                 } elsif ($act eq 'd') {
213                         return $i unless $pc->[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
214                 } elsif ($act eq 't') {
215                         return $i unless $pc->[$i] =~ /^[012]\d[012345]\dZ$/;
216                 } elsif ($act eq 'l') {
217                         return $i unless $pc->[$i] =~ /^[A-Z]$/;
218                 } elsif ($act eq 'a') {
219                         return $i unless is_ipaddr($pc->[$i]);
220                 }
221         }
222         return 0;
223 }
224
225 sub update_pc92_next
226 {
227         my $self = shift;
228         my $period = shift || ($self->{do_pc9x} ? $pc92_update_period : $pc92_extnode_update_period);
229         $self->{next_pc92_update} = $main::systime + $period - int rand($period / 4);
230         dbg("ROUTE: update_pc92_next: $self->{call} " . atime($self->{next_pc92_update})) if isdbg('obscount');
231 }
232
233 sub update_pc92_keepalive
234 {
235         my $self = shift;
236         my $period = shift || $pc92_keepalive_period;
237         $self->{next_pc92_keepalive} = $main::systime + $period - int rand($period / 4);
238         dbg("ROUTE: update_pc92_keepalive: $self->{call} " . atime($self->{next_pc92_keepalive})) if isdbg('obscount');
239 }
240
241 sub init
242 {
243         my $fn = localdata("hop_table.pl");
244         do $fn if -e $fn;
245         confess $@ if $@;
246
247         my $user = DXUser::get($main::mycall);
248         die "User $main::mycall not setup or disappeared RTFM" unless $user;
249
250         $myprot_version += $main::version*100;
251         $main::me = DXProt->new($main::mycall, 0, $user);
252         $main::me->{here} = 1;
253         $main::me->{state} = "indifferent";
254         $main::me->{sort} = 'S';    # S for spider
255         $main::me->{priv} = 9;
256         $main::me->{metric} = 0;
257         $main::me->{pingave} = 0;
258         $main::me->{registered} = 1;
259         $main::me->{version} = $main::version;
260         $main::me->{build} = $main::build;
261         $main::me->{do_pc9x} = 1;
262         $main::me->{hostname} = $main::clusteraddr;
263         $main::me->update_pc92_next($pc92_short_update_period);
264         $main::me->update_pc92_keepalive;
265 }
266
267 #
268 # obtain a new connection this is derived from dxchannel
269 #
270
271 sub new
272 {
273         my $self = DXChannel::alloc(@_);
274
275         # add this node to the table, the values get filled in later
276         my $pkg = shift;
277         my $call = shift;
278
279         # if we have an entry already, then send a PC21 to all connect
280         # old style connections, because we are about to get the real deal
281         if (my $ref = Route::Node::get($call)) {
282                 dbg("ROUTE: $call is already in the routing table, deleting") if isdbg('route');
283                 my @rout = $ref->delete;
284                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
285         }
286         $main::routeroot->add($call, '5000', Route::here(1), $self->{conn}->peerhost) if $call ne $main::mycall;
287
288         return $self;
289 }
290
291 # this is how a pc connection starts (for an incoming connection)
292 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
293 # all the crap that comes between).
294 sub start
295 {
296         my ($self, $line, $sort) = @_;
297         my $call = $self->{call};
298         my $user = $self->{user};
299
300         # log it
301         my $host = $self->{conn}->peerhost;
302         $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
303         $host ||= $host if is_ipaddr($host);
304         $host ||= "unknown";
305         $self->{hostname} = $host if is_ipaddr($host);
306         Log('DXProt', "$call connected from $host");
307
308         # remember type of connection
309         $self->{consort} = $line;
310         $self->{outbound} = $sort eq 'O';
311         my $priv = $user->priv;
312         $priv = $user->priv(1) unless defined $priv;
313         $self->{priv} = $priv;     # other clusters can always be 'normal' users
314         $self->{lang} = $user->lang || 'en';
315         $self->{isolate} = $user->{isolate};
316         $self->{consort} = $line;       # save the connection type
317         $self->{here} = 1;
318         $self->{width} = 80;
319
320         # sort out registration
321         $self->{registered} = 1;
322
323         # get the output filters
324 #       $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
325 #       $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
326 #       $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
327 #       $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
328 #       $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate};
329 #       $self->{pc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
330
331         Filter::load_dxchan($self, 'spots', 0);
332         Filter::load_dxchan($self, 'wwv', 0);
333         Filter::load_dxchan($self, 'wcy', 0);
334         Filter::load_dxchan($self, 'ann', 0);
335         Filter::load_dxchan($self, 'route', 0) unless $self->{isolate};
336         Filter::load_dxchan($self, 'pc92', 0) unless $self->{isolate};
337
338
339         # get the INPUT filters (these only pertain to Clusters)
340 #       $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
341 #       $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
342 #       $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
343 #       $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
344 #       $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
345 #       $self->{inpc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
346
347         Filter::load_dxchan($self, 'spots', 1);
348         Filter::load_dxchan($self, 'wwv', 1);
349         Filter::load_dxchan($self, 'wcy', 1);
350         Filter::load_dxchan($self, 'ann', 1);
351         Filter::load_dxchan($self, 'route', 1) unless $self->{isolate};
352         Filter::load_dxchan($self, 'pc92', 1) unless $self->{isolate};
353
354
355         # set unbuffered and no echo
356         $self->send_now('B',"0");
357         $self->send_now('E',"0");
358         $self->conn->echo(0) if $self->conn->can('echo');
359
360         # ping neighbour node stuff
361         my $ping = $user->pingint;
362         $ping = $pingint unless defined $ping;
363         $self->{pingint} = $ping;
364         $self->{nopings} = $user->nopings || $obscount;
365         $self->{pingtime} = [ ];
366         $self->{pingave} = 999;
367         $self->{metric} ||= 100;
368         $self->{lastping} = $main::systime;
369
370         # send initialisation string
371         unless ($self->{outbound}) {
372                 $self->sendinit;
373         }
374
375         $self->state('init');
376         $self->{pc50_t} = $main::systime;
377
378         # send info to all logged in thingies
379         $self->tell_login('loginn');
380
381         # run a script send the output to the debug file
382         my $script = new Script(lc $call) || new Script('node_default');
383         $script->run($self) if $script;
384
385         # set up a config broadcast "quite soon" to converge tables quicker
386         $main::me->update_pc92_next($pc92_short_update_period);
387         $self->update_pc92_next($pc92_short_update_period);
388
389         # set next keepalive time
390         $self->update_pc92_keepalive;
391 }
392
393 #
394 # send outgoing 'challenge'
395 #
396
397 sub sendinit
398 {
399         my $self = shift;
400         $self->send(pc18(($self->{isolate} || !$self->user->wantpc9x) ? "" : " pc9x"));
401 }
402
403 #
404 # This is the normal pcxx despatcher
405 #
406 sub normal
407 {
408         my ($self, $line) = @_;
409
410         if ($line =~ '^<\w+\s' && $main::do_xml) {
411                 DXXml::normal($self, $line);
412                 return;
413         }
414
415         my @field = split /\^/, $line;
416         return unless @field;
417
418         pop @field if $field[-1] eq '~';
419
420 #       print join(',', @field), "\n";
421
422
423         # process PC frames, this will fail unless the frame starts PCnn
424         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
425         unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
426                 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
427                 return;
428         }
429
430         # check for and dump bad protocol messages
431         my $n = check($pcno, \@field);
432         if ($n) {
433                 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
434                 return;
435         }
436
437         # modify the hop count here
438         if ($self != $main::me) {
439                 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
440                         $trail ||= '';
441                         $hops--;
442                         return if $hops < 0;
443                         $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
444                         $field[-1] = "H$hops";
445                 }
446         }
447
448         my $origin = $self->{call};
449
450         if (defined &Local::pcprot) {
451                 my $r;
452                 eval { $r = Local::pcprot($self, $pcno, $line,  $origin, \@field); };
453                 return if $r;                   # i.e don't process it
454         }
455
456         # send it out for processing
457         no strict 'subs';
458         my $sub = "handle_$pcno";
459
460         if ($self->can($sub)) {
461                 $self->$sub($pcno, $line, $origin, \@field);
462         } else {
463                 $self->handle_default($pcno, $line, $origin, \@field);
464         }
465 }
466
467 #
468 # This is called from inside the main cluster processing loop and is used
469 # for despatching commands that are doing some long processing job
470 #
471 sub process
472 {
473         my $t = time;
474         my @dxchan = DXChannel::get_all();
475         my $dxchan;
476         my $pc50s;
477
478         # every ten seconds
479         if ($t - $last10 >= 10) {
480                 # clean out ephemera
481
482                 eph_clean();
483                 import_chat();
484
485                 $last10 = $t;
486
487                 # send out a pc50 on EVERY channel all at once
488                 if ($t >= $last_pc50 + $pc50_interval) {
489                         $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
490                         eph_dup($pc50s);
491                         $last_pc50 = $t;
492                 }
493
494                 foreach $dxchan (@dxchan) {
495                         next unless $dxchan->is_node;
496                         next if $dxchan == $main::me;
497
498                         # send the pc50
499                         $dxchan->send($pc50s) if !$dxchan->{do_pc9x} && $pc50s;
500
501                         # send a ping out on this channel
502                         if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
503                                 if ($dxchan->{nopings} <= 0) {
504                                         dbg("ROUTE: $dxchan->{call} disconnected on ping obscount") if isdbg('obscount');
505                                         $dxchan->disconnect;
506                                 } else {
507                                         DXXml::Ping::add($main::me, $dxchan->call);
508                                         $dxchan->{nopings} -= 1;
509                                         $dxchan->{lastping} = $t;
510                                         $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
511                                         dbg("ROUTE: $dxchan->{call} ping obscount = $dxchan->{nopings}") if isdbg('obscount');
512                                 }
513                         }
514                 }
515
516                 clean_pc92_find();
517
518                 # send out config broadcasts
519                 foreach $dxchan (@dxchan) {
520                         next unless $dxchan->is_node;
521
522                         # send out a PC92 config record if required for me and
523                         # all my non pc9x dependent nodes. But for dependent nodes we only do
524                         # this if we have not seen any from anyone else for at least half
525                         # of one update period. This should stop quite a bit of excess C
526                         # records. Someone will win, it does not really matter who, because
527                         # we always believe "us".
528                         if ($main::systime >= $dxchan->{next_pc92_update}) {
529                                 if ($dxchan == $main::me || !$dxchan->{do_pc9x}) {
530                                         dbg("ROUTE: pc92 broadcast candidate: $dxchan->{call}") if isdbg('obscount');
531                                         my $ref = Route::Node::get($dxchan->{call});
532                                         if ($dxchan == $main::me || ($ref && ($ref->measure_pc9x_t($main::systime-$main::systime_daystart)) >= $pc92_extnode_update_period/2)) {
533                                                 $dxchan->broadcast_pc92_update($dxchan->{call});
534                                         } else {
535                                                 $dxchan->update_pc92_next;
536                                         }
537                                 } else {
538                                         $dxchan->update_pc92_next; # this won't actually do anything, it's just to be tidy
539                                 }
540                         }
541                 }
542
543                 # do the keepalive for me, if required
544                 if ($main::systime >= $main::me->{next_pc92_keepalive}) {
545                         time_out_pc92_routes();
546                         $main::me->broadcast_pc92_keepalive($main::mycall);
547                 }
548
549                 if ($pc92_slug_changes && $main::systime >= $last_pc92_slug + $pc92_slug_changes) {
550                         my ($add, $del) = gen_pc92_changes();
551                         $main::me->route_pc92d($main::mycall, undef, $main::routeroot, @$del) if @$del;
552                         $main::me->route_pc92a($main::mycall, undef, $main::routeroot, @$add) if @$add;
553                         clear_pc92_changes();
554                 }
555         }
556
557         if ($main::systime - 3600 > $last_hour) {
558                 $last_hour = $main::systime;
559         }
560
561     pc11_process();
562 }
563
564 #
565 # finish up a pc context
566 #
567
568 #
569 # some active measures
570 #
571
572
573 sub send_dx_spot
574 {
575         my $self = shift;
576         my $line = shift;
577         my @dxchan = DXChannel::get_all();
578         my $dxchan;
579         my $pc11;
580
581         # send it if it isn't the except list and isn't isolated and still has a hop count
582         # taking into account filtering and so on
583         foreach $dxchan (@dxchan) {
584                 next if $dxchan == $main::me;
585                 next if $dxchan == $self && $self->is_node;
586                 next if $dxchan == $self;
587                 next if $dxchan->is_rbn;
588                 if ($line =~ /PC61/ && !($dxchan->is_spider || $dxchan->is_user)) {
589                         unless ($pc11) {
590                                 my @f = split /\^/, $line;
591                                 $pc11 = join '^', 'PC11', @f[1..7,9];
592                         }
593                         $dxchan->dx_spot($pc11, $self->{isolate}, @_, $self->{call});
594                 } else {
595                         $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
596                 }
597         }
598 }
599
600 sub dx_spot
601 {
602         my $self = shift;
603         my $line = shift;
604         my $isolate = shift;
605         my ($filter, $hops);
606
607         if ($self->{spotsfilter}) {
608                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
609                 return unless $filter;
610         }
611         send_prot_line($self, $filter, $hops, $isolate, $line);
612 }
613
614 sub send_prot_line
615 {
616         my ($self, $filter, $hops, $isolate, $line) = @_;
617         my $routeit;
618
619
620         if ($hops) {
621                 $routeit = $line;
622                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
623         } else {
624                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
625                 return unless $routeit;
626         }
627         if ($filter) {
628                 $self->send($routeit);
629         } else {
630                 $self->send($routeit) unless $self->{isolate} || $isolate;
631         }
632 }
633
634
635 sub send_wwv_spot
636 {
637         my $self = shift;
638         my $line = shift;
639         my @dxchan = DXChannel::get_all();
640         my $dxchan;
641         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
642
643         # send it if it isn't the except list and isn't isolated and still has a hop count
644         # taking into account filtering and so on
645         foreach $dxchan (@dxchan) {
646                 next if $dxchan == $main::me;
647                 next if $dxchan == $self && $self->is_node;
648                 next if $dxchan->is_rbn;
649                 my $routeit;
650                 my ($filter, $hops);
651
652                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
653         }
654 }
655
656 sub wwv
657 {
658         my $self = shift;
659         my $line = shift;
660         my $isolate = shift;
661         my ($filter, $hops);
662
663         if ($self->{wwvfilter}) {
664                 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
665                 return unless $filter;
666         }
667         send_prot_line($self, $filter, $hops, $isolate, $line)
668 }
669
670 sub send_wcy_spot
671 {
672         my $self = shift;
673         my $line = shift;
674         my @dxchan = DXChannel::get_all();
675         my $dxchan;
676         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
677
678         # send it if it isn't the except list and isn't isolated and still has a hop count
679         # taking into account filtering and so on
680         foreach $dxchan (@dxchan) {
681                 next if $dxchan == $main::me;
682                 next if $dxchan == $self;
683                 next if $dxchan->is_rbn;
684
685                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
686         }
687 }
688
689 sub wcy
690 {
691         my $self = shift;
692         my $line = shift;
693         my $isolate = shift;
694         my ($filter, $hops);
695
696         if ($self->{wcyfilter}) {
697                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
698                 return unless $filter;
699         }
700         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
701 }
702
703 # send an announce
704 sub send_announce
705 {
706         my $self = shift;
707         my $from_pc9x = shift;
708         my $line = shift;
709         my @dxchan = DXChannel::get_all();
710         my $dxchan;
711         my $target = $_[6];
712         my $to = 'To ';
713         my $text = unpad($_[2]);
714         my $from = $_[0];
715
716         if ($_[3] eq '*') {     # sysops
717                 $target = "SYSOP";
718         } elsif ($_[3] gt ' ') { # speciality list handling
719                 my ($name) = split /\./, $_[3];
720                 $target = "$name"; # put the rest in later (if bothered)
721         }
722
723         if ($_[5] eq '1') {
724                 $target = "WX";
725                 $to = '';
726         }
727         $target = "ALL" if !$target;
728
729
730         # obtain country codes etc
731         my @a = Prefix::cty_data($from);
732         my @b = Prefix::cty_data($_[4]);
733         if ($self->{inannfilter}) {
734                 my ($filter, $hops) =
735                         $self->{inannfilter}->it(@_, $self->{call},
736                                                                          @a[0..2],
737                                                                          @b[0..2], $a[3], $b[3]);
738                 unless ($filter) {
739                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
740                         return;
741                 }
742         }
743
744         # the sysop ('*') thing is an attempt to minimise the damage caused by non-updated PC93 generators
745         if (AnnTalk::dup($from, $target, $_[2]) || ($_[3] eq '*' && AnnTalk::dup($from, 'ALL', $_[2]))) {
746                 my $dxchan = DXChannel::get($from);
747                 if ($self == $main::me && $dxchan && $dxchan->is_user) {
748                         if ($dxchan->priv < 5) {
749                                 $dxchan->send($dxchan->msg('dup'));
750                                 return;
751                         }
752                 } else {
753                         dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
754                         return;
755                 }
756         }
757
758         Log('ann', $target, $from, $text);
759         AnnTalk::add_anncache('ann', $target, $from, $text);
760
761         # send it if it isn't the except list and isn't isolated and still has a hop count
762         # taking into account filtering and so on
763         foreach $dxchan (@dxchan) {
764                 next if $dxchan == $main::me;
765                 next if $dxchan == $self && $self->is_node;
766                 next if $from_pc9x && $dxchan->{do_pc9x};
767                 next if $target eq 'LOCAL' && $dxchan->is_node;
768                 next if $dxchan->is_rbn;
769                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
770                                                   @a[0..2], @b[0..2]);
771         }
772 }
773
774 my $msgid = int rand(1000);
775
776 sub nextchatmsgid
777 {
778         $msgid++;
779         $msgid = 1 if $msgid > 999;
780         return $msgid;
781 }
782
783 # send a chat line
784 sub send_chat
785 {
786         my $self = shift;
787         my $from_pc9x = shift;
788         my $line = shift;
789         my @dxchan = DXChannel::get_all();
790         my $dxchan;
791         my $target = $_[3];
792         my $text = unpad($_[2]);
793         my $ak1a_line;
794         my $from = $_[0];
795
796         # munge the group and recast the line if required
797         if ($target =~ s/\.LST$//) {
798                 $ak1a_line = $line;
799         }
800
801         # obtain country codes etc
802         my @a = Prefix::cty_data($from);
803         my @b = Prefix::cty_data($_[4]);
804         if ($self->{inannfilter}) {
805                 my ($filter, $hops) =
806                         $self->{inannfilter}->it(@_, $self->{call},
807                                                                          @a[0..2],
808                                                                          @b[0..2], $a[3], $b[3]);
809                 unless ($filter) {
810                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
811                         return;
812                 }
813         }
814
815         if (AnnTalk::dup($from, $target, $_[2], $main::systime + $chatdupeage)) {
816                 my $dxchan = DXChannel::get($from);
817                 if ($self == $main::me && $dxchan && $dxchan->is_user) {
818                         if ($dxchan->priv < 5) {
819                                 $dxchan->send($dxchan->msg('dup'));
820                                 return;
821                         }
822                 } else {
823                         dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
824                         return;
825                 }
826         }
827
828
829         Log('chat', $target, $from, $text);
830
831         # send it if it isn't the except list and isn't isolated and still has a hop count
832         # taking into account filtering and so on
833         foreach $dxchan (@dxchan) {
834                 if ($dxchan->is_node) {
835                         next if $dxchan == $main::me;
836                         next if $dxchan == $self;
837                         next if $from_pc9x && $dxchan->do_pc9x;
838                         next unless $dxchan->is_spider && $dxchan->do_pc9x;
839                         next if $target eq 'LOCAL';
840                 }
841                 next if $dxchan->is_rbn;
842
843                 $dxchan->chat($line, $self->{isolate}, $target, $_[1],
844                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
845         }
846 }
847
848 sub announce
849 {
850         my $self = shift;
851         my $line = shift;
852         my $isolate = shift;
853         my $to = shift;
854         my $target = shift;
855         my $text = shift;
856         my ($filter, $hops);
857
858         if ($self->{annfilter}) {
859                 ($filter, $hops) = $self->{annfilter}->it(@_);
860                 return unless $filter;
861         }
862         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
863 }
864
865 sub chat
866 {
867         my $self = shift;
868         my $line = shift;
869         my $isolate = shift;
870         my $to = shift;
871         my $target = shift;
872         my $text = shift;
873         my ($filter, $hops);
874
875         if ($self->{annfilter}) {
876                 ($filter, $hops) = $self->{annfilter}->it(@_);
877                 return unless $filter;
878         }
879         if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
880                 send_prot_line($self, $filter, $hops, $isolate, $line);
881         }
882 }
883
884
885 sub send_local_config
886 {
887         my $self = shift;
888
889         dbg('DXProt::send_local_config') if isdbg('trace');
890
891         # send our nodes
892         my $node;
893         my @nodes;
894         my @localnodes;
895         my @remotenodes;
896
897         if ($self->{isolate}) {
898                 dbg("$self->{call} send_local_config: isolated");
899                 @localnodes = ( $main::routeroot );
900                 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
901         } elsif ($self->{do_pc9x}) {
902                 dbg("$self->{call} send_local_config: doing pc9x");
903                 my $node = Route::Node::get($self->{call});
904 #               $self->send_last_pc92_config($main::routeroot);
905 #               $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
906                 $self->send(pc92a($main::routeroot, $node));
907                 $self->send(pc92k($main::routeroot));
908         } else {
909                 # create a list of all the nodes that are not connected to this connection
910                 # and are not themselves isolated, this to make sure that isolated nodes
911                 # don't appear outside of this node
912
913                 dbg("$self->{call} send_local_config: traditional");
914
915                 # send locally connected nodes
916                 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
917                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
918                 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
919
920                 my $node;
921                 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
922                 my @intcalls;
923                 foreach $node (@rawintcalls) {
924                         push @intcalls, $node if grep $_ && $node != $_, @intcalls;
925                 }
926                 my $ref = Route::Node::get($self->{call});
927                 my @rnodes = $ref->nodes;
928                 foreach $node (@intcalls) {
929                         push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
930                 }
931                 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
932         }
933
934         # get all the users connected on the above nodes and send them out
935         unless ($self->{do_pc9x}) {
936                 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
937                         if ($node) {
938                                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
939                                 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
940                         } else {
941                                 dbg("sent a null value") if isdbg('chanerr');
942                         }
943                 }
944         }
945 }
946
947 sub gen_my_pc92_config
948 {
949         my $node = shift;
950
951         if ($node->{call} eq $main::mycall) {
952                 clear_pc92_changes();           # remove any slugged data, we are generating it as now
953                 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
954                 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
955                 my @localnodes = map { my $r = Route::get($_->{call}); ($_->is_node || $_->is_user) && $r ? $r : () } @dxchan;
956                 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
957                 return pc92c($node, @localnodes);
958         } else {
959                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
960                 return pc92c($node, @rout);
961         }
962 }
963
964 sub send_last_pc92_config
965 {
966         my $self = shift;
967         my $node = shift;
968         if (my $l = $node->last_PC92C) {
969                 $self->send($l);
970         } else {
971                 $self->send_pc92_config($node);
972         }
973 }
974
975 sub send_pc92_config
976 {
977         my $self = shift;
978         my $node = shift;
979
980         dbg('DXProt::send_pc92_config') if isdbg('trace');
981
982         $node->last_PC92C(gen_my_pc92_config($node));
983         $self->send($node->last_PC92C);
984 }
985
986 sub broadcast_pc92_update
987 {
988         my $self = shift;
989         my $call = shift;
990
991         dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
992
993         my $nref = Route::Node::get($call);
994         unless ($nref) {
995                 cluck("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
996                 $self->update_pc92_next;
997                 return;
998         }
999         my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
1000         $nref->lastid(last_pc9x_id());
1001         $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
1002         $self->update_pc92_next;
1003 }
1004
1005 sub broadcast_pc92_keepalive
1006 {
1007         my $self = shift;
1008         my $call = shift;
1009
1010         dbg("ROUTE: broadcast_pc92_keepalive $call") if isdbg('obscount');
1011
1012         my $nref = Route::Node::get($call);
1013         unless ($nref) {
1014                 cluck("ERROR: broadcast_pc92_keepalive - Route::Node $call disappeared");
1015                 $self->update_pc92_keepalive;
1016                 return;
1017         }
1018         my $l = pc92k($nref);
1019         $nref->lastid(last_pc9x_id());
1020         $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
1021         $self->update_pc92_keepalive;
1022 }
1023
1024 sub time_out_pc92_routes
1025 {
1026         my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
1027         my @rdel;
1028         foreach my $n (@nodes) {
1029                 my $o = $n->dec_obs;
1030                 if ($o <= 0) {
1031                         if (my $dxchan = DXChannel::get($n->call)) {
1032                                 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
1033                                 $dxchan->disconnect;
1034                                 next;
1035                         }
1036                         my @parents = map {Route::Node::get($_)} $n->parents;
1037                         for (@parents) {
1038                                 if ($_) {
1039                                         dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount")  if isdbg('obscount');
1040                                         push @rdel, $n->del($_);
1041                                 }
1042                         }
1043                 } else {
1044                         dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
1045                 }
1046         }
1047         for (@rdel) {
1048                 $main::me->route_pc21($main::mycall, undef, $_) if $_;
1049         }
1050 }
1051
1052 #
1053 # route a message down an appropriate interface for a callsign
1054 #
1055 # is called route(to, pcline);
1056 #
1057
1058 sub route
1059 {
1060         my ($self, $call, $line) = @_;
1061
1062         if (ref $self && $call eq $self->{call}) {
1063                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1064                 return;
1065         }
1066
1067         # always send it down the local interface if available
1068         my $dxchan = DXChannel::get($call);
1069         if ($dxchan) {
1070                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
1071         } else {
1072                 my $cl = Route::get($call);
1073                 $dxchan = $cl->dxchan if $cl;
1074                 if (ref $dxchan) {
1075                         if (ref $self && $dxchan eq $self) {
1076                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1077                                 return;
1078                         }
1079                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
1080                 }
1081         }
1082
1083         if ($dxchan) {
1084                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1085                 if ($routeit) {
1086                         $dxchan->send($routeit) unless $dxchan == $main::me;
1087                 }
1088         } else {
1089                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1090         }
1091 }
1092
1093 #
1094 # obtain the hops from the list for this callsign and pc no
1095 #
1096
1097 sub get_hops
1098 {
1099         my $pcno = shift;
1100         my $hops = $DXProt::hopcount{$pcno};
1101         $hops = $DXProt::def_hopcount unless $hops;
1102         return "H$hops";
1103 }
1104
1105 #
1106 # adjust the hop count on a per node basis using the user loadable
1107 # hop table if available or else decrement an existing one
1108 #
1109
1110 sub adjust_hops
1111 {
1112         my $self = shift;
1113         my $s = shift;
1114         my $call = $self->{call};
1115         my $hops;
1116
1117         if (($hops) = $s =~ /\^H([-\d]+)\^?~?$/o) {
1118                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1119                 confess "$call called adjust_hops with '$s'" unless $pcno;
1120                 my $ref = $nodehops{$call} if %nodehops;
1121                 if ($ref) {
1122                         my $newhops = $ref->{$pcno};
1123                         return "" if defined $newhops && $newhops == 0;
1124                         $newhops = $ref->{default} unless $newhops;
1125                         return "" if defined $newhops && $newhops == 0;
1126                         $newhops = $hops unless $newhops;
1127                         return "" unless $newhops > 0;
1128                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops != $hops;
1129                 } else {
1130                         return "" unless $hops > 0;
1131                 }
1132         }
1133         return $s;
1134 }
1135
1136 #
1137 # load hop tables
1138 #
1139 sub load_hops
1140 {
1141         my $self = shift;
1142         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1143         do "$main::data/hop_table.pl";
1144         return $@ if $@;
1145         return ();
1146 }
1147
1148 sub process_rcmd
1149 {
1150         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1151
1152         if ($tonode eq $main::mycall) {
1153                 my $ref = DXUser::get_current($fromnode);
1154                 unless ($ref && UNIVERSAL::isa($ref, 'DXUser')) {
1155                         dbg("DXProt process_rcmd: user $fromnode isn't a reference (check user_asc and tell G1TLH)"); 
1156                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1157                         return;
1158                 }
1159                 Log('rcmd', 'in', ($ref->{priv}||0), $fromnode, $cmd, $user);
1160                 my $cref = Route::Node::get($fromnode);
1161                 unless ($cref && UNIVERSAL::isa($cref, 'Route')) {
1162                         dbg("DXProt process_rcmd: Route $fromnode isn't a reference (tell G1TLH)"); 
1163                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1164                         return;
1165                 }
1166                 if ($cmd !~ /^\s*rcmd/i && $ref->homenode && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1167                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
1168                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1169                                 my $oldpriv = $self->{priv};
1170                                 $self->{priv} = 1; # set a maximum privilege 
1171
1172                                 # park homenode and user for any spawned command that run_cmd may do.
1173                                 $self->{_rcmd_user} = $user;
1174                                 $self->{_rcmd_fromnode} = $fromnode;
1175                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
1176                                 delete $self->{_rcmd_fromnode};
1177                                 delete $self->{_rcmd_user};
1178                                 $self->{priv} = $oldpriv;
1179                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1180                                 delete $self->{remotecmd};
1181                         } else {
1182                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1183                         }
1184                 } else {
1185                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1186                 }
1187         } else {
1188                 my $ref = DXUser::get_current($tonode);
1189                 if ($ref && $ref->is_clx) {
1190                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1191                 } else {
1192                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1193                 }
1194         }
1195 }
1196
1197
1198 sub send_rcmd_reply
1199 {
1200         my $self = shift;
1201         my $tonode = shift;
1202         my $fromnode = shift;
1203         my $user = shift;
1204         while (@_) {
1205                 my $line = shift;
1206                 $line =~ s/\s*$//;
1207                 Log('rcmd', 'out', $fromnode, $line, $user);
1208                 if ($self->is_clx) {
1209                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1210                 } else {
1211                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1212                 }
1213         }
1214 }
1215
1216 # Punt off a long running command into a separate process - this will be caused by an rcmd from outside
1217 #
1218 # This is called from commands to run some potentially long running
1219 # function. The process forks and then runs the function and returns
1220 # the result back to the cmd. 
1221 #
1222 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1223 #       IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER 
1224 #       THE CURRENT CONTEXT!!
1225
1226 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1227 sub spawn_cmd
1228 {
1229         my $self = shift;
1230         my $line = shift;
1231         my $cmdref = shift;
1232         my $call = $self->{call};
1233         my %args = @_;
1234         my @out;
1235         
1236         my $cb = delete $args{cb};
1237         my $prefix = delete $args{prefix};
1238         my $progress = delete $args{progress};
1239         my $args = delete $args{args} || [];
1240         my $t0 = [gettimeofday];
1241
1242         # remembered from process_cmd when spawn_cmd was called thru DXCommandmode::run_cmd which was called by process_rcmd
1243         my $fromnode = $self->{_rcmd_fromnode};
1244         my $user = $self->{_rcmd_user};
1245
1246         no strict 'refs';
1247                 
1248         my $fc = DXSubprocess->new;
1249
1250         # just behave normally if something has set the "one-shot" _nospawn in the channel
1251         if ($self->{_nospawn} || $main::is_win == 1) {
1252                 eval { @out = $cmdref->(@$args); };
1253                 if ($@) {
1254                         DXDebug::dbgprintring(25);
1255                         push @out, DXDebug::shortmess($@);
1256                 }
1257                 return @out;
1258         }
1259
1260         #       $fc->serializer(\&encode_json);
1261 #       $fc->deserializer(\&decode_json);
1262         $fc->run(
1263                          sub {
1264                                  my $subpro = shift;
1265                                  if (isdbg('progress')) {
1266                                          my $s = qq{line: "$line"};
1267                                          $s .= ", args: " . join(', ', map { defined $_ ? qq{'$_'} : q{'undef'} } @$args) if $args && @$args;
1268                                          dbg($s);
1269                                  }
1270                                  eval { @out = $cmdref->(@$args); };
1271                                  if ($@) {
1272                                          DXDebug::dbgprintring(25);
1273                                          push @out, DXDebug::shortmess($@);
1274                                  }
1275                                  return @out;
1276                          },
1277 #                        $args,
1278                          sub {
1279                                  my ($fc, $err, @res) = @_; 
1280                                  my $self = DXChannel::get($call);
1281                                  return unless $self;
1282
1283                                  if ($err) {
1284                                          my $s = "DXCommand::spawn_cmd: call $call error $err";
1285                                          dbg($s) if isdbg('chan');
1286                                          if ($fromnode && $user) {
1287                                                  $self->send_rcmd_reply($main::mycall, $fromnode, $user, $s);
1288                                          } else {
1289                                                  $self->send($s);
1290                                          }
1291                                          return;
1292                                  }
1293                                  if ($cb) {
1294                                          # transform output if required
1295                                          @res = $cb->($self, @res);
1296                                  }
1297                                  if (@res) {
1298                                          if ($fromnode && $user) {
1299                                                  $self->send_rcmd_reply($main::mycall, $fromnode, $user, @res);
1300                                          } else {
1301                                                  $self->send(@res);
1302                                          }
1303                                  }
1304                                  diffms("rcmd from $user on $call", $line, $t0, scalar @res) if isdbg('progress');
1305                          });
1306         
1307         return @out;
1308 }
1309
1310 sub process_rcmd_reply
1311 {
1312         my ($self, $tonode, $fromnode, $user, $line) = @_;
1313         if ($tonode eq $main::mycall) {
1314                 my $s = $rcmds{$fromnode};
1315                 if ($s) {
1316                         my $dxchan = DXChannel::get($s->{call});
1317                         my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1318                         $ref->send($line) if $ref;
1319                         delete $rcmds{$fromnode} if !$dxchan;
1320                 } else {
1321                         # send unsolicited ones to the sysop
1322                         my $dxchan = DXChannel::get($main::myalias);
1323                         $dxchan->send($line) if $dxchan;
1324                 }
1325         } else {
1326                 my $ref = DXUser::get_current($tonode);
1327                 if ($ref && $ref->is_clx) {
1328                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1329                 } else {
1330                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1331                 }
1332         }
1333 }
1334
1335
1336
1337 # add a rcmd request to the rcmd queues
1338 sub addrcmd
1339 {
1340         my ($self, $to, $cmd) = @_;
1341
1342         my $r = {};
1343         $r->{call} = $self->{call};
1344         $r->{t} = $main::systime;
1345         $r->{cmd} = $cmd;
1346         $rcmds{$to} = $r;
1347
1348         my $ref = Route::Node::get($to);
1349         my $dxchan = $ref->dxchan;
1350         if ($dxchan && $dxchan->is_clx) {
1351                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1352         } else {
1353                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1354         }
1355 }
1356
1357 sub disconnect
1358 {
1359         my $self = shift;
1360         my $pc39flag = shift || 0;
1361         my $call = $self->call;
1362
1363         return if $self->{disconnecting}++;
1364
1365         unless ($pc39flag == 1) {
1366                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1367         }
1368
1369         # get rid of any PC16/17/19
1370         eph_del_regex("^PC1[679]*$call");
1371
1372         # do routing stuff, remove me from routing table
1373         my $node = Route::Node::get($call);
1374
1375         Route::delete_interface($call);
1376
1377         # unbusy and stop and outgoing mail
1378         my $mref = DXMsg::get_busy($call);
1379         $mref->stop_msg($call) if $mref;
1380
1381         # remove outstanding pings
1382         delete $pings{$call};
1383
1384         # I was the last node visited
1385     $self->user->node($main::mycall);
1386
1387         # send info to all logged in thingies
1388         $self->tell_login('logoutn');
1389
1390         Log('DXProt', $call . " Disconnected");
1391
1392         $self->SUPER::disconnect;
1393
1394         # here we determine what needs to go out of the routing table
1395         my @rout;
1396         if ($node && $pc39flag != 2) {
1397                 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1398
1399                 @rout = $node->del($main::routeroot);
1400
1401                 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1402
1403                 # now we need to see what can't be routed anymore and came
1404                 # in via this node (probably).
1405                 my $n = 0;
1406                 while ($n != @rout) {
1407                         $n = @rout;
1408                         for (Route::Node::get_all()) {
1409                                 unless ($_->dxchan) {
1410                                         push @rout, $_->delete;
1411                                 }
1412                         }
1413                         dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1414                 }
1415
1416                 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1417
1418                 # and all my ephemera as well
1419                 for (@rout) {
1420                         my $c = $_->call;
1421                         eph_del_regex("^PC1[679].*$c");
1422                 }
1423         }
1424
1425         # broadcast to all other nodes that all the nodes connected to via me are gone
1426         unless ($pc39flag == 2)  {
1427                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1428                 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1429         }
1430 }
1431
1432
1433 #
1434 # send a talk message to this thingy
1435 #
1436 sub talk
1437 {
1438         my ($self, $from, $to, $via, $line, $origin) = @_;
1439
1440         my $ipaddr = DXCommandmode::alias_localhost($self->hostname || '127.0.0.1');
1441         if ($self->{do_pc9x}) {
1442                 $self->send(pc93($to, $from, $via, $line, undef, $ipaddr));
1443         } else {
1444                 $self->send(pc10($from, $to, $via, $line, $origin));
1445         }
1446         Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line, $ipaddr) unless $origin && $origin ne $main::mycall;
1447 }
1448
1449 # send it if it isn't the except list and isn't isolated and still has a hop count
1450 # taking into account filtering and so on
1451
1452 sub send_route
1453 {
1454         my $self = shift;
1455         my $origin = shift;
1456         my $generate = shift;
1457         my $no = shift;     # the no of things to filter on
1458         my $routeit;
1459         my ($filter, $hops);
1460         my @rin;
1461
1462         for (; @_ && $no; $no--) {
1463                 my $r = shift;
1464
1465                 # don't send messages with $self's call in back to them
1466                 if ($r->call eq $self->{call}) {
1467                         dbg("PCPROT: trying to send $self->{call} back itself") if isdbg('chanerr');
1468                         next;
1469                 }
1470
1471                 if (!$self->{isolate} && $self->{routefilter}) {
1472                         $filter = undef;
1473                         if ($r) {
1474                                 ($filter, $hops) = $self->{routefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq, $self->{state}, $r->{state});
1475                                 if ($filter) {
1476                                         push @rin, $r;
1477                                 } else {
1478                                         dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1479                                 }
1480                         } else {
1481                                 dbg("was sent a null value") if isdbg('chanerr');
1482                         }
1483                 } else {
1484                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1485                 }
1486         }
1487         if (@rin) {
1488                 foreach my $line (&$generate(@rin, @_)) {
1489                         if ($hops) {
1490                                 $routeit = $line;
1491                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1492                         } else {
1493                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1494                                 next unless $routeit;
1495                         }
1496
1497                         $self->send($routeit);
1498                 }
1499         }
1500 }
1501
1502 # broadcast everywhere
1503 sub broadcast_route
1504 {
1505         my $self = shift;
1506         my $origin = shift;
1507         my $generate = shift;
1508         my $line = shift;
1509         my @dxchan = DXChannel::get_all_nodes();
1510         my $dxchan;
1511
1512         if ($line) {
1513                 $line =~ /\^H(\d+)\^?\~?$/;
1514                 return unless $1 > 0;
1515         }
1516         unless ($self->{isolate}) {
1517                 foreach $dxchan (@dxchan) {
1518                         next if $dxchan == $self || $dxchan == $main::me;
1519                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1520                         next unless $dxchan->isa('DXProt');
1521
1522                         $dxchan->send_route($origin, $generate, @_);
1523                 }
1524         }
1525 }
1526
1527 # broadcast to non-pc9x nodes
1528 sub broadcast_route_nopc9x
1529 {
1530         my $self = shift;
1531         my $origin = shift;
1532         my $generate = shift;
1533         my $line = shift;
1534         my @dxchan = DXChannel::get_all_nodes();
1535         my $dxchan;
1536
1537         if ($line) {
1538                 $line =~ /\^H(\d+)\^?\~?$/;
1539                 return unless $1 > 0;
1540         }
1541         unless ($self->{isolate}) {
1542                 foreach $dxchan (@dxchan) {
1543                         next if $dxchan == $self || $dxchan == $main::me;
1544                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1545                         next unless $dxchan->isa('DXProt');
1546                         next if $dxchan->{do_pc9x};
1547                         if ($generate == \&pc16 || $generate==\&pc17) {
1548                                 next unless $dxchan->user->wantsendpc16;
1549                         }
1550                         $dxchan->send_route($origin, $generate, @_);
1551                 }
1552         }
1553 }
1554
1555 # this is only used for next door nodes on init
1556 sub send_route_pc92
1557 {
1558         my $self = shift;
1559
1560         return unless $self->{do_pc9x};
1561
1562         my $origin = shift;
1563         my $generate = shift;
1564         my $no = shift;     # the no of things to filter on
1565         my $line;
1566
1567         $line = &$generate(@_);
1568         $self->send($line);
1569 }
1570
1571 # broadcast only to pc9x nodes
1572 sub broadcast_route_pc9x
1573 {
1574         my $self = shift;
1575         my $origin = shift;
1576         my $generate = shift;
1577         my $line = shift;
1578         my $no = shift;
1579         my @dxchan = DXChannel::get_all_nodes();
1580         my $dxchan;
1581
1582         if ($origin eq $main::mycall && $generate && !$line) {
1583                 $line = &$generate(@_);
1584         }
1585
1586         $line =~ /\^H(\d+)\^\~?$/;
1587         unless ($1 > 0 && $self->{isolate}) {
1588                 foreach $dxchan (@dxchan) {
1589                         next if $dxchan == $self || $dxchan == $main::me;
1590                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1591                         next unless $dxchan->isa('DXProt');
1592                         next unless $dxchan->{do_pc9x};
1593
1594                         $dxchan->send($line);
1595                 }
1596         }
1597 }
1598
1599 sub route_pc16
1600 {
1601         my $self = shift;
1602         return unless $self->user->wantpc16;
1603         my $origin = shift;
1604         my $line = shift;
1605         broadcast_route_nopc9x($self, $origin, \&pc16, $line, 1, @_);
1606 }
1607
1608 sub route_pc17
1609 {
1610         my $self = shift;
1611         return unless $self->user->wantpc16;
1612         my $origin = shift;
1613         my $line = shift;
1614         broadcast_route_nopc9x($self, $origin, \&pc17, $line, 1, @_);
1615 }
1616
1617 sub route_pc19
1618 {
1619         my $self = shift;
1620         my $origin = shift;
1621         my $line = shift;
1622         broadcast_route_nopc9x($self, $origin, \&pc19, $line, scalar @_, @_);
1623 }
1624
1625 sub route_pc21
1626 {
1627         my $self = shift;
1628         my $origin = shift;
1629         my $line = shift;
1630         broadcast_route_nopc9x($self, $origin, \&pc21, $line, scalar @_, @_);
1631 }
1632
1633 sub route_pc24
1634 {
1635         my $self = shift;
1636         my $origin = shift;
1637         my $line = shift;
1638         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1639 }
1640
1641 sub route_pc41
1642 {
1643         my $self = shift;
1644         my $origin = shift;
1645         my $line = shift;
1646         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1647 }
1648
1649 # this is probably obsolete now
1650 sub route_pc50
1651 {
1652         my $self = shift;
1653         my $origin = shift;
1654         my $line = shift;
1655
1656         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1657 }
1658
1659 sub route_pc92c
1660 {
1661         my $self = shift;
1662         my $origin = shift;
1663         my $line = shift;
1664
1665         broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1666 }
1667
1668 sub route_pc92a
1669 {
1670         my $self = shift;
1671         my $origin = shift;
1672         my $line = shift;
1673         return unless $pc92_ad_enabled;
1674         broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1675 }
1676
1677 sub route_pc92d
1678 {
1679         my $self = shift;
1680         my $origin = shift;
1681         my $line = shift;
1682         return unless $pc92_ad_enabled;
1683         broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1684 }
1685
1686 sub in_filter_route
1687 {
1688         my $self = shift;
1689         my $r = shift;
1690         my ($filter, $hops) = (1, 1);
1691
1692         if ($self->{inroutefilter}) {
1693                 ($filter, $hops) = $self->{inroutefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq, $self->state, $r->state);
1694                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1695         }
1696         return $filter;
1697 }
1698
1699 sub eph_dup
1700 {
1701         my $s = shift;
1702         my $t = shift || $eph_restime;
1703         my $r;
1704
1705         # chop the end off
1706         $s =~ s/\^H\d\d?\^?\~?$//;
1707         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
1708         $eph{$s} = $main::systime + $t;
1709         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1710         return $r;
1711 }
1712
1713 sub eph_del_regex
1714 {
1715         my $regex = shift;
1716         my ($key, $val);
1717         while (($key, $val) = each %eph) {
1718                 if ($key =~ m{$regex}) {
1719                         delete $eph{$key};
1720                 }
1721         }
1722 }
1723
1724 sub eph_clean
1725 {
1726         my ($key, $val);
1727
1728         while (($key, $val) = each %eph) {
1729                 if ($main::systime >= $val) {
1730                         delete $eph{$key};
1731                 }
1732         }
1733 }
1734
1735 sub eph_list
1736 {
1737         my ($key, $val);
1738         my @out;
1739
1740         while (($key, $val) = each %eph) {
1741                 push @out, $key, $val;
1742         }
1743         return @out;
1744 }
1745
1746 sub run_cmd
1747 {
1748         goto &DXCommandmode::run_cmd;
1749 }
1750
1751
1752 # import any msgs in the chat directory
1753 # the messages are sent to the chat group which forms the
1754 # the first part of the name (eg: solar.1243.txt would be
1755 # sent to chat group SOLAR)
1756 #
1757 # Each message found is sent: one non-blank line to one chat
1758 # message. So 4 lines = 4 chat messages.
1759 #
1760 # The special name LOCAL is for local users ANN
1761 # The special name ALL is for ANN/FULL
1762 # The special name SYSOP is for ANN/SYSOP
1763 #
1764 sub import_chat
1765 {
1766         # are there any to do in this directory?
1767         return unless -d $chatimportfn;
1768         unless (opendir(DIR, $chatimportfn)) {
1769                 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1770                 Log('msg', "can\'t open $chatimportfn $!");
1771                 return;
1772         }
1773
1774         my @names = readdir(DIR);
1775         closedir(DIR);
1776         my $name;
1777         foreach $name (@names) {
1778                 next if $name =~ /^\./;
1779                 my $splitit = $name =~ /^split/;
1780                 my $fn = "$chatimportfn/$name";
1781                 next unless -f $fn;
1782                 unless (open(MSG, $fn)) {
1783                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1784                         Log('msg', "can\'t open import file $fn $!");
1785                         unlink($fn);
1786                         next;
1787                 }
1788                 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1789                 close(MSG);
1790                 unlink($fn);
1791
1792                 my @cat = split /\./, $name;
1793                 my $target = uc $cat[0];
1794
1795                 foreach my $text (@msg) {
1796                         next unless $text && $text !~ /^\s*#/;
1797                         if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP' || $target eq 'WX') {
1798                                 my $sysop = uc $target eq 'SYSOP' ? '*' : ' ';
1799                                 my $wx = uc $target eq 'WX' ? '1' : '0';
1800                                 my $via = $target;
1801                                 $via = '*' if $target eq 'ALL' || $target eq 'SYSOP';
1802                                 Log('ann', $target, $main::mycall, $text);
1803                                 AnnTalk::add_anncache('ann', $target, $main::mycall, $text);
1804                                 my $ipaddr = DXCommandmode::alias_localhost($main::me->hostname || '127.0.0.1');
1805                                 $main::me->normal(DXProt::pc93($target, $main::mycall, $via, $text, undef, $ipaddr));
1806                         } else {
1807                                 DXCommandmode::send_chats($main::me, $target, $text);
1808                         }
1809                 }
1810         }
1811 }
1812
1813 # start a pc92 find operation
1814 sub start_pc92_find
1815 {
1816         my $dxchan = shift;
1817         my $target = shift;
1818         my $key = "$dxchan->{call}|$target";
1819         if ($pc92_find{$key}) {
1820
1821         }
1822 }
1823
1824 # function (not method) to handle pc92 find returns
1825 sub handle_pc92_find_reply
1826 {
1827         my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1828
1829         $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');
1830 }
1831
1832 sub clean_pc92_find
1833 {
1834
1835 }
1836
1837
1838
1839 1;
1840 __END__