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