4e32c22e77940e1eea3db025e85a55b08c0039ca
[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 ($self != $main::me) {
375                 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
376                         $trail ||= '';
377                         $hops--;
378                         return if $hops < 0;
379                         $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
380                         $field[-1] = "H$hops";
381                 }
382         }
383
384         # send it out for processing
385         my $origin = $self->{call};
386         no strict 'subs';
387         my $sub = "handle_$pcno";
388
389         if ($self->can($sub)) {
390                 $self->$sub($pcno, $line, $origin, @field);
391         } else {
392                 $self->handle_default($pcno, $line, $origin, @field);
393         }
394 }
395
396 #
397 # This is called from inside the main cluster processing loop and is used
398 # for despatching commands that are doing some long processing job
399 #
400 sub process
401 {
402         my $t = time;
403         my @dxchan = DXChannel::get_all();
404         my $dxchan;
405         my $pc50s;
406         
407         # send out a pc50 on EVERY channel all at once
408         if ($t >= $last_pc50 + $DXProt::pc50_interval) {
409                 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
410                 eph_dup($pc50s);
411                 $last_pc50 = $t;
412         }
413
414         foreach $dxchan (@dxchan) {
415                 next unless $dxchan->is_node;
416                 next if $dxchan->handle_xml;
417                 next if $dxchan == $main::me;
418
419                 # send the pc50
420                 $dxchan->send($pc50s) if $pc50s;
421                 
422                 # send a ping out on this channel
423                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
424                         if ($dxchan->{nopings} <= 0) {
425                                 $dxchan->disconnect;
426                         } else {
427                                 DXXml::Ping::add($main::me, $dxchan->call);
428                                 $dxchan->{nopings} -= 1;
429                                 $dxchan->{lastping} = $t;
430                                 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
431                         }
432                 }
433         }
434
435         Investigate::process();
436
437         # every ten seconds
438         if ($t - $last10 >= 10) {       
439                 # clean out ephemera 
440
441                 eph_clean();
442                 import_chat();
443
444                 if ($main::systime >= $last_pc92_update + $pc92_update_period) {
445                         dbg("ROUTE: sending pc92 update") if isdbg('route');
446                         send_pc92_update();
447                         time_out_pc92_routes();
448                         $last_pc92_update = $main::systime + int rand(180);
449                 }
450                 
451                 $last10 = $t;
452         }
453         
454         if ($main::systime - 3600 > $last_hour) {
455                 $last_hour = $main::systime;
456         }
457 }
458
459 #
460 # finish up a pc context
461 #
462
463 #
464 # some active measures
465 #
466
467
468 sub send_dx_spot
469 {
470         my $self = shift;
471         my $line = shift;
472         my @dxchan = DXChannel::get_all();
473         my $dxchan;
474         
475         # send it if it isn't the except list and isn't isolated and still has a hop count
476         # taking into account filtering and so on
477         foreach $dxchan (@dxchan) {
478                 next if $dxchan == $main::me;
479                 next if $dxchan == $self && $self->is_node;
480                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
481         }
482 }
483
484 sub dx_spot
485 {
486         my $self = shift;
487         my $line = shift;
488         my $isolate = shift;
489         my ($filter, $hops);
490
491         if ($self->{spotsfilter}) {
492                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
493                 return unless $filter;
494         }
495         send_prot_line($self, $filter, $hops, $isolate, $line);
496 }
497
498 sub send_prot_line
499 {
500         my ($self, $filter, $hops, $isolate, $line) = @_;
501         my $routeit;
502
503
504         if ($hops) {
505                 $routeit = $line;
506                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
507         } else {
508                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
509                 return unless $routeit;
510         }
511         if ($filter) {
512                 $self->send($routeit);
513         } else {
514                 $self->send($routeit) unless $self->{isolate} || $isolate;
515         }
516 }
517
518
519 sub send_wwv_spot
520 {
521         my $self = shift;
522         my $line = shift;
523         my @dxchan = DXChannel::get_all();
524         my $dxchan;
525         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
526
527         # send it if it isn't the except list and isn't isolated and still has a hop count
528         # taking into account filtering and so on
529         foreach $dxchan (@dxchan) {
530                 next if $dxchan == $main::me;
531                 next if $dxchan == $self && $self->is_node;
532                 my $routeit;
533                 my ($filter, $hops);
534
535                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
536         }
537 }
538
539 sub wwv
540 {
541         my $self = shift;
542         my $line = shift;
543         my $isolate = shift;
544         my ($filter, $hops);
545         
546         if ($self->{wwvfilter}) {
547                 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
548                 return unless $filter;
549         }
550         send_prot_line($self, $filter, $hops, $isolate, $line)
551 }
552
553 sub send_wcy_spot
554 {
555         my $self = shift;
556         my $line = shift;
557         my @dxchan = DXChannel::get_all();
558         my $dxchan;
559         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
560         
561         # send it if it isn't the except list and isn't isolated and still has a hop count
562         # taking into account filtering and so on
563         foreach $dxchan (@dxchan) {
564                 next if $dxchan == $main::me;
565                 next if $dxchan == $self;
566
567                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
568         }
569 }
570
571 sub wcy
572 {
573         my $self = shift;
574         my $line = shift;
575         my $isolate = shift;
576         my ($filter, $hops);
577
578         if ($self->{wcyfilter}) {
579                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
580                 return unless $filter;
581         }
582         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
583 }
584
585 # send an announce
586 sub send_announce
587 {
588         my $self = shift;
589         my $line = shift;
590         my @dxchan = DXChannel::get_all();
591         my $dxchan;
592         my $target;
593         my $to = 'To ';
594         my $text = unpad($_[2]);
595                                 
596         if ($_[3] eq '*') {     # sysops
597                 $target = "SYSOP";
598         } elsif ($_[3] gt ' ') { # speciality list handling
599                 my ($name) = split /\./, $_[3]; 
600                 $target = "$name"; # put the rest in later (if bothered) 
601         } 
602         
603         if ($_[5] eq '1') {
604                 $target = "WX"; 
605                 $to = '';
606         }
607         $target = "ALL" if !$target;
608
609
610         # obtain country codes etc 
611         my @a = Prefix::cty_data($_[0]);
612         my @b = Prefix::cty_data($_[4]);
613         if ($self->{inannfilter}) {
614                 my ($filter, $hops) = 
615                         $self->{inannfilter}->it(@_, $self->{call}, 
616                                                                          @a[0..2],
617                                                                          @b[0..2], $a[3], $b[3]);
618                 unless ($filter) {
619                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
620                         return;
621                 }
622         }
623
624         if (AnnTalk::dup($_[0], $_[1], $_[2])) {
625                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
626                 return;
627         }
628
629         Log('ann', $target, $_[0], $text);
630
631         # send it if it isn't the except list and isn't isolated and still has a hop count
632         # taking into account filtering and so on
633         foreach $dxchan (@dxchan) {
634                 next if $dxchan == $main::me;
635                 next if $dxchan == $self && $self->is_node;
636                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
637                                                   @a[0..2], @b[0..2]);
638         }
639 }
640
641 my $msgid = 0;
642
643 sub nextchatmsgid
644 {
645         $msgid++;
646         $msgid = 1 if $msgid > 999;
647         return $msgid;
648 }
649
650 # send a chat line
651 sub send_chat
652 {
653         my $self = shift;
654         my $line = shift;
655         my @dxchan = DXChannel::get_all();
656         my $dxchan;
657         my $target = $_[3];
658         my $text = unpad($_[2]);
659         my $ak1a_line;
660                                 
661         # munge the group and recast the line if required
662         if ($target =~ s/\.LST$//) {
663                 $ak1a_line = $line;
664         }
665         
666         # obtain country codes etc 
667         my @a = Prefix::cty_data($_[0]);
668         my @b = Prefix::cty_data($_[4]);
669         if ($self->{inannfilter}) {
670                 my ($filter, $hops) = 
671                         $self->{inannfilter}->it(@_, $self->{call}, 
672                                                                          @a[0..2],
673                                                                          @b[0..2], $a[3], $b[3]);
674                 unless ($filter) {
675                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
676                         return;
677                 }
678         }
679
680         if (AnnTalk::dup($_[0], $_[1], $_[2], $chatdupeage)) {
681                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
682                 return;
683         }
684
685
686         Log('chat', $target, $_[0], $text);
687
688         # send it if it isn't the except list and isn't isolated and still has a hop count
689         # taking into account filtering and so on
690         foreach $dxchan (@dxchan) {
691                 my $is_ak1a = $dxchan->is_ak1a;
692                 
693                 if ($dxchan->is_node) {
694                         next if $dxchan == $main::me;
695                         next if $dxchan == $self;
696                         next unless $dxchan->is_spider || $is_ak1a;
697                         next if $target eq 'LOCAL';
698                         if (!$ak1a_line && $is_ak1a) {
699                                 $ak1a_line = DXProt::pc12($_[0], $text, $_[1], "$target.LST");
700                         }
701                 }
702                 
703                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1], 
704                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
705         }
706 }
707
708 sub announce
709 {
710         my $self = shift;
711         my $line = shift;
712         my $isolate = shift;
713         my $to = shift;
714         my $target = shift;
715         my $text = shift;
716         my ($filter, $hops);
717
718         if ($self->{annfilter}) {
719                 ($filter, $hops) = $self->{annfilter}->it(@_);
720                 return unless $filter;
721         }
722         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
723 }
724
725 sub chat
726 {
727         goto &announce;
728 }
729
730
731 sub send_local_config
732 {
733         my $self = shift;
734
735         dbg('DXProt::send_local_config') if isdbg('trace');
736
737         # send our nodes
738         if ($self->{do_pc92}) { 
739                 $self->send_pc92_config;
740         } else {
741                 my $node;
742                 my @nodes;
743                 my @localnodes;
744                 my @remotenodes;
745
746                 if ($self->{isolate}) {
747                         @localnodes = ( $main::routeroot );
748                         $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
749                 } else {
750                         # create a list of all the nodes that are not connected to this connection
751                         # and are not themselves isolated, this to make sure that isolated nodes
752                         # don't appear outside of this node
753
754                         # send locally connected nodes
755                         my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
756                         @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
757                         $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
758
759                         my $node;
760                         my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
761                         my @intcalls;
762                         for $node (@rawintcalls) {
763                                 push @intcalls, $node unless grep $node eq $_, @intcalls; 
764                         }
765                         my $ref = Route::Node::get($self->{call});
766                         my @rnodes = $ref->nodes;
767                         for $node (@intcalls) {
768                                 push @remotenodes, Route::Node::get($node) unless grep $node eq $_, @rnodes, @remotenodes;
769                         }
770                         $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
771                 }
772         
773                 # get all the users connected on the above nodes and send them out
774                 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
775                         if ($node) {
776                                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
777                                 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
778                         } else {
779                                 dbg("sent a null value") if isdbg('chanerr');
780                         }
781                 }
782         }
783 }
784
785 sub gen_my_pc92_config
786 {
787         my $node = shift;
788         
789         if ($node->{call} eq $main::mycall) {
790                 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
791                 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
792                 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
793                 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
794                 return pc92c($main::routeroot, @localnodes);
795         } else {
796                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
797                 return pc92c($node, @rout);
798         }
799 }
800
801 sub gen_pc92_update
802 {
803         my $self = shift;
804         my $with_pc92_nodes = shift;
805         my $node;
806         my @lines;
807         my @dxchan;
808         my @localnodes;
809
810         dbg('ROUTE: DXProt::gen_pc92_update start') if isdbg('routelow');
811
812         # send 'my' configuration for all channels
813         push @lines, gen_my_pc92_config($main::routeroot);
814         
815         if ($with_pc92_nodes) {
816                 # send out the configuration of all the directly connected PC92 nodes with current configuration
817                 # but with the dates that the last config came in with.
818                 @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && $_->{do_pc92} } DXChannel::get_all_nodes();
819                 dbg("ROUTE: pc92 dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
820                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
821                 dbg("ROUTE: pc92 localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
822                 foreach $node (@localnodes) {
823                         if ($node && $node->lastid->{92}) {
824                                 my @rout = map {my $r = Route::get($_); $r ? ($r) : ()} $node->nodes, $node->users;
825                                 push @lines, gen_pc92_with_time($node->call, 'C', $node->lastid->{92}, @rout);
826                         }
827                 }
828         }
829         
830         # send the configuration of all the directly connected 'external' nodes that don't handle PC92
831         # out with the 'external' marker on the first node.
832         @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && !$_->{do_pc92} } DXChannel::get_all_nodes();
833         dbg("ROUTE: non pc92 dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
834         @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
835         dbg("ROUTE: non pc92 localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
836         foreach $node (@localnodes) {
837                 if ($node) {
838                         push @lines, gen_my_pc92_config($node);
839                 } 
840         }
841
842         dbg('ROUTE: DXProt::gen_pc92_update end with ' . scalar @lines . ' lines') if isdbg('routelow');
843         return @lines;
844 }
845
846
847 sub send_pc92_config
848 {
849         my $self = shift;
850
851         dbg('DXProt::send_pc92_config') if isdbg('trace');
852
853         my @out = $self->gen_pc92_update(1);
854         
855         # send the complete config out on this interface
856         for (@out) {
857                 $self->send($_);
858         }
859
860
861 sub send_pc92_update
862 {
863         my @out = $main::me->gen_pc92_update(0);
864         
865         # broadcast the lines to all PC92 nodes
866         for (@out) {
867                 $main::me->broadcast_route_pc9x($main::mycall, undef, $_, 0);
868         }
869
870
871 sub time_out_pc92_routes
872 {
873         my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc92 || $_->via_pc92)} Route::Node::get_all();
874         my @rdel;
875         foreach my $n (@nodes) {
876                 my $o = $n->dec_obs;
877                 if ($o <= 0) {
878                         if (my $dxchan = DXChannel::get($n->call)) {
879                                 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('route');
880                                 $dxchan->disconnect;
881                                 next;
882                         }
883                         my @parents = map {Route::Node::get($_)} $n->parents;
884                         for (@parents) {
885                                 if ($_) {
886                                         dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('route');
887                                         push @rdel, $n->del($_);
888                                 }
889                         }
890                 } else {
891                         dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('route');
892                 }
893         }
894         for (@rdel) {
895                 $main::me->route_pc21($main::mycall, undef, $_) if $_;
896         }
897 }
898
899 #
900 # route a message down an appropriate interface for a callsign
901 #
902 # is called route(to, pcline);
903 #
904
905 sub route
906 {
907         my ($self, $call, $line) = @_;
908
909         if (ref $self && $call eq $self->{call}) {
910                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
911                 return;
912         }
913
914         # always send it down the local interface if available
915         my $dxchan = DXChannel::get($call);
916         if ($dxchan) {
917                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
918         } else {
919                 my $cl = Route::get($call);
920                 $dxchan = $cl->dxchan if $cl;
921                 if (ref $dxchan) {
922                         if (ref $self && $dxchan eq $self) {
923                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
924                                 return;
925                         }
926                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
927                 }
928         }
929
930         # try the backstop method
931         unless ($dxchan) {
932                 my $rcall = RouteDB::get($call);
933                 if ($rcall) {
934                         if ($self && $rcall eq $self->{call}) {
935                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
936                                 return;
937                         }
938                         $dxchan = DXChannel::get($rcall);
939                         dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
940                 }
941         }
942
943         if ($dxchan) {
944                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
945                 if ($routeit) {
946                         $dxchan->send($routeit) unless $dxchan == $main::me;
947                 }
948         } else {
949                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
950         }
951 }
952
953 #
954 # obtain the hops from the list for this callsign and pc no 
955 #
956
957 sub get_hops
958 {
959         my $pcno = shift;
960         my $hops = $DXProt::hopcount{$pcno};
961         $hops = $DXProt::def_hopcount if !$hops;
962         return "H$hops";       
963 }
964
965
966 # adjust the hop count on a per node basis using the user loadable 
967 # hop table if available or else decrement an existing one
968 #
969
970 sub adjust_hops
971 {
972         my $self = shift;
973         my $s = shift;
974         my $call = $self->{call};
975         my $hops;
976         
977         if (($hops) = $s =~ /\^H(\d+)\^?~?$/o) {
978                 my ($pcno) = $s =~ /^PC(\d\d)/o;
979                 confess "$call called adjust_hops with '$s'" unless $pcno;
980                 my $ref = $nodehops{$call} if %nodehops;
981                 if ($ref) {
982                         my $newhops = $ref->{$pcno};
983                         return "" if defined $newhops && $newhops == 0;
984                         $newhops = $ref->{default} unless $newhops;
985                         return "" if defined $newhops && $newhops == 0;
986                         $newhops = $hops if !$newhops;
987                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
988                 }
989         }
990         return $s;
991 }
992
993
994 # load hop tables
995 #
996 sub load_hops
997 {
998         my $self = shift;
999         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1000         do "$main::data/hop_table.pl";
1001         return $@ if $@;
1002         return ();
1003 }
1004
1005 sub process_rcmd
1006 {
1007         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1008         if ($tonode eq $main::mycall) {
1009                 my $ref = DXUser->get_current($fromnode);
1010                 my $cref = Route::Node::get($fromnode);
1011                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
1012                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1013                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
1014                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1015                                 my $oldpriv = $self->{priv};
1016                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1017                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
1018                                 $self->{priv} = $oldpriv;
1019                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1020                                 delete $self->{remotecmd};
1021                         } else {
1022                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1023                         }
1024                 } else {
1025                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1026                 }
1027         } else {
1028                 my $ref = DXUser->get_current($tonode);
1029                 if ($ref && $ref->is_clx) {
1030                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1031                 } else {
1032                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1033                 }
1034         }
1035 }
1036
1037 sub process_rcmd_reply
1038 {
1039         my ($self, $tonode, $fromnode, $user, $line) = @_;
1040         if ($tonode eq $main::mycall) {
1041                 my $s = $rcmds{$fromnode};
1042                 if ($s) {
1043                         my $dxchan = DXChannel::get($s->{call});
1044                         my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1045                         $ref->send($line) if $ref;
1046                         delete $rcmds{$fromnode} if !$dxchan;
1047                 } else {
1048                         # send unsolicited ones to the sysop
1049                         my $dxchan = DXChannel::get($main::myalias);
1050                         $dxchan->send($line) if $dxchan;
1051                 }
1052         } else {
1053                 my $ref = DXUser->get_current($tonode);
1054                 if ($ref && $ref->is_clx) {
1055                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1056                 } else {
1057                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1058                 }
1059         }
1060 }
1061
1062 sub send_rcmd_reply
1063 {
1064         my $self = shift;
1065         my $tonode = shift;
1066         my $fromnode = shift;
1067         my $user = shift;
1068         while (@_) {
1069                 my $line = shift;
1070                 $line =~ s/\s*$//;
1071                 Log('rcmd', 'out', $fromnode, $line);
1072                 if ($self->is_clx) {
1073                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1074                 } else {
1075                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1076                 }
1077         }
1078 }
1079
1080 # add a rcmd request to the rcmd queues
1081 sub addrcmd
1082 {
1083         my ($self, $to, $cmd) = @_;
1084
1085         my $r = {};
1086         $r->{call} = $self->{call};
1087         $r->{t} = $main::systime;
1088         $r->{cmd} = $cmd;
1089         $rcmds{$to} = $r;
1090         
1091         my $ref = Route::Node::get($to);
1092         my $dxchan = $ref->dxchan;
1093         if ($dxchan && $dxchan->is_clx) {
1094                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1095         } else {
1096                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1097         }
1098 }
1099
1100 sub disconnect
1101 {
1102         my $self = shift;
1103         my $pc39flag = shift;
1104         my $call = $self->call;
1105
1106         return if $self->{disconnecting}++;
1107         
1108         unless ($pc39flag && $pc39flag == 1) {
1109                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1110         }
1111
1112         # get rid of any PC16/17/19
1113         eph_del_regex("^PC1[679]*$call");
1114
1115         # do routing stuff, remove me from routing table
1116         my $node = Route::Node::get($call);
1117         my @rout;
1118         if ($node) {
1119                 @rout = $node->del($main::routeroot);
1120                 
1121                 # and all my ephemera as well
1122                 for (@rout) {
1123                         my $c = $_->call;
1124                         eph_del_regex("^PC1[679].*$c");
1125                 }
1126         }
1127
1128         RouteDB::delete_interface($call);
1129         
1130         # unbusy and stop and outgoing mail
1131         my $mref = DXMsg::get_busy($call);
1132         $mref->stop_msg($call) if $mref;
1133         
1134         # broadcast to all other nodes that all the nodes connected to via me are gone
1135         unless ($pc39flag && $pc39flag == 2)  {
1136                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1137                 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1138         }
1139
1140         # remove outstanding pings
1141         delete $pings{$call};
1142         
1143         # I was the last node visited
1144     $self->user->node($main::mycall);
1145
1146         # send info to all logged in thingies
1147         $self->tell_login('logoutn');
1148
1149         Log('DXProt', $call . " Disconnected");
1150
1151         $self->SUPER::disconnect;
1152 }
1153
1154
1155
1156 # send a talk message to this thingy
1157 #
1158 sub talk
1159 {
1160         my ($self, $from, $to, $via, $line, $origin) = @_;
1161         
1162         if ($self->{do_pc93}) {
1163                 $self->send(pc93($to, $from, $via, $line));
1164         } else {
1165                 $self->send(pc10($from, $to, $via, $line, $origin));
1166         }
1167         Log('talk', $to, $from, $via?$via:$self->call, $line) unless $origin && $origin ne $main::mycall;
1168 }
1169
1170 # send it if it isn't the except list and isn't isolated and still has a hop count
1171 # taking into account filtering and so on
1172
1173 sub send_route
1174 {
1175         my $self = shift;
1176         my $origin = shift;
1177         my $generate = shift;
1178         my $no = shift;     # the no of things to filter on 
1179         my $routeit;
1180         my ($filter, $hops);
1181         my @rin;
1182         
1183         for (; @_ && $no; $no--) {
1184                 my $r = shift;
1185                 
1186                 if (!$self->{isolate} && $self->{routefilter}) {
1187                         $filter = undef;
1188                         if ($r) {
1189                                 ($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});
1190                                 if ($filter) {
1191                                         push @rin, $r;
1192                                 } else {
1193                                         dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
1194                                 }
1195                         } else {
1196                                 dbg("was sent a null value") if isdbg('chanerr');
1197                         }
1198                 } else {
1199                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1200                 }
1201         }
1202         if (@rin) {
1203                 foreach my $line (&$generate(@rin, @_)) {
1204                         if ($hops) {
1205                                 $routeit = $line;
1206                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1207                         } else {
1208                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1209                                 next unless $routeit;
1210                         }
1211                         
1212                         $self->send($routeit);
1213                 }
1214         }
1215 }
1216
1217 sub broadcast_route
1218 {
1219         my $self = shift;
1220         my $origin = shift;
1221         my $generate = shift;
1222         my $line = shift;
1223         my @dxchan = DXChannel::get_all_nodes();
1224         my $dxchan;
1225
1226         if ($line) {
1227                 $line =~ /\^H(\d+)\^?\~?$/;
1228                 return unless $1 > 0;
1229         }
1230         unless ($self->{isolate}) {
1231                 foreach $dxchan (@dxchan) {
1232                         next if $dxchan == $self;
1233                         next if $dxchan == $main::me;
1234                         next unless $dxchan->isa('DXProt');
1235                         next if $dxchan->{do_pc92};
1236                         next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
1237  
1238                         $dxchan->send_route($origin, $generate, @_);
1239                 }
1240         }
1241 }
1242
1243 # this is only used for next door nodes on init
1244 sub send_route_pc92
1245 {
1246         my $self = shift;
1247
1248         return unless $self->{do_pc92};
1249         
1250         my $origin = shift;
1251         my $generate = shift;
1252         my $no = shift;     # the no of things to filter on 
1253         my $line;
1254
1255         $line = &$generate(@_);
1256         $self->send($line);
1257 }
1258
1259 sub broadcast_route_pc9x
1260 {
1261         my $self = shift;
1262         my $origin = shift;
1263         my $generate = shift;
1264         my $line = shift;
1265         my $no = shift;
1266         my @dxchan = DXChannel::get_all_nodes();
1267         my $dxchan;
1268
1269         if ($origin eq $main::mycall && $generate && !$line) {
1270                 $line = &$generate(@_);
1271         } 
1272
1273         $line =~ /\^H(\d+)\^\~?$/;
1274         unless ($1 > 0 && $self->{isolate}) {
1275                 foreach $dxchan (@dxchan) {
1276                         next if $dxchan == $self || $dxchan == $main::me;
1277                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1278                         next unless $dxchan->{do_pc92};
1279                         next unless $dxchan->isa('DXProt');
1280
1281                         $dxchan->send($line);
1282                 }
1283         }
1284 }
1285
1286 sub route_pc16
1287 {
1288         my $self = shift;
1289         return unless $self->user->wantpc16;
1290         my $origin = shift;
1291         my $line = shift;
1292         broadcast_route($self, $origin, \&pc16, $line, 1, @_);
1293 }
1294
1295 sub route_pc17
1296 {
1297         my $self = shift;
1298         return unless $self->user->wantpc16;
1299         my $origin = shift;
1300         my $line = shift;
1301         broadcast_route($self, $origin, \&pc17, $line, 1, @_);
1302 }
1303
1304 sub route_pc19
1305 {
1306         my $self = shift;
1307         my $origin = shift;
1308         my $line = shift;
1309         broadcast_route($self, $origin, \&pc19, $line, scalar @_, @_);
1310 }
1311
1312 sub route_pc21
1313 {
1314         my $self = shift;
1315         my $origin = shift;
1316         my $line = shift;
1317         broadcast_route($self, $origin, \&pc21, $line, scalar @_, @_);
1318 }
1319
1320 sub route_pc24
1321 {
1322         my $self = shift;
1323         my $origin = shift;
1324         my $line = shift;
1325         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1326 }
1327
1328 sub route_pc41
1329 {
1330         my $self = shift;
1331         my $origin = shift;
1332         my $line = shift;
1333         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1334 }
1335
1336 sub route_pc50
1337 {
1338         my $self = shift;
1339         my $origin = shift;
1340         my $line = shift;
1341         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1342 }
1343
1344 sub route_pc92c
1345 {
1346         my $self = shift;
1347         my $origin = shift;
1348         my $line = shift;
1349         broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1350 }
1351
1352 sub route_pc92a
1353 {
1354         my $self = shift;
1355         my $origin = shift;
1356         my $line = shift;
1357         broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1358 }
1359
1360 sub route_pc92d
1361 {
1362         my $self = shift;
1363         my $origin = shift;
1364         my $line = shift;
1365         broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1366 }
1367
1368 sub in_filter_route
1369 {
1370         my $self = shift;
1371         my $r = shift;
1372         my ($filter, $hops) = (1, 1);
1373         
1374         if ($self->{inroutefilter}) {
1375                 ($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);
1376                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1377         }
1378         return $filter;
1379 }
1380
1381 sub eph_dup
1382 {
1383         my $s = shift;
1384         my $t = shift || $eph_restime;
1385         my $r;
1386
1387         # chop the end off
1388         $s =~ s/\^H\d\d?\^?\~?$//;
1389         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
1390         $eph{$s} = $main::systime + $t;
1391         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr'); 
1392         return $r;
1393 }
1394
1395 sub eph_del_regex
1396 {
1397         my $regex = shift;
1398         my ($key, $val);
1399         while (($key, $val) = each %eph) {
1400                 if ($key =~ m{$regex}) {
1401                         delete $eph{$key};
1402                 }
1403         }
1404 }
1405
1406 sub eph_clean
1407 {
1408         my ($key, $val);
1409         
1410         while (($key, $val) = each %eph) {
1411                 if ($main::systime >= $val) {
1412                         delete $eph{$key};
1413                 }
1414         }
1415 }
1416
1417 sub eph_list
1418 {
1419         my ($key, $val);
1420         my @out;
1421
1422         while (($key, $val) = each %eph) {
1423                 push @out, $key, $val;
1424         }
1425         return @out;
1426 }
1427
1428 sub run_cmd
1429 {
1430         goto &DXCommandmode::run_cmd;
1431 }
1432
1433
1434 # import any msgs in the chat directory
1435 # the messages are sent to the chat group which forms the
1436 # the first part of the name (eg: solar.1243.txt would be
1437 # sent to chat group SOLAR)
1438
1439 # Each message found is sent: one non-blank line to one chat
1440 # message. So 4 lines = 4 chat messages.
1441
1442 # The special name LOCAL is for local users ANN
1443 # The special name ALL is for ANN/FULL
1444 # The special name SYSOP is for ANN/SYSOP
1445 #
1446 sub import_chat
1447 {
1448         # are there any to do in this directory?
1449         return unless -d $chatimportfn;
1450         unless (opendir(DIR, $chatimportfn)) {
1451                 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1452                 Log('msg', "can\'t open $chatimportfn $!");
1453                 return;
1454         } 
1455
1456         my @names = readdir(DIR);
1457         closedir(DIR);
1458         my $name;
1459         foreach $name (@names) {
1460                 next if $name =~ /^\./;
1461                 my $splitit = $name =~ /^split/;
1462                 my $fn = "$chatimportfn/$name";
1463                 next unless -f $fn;
1464                 unless (open(MSG, $fn)) {
1465                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1466                         Log('msg', "can\'t open import file $fn $!");
1467                         unlink($fn);
1468                         next;
1469                 }
1470                 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1471                 close(MSG);
1472                 unlink($fn);
1473
1474                 my @cat = split /\./, $name;
1475                 my $target = uc $cat[0];
1476
1477                 foreach my $text (@msg) {
1478                         next unless $text && $text !~ /^\s*#/;
1479                         if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP') {
1480                                 my $sysopflag = $target eq 'SYSOP' ? '*' : ' ';
1481                                 if ($target ne 'LOCAL') {
1482                                         send_announce($main::me, pc12($main::mycall, $text, '*', $sysopflag), $main::mycall, '*', $text, $sysopflag, $main::mycall, '0');
1483                                 } else {
1484                                         Log('ann', 'LOCAL', $main::mycall, $text);
1485                                         DXChannel::broadcast_list("To LOCAL de ${main::mycall}: $text\a", 'ann', undef, DXCommandmode->get_all());
1486                                 }
1487                         } else {
1488                                 my $msgid = nextchatmsgid();
1489                                 $text = "#$msgid $text";
1490                                 send_chat($main::me, pc12($main::mycall, $text, '*', $target), $main::mycall, '*', $text, $target, $main::mycall, '0');
1491                         }
1492                 }
1493         }
1494 }
1495
1496 1;
1497 __END__