9831e3965c4d47ee17797b550772ee1d6cf5d9ee
[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                         
209                         for ($i = 2; $i < $#field; $i++) {
210                                 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (-) (\d)/o;
211                                 next if length $call < 3;
212                                 next if !$confmode;
213                                 $call = uc $call;
214                                 next if DXCluster->get_exact($call); # we already have this (loop?)
215                                 
216                                 $confmode = $confmode eq '*';
217                                 DXNodeuser->new($self, $node, $call, $confmode, $here);
218                                 
219                                 # add this station to the user database, if required
220                                 $call =~ s/-\d+$//o;        # remove ssid for users
221                                 my $user = DXUser->get_current($call);
222                                 $user = DXUser->new($call) if !$user;
223                                 $user->homenode($node->call) if !$user->homenode;
224                                 $user->node($node->call);
225                                 $user->lastin($main::systime);
226                                 $user->put;
227                         }
228                         
229                         # queue up any messages (look for privates only)
230                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
231                         last SWITCH;
232                 }
233                 
234                 if ($pcno == 17) {              # remove a user
235                         
236                         my $ref = DXCluster->get_exact($field[1]);
237                         $ref->del() if $ref;
238                         last SWITCH;
239                 }
240                 
241                 if ($pcno == 18) {              # link request
242                         $self->send_local_config();
243                         $self->send(pc20());
244                         $self->state('init');   
245                         last SWITCH;
246                 }
247                 
248                 if ($pcno == 19) {              # incoming cluster list
249                         my $i;
250                         for ($i = 1; $i < $#field-1; $i += 4) {
251                                 my $here = $field[$i];
252                                 my $call = uc $field[$i+1];
253                                 my $confmode = $field[$i+2] eq '*';
254                                 my $ver = $field[$i+3];
255                                 
256                                 # now check the call over
257                                 next if DXCluster->get_exact($call); # we already have this
258                                 
259                                 # check for sane parameters
260                                 next if $ver < 5000; # only works with version 5 software
261                                 next if length $call < 3; # min 3 letter callsigns
262                                 DXNode->new($self, $call, $confmode, $here, $ver);
263                                 
264                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
265                                 my $mref = DXMsg::get_busy($call);
266                                 $mref->stop_msg($self) if $mref;
267                                 
268                                 # add this station to the user database, if required (don't remove SSID from nodes)
269                                 my $user = DXUser->get_current($call);
270                                 if (!$user) {
271                                         $user = DXUser->new($call);
272                                         $user->sort('A');
273                                         $user->homenode($call);
274                                         $user->node($call);
275                                 }
276                                 $user->lastin($main::systime);
277                                 $user->put;
278                         }
279                         
280                         # queue up any messages
281                         DXMsg::queue_msg() if $self->state eq 'normal';     
282                         last SWITCH;
283                 }
284                 
285                 if ($pcno == 20) {              # send local configuration
286                         $self->send_local_config();
287                         $self->send(pc22());
288                         $self->state('normal');
289                         
290                         # queue mail
291                         DXMsg::queue_msg();
292                         return;
293                 }
294                 
295                 if ($pcno == 21) {              # delete a cluster from the list
296                         my $call = uc $field[1];
297                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
298                                 my $ref = DXCluster->get_exact($call);
299                                 $ref->del() if $ref;
300                         }
301                         last SWITCH;
302                 }
303                 
304                 if ($pcno == 22) {
305                         $self->state('normal');
306                         
307                         # queue mail
308                         DXMsg::queue_msg();
309                         return;
310                 }
311                 
312                 if ($pcno == 23 || $pcno == 27) { # WWV info
313                         Geomag::update(@field[1..$#field]);
314                         last SWITCH;
315                 }
316                 
317                 if ($pcno == 24) {              # set here status
318                         my $call = uc $field[1];
319                         my $ref = DXCluster->get_exact($call);
320                         $ref->here($field[2]) if $ref;
321                         last SWITCH;
322                 }
323                 
324                 if ($pcno == 25) {
325                         last SWITCH;
326                 }
327                 
328                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42) { # mail/file handling
329                         DXMsg::process($self, $line);
330                         return;
331                 }
332                 
333                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
334                         if ($field[1] eq $main::mycall) {
335                                 my $ref = DXUser->get_current($field[2]);
336                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
337                                 if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
338                                         $self->{remotecmd} = 1; # for the benefit of any command that needs to know
339                                         my @in = (DXCommandmode::run_cmd($self, $field[3]));
340                                         for (@in) {
341                                                 s/\s*$//og;
342                                                 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
343                                                 Log('rcmd', 'out', $field[2], $_);
344                                         }
345                                         delete $self->{remotecmd};
346                                 }
347                         } else {
348                                 route($field[1], $line);
349                         }
350                         return;
351                 }
352                 
353                 if ($pcno == 35) {              # remote command replies
354                         if ($field[1] eq $main::mycall) {
355                                 my $s = $rcmds{$field[2]};
356                                 if ($s) {
357                                         my $dxchan = DXChannel->get($s->{call});
358                                         $dxchan->send($field[3]) if $dxchan;
359                                         delete $rcmds{$field[2]} if !$dxchan;
360                                 }
361                         } else {
362                                 route($field[1], $line);
363                         }
364                         return;
365                 }
366                 
367                 if ($pcno == 37) {
368                         last SWITCH;
369                 }
370                 
371                 if ($pcno == 38) {              # node connected list from neighbour
372                         return;
373                 }
374                 
375                 if ($pcno == 39) {              # incoming disconnect
376                         $self->disconnect();
377                         return;
378                 }
379                 
380                 if ($pcno == 41) {              # user info
381                         # add this station to the user database, if required
382                         my $user = DXUser->get_current($field[1]);
383                         if (!$user) {
384                                 # then try without an SSID
385                                 $field[1] =~ s/-\d+$//o;
386                                 $user = DXUser->get_current($field[1]);
387                         }
388                         $user = DXUser->new($field[1]) if !$user;
389                         
390                         if ($field[2] == 1) {
391                                 $user->name($field[3]);
392                         } elsif ($field[2] == 2) {
393                                 $user->qth($field[3]);
394                         } elsif ($field[2] == 3) {
395                                 my ($lat, $long) = DXBearing::stoll($field[3]);
396                                 $user->lat($lat);
397                                 $user->long($long);
398                         } elsif ($field[2] == 4) {
399                                 $user->homenode($field[3]);
400                         }
401                         $user->put;
402                         last SWITCH;
403                 }
404                 if ($pcno == 43) {
405                         last SWITCH;
406                 }
407                 if ($pcno == 44) {
408                         last SWITCH;
409                 }
410                 if ($pcno == 45) {
411                         last SWITCH;
412                 }
413                 if ($pcno == 46) {
414                         last SWITCH;
415                 }
416                 if ($pcno == 47) {
417                         last SWITCH;
418                 }
419                 if ($pcno == 48) {
420                         last SWITCH;
421                 }
422                 if ($pcno == 49) {
423                         last SWITCH;
424                 }
425                 
426                 if ($pcno == 50) {              # keep alive/user list
427                         my $ref = DXCluster->get_exact($field[1]);
428                         $ref->update_users($field[2]) if $ref;
429                         last SWITCH;
430                 }
431                 
432                 if ($pcno == 51) {              # incoming ping requests/answers
433                         
434                         # is it for us?
435                         if ($field[1] eq $main::mycall) {
436                                 my $flag = $field[3];
437                                 if ($flag == 1) {
438                                         $self->send(pc51($field[2], $field[1], '0'));
439                                 } else {
440                                         # it's a reply, look in the ping list for this one
441                                         my $ref = $pings{$field[2]};
442                                         if ($ref) {
443                                                 my $r = shift @$ref;
444                                                 my $dxchan = DXChannel->get($r->{call});
445                                                 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
446                                         }
447                                 }
448                                 
449                         } else {
450                                 # route down an appropriate thingy
451                                 route($field[1], $line);
452                         }
453                         return;
454                 }
455         }
456          
457          # if get here then rebroadcast the thing with its Hop count decremented (if
458          # there is one). If it has a hop count and it decrements to zero then don't
459          # rebroadcast it.
460          #
461          # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
462          #        REBROADCAST!!!!
463          #
464          
465          my $hops;
466         if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
467                 my $newhops = $hops - 1;
468                 if ($newhops > 0) {
469                         $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/;     # change the hop count
470                         broadcast_ak1a($line, $self); # send it to everyone but me
471                 }
472         }
473 }
474
475 #
476 # This is called from inside the main cluster processing loop and is used
477 # for despatching commands that are doing some long processing job
478 #
479 sub process
480 {
481         my $t = time;
482         my @chan = DXChannel->get_all();
483         my $chan;
484         
485         foreach $chan (@chan) {
486                 next if !$chan->is_ak1a();
487                 
488                 # send a pc50 out on this channel
489                 if ($t >= $chan->pc50_t + $DXProt::pc50_interval) {
490                         $chan->send(pc50());
491                         $chan->pc50_t($t);
492                 }
493         }
494         
495         my $key;
496         my $val;
497         my $cutoff;
498         if ($main::systime - 3600 > $last_hour) {
499                 $cutoff  = $main::systime - $pc11_dup_age;
500                 while (($key, $val) = each %dup) {
501                         delete $dup{$key} if $val < $cutoff;
502                 }
503                 $last_hour = $main::systime;
504         }
505 }
506
507 #
508 # finish up a pc context
509 #
510 sub finish
511 {
512         my $self = shift;
513         my $call = $self->call;
514         my $ref = DXCluster->get_exact($call);
515         
516         # unbusy and stop and outgoing mail
517         my $mref = DXMsg::get_busy($call);
518         $mref->stop_msg($self) if $mref;
519         
520         # broadcast to all other nodes that all the nodes connected to via me are gone
521         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
522         my $node;
523         
524         foreach $node (@gonenodes) {
525                 next if $node->call eq $call; 
526                 broadcast_ak1a(pc21($node->call, 'Gone'), $self); # done like this 'cos DXNodes don't have a pc21 method
527                 $node->del();
528         }
529
530         # remove outstanding pings
531         delete $pings{$call};
532         
533         # now broadcast to all other ak1a nodes that I have gone
534         broadcast_ak1a(pc21($call, 'Gone.'), $self);
535         
536         Log('DXProt', $call . " Disconnected");
537         $ref->del() if $ref;
538 }
539
540 #
541 # some active measures
542 #
543
544 sub send_local_config
545 {
546         my $self = shift;
547         my $n;
548         
549         # send our nodes
550         my @nodes = DXNode::get_all();
551         
552         # create a list of all the nodes that are not connected to this connection
553         @nodes = grep { $_->dxchan != $self } @nodes;
554         $self->send($me->pc19(@nodes));
555         
556         # get all the users connected on the above nodes and send them out
557         foreach $n (@nodes) {
558                 my @users = values %{$n->list};
559                 $self->send(DXProt::pc16($n, @users));
560         }
561 }
562
563 #
564 # route a message down an appropriate interface for a callsign
565 #
566 # is called route(to, pcline);
567 #
568 sub route
569 {
570         my ($call, $line) = @_;
571         my $cl = DXCluster->get_exact($call);
572         if ($cl) {
573                 my $hops;
574                 my $dxchan = $cl->{dxchan};
575                 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
576                         my $newhops = $hops - 1;
577                         if ($newhops > 0) {
578                                 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/;     # change the hop count
579                                 $dxchan->send($line) if $dxchan;
580                         }
581                 } else {
582                         $dxchan->send($line) if $dxchan; # for them wot don't have Hops
583                 }
584         }
585 }
586
587 # broadcast a message to all clusters [except those mentioned after buffer]
588 sub broadcast_ak1a
589 {
590         my $s = shift;                          # the line to be rebroadcast
591         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
592         my @chan = get_all_ak1a();
593         my $chan;
594         
595         foreach $chan (@chan) {
596                 next if grep $chan == $_, @except;
597                 $chan->send($s);                # send it if it isn't the except list
598         }
599 }
600
601 # broadcast to all users
602 sub broadcast_users
603 {
604         my $s = shift;                          # the line to be rebroadcast
605         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
606         my @chan = get_all_users();
607         my $chan;
608         
609         foreach $chan (@chan) {
610                 next if grep $chan == $_, @except;
611                 $s =~ s/\a//og if !$chan->{beep};
612                 $chan->send($s);                # send it if it isn't the except list
613         }
614 }
615
616 # broadcast to a list of users
617 sub broadcast_list
618 {
619         my $s = shift;
620         my $chan;
621         
622         foreach $chan (@_) {
623                 $chan->send($s);                # send it 
624         }
625 }
626
627 #
628 # gimme all the ak1a nodes
629 #
630 sub get_all_ak1a
631 {
632         my @list = DXChannel->get_all();
633         my $ref;
634         my @out;
635         foreach $ref (@list) {
636                 push @out, $ref if $ref->is_ak1a;
637         }
638         return @out;
639 }
640
641 # return a list of all users
642 sub get_all_users
643 {
644         my @list = DXChannel->get_all();
645         my $ref;
646         my @out;
647         foreach $ref (@list) {
648                 push @out, $ref if $ref->is_user;
649         }
650         return @out;
651 }
652
653 # return a list of all user callsigns
654 sub get_all_user_calls
655 {
656         my @list = DXChannel->get_all();
657         my $ref;
658         my @out;
659         foreach $ref (@list) {
660                 push @out, $ref->call if $ref->is_user;
661         }
662         return @out;
663 }
664
665 #
666 # obtain the hops from the list for this callsign and pc no 
667 #
668
669 sub get_hops
670 {
671         my ($pcno) = @_;
672         my $hops = $DXProt::hopcount{$pcno};
673         $hops = $DXProt::def_hopcount if !$hops;
674         return "H$hops";       
675 }
676
677 # remove leading and trailing spaces from an input string
678 sub unpad
679 {
680         my $s = shift;
681         $s =~ s/^\s+|\s+$//;
682         return $s;
683 }
684
685 # add a ping request to the ping queues
686 sub addping
687 {
688         my ($from, $to) = @_;
689         my $ref = $pings{$to};
690         $ref = $pings{$to} = [] if !$ref;
691         my $r = {};
692         $r->{call} = $from;
693         $r->{t} = $main::systime;
694         route($to, pc51($to, $main::mycall, 1));
695         push @$ref, $r;
696 }
697
698 # add a rcmd request to the rcmd queues
699 sub addrcmd
700 {
701         my ($from, $to, $cmd) = @_;
702         my $r = {};
703         $r->{call} = $from;
704         $r->{t} = $main::systime;
705         $r->{cmd} = $cmd;
706         route($to, pc34($main::mycall, $to, $cmd));
707         $rcmds{$to} = $r;
708 }
709 1;
710 __END__