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