f825ebb87b772006f5eec0aa28c19aa5a30d3cdd
[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
27 my $me;            # the channel id for this cluster
28
29 sub init
30 {
31   my $user = DXUser->get($main::mycall);
32   $me = DXChannel::alloc('DXProt', $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 $date = $field[3];
107           my $time = $field[4];
108           $date =~ s/^\s*(\d+)-(\w\w\w)-(19\d\d)$/$1 $2 $3/;
109           $time =~ s/^(\d\d)(\d\d)Z$/$1:$2 +0000/;
110           my $d = str2time("$date $time");
111           return if !$d;               # bang out (and don't pass on) if date is invalid
112           
113           # strip off the leading & trailing spaces from the comment
114           my $text = unpad($field[5]);
115           
116           # store it away
117           Spot::add($field[1], $field[2], $d, $text, $field[6]);
118           
119           # format and broadcast it to users
120           my $spotter = $field[6];
121           $spotter =~ s/^(\w+)-\d+/$1/;    # strip off the ssid from the spotter
122       $spotter .= ':';                # add a colon
123           
124           # send orf to the users
125           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];
126       broadcast_users($buf);
127           
128           last SWITCH;
129         }
130         
131     if ($pcno == 12) {             # announces
132         
133           if ($field[2] eq '*' || $field[2] eq $main::mycall) {
134
135         # strip leading and trailing stuff
136             my $text = unpad($field[3]);
137                 my $target = "To Sysops" if $field[4] eq '*';
138                 $target = "WX" if $field[6];
139                 $target = "To All" if !$target;
140                 broadcast_users("$target de $field[1]: $text"); 
141                 
142                 return if $field[2] eq $main::mycall;   # it's routed to me
143           } else {
144             route($field[2], $line);
145                 return;                     # only on a routed one
146           }
147           
148           last SWITCH;
149         }
150         
151     if ($pcno == 13) {last SWITCH;}
152     if ($pcno == 14) {last SWITCH;}
153     if ($pcno == 15) {last SWITCH;}
154         
155     if ($pcno == 16) {              # add a user
156           my $node = DXCluster->get($field[1]);
157           last SWITCH if !$node;        # ignore if havn't seen a PC19 for this one yet
158           my $i;
159           
160           for ($i = 2; $i < $#field-1; $i++) {
161             my ($call, $confmode, $here) = $field[$i] =~ /^(\w+) (-) (\d)/o;
162                 next if length $call < 3;
163                 next if !$confmode;
164         $call =~ s/^(\w+)-\d+/$1/;        # remove ssid
165                 next if DXCluster->get($call);    # we already have this (loop?)
166                 
167                 $confmode = $confmode eq '*';
168                 DXNodeuser->new($self, $node, $call, $confmode, $here);
169           }
170           last SWITCH;
171         }
172         
173     if ($pcno == 17) {              # remove a user
174           my $ref = DXCluster->get($field[1]);
175           $ref->del() if $ref;
176           last SWITCH;
177         }
178         
179     if ($pcno == 18) {              # link request
180         
181       # send our nodes
182           my $hops = get_hops(19);
183           $self->send($me->pc19(get_all_ak1a()));
184           
185       # get all the local users and send them out
186           $self->send($me->pc16(get_all_users()));
187           $self->send(pc20());
188           last SWITCH;
189         }
190         
191     if ($pcno == 19) {               # incoming cluster list
192       my $i;
193           for ($i = 1; $i < $#field-1; $i += 4) {
194             my $here = $field[$i];
195             my $call = $field[$i+1];
196                 my $confmode = $field[$i+2] eq '*';
197                 my $ver = $field[$i+3];
198                 
199                 # now check the call over
200                 next if DXCluster->get($call);   # we already have this
201                 
202                 # check for sane parameters
203                 next if $ver < 5000;             # only works with version 5 software
204                 next if length $call < 3;        # min 3 letter callsigns
205         DXNode->new($self, $call, $confmode, $here, $ver);
206           }
207           last SWITCH;
208         }
209         
210     if ($pcno == 20) {              # send local configuration
211
212       # send our nodes
213           my $hops = get_hops(19);
214           $self->send($me->pc19(get_all_ak1a()));
215           
216       # get all the local users and send them out
217           $self->send($me->pc16(get_all_users()));
218           $self->send(pc22());
219           return;
220         }
221         
222     if ($pcno == 21) {             # delete a cluster from the list
223           my $ref = DXCluster->get($field[1]);
224           $ref->del() if $ref;
225           last SWITCH;
226         }
227         
228     if ($pcno == 22) {last SWITCH;}
229     if ($pcno == 23) {last SWITCH;}
230     if ($pcno == 24) {last SWITCH;}
231     if ($pcno == 25) {last SWITCH;}
232     if ($pcno == 26) {last SWITCH;}
233     if ($pcno == 27) {last SWITCH;}
234     if ($pcno == 28) {last SWITCH;}
235     if ($pcno == 29) {last SWITCH;}
236     if ($pcno == 30) {last SWITCH;}
237     if ($pcno == 31) {last SWITCH;}
238     if ($pcno == 32) {last SWITCH;}
239     if ($pcno == 33) {last SWITCH;}
240     if ($pcno == 34) {last SWITCH;}
241     if ($pcno == 35) {last SWITCH;}
242     if ($pcno == 36) {last SWITCH;}
243     if ($pcno == 37) {last SWITCH;}
244     if ($pcno == 38) {last SWITCH;}
245     if ($pcno == 39) {last SWITCH;}
246     if ($pcno == 40) {last SWITCH;}
247     if ($pcno == 41) {last SWITCH;}
248     if ($pcno == 42) {last SWITCH;}
249     if ($pcno == 43) {last SWITCH;}
250     if ($pcno == 44) {last SWITCH;}
251     if ($pcno == 45) {last SWITCH;}
252     if ($pcno == 46) {last SWITCH;}
253     if ($pcno == 47) {last SWITCH;}
254     if ($pcno == 48) {last SWITCH;}
255     if ($pcno == 49) {last SWITCH;}
256         
257     if ($pcno == 50) {              # keep alive/user list
258           my $ref = DXCluster->get($field[1]);
259           $ref->update_users($field[2]) if $ref;
260           last SWITCH;
261         }
262         
263     if ($pcno == 51) {              # incoming ping requests/answers
264           
265           # is it for us?
266           if ($field[1] eq $main::mycall) {
267             my $flag = $field[3];
268             $flag ^= 1;
269             $self->send($self->pc51($field[2], $field[1], $flag));
270           } else {
271             # route down an appropriate thingy
272                 route($field[1], $line);
273           }
274           return;
275         }
276   }
277   
278   # if get here then rebroadcast the thing with its Hop count decremented (if
279   # there is one). If it has a hop count and it decrements to zero then don't
280   # rebroadcast it.
281   #
282   # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
283   #        REBROADCAST!!!!
284   #
285   
286   my $hopfield = pop @field;
287   push @field, $hopfield; 
288   
289   my $hops;
290   if (($hops) = $hopfield =~ /H(\d+)\^\~?$/o) {
291         my $newhops = $hops - 1;
292         if ($newhops > 0) {
293           $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/;       # change the hop count
294           broadcast_ak1a($line, $self);             # send it to everyone but me
295         }
296   }
297 }
298
299 #
300 # This is called from inside the main cluster processing loop and is used
301 # for despatching commands that are doing some long processing job
302 #
303 sub process
304 {
305   my $t = time;
306   my @chan = DXChannel->get_all();
307   my $chan;
308   
309   foreach $chan (@chan) {
310     next if !$chan->is_ak1a();
311
312     # send a pc50 out on this channel
313     if ($t >= $chan->pc50_t + $DXProt::pc50_interval) {
314       $chan->send(pc50());
315           $chan->pc50_t($t);
316         }
317   }
318 }
319
320 #
321 # finish up a pc context
322 #
323 sub finish
324 {
325   my $self = shift;
326   broadcast_ak1a($self->pc21('Gone.'));
327   $self->delnode();
328 }
329  
330
331 # add a (local) user to the cluster
332 #
333
334 sub adduser
335 {
336   DXNodeuser->add(@_);
337 }
338
339 #
340 # delete a (local) user to the cluster
341 #
342
343 sub deluser
344 {
345   my $self = shift;
346   my $ref = DXCluster->get($self->call);
347   $ref->del() if $ref;
348 }
349
350 #
351 # add a (locally connected) node to the cluster
352 #
353
354 sub addnode
355 {
356   DXNode->new(@_);
357 }
358
359 #
360 # delete a (locally connected) node to the cluster
361 #
362 sub delnode
363 {
364   my $self = shift;
365   my $ref = DXCluster->get($self->call);
366   $ref->del() if $ref;
367 }
368
369 #
370 # some active measures
371 #
372
373 #
374 # route a message down an appropriate interface for a callsign
375 #
376 # is called route(to, pcline);
377 #
378 sub route
379 {
380   my ($call, $line) = @_;
381   my $cl = DXCluster->get($call);
382   if ($cl) {
383     my $dxchan = $cl->{dxchan};
384     $cl->send($line) if $dxchan;
385   }
386 }
387
388 # broadcast a message to all clusters [except those mentioned after buffer]
389 sub broadcast_ak1a
390 {
391   my $s = shift;                  # the line to be rebroadcast
392   my @except = @_;                # to all channels EXCEPT these (dxchannel refs)
393   my @chan = get_all_ak1a();
394   my $chan;
395   
396   foreach $chan (@chan) {
397          $chan->send($s) if !grep $chan, @except;              # send it if it isn't the except list
398   }
399 }
400
401 # broadcast to all users
402 sub broadcast_users
403 {
404   my $s = shift;                  # the line to be rebroadcast
405   my @except = @_;                # to all channels EXCEPT these (dxchannel refs)
406   my @chan = get_all_users();
407   my $chan;
408   
409   foreach $chan (@chan) {
410          $chan->send($s) if !grep $chan, @except;              # send it if it isn't the except list
411   }
412 }
413
414 #
415 # gimme all the ak1a nodes
416 #
417 sub get_all_ak1a
418 {
419   my @list = DXChannel->get_all();
420   my $ref;
421   my @out;
422   foreach $ref (@list) {
423     push @out, $ref if $ref->is_ak1a;
424   }
425   return @out;
426 }
427
428 # return a list of all users
429 sub get_all_users
430 {
431   my @list = DXChannel->get_all();
432   my $ref;
433   my @out;
434   foreach $ref (@list) {
435     push @out, $ref if $ref->is_user;
436   }
437   return @out;
438 }
439
440 # return a list of all user callsigns
441 sub get_all_user_calls
442 {
443   my @list = DXChannel->get_all();
444   my $ref;
445   my @out;
446   foreach $ref (@list) {
447     push @out, $ref->call if $ref->is_user;
448   }
449   return @out;
450 }
451
452 #
453 # obtain the hops from the list for this callsign and pc no 
454 #
455
456 sub get_hops
457 {
458   my ($pcno) = @_;
459   my $hops = $DXProt::hopcount{$pcno};
460   $hops = $DXProt::def_hopcount if !$hops;
461   return "H$hops";       
462 }
463
464 # remove leading and trailing spaces from an input string
465 sub unpad
466 {
467   my $s = shift;
468   $s =~ s/^\s+|\s+$//;
469   return $s;
470 }
471 1;
472 __END__