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