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