1. Incorporated sh/st, (un)set/lockout, forward/opername from Iain G0RDI
[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 DXCluster;
19 use DXProtVars;
20 use DXCommandmode;
21 use DXLog;
22 use Spot;
23 use DXProtout;
24 use Carp;
25
26 use strict;
27 use vars qw($me $pc11_max_age $pc11_dup_age %dup $last_hour %pings %rcmds);
28
29 $me = undef;                                    # the channel id for this cluster
30 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
31 $pc11_dup_age = 24*3600;                # the maximum time to keep the dup list for
32 %dup = ();                                              # the pc11 and 26 dup hash 
33 $last_hour = time;                              # last time I did an hourly periodic update
34 %pings = ();                    # outstanding ping requests outbound
35 %rcmds = ();                    # outstanding rcmd requests outbound
36
37 sub init
38 {
39         my $user = DXUser->get($main::mycall);
40         $me = DXProt->new($main::mycall, undef, $user); 
41         $me->{here} = 1;
42         #  $me->{sort} = 'M';    # M for me
43 }
44
45 #
46 # obtain a new connection this is derived from dxchannel
47 #
48
49 sub new 
50 {
51         my $self = DXChannel::alloc(@_);
52         $self->{sort} = 'A';            # in absence of how to find out what sort of an object I am
53         return $self;
54 }
55
56 # this is how a pc connection starts (for an incoming connection)
57 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
58 # all the crap that comes between).
59 sub start
60 {
61         my ($self, $line, $sort) = @_;
62         my $call = $self->{call};
63         my $user = $self->{user};
64         
65         # remember type of connection
66         $self->{consort} = $line;
67         $self->{outbound} = $sort eq 'O';
68         $self->{priv} = $user->priv;
69         $self->{lang} = $user->lang;
70         $self->{consort} = $line;       # save the connection type
71         $self->{here} = 1;
72         
73         # set unbuffered
74         $self->send_now('B',"0");
75         
76         # send initialisation string
77         if (!$self->{outbound}) {
78                 $self->send(pc38()) if DXNode->get_all();
79                 $self->send(pc18());
80         }
81         $self->state('init');
82         $self->pc50_t(time);
83
84         Log('DXProt', "$call connected");
85 }
86
87 #
88 # This is the normal pcxx despatcher
89 #
90 sub normal
91 {
92         my ($self, $line) = @_;
93         my @field = split /[\^\~]/, $line;
94         
95         # ignore any lines that don't start with PC
96         return if !$field[0] =~ /^PC/;
97         
98         # process PC frames
99         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
100         return if $pcno < 10 || $pcno > 51;
101         
102  SWITCH: {
103                 if ($pcno == 10) {              # incoming talk
104                         
105                         # is it for me or one of mine?
106                         my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
107                         if ($call eq $main::mycall || grep $_ eq $call, get_all_user_calls()) {
108                                 
109                                 # yes, it is
110                                 my $text = unpad($field[3]);
111                                 Log('talk', $call, $field[1], $field[6], $text);
112                                 $call = $main::myalias if $call eq $main::mycall;
113                                 my $ref = DXChannel->get($call);
114                                 $ref->send("$call de $field[1]: $text") if $ref;
115                         } else {
116                                 route($field[2], $line); # relay it on its way
117                         }
118                         return;
119                 }
120                 
121                 if ($pcno == 11 || $pcno == 26) { # dx spot
122                         
123                         # if this is a 'nodx' node then ignore it
124                         last SWITCH if grep $field[7] =~ /^$_/,  @DXProt::nodx_node;
125                         
126                         # convert the date to a unix date
127                         my $d = cltounix($field[3], $field[4]);
128                         return if !$d || ($pcno == 11 && $d < $main::systime - $pc11_max_age); # bang out (and don't pass on) if date is invalid or the spot is too old
129                         
130                         # strip off the leading & trailing spaces from the comment
131                         my $text = unpad($field[5]);
132                         
133                         # store it away
134                         my $spotter = $field[6];
135                         $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
136                         
137                         # do some de-duping
138                         my $dupkey = "$field[1]$field[2]$d$text$field[6]";
139                         return if $dup{$dupkey};
140                         $dup{$dupkey} = $d;
141                         
142                         my $spot = Spot::add($field[1], $field[2], $d, $text, $spotter);
143                         
144                         # send orf to the users
145                         if ($spot && $pcno == 11) {
146                                 my $buf = Spot::formatb($field[1], $field[2], $d, $text, $spotter);
147                                 broadcast_users("$buf\a\a");
148                         }
149                         
150                         last SWITCH;
151                 }
152                 
153                 if ($pcno == 12) {              # announces
154                         
155                         if ($field[2] eq '*' || $field[2] eq $main::mycall) {
156                                 
157                                 # strip leading and trailing stuff
158                                 my $text = unpad($field[3]);
159                                 my $target;
160                                 my $to = 'To ';
161                                 my @list;
162                                 
163                                 if ($field[4] eq '*') { # sysops
164                                         $target = "Sysops";
165                                         @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
166                                 } elsif ($field[4] gt ' ') { # speciality list handling
167                                         my ($name) = split /\./, $field[4]; 
168                                         $target = "$name"; # put the rest in later (if bothered) 
169                                 } 
170                                 
171                                 if ($field[6] eq '1') {
172                                         $target = "WX"; 
173                                         $to = '';
174                                 }
175                                 $target = "All" if !$target;
176                                 
177                                 if (@list > 0) {
178                                         broadcast_list("$to$target de $field[1]: $text", @list);
179                                 } else {
180                                         broadcast_users("$target de $field[1]: $text");
181                                 }
182                                 Log('ann', $target, $field[1], $text);
183                                 
184                                 return if $field[2] eq $main::mycall; # it's routed to me
185                         } else {
186                                 route($field[2], $line);
187                                 return;                 # only on a routed one
188                         }
189                         
190                         last SWITCH;
191                 }
192                 
193                 if ($pcno == 13) {
194                         last SWITCH;
195                 }
196                 if ($pcno == 14) {
197                         last SWITCH;
198                 }
199                 if ($pcno == 15) {
200                         last SWITCH;
201                 }
202                 
203                 if ($pcno == 16) {              # add a user
204                         my $node = DXCluster->get_exact($field[1]); 
205                         last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
206                         my $i;
207                         
208                         for ($i = 2; $i < $#field; $i++) {
209                                 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (-) (\d)/o;
210                                 next if length $call < 3;
211                                 next if !$confmode;
212                                 $call = uc $call;
213                                 next if DXCluster->get_exact($call); # we already have this (loop?)
214                                 
215                                 $confmode = $confmode eq '*';
216                                 DXNodeuser->new($self, $node, $call, $confmode, $here);
217                                 
218                                 # add this station to the user database, if required
219                                 $call =~ s/-\d+$//o;        # remove ssid for users
220                                 my $user = DXUser->get_current($call);
221                                 $user = DXUser->new($call) if !$user;
222                                 $user->node($node->call);
223                                 $user->lastin($main::systime);
224                                 $user->homenode($node->call) if !$user->homenode;
225                                 $user->put;
226                         }
227                         
228                         # queue up any messages (look for privates only)
229                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
230                         last SWITCH;
231                 }
232                 
233                 if ($pcno == 17) {              # remove a user
234                         
235                         my $ref = DXCluster->get_exact($field[1]);
236                         $ref->del() if $ref;
237                         last SWITCH;
238                 }
239                 
240                 if ($pcno == 18) {              # link request
241                         $self->send_local_config();
242                         $self->send(pc20());
243                         $self->state('init');   
244                         last SWITCH;
245                 }
246                 
247                 if ($pcno == 19) {              # incoming cluster list
248                         my $i;
249                         for ($i = 1; $i < $#field-1; $i += 4) {
250                                 my $here = $field[$i];
251                                 my $call = uc $field[$i+1];
252                                 my $confmode = $field[$i+2] eq '*';
253                                 my $ver = $field[$i+3];
254                                 
255                                 # now check the call over
256                                 next if DXCluster->get_exact($call); # we already have this
257                                 
258                                 # check for sane parameters
259                                 next if $ver < 5000; # only works with version 5 software
260                                 next if length $call < 3; # min 3 letter callsigns
261                                 DXNode->new($self, $call, $confmode, $here, $ver);
262                                 
263                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
264                                 my $mref = DXMsg::get_busy($call);
265                                 $mref->stop_msg($self) if $mref;
266                                 
267                                 # add this station to the user database, if required (don't remove SSID from nodes)
268                                 my $user = DXUser->get_current($call);
269                                 if (!$user) {
270                                         $user = DXUser->new($call);
271                                         $user->sort('A');
272                                         $user->node($call);
273                                         $user->homenode($call);
274                                         $user->lastin($main::systime);
275                                         $user->put;
276                                 }
277                         }
278                         
279                         # queue up any messages
280                         DXMsg::queue_msg() if $self->state eq 'normal';     
281                         last SWITCH;
282                 }
283                 
284                 if ($pcno == 20) {              # send local configuration
285                         $self->send_local_config();
286                         $self->send(pc22());
287                         $self->state('normal');
288                         
289                         # queue mail
290                         DXMsg::queue_msg();
291                         return;
292                 }
293                 
294                 if ($pcno == 21) {              # delete a cluster from the list
295                         my $call = uc $field[1];
296                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
297                                 my $ref = DXCluster->get_exact($call);
298                                 $ref->del() if $ref;
299                         }
300                         last SWITCH;
301                 }
302                 
303                 if ($pcno == 22) {
304                         $self->state('normal');
305                         
306                         # queue mail
307                         DXMsg::queue_msg();
308                         return;
309                 }
310                 
311                 if ($pcno == 23 || $pcno == 27) { # WWV info
312                         Geomag::update(@field[1..$#field]);
313                         last SWITCH;
314                 }
315                 
316                 if ($pcno == 24) {              # set here status
317                         my $call = uc $field[1];
318                         my $ref = DXCluster->get_exact($call);
319                         $ref->here($field[2]) if $ref;
320                         last SWITCH;
321                 }
322                 
323                 if ($pcno == 25) {
324                         last SWITCH;
325                 }
326                 
327                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42) { # mail/file handling
328                         DXMsg::process($self, $line);
329                         return;
330                 }
331                 
332                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
333                         if ($field[1] eq $main::mycall) {
334                                 my $ref = DXUser->get_current($field[2]);
335                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
336                                 if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
337                                         $self->{remotecmd} = 1; # for the benefit of any command that needs to know
338                                         my @in = (DXCommandmode::run_cmd($self, $field[3]));
339                                         for (@in) {
340                                                 s/\s*$//og;
341                                                 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
342                                                 Log('rcmd', 'out', $field[2], $_);
343                                         }
344                                         delete $self->{remotecmd};
345                                 }
346                         } else {
347                                 route($field[1], $line);
348                         }
349                         return;
350                 }
351                 
352                 if ($pcno == 35) {              # remote command replies
353                         if ($field[1] eq $main::mycall) {
354                                 my $s = $rcmds{$field[2]};
355                                 if ($s) {
356                                         my $dxchan = DXChannel->get($s->{call});
357                                         $dxchan->send($field[3]) if $dxchan;
358                                         delete $rcmds{$field[2]} if !$dxchan;
359                                 }
360                         } else {
361                                 route($field[1], $line);
362                         }
363                         return;
364                 }
365                 
366                 if ($pcno == 37) {
367                         last SWITCH;
368                 }
369                 
370                 if ($pcno == 38) {              # node connected list from neighbour
371                         return;
372                 }
373                 
374                 if ($pcno == 39) {              # incoming disconnect
375                         $self->disconnect();
376                         return;
377                 }
378                 
379                 if ($pcno == 41) {              # user info
380                         # add this station to the user database, if required
381                         my $user = DXUser->get_current($field[1]);
382                         if (!$user) {
383                                 # then try without an SSID
384                                 $field[1] =~ s/-\d+$//o;
385                                 $user = DXUser->get_current($field[1]);
386                         }
387                         $user = DXUser->new($field[1]) if !$user;
388                         
389                         if ($field[2] == 1) {
390                                 $user->name($field[3]);
391                         } elsif ($field[2] == 2) {
392                                 $user->qth($field[3]);
393                         } elsif ($field[2] == 3) {
394                                 my ($lat, $long) = DXBearing::stoll($field[3]);
395                                 $user->lat($lat);
396                                 $user->long($long);
397                         } elsif ($field[2] == 4) {
398                                 $user->homenode($field[3]);
399                         }
400                         $user->put;
401                         last SWITCH;
402                 }
403                 if ($pcno == 43) {
404                         last SWITCH;
405                 }
406                 if ($pcno == 44) {
407                         last SWITCH;
408                 }
409                 if ($pcno == 45) {
410                         last SWITCH;
411                 }
412                 if ($pcno == 46) {
413                         last SWITCH;
414                 }
415                 if ($pcno == 47) {
416                         last SWITCH;
417                 }
418                 if ($pcno == 48) {
419                         last SWITCH;
420                 }
421                 if ($pcno == 49) {
422                         last SWITCH;
423                 }
424                 
425                 if ($pcno == 50) {              # keep alive/user list
426                         my $ref = DXCluster->get_exact($field[1]);
427                         $ref->update_users($field[2]) if $ref;
428                         last SWITCH;
429                 }
430                 
431                 if ($pcno == 51) {              # incoming ping requests/answers
432                         
433                         # is it for us?
434                         if ($field[1] eq $main::mycall) {
435                                 my $flag = $field[3];
436                                 if ($flag == 1) {
437                                         $self->send(pc51($field[2], $field[1], '0'));
438                                 } else {
439                                         # it's a reply, look in the ping list for this one
440                                         my $ref = $pings{$field[2]};
441                                         if ($ref) {
442                                                 my $r = shift @$ref;
443                                                 my $dxchan = DXChannel->get($r->{call});
444                                                 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
445                                         }
446                                 }
447                                 
448                         } else {
449                                 # route down an appropriate thingy
450                                 route($field[1], $line);
451                         }
452                         return;
453                 }
454         }
455          
456          # if get here then rebroadcast the thing with its Hop count decremented (if
457          # there is one). If it has a hop count and it decrements to zero then don't
458          # rebroadcast it.
459          #
460          # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
461          #        REBROADCAST!!!!
462          #
463          
464          my $hops;
465         if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
466                 my $newhops = $hops - 1;
467                 if ($newhops > 0) {
468                         $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/;     # change the hop count
469                         broadcast_ak1a($line, $self); # send it to everyone but me
470                 }
471         }
472 }
473
474 #
475 # This is called from inside the main cluster processing loop and is used
476 # for despatching commands that are doing some long processing job
477 #
478 sub process
479 {
480         my $t = time;
481         my @chan = DXChannel->get_all();
482         my $chan;
483         
484         foreach $chan (@chan) {
485                 next if !$chan->is_ak1a();
486                 
487                 # send a pc50 out on this channel
488                 if ($t >= $chan->pc50_t + $DXProt::pc50_interval) {
489                         $chan->send(pc50());
490                         $chan->pc50_t($t);
491                 }
492         }
493         
494         my $key;
495         my $val;
496         my $cutoff;
497         if ($main::systime - 3600 > $last_hour) {
498                 $cutoff  = $main::systime - $pc11_dup_age;
499                 while (($key, $val) = each %dup) {
500                         delete $dup{$key} if $val < $cutoff;
501                 }
502                 $last_hour = $main::systime;
503         }
504 }
505
506 #
507 # finish up a pc context
508 #
509 sub finish
510 {
511         my $self = shift;
512         my $call = $self->call;
513         my $ref = DXCluster->get_exact($call);
514         
515         # unbusy and stop and outgoing mail
516         my $mref = DXMsg::get_busy($call);
517         $mref->stop_msg($self) if $mref;
518         
519         # broadcast to all other nodes that all the nodes connected to via me are gone
520         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
521         my $node;
522         
523         foreach $node (@gonenodes) {
524                 next if $node->call eq $call; 
525                 broadcast_ak1a(pc21($node->call, 'Gone'), $self); # done like this 'cos DXNodes don't have a pc21 method
526                 $node->del();
527         }
528
529         # remove outstanding pings
530         delete $pings{$call};
531         
532         # now broadcast to all other ak1a nodes that I have gone
533         broadcast_ak1a(pc21($call, 'Gone.'), $self);
534         
535         Log('DXProt', $call . " Disconnected");
536         $ref->del() if $ref;
537 }
538
539 #
540 # some active measures
541 #
542
543 sub send_local_config
544 {
545         my $self = shift;
546         my $n;
547         
548         # send our nodes
549         my @nodes = DXNode::get_all();
550         
551         # create a list of all the nodes that are not connected to this connection
552         @nodes = grep { $_->dxchan != $self } @nodes;
553         $self->send($me->pc19(@nodes));
554         
555         # get all the users connected on the above nodes and send them out
556         foreach $n (@nodes) {
557                 my @users = values %{$n->list};
558                 $self->send(DXProt::pc16($n, @users));
559         }
560 }
561
562 #
563 # route a message down an appropriate interface for a callsign
564 #
565 # is called route(to, pcline);
566 #
567 sub route
568 {
569         my ($call, $line) = @_;
570         my $cl = DXCluster->get_exact($call);
571         if ($cl) {
572                 my $hops;
573                 my $dxchan = $cl->{dxchan};
574                 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
575                         my $newhops = $hops - 1;
576                         if ($newhops > 0) {
577                                 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/;     # change the hop count
578                                 $dxchan->send($line) if $dxchan;
579                         }
580                 } else {
581                         $dxchan->send($line) if $dxchan; # for them wot don't have Hops
582                 }
583         }
584 }
585
586 # broadcast a message to all clusters [except those mentioned after buffer]
587 sub broadcast_ak1a
588 {
589         my $s = shift;                          # the line to be rebroadcast
590         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
591         my @chan = get_all_ak1a();
592         my $chan;
593         
594         foreach $chan (@chan) {
595                 next if grep $chan == $_, @except;
596                 $chan->send($s);                # send it if it isn't the except list
597         }
598 }
599
600 # broadcast to all users
601 sub broadcast_users
602 {
603         my $s = shift;                          # the line to be rebroadcast
604         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
605         my @chan = get_all_users();
606         my $chan;
607         
608         foreach $chan (@chan) {
609                 next if grep $chan == $_, @except;
610                 $s =~ s/\a//og if !$chan->{beep};
611                 $chan->send($s);                # send it if it isn't the except list
612         }
613 }
614
615 # broadcast to a list of users
616 sub broadcast_list
617 {
618         my $s = shift;
619         my $chan;
620         
621         foreach $chan (@_) {
622                 $chan->send($s);                # send it 
623         }
624 }
625
626 #
627 # gimme all the ak1a nodes
628 #
629 sub get_all_ak1a
630 {
631         my @list = DXChannel->get_all();
632         my $ref;
633         my @out;
634         foreach $ref (@list) {
635                 push @out, $ref if $ref->is_ak1a;
636         }
637         return @out;
638 }
639
640 # return a list of all users
641 sub get_all_users
642 {
643         my @list = DXChannel->get_all();
644         my $ref;
645         my @out;
646         foreach $ref (@list) {
647                 push @out, $ref if $ref->is_user;
648         }
649         return @out;
650 }
651
652 # return a list of all user callsigns
653 sub get_all_user_calls
654 {
655         my @list = DXChannel->get_all();
656         my $ref;
657         my @out;
658         foreach $ref (@list) {
659                 push @out, $ref->call if $ref->is_user;
660         }
661         return @out;
662 }
663
664 #
665 # obtain the hops from the list for this callsign and pc no 
666 #
667
668 sub get_hops
669 {
670         my ($pcno) = @_;
671         my $hops = $DXProt::hopcount{$pcno};
672         $hops = $DXProt::def_hopcount if !$hops;
673         return "H$hops";       
674 }
675
676 # remove leading and trailing spaces from an input string
677 sub unpad
678 {
679         my $s = shift;
680         $s =~ s/^\s+|\s+$//;
681         return $s;
682 }
683
684 # add a ping request to the ping queues
685 sub addping
686 {
687         my ($from, $to) = @_;
688         my $ref = $pings{$to};
689         $ref = $pings{$to} = [] if !$ref;
690         my $r = {};
691         $r->{call} = $from;
692         $r->{t} = $main::systime;
693         route($to, pc51($to, $main::mycall, 1));
694         push @$ref, $r;
695 }
696
697 # add a rcmd request to the rcmd queues
698 sub addrcmd
699 {
700         my ($from, $to, $cmd) = @_;
701         my $r = {};
702         $r->{call} = $from;
703         $r->{t} = $main::systime;
704         $r->{cmd} = $cmd;
705         route($to, pc34($main::mycall, $to, $cmd));
706         $rcmds{$to} = $r;
707 }
708 1;
709 __END__