try to sort out PC16 so that local connections override and update remote
[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 # $Id$
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 RouteDB;
36 use DXProtHandle;
37
38 use strict;
39
40 use vars qw($VERSION $BRANCH);
41 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
42                         $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
43                         $pingint $obscount %pc19list $chatdupeage $chatimportfn
44                         $investigation_int $pc19_version $myprot_version
45                         %nodehops $baddx $badspotter $badnode $censorpc $rspfcheck
46                         $allowzero $decode_dk0wcy $send_opernam @checklist
47                         $eph_pc15_restime $pc92_update_period $last_pc92_update
48                    );
49
50 ($VERSION, $BRANCH) = dxver(q$Revision$);
51
52 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
53 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
54
55 $last_hour = time;                              # last time I did an hourly periodic update
56 %rcmds = ();                    # outstanding rcmd requests outbound
57 %nodehops = ();                 # node specific hop control
58 %pc19list = ();                                 # list of outstanding PC19s that haven't had PC16s on them
59
60 $censorpc = 1;                                  # Do a BadWords::check on text fields and reject things
61                                                                 # loads of 'bad things'
62 $baddx = new DXHash "baddx";
63 $badspotter = new DXHash "badspotter";
64 $badnode = new DXHash "badnode";
65 $last10 = $last_pc50 = time;
66 $ann_to_talk = 1;
67 $rspfcheck = 1;
68 $eph_restime = 180;
69 $eph_info_restime = 60*60;
70 $eph_pc15_restime = 6*60;
71 $eph_pc34_restime = 30;
72 $pingint = 5*60;
73 $obscount = 2;
74 $chatdupeage = 20 * 60 * 60;
75 $chatimportfn = "$main::root/chat_import";
76 $investigation_int = 12*60*60;  # time between checks to see if we can see this node
77 $pc19_version = 5466;                   # the visible version no for outgoing PC19s generated from pc59
78 $pc92_update_period = 30*60;    # the period between PC92 C updates
79 $last_pc92_update = time + int rand(180);               # the last time a PC92 config update
80
81
82 @checklist = 
83 (
84  [ qw(i c c m bp bc c) ],                       # pc10
85  [ qw(i f m d t m c c h) ],             # pc11
86  [ qw(i c bm m bm bm p h) ],            # pc12
87  [ qw(i c h) ],                                 # 
88  [ qw(i c h) ],                                 # 
89  [ qw(i c m h) ],                                       # 
90  undef ,                                                # pc16 has to be validated manually
91  [ qw(i c c h) ],                                       # pc17
92  [ qw(i m n) ],                                 # pc18
93  undef ,                                                # pc19 has to be validated manually
94  undef ,                                                # pc20 no validation
95  [ qw(i c m h) ],                                       # pc21
96  undef ,                                                # pc22 no validation
97  [ qw(i d n n n n m c c h) ],           # pc23
98  [ qw(i c p h) ],                                       # pc24
99  [ qw(i c c n n) ],                             # pc25
100  [ qw(i f m d t m c c bc) ],            # pc26
101  [ qw(i d n n n n m c c bc) ],  # pc27
102  [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
103  [ qw(i c c n m) ],                             # pc29
104  [ qw(i c c n) ],                                       # pc30
105  [ qw(i c c n) ],                                       # pc31
106  [ qw(i c c n) ],                                       # pc32
107  [ qw(i c c n) ],                                       # pc33
108  [ qw(i c c m) ],                                       # pc34
109  [ qw(i c c m) ],                                       # pc35
110  [ qw(i c c m) ],                                       # pc36
111  [ qw(i c c n m) ],                             # pc37
112  undef,                                                 # pc38 not interested
113  [ qw(i c m) ],                                 # pc39
114  [ qw(i c c m p n) ],                           # pc40
115  [ qw(i c n m h) ],                             # pc41
116  [ qw(i c c n) ],                                       # pc42
117  undef,                                                 # pc43 don't handle it
118  [ qw(i c c n m m c) ],                 # pc44
119  [ qw(i c c n m) ],                             # pc45
120  [ qw(i c c n) ],                                       # pc46
121  undef,                                                 # pc47
122  undef,                                                 # pc48
123  [ qw(i c m h) ],                                       # pc49
124  [ qw(i c n h) ],                                       # pc50
125  [ qw(i c c n) ],                                       # pc51
126  undef,
127  undef,
128  undef,
129  undef,
130  undef,
131  undef,
132  undef,
133  undef,
134  undef,                                                 # pc60
135  undef,
136  undef,
137  undef,
138  undef,
139  undef,
140  undef,
141  undef,
142  undef,
143  undef,
144  undef,                                                 # pc70
145  undef,
146  undef,
147  [ qw(i d n n n n n n m m m c c h) ],   # pc73
148  undef,
149  undef,
150  undef,
151  undef,
152  undef,
153  undef,
154  undef,                                                 # pc80
155  undef,
156  undef,
157  undef,
158  [ qw(i c c c m) ],                             # pc84
159  [ qw(i c c c m) ],                             # pc85
160  undef,
161  undef,
162  undef,
163  undef,
164  [ qw(i c n) ],                                 # pc90
165 );
166
167 # use the entry in the check list to check the field list presented
168 # return OK if line NOT in check list (for now)
169 sub check
170 {
171         my $n = shift;
172         $n -= 10;
173         return 0 if $n < 0 || $n > @checklist; 
174         my $ref = $checklist[$n];
175         return 0 unless ref $ref;
176         
177         my $i;
178         for ($i = 1; $i < @$ref; $i++) {
179                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
180                 return 0 unless $act;
181                 next if $blank && $_[$i] =~ /^[ \*]$/;
182                 if ($act eq 'c') {
183                         return $i unless is_callsign($_[$i]);
184                 } elsif ($act eq 'i') {                 
185                         ;                                       # do nothing
186                 } elsif ($act eq 'm') {
187                         return $i unless is_pctext($_[$i]);
188                 } elsif ($act eq 'p') {
189                         return $i unless is_pcflag($_[$i]);
190                 } elsif ($act eq 'f') {
191                         return $i unless is_freq($_[$i]);
192                 } elsif ($act eq 'n') {
193                         return $i unless $_[$i] =~ /^[\d ]+$/;
194                 } elsif ($act eq 'h') {
195                         return $i unless $_[$i] =~ /^H\d\d?$/;
196                 } elsif ($act eq 'd') {
197                         return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
198                 } elsif ($act eq 't') {
199                         return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
200                 } 
201         }
202         return 0;
203 }
204
205 sub init
206 {
207         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
208         confess $@ if $@;
209
210         my $user = DXUser->get($main::mycall);
211         die "User $main::mycall not setup or disappeared RTFM" unless $user;
212         
213         $myprot_version += $main::version*100;
214         $main::me = DXProt->new($main::mycall, 0, $user); 
215         $main::me->{here} = 1;
216         $main::me->{state} = "indifferent";
217         $main::me->{sort} = 'S';    # S for spider
218         $main::me->{priv} = 9;
219         $main::me->{metric} = 0;
220         $main::me->{pingave} = 0;
221         $main::me->{registered} = 1;
222         $main::me->{version} = $main::version;
223         $main::me->{build} = $main::build;
224         $main::me->{do_pc92} = 1;
225         $main::me->{via_pc92} = 1;
226 }
227
228 #
229 # obtain a new connection this is derived from dxchannel
230 #
231
232 sub new 
233 {
234         my $self = DXChannel::alloc(@_);
235
236         # add this node to the table, the values get filled in later
237         my $pkg = shift;
238         my $call = shift;
239
240         # if we have an entry already, then send a PC21 to all connect
241         # old style connections, because we are about to get the real deal
242         if (my $ref = Route::Node::get($call)) {
243                 dbg("ROUTE: $call is already in the routing table, deleting") if isdbg('route');
244                 my @rout = $ref->delete;
245                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
246         }
247         $main::routeroot->add($call, '5000', Route::here(1)) if $call ne $main::mycall;
248
249         return $self;
250 }
251
252 # this is how a pc connection starts (for an incoming connection)
253 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
254 # all the crap that comes between).
255 sub start
256 {
257         my ($self, $line, $sort) = @_;
258         my $call = $self->{call};
259         my $user = $self->{user};
260
261         # log it
262         my $host = $self->{conn}->{peerhost};
263         $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
264         $host ||= "unknown";
265
266         Log('DXProt', "$call connected from $host");
267         
268         # remember type of connection
269         $self->{consort} = $line;
270         $self->{outbound} = $sort eq 'O';
271         my $priv = $user->priv;
272         $priv = $user->priv(1) unless $priv;
273         $self->{priv} = $priv;     # other clusters can always be 'normal' users
274         $self->{lang} = $user->lang || 'en';
275         $self->{isolate} = $user->{isolate};
276         $self->{consort} = $line;       # save the connection type
277         $self->{here} = 1;
278         $self->{width} = 80;
279
280         # sort out registration
281         $self->{registered} = 1;
282
283         # get the output filters
284         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
285         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
286         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
287         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
288         $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
289
290
291         # get the INPUT filters (these only pertain to Clusters)
292         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
293         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
294         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
295         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
296         $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
297         
298         # set unbuffered and no echo
299         $self->send_now('B',"0");
300         $self->send_now('E',"0");
301         $self->conn->echo(0) if $self->conn->can('echo');
302         
303         # ping neighbour node stuff
304         my $ping = $user->pingint;
305         $ping = $pingint unless defined $ping;
306         $self->{pingint} = $ping;
307         $self->{nopings} = $user->nopings || $obscount;
308         $self->{pingtime} = [ ];
309         $self->{pingave} = 999;
310         $self->{metric} ||= 100;
311         $self->{lastping} = $main::systime;
312         
313         # send initialisation string
314         unless ($self->{outbound}) {
315                 $self->sendinit;
316         }
317         
318         $self->state('init');
319         $self->{pc50_t} = $main::systime;
320
321         # send info to all logged in thingies
322         $self->tell_login('loginn');
323
324         # run a script send the output to the debug file
325         my $script = new Script(lc $call) || new Script('node_default');
326         $script->run($self) if $script;
327 }
328
329 #
330 # send outgoing 'challenge'
331 #
332
333 sub sendinit
334 {
335         my $self = shift;
336         $self->send(pc18());
337 }
338
339 #
340 # This is the normal pcxx despatcher
341 #
342 sub normal
343 {
344         my ($self, $line) = @_;
345
346         if ($line =~ '^<\w+\s' && $main::do_xml) {
347                 DXXml::normal($self, $line);
348                 return;
349         }
350
351         my @field = split /\^/, $line;
352         return unless @field;
353         
354         pop @field if $field[-1] eq '~';
355         
356 #       print join(',', @field), "\n";
357                                                 
358         
359         # process PC frames, this will fail unless the frame starts PCnn
360         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
361         unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
362                 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
363                 return;
364         }
365
366         # check for and dump bad protocol messages
367         my $n = check($pcno, @field);
368         if ($n) {
369                 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
370                 return;
371         }
372
373         # modify the hop count here
374         if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
375                 $trail ||= '';
376                 $hops--;
377                 return if $hops < 0;
378                 $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
379                 $field[-1] = "H$hops";
380         }
381
382         # send it out for processing
383         my $origin = $self->{call};
384         no strict 'subs';
385         my $sub = "handle_$pcno";
386
387         if ($self->can($sub)) {
388                 $self->$sub($pcno, $line, $origin, @field);
389         } else {
390                 $self->handle_default($pcno, $line, $origin, @field);
391         }
392 }
393
394 #
395 # This is called from inside the main cluster processing loop and is used
396 # for despatching commands that are doing some long processing job
397 #
398 sub process
399 {
400         my $t = time;
401         my @dxchan = DXChannel::get_all();
402         my $dxchan;
403         my $pc50s;
404         
405         # send out a pc50 on EVERY channel all at once
406         if ($t >= $last_pc50 + $DXProt::pc50_interval) {
407                 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
408                 eph_dup($pc50s);
409                 $last_pc50 = $t;
410         }
411
412         foreach $dxchan (@dxchan) {
413                 next unless $dxchan->is_node;
414                 next if $dxchan->handle_xml;
415                 next if $dxchan == $main::me;
416
417                 # send the pc50
418                 $dxchan->send($pc50s) if $pc50s;
419                 
420                 # send a ping out on this channel
421                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
422                         if ($dxchan->{nopings} <= 0) {
423                                 $dxchan->disconnect;
424                         } else {
425                                 DXXml::Ping::add($main::me, $dxchan->call);
426                                 $dxchan->{nopings} -= 1;
427                                 $dxchan->{lastping} = $t;
428                                 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
429                         }
430                 }
431         }
432
433         Investigate::process();
434
435         # every ten seconds
436         if ($t - $last10 >= 10) {       
437                 # clean out ephemera 
438
439                 eph_clean();
440                 import_chat();
441
442                 if ($main::systime >= $last_pc92_update + $pc92_update_period) {
443                         dbg("ROUTE: sending pc92 update") if isdbg('route');
444                         send_pc92_update();
445                         time_out_pc92_routes();
446                         $last_pc92_update = $main::systime + int rand(180);
447                 }
448                 
449                 $last10 = $t;
450         }
451         
452         if ($main::systime - 3600 > $last_hour) {
453                 $last_hour = $main::systime;
454         }
455 }
456
457 #
458 # finish up a pc context
459 #
460
461 #
462 # some active measures
463 #
464
465
466 sub send_dx_spot
467 {
468         my $self = shift;
469         my $line = shift;
470         my @dxchan = DXChannel::get_all();
471         my $dxchan;
472         
473         # send it if it isn't the except list and isn't isolated and still has a hop count
474         # taking into account filtering and so on
475         foreach $dxchan (@dxchan) {
476                 next if $dxchan == $main::me;
477                 next if $dxchan == $self && $self->is_node;
478                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
479         }
480 }
481
482 sub dx_spot
483 {
484         my $self = shift;
485         my $line = shift;
486         my $isolate = shift;
487         my ($filter, $hops);
488
489         if ($self->{spotsfilter}) {
490                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
491                 return unless $filter;
492         }
493         send_prot_line($self, $filter, $hops, $isolate, $line);
494 }
495
496 sub send_prot_line
497 {
498         my ($self, $filter, $hops, $isolate, $line) = @_;
499         my $routeit;
500
501
502         if ($hops) {
503                 $routeit = $line;
504                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
505         } else {
506                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
507                 return unless $routeit;
508         }
509         if ($filter) {
510                 $self->send($routeit);
511         } else {
512                 $self->send($routeit) unless $self->{isolate} || $isolate;
513         }
514 }
515
516
517 sub send_wwv_spot
518 {
519         my $self = shift;
520         my $line = shift;
521         my @dxchan = DXChannel::get_all();
522         my $dxchan;
523         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
524
525         # send it if it isn't the except list and isn't isolated and still has a hop count
526         # taking into account filtering and so on
527         foreach $dxchan (@dxchan) {
528                 next if $dxchan == $main::me;
529                 next if $dxchan == $self && $self->is_node;
530                 my $routeit;
531                 my ($filter, $hops);
532
533                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
534         }
535 }
536
537 sub wwv
538 {
539         my $self = shift;
540         my $line = shift;
541         my $isolate = shift;
542         my ($filter, $hops);
543         
544         if ($self->{wwvfilter}) {
545                 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
546                 return unless $filter;
547         }
548         send_prot_line($self, $filter, $hops, $isolate, $line)
549 }
550
551 sub send_wcy_spot
552 {
553         my $self = shift;
554         my $line = shift;
555         my @dxchan = DXChannel::get_all();
556         my $dxchan;
557         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
558         
559         # send it if it isn't the except list and isn't isolated and still has a hop count
560         # taking into account filtering and so on
561         foreach $dxchan (@dxchan) {
562                 next if $dxchan == $main::me;
563                 next if $dxchan == $self;
564
565                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
566         }
567 }
568
569 sub wcy
570 {
571         my $self = shift;
572         my $line = shift;
573         my $isolate = shift;
574         my ($filter, $hops);
575
576         if ($self->{wcyfilter}) {
577                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
578                 return unless $filter;
579         }
580         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
581 }
582
583 # send an announce
584 sub send_announce
585 {
586         my $self = shift;
587         my $line = shift;
588         my @dxchan = DXChannel::get_all();
589         my $dxchan;
590         my $target;
591         my $to = 'To ';
592         my $text = unpad($_[2]);
593                                 
594         if ($_[3] eq '*') {     # sysops
595                 $target = "SYSOP";
596         } elsif ($_[3] gt ' ') { # speciality list handling
597                 my ($name) = split /\./, $_[3]; 
598                 $target = "$name"; # put the rest in later (if bothered) 
599         } 
600         
601         if ($_[5] eq '1') {
602                 $target = "WX"; 
603                 $to = '';
604         }
605         $target = "ALL" if !$target;
606
607
608         # obtain country codes etc 
609         my @a = Prefix::cty_data($_[0]);
610         my @b = Prefix::cty_data($_[4]);
611         if ($self->{inannfilter}) {
612                 my ($filter, $hops) = 
613                         $self->{inannfilter}->it(@_, $self->{call}, 
614                                                                          @a[0..2],
615                                                                          @b[0..2], $a[3], $b[3]);
616                 unless ($filter) {
617                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
618                         return;
619                 }
620         }
621
622         if (AnnTalk::dup($_[0], $_[1], $_[2])) {
623                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
624                 return;
625         }
626
627         Log('ann', $target, $_[0], $text);
628
629         # send it if it isn't the except list and isn't isolated and still has a hop count
630         # taking into account filtering and so on
631         foreach $dxchan (@dxchan) {
632                 next if $dxchan == $main::me;
633                 next if $dxchan == $self && $self->is_node;
634                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
635                                                   @a[0..2], @b[0..2]);
636         }
637 }
638
639 my $msgid = 0;
640
641 sub nextchatmsgid
642 {
643         $msgid++;
644         $msgid = 1 if $msgid > 999;
645         return $msgid;
646 }
647
648 # send a chat line
649 sub send_chat
650 {
651         my $self = shift;
652         my $line = shift;
653         my @dxchan = DXChannel::get_all();
654         my $dxchan;
655         my $target = $_[3];
656         my $text = unpad($_[2]);
657         my $ak1a_line;
658                                 
659         # munge the group and recast the line if required
660         if ($target =~ s/\.LST$//) {
661                 $ak1a_line = $line;
662         }
663         
664         # obtain country codes etc 
665         my @a = Prefix::cty_data($_[0]);
666         my @b = Prefix::cty_data($_[4]);
667         if ($self->{inannfilter}) {
668                 my ($filter, $hops) = 
669                         $self->{inannfilter}->it(@_, $self->{call}, 
670                                                                          @a[0..2],
671                                                                          @b[0..2], $a[3], $b[3]);
672                 unless ($filter) {
673                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
674                         return;
675                 }
676         }
677
678         if (AnnTalk::dup($_[0], $_[1], $_[2], $chatdupeage)) {
679                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
680                 return;
681         }
682
683
684         Log('chat', $target, $_[0], $text);
685
686         # send it if it isn't the except list and isn't isolated and still has a hop count
687         # taking into account filtering and so on
688         foreach $dxchan (@dxchan) {
689                 my $is_ak1a = $dxchan->is_ak1a;
690                 
691                 if ($dxchan->is_node) {
692                         next if $dxchan == $main::me;
693                         next if $dxchan == $self;
694                         next unless $dxchan->is_spider || $is_ak1a;
695                         next if $target eq 'LOCAL';
696                         if (!$ak1a_line && $is_ak1a) {
697                                 $ak1a_line = DXProt::pc12($_[0], $text, $_[1], "$target.LST");
698                         }
699                 }
700                 
701                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1], 
702                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
703         }
704 }
705
706 sub announce
707 {
708         my $self = shift;
709         my $line = shift;
710         my $isolate = shift;
711         my $to = shift;
712         my $target = shift;
713         my $text = shift;
714         my ($filter, $hops);
715
716         if ($self->{annfilter}) {
717                 ($filter, $hops) = $self->{annfilter}->it(@_);
718                 return unless $filter;
719         }
720         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
721 }
722
723 sub chat
724 {
725         goto &announce;
726 }
727
728
729 sub send_local_config
730 {
731         my $self = shift;
732
733         dbg('DXProt::send_local_config') if isdbg('trace');
734
735         # send our nodes
736         if ($self->{do_pc92}) { 
737                 $self->send_pc92_config;
738         } else {
739                 my $node;
740                 my @nodes;
741                 my @localnodes;
742                 my @remotenodes;
743
744                 if ($self->{isolate}) {
745                         @localnodes = ( $main::routeroot );
746                         $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
747                 } else {
748                         # create a list of all the nodes that are not connected to this connection
749                         # and are not themselves isolated, this to make sure that isolated nodes
750                         # don't appear outside of this node
751
752                         # send locally connected nodes
753                         my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
754                         @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
755                         $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
756
757                         my $node;
758                         my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
759                         my @intcalls;
760                         for $node (@rawintcalls) {
761                                 push @intcalls, $node unless grep $node eq $_, @intcalls; 
762                         }
763                         my $ref = Route::Node::get($self->{call});
764                         my @rnodes = $ref->nodes;
765                         for $node (@intcalls) {
766                                 push @remotenodes, Route::Node::get($node) unless grep $node eq $_, @rnodes, @remotenodes;
767                         }
768                         $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
769                 }
770         
771                 # get all the users connected on the above nodes and send them out
772                 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
773                         if ($node) {
774                                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
775                                 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
776                         } else {
777                                 dbg("sent a null value") if isdbg('chanerr');
778                         }
779                 }
780         }
781 }
782
783 sub gen_my_pc92_config
784 {
785         my $node = shift;
786         
787         if ($node->{call} eq $main::mycall) {
788                 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
789                 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
790                 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
791                 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
792                 return pc92c($main::routeroot, @localnodes);
793         } else {
794                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
795                 return pc92c($node, @rout);
796         }
797 }
798
799 sub gen_pc92_update
800 {
801         my $self = shift;
802         my $with_pc92_nodes = shift;
803         my $node;
804         my @lines;
805         my @dxchan;
806         my @localnodes;
807
808         dbg('ROUTE: DXProt::gen_pc92_update start') if isdbg('routelow');
809
810         # send 'my' configuration for all channels
811         push @lines, gen_my_pc92_config($main::routeroot);
812         
813         if ($with_pc92_nodes) {
814                 # send out the configuration of all the directly connected PC92 nodes with current configuration
815                 # but with the dates that the last config came in with.
816                 @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && $_->{do_pc92} } DXChannel::get_all_nodes();
817                 dbg("ROUTE: pc92 dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
818                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
819                 dbg("ROUTE: pc92 localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
820                 foreach $node (@localnodes) {
821                         if ($node && $node->lastid->{92}) {
822                                 my @rout = map {my $r = Route::get($_); $r ? ($r) : ()} $node->nodes, $node->users;
823                                 push @lines, gen_pc92_with_time($node->call, 'C', $node->lastid->{92}, @rout);
824                         }
825                 }
826         }
827         
828         # send the configuration of all the directly connected 'external' nodes that don't handle PC92
829         # out with the 'external' marker on the first node.
830         @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && !$_->{do_pc92} } DXChannel::get_all_nodes();
831         dbg("ROUTE: non pc92 dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
832         @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
833         dbg("ROUTE: non pc92 localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
834         foreach $node (@localnodes) {
835                 if ($node) {
836                         push @lines, gen_my_pc92_config($node);
837                 } 
838         }
839
840         dbg('ROUTE: DXProt::gen_pc92_update end with ' . scalar @lines . ' lines') if isdbg('routelow');
841         return @lines;
842 }
843
844
845 sub send_pc92_config
846 {
847         my $self = shift;
848
849         dbg('DXProt::send_pc92_config') if isdbg('trace');
850
851         my @out = $self->gen_pc92_update(1);
852         
853         # send the complete config out on this interface
854         for (@out) {
855                 $self->send($_);
856         }
857
858
859 sub send_pc92_update
860 {
861         my @out = $main::me->gen_pc92_update(0);
862         
863         # broadcast the lines to all PC92 nodes
864         for (@out) {
865                 $main::me->broadcast_route_pc9x($main::mycall, undef, $_, 0);
866         }
867
868
869 sub time_out_pc92_routes
870 {
871         my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc92 || $_->via_pc92)} Route::Node::get_all();
872         my @rdel;
873         foreach my $n (@nodes) {
874                 my $o = $n->dec_obs;
875                 if ($o <= 0) {
876                         if (my $dxchan = DXChannel::get($n->call)) {
877                                 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('route');
878                                 $dxchan->disconnect;
879                                 next;
880                         }
881                         my @parents = map {Route::Node::get($_)} $n->parents;
882                         for (@parents) {
883                                 if ($_) {
884                                         dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('route');
885                                         push @rdel, $n->del($_);
886                                 }
887                         }
888                 } else {
889                         dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('route');
890                 }
891         }
892         for (@rdel) {
893                 $main::me->route_pc21($main::mycall, undef, $_) if $_;
894         }
895 }
896
897 #
898 # route a message down an appropriate interface for a callsign
899 #
900 # is called route(to, pcline);
901 #
902
903 sub route
904 {
905         my ($self, $call, $line) = @_;
906
907         if (ref $self && $call eq $self->{call}) {
908                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
909                 return;
910         }
911
912         # always send it down the local interface if available
913         my $dxchan = DXChannel::get($call);
914         if ($dxchan) {
915                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
916         } else {
917                 my $cl = Route::get($call);
918                 $dxchan = $cl->dxchan if $cl;
919                 if (ref $dxchan) {
920                         if (ref $self && $dxchan eq $self) {
921                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
922                                 return;
923                         }
924                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
925                 }
926         }
927
928         # try the backstop method
929         unless ($dxchan) {
930                 my $rcall = RouteDB::get($call);
931                 if ($rcall) {
932                         if ($self && $rcall eq $self->{call}) {
933                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
934                                 return;
935                         }
936                         $dxchan = DXChannel::get($rcall);
937                         dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
938                 }
939         }
940
941         if ($dxchan) {
942                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
943                 if ($routeit) {
944                         $dxchan->send($routeit) unless $dxchan == $main::me;
945                 }
946         } else {
947                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
948         }
949 }
950
951 #
952 # obtain the hops from the list for this callsign and pc no 
953 #
954
955 sub get_hops
956 {
957         my $pcno = shift;
958         my $hops = $DXProt::hopcount{$pcno};
959         $hops = $DXProt::def_hopcount if !$hops;
960         return "H$hops";       
961 }
962
963
964 # adjust the hop count on a per node basis using the user loadable 
965 # hop table if available or else decrement an existing one
966 #
967
968 sub adjust_hops
969 {
970         my $self = shift;
971         my $s = shift;
972         my $call = $self->{call};
973         my $hops;
974         
975         if (($hops) = $s =~ /\^H(\d+)\^?~?$/o) {
976                 my ($pcno) = $s =~ /^PC(\d\d)/o;
977                 confess "$call called adjust_hops with '$s'" unless $pcno;
978                 my $ref = $nodehops{$call} if %nodehops;
979                 if ($ref) {
980                         my $newhops = $ref->{$pcno};
981                         return "" if defined $newhops && $newhops == 0;
982                         $newhops = $ref->{default} unless $newhops;
983                         return "" if defined $newhops && $newhops == 0;
984                         $newhops = $hops if !$newhops;
985                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
986                 }
987         }
988         return $s;
989 }
990
991
992 # load hop tables
993 #
994 sub load_hops
995 {
996         my $self = shift;
997         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
998         do "$main::data/hop_table.pl";
999         return $@ if $@;
1000         return ();
1001 }
1002
1003 sub process_rcmd
1004 {
1005         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1006         if ($tonode eq $main::mycall) {
1007                 my $ref = DXUser->get_current($fromnode);
1008                 my $cref = Route::Node::get($fromnode);
1009                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
1010                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1011                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
1012                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1013                                 my $oldpriv = $self->{priv};
1014                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1015                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
1016                                 $self->{priv} = $oldpriv;
1017                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1018                                 delete $self->{remotecmd};
1019                         } else {
1020                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1021                         }
1022                 } else {
1023                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1024                 }
1025         } else {
1026                 my $ref = DXUser->get_current($tonode);
1027                 if ($ref && $ref->is_clx) {
1028                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1029                 } else {
1030                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1031                 }
1032         }
1033 }
1034
1035 sub process_rcmd_reply
1036 {
1037         my ($self, $tonode, $fromnode, $user, $line) = @_;
1038         if ($tonode eq $main::mycall) {
1039                 my $s = $rcmds{$fromnode};
1040                 if ($s) {
1041                         my $dxchan = DXChannel::get($s->{call});
1042                         my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1043                         $ref->send($line) if $ref;
1044                         delete $rcmds{$fromnode} if !$dxchan;
1045                 } else {
1046                         # send unsolicited ones to the sysop
1047                         my $dxchan = DXChannel::get($main::myalias);
1048                         $dxchan->send($line) if $dxchan;
1049                 }
1050         } else {
1051                 my $ref = DXUser->get_current($tonode);
1052                 if ($ref && $ref->is_clx) {
1053                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1054                 } else {
1055                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1056                 }
1057         }
1058 }
1059
1060 sub send_rcmd_reply
1061 {
1062         my $self = shift;
1063         my $tonode = shift;
1064         my $fromnode = shift;
1065         my $user = shift;
1066         while (@_) {
1067                 my $line = shift;
1068                 $line =~ s/\s*$//;
1069                 Log('rcmd', 'out', $fromnode, $line);
1070                 if ($self->is_clx) {
1071                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1072                 } else {
1073                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1074                 }
1075         }
1076 }
1077
1078 # add a rcmd request to the rcmd queues
1079 sub addrcmd
1080 {
1081         my ($self, $to, $cmd) = @_;
1082
1083         my $r = {};
1084         $r->{call} = $self->{call};
1085         $r->{t} = $main::systime;
1086         $r->{cmd} = $cmd;
1087         $rcmds{$to} = $r;
1088         
1089         my $ref = Route::Node::get($to);
1090         my $dxchan = $ref->dxchan;
1091         if ($dxchan && $dxchan->is_clx) {
1092                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1093         } else {
1094                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1095         }
1096 }
1097
1098 sub disconnect
1099 {
1100         my $self = shift;
1101         my $pc39flag = shift;
1102         my $call = $self->call;
1103
1104         return if $self->{disconnecting}++;
1105         
1106         unless ($pc39flag && $pc39flag == 1) {
1107                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1108         }
1109
1110         # get rid of any PC16/17/19
1111         eph_del_regex("^PC1[679]*$call");
1112
1113         # do routing stuff, remove me from routing table
1114         my $node = Route::Node::get($call);
1115         my @rout;
1116         if ($node) {
1117                 @rout = $node->del($main::routeroot);
1118                 
1119                 # and all my ephemera as well
1120                 for (@rout) {
1121                         my $c = $_->call;
1122                         eph_del_regex("^PC1[679].*$c");
1123                 }
1124         }
1125
1126         RouteDB::delete_interface($call);
1127         
1128         # unbusy and stop and outgoing mail
1129         my $mref = DXMsg::get_busy($call);
1130         $mref->stop_msg($call) if $mref;
1131         
1132         # broadcast to all other nodes that all the nodes connected to via me are gone
1133         unless ($pc39flag && $pc39flag == 2)  {
1134                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1135                 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1136         }
1137
1138         # remove outstanding pings
1139         delete $pings{$call};
1140         
1141         # I was the last node visited
1142     $self->user->node($main::mycall);
1143
1144         # send info to all logged in thingies
1145         $self->tell_login('logoutn');
1146
1147         Log('DXProt', $call . " Disconnected");
1148
1149         $self->SUPER::disconnect;
1150 }
1151
1152
1153
1154 # send a talk message to this thingy
1155 #
1156 sub talk
1157 {
1158         my ($self, $from, $to, $via, $line, $origin) = @_;
1159         
1160         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
1161         $self->send(DXProt::pc10($from, $to, $via, $line, $origin));
1162         Log('talk', $to, $from, $via?$via:$self->call, $line) unless $origin && $origin ne $main::mycall;
1163 }
1164
1165 # send it if it isn't the except list and isn't isolated and still has a hop count
1166 # taking into account filtering and so on
1167
1168 sub send_route
1169 {
1170         my $self = shift;
1171         my $origin = shift;
1172         my $generate = shift;
1173         my $no = shift;     # the no of things to filter on 
1174         my $routeit;
1175         my ($filter, $hops);
1176         my @rin;
1177         
1178         for (; @_ && $no; $no--) {
1179                 my $r = shift;
1180                 
1181                 if (!$self->{isolate} && $self->{routefilter}) {
1182                         $filter = undef;
1183                         if ($r) {
1184                                 ($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});
1185                                 if ($filter) {
1186                                         push @rin, $r;
1187                                 } else {
1188                                         dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
1189                                 }
1190                         } else {
1191                                 dbg("was sent a null value") if isdbg('chanerr');
1192                         }
1193                 } else {
1194                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1195                 }
1196         }
1197         if (@rin) {
1198                 foreach my $line (&$generate(@rin, @_)) {
1199                         if ($hops) {
1200                                 $routeit = $line;
1201                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1202                         } else {
1203                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1204                                 next unless $routeit;
1205                         }
1206                         
1207                         $self->send($routeit);
1208                 }
1209         }
1210 }
1211
1212 sub broadcast_route
1213 {
1214         my $self = shift;
1215         my $origin = shift;
1216         my $generate = shift;
1217         my $line = shift;
1218         my @dxchan = DXChannel::get_all_nodes();
1219         my $dxchan;
1220
1221         if ($line) {
1222                 $line =~ /\^H(\d+)\^?\~?$/;
1223                 return unless $1 > 0;
1224         }
1225         unless ($self->{isolate}) {
1226                 foreach $dxchan (@dxchan) {
1227                         next if $dxchan == $self;
1228                         next if $dxchan == $main::me;
1229                         next unless $dxchan->isa('DXProt');
1230                         next if $dxchan->{do_pc92};
1231                         next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
1232  
1233                         $dxchan->send_route($origin, $generate, @_);
1234                 }
1235         }
1236 }
1237
1238 # this is only used for next door nodes on init
1239 sub send_route_pc92
1240 {
1241         my $self = shift;
1242
1243         return unless $self->{do_pc92};
1244         
1245         my $origin = shift;
1246         my $generate = shift;
1247         my $no = shift;     # the no of things to filter on 
1248         my $line;
1249
1250         $line = &$generate(@_);
1251         $self->send($line);
1252 }
1253
1254 sub broadcast_route_pc9x
1255 {
1256         my $self = shift;
1257         my $origin = shift;
1258         my $generate = shift;
1259         my $line = shift;
1260         my $no = shift;
1261         my @dxchan = DXChannel::get_all_nodes();
1262         my $dxchan;
1263
1264         if ($origin eq $main::mycall && $generate && !$line) {
1265                 $line = &$generate(@_);
1266         } 
1267
1268         $line =~ /\^H(\d+)\^\~?$/;
1269         unless ($1 > 0 && $self->{isolate}) {
1270                 foreach $dxchan (@dxchan) {
1271                         next if $dxchan == $self || $dxchan == $main::me;
1272                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1273                         next unless $dxchan->{do_pc92};
1274                         next unless $dxchan->isa('DXProt');
1275
1276                         $dxchan->send($line);
1277                 }
1278         }
1279 }
1280
1281 sub route_pc16
1282 {
1283         my $self = shift;
1284         return unless $self->user->wantpc16;
1285         my $origin = shift;
1286         my $line = shift;
1287         broadcast_route($self, $origin, \&pc16, $line, 1, @_);
1288 }
1289
1290 sub route_pc17
1291 {
1292         my $self = shift;
1293         return unless $self->user->wantpc16;
1294         my $origin = shift;
1295         my $line = shift;
1296         broadcast_route($self, $origin, \&pc17, $line, 1, @_);
1297 }
1298
1299 sub route_pc19
1300 {
1301         my $self = shift;
1302         my $origin = shift;
1303         my $line = shift;
1304         broadcast_route($self, $origin, \&pc19, $line, scalar @_, @_);
1305 }
1306
1307 sub route_pc21
1308 {
1309         my $self = shift;
1310         my $origin = shift;
1311         my $line = shift;
1312         broadcast_route($self, $origin, \&pc21, $line, scalar @_, @_);
1313 }
1314
1315 sub route_pc24
1316 {
1317         my $self = shift;
1318         my $origin = shift;
1319         my $line = shift;
1320         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1321 }
1322
1323 sub route_pc41
1324 {
1325         my $self = shift;
1326         my $origin = shift;
1327         my $line = shift;
1328         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1329 }
1330
1331 sub route_pc50
1332 {
1333         my $self = shift;
1334         my $origin = shift;
1335         my $line = shift;
1336         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1337 }
1338
1339 sub route_pc92c
1340 {
1341         my $self = shift;
1342         my $origin = shift;
1343         my $line = shift;
1344         broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1345 }
1346
1347 sub route_pc92a
1348 {
1349         my $self = shift;
1350         my $origin = shift;
1351         my $line = shift;
1352         broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1353 }
1354
1355 sub route_pc92d
1356 {
1357         my $self = shift;
1358         my $origin = shift;
1359         my $line = shift;
1360         broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1361 }
1362
1363 sub in_filter_route
1364 {
1365         my $self = shift;
1366         my $r = shift;
1367         my ($filter, $hops) = (1, 1);
1368         
1369         if ($self->{inroutefilter}) {
1370                 ($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);
1371                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1372         }
1373         return $filter;
1374 }
1375
1376 sub eph_dup
1377 {
1378         my $s = shift;
1379         my $t = shift || $eph_restime;
1380         my $r;
1381
1382         # chop the end off
1383         $s =~ s/\^H\d\d?\^?\~?$//;
1384         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
1385         $eph{$s} = $main::systime + $t;
1386         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr'); 
1387         return $r;
1388 }
1389
1390 sub eph_del_regex
1391 {
1392         my $regex = shift;
1393         my ($key, $val);
1394         while (($key, $val) = each %eph) {
1395                 if ($key =~ m{$regex}) {
1396                         delete $eph{$key};
1397                 }
1398         }
1399 }
1400
1401 sub eph_clean
1402 {
1403         my ($key, $val);
1404         
1405         while (($key, $val) = each %eph) {
1406                 if ($main::systime >= $val) {
1407                         delete $eph{$key};
1408                 }
1409         }
1410 }
1411
1412 sub eph_list
1413 {
1414         my ($key, $val);
1415         my @out;
1416
1417         while (($key, $val) = each %eph) {
1418                 push @out, $key, $val;
1419         }
1420         return @out;
1421 }
1422
1423 sub run_cmd
1424 {
1425         goto &DXCommandmode::run_cmd;
1426 }
1427
1428
1429 # import any msgs in the chat directory
1430 # the messages are sent to the chat group which forms the
1431 # the first part of the name (eg: solar.1243.txt would be
1432 # sent to chat group SOLAR)
1433
1434 # Each message found is sent: one non-blank line to one chat
1435 # message. So 4 lines = 4 chat messages.
1436
1437 # The special name LOCAL is for local users ANN
1438 # The special name ALL is for ANN/FULL
1439 # The special name SYSOP is for ANN/SYSOP
1440 #
1441 sub import_chat
1442 {
1443         # are there any to do in this directory?
1444         return unless -d $chatimportfn;
1445         unless (opendir(DIR, $chatimportfn)) {
1446                 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1447                 Log('msg', "can\'t open $chatimportfn $!");
1448                 return;
1449         } 
1450
1451         my @names = readdir(DIR);
1452         closedir(DIR);
1453         my $name;
1454         foreach $name (@names) {
1455                 next if $name =~ /^\./;
1456                 my $splitit = $name =~ /^split/;
1457                 my $fn = "$chatimportfn/$name";
1458                 next unless -f $fn;
1459                 unless (open(MSG, $fn)) {
1460                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1461                         Log('msg', "can\'t open import file $fn $!");
1462                         unlink($fn);
1463                         next;
1464                 }
1465                 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1466                 close(MSG);
1467                 unlink($fn);
1468
1469                 my @cat = split /\./, $name;
1470                 my $target = uc $cat[0];
1471
1472                 foreach my $text (@msg) {
1473                         next unless $text && $text !~ /^\s*#/;
1474                         if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP') {
1475                                 my $sysopflag = $target eq 'SYSOP' ? '*' : ' ';
1476                                 if ($target ne 'LOCAL') {
1477                                         send_announce($main::me, pc12($main::mycall, $text, '*', $sysopflag), $main::mycall, '*', $text, $sysopflag, $main::mycall, '0');
1478                                 } else {
1479                                         Log('ann', 'LOCAL', $main::mycall, $text);
1480                                         DXChannel::broadcast_list("To LOCAL de ${main::mycall}: $text\a", 'ann', undef, DXCommandmode->get_all());
1481                                 }
1482                         } else {
1483                                 my $msgid = nextchatmsgid();
1484                                 $text = "#$msgid $text";
1485                                 send_chat($main::me, pc12($main::mycall, $text, '*', $target), $main::mycall, '*', $text, $target, $main::mycall, '0');
1486                         }
1487                 }
1488         }
1489 }
1490
1491 1;
1492 __END__