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